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 class ExitTask |
| 83 : public blink::WebThread::Task { |
| 84 public: |
| 85 ExitTask(blink::WebThread* thread) |
| 86 : m_thread(thread) |
| 87 { |
| 88 } |
| 89 virtual void run() OVERRIDE |
| 90 { |
| 91 m_thread->exitRunLoop(); |
| 92 } |
| 93 |
| 94 private: |
| 95 blink::WebThread* m_thread; |
| 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 m_notifier.setWebConnectionType(type); |
| 120 |
| 121 blink::WebThread* thread = blink::Platform::current()->currentThread(); |
| 122 thread->postTask(new ExitTask(thread)); |
| 123 thread->enterRunLoop(); |
| 124 } |
| 125 |
| 126 void addObserverOnNotification(StateObserver* observer, StateObserver* obser
verToAdd) |
| 127 { |
| 128 observer->setNotificationCallback(bind(&NetworkStateNotifier::addObserve
r, &m_notifier, observerToAdd, executionContext())); |
| 129 } |
| 130 |
| 131 void removeObserverOnNotification(StateObserver* observer, StateObserver* ob
serverToRemove) |
| 132 { |
| 133 observer->setNotificationCallback(bind(&NetworkStateNotifier::removeObse
rver, &m_notifier, observerToRemove, executionContext())); |
| 134 } |
| 135 |
| 136 RefPtr<Document> m_document; |
| 137 RefPtr<Document> m_document2; |
| 138 NetworkStateNotifier m_notifier; |
| 139 }; |
| 140 |
| 141 TEST_F(NetworkStateNotifierTest, AddObserver) |
| 142 { |
| 143 StateObserver observer; |
| 144 m_notifier.addObserver(&observer, executionContext()); |
| 145 EXPECT_EQ(observer.observedType(), blink::WebNetworkConnection::None); |
| 146 |
| 147 setType(blink::WebNetworkConnection::Bluetooth); |
| 148 EXPECT_EQ(observer.observedType(), blink::WebNetworkConnection::Bluetooth); |
| 149 EXPECT_EQ(observer.callbackCount(), 1); |
| 150 } |
| 151 |
| 152 TEST_F(NetworkStateNotifierTest, RemoveObserver) |
| 153 { |
| 154 StateObserver observer1, observer2; |
| 155 m_notifier.addObserver(&observer1, executionContext()); |
| 156 m_notifier.removeObserver(&observer1, executionContext()); |
| 157 m_notifier.addObserver(&observer2, executionContext()); |
| 158 |
| 159 setType(blink::WebNetworkConnection::Bluetooth); |
| 160 EXPECT_EQ(observer1.observedType(), blink::WebNetworkConnection::None); |
| 161 EXPECT_EQ(observer2.observedType(), blink::WebNetworkConnection::Bluetooth); |
| 162 } |
| 163 |
| 164 TEST_F(NetworkStateNotifierTest, RemoveSoleObserver) |
| 165 { |
| 166 StateObserver observer1, observer2; |
| 167 m_notifier.addObserver(&observer1, executionContext()); |
| 168 m_notifier.removeObserver(&observer1, executionContext()); |
| 169 |
| 170 setType(blink::WebNetworkConnection::Bluetooth); |
| 171 EXPECT_EQ(observer1.observedType(), blink::WebNetworkConnection::None); |
| 172 } |
| 173 |
| 174 TEST_F(NetworkStateNotifierTest, AddObserverWhileNotifying) |
| 175 { |
| 176 StateObserver observer1, observer2; |
| 177 m_notifier.addObserver(&observer1, executionContext()); |
| 178 addObserverOnNotification(&observer1, &observer2); |
| 179 |
| 180 setType(blink::WebNetworkConnection::Bluetooth); |
| 181 EXPECT_EQ(observer1.observedType(), blink::WebNetworkConnection::Bluetooth); |
| 182 EXPECT_EQ(observer2.observedType(), blink::WebNetworkConnection::Bluetooth); |
| 183 } |
| 184 |
| 185 TEST_F(NetworkStateNotifierTest, RemoveSoleObserverWhileNotifying) |
| 186 { |
| 187 StateObserver observer1; |
| 188 m_notifier.addObserver(&observer1, executionContext()); |
| 189 removeObserverOnNotification(&observer1, &observer1); |
| 190 |
| 191 setType(blink::WebNetworkConnection::Bluetooth); |
| 192 EXPECT_EQ(observer1.observedType(), blink::WebNetworkConnection::Bluetooth); |
| 193 |
| 194 setType(blink::WebNetworkConnection::Ethernet); |
| 195 EXPECT_EQ(observer1.observedType(), blink::WebNetworkConnection::Bluetooth); |
| 196 } |
| 197 |
| 198 TEST_F(NetworkStateNotifierTest, RemoveCurrentObserverWhileNotifying) |
| 199 { |
| 200 StateObserver observer1, observer2; |
| 201 m_notifier.addObserver(&observer1, executionContext()); |
| 202 m_notifier.addObserver(&observer2, executionContext()); |
| 203 removeObserverOnNotification(&observer1, &observer1); |
| 204 |
| 205 setType(blink::WebNetworkConnection::Bluetooth); |
| 206 EXPECT_EQ(observer1.observedType(), blink::WebNetworkConnection::Bluetooth); |
| 207 EXPECT_EQ(observer2.observedType(), blink::WebNetworkConnection::Bluetooth); |
| 208 |
| 209 setType(blink::WebNetworkConnection::Ethernet); |
| 210 EXPECT_EQ(observer1.observedType(), blink::WebNetworkConnection::Bluetooth); |
| 211 EXPECT_EQ(observer2.observedType(), blink::WebNetworkConnection::Ethernet); |
| 212 } |
| 213 |
| 214 TEST_F(NetworkStateNotifierTest, RemovePastObserverWhileNotifying) |
| 215 { |
| 216 StateObserver observer1, observer2; |
| 217 m_notifier.addObserver(&observer1, executionContext()); |
| 218 m_notifier.addObserver(&observer2, executionContext()); |
| 219 removeObserverOnNotification(&observer2, &observer1); |
| 220 |
| 221 setType(blink::WebNetworkConnection::Bluetooth); |
| 222 EXPECT_EQ(observer1.observedType(), blink::WebNetworkConnection::Bluetooth); |
| 223 EXPECT_EQ(observer2.observedType(), blink::WebNetworkConnection::Bluetooth); |
| 224 |
| 225 setType(blink::WebNetworkConnection::Ethernet); |
| 226 EXPECT_EQ(observer1.observedType(), blink::WebNetworkConnection::Bluetooth); |
| 227 EXPECT_EQ(observer2.observedType(), blink::WebNetworkConnection::Ethernet); |
| 228 } |
| 229 |
| 230 TEST_F(NetworkStateNotifierTest, RemoveFutureObserverWhileNotifying) |
| 231 { |
| 232 StateObserver observer1, observer2, observer3; |
| 233 m_notifier.addObserver(&observer1, executionContext()); |
| 234 m_notifier.addObserver(&observer2, executionContext()); |
| 235 m_notifier.addObserver(&observer3, executionContext()); |
| 236 removeObserverOnNotification(&observer1, &observer2); |
| 237 |
| 238 setType(blink::WebNetworkConnection::Bluetooth); |
| 239 |
| 240 EXPECT_EQ(observer1.observedType(), blink::WebNetworkConnection::Bluetooth); |
| 241 EXPECT_EQ(observer2.observedType(), blink::WebNetworkConnection::None); |
| 242 EXPECT_EQ(observer3.observedType(), blink::WebNetworkConnection::Bluetooth); |
| 243 } |
| 244 |
| 245 TEST_F(NetworkStateNotifierTest, MultipleContextsAddObserver) |
| 246 { |
| 247 StateObserver observer1, observer2; |
| 248 m_notifier.addObserver(&observer1, executionContext()); |
| 249 m_notifier.addObserver(&observer2, executionContext2()); |
| 250 |
| 251 setType(blink::WebNetworkConnection::Bluetooth); |
| 252 EXPECT_EQ(observer1.observedType(), blink::WebNetworkConnection::Bluetooth); |
| 253 EXPECT_EQ(observer2.observedType(), blink::WebNetworkConnection::Bluetooth); |
| 254 } |
| 255 |
| 256 TEST_F(NetworkStateNotifierTest, RemoveContext) |
| 257 { |
| 258 StateObserver observer1, observer2; |
| 259 m_notifier.addObserver(&observer1, executionContext()); |
| 260 m_notifier.addObserver(&observer2, executionContext2()); |
| 261 m_notifier.removeObserver(&observer2, executionContext2()); |
| 262 |
| 263 setType(blink::WebNetworkConnection::Bluetooth); |
| 264 EXPECT_EQ(observer1.observedType(), blink::WebNetworkConnection::Bluetooth); |
| 265 EXPECT_EQ(observer2.observedType(), blink::WebNetworkConnection::None); |
| 266 } |
| 267 |
| 268 TEST_F(NetworkStateNotifierTest, RemoveAllContexts) |
| 269 { |
| 270 StateObserver observer1, observer2; |
| 271 m_notifier.addObserver(&observer1, executionContext()); |
| 272 m_notifier.addObserver(&observer2, executionContext2()); |
| 273 m_notifier.removeObserver(&observer1, executionContext()); |
| 274 m_notifier.removeObserver(&observer2, executionContext2()); |
| 275 |
| 276 setType(blink::WebNetworkConnection::Bluetooth); |
| 277 EXPECT_EQ(observer1.observedType(), blink::WebNetworkConnection::None); |
| 278 EXPECT_EQ(observer2.observedType(), blink::WebNetworkConnection::None); |
| 279 } |
| 280 |
| 281 } // namespace WebCore |
OLD | NEW |