| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "net/base/network_change_notifier_linux.h" | 5 #include "net/base/network_change_notifier_linux.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/message_loop_proxy.h" | 8 #include "base/message_loop_proxy.h" |
| 9 #include "base/synchronization/waitable_event.h" | 9 #include "base/synchronization/waitable_event.h" |
| 10 #include "dbus/mock_bus.h" | 10 #include "dbus/mock_bus.h" |
| (...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 112 | 112 |
| 113 // Valid only after initialized_ is signaled. | 113 // Valid only after initialized_ is signaled. |
| 114 scoped_refptr<base::MessageLoopProxy> notifier_thread_proxy_; | 114 scoped_refptr<base::MessageLoopProxy> notifier_thread_proxy_; |
| 115 | 115 |
| 116 scoped_refptr<dbus::MockBus> mock_bus_; | 116 scoped_refptr<dbus::MockBus> mock_bus_; |
| 117 scoped_refptr<dbus::MockObjectProxy> mock_object_proxy_; | 117 scoped_refptr<dbus::MockObjectProxy> mock_object_proxy_; |
| 118 }; | 118 }; |
| 119 | 119 |
| 120 namespace { | 120 namespace { |
| 121 | 121 |
| 122 class OfflineObserver : public NetworkChangeNotifier::OnlineStateObserver { | 122 class OfflineObserver : public NetworkChangeNotifier::ConnectionTypeObserver { |
| 123 public: | 123 public: |
| 124 OfflineObserver() | 124 OfflineObserver() |
| 125 : notification_count(0), | 125 : notification_count(0), |
| 126 last_online_value(true) { | 126 last_online_value(true) { |
| 127 NetworkChangeNotifier::AddOnlineStateObserver(this); | 127 NetworkChangeNotifier::AddConnectionTypeObserver(this); |
| 128 } | 128 } |
| 129 | 129 |
| 130 ~OfflineObserver() { | 130 ~OfflineObserver() { |
| 131 NetworkChangeNotifier::RemoveOnlineStateObserver(this); | 131 NetworkChangeNotifier::RemoveConnectionTypeObserver(this); |
| 132 } | 132 } |
| 133 | 133 |
| 134 virtual void OnOnlineStateChanged(bool online) OVERRIDE { | 134 virtual void OnConnectionTypeChanged( |
| 135 NetworkChangeNotifier::ConnectionType type) OVERRIDE { |
| 135 notification_count++; | 136 notification_count++; |
| 136 last_online_value = online; | 137 last_online_value = type != NetworkChangeNotifier::CONNECTION_NONE; |
| 137 } | 138 } |
| 138 | 139 |
| 139 int notification_count; | 140 int notification_count; |
| 140 bool last_online_value; | 141 bool last_online_value; |
| 141 }; | 142 }; |
| 142 | 143 |
| 143 TEST_F(NetworkChangeNotifierLinuxTest, Offline) { | 144 TEST_F(NetworkChangeNotifierLinuxTest, Offline) { |
| 144 SendResponse(NM_STATE_DISCONNECTED); | 145 SendResponse(NM_STATE_DISCONNECTED); |
| 145 EXPECT_TRUE(NetworkChangeNotifier::IsOffline()); | 146 EXPECT_EQ(NetworkChangeNotifier::CONNECTION_NONE, |
| 147 NetworkChangeNotifier::GetConnectionType()); |
| 146 } | 148 } |
| 147 | 149 |
| 148 TEST_F(NetworkChangeNotifierLinuxTest, Online) { | 150 TEST_F(NetworkChangeNotifierLinuxTest, Online) { |
| 149 SendResponse(NM_STATE_CONNECTED_GLOBAL); | 151 SendResponse(NM_STATE_CONNECTED_GLOBAL); |
| 150 EXPECT_FALSE(NetworkChangeNotifier::IsOffline()); | 152 EXPECT_NE(NetworkChangeNotifier::CONNECTION_NONE, |
| 151 } | 153 NetworkChangeNotifier::GetConnectionType());} |
| 152 | 154 |
| 153 TEST_F(NetworkChangeNotifierLinuxTest, OfflineThenOnline) { | 155 TEST_F(NetworkChangeNotifierLinuxTest, OfflineThenOnline) { |
| 154 OfflineObserver observer; | 156 OfflineObserver observer; |
| 155 | 157 |
| 156 SendResponse(NM_STATE_DISCONNECTED); | 158 SendResponse(NM_STATE_DISCONNECTED); |
| 157 EXPECT_TRUE(NetworkChangeNotifier::IsOffline()); | 159 EXPECT_EQ(NetworkChangeNotifier::CONNECTION_NONE, |
| 160 NetworkChangeNotifier::GetConnectionType()); |
| 158 EXPECT_EQ(0, observer.notification_count); | 161 EXPECT_EQ(0, observer.notification_count); |
| 159 | 162 |
| 160 SendSignal(NM_STATE_CONNECTED_GLOBAL); | 163 SendSignal(NM_STATE_CONNECTED_GLOBAL); |
| 161 EXPECT_FALSE(NetworkChangeNotifier::IsOffline()); | 164 EXPECT_NE(NetworkChangeNotifier::CONNECTION_NONE, |
| 165 NetworkChangeNotifier::GetConnectionType()); |
| 162 EXPECT_EQ(1, observer.notification_count); | 166 EXPECT_EQ(1, observer.notification_count); |
| 163 EXPECT_TRUE(observer.last_online_value); | 167 EXPECT_TRUE(observer.last_online_value); |
| 164 } | 168 } |
| 165 | 169 |
| 166 TEST_F(NetworkChangeNotifierLinuxTest, MultipleStateChanges) { | 170 TEST_F(NetworkChangeNotifierLinuxTest, MultipleStateChanges) { |
| 167 OfflineObserver observer; | 171 OfflineObserver observer; |
| 168 | 172 |
| 169 SendResponse(NM_STATE_CONNECTED_GLOBAL); | 173 SendResponse(NM_STATE_CONNECTED_GLOBAL); |
| 170 EXPECT_FALSE(NetworkChangeNotifier::IsOffline()); | 174 EXPECT_NE(NetworkChangeNotifier::CONNECTION_NONE, |
| 175 NetworkChangeNotifier::GetConnectionType()); |
| 171 EXPECT_EQ(0, observer.notification_count); | 176 EXPECT_EQ(0, observer.notification_count); |
| 172 | 177 |
| 173 SendSignal(NM_STATE_DISCONNECTED); | 178 SendSignal(NM_STATE_DISCONNECTED); |
| 174 EXPECT_TRUE(NetworkChangeNotifier::IsOffline()); | 179 EXPECT_EQ(NetworkChangeNotifier::CONNECTION_NONE, |
| 180 NetworkChangeNotifier::GetConnectionType()); |
| 175 EXPECT_EQ(1, observer.notification_count); | 181 EXPECT_EQ(1, observer.notification_count); |
| 176 EXPECT_FALSE(observer.last_online_value); | 182 EXPECT_FALSE(observer.last_online_value); |
| 177 | 183 |
| 178 SendSignal(NM_STATE_CONNECTED_GLOBAL); | 184 SendSignal(NM_STATE_CONNECTED_GLOBAL); |
| 179 EXPECT_FALSE(NetworkChangeNotifier::IsOffline()); | 185 EXPECT_NE(NetworkChangeNotifier::CONNECTION_NONE, |
| 186 NetworkChangeNotifier::GetConnectionType()); |
| 180 EXPECT_EQ(2, observer.notification_count); | 187 EXPECT_EQ(2, observer.notification_count); |
| 181 EXPECT_TRUE(observer.last_online_value); | 188 EXPECT_TRUE(observer.last_online_value); |
| 182 } | 189 } |
| 183 | 190 |
| 184 TEST_F(NetworkChangeNotifierLinuxTest, IgnoreContinuedOnlineState) { | 191 TEST_F(NetworkChangeNotifierLinuxTest, IgnoreContinuedOnlineState) { |
| 185 OfflineObserver observer; | 192 OfflineObserver observer; |
| 186 | 193 |
| 187 SendResponse(NM_STATE_CONNECTED_SITE); | 194 SendResponse(NM_STATE_CONNECTED_SITE); |
| 188 EXPECT_FALSE(NetworkChangeNotifier::IsOffline()); | 195 EXPECT_NE(NetworkChangeNotifier::CONNECTION_NONE, |
| 196 NetworkChangeNotifier::GetConnectionType()); |
| 189 EXPECT_EQ(0, observer.notification_count); | 197 EXPECT_EQ(0, observer.notification_count); |
| 190 | 198 |
| 191 SendSignal(NM_STATE_CONNECTED_GLOBAL); | 199 SendSignal(NM_STATE_CONNECTED_GLOBAL); |
| 192 EXPECT_FALSE(NetworkChangeNotifier::IsOffline()); | 200 EXPECT_NE(NetworkChangeNotifier::CONNECTION_NONE, |
| 201 NetworkChangeNotifier::GetConnectionType()); |
| 193 EXPECT_EQ(0, observer.notification_count); | 202 EXPECT_EQ(0, observer.notification_count); |
| 194 } | 203 } |
| 195 | 204 |
| 196 TEST_F(NetworkChangeNotifierLinuxTest, IgnoreContinuedOfflineState) { | 205 TEST_F(NetworkChangeNotifierLinuxTest, IgnoreContinuedOfflineState) { |
| 197 OfflineObserver observer; | 206 OfflineObserver observer; |
| 198 | 207 |
| 199 SendResponse(NM_STATE_DISCONNECTING); | 208 SendResponse(NM_STATE_DISCONNECTING); |
| 200 EXPECT_TRUE(NetworkChangeNotifier::IsOffline()); | 209 EXPECT_EQ(NetworkChangeNotifier::CONNECTION_NONE, |
| 210 NetworkChangeNotifier::GetConnectionType()); |
| 201 EXPECT_EQ(0, observer.notification_count); | 211 EXPECT_EQ(0, observer.notification_count); |
| 202 | 212 |
| 203 SendSignal(NM_STATE_DISCONNECTED); | 213 SendSignal(NM_STATE_DISCONNECTED); |
| 204 EXPECT_TRUE(NetworkChangeNotifier::IsOffline()); | 214 EXPECT_EQ(NetworkChangeNotifier::CONNECTION_NONE, |
| 215 NetworkChangeNotifier::GetConnectionType()); |
| 205 EXPECT_EQ(0, observer.notification_count); | 216 EXPECT_EQ(0, observer.notification_count); |
| 206 } | 217 } |
| 207 | 218 |
| 208 TEST_F(NetworkChangeNotifierLinuxTest, NullResponse) { | 219 TEST_F(NetworkChangeNotifierLinuxTest, NullResponse) { |
| 209 RunOnNotifierThread(base::Bind( | 220 RunOnNotifierThread(base::Bind( |
| 210 response_callback_, static_cast<dbus::Response*>(NULL))); | 221 response_callback_, static_cast<dbus::Response*>(NULL))); |
| 211 EXPECT_FALSE(NetworkChangeNotifier::IsOffline()); | 222 EXPECT_NE(NetworkChangeNotifier::CONNECTION_NONE, |
| 223 NetworkChangeNotifier::GetConnectionType()); |
| 212 } | 224 } |
| 213 | 225 |
| 214 TEST_F(NetworkChangeNotifierLinuxTest, EmptyResponse) { | 226 TEST_F(NetworkChangeNotifierLinuxTest, EmptyResponse) { |
| 215 scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty()); | 227 scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty()); |
| 216 RunOnNotifierThread(base::Bind(response_callback_, response.get())); | 228 RunOnNotifierThread(base::Bind(response_callback_, response.get())); |
| 217 EXPECT_FALSE(NetworkChangeNotifier::IsOffline()); | 229 EXPECT_NE(NetworkChangeNotifier::CONNECTION_NONE, |
| 230 NetworkChangeNotifier::GetConnectionType()); |
| 218 } | 231 } |
| 219 | 232 |
| 220 TEST_F(NetworkChangeNotifierLinuxTest, InvalidResponse) { | 233 TEST_F(NetworkChangeNotifierLinuxTest, InvalidResponse) { |
| 221 scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty()); | 234 scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty()); |
| 222 dbus::MessageWriter writer(response.get()); | 235 dbus::MessageWriter writer(response.get()); |
| 223 writer.AppendUint16(20); // Uint16 instead of the expected Uint32 | 236 writer.AppendUint16(20); // Uint16 instead of the expected Uint32 |
| 224 RunOnNotifierThread(base::Bind(response_callback_, response.get())); | 237 RunOnNotifierThread(base::Bind(response_callback_, response.get())); |
| 225 EXPECT_FALSE(NetworkChangeNotifier::IsOffline()); | 238 EXPECT_NE(NetworkChangeNotifier::CONNECTION_NONE, |
| 239 NetworkChangeNotifier::GetConnectionType()); |
| 226 } | 240 } |
| 227 | 241 |
| 228 } // namespace | 242 } // namespace |
| 229 } // namespace net | 243 } // namespace net |
| OLD | NEW |