Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 /* | |
| 2 * Copyright (c) 2014, Google Inc. All rights reserved. | |
| 3 * | |
| 4 * Redistribution and use in source and binary forms, with or without | |
| 5 * modification, are permitted provided that the following conditions are | |
| 6 * met: | |
| 7 * | |
| 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 | |
| 13 * distribution. | |
| 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. | |
| 17 * | |
| 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. | |
| 29 */ | |
| 30 | |
| 31 #include "config.h" | |
| 32 #include "core/page/NetworkStateNotifier.h" | |
| 33 | |
| 34 #include "core/dom/Document.h" | |
| 35 #include "public/platform/Platform.h" | |
| 36 #include "public/platform/WebNetworkConnection.h" | |
| 37 #include "public/platform/WebThread.h" | |
| 38 #include "wtf/Functional.h" | |
| 39 #include <gtest/gtest.h> | |
| 40 | |
| 41 namespace WebCore { | |
| 42 | |
| 43 class StateObserver : public NetworkStateNotifier::NetworkStateObserver { | |
| 44 public: | |
| 45 StateObserver() | |
| 46 : m_observedType(blink::WebNetworkConnection::None) | |
| 47 , m_callbackCount(0) | |
| 48 { | |
| 49 } | |
| 50 | |
| 51 virtual void connectionTypeChange(blink::WebNetworkConnection::ConnectionTyp e type) | |
| 52 { | |
| 53 m_observedType = type; | |
| 54 m_callbackCount += 1; | |
| 55 | |
| 56 if (!m_closure.isNull()) | |
| 57 m_closure(); | |
| 58 } | |
| 59 | |
| 60 blink::WebNetworkConnection::ConnectionType observedType() const | |
| 61 { | |
| 62 return m_observedType; | |
| 63 } | |
| 64 | |
| 65 int callbackCount() const | |
| 66 { | |
| 67 return m_callbackCount; | |
| 68 } | |
| 69 | |
| 70 void setNotificationCallback(const Closure& closure) | |
| 71 { | |
| 72 m_closure = closure; | |
| 73 } | |
| 74 | |
| 75 private: | |
| 76 Closure m_closure; | |
| 77 blink::WebNetworkConnection::ConnectionType m_observedType; | |
| 78 int m_callbackCount; | |
| 79 StateObserver* m_observerToAdd; | |
| 80 }; | |
| 81 | |
| 82 // A normal NetworkStateNotifier but it exits the run loop after its observers a re notified of connection changes. | |
| 83 class TestNetworkStateNotifier : public NetworkStateNotifier { | |
| 84 public: | |
| 85 bool hasObservers() const | |
| 86 { | |
| 87 MutexLocker locker(m_mutex); | |
| 88 return !m_observers.isEmpty(); | |
| 89 } | |
| 90 | |
| 91 virtual void notifyObserversOnContext(ExecutionContext* context, blink::WebN etworkConnection::ConnectionType type) OVERRIDE | |
| 92 { | |
| 93 NetworkStateNotifier::notifyObserversOnContext(context, type); | |
| 94 blink::Platform::current()->currentThread()->exitRunLoop(); | |
|
adamk
2014/05/21 16:06:05
Rather than having to override this method, could
jkarlin
2014/05/21 16:34:01
Good idea. Done.
| |
| 95 } | |
| 96 }; | |
| 97 | |
| 98 class NetworkStateNotifierTest : public testing::Test { | |
| 99 public: | |
| 100 NetworkStateNotifierTest() | |
| 101 : m_document(Document::create()) | |
| 102 , m_document2(Document::create()) | |
| 103 { | |
| 104 } | |
| 105 | |
| 106 ExecutionContext* executionContext() | |
| 107 { | |
| 108 return m_document.get(); | |
| 109 } | |
| 110 | |
| 111 ExecutionContext* executionContext2() | |
| 112 { | |
| 113 return m_document2.get(); | |
| 114 } | |
| 115 | |
| 116 protected: | |
| 117 void setType(blink::WebNetworkConnection::ConnectionType type) | |
| 118 { | |
| 119 bool hasObservers = m_notifier.hasObservers(); | |
| 120 m_notifier.setWebConnectionType(type); | |
| 121 | |
| 122 if (hasObservers) { | |
| 123 // Enter run loop if tasks were posted. | |
| 124 blink::Platform::current()->currentThread()->enterRunLoop(); | |
| 125 } | |
| 126 } | |
| 127 | |
| 128 void addObserverOnNotification(StateObserver* observer, StateObserver* obser verToAdd) | |
| 129 { | |
| 130 observer->setNotificationCallback(bind(&NetworkStateNotifier::addObserve r, &m_notifier, observerToAdd, executionContext())); | |
| 131 } | |
| 132 | |
| 133 void removeObserverOnNotification(StateObserver* observer, StateObserver* ob serverToRemove) | |
| 134 { | |
| 135 observer->setNotificationCallback(bind(&NetworkStateNotifier::removeObse rver, &m_notifier, observerToRemove, executionContext())); | |
| 136 } | |
| 137 | |
| 138 RefPtr<Document> m_document; | |
| 139 RefPtr<Document> m_document2; | |
| 140 TestNetworkStateNotifier m_notifier; | |
| 141 }; | |
| 142 | |
| 143 TEST_F(NetworkStateNotifierTest, AddObserver) | |
| 144 { | |
| 145 StateObserver observer; | |
| 146 m_notifier.addObserver(&observer, executionContext()); | |
| 147 EXPECT_EQ(observer.observedType(), blink::WebNetworkConnection::None); | |
| 148 | |
| 149 setType(blink::WebNetworkConnection::Bluetooth); | |
| 150 EXPECT_EQ(observer.observedType(), blink::WebNetworkConnection::Bluetooth); | |
| 151 EXPECT_EQ(observer.callbackCount(), 1); | |
| 152 } | |
| 153 | |
| 154 TEST_F(NetworkStateNotifierTest, RemoveObserver) | |
| 155 { | |
| 156 StateObserver observer1, observer2; | |
| 157 m_notifier.addObserver(&observer1, executionContext()); | |
| 158 m_notifier.removeObserver(&observer1, executionContext()); | |
| 159 m_notifier.addObserver(&observer2, executionContext()); | |
| 160 | |
| 161 setType(blink::WebNetworkConnection::Bluetooth); | |
| 162 EXPECT_EQ(observer1.observedType(), blink::WebNetworkConnection::None); | |
| 163 EXPECT_EQ(observer2.observedType(), blink::WebNetworkConnection::Bluetooth); | |
| 164 } | |
| 165 | |
| 166 TEST_F(NetworkStateNotifierTest, RemoveSoleObserver) | |
| 167 { | |
| 168 StateObserver observer1, observer2; | |
| 169 m_notifier.addObserver(&observer1, executionContext()); | |
| 170 m_notifier.removeObserver(&observer1, executionContext()); | |
| 171 | |
| 172 setType(blink::WebNetworkConnection::Bluetooth); | |
| 173 EXPECT_EQ(observer1.observedType(), blink::WebNetworkConnection::None); | |
| 174 } | |
| 175 | |
| 176 TEST_F(NetworkStateNotifierTest, AddObserverWhileNotifying) | |
| 177 { | |
| 178 StateObserver observer1, observer2; | |
| 179 m_notifier.addObserver(&observer1, executionContext()); | |
| 180 addObserverOnNotification(&observer1, &observer2); | |
| 181 | |
| 182 setType(blink::WebNetworkConnection::Bluetooth); | |
| 183 EXPECT_EQ(observer1.observedType(), blink::WebNetworkConnection::Bluetooth); | |
| 184 EXPECT_EQ(observer2.observedType(), blink::WebNetworkConnection::Bluetooth); | |
| 185 } | |
| 186 | |
| 187 TEST_F(NetworkStateNotifierTest, RemoveSoleObserverWhileNotifying) | |
| 188 { | |
| 189 StateObserver observer1; | |
| 190 m_notifier.addObserver(&observer1, executionContext()); | |
| 191 removeObserverOnNotification(&observer1, &observer1); | |
| 192 | |
| 193 setType(blink::WebNetworkConnection::Bluetooth); | |
| 194 EXPECT_EQ(observer1.observedType(), blink::WebNetworkConnection::Bluetooth); | |
| 195 | |
| 196 setType(blink::WebNetworkConnection::Ethernet); | |
| 197 EXPECT_EQ(observer1.observedType(), blink::WebNetworkConnection::Bluetooth); | |
| 198 } | |
| 199 | |
| 200 TEST_F(NetworkStateNotifierTest, RemoveCurrentObserverWhileNotifying) | |
| 201 { | |
| 202 StateObserver observer1, observer2; | |
| 203 m_notifier.addObserver(&observer1, executionContext()); | |
| 204 m_notifier.addObserver(&observer2, executionContext()); | |
| 205 removeObserverOnNotification(&observer1, &observer1); | |
| 206 | |
| 207 setType(blink::WebNetworkConnection::Bluetooth); | |
| 208 EXPECT_EQ(observer1.observedType(), blink::WebNetworkConnection::Bluetooth); | |
| 209 EXPECT_EQ(observer2.observedType(), blink::WebNetworkConnection::Bluetooth); | |
| 210 | |
| 211 setType(blink::WebNetworkConnection::Ethernet); | |
| 212 EXPECT_EQ(observer1.observedType(), blink::WebNetworkConnection::Bluetooth); | |
| 213 EXPECT_EQ(observer2.observedType(), blink::WebNetworkConnection::Ethernet); | |
| 214 } | |
| 215 | |
| 216 TEST_F(NetworkStateNotifierTest, RemovePastObserverWhileNotifying) | |
| 217 { | |
| 218 StateObserver observer1, observer2; | |
| 219 m_notifier.addObserver(&observer1, executionContext()); | |
| 220 m_notifier.addObserver(&observer2, executionContext()); | |
| 221 removeObserverOnNotification(&observer2, &observer1); | |
| 222 | |
| 223 setType(blink::WebNetworkConnection::Bluetooth); | |
| 224 EXPECT_EQ(observer1.observedType(), blink::WebNetworkConnection::Bluetooth); | |
| 225 EXPECT_EQ(observer2.observedType(), blink::WebNetworkConnection::Bluetooth); | |
| 226 | |
| 227 setType(blink::WebNetworkConnection::Ethernet); | |
| 228 EXPECT_EQ(observer1.observedType(), blink::WebNetworkConnection::Bluetooth); | |
| 229 EXPECT_EQ(observer2.observedType(), blink::WebNetworkConnection::Ethernet); | |
| 230 } | |
| 231 | |
| 232 TEST_F(NetworkStateNotifierTest, RemoveFutureObserverWhileNotifying) | |
| 233 { | |
| 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); | |
| 239 | |
| 240 setType(blink::WebNetworkConnection::Bluetooth); | |
| 241 | |
| 242 EXPECT_EQ(observer1.observedType(), blink::WebNetworkConnection::Bluetooth); | |
| 243 EXPECT_EQ(observer2.observedType(), blink::WebNetworkConnection::None); | |
| 244 EXPECT_EQ(observer3.observedType(), blink::WebNetworkConnection::Bluetooth); | |
| 245 } | |
| 246 | |
| 247 TEST_F(NetworkStateNotifierTest, MultipleContextsAddObserver) | |
| 248 { | |
| 249 StateObserver observer1, observer2; | |
| 250 m_notifier.addObserver(&observer1, executionContext()); | |
| 251 m_notifier.addObserver(&observer2, executionContext2()); | |
| 252 | |
| 253 setType(blink::WebNetworkConnection::Bluetooth); | |
| 254 EXPECT_EQ(observer1.observedType(), blink::WebNetworkConnection::Bluetooth); | |
| 255 EXPECT_EQ(observer2.observedType(), blink::WebNetworkConnection::Bluetooth); | |
| 256 } | |
| 257 | |
| 258 TEST_F(NetworkStateNotifierTest, RemoveContext) | |
| 259 { | |
| 260 StateObserver observer1, observer2; | |
| 261 m_notifier.addObserver(&observer1, executionContext()); | |
| 262 m_notifier.addObserver(&observer2, executionContext2()); | |
| 263 m_notifier.removeObserver(&observer2, executionContext2()); | |
| 264 | |
| 265 setType(blink::WebNetworkConnection::Bluetooth); | |
| 266 EXPECT_EQ(observer1.observedType(), blink::WebNetworkConnection::Bluetooth); | |
| 267 EXPECT_EQ(observer2.observedType(), blink::WebNetworkConnection::None); | |
| 268 } | |
| 269 | |
| 270 TEST_F(NetworkStateNotifierTest, RemoveAllContexts) | |
| 271 { | |
| 272 StateObserver observer1, observer2; | |
| 273 m_notifier.addObserver(&observer1, executionContext()); | |
| 274 m_notifier.addObserver(&observer2, executionContext2()); | |
| 275 m_notifier.removeObserver(&observer1, executionContext()); | |
| 276 m_notifier.removeObserver(&observer2, executionContext2()); | |
| 277 | |
| 278 setType(blink::WebNetworkConnection::Bluetooth); | |
| 279 EXPECT_EQ(observer1.observedType(), blink::WebNetworkConnection::None); | |
| 280 EXPECT_EQ(observer2.observedType(), blink::WebNetworkConnection::None); | |
| 281 } | |
| 282 | |
| 283 } // namespace WebCore | |
| OLD | NEW |