Debian Codegraph @

closure-library/closure/goog/testing/events/onlinehandler_test.js

raw
1 // Copyright 2012 The Closure Library Authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS-IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 goog.provide('goog.testing.events.OnlineHandlerTest');
16 goog.setTestOnly('goog.testing.events.OnlineHandlerTest');
17
18 goog.require('goog.events');
19 goog.require('goog.net.NetworkStatusMonitor');
20 goog.require('goog.testing.events.EventObserver');
21 goog.require('goog.testing.events.OnlineHandler');
22 goog.require('goog.testing.jsunit');
23
24 var handler;
25
26 var observer;
27
28 function tearDown() {
29 handler = null;
30 observer = null;
31 }
32
33 function testInitialValue() {
34 createHandler(true);
35 assertEquals(true, handler.isOnline());
36 createHandler(false);
37 assertEquals(false, handler.isOnline());
38 }
39
40 function testStateChange() {
41 createHandler(true);
42 assertEventCounts(
43 0 /* expectedOnlineEvents */, 0 /* expectedOfflineEvents */);
44
45 // Expect no events.
46 handler.setOnline(true);
47 assertEquals(true, handler.isOnline());
48 assertEventCounts(
49 0 /* expectedOnlineEvents */, 0 /* expectedOfflineEvents */);
50
51 // Expect one offline event.
52 handler.setOnline(false);
53 assertEquals(false, handler.isOnline());
54 assertEventCounts(
55 0 /* expectedOnlineEvents */, 1 /* expectedOfflineEvents */);
56
57 // Expect no events.
58 handler.setOnline(false);
59 assertEquals(false, handler.isOnline());
60 assertEventCounts(
61 0 /* expectedOnlineEvents */, 1 /* expectedOfflineEvents */);
62
63 // Expect one online event.
64 handler.setOnline(true);
65 assertEquals(true, handler.isOnline());
66 assertEventCounts(
67 1 /* expectedOnlineEvents */, 1 /* expectedOfflineEvents */);
68 }
69
70 function createHandler(initialValue) {
71 handler = new goog.testing.events.OnlineHandler(initialValue);
72 observer = new goog.testing.events.EventObserver();
73 goog.events.listen(
74 handler,
75 [
76 goog.net.NetworkStatusMonitor.EventType.ONLINE,
77 goog.net.NetworkStatusMonitor.EventType.OFFLINE
78 ],
79 observer);
80 }
81
82 function assertEventCounts(expectedOnlineEvents, expectedOfflineEvents) {
83 assertEquals(
84 expectedOnlineEvents,
85 observer.getEvents(goog.net.NetworkStatusMonitor.EventType.ONLINE)
86 .length);
87 assertEquals(
88 expectedOfflineEvents,
89 observer.getEvents(goog.net.NetworkStatusMonitor.EventType.OFFLINE)
90 .length);
91 }