Revert 248827 "android: Migrate old content readback to use asyn..."
[chromium-blink-merge.git] / chromeos / network / network_device_handler_unittest.cc
blob60a264bc6e9a34dbfd84ae38e3abf38bd60d21f4
1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/bind.h"
6 #include "base/memory/scoped_ptr.h"
7 #include "base/message_loop/message_loop.h"
8 #include "base/values.h"
9 #include "chromeos/dbus/fake_dbus_thread_manager.h"
10 #include "chromeos/dbus/fake_shill_device_client.h"
11 #include "chromeos/dbus/fake_shill_manager_client.h"
12 #include "chromeos/network/network_device_handler_impl.h"
13 #include "chromeos/network/network_state_handler.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15 #include "third_party/cros_system_api/dbus/service_constants.h"
17 namespace chromeos {
19 namespace {
21 const char kDefaultCellularDevicePath[] = "stub_cellular_device";
22 const char kUnknownCellularDevicePath[] = "unknown_cellular_device";
23 const char kDefaultWifiDevicePath[] = "stub_wifi_device";
24 const char kResultSuccess[] = "success";
26 } // namespace
28 class NetworkDeviceHandlerTest : public testing::Test {
29 public:
30 NetworkDeviceHandlerTest() : fake_device_client_(NULL) {}
31 virtual ~NetworkDeviceHandlerTest() {}
33 virtual void SetUp() OVERRIDE {
34 FakeDBusThreadManager* dbus_manager = new FakeDBusThreadManager;
35 dbus_manager->SetFakeShillClients();
37 fake_device_client_ = new FakeShillDeviceClient;
38 dbus_manager->SetShillDeviceClient(
39 scoped_ptr<ShillDeviceClient>(fake_device_client_));
40 DBusThreadManager::InitializeForTesting(dbus_manager);
42 ShillDeviceClient::TestInterface* device_test =
43 fake_device_client_->GetTestInterface();
44 device_test->AddDevice(
45 kDefaultCellularDevicePath, shill::kTypeCellular, "cellular1");
46 device_test->AddDevice(kDefaultWifiDevicePath, shill::kTypeWifi, "wifi1");
48 base::ListValue test_ip_configs;
49 test_ip_configs.AppendString("ip_config1");
50 device_test->SetDeviceProperty(
51 kDefaultWifiDevicePath, shill::kIPConfigsProperty, test_ip_configs);
53 success_callback_ = base::Bind(&NetworkDeviceHandlerTest::SuccessCallback,
54 base::Unretained(this));
55 properties_success_callback_ =
56 base::Bind(&NetworkDeviceHandlerTest::PropertiesSuccessCallback,
57 base::Unretained(this));
58 error_callback_ = base::Bind(&NetworkDeviceHandlerTest::ErrorCallback,
59 base::Unretained(this));
61 network_state_handler_.reset(NetworkStateHandler::InitializeForTest());
62 NetworkDeviceHandlerImpl* device_handler = new NetworkDeviceHandlerImpl;
63 device_handler->Init(network_state_handler_.get());
64 network_device_handler_.reset(device_handler);
67 virtual void TearDown() OVERRIDE {
68 network_device_handler_.reset();
69 network_state_handler_.reset();
70 DBusThreadManager::Shutdown();
73 void ErrorCallback(const std::string& error_name,
74 scoped_ptr<base::DictionaryValue> error_data) {
75 result_ = error_name;
78 void SuccessCallback() {
79 result_ = kResultSuccess;
82 void PropertiesSuccessCallback(const std::string& device_path,
83 const base::DictionaryValue& properties) {
84 result_ = kResultSuccess;
85 properties_.reset(properties.DeepCopy());
88 protected:
89 std::string result_;
91 FakeShillDeviceClient* fake_device_client_;
92 scoped_ptr<NetworkDeviceHandler> network_device_handler_;
93 scoped_ptr<NetworkStateHandler> network_state_handler_;
94 base::MessageLoopForUI message_loop_;
95 base::Closure success_callback_;
96 network_handler::DictionaryResultCallback properties_success_callback_;
97 network_handler::ErrorCallback error_callback_;
98 scoped_ptr<base::DictionaryValue> properties_;
100 private:
101 DISALLOW_COPY_AND_ASSIGN(NetworkDeviceHandlerTest);
104 TEST_F(NetworkDeviceHandlerTest, GetDeviceProperties) {
105 network_device_handler_->GetDeviceProperties(
106 kDefaultWifiDevicePath, properties_success_callback_, error_callback_);
107 message_loop_.RunUntilIdle();
108 EXPECT_EQ(kResultSuccess, result_);
109 std::string type;
110 properties_->GetString(shill::kTypeProperty, &type);
111 EXPECT_EQ(shill::kTypeWifi, type);
114 TEST_F(NetworkDeviceHandlerTest, SetDeviceProperty) {
115 // Set the shill::kScanIntervalProperty to true. The call
116 // should succeed and the value should be set.
117 network_device_handler_->SetDeviceProperty(kDefaultCellularDevicePath,
118 shill::kScanIntervalProperty,
119 base::FundamentalValue(1),
120 success_callback_,
121 error_callback_);
122 message_loop_.RunUntilIdle();
123 EXPECT_EQ(kResultSuccess, result_);
125 // GetDeviceProperties should return the value set by SetDeviceProperty.
126 network_device_handler_->GetDeviceProperties(kDefaultCellularDevicePath,
127 properties_success_callback_,
128 error_callback_);
129 message_loop_.RunUntilIdle();
130 EXPECT_EQ(kResultSuccess, result_);
131 int interval = 0;
132 EXPECT_TRUE(properties_->GetIntegerWithoutPathExpansion(
133 shill::kScanIntervalProperty, &interval));
134 EXPECT_EQ(1, interval);
136 // Repeat the same with value false.
137 network_device_handler_->SetDeviceProperty(kDefaultCellularDevicePath,
138 shill::kScanIntervalProperty,
139 base::FundamentalValue(2),
140 success_callback_,
141 error_callback_);
142 message_loop_.RunUntilIdle();
143 EXPECT_EQ(kResultSuccess, result_);
145 network_device_handler_->GetDeviceProperties(kDefaultCellularDevicePath,
146 properties_success_callback_,
147 error_callback_);
148 message_loop_.RunUntilIdle();
149 EXPECT_EQ(kResultSuccess, result_);
150 EXPECT_TRUE(properties_->GetIntegerWithoutPathExpansion(
151 shill::kScanIntervalProperty, &interval));
152 EXPECT_EQ(2, interval);
154 // Set property on an invalid path.
155 network_device_handler_->SetDeviceProperty(kUnknownCellularDevicePath,
156 shill::kScanIntervalProperty,
157 base::FundamentalValue(1),
158 success_callback_,
159 error_callback_);
160 message_loop_.RunUntilIdle();
161 EXPECT_EQ(NetworkDeviceHandler::kErrorFailure, result_);
163 // Setting a owner-protected device property through SetDeviceProperty must
164 // fail.
165 network_device_handler_->SetDeviceProperty(
166 kDefaultCellularDevicePath,
167 shill::kCellularAllowRoamingProperty,
168 base::FundamentalValue(true),
169 success_callback_,
170 error_callback_);
171 message_loop_.RunUntilIdle();
172 EXPECT_NE(kResultSuccess, result_);
176 TEST_F(NetworkDeviceHandlerTest, CellularAllowRoaming) {
177 // Start with disabled data roaming.
178 ShillDeviceClient::TestInterface* device_test =
179 fake_device_client_->GetTestInterface();
180 device_test->SetDeviceProperty(kDefaultCellularDevicePath,
181 shill::kCellularAllowRoamingProperty,
182 base::FundamentalValue(false));
184 network_device_handler_->SetCellularAllowRoaming(true);
185 message_loop_.RunUntilIdle();
187 // Roaming should be enabled now.
188 network_device_handler_->GetDeviceProperties(kDefaultCellularDevicePath,
189 properties_success_callback_,
190 error_callback_);
191 message_loop_.RunUntilIdle();
192 EXPECT_EQ(kResultSuccess, result_);
193 bool allow_roaming;
194 EXPECT_TRUE(properties_->GetBooleanWithoutPathExpansion(
195 shill::kCellularAllowRoamingProperty, &allow_roaming));
196 EXPECT_TRUE(allow_roaming);
198 network_device_handler_->SetCellularAllowRoaming(false);
199 message_loop_.RunUntilIdle();
201 // Roaming should be disable again.
202 network_device_handler_->GetDeviceProperties(kDefaultCellularDevicePath,
203 properties_success_callback_,
204 error_callback_);
205 message_loop_.RunUntilIdle();
206 EXPECT_EQ(kResultSuccess, result_);
207 EXPECT_TRUE(properties_->GetBooleanWithoutPathExpansion(
208 shill::kCellularAllowRoamingProperty, &allow_roaming));
209 EXPECT_FALSE(allow_roaming);
212 TEST_F(NetworkDeviceHandlerTest, RequestRefreshIPConfigs) {
213 network_device_handler_->RequestRefreshIPConfigs(
214 kDefaultWifiDevicePath, success_callback_, error_callback_);
215 message_loop_.RunUntilIdle();
216 EXPECT_EQ(kResultSuccess, result_);
217 // TODO(stevenjb): Add test interface to ShillIPConfigClient and test
218 // refresh calls.
221 TEST_F(NetworkDeviceHandlerTest, SetCarrier) {
222 const char kCarrier[] = "carrier";
224 // Test that the success callback gets called.
225 network_device_handler_->SetCarrier(
226 kDefaultCellularDevicePath, kCarrier, success_callback_, error_callback_);
227 message_loop_.RunUntilIdle();
228 EXPECT_EQ(kResultSuccess, result_);
230 // Test that the shill error propagates to the error callback.
231 network_device_handler_->SetCarrier(
232 kUnknownCellularDevicePath, kCarrier, success_callback_, error_callback_);
233 message_loop_.RunUntilIdle();
234 EXPECT_EQ(NetworkDeviceHandler::kErrorFailure, result_);
237 TEST_F(NetworkDeviceHandlerTest, RequirePin) {
238 const char kPin[] = "1234";
240 // Test that the success callback gets called.
241 network_device_handler_->RequirePin(kDefaultCellularDevicePath,
242 true,
243 kPin,
244 success_callback_,
245 error_callback_);
246 message_loop_.RunUntilIdle();
247 EXPECT_EQ(kResultSuccess, result_);
249 // Test that the shill error propagates to the error callback.
250 network_device_handler_->RequirePin(kUnknownCellularDevicePath,
251 true,
252 kPin,
253 success_callback_,
254 error_callback_);
255 message_loop_.RunUntilIdle();
256 EXPECT_EQ(NetworkDeviceHandler::kErrorFailure, result_);
259 TEST_F(NetworkDeviceHandlerTest, EnterPin) {
260 const char kPin[] = "1234";
262 // Test that the success callback gets called.
263 network_device_handler_->EnterPin(
264 kDefaultCellularDevicePath, kPin, success_callback_, error_callback_);
265 message_loop_.RunUntilIdle();
266 EXPECT_EQ(kResultSuccess, result_);
268 // Test that the shill error propagates to the error callback.
269 network_device_handler_->EnterPin(
270 kUnknownCellularDevicePath, kPin, success_callback_, error_callback_);
271 message_loop_.RunUntilIdle();
272 EXPECT_EQ(NetworkDeviceHandler::kErrorFailure, result_);
275 TEST_F(NetworkDeviceHandlerTest, UnblockPin) {
276 const char kPuk[] = "12345678";
277 const char kPin[] = "1234";
279 // Test that the success callback gets called.
280 network_device_handler_->UnblockPin(kDefaultCellularDevicePath,
281 kPin,
282 kPuk,
283 success_callback_,
284 error_callback_);
285 message_loop_.RunUntilIdle();
286 EXPECT_EQ(kResultSuccess, result_);
288 // Test that the shill error propagates to the error callback.
289 network_device_handler_->UnblockPin(kUnknownCellularDevicePath,
290 kPin,
291 kPuk,
292 success_callback_,
293 error_callback_);
294 message_loop_.RunUntilIdle();
295 EXPECT_EQ(NetworkDeviceHandler::kErrorFailure, result_);
298 TEST_F(NetworkDeviceHandlerTest, ChangePin) {
299 const char kOldPin[] = "4321";
300 const char kNewPin[] = "1234";
302 // Test that the success callback gets called.
303 network_device_handler_->ChangePin(kDefaultCellularDevicePath,
304 kOldPin,
305 kNewPin,
306 success_callback_,
307 error_callback_);
308 message_loop_.RunUntilIdle();
309 EXPECT_EQ(kResultSuccess, result_);
311 // Test that the shill error propagates to the error callback.
312 network_device_handler_->ChangePin(kUnknownCellularDevicePath,
313 kOldPin,
314 kNewPin,
315 success_callback_,
316 error_callback_);
317 message_loop_.RunUntilIdle();
318 EXPECT_EQ(NetworkDeviceHandler::kErrorFailure, result_);
321 } // namespace chromeos