Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(8)

Side by Side Diff: third_party/WebKit/Source/core/page/NetworkStateNotifierTest.cpp

Issue 2710023006: Move NetworkStateNotifier from core (and web) into platform/network (Closed)
Patch Set: rebase Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 /*
2 * Copyright (c) 2014, Google Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 *
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
13 * distribution.
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 #include "core/page/NetworkStateNotifier.h"
32
33 #include "core/dom/Document.h"
34 #include "core/dom/TaskRunnerHelper.h"
35 #include "platform/testing/UnitTestHelpers.h"
36 #include "public/platform/Platform.h"
37 #include "public/platform/WebConnectionType.h"
38 #include "public/platform/WebThread.h"
39 #include "testing/gtest/include/gtest/gtest.h"
40 #include "wtf/Functional.h"
41
42 namespace blink {
43
44 namespace {
45 const double kNoneMaxBandwidthMbps = 0.0;
46 const double kBluetoothMaxBandwidthMbps = 1.0;
47 const double kEthernetMaxBandwidthMbps = 2.0;
48 }
49
50 class StateObserver : public NetworkStateNotifier::NetworkStateObserver {
51 public:
52 StateObserver()
53 : m_observedType(WebConnectionTypeNone),
54 m_observedMaxBandwidthMbps(0.0),
55 m_observedOnLineState(false),
56 m_callbackCount(0) {}
57
58 virtual void connectionChange(WebConnectionType type,
59 double maxBandwidthMbps) {
60 m_observedType = type;
61 m_observedMaxBandwidthMbps = maxBandwidthMbps;
62 m_callbackCount += 1;
63
64 if (m_closure)
65 (*m_closure)();
66 }
67
68 virtual void onLineStateChange(bool onLine) {
69 m_observedOnLineState = onLine;
70 m_callbackCount += 1;
71
72 if (m_closure)
73 (*m_closure)();
74 }
75
76 WebConnectionType observedType() const { return m_observedType; }
77 double observedMaxBandwidth() const { return m_observedMaxBandwidthMbps; }
78 bool observedOnLineState() const { return m_observedOnLineState; }
79 int callbackCount() const { return m_callbackCount; }
80
81 void setNotificationCallback(std::unique_ptr<WTF::Closure> closure) {
82 m_closure = std::move(closure);
83 }
84
85 private:
86 std::unique_ptr<WTF::Closure> m_closure;
87 WebConnectionType m_observedType;
88 double m_observedMaxBandwidthMbps;
89 bool m_observedOnLineState;
90 int m_callbackCount;
91 };
92
93 class NetworkStateNotifierTest : public ::testing::Test {
94 public:
95 NetworkStateNotifierTest()
96 : m_document(Document::create()), m_document2(Document::create()) {
97 // Initialize connection, so that future calls to setWebConnection issue
98 // notifications.
99 m_notifier.setWebConnection(WebConnectionTypeUnknown, 0.0);
100 m_notifier.setOnLine(false);
101 }
102
103 WebTaskRunner* getTaskRunner() {
104 return TaskRunnerHelper::get(TaskType::Networking, m_document.get()).get();
105 }
106
107 WebTaskRunner* getTaskRunner2() {
108 return TaskRunnerHelper::get(TaskType::Networking, m_document2.get()).get();
109 }
110
111 protected:
112 void setConnection(WebConnectionType type, double maxBandwidthMbps) {
113 m_notifier.setWebConnection(type, maxBandwidthMbps);
114 testing::runPendingTasks();
115 }
116 void setOnLine(bool onLine) {
117 m_notifier.setOnLine(onLine);
118 testing::runPendingTasks();
119 }
120
121 void addObserverOnNotification(StateObserver* observer,
122 StateObserver* observerToAdd) {
123 observer->setNotificationCallback(
124 bind(&NetworkStateNotifier::addConnectionObserver,
125 WTF::unretained(&m_notifier), WTF::unretained(observerToAdd),
126 WTF::unretained(getTaskRunner())));
127 }
128
129 void removeObserverOnNotification(StateObserver* observer,
130 StateObserver* observerToRemove) {
131 observer->setNotificationCallback(
132 bind(&NetworkStateNotifier::removeConnectionObserver,
133 WTF::unretained(&m_notifier), WTF::unretained(observerToRemove),
134 WTF::unretained(getTaskRunner())));
135 }
136
137 bool verifyObservations(const StateObserver& observer,
138 WebConnectionType type,
139 double maxBandwidthMbps) {
140 EXPECT_EQ(observer.observedType(), type);
141 EXPECT_EQ(observer.observedMaxBandwidth(), maxBandwidthMbps);
142 return observer.observedType() == type &&
143 observer.observedMaxBandwidth() == maxBandwidthMbps;
144 }
145
146 Persistent<Document> m_document;
147 Persistent<Document> m_document2;
148 NetworkStateNotifier m_notifier;
149 };
150
151 TEST_F(NetworkStateNotifierTest, AddObserver) {
152 StateObserver observer;
153 m_notifier.addConnectionObserver(&observer, getTaskRunner());
154 EXPECT_TRUE(verifyObservations(observer, WebConnectionTypeNone,
155 kNoneMaxBandwidthMbps));
156
157 setConnection(WebConnectionTypeBluetooth, kBluetoothMaxBandwidthMbps);
158 EXPECT_TRUE(verifyObservations(observer, WebConnectionTypeBluetooth,
159 kBluetoothMaxBandwidthMbps));
160 EXPECT_EQ(observer.callbackCount(), 1);
161 }
162
163 TEST_F(NetworkStateNotifierTest, RemoveObserver) {
164 StateObserver observer1, observer2;
165 m_notifier.addConnectionObserver(&observer1, getTaskRunner());
166 m_notifier.removeConnectionObserver(&observer1, getTaskRunner());
167 m_notifier.addConnectionObserver(&observer2, getTaskRunner());
168
169 setConnection(WebConnectionTypeBluetooth, kBluetoothMaxBandwidthMbps);
170 EXPECT_TRUE(verifyObservations(observer1, WebConnectionTypeNone,
171 kNoneMaxBandwidthMbps));
172 EXPECT_TRUE(verifyObservations(observer2, WebConnectionTypeBluetooth,
173 kBluetoothMaxBandwidthMbps));
174 }
175
176 TEST_F(NetworkStateNotifierTest, RemoveSoleObserver) {
177 StateObserver observer1;
178 m_notifier.addConnectionObserver(&observer1, getTaskRunner());
179 m_notifier.removeConnectionObserver(&observer1, getTaskRunner());
180
181 setConnection(WebConnectionTypeBluetooth, kBluetoothMaxBandwidthMbps);
182 EXPECT_TRUE(verifyObservations(observer1, WebConnectionTypeNone,
183 kNoneMaxBandwidthMbps));
184 }
185
186 TEST_F(NetworkStateNotifierTest, AddObserverWhileNotifying) {
187 StateObserver observer1, observer2;
188 m_notifier.addConnectionObserver(&observer1, getTaskRunner());
189 addObserverOnNotification(&observer1, &observer2);
190
191 setConnection(WebConnectionTypeBluetooth, kBluetoothMaxBandwidthMbps);
192 EXPECT_TRUE(verifyObservations(observer1, WebConnectionTypeBluetooth,
193 kBluetoothMaxBandwidthMbps));
194 EXPECT_TRUE(verifyObservations(observer2, WebConnectionTypeBluetooth,
195 kBluetoothMaxBandwidthMbps));
196 }
197
198 TEST_F(NetworkStateNotifierTest, RemoveSoleObserverWhileNotifying) {
199 StateObserver observer1;
200 m_notifier.addConnectionObserver(&observer1, getTaskRunner());
201 removeObserverOnNotification(&observer1, &observer1);
202
203 setConnection(WebConnectionTypeBluetooth, kBluetoothMaxBandwidthMbps);
204 EXPECT_TRUE(verifyObservations(observer1, WebConnectionTypeBluetooth,
205 kBluetoothMaxBandwidthMbps));
206
207 setConnection(WebConnectionTypeEthernet, kEthernetMaxBandwidthMbps);
208 EXPECT_TRUE(verifyObservations(observer1, WebConnectionTypeBluetooth,
209 kBluetoothMaxBandwidthMbps));
210 }
211
212 TEST_F(NetworkStateNotifierTest, RemoveCurrentObserverWhileNotifying) {
213 StateObserver observer1, observer2;
214 m_notifier.addConnectionObserver(&observer1, getTaskRunner());
215 m_notifier.addConnectionObserver(&observer2, getTaskRunner());
216 removeObserverOnNotification(&observer1, &observer1);
217
218 setConnection(WebConnectionTypeBluetooth, kBluetoothMaxBandwidthMbps);
219 EXPECT_TRUE(verifyObservations(observer1, WebConnectionTypeBluetooth,
220 kBluetoothMaxBandwidthMbps));
221 EXPECT_TRUE(verifyObservations(observer2, WebConnectionTypeBluetooth,
222 kBluetoothMaxBandwidthMbps));
223
224 setConnection(WebConnectionTypeEthernet, kEthernetMaxBandwidthMbps);
225 EXPECT_TRUE(verifyObservations(observer1, WebConnectionTypeBluetooth,
226 kBluetoothMaxBandwidthMbps));
227 EXPECT_TRUE(verifyObservations(observer2, WebConnectionTypeEthernet,
228 kEthernetMaxBandwidthMbps));
229 }
230
231 TEST_F(NetworkStateNotifierTest, RemovePastObserverWhileNotifying) {
232 StateObserver observer1, observer2;
233 m_notifier.addConnectionObserver(&observer1, getTaskRunner());
234 m_notifier.addConnectionObserver(&observer2, getTaskRunner());
235 removeObserverOnNotification(&observer2, &observer1);
236
237 setConnection(WebConnectionTypeBluetooth, kBluetoothMaxBandwidthMbps);
238 EXPECT_EQ(observer1.observedType(), WebConnectionTypeBluetooth);
239 EXPECT_EQ(observer2.observedType(), WebConnectionTypeBluetooth);
240
241 setConnection(WebConnectionTypeEthernet, kEthernetMaxBandwidthMbps);
242 EXPECT_TRUE(verifyObservations(observer1, WebConnectionTypeBluetooth,
243 kBluetoothMaxBandwidthMbps));
244 EXPECT_TRUE(verifyObservations(observer2, WebConnectionTypeEthernet,
245 kEthernetMaxBandwidthMbps));
246 }
247
248 TEST_F(NetworkStateNotifierTest, RemoveFutureObserverWhileNotifying) {
249 StateObserver observer1, observer2, observer3;
250 m_notifier.addConnectionObserver(&observer1, getTaskRunner());
251 m_notifier.addConnectionObserver(&observer2, getTaskRunner());
252 m_notifier.addConnectionObserver(&observer3, getTaskRunner());
253 removeObserverOnNotification(&observer1, &observer2);
254
255 setConnection(WebConnectionTypeBluetooth, kBluetoothMaxBandwidthMbps);
256 EXPECT_TRUE(verifyObservations(observer1, WebConnectionTypeBluetooth,
257 kBluetoothMaxBandwidthMbps));
258 EXPECT_TRUE(verifyObservations(observer2, WebConnectionTypeNone,
259 kNoneMaxBandwidthMbps));
260 EXPECT_TRUE(verifyObservations(observer3, WebConnectionTypeBluetooth,
261 kBluetoothMaxBandwidthMbps));
262 }
263
264 TEST_F(NetworkStateNotifierTest, MultipleContextsAddObserver) {
265 StateObserver observer1, observer2;
266 m_notifier.addConnectionObserver(&observer1, getTaskRunner());
267 m_notifier.addConnectionObserver(&observer2, getTaskRunner2());
268
269 setConnection(WebConnectionTypeBluetooth, kBluetoothMaxBandwidthMbps);
270 EXPECT_TRUE(verifyObservations(observer1, WebConnectionTypeBluetooth,
271 kBluetoothMaxBandwidthMbps));
272 EXPECT_TRUE(verifyObservations(observer2, WebConnectionTypeBluetooth,
273 kBluetoothMaxBandwidthMbps));
274 }
275
276 TEST_F(NetworkStateNotifierTest, RemoveContext) {
277 StateObserver observer1, observer2;
278 m_notifier.addConnectionObserver(&observer1, getTaskRunner());
279 m_notifier.addConnectionObserver(&observer2, getTaskRunner2());
280 m_notifier.removeConnectionObserver(&observer2, getTaskRunner2());
281
282 setConnection(WebConnectionTypeBluetooth, kBluetoothMaxBandwidthMbps);
283 EXPECT_TRUE(verifyObservations(observer1, WebConnectionTypeBluetooth,
284 kBluetoothMaxBandwidthMbps));
285 EXPECT_TRUE(verifyObservations(observer2, WebConnectionTypeNone,
286 kNoneMaxBandwidthMbps));
287 }
288
289 TEST_F(NetworkStateNotifierTest, RemoveAllContexts) {
290 StateObserver observer1, observer2;
291 m_notifier.addConnectionObserver(&observer1, getTaskRunner());
292 m_notifier.addConnectionObserver(&observer2, getTaskRunner2());
293 m_notifier.removeConnectionObserver(&observer1, getTaskRunner());
294 m_notifier.removeConnectionObserver(&observer2, getTaskRunner2());
295
296 setConnection(WebConnectionTypeBluetooth, kBluetoothMaxBandwidthMbps);
297 EXPECT_TRUE(verifyObservations(observer1, WebConnectionTypeNone,
298 kNoneMaxBandwidthMbps));
299 EXPECT_TRUE(verifyObservations(observer2, WebConnectionTypeNone,
300 kNoneMaxBandwidthMbps));
301 }
302
303 TEST_F(NetworkStateNotifierTest, SetOverride) {
304 StateObserver observer;
305 m_notifier.addConnectionObserver(&observer, getTaskRunner());
306
307 m_notifier.setOnLine(true);
308 setConnection(WebConnectionTypeBluetooth, kBluetoothMaxBandwidthMbps);
309 EXPECT_TRUE(verifyObservations(observer, WebConnectionTypeBluetooth,
310 kBluetoothMaxBandwidthMbps));
311 EXPECT_TRUE(m_notifier.onLine());
312 EXPECT_EQ(WebConnectionTypeBluetooth, m_notifier.connectionType());
313 EXPECT_EQ(kBluetoothMaxBandwidthMbps, m_notifier.maxBandwidth());
314
315 m_notifier.setOverride(true, WebConnectionTypeEthernet,
316 kEthernetMaxBandwidthMbps);
317 testing::runPendingTasks();
318 EXPECT_TRUE(verifyObservations(observer, WebConnectionTypeEthernet,
319 kEthernetMaxBandwidthMbps));
320 EXPECT_TRUE(m_notifier.onLine());
321 EXPECT_EQ(WebConnectionTypeEthernet, m_notifier.connectionType());
322 EXPECT_EQ(kEthernetMaxBandwidthMbps, m_notifier.maxBandwidth());
323
324 // When override is active, calls to setOnLine and setConnection are temporary
325 // ignored.
326 m_notifier.setOnLine(false);
327 setConnection(WebConnectionTypeNone, kNoneMaxBandwidthMbps);
328 testing::runPendingTasks();
329 EXPECT_TRUE(verifyObservations(observer, WebConnectionTypeEthernet,
330 kEthernetMaxBandwidthMbps));
331 EXPECT_TRUE(m_notifier.onLine());
332 EXPECT_EQ(WebConnectionTypeEthernet, m_notifier.connectionType());
333 EXPECT_EQ(kEthernetMaxBandwidthMbps, m_notifier.maxBandwidth());
334
335 m_notifier.clearOverride();
336 testing::runPendingTasks();
337 EXPECT_TRUE(verifyObservations(observer, WebConnectionTypeNone,
338 kNoneMaxBandwidthMbps));
339 EXPECT_FALSE(m_notifier.onLine());
340 EXPECT_EQ(WebConnectionTypeNone, m_notifier.connectionType());
341 EXPECT_EQ(kNoneMaxBandwidthMbps, m_notifier.maxBandwidth());
342
343 m_notifier.removeConnectionObserver(&observer, getTaskRunner());
344 }
345
346 TEST_F(NetworkStateNotifierTest, NoExtraNotifications) {
347 StateObserver observer;
348 m_notifier.addConnectionObserver(&observer, getTaskRunner());
349
350 setConnection(WebConnectionTypeBluetooth, kBluetoothMaxBandwidthMbps);
351 EXPECT_TRUE(verifyObservations(observer, WebConnectionTypeBluetooth,
352 kBluetoothMaxBandwidthMbps));
353 EXPECT_EQ(observer.callbackCount(), 1);
354
355 setConnection(WebConnectionTypeBluetooth, kBluetoothMaxBandwidthMbps);
356 EXPECT_EQ(observer.callbackCount(), 1);
357
358 setConnection(WebConnectionTypeEthernet, kEthernetMaxBandwidthMbps);
359 EXPECT_TRUE(verifyObservations(observer, WebConnectionTypeEthernet,
360 kEthernetMaxBandwidthMbps));
361 EXPECT_EQ(observer.callbackCount(), 2);
362
363 setConnection(WebConnectionTypeEthernet, kEthernetMaxBandwidthMbps);
364 EXPECT_EQ(observer.callbackCount(), 2);
365
366 setConnection(WebConnectionTypeBluetooth, kBluetoothMaxBandwidthMbps);
367 EXPECT_TRUE(verifyObservations(observer, WebConnectionTypeBluetooth,
368 kBluetoothMaxBandwidthMbps));
369 EXPECT_EQ(observer.callbackCount(), 3);
370
371 m_notifier.removeConnectionObserver(&observer, getTaskRunner());
372 }
373
374 TEST_F(NetworkStateNotifierTest, NoNotificationOnInitialization) {
375 NetworkStateNotifier notifier;
376 Persistent<Document> document(Document::create());
377 StateObserver observer;
378
379 notifier.addConnectionObserver(&observer, getTaskRunner());
380 notifier.addOnLineObserver(&observer, getTaskRunner());
381 testing::runPendingTasks();
382 EXPECT_EQ(observer.callbackCount(), 0);
383
384 notifier.setWebConnection(WebConnectionTypeBluetooth,
385 kBluetoothMaxBandwidthMbps);
386 notifier.setOnLine(true);
387 testing::runPendingTasks();
388 EXPECT_EQ(observer.callbackCount(), 0);
389
390 notifier.setOnLine(true);
391 notifier.setWebConnection(WebConnectionTypeBluetooth,
392 kBluetoothMaxBandwidthMbps);
393 testing::runPendingTasks();
394 EXPECT_EQ(observer.callbackCount(), 0);
395
396 notifier.setWebConnection(WebConnectionTypeEthernet,
397 kEthernetMaxBandwidthMbps);
398 testing::runPendingTasks();
399 EXPECT_EQ(observer.callbackCount(), 1);
400 EXPECT_EQ(observer.observedType(), WebConnectionTypeEthernet);
401 EXPECT_EQ(observer.observedMaxBandwidth(), kEthernetMaxBandwidthMbps);
402
403 notifier.setOnLine(false);
404 testing::runPendingTasks();
405 EXPECT_EQ(observer.callbackCount(), 2);
406 EXPECT_FALSE(observer.observedOnLineState());
407 }
408
409 TEST_F(NetworkStateNotifierTest, OnLineNotification) {
410 StateObserver observer;
411 m_notifier.addOnLineObserver(&observer, getTaskRunner());
412
413 setOnLine(true);
414 testing::runPendingTasks();
415 EXPECT_TRUE(observer.observedOnLineState());
416 EXPECT_EQ(observer.callbackCount(), 1);
417
418 setOnLine(false);
419 testing::runPendingTasks();
420 EXPECT_FALSE(observer.observedOnLineState());
421 EXPECT_EQ(observer.callbackCount(), 2);
422
423 m_notifier.removeConnectionObserver(&observer, getTaskRunner());
424 }
425
426 TEST_F(NetworkStateNotifierTest, MultipleObservers) {
427 StateObserver observer1;
428 StateObserver observer2;
429
430 // Observer1 observes online state, Observer2 observes both.
431 m_notifier.addOnLineObserver(&observer1, getTaskRunner());
432 m_notifier.addConnectionObserver(&observer2, getTaskRunner());
433 m_notifier.addOnLineObserver(&observer2, getTaskRunner());
434
435 m_notifier.setOnLine(true);
436 testing::runPendingTasks();
437 EXPECT_TRUE(observer1.observedOnLineState());
438 EXPECT_TRUE(observer2.observedOnLineState());
439 EXPECT_EQ(observer1.callbackCount(), 1);
440 EXPECT_EQ(observer2.callbackCount(), 1);
441
442 m_notifier.setOnLine(false);
443 testing::runPendingTasks();
444 EXPECT_FALSE(observer1.observedOnLineState());
445 EXPECT_FALSE(observer2.observedOnLineState());
446 EXPECT_EQ(observer1.callbackCount(), 2);
447 EXPECT_EQ(observer2.callbackCount(), 2);
448
449 m_notifier.setOnLine(true);
450 m_notifier.setWebConnection(WebConnectionTypeEthernet,
451 kEthernetMaxBandwidthMbps);
452 testing::runPendingTasks();
453 EXPECT_TRUE(observer1.observedOnLineState());
454 EXPECT_TRUE(observer2.observedOnLineState());
455 EXPECT_TRUE(verifyObservations(observer2, WebConnectionTypeEthernet,
456 kEthernetMaxBandwidthMbps));
457 EXPECT_EQ(observer1.callbackCount(), 3);
458 EXPECT_EQ(observer2.callbackCount(), 4);
459
460 m_notifier.removeConnectionObserver(&observer1, getTaskRunner());
461 m_notifier.removeConnectionObserver(&observer2, getTaskRunner());
462 }
463
464 } // namespace blink
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/core/page/NetworkStateNotifier.cpp ('k') | third_party/WebKit/Source/core/testing/Internals.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698