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

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

Issue 1308943005: [NetInfo] Add Blink support for connection.change, connection.downlinkMax, and wimax (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Rebase Created 5 years, 3 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
« no previous file with comments | « Source/core/page/NetworkStateNotifier.cpp ('k') | Source/core/testing/Internals.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 23 matching lines...) Expand all
34 #include "core/dom/Document.h" 34 #include "core/dom/Document.h"
35 #include "platform/testing/UnitTestHelpers.h" 35 #include "platform/testing/UnitTestHelpers.h"
36 #include "public/platform/Platform.h" 36 #include "public/platform/Platform.h"
37 #include "public/platform/WebConnectionType.h" 37 #include "public/platform/WebConnectionType.h"
38 #include "public/platform/WebThread.h" 38 #include "public/platform/WebThread.h"
39 #include "wtf/Functional.h" 39 #include "wtf/Functional.h"
40 #include <gtest/gtest.h> 40 #include <gtest/gtest.h>
41 41
42 namespace blink { 42 namespace blink {
43 43
44 namespace {
45 const double kNoneMaxBandwidthMbps = 0.0;
46 const double kBluetoothMaxBandwidthMbps = 1.0;
47 const double kEthernetMaxBandwidthMbps = 2.0;
48 }
49
44 class StateObserver : public NetworkStateNotifier::NetworkStateObserver { 50 class StateObserver : public NetworkStateNotifier::NetworkStateObserver {
45 public: 51 public:
46 StateObserver() 52 StateObserver()
47 : m_observedType(ConnectionTypeNone) 53 : m_observedType(ConnectionTypeNone)
54 , m_observedMaxBandwidthMbps(0.0)
48 , m_callbackCount(0) 55 , m_callbackCount(0)
49 { 56 {
50 } 57 }
51 58
52 virtual void connectionTypeChange(WebConnectionType type) 59 virtual void connectionChange(WebConnectionType type, double maxBandwidthMbp s)
53 { 60 {
54 m_observedType = type; 61 m_observedType = type;
62 m_observedMaxBandwidthMbps = maxBandwidthMbps;
55 m_callbackCount += 1; 63 m_callbackCount += 1;
56 64
57 if (m_closure) 65 if (m_closure)
58 (*m_closure)(); 66 (*m_closure)();
59 } 67 }
60 68
61 WebConnectionType observedType() const 69 WebConnectionType observedType() const
62 { 70 {
63 return m_observedType; 71 return m_observedType;
64 } 72 }
65 73
74 double observedMaxBandwidth() const
75 {
76 return m_observedMaxBandwidthMbps;
77 }
78
66 int callbackCount() const 79 int callbackCount() const
67 { 80 {
68 return m_callbackCount; 81 return m_callbackCount;
69 } 82 }
70 83
71 void setNotificationCallback(PassOwnPtr<Closure> closure) 84 void setNotificationCallback(PassOwnPtr<Closure> closure)
72 { 85 {
73 m_closure = closure; 86 m_closure = closure;
74 } 87 }
75 88
76 private: 89 private:
77 OwnPtr<Closure> m_closure; 90 OwnPtr<Closure> m_closure;
78 WebConnectionType m_observedType; 91 WebConnectionType m_observedType;
92 double m_observedMaxBandwidthMbps;
79 int m_callbackCount; 93 int m_callbackCount;
80 }; 94 };
81 95
82 class NetworkStateNotifierTest : public ::testing::Test { 96 class NetworkStateNotifierTest : public ::testing::Test {
83 public: 97 public:
84 NetworkStateNotifierTest() 98 NetworkStateNotifierTest()
85 : m_document(Document::create()) 99 : m_document(Document::create())
86 , m_document2(Document::create()) 100 , m_document2(Document::create())
87 { 101 {
88 } 102 }
89 103
90 ExecutionContext* executionContext() 104 ExecutionContext* executionContext()
91 { 105 {
92 return m_document.get(); 106 return m_document.get();
93 } 107 }
94 108
95 ExecutionContext* executionContext2() 109 ExecutionContext* executionContext2()
96 { 110 {
97 return m_document2.get(); 111 return m_document2.get();
98 } 112 }
99 113
100 protected: 114 protected:
101 void setType(WebConnectionType type) 115 void setConnection(WebConnectionType type, double maxBandwidthMbps)
102 { 116 {
103 m_notifier.setWebConnectionType(type); 117 m_notifier.setWebConnection(type, maxBandwidthMbps);
104 testing::runPendingTasks(); 118 testing::runPendingTasks();
105 } 119 }
106 120
107 void addObserverOnNotification(StateObserver* observer, StateObserver* obser verToAdd) 121 void addObserverOnNotification(StateObserver* observer, StateObserver* obser verToAdd)
108 { 122 {
109 observer->setNotificationCallback(bind(&NetworkStateNotifier::addObserve r, &m_notifier, observerToAdd, executionContext())); 123 observer->setNotificationCallback(bind(&NetworkStateNotifier::addObserve r, &m_notifier, observerToAdd, executionContext()));
110 } 124 }
111 125
112 void removeObserverOnNotification(StateObserver* observer, StateObserver* ob serverToRemove) 126 void removeObserverOnNotification(StateObserver* observer, StateObserver* ob serverToRemove)
113 { 127 {
114 observer->setNotificationCallback(bind(&NetworkStateNotifier::removeObse rver, &m_notifier, observerToRemove, executionContext())); 128 observer->setNotificationCallback(bind(&NetworkStateNotifier::removeObse rver, &m_notifier, observerToRemove, executionContext()));
115 } 129 }
116 130
131 bool verifyObservations(const StateObserver& observer, WebConnectionType typ e, double maxBandwidthMbps)
132 {
133 EXPECT_EQ(observer.observedType(), type);
134 EXPECT_EQ(observer.observedMaxBandwidth(), maxBandwidthMbps);
135 return observer.observedType() == type && observer.observedMaxBandwidth( ) == maxBandwidthMbps;
136 }
137
117 RefPtrWillBePersistent<Document> m_document; 138 RefPtrWillBePersistent<Document> m_document;
118 RefPtrWillBePersistent<Document> m_document2; 139 RefPtrWillBePersistent<Document> m_document2;
119 NetworkStateNotifier m_notifier; 140 NetworkStateNotifier m_notifier;
120 }; 141 };
121 142
122 TEST_F(NetworkStateNotifierTest, AddObserver) 143 TEST_F(NetworkStateNotifierTest, AddObserver)
123 { 144 {
124 StateObserver observer; 145 StateObserver observer;
125 m_notifier.addObserver(&observer, executionContext()); 146 m_notifier.addObserver(&observer, executionContext());
126 EXPECT_EQ(observer.observedType(), ConnectionTypeNone); 147 EXPECT_TRUE(verifyObservations(observer, ConnectionTypeNone, kNoneMaxBandwid thMbps));
127 148
128 setType(ConnectionTypeBluetooth); 149 setConnection(ConnectionTypeBluetooth, kBluetoothMaxBandwidthMbps);
129 EXPECT_EQ(observer.observedType(), ConnectionTypeBluetooth); 150 EXPECT_TRUE(verifyObservations(observer, ConnectionTypeBluetooth, kBluetooth MaxBandwidthMbps));
130 EXPECT_EQ(observer.callbackCount(), 1); 151 EXPECT_EQ(observer.callbackCount(), 1);
131 } 152 }
132 153
133 TEST_F(NetworkStateNotifierTest, RemoveObserver) 154 TEST_F(NetworkStateNotifierTest, RemoveObserver)
134 { 155 {
135 StateObserver observer1, observer2; 156 StateObserver observer1, observer2;
136 m_notifier.addObserver(&observer1, executionContext()); 157 m_notifier.addObserver(&observer1, executionContext());
137 m_notifier.removeObserver(&observer1, executionContext()); 158 m_notifier.removeObserver(&observer1, executionContext());
138 m_notifier.addObserver(&observer2, executionContext()); 159 m_notifier.addObserver(&observer2, executionContext());
139 160
140 setType(ConnectionTypeBluetooth); 161 setConnection(ConnectionTypeBluetooth, kBluetoothMaxBandwidthMbps);
141 EXPECT_EQ(observer1.observedType(), ConnectionTypeNone); 162 EXPECT_TRUE(verifyObservations(observer1, ConnectionTypeNone, kNoneMaxBandwi dthMbps));
142 EXPECT_EQ(observer2.observedType(), ConnectionTypeBluetooth); 163 EXPECT_TRUE(verifyObservations(observer2, ConnectionTypeBluetooth, kBluetoot hMaxBandwidthMbps));
143 } 164 }
144 165
145 TEST_F(NetworkStateNotifierTest, RemoveSoleObserver) 166 TEST_F(NetworkStateNotifierTest, RemoveSoleObserver)
146 { 167 {
147 StateObserver observer1, observer2; 168 StateObserver observer1;
148 m_notifier.addObserver(&observer1, executionContext()); 169 m_notifier.addObserver(&observer1, executionContext());
149 m_notifier.removeObserver(&observer1, executionContext()); 170 m_notifier.removeObserver(&observer1, executionContext());
150 171
151 setType(ConnectionTypeBluetooth); 172 setConnection(ConnectionTypeBluetooth, kBluetoothMaxBandwidthMbps);
152 EXPECT_EQ(observer1.observedType(), ConnectionTypeNone); 173 EXPECT_TRUE(verifyObservations(observer1, ConnectionTypeNone, kNoneMaxBandwi dthMbps));
153 } 174 }
154 175
155 TEST_F(NetworkStateNotifierTest, AddObserverWhileNotifying) 176 TEST_F(NetworkStateNotifierTest, AddObserverWhileNotifying)
156 { 177 {
157 StateObserver observer1, observer2; 178 StateObserver observer1, observer2;
158 m_notifier.addObserver(&observer1, executionContext()); 179 m_notifier.addObserver(&observer1, executionContext());
159 addObserverOnNotification(&observer1, &observer2); 180 addObserverOnNotification(&observer1, &observer2);
160 181
161 setType(ConnectionTypeBluetooth); 182 setConnection(ConnectionTypeBluetooth, kBluetoothMaxBandwidthMbps);
162 EXPECT_EQ(observer1.observedType(), ConnectionTypeBluetooth); 183 EXPECT_TRUE(verifyObservations(observer1, ConnectionTypeBluetooth, kBluetoot hMaxBandwidthMbps));
163 EXPECT_EQ(observer2.observedType(), ConnectionTypeBluetooth); 184 EXPECT_TRUE(verifyObservations(observer2, ConnectionTypeBluetooth, kBluetoot hMaxBandwidthMbps));
164 } 185 }
165 186
166 TEST_F(NetworkStateNotifierTest, RemoveSoleObserverWhileNotifying) 187 TEST_F(NetworkStateNotifierTest, RemoveSoleObserverWhileNotifying)
167 { 188 {
168 StateObserver observer1; 189 StateObserver observer1;
169 m_notifier.addObserver(&observer1, executionContext()); 190 m_notifier.addObserver(&observer1, executionContext());
170 removeObserverOnNotification(&observer1, &observer1); 191 removeObserverOnNotification(&observer1, &observer1);
171 192
172 setType(ConnectionTypeBluetooth); 193 setConnection(ConnectionTypeBluetooth, kBluetoothMaxBandwidthMbps);
173 EXPECT_EQ(observer1.observedType(), ConnectionTypeBluetooth); 194 EXPECT_TRUE(verifyObservations(observer1, ConnectionTypeBluetooth, kBluetoot hMaxBandwidthMbps));
174 195
175 setType(ConnectionTypeEthernet); 196 setConnection(ConnectionTypeEthernet, kEthernetMaxBandwidthMbps);
176 EXPECT_EQ(observer1.observedType(), ConnectionTypeBluetooth); 197 EXPECT_TRUE(verifyObservations(observer1, ConnectionTypeBluetooth, kBluetoot hMaxBandwidthMbps));
177 } 198 }
178 199
179 TEST_F(NetworkStateNotifierTest, RemoveCurrentObserverWhileNotifying) 200 TEST_F(NetworkStateNotifierTest, RemoveCurrentObserverWhileNotifying)
180 { 201 {
181 StateObserver observer1, observer2; 202 StateObserver observer1, observer2;
182 m_notifier.addObserver(&observer1, executionContext()); 203 m_notifier.addObserver(&observer1, executionContext());
183 m_notifier.addObserver(&observer2, executionContext()); 204 m_notifier.addObserver(&observer2, executionContext());
184 removeObserverOnNotification(&observer1, &observer1); 205 removeObserverOnNotification(&observer1, &observer1);
185 206
186 setType(ConnectionTypeBluetooth); 207 setConnection(ConnectionTypeBluetooth, kBluetoothMaxBandwidthMbps);
187 EXPECT_EQ(observer1.observedType(), ConnectionTypeBluetooth); 208 EXPECT_TRUE(verifyObservations(observer1, ConnectionTypeBluetooth, kBluetoot hMaxBandwidthMbps));
188 EXPECT_EQ(observer2.observedType(), ConnectionTypeBluetooth); 209 EXPECT_TRUE(verifyObservations(observer2, ConnectionTypeBluetooth, kBluetoot hMaxBandwidthMbps));
189 210
190 setType(ConnectionTypeEthernet); 211 setConnection(ConnectionTypeEthernet, kEthernetMaxBandwidthMbps);
191 EXPECT_EQ(observer1.observedType(), ConnectionTypeBluetooth); 212 EXPECT_TRUE(verifyObservations(observer1, ConnectionTypeBluetooth, kBluetoot hMaxBandwidthMbps));
192 EXPECT_EQ(observer2.observedType(), ConnectionTypeEthernet); 213 EXPECT_TRUE(verifyObservations(observer2, ConnectionTypeEthernet, kEthernetM axBandwidthMbps));
193 } 214 }
194 215
195 TEST_F(NetworkStateNotifierTest, RemovePastObserverWhileNotifying) 216 TEST_F(NetworkStateNotifierTest, RemovePastObserverWhileNotifying)
196 { 217 {
197 StateObserver observer1, observer2; 218 StateObserver observer1, observer2;
198 m_notifier.addObserver(&observer1, executionContext()); 219 m_notifier.addObserver(&observer1, executionContext());
199 m_notifier.addObserver(&observer2, executionContext()); 220 m_notifier.addObserver(&observer2, executionContext());
200 removeObserverOnNotification(&observer2, &observer1); 221 removeObserverOnNotification(&observer2, &observer1);
201 222
202 setType(ConnectionTypeBluetooth); 223 setConnection(ConnectionTypeBluetooth, kBluetoothMaxBandwidthMbps);
203 EXPECT_EQ(observer1.observedType(), ConnectionTypeBluetooth); 224 EXPECT_EQ(observer1.observedType(), ConnectionTypeBluetooth);
204 EXPECT_EQ(observer2.observedType(), ConnectionTypeBluetooth); 225 EXPECT_EQ(observer2.observedType(), ConnectionTypeBluetooth);
205 226
206 setType(ConnectionTypeEthernet); 227 setConnection(ConnectionTypeEthernet, kEthernetMaxBandwidthMbps);
207 EXPECT_EQ(observer1.observedType(), ConnectionTypeBluetooth); 228 EXPECT_TRUE(verifyObservations(observer1, ConnectionTypeBluetooth, kBluetoot hMaxBandwidthMbps));
208 EXPECT_EQ(observer2.observedType(), ConnectionTypeEthernet); 229 EXPECT_TRUE(verifyObservations(observer2, ConnectionTypeEthernet, kEthernetM axBandwidthMbps));
209 } 230 }
210 231
211 TEST_F(NetworkStateNotifierTest, RemoveFutureObserverWhileNotifying) 232 TEST_F(NetworkStateNotifierTest, RemoveFutureObserverWhileNotifying)
212 { 233 {
213 StateObserver observer1, observer2, observer3; 234 StateObserver observer1, observer2, observer3;
214 m_notifier.addObserver(&observer1, executionContext()); 235 m_notifier.addObserver(&observer1, executionContext());
215 m_notifier.addObserver(&observer2, executionContext()); 236 m_notifier.addObserver(&observer2, executionContext());
216 m_notifier.addObserver(&observer3, executionContext()); 237 m_notifier.addObserver(&observer3, executionContext());
217 removeObserverOnNotification(&observer1, &observer2); 238 removeObserverOnNotification(&observer1, &observer2);
218 239
219 setType(ConnectionTypeBluetooth); 240 setConnection(ConnectionTypeBluetooth, kBluetoothMaxBandwidthMbps);
220 241 EXPECT_TRUE(verifyObservations(observer1, ConnectionTypeBluetooth, kBluetoot hMaxBandwidthMbps));
221 EXPECT_EQ(observer1.observedType(), ConnectionTypeBluetooth); 242 EXPECT_TRUE(verifyObservations(observer2, ConnectionTypeNone, kNoneMaxBandwi dthMbps));
222 EXPECT_EQ(observer2.observedType(), ConnectionTypeNone); 243 EXPECT_TRUE(verifyObservations(observer3, ConnectionTypeBluetooth, kBluetoot hMaxBandwidthMbps));
223 EXPECT_EQ(observer3.observedType(), ConnectionTypeBluetooth);
224 } 244 }
225 245
226 TEST_F(NetworkStateNotifierTest, MultipleContextsAddObserver) 246 TEST_F(NetworkStateNotifierTest, MultipleContextsAddObserver)
227 { 247 {
228 StateObserver observer1, observer2; 248 StateObserver observer1, observer2;
229 m_notifier.addObserver(&observer1, executionContext()); 249 m_notifier.addObserver(&observer1, executionContext());
230 m_notifier.addObserver(&observer2, executionContext2()); 250 m_notifier.addObserver(&observer2, executionContext2());
231 251
232 setType(ConnectionTypeBluetooth); 252 setConnection(ConnectionTypeBluetooth, kBluetoothMaxBandwidthMbps);
233 EXPECT_EQ(observer1.observedType(), ConnectionTypeBluetooth); 253 EXPECT_TRUE(verifyObservations(observer1, ConnectionTypeBluetooth, kBluetoot hMaxBandwidthMbps));
234 EXPECT_EQ(observer2.observedType(), ConnectionTypeBluetooth); 254 EXPECT_TRUE(verifyObservations(observer2, ConnectionTypeBluetooth, kBluetoot hMaxBandwidthMbps));
235 } 255 }
236 256
237 TEST_F(NetworkStateNotifierTest, RemoveContext) 257 TEST_F(NetworkStateNotifierTest, RemoveContext)
238 { 258 {
239 StateObserver observer1, observer2; 259 StateObserver observer1, observer2;
240 m_notifier.addObserver(&observer1, executionContext()); 260 m_notifier.addObserver(&observer1, executionContext());
241 m_notifier.addObserver(&observer2, executionContext2()); 261 m_notifier.addObserver(&observer2, executionContext2());
242 m_notifier.removeObserver(&observer2, executionContext2()); 262 m_notifier.removeObserver(&observer2, executionContext2());
243 263
244 setType(ConnectionTypeBluetooth); 264 setConnection(ConnectionTypeBluetooth, kBluetoothMaxBandwidthMbps);
245 EXPECT_EQ(observer1.observedType(), ConnectionTypeBluetooth); 265 EXPECT_TRUE(verifyObservations(observer1, ConnectionTypeBluetooth, kBluetoot hMaxBandwidthMbps));
246 EXPECT_EQ(observer2.observedType(), ConnectionTypeNone); 266 EXPECT_TRUE(verifyObservations(observer2, ConnectionTypeNone, kNoneMaxBandwi dthMbps));
247 } 267 }
248 268
249 TEST_F(NetworkStateNotifierTest, RemoveAllContexts) 269 TEST_F(NetworkStateNotifierTest, RemoveAllContexts)
250 { 270 {
251 StateObserver observer1, observer2; 271 StateObserver observer1, observer2;
252 m_notifier.addObserver(&observer1, executionContext()); 272 m_notifier.addObserver(&observer1, executionContext());
253 m_notifier.addObserver(&observer2, executionContext2()); 273 m_notifier.addObserver(&observer2, executionContext2());
254 m_notifier.removeObserver(&observer1, executionContext()); 274 m_notifier.removeObserver(&observer1, executionContext());
255 m_notifier.removeObserver(&observer2, executionContext2()); 275 m_notifier.removeObserver(&observer2, executionContext2());
256 276
257 setType(ConnectionTypeBluetooth); 277 setConnection(ConnectionTypeBluetooth, kBluetoothMaxBandwidthMbps);
258 EXPECT_EQ(observer1.observedType(), ConnectionTypeNone); 278 EXPECT_TRUE(verifyObservations(observer1, ConnectionTypeNone, kNoneMaxBandwi dthMbps));
259 EXPECT_EQ(observer2.observedType(), ConnectionTypeNone); 279 EXPECT_TRUE(verifyObservations(observer2, ConnectionTypeNone, kNoneMaxBandwi dthMbps));
260 } 280 }
261 281
262 } // namespace blink 282 } // namespace blink
OLDNEW
« no previous file with comments | « Source/core/page/NetworkStateNotifier.cpp ('k') | Source/core/testing/Internals.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698