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

Side by Side Diff: third_party/WebKit/Source/platform/network/NetworkStateNotifierTest.cpp

Issue 2710023006: Move NetworkStateNotifier from core (and web) into platform/network (Closed)
Patch Set: . 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
1 /* 1 /*
2 * Copyright (c) 2014, Google Inc. All rights reserved. 2 * Copyright (c) 2014, Google Inc. All rights reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are 5 * modification, are permitted provided that the following conditions are
6 * met: 6 * met:
7 * 7 *
8 * * Redistributions of source code must retain the above copyright 8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer. 9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above 10 * * Redistributions in binary form must reproduce the above
(...skipping 10 matching lines...) Expand all
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 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. 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */ 29 */
30 30
31 #include "core/page/NetworkStateNotifier.h" 31 #include "platform/network/NetworkStateNotifier.h"
32 32
33 #include "core/dom/Document.h" 33 #include "platform/scheduler/test/fake_web_task_runner.h"
34 #include "core/dom/TaskRunnerHelper.h"
35 #include "platform/testing/UnitTestHelpers.h" 34 #include "platform/testing/UnitTestHelpers.h"
36 #include "public/platform/Platform.h" 35 #include "public/platform/Platform.h"
37 #include "public/platform/WebConnectionType.h" 36 #include "public/platform/WebConnectionType.h"
38 #include "public/platform/WebThread.h" 37 #include "public/platform/WebThread.h"
39 #include "testing/gtest/include/gtest/gtest.h" 38 #include "testing/gtest/include/gtest/gtest.h"
40 #include "wtf/Functional.h" 39 #include "wtf/Functional.h"
41 40
42 namespace blink { 41 namespace blink {
43 42
43 using scheduler::FakeWebTaskRunner;
44
44 namespace { 45 namespace {
45 const double kNoneMaxBandwidthMbps = 0.0; 46 const double kNoneMaxBandwidthMbps = 0.0;
46 const double kBluetoothMaxBandwidthMbps = 1.0; 47 const double kBluetoothMaxBandwidthMbps = 1.0;
47 const double kEthernetMaxBandwidthMbps = 2.0; 48 const double kEthernetMaxBandwidthMbps = 2.0;
48 } 49 }
49 50
50 class StateObserver : public NetworkStateNotifier::NetworkStateObserver { 51 class StateObserver : public NetworkStateNotifier::NetworkStateObserver {
51 public: 52 public:
52 StateObserver() 53 StateObserver()
53 : m_observedType(WebConnectionTypeNone), 54 : m_observedType(WebConnectionTypeNone),
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
86 std::unique_ptr<WTF::Closure> m_closure; 87 std::unique_ptr<WTF::Closure> m_closure;
87 WebConnectionType m_observedType; 88 WebConnectionType m_observedType;
88 double m_observedMaxBandwidthMbps; 89 double m_observedMaxBandwidthMbps;
89 bool m_observedOnLineState; 90 bool m_observedOnLineState;
90 int m_callbackCount; 91 int m_callbackCount;
91 }; 92 };
92 93
93 class NetworkStateNotifierTest : public ::testing::Test { 94 class NetworkStateNotifierTest : public ::testing::Test {
94 public: 95 public:
95 NetworkStateNotifierTest() 96 NetworkStateNotifierTest()
96 : m_document(Document::create()), m_document2(Document::create()) { 97 : m_taskRunner(adoptRef(new FakeWebTaskRunner())),
98 m_taskRunner2(adoptRef(new FakeWebTaskRunner())) {
97 // Initialize connection, so that future calls to setWebConnection issue 99 // Initialize connection, so that future calls to setWebConnection issue
98 // notifications. 100 // notifications.
99 m_notifier.setWebConnection(WebConnectionTypeUnknown, 0.0); 101 m_notifier.setWebConnection(WebConnectionTypeUnknown, 0.0);
100 m_notifier.setOnLine(false); 102 m_notifier.setOnLine(false);
101 } 103 }
102 104
103 WebTaskRunner* getTaskRunner() { 105 WebTaskRunner* getTaskRunner() { return m_taskRunner.get(); }
104 return TaskRunnerHelper::get(TaskType::Networking, m_document.get()).get(); 106 WebTaskRunner* getTaskRunner2() { return m_taskRunner2.get(); }
105 }
106 107
107 WebTaskRunner* getTaskRunner2() { 108 void TearDown() override {
108 return TaskRunnerHelper::get(TaskType::Networking, m_document2.get()).get(); 109 runPendingTasks();
110 m_taskRunner = nullptr;
111 m_taskRunner2 = nullptr;
109 } 112 }
110 113
111 protected: 114 protected:
115 void runPendingTasks() {
116 m_taskRunner->runUntilIdle();
117 m_taskRunner2->runUntilIdle();
118 }
119
112 void setConnection(WebConnectionType type, double maxBandwidthMbps) { 120 void setConnection(WebConnectionType type, double maxBandwidthMbps) {
113 m_notifier.setWebConnection(type, maxBandwidthMbps); 121 m_notifier.setWebConnection(type, maxBandwidthMbps);
114 testing::runPendingTasks(); 122 runPendingTasks();
115 } 123 }
116 void setOnLine(bool onLine) { 124 void setOnLine(bool onLine) {
117 m_notifier.setOnLine(onLine); 125 m_notifier.setOnLine(onLine);
118 testing::runPendingTasks(); 126 runPendingTasks();
119 } 127 }
120 128
121 void addObserverOnNotification(StateObserver* observer, 129 void addObserverOnNotification(StateObserver* observer,
122 StateObserver* observerToAdd) { 130 StateObserver* observerToAdd) {
123 observer->setNotificationCallback( 131 observer->setNotificationCallback(
124 bind(&NetworkStateNotifier::addConnectionObserver, 132 bind(&NetworkStateNotifier::addConnectionObserver,
125 WTF::unretained(&m_notifier), WTF::unretained(observerToAdd), 133 WTF::unretained(&m_notifier), WTF::unretained(observerToAdd),
126 WTF::unretained(getTaskRunner()))); 134 WTF::unretained(getTaskRunner())));
127 } 135 }
128 136
129 void removeObserverOnNotification(StateObserver* observer, 137 void removeObserverOnNotification(StateObserver* observer,
130 StateObserver* observerToRemove) { 138 StateObserver* observerToRemove) {
131 observer->setNotificationCallback( 139 observer->setNotificationCallback(
132 bind(&NetworkStateNotifier::removeConnectionObserver, 140 bind(&NetworkStateNotifier::removeConnectionObserver,
133 WTF::unretained(&m_notifier), WTF::unretained(observerToRemove), 141 WTF::unretained(&m_notifier), WTF::unretained(observerToRemove),
134 WTF::unretained(getTaskRunner()))); 142 WTF::unretained(getTaskRunner())));
135 } 143 }
136 144
137 bool verifyObservations(const StateObserver& observer, 145 bool verifyObservations(const StateObserver& observer,
138 WebConnectionType type, 146 WebConnectionType type,
139 double maxBandwidthMbps) { 147 double maxBandwidthMbps) {
140 EXPECT_EQ(observer.observedType(), type); 148 EXPECT_EQ(observer.observedType(), type);
141 EXPECT_EQ(observer.observedMaxBandwidth(), maxBandwidthMbps); 149 EXPECT_EQ(observer.observedMaxBandwidth(), maxBandwidthMbps);
142 return observer.observedType() == type && 150 return observer.observedType() == type &&
143 observer.observedMaxBandwidth() == maxBandwidthMbps; 151 observer.observedMaxBandwidth() == maxBandwidthMbps;
144 } 152 }
145 153
146 Persistent<Document> m_document; 154 RefPtr<FakeWebTaskRunner> m_taskRunner;
147 Persistent<Document> m_document2; 155 RefPtr<FakeWebTaskRunner> m_taskRunner2;
148 NetworkStateNotifier m_notifier; 156 NetworkStateNotifier m_notifier;
149 }; 157 };
150 158
151 TEST_F(NetworkStateNotifierTest, AddObserver) { 159 TEST_F(NetworkStateNotifierTest, AddObserver) {
152 StateObserver observer; 160 StateObserver observer;
153 m_notifier.addConnectionObserver(&observer, getTaskRunner()); 161 m_notifier.addConnectionObserver(&observer, getTaskRunner());
154 EXPECT_TRUE(verifyObservations(observer, WebConnectionTypeNone, 162 EXPECT_TRUE(verifyObservations(observer, WebConnectionTypeNone,
155 kNoneMaxBandwidthMbps)); 163 kNoneMaxBandwidthMbps));
156 164
157 setConnection(WebConnectionTypeBluetooth, kBluetoothMaxBandwidthMbps); 165 setConnection(WebConnectionTypeBluetooth, kBluetoothMaxBandwidthMbps);
158 EXPECT_TRUE(verifyObservations(observer, WebConnectionTypeBluetooth, 166 EXPECT_TRUE(verifyObservations(observer, WebConnectionTypeBluetooth,
159 kBluetoothMaxBandwidthMbps)); 167 kBluetoothMaxBandwidthMbps));
160 EXPECT_EQ(observer.callbackCount(), 1); 168 EXPECT_EQ(observer.callbackCount(), 1);
169 m_notifier.removeConnectionObserver(&observer, getTaskRunner());
161 } 170 }
162 171
163 TEST_F(NetworkStateNotifierTest, RemoveObserver) { 172 TEST_F(NetworkStateNotifierTest, RemoveObserver) {
164 StateObserver observer1, observer2; 173 StateObserver observer1, observer2;
165 m_notifier.addConnectionObserver(&observer1, getTaskRunner()); 174 m_notifier.addConnectionObserver(&observer1, getTaskRunner());
166 m_notifier.removeConnectionObserver(&observer1, getTaskRunner()); 175 m_notifier.removeConnectionObserver(&observer1, getTaskRunner());
167 m_notifier.addConnectionObserver(&observer2, getTaskRunner()); 176 m_notifier.addConnectionObserver(&observer2, getTaskRunner());
168 177
169 setConnection(WebConnectionTypeBluetooth, kBluetoothMaxBandwidthMbps); 178 setConnection(WebConnectionTypeBluetooth, kBluetoothMaxBandwidthMbps);
170 EXPECT_TRUE(verifyObservations(observer1, WebConnectionTypeNone, 179 EXPECT_TRUE(verifyObservations(observer1, WebConnectionTypeNone,
171 kNoneMaxBandwidthMbps)); 180 kNoneMaxBandwidthMbps));
172 EXPECT_TRUE(verifyObservations(observer2, WebConnectionTypeBluetooth, 181 EXPECT_TRUE(verifyObservations(observer2, WebConnectionTypeBluetooth,
173 kBluetoothMaxBandwidthMbps)); 182 kBluetoothMaxBandwidthMbps));
183 m_notifier.removeConnectionObserver(&observer2, getTaskRunner());
174 } 184 }
175 185
176 TEST_F(NetworkStateNotifierTest, RemoveSoleObserver) { 186 TEST_F(NetworkStateNotifierTest, RemoveSoleObserver) {
177 StateObserver observer1; 187 StateObserver observer1;
178 m_notifier.addConnectionObserver(&observer1, getTaskRunner()); 188 m_notifier.addConnectionObserver(&observer1, getTaskRunner());
179 m_notifier.removeConnectionObserver(&observer1, getTaskRunner()); 189 m_notifier.removeConnectionObserver(&observer1, getTaskRunner());
180 190
181 setConnection(WebConnectionTypeBluetooth, kBluetoothMaxBandwidthMbps); 191 setConnection(WebConnectionTypeBluetooth, kBluetoothMaxBandwidthMbps);
182 EXPECT_TRUE(verifyObservations(observer1, WebConnectionTypeNone, 192 EXPECT_TRUE(verifyObservations(observer1, WebConnectionTypeNone,
183 kNoneMaxBandwidthMbps)); 193 kNoneMaxBandwidthMbps));
184 } 194 }
185 195
186 TEST_F(NetworkStateNotifierTest, AddObserverWhileNotifying) { 196 TEST_F(NetworkStateNotifierTest, AddObserverWhileNotifying) {
187 StateObserver observer1, observer2; 197 StateObserver observer1, observer2;
188 m_notifier.addConnectionObserver(&observer1, getTaskRunner()); 198 m_notifier.addConnectionObserver(&observer1, getTaskRunner());
189 addObserverOnNotification(&observer1, &observer2); 199 addObserverOnNotification(&observer1, &observer2);
190 200
191 setConnection(WebConnectionTypeBluetooth, kBluetoothMaxBandwidthMbps); 201 setConnection(WebConnectionTypeBluetooth, kBluetoothMaxBandwidthMbps);
192 EXPECT_TRUE(verifyObservations(observer1, WebConnectionTypeBluetooth, 202 EXPECT_TRUE(verifyObservations(observer1, WebConnectionTypeBluetooth,
193 kBluetoothMaxBandwidthMbps)); 203 kBluetoothMaxBandwidthMbps));
194 EXPECT_TRUE(verifyObservations(observer2, WebConnectionTypeBluetooth, 204 EXPECT_TRUE(verifyObservations(observer2, WebConnectionTypeBluetooth,
195 kBluetoothMaxBandwidthMbps)); 205 kBluetoothMaxBandwidthMbps));
206 m_notifier.removeConnectionObserver(&observer1, getTaskRunner());
207 m_notifier.removeConnectionObserver(&observer2, getTaskRunner());
196 } 208 }
197 209
198 TEST_F(NetworkStateNotifierTest, RemoveSoleObserverWhileNotifying) { 210 TEST_F(NetworkStateNotifierTest, RemoveSoleObserverWhileNotifying) {
199 StateObserver observer1; 211 StateObserver observer1;
200 m_notifier.addConnectionObserver(&observer1, getTaskRunner()); 212 m_notifier.addConnectionObserver(&observer1, getTaskRunner());
201 removeObserverOnNotification(&observer1, &observer1); 213 removeObserverOnNotification(&observer1, &observer1);
202 214
203 setConnection(WebConnectionTypeBluetooth, kBluetoothMaxBandwidthMbps); 215 setConnection(WebConnectionTypeBluetooth, kBluetoothMaxBandwidthMbps);
204 EXPECT_TRUE(verifyObservations(observer1, WebConnectionTypeBluetooth, 216 EXPECT_TRUE(verifyObservations(observer1, WebConnectionTypeBluetooth,
205 kBluetoothMaxBandwidthMbps)); 217 kBluetoothMaxBandwidthMbps));
(...skipping 13 matching lines...) Expand all
219 EXPECT_TRUE(verifyObservations(observer1, WebConnectionTypeBluetooth, 231 EXPECT_TRUE(verifyObservations(observer1, WebConnectionTypeBluetooth,
220 kBluetoothMaxBandwidthMbps)); 232 kBluetoothMaxBandwidthMbps));
221 EXPECT_TRUE(verifyObservations(observer2, WebConnectionTypeBluetooth, 233 EXPECT_TRUE(verifyObservations(observer2, WebConnectionTypeBluetooth,
222 kBluetoothMaxBandwidthMbps)); 234 kBluetoothMaxBandwidthMbps));
223 235
224 setConnection(WebConnectionTypeEthernet, kEthernetMaxBandwidthMbps); 236 setConnection(WebConnectionTypeEthernet, kEthernetMaxBandwidthMbps);
225 EXPECT_TRUE(verifyObservations(observer1, WebConnectionTypeBluetooth, 237 EXPECT_TRUE(verifyObservations(observer1, WebConnectionTypeBluetooth,
226 kBluetoothMaxBandwidthMbps)); 238 kBluetoothMaxBandwidthMbps));
227 EXPECT_TRUE(verifyObservations(observer2, WebConnectionTypeEthernet, 239 EXPECT_TRUE(verifyObservations(observer2, WebConnectionTypeEthernet,
228 kEthernetMaxBandwidthMbps)); 240 kEthernetMaxBandwidthMbps));
241
242 m_notifier.removeConnectionObserver(&observer1, getTaskRunner());
243 m_notifier.removeConnectionObserver(&observer2, getTaskRunner());
229 } 244 }
230 245
231 TEST_F(NetworkStateNotifierTest, RemovePastObserverWhileNotifying) { 246 TEST_F(NetworkStateNotifierTest, RemovePastObserverWhileNotifying) {
232 StateObserver observer1, observer2; 247 StateObserver observer1, observer2;
233 m_notifier.addConnectionObserver(&observer1, getTaskRunner()); 248 m_notifier.addConnectionObserver(&observer1, getTaskRunner());
234 m_notifier.addConnectionObserver(&observer2, getTaskRunner()); 249 m_notifier.addConnectionObserver(&observer2, getTaskRunner());
235 removeObserverOnNotification(&observer2, &observer1); 250 removeObserverOnNotification(&observer2, &observer1);
236 251
237 setConnection(WebConnectionTypeBluetooth, kBluetoothMaxBandwidthMbps); 252 setConnection(WebConnectionTypeBluetooth, kBluetoothMaxBandwidthMbps);
238 EXPECT_EQ(observer1.observedType(), WebConnectionTypeBluetooth); 253 EXPECT_EQ(observer1.observedType(), WebConnectionTypeBluetooth);
239 EXPECT_EQ(observer2.observedType(), WebConnectionTypeBluetooth); 254 EXPECT_EQ(observer2.observedType(), WebConnectionTypeBluetooth);
240 255
241 setConnection(WebConnectionTypeEthernet, kEthernetMaxBandwidthMbps); 256 setConnection(WebConnectionTypeEthernet, kEthernetMaxBandwidthMbps);
242 EXPECT_TRUE(verifyObservations(observer1, WebConnectionTypeBluetooth, 257 EXPECT_TRUE(verifyObservations(observer1, WebConnectionTypeBluetooth,
243 kBluetoothMaxBandwidthMbps)); 258 kBluetoothMaxBandwidthMbps));
244 EXPECT_TRUE(verifyObservations(observer2, WebConnectionTypeEthernet, 259 EXPECT_TRUE(verifyObservations(observer2, WebConnectionTypeEthernet,
245 kEthernetMaxBandwidthMbps)); 260 kEthernetMaxBandwidthMbps));
261
262 m_notifier.removeConnectionObserver(&observer1, getTaskRunner());
263 m_notifier.removeConnectionObserver(&observer2, getTaskRunner());
246 } 264 }
247 265
248 TEST_F(NetworkStateNotifierTest, RemoveFutureObserverWhileNotifying) { 266 TEST_F(NetworkStateNotifierTest, RemoveFutureObserverWhileNotifying) {
249 StateObserver observer1, observer2, observer3; 267 StateObserver observer1, observer2, observer3;
250 m_notifier.addConnectionObserver(&observer1, getTaskRunner()); 268 m_notifier.addConnectionObserver(&observer1, getTaskRunner());
251 m_notifier.addConnectionObserver(&observer2, getTaskRunner()); 269 m_notifier.addConnectionObserver(&observer2, getTaskRunner());
252 m_notifier.addConnectionObserver(&observer3, getTaskRunner()); 270 m_notifier.addConnectionObserver(&observer3, getTaskRunner());
253 removeObserverOnNotification(&observer1, &observer2); 271 removeObserverOnNotification(&observer1, &observer2);
254 272
255 setConnection(WebConnectionTypeBluetooth, kBluetoothMaxBandwidthMbps); 273 setConnection(WebConnectionTypeBluetooth, kBluetoothMaxBandwidthMbps);
256 EXPECT_TRUE(verifyObservations(observer1, WebConnectionTypeBluetooth, 274 EXPECT_TRUE(verifyObservations(observer1, WebConnectionTypeBluetooth,
257 kBluetoothMaxBandwidthMbps)); 275 kBluetoothMaxBandwidthMbps));
258 EXPECT_TRUE(verifyObservations(observer2, WebConnectionTypeNone, 276 EXPECT_TRUE(verifyObservations(observer2, WebConnectionTypeNone,
259 kNoneMaxBandwidthMbps)); 277 kNoneMaxBandwidthMbps));
260 EXPECT_TRUE(verifyObservations(observer3, WebConnectionTypeBluetooth, 278 EXPECT_TRUE(verifyObservations(observer3, WebConnectionTypeBluetooth,
261 kBluetoothMaxBandwidthMbps)); 279 kBluetoothMaxBandwidthMbps));
280
281 m_notifier.removeConnectionObserver(&observer1, getTaskRunner());
282 m_notifier.removeConnectionObserver(&observer2, getTaskRunner());
283 m_notifier.removeConnectionObserver(&observer3, getTaskRunner());
262 } 284 }
263 285
264 TEST_F(NetworkStateNotifierTest, MultipleContextsAddObserver) { 286 TEST_F(NetworkStateNotifierTest, MultipleContextsAddObserver) {
265 StateObserver observer1, observer2; 287 StateObserver observer1, observer2;
266 m_notifier.addConnectionObserver(&observer1, getTaskRunner()); 288 m_notifier.addConnectionObserver(&observer1, getTaskRunner());
267 m_notifier.addConnectionObserver(&observer2, getTaskRunner2()); 289 m_notifier.addConnectionObserver(&observer2, getTaskRunner2());
268 290
269 setConnection(WebConnectionTypeBluetooth, kBluetoothMaxBandwidthMbps); 291 setConnection(WebConnectionTypeBluetooth, kBluetoothMaxBandwidthMbps);
270 EXPECT_TRUE(verifyObservations(observer1, WebConnectionTypeBluetooth, 292 EXPECT_TRUE(verifyObservations(observer1, WebConnectionTypeBluetooth,
271 kBluetoothMaxBandwidthMbps)); 293 kBluetoothMaxBandwidthMbps));
272 EXPECT_TRUE(verifyObservations(observer2, WebConnectionTypeBluetooth, 294 EXPECT_TRUE(verifyObservations(observer2, WebConnectionTypeBluetooth,
273 kBluetoothMaxBandwidthMbps)); 295 kBluetoothMaxBandwidthMbps));
296
297 m_notifier.removeConnectionObserver(&observer1, getTaskRunner());
298 m_notifier.removeConnectionObserver(&observer2, getTaskRunner());
sof 2017/03/06 06:43:03 getTaskRunner2()
sof 2017/03/06 06:53:21 ..but why are explicit removals now needed?
kinuko 2017/03/06 07:39:02 Done.
274 } 299 }
275 300
276 TEST_F(NetworkStateNotifierTest, RemoveContext) { 301 TEST_F(NetworkStateNotifierTest, RemoveContext) {
277 StateObserver observer1, observer2; 302 StateObserver observer1, observer2;
278 m_notifier.addConnectionObserver(&observer1, getTaskRunner()); 303 m_notifier.addConnectionObserver(&observer1, getTaskRunner());
279 m_notifier.addConnectionObserver(&observer2, getTaskRunner2()); 304 m_notifier.addConnectionObserver(&observer2, getTaskRunner2());
280 m_notifier.removeConnectionObserver(&observer2, getTaskRunner2()); 305 m_notifier.removeConnectionObserver(&observer2, getTaskRunner2());
281 306
282 setConnection(WebConnectionTypeBluetooth, kBluetoothMaxBandwidthMbps); 307 setConnection(WebConnectionTypeBluetooth, kBluetoothMaxBandwidthMbps);
283 EXPECT_TRUE(verifyObservations(observer1, WebConnectionTypeBluetooth, 308 EXPECT_TRUE(verifyObservations(observer1, WebConnectionTypeBluetooth,
284 kBluetoothMaxBandwidthMbps)); 309 kBluetoothMaxBandwidthMbps));
285 EXPECT_TRUE(verifyObservations(observer2, WebConnectionTypeNone, 310 EXPECT_TRUE(verifyObservations(observer2, WebConnectionTypeNone,
286 kNoneMaxBandwidthMbps)); 311 kNoneMaxBandwidthMbps));
312
313 m_notifier.removeConnectionObserver(&observer1, getTaskRunner());
287 } 314 }
288 315
289 TEST_F(NetworkStateNotifierTest, RemoveAllContexts) { 316 TEST_F(NetworkStateNotifierTest, RemoveAllContexts) {
290 StateObserver observer1, observer2; 317 StateObserver observer1, observer2;
291 m_notifier.addConnectionObserver(&observer1, getTaskRunner()); 318 m_notifier.addConnectionObserver(&observer1, getTaskRunner());
292 m_notifier.addConnectionObserver(&observer2, getTaskRunner2()); 319 m_notifier.addConnectionObserver(&observer2, getTaskRunner2());
293 m_notifier.removeConnectionObserver(&observer1, getTaskRunner()); 320 m_notifier.removeConnectionObserver(&observer1, getTaskRunner());
294 m_notifier.removeConnectionObserver(&observer2, getTaskRunner2()); 321 m_notifier.removeConnectionObserver(&observer2, getTaskRunner2());
295 322
296 setConnection(WebConnectionTypeBluetooth, kBluetoothMaxBandwidthMbps); 323 setConnection(WebConnectionTypeBluetooth, kBluetoothMaxBandwidthMbps);
(...skipping 10 matching lines...) Expand all
307 m_notifier.setOnLine(true); 334 m_notifier.setOnLine(true);
308 setConnection(WebConnectionTypeBluetooth, kBluetoothMaxBandwidthMbps); 335 setConnection(WebConnectionTypeBluetooth, kBluetoothMaxBandwidthMbps);
309 EXPECT_TRUE(verifyObservations(observer, WebConnectionTypeBluetooth, 336 EXPECT_TRUE(verifyObservations(observer, WebConnectionTypeBluetooth,
310 kBluetoothMaxBandwidthMbps)); 337 kBluetoothMaxBandwidthMbps));
311 EXPECT_TRUE(m_notifier.onLine()); 338 EXPECT_TRUE(m_notifier.onLine());
312 EXPECT_EQ(WebConnectionTypeBluetooth, m_notifier.connectionType()); 339 EXPECT_EQ(WebConnectionTypeBluetooth, m_notifier.connectionType());
313 EXPECT_EQ(kBluetoothMaxBandwidthMbps, m_notifier.maxBandwidth()); 340 EXPECT_EQ(kBluetoothMaxBandwidthMbps, m_notifier.maxBandwidth());
314 341
315 m_notifier.setOverride(true, WebConnectionTypeEthernet, 342 m_notifier.setOverride(true, WebConnectionTypeEthernet,
316 kEthernetMaxBandwidthMbps); 343 kEthernetMaxBandwidthMbps);
317 testing::runPendingTasks(); 344 runPendingTasks();
318 EXPECT_TRUE(verifyObservations(observer, WebConnectionTypeEthernet, 345 EXPECT_TRUE(verifyObservations(observer, WebConnectionTypeEthernet,
319 kEthernetMaxBandwidthMbps)); 346 kEthernetMaxBandwidthMbps));
320 EXPECT_TRUE(m_notifier.onLine()); 347 EXPECT_TRUE(m_notifier.onLine());
321 EXPECT_EQ(WebConnectionTypeEthernet, m_notifier.connectionType()); 348 EXPECT_EQ(WebConnectionTypeEthernet, m_notifier.connectionType());
322 EXPECT_EQ(kEthernetMaxBandwidthMbps, m_notifier.maxBandwidth()); 349 EXPECT_EQ(kEthernetMaxBandwidthMbps, m_notifier.maxBandwidth());
323 350
324 // When override is active, calls to setOnLine and setConnection are temporary 351 // When override is active, calls to setOnLine and setConnection are temporary
325 // ignored. 352 // ignored.
326 m_notifier.setOnLine(false); 353 m_notifier.setOnLine(false);
327 setConnection(WebConnectionTypeNone, kNoneMaxBandwidthMbps); 354 setConnection(WebConnectionTypeNone, kNoneMaxBandwidthMbps);
328 testing::runPendingTasks(); 355 runPendingTasks();
329 EXPECT_TRUE(verifyObservations(observer, WebConnectionTypeEthernet, 356 EXPECT_TRUE(verifyObservations(observer, WebConnectionTypeEthernet,
330 kEthernetMaxBandwidthMbps)); 357 kEthernetMaxBandwidthMbps));
331 EXPECT_TRUE(m_notifier.onLine()); 358 EXPECT_TRUE(m_notifier.onLine());
332 EXPECT_EQ(WebConnectionTypeEthernet, m_notifier.connectionType()); 359 EXPECT_EQ(WebConnectionTypeEthernet, m_notifier.connectionType());
333 EXPECT_EQ(kEthernetMaxBandwidthMbps, m_notifier.maxBandwidth()); 360 EXPECT_EQ(kEthernetMaxBandwidthMbps, m_notifier.maxBandwidth());
334 361
335 m_notifier.clearOverride(); 362 m_notifier.clearOverride();
336 testing::runPendingTasks(); 363 runPendingTasks();
337 EXPECT_TRUE(verifyObservations(observer, WebConnectionTypeNone, 364 EXPECT_TRUE(verifyObservations(observer, WebConnectionTypeNone,
338 kNoneMaxBandwidthMbps)); 365 kNoneMaxBandwidthMbps));
339 EXPECT_FALSE(m_notifier.onLine()); 366 EXPECT_FALSE(m_notifier.onLine());
340 EXPECT_EQ(WebConnectionTypeNone, m_notifier.connectionType()); 367 EXPECT_EQ(WebConnectionTypeNone, m_notifier.connectionType());
341 EXPECT_EQ(kNoneMaxBandwidthMbps, m_notifier.maxBandwidth()); 368 EXPECT_EQ(kNoneMaxBandwidthMbps, m_notifier.maxBandwidth());
342 369
343 m_notifier.removeConnectionObserver(&observer, getTaskRunner()); 370 m_notifier.removeConnectionObserver(&observer, getTaskRunner());
344 } 371 }
345 372
346 TEST_F(NetworkStateNotifierTest, NoExtraNotifications) { 373 TEST_F(NetworkStateNotifierTest, NoExtraNotifications) {
(...skipping 19 matching lines...) Expand all
366 setConnection(WebConnectionTypeBluetooth, kBluetoothMaxBandwidthMbps); 393 setConnection(WebConnectionTypeBluetooth, kBluetoothMaxBandwidthMbps);
367 EXPECT_TRUE(verifyObservations(observer, WebConnectionTypeBluetooth, 394 EXPECT_TRUE(verifyObservations(observer, WebConnectionTypeBluetooth,
368 kBluetoothMaxBandwidthMbps)); 395 kBluetoothMaxBandwidthMbps));
369 EXPECT_EQ(observer.callbackCount(), 3); 396 EXPECT_EQ(observer.callbackCount(), 3);
370 397
371 m_notifier.removeConnectionObserver(&observer, getTaskRunner()); 398 m_notifier.removeConnectionObserver(&observer, getTaskRunner());
372 } 399 }
373 400
374 TEST_F(NetworkStateNotifierTest, NoNotificationOnInitialization) { 401 TEST_F(NetworkStateNotifierTest, NoNotificationOnInitialization) {
375 NetworkStateNotifier notifier; 402 NetworkStateNotifier notifier;
376 Persistent<Document> document(Document::create());
377 StateObserver observer; 403 StateObserver observer;
378 404
379 notifier.addConnectionObserver(&observer, getTaskRunner()); 405 notifier.addConnectionObserver(&observer, getTaskRunner());
380 notifier.addOnLineObserver(&observer, getTaskRunner()); 406 notifier.addOnLineObserver(&observer, getTaskRunner());
381 testing::runPendingTasks(); 407 runPendingTasks();
382 EXPECT_EQ(observer.callbackCount(), 0); 408 EXPECT_EQ(observer.callbackCount(), 0);
383 409
384 notifier.setWebConnection(WebConnectionTypeBluetooth, 410 notifier.setWebConnection(WebConnectionTypeBluetooth,
385 kBluetoothMaxBandwidthMbps); 411 kBluetoothMaxBandwidthMbps);
386 notifier.setOnLine(true); 412 notifier.setOnLine(true);
387 testing::runPendingTasks(); 413 runPendingTasks();
388 EXPECT_EQ(observer.callbackCount(), 0); 414 EXPECT_EQ(observer.callbackCount(), 0);
389 415
390 notifier.setOnLine(true); 416 notifier.setOnLine(true);
391 notifier.setWebConnection(WebConnectionTypeBluetooth, 417 notifier.setWebConnection(WebConnectionTypeBluetooth,
392 kBluetoothMaxBandwidthMbps); 418 kBluetoothMaxBandwidthMbps);
393 testing::runPendingTasks(); 419 runPendingTasks();
394 EXPECT_EQ(observer.callbackCount(), 0); 420 EXPECT_EQ(observer.callbackCount(), 0);
395 421
396 notifier.setWebConnection(WebConnectionTypeEthernet, 422 notifier.setWebConnection(WebConnectionTypeEthernet,
397 kEthernetMaxBandwidthMbps); 423 kEthernetMaxBandwidthMbps);
398 testing::runPendingTasks(); 424 runPendingTasks();
399 EXPECT_EQ(observer.callbackCount(), 1); 425 EXPECT_EQ(observer.callbackCount(), 1);
400 EXPECT_EQ(observer.observedType(), WebConnectionTypeEthernet); 426 EXPECT_EQ(observer.observedType(), WebConnectionTypeEthernet);
401 EXPECT_EQ(observer.observedMaxBandwidth(), kEthernetMaxBandwidthMbps); 427 EXPECT_EQ(observer.observedMaxBandwidth(), kEthernetMaxBandwidthMbps);
402 428
403 notifier.setOnLine(false); 429 notifier.setOnLine(false);
404 testing::runPendingTasks(); 430 runPendingTasks();
405 EXPECT_EQ(observer.callbackCount(), 2); 431 EXPECT_EQ(observer.callbackCount(), 2);
406 EXPECT_FALSE(observer.observedOnLineState()); 432 EXPECT_FALSE(observer.observedOnLineState());
433
434 m_notifier.removeConnectionObserver(&observer, getTaskRunner());
435 m_notifier.removeOnLineObserver(&observer, getTaskRunner());
407 } 436 }
408 437
409 TEST_F(NetworkStateNotifierTest, OnLineNotification) { 438 TEST_F(NetworkStateNotifierTest, OnLineNotification) {
410 StateObserver observer; 439 StateObserver observer;
411 m_notifier.addOnLineObserver(&observer, getTaskRunner()); 440 m_notifier.addOnLineObserver(&observer, getTaskRunner());
412 441
413 setOnLine(true); 442 setOnLine(true);
414 testing::runPendingTasks(); 443 runPendingTasks();
415 EXPECT_TRUE(observer.observedOnLineState()); 444 EXPECT_TRUE(observer.observedOnLineState());
416 EXPECT_EQ(observer.callbackCount(), 1); 445 EXPECT_EQ(observer.callbackCount(), 1);
417 446
418 setOnLine(false); 447 setOnLine(false);
419 testing::runPendingTasks(); 448 runPendingTasks();
420 EXPECT_FALSE(observer.observedOnLineState()); 449 EXPECT_FALSE(observer.observedOnLineState());
421 EXPECT_EQ(observer.callbackCount(), 2); 450 EXPECT_EQ(observer.callbackCount(), 2);
422 451
423 m_notifier.removeConnectionObserver(&observer, getTaskRunner()); 452 m_notifier.removeConnectionObserver(&observer, getTaskRunner());
424 } 453 }
425 454
426 TEST_F(NetworkStateNotifierTest, MultipleObservers) { 455 TEST_F(NetworkStateNotifierTest, MultipleObservers) {
427 StateObserver observer1; 456 StateObserver observer1;
428 StateObserver observer2; 457 StateObserver observer2;
429 458
430 // Observer1 observes online state, Observer2 observes both. 459 // Observer1 observes online state, Observer2 observes both.
431 m_notifier.addOnLineObserver(&observer1, getTaskRunner()); 460 m_notifier.addOnLineObserver(&observer1, getTaskRunner());
432 m_notifier.addConnectionObserver(&observer2, getTaskRunner()); 461 m_notifier.addConnectionObserver(&observer2, getTaskRunner());
433 m_notifier.addOnLineObserver(&observer2, getTaskRunner()); 462 m_notifier.addOnLineObserver(&observer2, getTaskRunner());
434 463
435 m_notifier.setOnLine(true); 464 m_notifier.setOnLine(true);
436 testing::runPendingTasks(); 465 runPendingTasks();
437 EXPECT_TRUE(observer1.observedOnLineState()); 466 EXPECT_TRUE(observer1.observedOnLineState());
438 EXPECT_TRUE(observer2.observedOnLineState()); 467 EXPECT_TRUE(observer2.observedOnLineState());
439 EXPECT_EQ(observer1.callbackCount(), 1); 468 EXPECT_EQ(observer1.callbackCount(), 1);
440 EXPECT_EQ(observer2.callbackCount(), 1); 469 EXPECT_EQ(observer2.callbackCount(), 1);
441 470
442 m_notifier.setOnLine(false); 471 m_notifier.setOnLine(false);
443 testing::runPendingTasks(); 472 runPendingTasks();
444 EXPECT_FALSE(observer1.observedOnLineState()); 473 EXPECT_FALSE(observer1.observedOnLineState());
445 EXPECT_FALSE(observer2.observedOnLineState()); 474 EXPECT_FALSE(observer2.observedOnLineState());
446 EXPECT_EQ(observer1.callbackCount(), 2); 475 EXPECT_EQ(observer1.callbackCount(), 2);
447 EXPECT_EQ(observer2.callbackCount(), 2); 476 EXPECT_EQ(observer2.callbackCount(), 2);
448 477
449 m_notifier.setOnLine(true); 478 m_notifier.setOnLine(true);
450 m_notifier.setWebConnection(WebConnectionTypeEthernet, 479 m_notifier.setWebConnection(WebConnectionTypeEthernet,
451 kEthernetMaxBandwidthMbps); 480 kEthernetMaxBandwidthMbps);
452 testing::runPendingTasks(); 481 runPendingTasks();
453 EXPECT_TRUE(observer1.observedOnLineState()); 482 EXPECT_TRUE(observer1.observedOnLineState());
454 EXPECT_TRUE(observer2.observedOnLineState()); 483 EXPECT_TRUE(observer2.observedOnLineState());
455 EXPECT_TRUE(verifyObservations(observer2, WebConnectionTypeEthernet, 484 EXPECT_TRUE(verifyObservations(observer2, WebConnectionTypeEthernet,
456 kEthernetMaxBandwidthMbps)); 485 kEthernetMaxBandwidthMbps));
457 EXPECT_EQ(observer1.callbackCount(), 3); 486 EXPECT_EQ(observer1.callbackCount(), 3);
458 EXPECT_EQ(observer2.callbackCount(), 4); 487 EXPECT_EQ(observer2.callbackCount(), 4);
459 488
460 m_notifier.removeConnectionObserver(&observer1, getTaskRunner()); 489 m_notifier.removeConnectionObserver(&observer1, getTaskRunner());
461 m_notifier.removeConnectionObserver(&observer2, getTaskRunner()); 490 m_notifier.removeConnectionObserver(&observer2, getTaskRunner());
462 } 491 }
463 492
464 } // namespace blink 493 } // namespace blink
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/platform/network/NetworkStateNotifier.cpp ('k') | third_party/WebKit/Source/web/BUILD.gn » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698