OLD | NEW |
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 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
93 }; | 93 }; |
94 | 94 |
95 class NetworkStateNotifierTest : public ::testing::Test { | 95 class NetworkStateNotifierTest : public ::testing::Test { |
96 public: | 96 public: |
97 NetworkStateNotifierTest() | 97 NetworkStateNotifierTest() |
98 : m_document(Document::create()) | 98 : m_document(Document::create()) |
99 , m_document2(Document::create()) | 99 , m_document2(Document::create()) |
100 { | 100 { |
101 } | 101 } |
102 | 102 |
103 ExecutionContext* executionContext() | 103 ExecutionContext* getExecutionContext() |
104 { | 104 { |
105 return m_document.get(); | 105 return m_document.get(); |
106 } | 106 } |
107 | 107 |
108 ExecutionContext* executionContext2() | 108 ExecutionContext* executionContext2() |
109 { | 109 { |
110 return m_document2.get(); | 110 return m_document2.get(); |
111 } | 111 } |
112 | 112 |
113 protected: | 113 protected: |
114 void setConnection(WebConnectionType type, double maxBandwidthMbps) | 114 void setConnection(WebConnectionType type, double maxBandwidthMbps) |
115 { | 115 { |
116 m_notifier.setWebConnection(type, maxBandwidthMbps); | 116 m_notifier.setWebConnection(type, maxBandwidthMbps); |
117 testing::runPendingTasks(); | 117 testing::runPendingTasks(); |
118 } | 118 } |
119 | 119 |
120 void addObserverOnNotification(StateObserver* observer, StateObserver* obser
verToAdd) | 120 void addObserverOnNotification(StateObserver* observer, StateObserver* obser
verToAdd) |
121 { | 121 { |
122 observer->setNotificationCallback(bind(&NetworkStateNotifier::addObserve
r, &m_notifier, observerToAdd, executionContext())); | 122 observer->setNotificationCallback(bind(&NetworkStateNotifier::addObserve
r, &m_notifier, observerToAdd, getExecutionContext())); |
123 } | 123 } |
124 | 124 |
125 void removeObserverOnNotification(StateObserver* observer, StateObserver* ob
serverToRemove) | 125 void removeObserverOnNotification(StateObserver* observer, StateObserver* ob
serverToRemove) |
126 { | 126 { |
127 observer->setNotificationCallback(bind(&NetworkStateNotifier::removeObse
rver, &m_notifier, observerToRemove, executionContext())); | 127 observer->setNotificationCallback(bind(&NetworkStateNotifier::removeObse
rver, &m_notifier, observerToRemove, getExecutionContext())); |
128 } | 128 } |
129 | 129 |
130 bool verifyObservations(const StateObserver& observer, WebConnectionType typ
e, double maxBandwidthMbps) | 130 bool verifyObservations(const StateObserver& observer, WebConnectionType typ
e, double maxBandwidthMbps) |
131 { | 131 { |
132 EXPECT_EQ(observer.observedType(), type); | 132 EXPECT_EQ(observer.observedType(), type); |
133 EXPECT_EQ(observer.observedMaxBandwidth(), maxBandwidthMbps); | 133 EXPECT_EQ(observer.observedMaxBandwidth(), maxBandwidthMbps); |
134 return observer.observedType() == type && observer.observedMaxBandwidth(
) == maxBandwidthMbps; | 134 return observer.observedType() == type && observer.observedMaxBandwidth(
) == maxBandwidthMbps; |
135 } | 135 } |
136 | 136 |
137 RefPtrWillBePersistent<Document> m_document; | 137 RefPtrWillBePersistent<Document> m_document; |
138 RefPtrWillBePersistent<Document> m_document2; | 138 RefPtrWillBePersistent<Document> m_document2; |
139 NetworkStateNotifier m_notifier; | 139 NetworkStateNotifier m_notifier; |
140 }; | 140 }; |
141 | 141 |
142 TEST_F(NetworkStateNotifierTest, AddObserver) | 142 TEST_F(NetworkStateNotifierTest, AddObserver) |
143 { | 143 { |
144 StateObserver observer; | 144 StateObserver observer; |
145 m_notifier.addObserver(&observer, executionContext()); | 145 m_notifier.addObserver(&observer, getExecutionContext()); |
146 EXPECT_TRUE(verifyObservations(observer, WebConnectionTypeNone, kNoneMaxBand
widthMbps)); | 146 EXPECT_TRUE(verifyObservations(observer, WebConnectionTypeNone, kNoneMaxBand
widthMbps)); |
147 | 147 |
148 setConnection(WebConnectionTypeBluetooth, kBluetoothMaxBandwidthMbps); | 148 setConnection(WebConnectionTypeBluetooth, kBluetoothMaxBandwidthMbps); |
149 EXPECT_TRUE(verifyObservations(observer, WebConnectionTypeBluetooth, kBlueto
othMaxBandwidthMbps)); | 149 EXPECT_TRUE(verifyObservations(observer, WebConnectionTypeBluetooth, kBlueto
othMaxBandwidthMbps)); |
150 EXPECT_EQ(observer.callbackCount(), 1); | 150 EXPECT_EQ(observer.callbackCount(), 1); |
151 } | 151 } |
152 | 152 |
153 TEST_F(NetworkStateNotifierTest, RemoveObserver) | 153 TEST_F(NetworkStateNotifierTest, RemoveObserver) |
154 { | 154 { |
155 StateObserver observer1, observer2; | 155 StateObserver observer1, observer2; |
156 m_notifier.addObserver(&observer1, executionContext()); | 156 m_notifier.addObserver(&observer1, getExecutionContext()); |
157 m_notifier.removeObserver(&observer1, executionContext()); | 157 m_notifier.removeObserver(&observer1, getExecutionContext()); |
158 m_notifier.addObserver(&observer2, executionContext()); | 158 m_notifier.addObserver(&observer2, getExecutionContext()); |
159 | 159 |
160 setConnection(WebConnectionTypeBluetooth, kBluetoothMaxBandwidthMbps); | 160 setConnection(WebConnectionTypeBluetooth, kBluetoothMaxBandwidthMbps); |
161 EXPECT_TRUE(verifyObservations(observer1, WebConnectionTypeNone, kNoneMaxBan
dwidthMbps)); | 161 EXPECT_TRUE(verifyObservations(observer1, WebConnectionTypeNone, kNoneMaxBan
dwidthMbps)); |
162 EXPECT_TRUE(verifyObservations(observer2, WebConnectionTypeBluetooth, kBluet
oothMaxBandwidthMbps)); | 162 EXPECT_TRUE(verifyObservations(observer2, WebConnectionTypeBluetooth, kBluet
oothMaxBandwidthMbps)); |
163 } | 163 } |
164 | 164 |
165 TEST_F(NetworkStateNotifierTest, RemoveSoleObserver) | 165 TEST_F(NetworkStateNotifierTest, RemoveSoleObserver) |
166 { | 166 { |
167 StateObserver observer1; | 167 StateObserver observer1; |
168 m_notifier.addObserver(&observer1, executionContext()); | 168 m_notifier.addObserver(&observer1, getExecutionContext()); |
169 m_notifier.removeObserver(&observer1, executionContext()); | 169 m_notifier.removeObserver(&observer1, getExecutionContext()); |
170 | 170 |
171 setConnection(WebConnectionTypeBluetooth, kBluetoothMaxBandwidthMbps); | 171 setConnection(WebConnectionTypeBluetooth, kBluetoothMaxBandwidthMbps); |
172 EXPECT_TRUE(verifyObservations(observer1, WebConnectionTypeNone, kNoneMaxBan
dwidthMbps)); | 172 EXPECT_TRUE(verifyObservations(observer1, WebConnectionTypeNone, kNoneMaxBan
dwidthMbps)); |
173 } | 173 } |
174 | 174 |
175 TEST_F(NetworkStateNotifierTest, AddObserverWhileNotifying) | 175 TEST_F(NetworkStateNotifierTest, AddObserverWhileNotifying) |
176 { | 176 { |
177 StateObserver observer1, observer2; | 177 StateObserver observer1, observer2; |
178 m_notifier.addObserver(&observer1, executionContext()); | 178 m_notifier.addObserver(&observer1, getExecutionContext()); |
179 addObserverOnNotification(&observer1, &observer2); | 179 addObserverOnNotification(&observer1, &observer2); |
180 | 180 |
181 setConnection(WebConnectionTypeBluetooth, kBluetoothMaxBandwidthMbps); | 181 setConnection(WebConnectionTypeBluetooth, kBluetoothMaxBandwidthMbps); |
182 EXPECT_TRUE(verifyObservations(observer1, WebConnectionTypeBluetooth, kBluet
oothMaxBandwidthMbps)); | 182 EXPECT_TRUE(verifyObservations(observer1, WebConnectionTypeBluetooth, kBluet
oothMaxBandwidthMbps)); |
183 EXPECT_TRUE(verifyObservations(observer2, WebConnectionTypeBluetooth, kBluet
oothMaxBandwidthMbps)); | 183 EXPECT_TRUE(verifyObservations(observer2, WebConnectionTypeBluetooth, kBluet
oothMaxBandwidthMbps)); |
184 } | 184 } |
185 | 185 |
186 TEST_F(NetworkStateNotifierTest, RemoveSoleObserverWhileNotifying) | 186 TEST_F(NetworkStateNotifierTest, RemoveSoleObserverWhileNotifying) |
187 { | 187 { |
188 StateObserver observer1; | 188 StateObserver observer1; |
189 m_notifier.addObserver(&observer1, executionContext()); | 189 m_notifier.addObserver(&observer1, getExecutionContext()); |
190 removeObserverOnNotification(&observer1, &observer1); | 190 removeObserverOnNotification(&observer1, &observer1); |
191 | 191 |
192 setConnection(WebConnectionTypeBluetooth, kBluetoothMaxBandwidthMbps); | 192 setConnection(WebConnectionTypeBluetooth, kBluetoothMaxBandwidthMbps); |
193 EXPECT_TRUE(verifyObservations(observer1, WebConnectionTypeBluetooth, kBluet
oothMaxBandwidthMbps)); | 193 EXPECT_TRUE(verifyObservations(observer1, WebConnectionTypeBluetooth, kBluet
oothMaxBandwidthMbps)); |
194 | 194 |
195 setConnection(WebConnectionTypeEthernet, kEthernetMaxBandwidthMbps); | 195 setConnection(WebConnectionTypeEthernet, kEthernetMaxBandwidthMbps); |
196 EXPECT_TRUE(verifyObservations(observer1, WebConnectionTypeBluetooth, kBluet
oothMaxBandwidthMbps)); | 196 EXPECT_TRUE(verifyObservations(observer1, WebConnectionTypeBluetooth, kBluet
oothMaxBandwidthMbps)); |
197 } | 197 } |
198 | 198 |
199 TEST_F(NetworkStateNotifierTest, RemoveCurrentObserverWhileNotifying) | 199 TEST_F(NetworkStateNotifierTest, RemoveCurrentObserverWhileNotifying) |
200 { | 200 { |
201 StateObserver observer1, observer2; | 201 StateObserver observer1, observer2; |
202 m_notifier.addObserver(&observer1, executionContext()); | 202 m_notifier.addObserver(&observer1, getExecutionContext()); |
203 m_notifier.addObserver(&observer2, executionContext()); | 203 m_notifier.addObserver(&observer2, getExecutionContext()); |
204 removeObserverOnNotification(&observer1, &observer1); | 204 removeObserverOnNotification(&observer1, &observer1); |
205 | 205 |
206 setConnection(WebConnectionTypeBluetooth, kBluetoothMaxBandwidthMbps); | 206 setConnection(WebConnectionTypeBluetooth, kBluetoothMaxBandwidthMbps); |
207 EXPECT_TRUE(verifyObservations(observer1, WebConnectionTypeBluetooth, kBluet
oothMaxBandwidthMbps)); | 207 EXPECT_TRUE(verifyObservations(observer1, WebConnectionTypeBluetooth, kBluet
oothMaxBandwidthMbps)); |
208 EXPECT_TRUE(verifyObservations(observer2, WebConnectionTypeBluetooth, kBluet
oothMaxBandwidthMbps)); | 208 EXPECT_TRUE(verifyObservations(observer2, WebConnectionTypeBluetooth, kBluet
oothMaxBandwidthMbps)); |
209 | 209 |
210 setConnection(WebConnectionTypeEthernet, kEthernetMaxBandwidthMbps); | 210 setConnection(WebConnectionTypeEthernet, kEthernetMaxBandwidthMbps); |
211 EXPECT_TRUE(verifyObservations(observer1, WebConnectionTypeBluetooth, kBluet
oothMaxBandwidthMbps)); | 211 EXPECT_TRUE(verifyObservations(observer1, WebConnectionTypeBluetooth, kBluet
oothMaxBandwidthMbps)); |
212 EXPECT_TRUE(verifyObservations(observer2, WebConnectionTypeEthernet, kEthern
etMaxBandwidthMbps)); | 212 EXPECT_TRUE(verifyObservations(observer2, WebConnectionTypeEthernet, kEthern
etMaxBandwidthMbps)); |
213 } | 213 } |
214 | 214 |
215 TEST_F(NetworkStateNotifierTest, RemovePastObserverWhileNotifying) | 215 TEST_F(NetworkStateNotifierTest, RemovePastObserverWhileNotifying) |
216 { | 216 { |
217 StateObserver observer1, observer2; | 217 StateObserver observer1, observer2; |
218 m_notifier.addObserver(&observer1, executionContext()); | 218 m_notifier.addObserver(&observer1, getExecutionContext()); |
219 m_notifier.addObserver(&observer2, executionContext()); | 219 m_notifier.addObserver(&observer2, getExecutionContext()); |
220 removeObserverOnNotification(&observer2, &observer1); | 220 removeObserverOnNotification(&observer2, &observer1); |
221 | 221 |
222 setConnection(WebConnectionTypeBluetooth, kBluetoothMaxBandwidthMbps); | 222 setConnection(WebConnectionTypeBluetooth, kBluetoothMaxBandwidthMbps); |
223 EXPECT_EQ(observer1.observedType(), WebConnectionTypeBluetooth); | 223 EXPECT_EQ(observer1.observedType(), WebConnectionTypeBluetooth); |
224 EXPECT_EQ(observer2.observedType(), WebConnectionTypeBluetooth); | 224 EXPECT_EQ(observer2.observedType(), WebConnectionTypeBluetooth); |
225 | 225 |
226 setConnection(WebConnectionTypeEthernet, kEthernetMaxBandwidthMbps); | 226 setConnection(WebConnectionTypeEthernet, kEthernetMaxBandwidthMbps); |
227 EXPECT_TRUE(verifyObservations(observer1, WebConnectionTypeBluetooth, kBluet
oothMaxBandwidthMbps)); | 227 EXPECT_TRUE(verifyObservations(observer1, WebConnectionTypeBluetooth, kBluet
oothMaxBandwidthMbps)); |
228 EXPECT_TRUE(verifyObservations(observer2, WebConnectionTypeEthernet, kEthern
etMaxBandwidthMbps)); | 228 EXPECT_TRUE(verifyObservations(observer2, WebConnectionTypeEthernet, kEthern
etMaxBandwidthMbps)); |
229 } | 229 } |
230 | 230 |
231 TEST_F(NetworkStateNotifierTest, RemoveFutureObserverWhileNotifying) | 231 TEST_F(NetworkStateNotifierTest, RemoveFutureObserverWhileNotifying) |
232 { | 232 { |
233 StateObserver observer1, observer2, observer3; | 233 StateObserver observer1, observer2, observer3; |
234 m_notifier.addObserver(&observer1, executionContext()); | 234 m_notifier.addObserver(&observer1, getExecutionContext()); |
235 m_notifier.addObserver(&observer2, executionContext()); | 235 m_notifier.addObserver(&observer2, getExecutionContext()); |
236 m_notifier.addObserver(&observer3, executionContext()); | 236 m_notifier.addObserver(&observer3, getExecutionContext()); |
237 removeObserverOnNotification(&observer1, &observer2); | 237 removeObserverOnNotification(&observer1, &observer2); |
238 | 238 |
239 setConnection(WebConnectionTypeBluetooth, kBluetoothMaxBandwidthMbps); | 239 setConnection(WebConnectionTypeBluetooth, kBluetoothMaxBandwidthMbps); |
240 EXPECT_TRUE(verifyObservations(observer1, WebConnectionTypeBluetooth, kBluet
oothMaxBandwidthMbps)); | 240 EXPECT_TRUE(verifyObservations(observer1, WebConnectionTypeBluetooth, kBluet
oothMaxBandwidthMbps)); |
241 EXPECT_TRUE(verifyObservations(observer2, WebConnectionTypeNone, kNoneMaxBan
dwidthMbps)); | 241 EXPECT_TRUE(verifyObservations(observer2, WebConnectionTypeNone, kNoneMaxBan
dwidthMbps)); |
242 EXPECT_TRUE(verifyObservations(observer3, WebConnectionTypeBluetooth, kBluet
oothMaxBandwidthMbps)); | 242 EXPECT_TRUE(verifyObservations(observer3, WebConnectionTypeBluetooth, kBluet
oothMaxBandwidthMbps)); |
243 } | 243 } |
244 | 244 |
245 TEST_F(NetworkStateNotifierTest, MultipleContextsAddObserver) | 245 TEST_F(NetworkStateNotifierTest, MultipleContextsAddObserver) |
246 { | 246 { |
247 StateObserver observer1, observer2; | 247 StateObserver observer1, observer2; |
248 m_notifier.addObserver(&observer1, executionContext()); | 248 m_notifier.addObserver(&observer1, getExecutionContext()); |
249 m_notifier.addObserver(&observer2, executionContext2()); | 249 m_notifier.addObserver(&observer2, executionContext2()); |
250 | 250 |
251 setConnection(WebConnectionTypeBluetooth, kBluetoothMaxBandwidthMbps); | 251 setConnection(WebConnectionTypeBluetooth, kBluetoothMaxBandwidthMbps); |
252 EXPECT_TRUE(verifyObservations(observer1, WebConnectionTypeBluetooth, kBluet
oothMaxBandwidthMbps)); | 252 EXPECT_TRUE(verifyObservations(observer1, WebConnectionTypeBluetooth, kBluet
oothMaxBandwidthMbps)); |
253 EXPECT_TRUE(verifyObservations(observer2, WebConnectionTypeBluetooth, kBluet
oothMaxBandwidthMbps)); | 253 EXPECT_TRUE(verifyObservations(observer2, WebConnectionTypeBluetooth, kBluet
oothMaxBandwidthMbps)); |
254 } | 254 } |
255 | 255 |
256 TEST_F(NetworkStateNotifierTest, RemoveContext) | 256 TEST_F(NetworkStateNotifierTest, RemoveContext) |
257 { | 257 { |
258 StateObserver observer1, observer2; | 258 StateObserver observer1, observer2; |
259 m_notifier.addObserver(&observer1, executionContext()); | 259 m_notifier.addObserver(&observer1, getExecutionContext()); |
260 m_notifier.addObserver(&observer2, executionContext2()); | 260 m_notifier.addObserver(&observer2, executionContext2()); |
261 m_notifier.removeObserver(&observer2, executionContext2()); | 261 m_notifier.removeObserver(&observer2, executionContext2()); |
262 | 262 |
263 setConnection(WebConnectionTypeBluetooth, kBluetoothMaxBandwidthMbps); | 263 setConnection(WebConnectionTypeBluetooth, kBluetoothMaxBandwidthMbps); |
264 EXPECT_TRUE(verifyObservations(observer1, WebConnectionTypeBluetooth, kBluet
oothMaxBandwidthMbps)); | 264 EXPECT_TRUE(verifyObservations(observer1, WebConnectionTypeBluetooth, kBluet
oothMaxBandwidthMbps)); |
265 EXPECT_TRUE(verifyObservations(observer2, WebConnectionTypeNone, kNoneMaxBan
dwidthMbps)); | 265 EXPECT_TRUE(verifyObservations(observer2, WebConnectionTypeNone, kNoneMaxBan
dwidthMbps)); |
266 } | 266 } |
267 | 267 |
268 TEST_F(NetworkStateNotifierTest, RemoveAllContexts) | 268 TEST_F(NetworkStateNotifierTest, RemoveAllContexts) |
269 { | 269 { |
270 StateObserver observer1, observer2; | 270 StateObserver observer1, observer2; |
271 m_notifier.addObserver(&observer1, executionContext()); | 271 m_notifier.addObserver(&observer1, getExecutionContext()); |
272 m_notifier.addObserver(&observer2, executionContext2()); | 272 m_notifier.addObserver(&observer2, executionContext2()); |
273 m_notifier.removeObserver(&observer1, executionContext()); | 273 m_notifier.removeObserver(&observer1, getExecutionContext()); |
274 m_notifier.removeObserver(&observer2, executionContext2()); | 274 m_notifier.removeObserver(&observer2, executionContext2()); |
275 | 275 |
276 setConnection(WebConnectionTypeBluetooth, kBluetoothMaxBandwidthMbps); | 276 setConnection(WebConnectionTypeBluetooth, kBluetoothMaxBandwidthMbps); |
277 EXPECT_TRUE(verifyObservations(observer1, WebConnectionTypeNone, kNoneMaxBan
dwidthMbps)); | 277 EXPECT_TRUE(verifyObservations(observer1, WebConnectionTypeNone, kNoneMaxBan
dwidthMbps)); |
278 EXPECT_TRUE(verifyObservations(observer2, WebConnectionTypeNone, kNoneMaxBan
dwidthMbps)); | 278 EXPECT_TRUE(verifyObservations(observer2, WebConnectionTypeNone, kNoneMaxBan
dwidthMbps)); |
279 } | 279 } |
280 | 280 |
281 } // namespace blink | 281 } // namespace blink |
OLD | NEW |