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 23 matching lines...) Expand all Loading... |
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 kNoneMaxBandwidth = 0.0; |
| 46 const double kBluetoothMaxBandwidth = 1.0; |
| 47 const double kEthernetMaxBandwidth = 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_observedMaxBandwidth(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 maxBandwidth) |
53 { | 60 { |
54 m_observedType = type; | 61 m_observedType = type; |
| 62 m_observedMaxBandwidth = maxBandwidth; |
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_observedMaxBandwidth; |
| 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_observedMaxBandwidth; |
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 maxBandwidth) |
102 { | 116 { |
103 m_notifier.setWebConnectionType(type); | 117 m_notifier.setWebConnection(type, maxBandwidth); |
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 maxBandwidth) |
| 132 { |
| 133 EXPECT_EQ(observer.observedType(), type); |
| 134 EXPECT_EQ(observer.observedMaxBandwidth(), maxBandwidth); |
| 135 return observer.observedType() == type && observer.observedMaxBandwidth(
) == maxBandwidth; |
| 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
th)); |
127 | 148 |
128 setType(ConnectionTypeBluetooth); | 149 setConnection(ConnectionTypeBluetooth, kBluetoothMaxBandwidth); |
129 EXPECT_EQ(observer.observedType(), ConnectionTypeBluetooth); | 150 EXPECT_TRUE(verifyObservations(observer, ConnectionTypeBluetooth, kBluetooth
MaxBandwidth)); |
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, kBluetoothMaxBandwidth); |
141 EXPECT_EQ(observer1.observedType(), ConnectionTypeNone); | 162 EXPECT_TRUE(verifyObservations(observer1, ConnectionTypeNone, kNoneMaxBandwi
dth)); |
142 EXPECT_EQ(observer2.observedType(), ConnectionTypeBluetooth); | 163 EXPECT_TRUE(verifyObservations(observer2, ConnectionTypeBluetooth, kBluetoot
hMaxBandwidth)); |
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, kBluetoothMaxBandwidth); |
152 EXPECT_EQ(observer1.observedType(), ConnectionTypeNone); | 173 EXPECT_TRUE(verifyObservations(observer1, ConnectionTypeNone, kNoneMaxBandwi
dth)); |
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, kBluetoothMaxBandwidth); |
162 EXPECT_EQ(observer1.observedType(), ConnectionTypeBluetooth); | 183 EXPECT_TRUE(verifyObservations(observer1, ConnectionTypeBluetooth, kBluetoot
hMaxBandwidth)); |
163 EXPECT_EQ(observer2.observedType(), ConnectionTypeBluetooth); | 184 EXPECT_TRUE(verifyObservations(observer2, ConnectionTypeBluetooth, kBluetoot
hMaxBandwidth)); |
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, kBluetoothMaxBandwidth); |
173 EXPECT_EQ(observer1.observedType(), ConnectionTypeBluetooth); | 194 EXPECT_TRUE(verifyObservations(observer1, ConnectionTypeBluetooth, kBluetoot
hMaxBandwidth)); |
174 | 195 |
175 setType(ConnectionTypeEthernet); | 196 setConnection(ConnectionTypeEthernet, kEthernetMaxBandwidth); |
176 EXPECT_EQ(observer1.observedType(), ConnectionTypeBluetooth); | 197 EXPECT_TRUE(verifyObservations(observer1, ConnectionTypeBluetooth, kBluetoot
hMaxBandwidth)); |
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, kBluetoothMaxBandwidth); |
187 EXPECT_EQ(observer1.observedType(), ConnectionTypeBluetooth); | 208 EXPECT_TRUE(verifyObservations(observer1, ConnectionTypeBluetooth, kBluetoot
hMaxBandwidth)); |
188 EXPECT_EQ(observer2.observedType(), ConnectionTypeBluetooth); | 209 EXPECT_TRUE(verifyObservations(observer2, ConnectionTypeBluetooth, kBluetoot
hMaxBandwidth)); |
189 | 210 |
190 setType(ConnectionTypeEthernet); | 211 setConnection(ConnectionTypeEthernet, kEthernetMaxBandwidth); |
191 EXPECT_EQ(observer1.observedType(), ConnectionTypeBluetooth); | 212 EXPECT_TRUE(verifyObservations(observer1, ConnectionTypeBluetooth, kBluetoot
hMaxBandwidth)); |
192 EXPECT_EQ(observer2.observedType(), ConnectionTypeEthernet); | 213 EXPECT_TRUE(verifyObservations(observer2, ConnectionTypeEthernet, kEthernetM
axBandwidth)); |
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, kBluetoothMaxBandwidth); |
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, kEthernetMaxBandwidth); |
207 EXPECT_EQ(observer1.observedType(), ConnectionTypeBluetooth); | 228 EXPECT_TRUE(verifyObservations(observer1, ConnectionTypeBluetooth, kBluetoot
hMaxBandwidth)); |
208 EXPECT_EQ(observer2.observedType(), ConnectionTypeEthernet); | 229 EXPECT_TRUE(verifyObservations(observer2, ConnectionTypeEthernet, kEthernetM
axBandwidth)); |
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, kBluetoothMaxBandwidth); |
220 | 241 EXPECT_TRUE(verifyObservations(observer1, ConnectionTypeBluetooth, kBluetoot
hMaxBandwidth)); |
221 EXPECT_EQ(observer1.observedType(), ConnectionTypeBluetooth); | 242 EXPECT_TRUE(verifyObservations(observer2, ConnectionTypeNone, kNoneMaxBandwi
dth)); |
222 EXPECT_EQ(observer2.observedType(), ConnectionTypeNone); | 243 EXPECT_TRUE(verifyObservations(observer3, ConnectionTypeBluetooth, kBluetoot
hMaxBandwidth)); |
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, kBluetoothMaxBandwidth); |
233 EXPECT_EQ(observer1.observedType(), ConnectionTypeBluetooth); | 253 EXPECT_TRUE(verifyObservations(observer1, ConnectionTypeBluetooth, kBluetoot
hMaxBandwidth)); |
234 EXPECT_EQ(observer2.observedType(), ConnectionTypeBluetooth); | 254 EXPECT_TRUE(verifyObservations(observer2, ConnectionTypeBluetooth, kBluetoot
hMaxBandwidth)); |
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, kBluetoothMaxBandwidth); |
245 EXPECT_EQ(observer1.observedType(), ConnectionTypeBluetooth); | 265 EXPECT_TRUE(verifyObservations(observer1, ConnectionTypeBluetooth, kBluetoot
hMaxBandwidth)); |
246 EXPECT_EQ(observer2.observedType(), ConnectionTypeNone); | 266 EXPECT_TRUE(verifyObservations(observer2, ConnectionTypeNone, kNoneMaxBandwi
dth)); |
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, kBluetoothMaxBandwidth); |
258 EXPECT_EQ(observer1.observedType(), ConnectionTypeNone); | 278 EXPECT_TRUE(verifyObservations(observer1, ConnectionTypeNone, kNoneMaxBandwi
dth)); |
259 EXPECT_EQ(observer2.observedType(), ConnectionTypeNone); | 279 EXPECT_TRUE(verifyObservations(observer2, ConnectionTypeNone, kNoneMaxBandwi
dth)); |
260 } | 280 } |
261 | 281 |
262 } // namespace blink | 282 } // namespace blink |
OLD | NEW |