2 * Copyright (c) 2014, Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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
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.
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.
32 #include "core/page/NetworkStateNotifier.h"
34 #include "core/dom/Document.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 "wtf/Functional.h"
40 #include <gtest/gtest.h>
45 const double kNoneMaxBandwidthMbps
= 0.0;
46 const double kBluetoothMaxBandwidthMbps
= 1.0;
47 const double kEthernetMaxBandwidthMbps
= 2.0;
50 class StateObserver
: public NetworkStateNotifier::NetworkStateObserver
{
53 : m_observedType(WebConnectionTypeNone
)
54 , m_observedMaxBandwidthMbps(0.0)
59 virtual void connectionChange(WebConnectionType type
, double maxBandwidthMbps
)
61 m_observedType
= type
;
62 m_observedMaxBandwidthMbps
= maxBandwidthMbps
;
69 WebConnectionType
observedType() const
71 return m_observedType
;
74 double observedMaxBandwidth() const
76 return m_observedMaxBandwidthMbps
;
79 int callbackCount() const
81 return m_callbackCount
;
84 void setNotificationCallback(PassOwnPtr
<Closure
> closure
)
90 OwnPtr
<Closure
> m_closure
;
91 WebConnectionType m_observedType
;
92 double m_observedMaxBandwidthMbps
;
96 class NetworkStateNotifierTest
: public ::testing::Test
{
98 NetworkStateNotifierTest()
99 : m_document(Document::create())
100 , m_document2(Document::create())
104 ExecutionContext
* executionContext()
106 return m_document
.get();
109 ExecutionContext
* executionContext2()
111 return m_document2
.get();
115 void setConnection(WebConnectionType type
, double maxBandwidthMbps
)
117 m_notifier
.setWebConnection(type
, maxBandwidthMbps
);
118 testing::runPendingTasks();
121 void addObserverOnNotification(StateObserver
* observer
, StateObserver
* observerToAdd
)
123 observer
->setNotificationCallback(bind(&NetworkStateNotifier::addObserver
, &m_notifier
, observerToAdd
, executionContext()));
126 void removeObserverOnNotification(StateObserver
* observer
, StateObserver
* observerToRemove
)
128 observer
->setNotificationCallback(bind(&NetworkStateNotifier::removeObserver
, &m_notifier
, observerToRemove
, executionContext()));
131 bool verifyObservations(const StateObserver
& observer
, WebConnectionType type
, double maxBandwidthMbps
)
133 EXPECT_EQ(observer
.observedType(), type
);
134 EXPECT_EQ(observer
.observedMaxBandwidth(), maxBandwidthMbps
);
135 return observer
.observedType() == type
&& observer
.observedMaxBandwidth() == maxBandwidthMbps
;
138 RefPtrWillBePersistent
<Document
> m_document
;
139 RefPtrWillBePersistent
<Document
> m_document2
;
140 NetworkStateNotifier m_notifier
;
143 TEST_F(NetworkStateNotifierTest
, AddObserver
)
145 StateObserver observer
;
146 m_notifier
.addObserver(&observer
, executionContext());
147 EXPECT_TRUE(verifyObservations(observer
, WebConnectionTypeNone
, kNoneMaxBandwidthMbps
));
149 setConnection(WebConnectionTypeBluetooth
, kBluetoothMaxBandwidthMbps
);
150 EXPECT_TRUE(verifyObservations(observer
, WebConnectionTypeBluetooth
, kBluetoothMaxBandwidthMbps
));
151 EXPECT_EQ(observer
.callbackCount(), 1);
154 TEST_F(NetworkStateNotifierTest
, RemoveObserver
)
156 StateObserver observer1
, observer2
;
157 m_notifier
.addObserver(&observer1
, executionContext());
158 m_notifier
.removeObserver(&observer1
, executionContext());
159 m_notifier
.addObserver(&observer2
, executionContext());
161 setConnection(WebConnectionTypeBluetooth
, kBluetoothMaxBandwidthMbps
);
162 EXPECT_TRUE(verifyObservations(observer1
, WebConnectionTypeNone
, kNoneMaxBandwidthMbps
));
163 EXPECT_TRUE(verifyObservations(observer2
, WebConnectionTypeBluetooth
, kBluetoothMaxBandwidthMbps
));
166 TEST_F(NetworkStateNotifierTest
, RemoveSoleObserver
)
168 StateObserver observer1
;
169 m_notifier
.addObserver(&observer1
, executionContext());
170 m_notifier
.removeObserver(&observer1
, executionContext());
172 setConnection(WebConnectionTypeBluetooth
, kBluetoothMaxBandwidthMbps
);
173 EXPECT_TRUE(verifyObservations(observer1
, WebConnectionTypeNone
, kNoneMaxBandwidthMbps
));
176 TEST_F(NetworkStateNotifierTest
, AddObserverWhileNotifying
)
178 StateObserver observer1
, observer2
;
179 m_notifier
.addObserver(&observer1
, executionContext());
180 addObserverOnNotification(&observer1
, &observer2
);
182 setConnection(WebConnectionTypeBluetooth
, kBluetoothMaxBandwidthMbps
);
183 EXPECT_TRUE(verifyObservations(observer1
, WebConnectionTypeBluetooth
, kBluetoothMaxBandwidthMbps
));
184 EXPECT_TRUE(verifyObservations(observer2
, WebConnectionTypeBluetooth
, kBluetoothMaxBandwidthMbps
));
187 TEST_F(NetworkStateNotifierTest
, RemoveSoleObserverWhileNotifying
)
189 StateObserver observer1
;
190 m_notifier
.addObserver(&observer1
, executionContext());
191 removeObserverOnNotification(&observer1
, &observer1
);
193 setConnection(WebConnectionTypeBluetooth
, kBluetoothMaxBandwidthMbps
);
194 EXPECT_TRUE(verifyObservations(observer1
, WebConnectionTypeBluetooth
, kBluetoothMaxBandwidthMbps
));
196 setConnection(WebConnectionTypeEthernet
, kEthernetMaxBandwidthMbps
);
197 EXPECT_TRUE(verifyObservations(observer1
, WebConnectionTypeBluetooth
, kBluetoothMaxBandwidthMbps
));
200 TEST_F(NetworkStateNotifierTest
, RemoveCurrentObserverWhileNotifying
)
202 StateObserver observer1
, observer2
;
203 m_notifier
.addObserver(&observer1
, executionContext());
204 m_notifier
.addObserver(&observer2
, executionContext());
205 removeObserverOnNotification(&observer1
, &observer1
);
207 setConnection(WebConnectionTypeBluetooth
, kBluetoothMaxBandwidthMbps
);
208 EXPECT_TRUE(verifyObservations(observer1
, WebConnectionTypeBluetooth
, kBluetoothMaxBandwidthMbps
));
209 EXPECT_TRUE(verifyObservations(observer2
, WebConnectionTypeBluetooth
, kBluetoothMaxBandwidthMbps
));
211 setConnection(WebConnectionTypeEthernet
, kEthernetMaxBandwidthMbps
);
212 EXPECT_TRUE(verifyObservations(observer1
, WebConnectionTypeBluetooth
, kBluetoothMaxBandwidthMbps
));
213 EXPECT_TRUE(verifyObservations(observer2
, WebConnectionTypeEthernet
, kEthernetMaxBandwidthMbps
));
216 TEST_F(NetworkStateNotifierTest
, RemovePastObserverWhileNotifying
)
218 StateObserver observer1
, observer2
;
219 m_notifier
.addObserver(&observer1
, executionContext());
220 m_notifier
.addObserver(&observer2
, executionContext());
221 removeObserverOnNotification(&observer2
, &observer1
);
223 setConnection(WebConnectionTypeBluetooth
, kBluetoothMaxBandwidthMbps
);
224 EXPECT_EQ(observer1
.observedType(), WebConnectionTypeBluetooth
);
225 EXPECT_EQ(observer2
.observedType(), WebConnectionTypeBluetooth
);
227 setConnection(WebConnectionTypeEthernet
, kEthernetMaxBandwidthMbps
);
228 EXPECT_TRUE(verifyObservations(observer1
, WebConnectionTypeBluetooth
, kBluetoothMaxBandwidthMbps
));
229 EXPECT_TRUE(verifyObservations(observer2
, WebConnectionTypeEthernet
, kEthernetMaxBandwidthMbps
));
232 TEST_F(NetworkStateNotifierTest
, RemoveFutureObserverWhileNotifying
)
234 StateObserver observer1
, observer2
, observer3
;
235 m_notifier
.addObserver(&observer1
, executionContext());
236 m_notifier
.addObserver(&observer2
, executionContext());
237 m_notifier
.addObserver(&observer3
, executionContext());
238 removeObserverOnNotification(&observer1
, &observer2
);
240 setConnection(WebConnectionTypeBluetooth
, kBluetoothMaxBandwidthMbps
);
241 EXPECT_TRUE(verifyObservations(observer1
, WebConnectionTypeBluetooth
, kBluetoothMaxBandwidthMbps
));
242 EXPECT_TRUE(verifyObservations(observer2
, WebConnectionTypeNone
, kNoneMaxBandwidthMbps
));
243 EXPECT_TRUE(verifyObservations(observer3
, WebConnectionTypeBluetooth
, kBluetoothMaxBandwidthMbps
));
246 TEST_F(NetworkStateNotifierTest
, MultipleContextsAddObserver
)
248 StateObserver observer1
, observer2
;
249 m_notifier
.addObserver(&observer1
, executionContext());
250 m_notifier
.addObserver(&observer2
, executionContext2());
252 setConnection(WebConnectionTypeBluetooth
, kBluetoothMaxBandwidthMbps
);
253 EXPECT_TRUE(verifyObservations(observer1
, WebConnectionTypeBluetooth
, kBluetoothMaxBandwidthMbps
));
254 EXPECT_TRUE(verifyObservations(observer2
, WebConnectionTypeBluetooth
, kBluetoothMaxBandwidthMbps
));
257 TEST_F(NetworkStateNotifierTest
, RemoveContext
)
259 StateObserver observer1
, observer2
;
260 m_notifier
.addObserver(&observer1
, executionContext());
261 m_notifier
.addObserver(&observer2
, executionContext2());
262 m_notifier
.removeObserver(&observer2
, executionContext2());
264 setConnection(WebConnectionTypeBluetooth
, kBluetoothMaxBandwidthMbps
);
265 EXPECT_TRUE(verifyObservations(observer1
, WebConnectionTypeBluetooth
, kBluetoothMaxBandwidthMbps
));
266 EXPECT_TRUE(verifyObservations(observer2
, WebConnectionTypeNone
, kNoneMaxBandwidthMbps
));
269 TEST_F(NetworkStateNotifierTest
, RemoveAllContexts
)
271 StateObserver observer1
, observer2
;
272 m_notifier
.addObserver(&observer1
, executionContext());
273 m_notifier
.addObserver(&observer2
, executionContext2());
274 m_notifier
.removeObserver(&observer1
, executionContext());
275 m_notifier
.removeObserver(&observer2
, executionContext2());
277 setConnection(WebConnectionTypeBluetooth
, kBluetoothMaxBandwidthMbps
);
278 EXPECT_TRUE(verifyObservations(observer1
, WebConnectionTypeNone
, kNoneMaxBandwidthMbps
));
279 EXPECT_TRUE(verifyObservations(observer2
, WebConnectionTypeNone
, kNoneMaxBandwidthMbps
));