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 "base/bind.h" | 5 #include "base/bind.h" |
6 #include "base/memory/scoped_ptr.h" | 6 #include "base/memory/scoped_ptr.h" |
7 #include "base/values.h" | 7 #include "base/values.h" |
8 #include "chrome/browser/chromeos/cros/cros_network_functions.h" | 8 #include "chrome/browser/chromeos/cros/cros_network_functions.h" |
9 #include "chrome/browser/chromeos/cros/gvalue_util.h" | 9 #include "chrome/browser/chromeos/cros/gvalue_util.h" |
10 #include "chrome/browser/chromeos/cros/mock_chromeos_network.h" | 10 #include "chrome/browser/chromeos/cros/mock_chromeos_network.h" |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
76 | 76 |
77 return base::Bind(&MockNetworkPropertiesWatcherCallback::Run, | 77 return base::Bind(&MockNetworkPropertiesWatcherCallback::Run, |
78 base::Owned(mock_callback)); | 78 base::Owned(mock_callback)); |
79 } | 79 } |
80 | 80 |
81 MOCK_METHOD3(Run, void(const std::string& expected_path, | 81 MOCK_METHOD3(Run, void(const std::string& expected_path, |
82 const std::string& expected_key, | 82 const std::string& expected_key, |
83 const base::Value& value)); | 83 const base::Value& value)); |
84 }; | 84 }; |
85 | 85 |
| 86 // A mock to check arguments of CellularDataPlanWatcherCallback and ensure that |
| 87 // the callback is called exactly once. |
| 88 class MockDataPlanUpdateWatcherCallback { |
| 89 public: |
| 90 // Creates a NetworkPropertiesWatcherCallback with expectations. |
| 91 static DataPlanUpdateWatcherCallback CreateCallback( |
| 92 const std::string& expected_modem_service_path, |
| 93 const CellularDataPlanVector& expected_data_plan_vector) { |
| 94 MockDataPlanUpdateWatcherCallback* mock_callback = |
| 95 new MockDataPlanUpdateWatcherCallback; |
| 96 mock_callback->expected_data_plan_vector_ = &expected_data_plan_vector; |
| 97 |
| 98 EXPECT_CALL(*mock_callback, |
| 99 Run(expected_modem_service_path, _)) |
| 100 .WillOnce(Invoke(mock_callback, |
| 101 &MockDataPlanUpdateWatcherCallback::CheckDataPlans)); |
| 102 |
| 103 return base::Bind(&MockDataPlanUpdateWatcherCallback::Run, |
| 104 base::Owned(mock_callback)); |
| 105 } |
| 106 |
| 107 MOCK_METHOD2(Run, void(const std::string& modem_service_path, |
| 108 CellularDataPlanVector* data_plan_vector)); |
| 109 |
| 110 private: |
| 111 void CheckDataPlans(const std::string& modem_service_path, |
| 112 CellularDataPlanVector* data_plan_vector) { |
| 113 ASSERT_EQ(expected_data_plan_vector_->size(), data_plan_vector->size()); |
| 114 for (size_t i = 0; i != data_plan_vector->size(); ++i) { |
| 115 EXPECT_EQ((*expected_data_plan_vector_)[i]->plan_name, |
| 116 (*data_plan_vector)[i]->plan_name); |
| 117 EXPECT_EQ((*expected_data_plan_vector_)[i]->plan_type, |
| 118 (*data_plan_vector)[i]->plan_type); |
| 119 EXPECT_EQ((*expected_data_plan_vector_)[i]->update_time, |
| 120 (*data_plan_vector)[i]->update_time); |
| 121 EXPECT_EQ((*expected_data_plan_vector_)[i]->plan_start_time, |
| 122 (*data_plan_vector)[i]->plan_start_time); |
| 123 EXPECT_EQ((*expected_data_plan_vector_)[i]->plan_end_time, |
| 124 (*data_plan_vector)[i]->plan_end_time); |
| 125 EXPECT_EQ((*expected_data_plan_vector_)[i]->plan_data_bytes, |
| 126 (*data_plan_vector)[i]->plan_data_bytes); |
| 127 EXPECT_EQ((*expected_data_plan_vector_)[i]->data_bytes_used, |
| 128 (*data_plan_vector)[i]->data_bytes_used); |
| 129 } |
| 130 delete data_plan_vector; |
| 131 } |
| 132 |
| 133 const CellularDataPlanVector* expected_data_plan_vector_; |
| 134 }; |
| 135 |
86 } // namespace | 136 } // namespace |
87 | 137 |
88 // Test for cros_network_functions.cc with Libcros. | 138 // Test for cros_network_functions.cc with Libcros. |
89 class CrosNetworkFunctionsLibcrosTest : public testing::Test { | 139 class CrosNetworkFunctionsLibcrosTest : public testing::Test { |
90 public: | 140 public: |
91 CrosNetworkFunctionsLibcrosTest() : argument_ghash_table_(NULL) {} | 141 CrosNetworkFunctionsLibcrosTest() : argument_ghash_table_(NULL) {} |
92 | 142 |
93 virtual void SetUp() { | 143 virtual void SetUp() { |
94 ::g_type_init(); | 144 ::g_type_init(); |
95 MockChromeOSNetwork::Initialize(); | 145 MockChromeOSNetwork::Initialize(); |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
171 OnRequestNetworkProperties2(callback, object); | 221 OnRequestNetworkProperties2(callback, object); |
172 } | 222 } |
173 | 223 |
174 // Does nothing. Used as an argument. | 224 // Does nothing. Used as an argument. |
175 static void OnNetworkAction(void* object, | 225 static void OnNetworkAction(void* object, |
176 const char* path, | 226 const char* path, |
177 NetworkMethodErrorType error, | 227 NetworkMethodErrorType error, |
178 const char* error_message) {} | 228 const char* error_message) {} |
179 | 229 |
180 // Does nothing. Used as an argument. | 230 // Does nothing. Used as an argument. |
181 static void OnDataPlansUpdate(void* object, | |
182 const char* modem_service_path, | |
183 const CellularDataPlanList* dataplan) {} | |
184 | |
185 // Does nothing. Used as an argument. | |
186 static void OnSmsReceived(void* object, | 231 static void OnSmsReceived(void* object, |
187 const char* modem_device_path, | 232 const char* modem_device_path, |
188 const SMS* message) {} | 233 const SMS* message) {} |
189 | 234 |
190 protected: | 235 protected: |
191 ScopedGValue argument_gvalue_; | 236 ScopedGValue argument_gvalue_; |
192 ScopedGHashTable argument_ghash_table_; | 237 ScopedGHashTable argument_ghash_table_; |
193 ScopedGHashTable result_ghash_table_; | 238 ScopedGHashTable result_ghash_table_; |
194 base::Callback<void(const char* path, | 239 base::Callback<void(const char* path, |
195 const char* key, | 240 const char* key, |
(...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
369 g_value_set_int(gvalue.get(), kValue); | 414 g_value_set_int(gvalue.get(), kValue); |
370 property_changed_callback_.Run(path.c_str(), key.c_str(), gvalue.get()); | 415 property_changed_callback_.Run(path.c_str(), key.c_str(), gvalue.get()); |
371 | 416 |
372 // Stop monitoring. | 417 // Stop monitoring. |
373 EXPECT_CALL(*MockChromeOSNetwork::Get(), | 418 EXPECT_CALL(*MockChromeOSNetwork::Get(), |
374 DisconnectNetworkPropertiesMonitor(_)).Times(1); | 419 DisconnectNetworkPropertiesMonitor(_)).Times(1); |
375 delete watcher; | 420 delete watcher; |
376 } | 421 } |
377 | 422 |
378 TEST_F(CrosNetworkFunctionsLibcrosTest, CrosMonitorCellularDataPlan) { | 423 TEST_F(CrosNetworkFunctionsLibcrosTest, CrosMonitorCellularDataPlan) { |
379 MonitorDataPlanCallback callback = | 424 DataPlanUpdateMonitor monitor = NULL; // dummy |
380 &CrosNetworkFunctionsLibcrosTest::OnDataPlansUpdate; | 425 const std::string modem_service_path = "/modem/path"; |
381 void* object = this; | 426 CellularDataPlanInfo data_plan = {}; |
| 427 data_plan.plan_name = "plan name"; |
| 428 data_plan.update_time = 123456; |
| 429 data_plan.plan_start_time = 234567; |
| 430 data_plan.plan_end_time = 345678; |
| 431 data_plan.plan_data_bytes = 1024*1024; |
| 432 data_plan.data_bytes_used = 12345; |
| 433 CellularDataPlanList data_plans = {}; |
| 434 data_plans.plans = &data_plan; |
| 435 data_plans.plans_size = 1; |
| 436 data_plans.data_plan_size = sizeof(data_plan); |
| 437 |
| 438 CellularDataPlanVector result; |
| 439 result.push_back(new CellularDataPlan(data_plan)); |
| 440 |
| 441 // Set expectations. |
| 442 DataPlanUpdateWatcherCallback callback = |
| 443 MockDataPlanUpdateWatcherCallback::CreateCallback(modem_service_path, |
| 444 result); |
| 445 MonitorDataPlanCallback arg_callback = NULL; |
| 446 void* arg_object = NULL; |
| 447 EXPECT_CALL(*MockChromeOSNetwork::Get(), MonitorCellularDataPlan(_, _)) |
| 448 .WillOnce(DoAll(SaveArg<0>(&arg_callback), SaveArg<1>(&arg_object), |
| 449 Return(monitor))); |
382 | 450 |
383 // Start monitoring. | 451 // Start monitoring. |
384 EXPECT_CALL(*MockChromeOSNetwork::Get(), | 452 CrosNetworkWatcher* watcher = CrosMonitorCellularDataPlan(callback); |
385 MonitorCellularDataPlan(callback, object)).Times(1); | 453 |
386 CrosNetworkWatcher* watcher = CrosMonitorCellularDataPlan(callback, object); | 454 // Run callback. |
| 455 arg_callback(arg_object, modem_service_path.c_str(), &data_plans); |
387 | 456 |
388 // Stop monitoring. | 457 // Stop monitoring. |
389 EXPECT_CALL(*MockChromeOSNetwork::Get(), | 458 EXPECT_CALL(*MockChromeOSNetwork::Get(), |
390 DisconnectDataPlanUpdateMonitor(_)).Times(1); | 459 DisconnectDataPlanUpdateMonitor(monitor)).Times(1); |
391 delete watcher; | 460 delete watcher; |
392 } | 461 } |
393 | 462 |
394 TEST_F(CrosNetworkFunctionsLibcrosTest, CrosMonitorSMS) { | 463 TEST_F(CrosNetworkFunctionsLibcrosTest, CrosMonitorSMS) { |
395 const std::string modem_device_path = "/modem/device/path"; | 464 const std::string modem_device_path = "/modem/device/path"; |
396 MonitorSMSCallback callback = &CrosNetworkFunctionsLibcrosTest::OnSmsReceived; | 465 MonitorSMSCallback callback = &CrosNetworkFunctionsLibcrosTest::OnSmsReceived; |
397 void* object = this; | 466 void* object = this; |
398 | 467 |
399 // Start monitoring. | 468 // Start monitoring. |
400 EXPECT_CALL(*MockChromeOSNetwork::Get(), | 469 EXPECT_CALL(*MockChromeOSNetwork::Get(), |
(...skipping 1018 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1419 const std::string string2 = "string2"; | 1488 const std::string string2 = "string2"; |
1420 base::DictionaryValue value; | 1489 base::DictionaryValue value; |
1421 value.SetString(key1, string1); | 1490 value.SetString(key1, string1); |
1422 value.SetString(key2, string2); | 1491 value.SetString(key2, string2); |
1423 EXPECT_CALL(*mock_manager_client_, ConfigureService(IsEqualTo(&value), _)) | 1492 EXPECT_CALL(*mock_manager_client_, ConfigureService(IsEqualTo(&value), _)) |
1424 .Times(1); | 1493 .Times(1); |
1425 CrosConfigureService(value); | 1494 CrosConfigureService(value); |
1426 } | 1495 } |
1427 | 1496 |
1428 } // namespace chromeos | 1497 } // namespace chromeos |
OLD | NEW |