Chromium Code Reviews| 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_TRUE(NetworkChangeNotifier::GetConnectionType() == |
| 147 NetworkChangeNotifier::CONNECTION_NONE); | |
|
wtc
2012/05/11 01:35:05
Nit: these should ideally be replaced by EXPECT_EQ
| |
| 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_FALSE(NetworkChangeNotifier::GetConnectionType() == |
| 153 NetworkChangeNotifier::CONNECTION_NONE); | |
| 151 } | 154 } |
| 152 | 155 |
| 153 TEST_F(NetworkChangeNotifierLinuxTest, OfflineThenOnline) { | 156 TEST_F(NetworkChangeNotifierLinuxTest, OfflineThenOnline) { |
| 154 OfflineObserver observer; | 157 OfflineObserver observer; |
| 155 | 158 |
| 156 SendResponse(NM_STATE_DISCONNECTED); | 159 SendResponse(NM_STATE_DISCONNECTED); |
| 157 EXPECT_TRUE(NetworkChangeNotifier::IsOffline()); | 160 EXPECT_TRUE(NetworkChangeNotifier::GetConnectionType() == |
| 161 NetworkChangeNotifier::CONNECTION_NONE); | |
| 158 EXPECT_EQ(0, observer.notification_count); | 162 EXPECT_EQ(0, observer.notification_count); |
| 159 | 163 |
| 160 SendSignal(NM_STATE_CONNECTED_GLOBAL); | 164 SendSignal(NM_STATE_CONNECTED_GLOBAL); |
| 161 EXPECT_FALSE(NetworkChangeNotifier::IsOffline()); | 165 EXPECT_FALSE(NetworkChangeNotifier::GetConnectionType() == |
| 166 NetworkChangeNotifier::CONNECTION_NONE); | |
| 162 EXPECT_EQ(1, observer.notification_count); | 167 EXPECT_EQ(1, observer.notification_count); |
| 163 EXPECT_TRUE(observer.last_online_value); | 168 EXPECT_TRUE(observer.last_online_value); |
| 164 } | 169 } |
| 165 | 170 |
| 166 TEST_F(NetworkChangeNotifierLinuxTest, MultipleStateChanges) { | 171 TEST_F(NetworkChangeNotifierLinuxTest, MultipleStateChanges) { |
| 167 OfflineObserver observer; | 172 OfflineObserver observer; |
| 168 | 173 |
| 169 SendResponse(NM_STATE_CONNECTED_GLOBAL); | 174 SendResponse(NM_STATE_CONNECTED_GLOBAL); |
| 170 EXPECT_FALSE(NetworkChangeNotifier::IsOffline()); | 175 EXPECT_FALSE(NetworkChangeNotifier::GetConnectionType() == |
| 176 NetworkChangeNotifier::CONNECTION_NONE); | |
| 171 EXPECT_EQ(0, observer.notification_count); | 177 EXPECT_EQ(0, observer.notification_count); |
| 172 | 178 |
| 173 SendSignal(NM_STATE_DISCONNECTED); | 179 SendSignal(NM_STATE_DISCONNECTED); |
| 174 EXPECT_TRUE(NetworkChangeNotifier::IsOffline()); | 180 EXPECT_TRUE(NetworkChangeNotifier::GetConnectionType() == |
| 181 NetworkChangeNotifier::CONNECTION_NONE); | |
| 175 EXPECT_EQ(1, observer.notification_count); | 182 EXPECT_EQ(1, observer.notification_count); |
| 176 EXPECT_FALSE(observer.last_online_value); | 183 EXPECT_FALSE(observer.last_online_value); |
| 177 | 184 |
| 178 SendSignal(NM_STATE_CONNECTED_GLOBAL); | 185 SendSignal(NM_STATE_CONNECTED_GLOBAL); |
| 179 EXPECT_FALSE(NetworkChangeNotifier::IsOffline()); | 186 EXPECT_FALSE(NetworkChangeNotifier::GetConnectionType() == |
| 187 NetworkChangeNotifier::CONNECTION_NONE); | |
| 180 EXPECT_EQ(2, observer.notification_count); | 188 EXPECT_EQ(2, observer.notification_count); |
| 181 EXPECT_TRUE(observer.last_online_value); | 189 EXPECT_TRUE(observer.last_online_value); |
| 182 } | 190 } |
| 183 | 191 |
| 184 TEST_F(NetworkChangeNotifierLinuxTest, IgnoreContinuedOnlineState) { | 192 TEST_F(NetworkChangeNotifierLinuxTest, IgnoreContinuedOnlineState) { |
| 185 OfflineObserver observer; | 193 OfflineObserver observer; |
| 186 | 194 |
| 187 SendResponse(NM_STATE_CONNECTED_SITE); | 195 SendResponse(NM_STATE_CONNECTED_SITE); |
| 188 EXPECT_FALSE(NetworkChangeNotifier::IsOffline()); | 196 EXPECT_FALSE(NetworkChangeNotifier::GetConnectionType() == |
| 197 NetworkChangeNotifier::CONNECTION_NONE); | |
| 189 EXPECT_EQ(0, observer.notification_count); | 198 EXPECT_EQ(0, observer.notification_count); |
| 190 | 199 |
| 191 SendSignal(NM_STATE_CONNECTED_GLOBAL); | 200 SendSignal(NM_STATE_CONNECTED_GLOBAL); |
| 192 EXPECT_FALSE(NetworkChangeNotifier::IsOffline()); | 201 EXPECT_FALSE(NetworkChangeNotifier::GetConnectionType() == |
| 202 NetworkChangeNotifier::CONNECTION_NONE); | |
| 193 EXPECT_EQ(0, observer.notification_count); | 203 EXPECT_EQ(0, observer.notification_count); |
| 194 } | 204 } |
| 195 | 205 |
| 196 TEST_F(NetworkChangeNotifierLinuxTest, IgnoreContinuedOfflineState) { | 206 TEST_F(NetworkChangeNotifierLinuxTest, IgnoreContinuedOfflineState) { |
| 197 OfflineObserver observer; | 207 OfflineObserver observer; |
| 198 | 208 |
| 199 SendResponse(NM_STATE_DISCONNECTING); | 209 SendResponse(NM_STATE_DISCONNECTING); |
| 200 EXPECT_TRUE(NetworkChangeNotifier::IsOffline()); | 210 EXPECT_TRUE(NetworkChangeNotifier::GetConnectionType() == |
| 211 NetworkChangeNotifier::CONNECTION_NONE); | |
| 201 EXPECT_EQ(0, observer.notification_count); | 212 EXPECT_EQ(0, observer.notification_count); |
| 202 | 213 |
| 203 SendSignal(NM_STATE_DISCONNECTED); | 214 SendSignal(NM_STATE_DISCONNECTED); |
| 204 EXPECT_TRUE(NetworkChangeNotifier::IsOffline()); | 215 EXPECT_TRUE(NetworkChangeNotifier::GetConnectionType() == |
| 216 NetworkChangeNotifier::CONNECTION_NONE); | |
| 205 EXPECT_EQ(0, observer.notification_count); | 217 EXPECT_EQ(0, observer.notification_count); |
| 206 } | 218 } |
| 207 | 219 |
| 208 TEST_F(NetworkChangeNotifierLinuxTest, NullResponse) { | 220 TEST_F(NetworkChangeNotifierLinuxTest, NullResponse) { |
| 209 RunOnNotifierThread(base::Bind( | 221 RunOnNotifierThread(base::Bind( |
| 210 response_callback_, static_cast<dbus::Response*>(NULL))); | 222 response_callback_, static_cast<dbus::Response*>(NULL))); |
| 211 EXPECT_FALSE(NetworkChangeNotifier::IsOffline()); | 223 EXPECT_FALSE(NetworkChangeNotifier::GetConnectionType() == |
| 224 NetworkChangeNotifier::CONNECTION_NONE); | |
| 212 } | 225 } |
| 213 | 226 |
| 214 TEST_F(NetworkChangeNotifierLinuxTest, EmptyResponse) { | 227 TEST_F(NetworkChangeNotifierLinuxTest, EmptyResponse) { |
| 215 scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty()); | 228 scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty()); |
| 216 RunOnNotifierThread(base::Bind(response_callback_, response.get())); | 229 RunOnNotifierThread(base::Bind(response_callback_, response.get())); |
| 217 EXPECT_FALSE(NetworkChangeNotifier::IsOffline()); | 230 EXPECT_FALSE(NetworkChangeNotifier::GetConnectionType() == |
| 231 NetworkChangeNotifier::CONNECTION_NONE); | |
| 218 } | 232 } |
| 219 | 233 |
| 220 TEST_F(NetworkChangeNotifierLinuxTest, InvalidResponse) { | 234 TEST_F(NetworkChangeNotifierLinuxTest, InvalidResponse) { |
| 221 scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty()); | 235 scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty()); |
| 222 dbus::MessageWriter writer(response.get()); | 236 dbus::MessageWriter writer(response.get()); |
| 223 writer.AppendUint16(20); // Uint16 instead of the expected Uint32 | 237 writer.AppendUint16(20); // Uint16 instead of the expected Uint32 |
| 224 RunOnNotifierThread(base::Bind(response_callback_, response.get())); | 238 RunOnNotifierThread(base::Bind(response_callback_, response.get())); |
| 225 EXPECT_FALSE(NetworkChangeNotifier::IsOffline()); | 239 EXPECT_FALSE(NetworkChangeNotifier::GetConnectionType() == |
| 240 NetworkChangeNotifier::CONNECTION_NONE); | |
| 226 } | 241 } |
| 227 | 242 |
| 228 } // namespace | 243 } // namespace |
| 229 } // namespace net | 244 } // namespace net |
| OLD | NEW |