Add python coverage module to third_party
[chromium-blink-merge.git] / chromeos / network / network_configuration_handler_unittest.cc
blobf92e85a7f842316e5c96cd5ebe6497a8c8727443
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
3 // found in the LICENSE file.
5 #include <map>
6 #include <set>
8 #include "base/bind.h"
9 #include "base/json/json_writer.h"
10 #include "base/message_loop/message_loop.h"
11 #include "base/stl_util.h"
12 #include "base/strings/string_piece.h"
13 #include "base/values.h"
14 #include "chromeos/dbus/dbus_thread_manager.h"
15 #include "chromeos/dbus/mock_shill_manager_client.h"
16 #include "chromeos/dbus/mock_shill_profile_client.h"
17 #include "chromeos/dbus/mock_shill_service_client.h"
18 #include "chromeos/network/network_configuration_handler.h"
19 #include "chromeos/network/network_configuration_observer.h"
20 #include "chromeos/network/network_profile_handler.h"
21 #include "chromeos/network/network_state.h"
22 #include "chromeos/network/network_state_handler.h"
23 #include "chromeos/network/network_state_handler_observer.h"
24 #include "chromeos/network/shill_property_util.h"
25 #include "testing/gmock/include/gmock/gmock.h"
26 #include "testing/gtest/include/gtest/gtest.h"
27 #include "third_party/cros_system_api/dbus/service_constants.h"
29 using ::testing::_;
30 using ::testing::AnyNumber;
31 using ::testing::Invoke;
32 using ::testing::Pointee;
33 using ::testing::Return;
34 using ::testing::SaveArg;
35 using ::testing::StrEq;
37 // Matcher to match base::Value.
38 MATCHER_P(IsEqualTo, value, "") {
39 return arg.Equals(value);
42 namespace chromeos {
44 namespace {
46 static std::string PrettyJson(const base::DictionaryValue& value) {
47 std::string pretty;
48 base::JSONWriter::WriteWithOptions(
49 &value, base::JSONWriter::OPTIONS_PRETTY_PRINT, &pretty);
50 return pretty;
53 void DictionaryValueCallback(const std::string& expected_id,
54 const std::string& expected_json,
55 const std::string& service_path,
56 const base::DictionaryValue& dictionary) {
57 std::string dict_str = PrettyJson(dictionary);
58 EXPECT_EQ(expected_json, dict_str);
59 EXPECT_EQ(expected_id, service_path);
62 void ErrorCallback(bool error_expected,
63 const std::string& expected_id,
64 const std::string& error_name,
65 scoped_ptr<base::DictionaryValue> error_data) {
66 EXPECT_TRUE(error_expected) << "Unexpected error: " << error_name
67 << " with associated data: \n"
68 << PrettyJson(*error_data);
71 void StringResultCallback(const std::string& expected_result,
72 const std::string& result) {
73 EXPECT_EQ(expected_result, result);
76 void DBusErrorCallback(const std::string& error_name,
77 const std::string& error_message) {
78 EXPECT_TRUE(false) << "DBus Error: " << error_name << "(" << error_message
79 << ")";
82 class TestCallback {
83 public:
84 TestCallback() : run_count_(0) {}
85 void Run() { ++run_count_; }
86 int run_count() const { return run_count_; }
88 private:
89 int run_count_;
92 class TestNetworkConfigurationObserver : public NetworkConfigurationObserver {
93 public:
94 TestNetworkConfigurationObserver() {}
95 ~TestNetworkConfigurationObserver() override {
96 STLDeleteContainerPairSecondPointers(configurations_.begin(),
97 configurations_.end());
100 // NetworkConfigurationObserver
101 void OnConfigurationCreated(
102 const std::string& service_path,
103 const std::string& profile_path,
104 const base::DictionaryValue& properties,
105 NetworkConfigurationObserver::Source source) override {
106 ASSERT_EQ(0u, configurations_.count(service_path));
107 configurations_[service_path] = properties.DeepCopy();
108 profiles_[profile_path].insert(service_path);
111 void OnConfigurationRemoved(
112 const std::string& service_path,
113 const std::string& guid,
114 NetworkConfigurationObserver::Source source) override {
115 ASSERT_EQ(1u, configurations_.count(service_path));
116 delete configurations_[service_path];
117 configurations_.erase(service_path);
118 for (auto& p : profiles_) {
119 p.second.erase(service_path);
123 void OnConfigurationProfileChanged(
124 const std::string& service_path,
125 const std::string& profile_path,
126 NetworkConfigurationObserver::Source source) override {
127 for (auto& p : profiles_) {
128 p.second.erase(service_path);
130 profiles_[profile_path].insert(service_path);
133 void OnPropertiesSet(const std::string& service_path,
134 const std::string& guid,
135 const base::DictionaryValue& set_properties,
136 NetworkConfigurationObserver::Source source) override {
137 configurations_[service_path]->MergeDictionary(&set_properties);
140 bool HasConfiguration(const std::string& service_path) {
141 return configurations_.count(service_path) == 1;
144 std::string GetStringProperty(const std::string& service_path,
145 const std::string& property) {
146 if (!HasConfiguration(service_path))
147 return "";
148 std::string result;
149 configurations_[service_path]->GetStringWithoutPathExpansion(property,
150 &result);
151 return result;
154 bool HasConfigurationInProfile(const std::string& service_path,
155 const std::string& profile_path) {
156 if (profiles_.count(profile_path) == 0)
157 return false;
158 return profiles_[profile_path].count(service_path) == 1;
161 private:
162 std::map<std::string, base::DictionaryValue*> configurations_;
163 std::map<std::string, std::set<std::string>> profiles_;
165 DISALLOW_COPY_AND_ASSIGN(TestNetworkConfigurationObserver);
168 } // namespace
170 class NetworkConfigurationHandlerTest : public testing::Test {
171 public:
172 NetworkConfigurationHandlerTest()
173 : mock_manager_client_(NULL),
174 mock_profile_client_(NULL),
175 mock_service_client_(NULL),
176 dictionary_value_result_(NULL) {}
177 virtual ~NetworkConfigurationHandlerTest() {}
179 virtual void SetUp() override {
180 scoped_ptr<DBusThreadManagerSetter> dbus_setter =
181 DBusThreadManager::GetSetterForTesting();
182 mock_manager_client_ = new MockShillManagerClient();
183 mock_profile_client_ = new MockShillProfileClient();
184 mock_service_client_ = new MockShillServiceClient();
185 dbus_setter->SetShillManagerClient(
186 scoped_ptr<ShillManagerClient>(mock_manager_client_).Pass());
187 dbus_setter->SetShillProfileClient(
188 scoped_ptr<ShillProfileClient>(mock_profile_client_).Pass());
189 dbus_setter->SetShillServiceClient(
190 scoped_ptr<ShillServiceClient>(mock_service_client_).Pass());
192 EXPECT_CALL(*mock_service_client_, GetProperties(_, _)).Times(AnyNumber());
193 EXPECT_CALL(*mock_manager_client_, GetProperties(_)).Times(AnyNumber());
194 EXPECT_CALL(*mock_manager_client_, AddPropertyChangedObserver(_))
195 .Times(AnyNumber());
196 EXPECT_CALL(*mock_manager_client_, RemovePropertyChangedObserver(_))
197 .Times(AnyNumber());
199 network_state_handler_.reset(NetworkStateHandler::InitializeForTest());
200 network_configuration_handler_.reset(new NetworkConfigurationHandler());
201 network_configuration_handler_->Init(network_state_handler_.get());
202 message_loop_.RunUntilIdle();
205 virtual void TearDown() override {
206 network_configuration_handler_.reset();
207 network_state_handler_.reset();
208 DBusThreadManager::Shutdown();
211 // Handles responses for GetProperties method calls.
212 void OnGetProperties(
213 const dbus::ObjectPath& path,
214 const ShillClientHelper::DictionaryValueCallback& callback) {
215 callback.Run(DBUS_METHOD_CALL_SUCCESS, *dictionary_value_result_);
218 // Handles responses for SetProperties method calls.
219 void OnSetProperties(const dbus::ObjectPath& service_path,
220 const base::DictionaryValue& properties,
221 const base::Closure& callback,
222 const ShillClientHelper::ErrorCallback& error_callback) {
223 callback.Run();
226 // Handles responses for ClearProperties method calls.
227 void OnClearProperties(
228 const dbus::ObjectPath& service_path,
229 const std::vector<std::string>& names,
230 const ShillClientHelper::ListValueCallback& callback,
231 const ShillClientHelper::ErrorCallback& error_callback) {
232 base::ListValue result;
233 result.AppendBoolean(true);
234 callback.Run(result);
237 // Handles responses for ClearProperties method calls, and simulates an error
238 // result.
239 void OnClearPropertiesError(
240 const dbus::ObjectPath& service_path,
241 const std::vector<std::string>& names,
242 const ShillClientHelper::ListValueCallback& callback,
243 const ShillClientHelper::ErrorCallback& error_callback) {
244 base::ListValue result;
245 result.AppendBoolean(false);
246 callback.Run(result);
249 void OnConfigureService(
250 const dbus::ObjectPath& profile_path,
251 const base::DictionaryValue& properties,
252 const ObjectPathCallback& callback,
253 const ShillClientHelper::ErrorCallback& error_callback) {
254 callback.Run(dbus::ObjectPath("/service/2"));
257 void OnGetLoadableProfileEntries(
258 const dbus::ObjectPath& service_path,
259 const ShillClientHelper::DictionaryValueCallback& callback) {
260 base::DictionaryValue entries;
261 entries.SetString("profile1", "entry1");
262 entries.SetString("profile2", "entry2");
263 callback.Run(DBUS_METHOD_CALL_SUCCESS, entries);
266 void OnDeleteEntry(const dbus::ObjectPath& profile_path,
267 const std::string& entry_path,
268 const base::Closure& callback,
269 const ShillClientHelper::ErrorCallback& error_callback) {
270 // Don't run the callback immediately to emulate actual behavior.
271 message_loop_.PostTask(FROM_HERE, callback);
274 bool PendingProfileEntryDeleterForTest(const std::string& service_path) {
275 return network_configuration_handler_->PendingProfileEntryDeleterForTest(
276 service_path);
279 protected:
280 MockShillManagerClient* mock_manager_client_;
281 MockShillProfileClient* mock_profile_client_;
282 MockShillServiceClient* mock_service_client_;
283 scoped_ptr<NetworkStateHandler> network_state_handler_;
284 scoped_ptr<NetworkConfigurationHandler> network_configuration_handler_;
285 base::MessageLoopForUI message_loop_;
286 base::DictionaryValue* dictionary_value_result_;
289 TEST_F(NetworkConfigurationHandlerTest, GetProperties) {
290 std::string service_path = "/service/1";
291 std::string expected_json = "{\n \"SSID\": \"MyNetwork\"\n}\n";
292 std::string networkName = "MyNetwork";
293 std::string key = "SSID";
294 scoped_ptr<base::StringValue> networkNameValue(
295 new base::StringValue(networkName));
297 base::DictionaryValue value;
298 value.Set(key, new base::StringValue(networkName));
299 dictionary_value_result_ = &value;
300 EXPECT_CALL(*mock_service_client_,
301 SetProperty(dbus::ObjectPath(service_path), key,
302 IsEqualTo(networkNameValue.get()), _, _)).Times(1);
303 mock_service_client_->SetProperty(
304 dbus::ObjectPath(service_path), key, *networkNameValue,
305 base::Bind(&base::DoNothing), base::Bind(&DBusErrorCallback));
306 message_loop_.RunUntilIdle();
308 ShillServiceClient::DictionaryValueCallback get_properties_callback;
309 EXPECT_CALL(*mock_service_client_, GetProperties(_, _))
310 .WillOnce(
311 Invoke(this, &NetworkConfigurationHandlerTest::OnGetProperties));
312 network_configuration_handler_->GetProperties(
313 service_path,
314 base::Bind(&DictionaryValueCallback, service_path, expected_json),
315 base::Bind(&ErrorCallback, false, service_path));
316 message_loop_.RunUntilIdle();
319 TEST_F(NetworkConfigurationHandlerTest, SetProperties) {
320 std::string service_path = "/service/1";
321 std::string networkName = "MyNetwork";
322 std::string key = "SSID";
323 scoped_ptr<base::StringValue> networkNameValue(
324 new base::StringValue(networkName));
326 base::DictionaryValue value;
327 value.Set(key, new base::StringValue(networkName));
328 dictionary_value_result_ = &value;
329 EXPECT_CALL(*mock_service_client_, SetProperties(_, _, _, _))
330 .WillOnce(
331 Invoke(this, &NetworkConfigurationHandlerTest::OnSetProperties));
332 network_configuration_handler_->SetProperties(
333 service_path, value, NetworkConfigurationObserver::SOURCE_USER_ACTION,
334 base::Bind(&base::DoNothing),
335 base::Bind(&ErrorCallback, false, service_path));
336 message_loop_.RunUntilIdle();
339 TEST_F(NetworkConfigurationHandlerTest, ClearProperties) {
340 std::string service_path = "/service/1";
341 std::string networkName = "MyNetwork";
342 std::string key = "SSID";
343 scoped_ptr<base::StringValue> networkNameValue(
344 new base::StringValue(networkName));
346 // First set up a value to clear.
347 base::DictionaryValue value;
348 value.Set(key, new base::StringValue(networkName));
349 dictionary_value_result_ = &value;
350 EXPECT_CALL(*mock_service_client_, SetProperties(_, _, _, _))
351 .WillOnce(
352 Invoke(this, &NetworkConfigurationHandlerTest::OnSetProperties));
353 network_configuration_handler_->SetProperties(
354 service_path, value, NetworkConfigurationObserver::SOURCE_USER_ACTION,
355 base::Bind(&base::DoNothing),
356 base::Bind(&ErrorCallback, false, service_path));
357 message_loop_.RunUntilIdle();
359 // Now clear it.
360 std::vector<std::string> values_to_clear;
361 values_to_clear.push_back(key);
362 EXPECT_CALL(*mock_service_client_, ClearProperties(_, _, _, _))
363 .WillOnce(
364 Invoke(this, &NetworkConfigurationHandlerTest::OnClearProperties));
365 network_configuration_handler_->ClearProperties(
366 service_path, values_to_clear, base::Bind(&base::DoNothing),
367 base::Bind(&ErrorCallback, false, service_path));
368 message_loop_.RunUntilIdle();
371 TEST_F(NetworkConfigurationHandlerTest, ClearPropertiesError) {
372 std::string service_path = "/service/1";
373 std::string networkName = "MyNetwork";
374 std::string key = "SSID";
375 scoped_ptr<base::StringValue> networkNameValue(
376 new base::StringValue(networkName));
378 // First set up a value to clear.
379 base::DictionaryValue value;
380 value.Set(key, new base::StringValue(networkName));
381 dictionary_value_result_ = &value;
382 EXPECT_CALL(*mock_service_client_, SetProperties(_, _, _, _))
383 .WillOnce(
384 Invoke(this, &NetworkConfigurationHandlerTest::OnSetProperties));
385 network_configuration_handler_->SetProperties(
386 service_path, value, NetworkConfigurationObserver::SOURCE_USER_ACTION,
387 base::Bind(&base::DoNothing),
388 base::Bind(&ErrorCallback, false, service_path));
389 message_loop_.RunUntilIdle();
391 // Now clear it.
392 std::vector<std::string> values_to_clear;
393 values_to_clear.push_back(key);
394 EXPECT_CALL(*mock_service_client_, ClearProperties(_, _, _, _))
395 .WillOnce(Invoke(
396 this, &NetworkConfigurationHandlerTest::OnClearPropertiesError));
397 network_configuration_handler_->ClearProperties(
398 service_path, values_to_clear, base::Bind(&base::DoNothing),
399 base::Bind(&ErrorCallback, true, service_path));
400 message_loop_.RunUntilIdle();
403 TEST_F(NetworkConfigurationHandlerTest, CreateConfiguration) {
404 std::string networkName = "MyNetwork";
405 std::string key = "SSID";
406 std::string type = "wifi";
407 std::string profile = "profile path";
408 base::DictionaryValue value;
409 shill_property_util::SetSSID(networkName, &value);
410 value.SetWithoutPathExpansion(shill::kTypeProperty,
411 new base::StringValue(type));
412 value.SetWithoutPathExpansion(shill::kProfileProperty,
413 new base::StringValue(profile));
415 EXPECT_CALL(*mock_manager_client_,
416 ConfigureServiceForProfile(dbus::ObjectPath(profile), _, _, _))
417 .WillOnce(
418 Invoke(this, &NetworkConfigurationHandlerTest::OnConfigureService));
419 network_configuration_handler_->CreateConfiguration(
420 value, NetworkConfigurationObserver::SOURCE_USER_ACTION,
421 base::Bind(&StringResultCallback, std::string("/service/2")),
422 base::Bind(&ErrorCallback, false, std::string()));
423 message_loop_.RunUntilIdle();
426 TEST_F(NetworkConfigurationHandlerTest, RemoveConfiguration) {
427 std::string service_path = "/service/1";
429 TestCallback test_callback;
430 EXPECT_CALL(*mock_service_client_, GetLoadableProfileEntries(_, _))
431 .WillOnce(Invoke(
432 this, &NetworkConfigurationHandlerTest::OnGetLoadableProfileEntries));
433 EXPECT_CALL(*mock_profile_client_, DeleteEntry(_, _, _, _))
434 .WillRepeatedly(
435 Invoke(this, &NetworkConfigurationHandlerTest::OnDeleteEntry));
437 network_configuration_handler_->RemoveConfiguration(
438 service_path, NetworkConfigurationObserver::SOURCE_USER_ACTION,
439 base::Bind(&TestCallback::Run, base::Unretained(&test_callback)),
440 base::Bind(&ErrorCallback, false, service_path));
441 message_loop_.RunUntilIdle();
442 EXPECT_EQ(1, test_callback.run_count());
443 EXPECT_FALSE(PendingProfileEntryDeleterForTest(service_path));
446 ////////////////////////////////////////////////////////////////////////////////
447 // Stub based tests
449 namespace {
451 class TestObserver : public chromeos::NetworkStateHandlerObserver {
452 public:
453 TestObserver() : network_list_changed_count_(0) {}
454 virtual ~TestObserver() {}
456 virtual void NetworkListChanged() override { ++network_list_changed_count_; }
458 virtual void NetworkPropertiesUpdated(const NetworkState* network) override {
459 property_updates_[network->path()]++;
462 size_t network_list_changed_count() { return network_list_changed_count_; }
464 int PropertyUpdatesForService(const std::string& service_path) {
465 return property_updates_[service_path];
468 void ClearPropertyUpdates() { property_updates_.clear(); }
470 private:
471 size_t network_list_changed_count_;
472 std::map<std::string, int> property_updates_;
474 DISALLOW_COPY_AND_ASSIGN(TestObserver);
477 } // namespace
479 class NetworkConfigurationHandlerStubTest : public testing::Test {
480 public:
481 NetworkConfigurationHandlerStubTest() {}
483 virtual ~NetworkConfigurationHandlerStubTest() {}
485 virtual void SetUp() override {
486 DBusThreadManager::Initialize();
488 network_state_handler_.reset(NetworkStateHandler::InitializeForTest());
489 test_observer_.reset(new TestObserver());
490 network_state_handler_->AddObserver(test_observer_.get(), FROM_HERE);
492 network_configuration_handler_.reset(new NetworkConfigurationHandler());
493 network_configuration_handler_->Init(network_state_handler_.get());
495 message_loop_.RunUntilIdle();
496 test_observer_->ClearPropertyUpdates();
499 virtual void TearDown() override {
500 network_configuration_handler_.reset();
501 network_state_handler_->RemoveObserver(test_observer_.get(), FROM_HERE);
502 network_state_handler_.reset();
503 DBusThreadManager::Shutdown();
506 void SuccessCallback(const std::string& callback_name) {
507 success_callback_name_ = callback_name;
510 void GetPropertiesCallback(const std::string& service_path,
511 const base::DictionaryValue& dictionary) {
512 get_properties_path_ = service_path;
513 get_properties_.reset(dictionary.DeepCopy());
516 void CreateConfigurationCallback(const std::string& service_path) {
517 create_service_path_ = service_path;
520 void CreateTestConfiguration(const std::string& service_path,
521 const std::string& type) {
522 base::DictionaryValue properties;
523 shill_property_util::SetSSID(service_path, &properties);
524 properties.SetStringWithoutPathExpansion(shill::kNameProperty,
525 service_path);
526 properties.SetStringWithoutPathExpansion(shill::kGuidProperty,
527 service_path);
528 properties.SetStringWithoutPathExpansion(shill::kTypeProperty, type);
529 properties.SetStringWithoutPathExpansion(shill::kStateProperty,
530 shill::kStateIdle);
531 properties.SetStringWithoutPathExpansion(
532 shill::kProfileProperty, NetworkProfileHandler::GetSharedProfilePath());
534 network_configuration_handler_->CreateConfiguration(
535 properties, NetworkConfigurationObserver::SOURCE_USER_ACTION,
536 base::Bind(
537 &NetworkConfigurationHandlerStubTest::CreateConfigurationCallback,
538 base::Unretained(this)),
539 base::Bind(&ErrorCallback, false, service_path));
540 message_loop_.RunUntilIdle();
543 protected:
544 bool GetServiceStringProperty(const std::string& service_path,
545 const std::string& key,
546 std::string* result) {
547 ShillServiceClient::TestInterface* service_test =
548 DBusThreadManager::Get()->GetShillServiceClient()->GetTestInterface();
549 const base::DictionaryValue* properties =
550 service_test->GetServiceProperties(service_path);
551 if (properties && properties->GetStringWithoutPathExpansion(key, result))
552 return true;
553 return false;
556 bool GetReceivedStringProperty(const std::string& service_path,
557 const std::string& key,
558 std::string* result) {
559 if (get_properties_path_ != service_path)
560 return false;
561 if (get_properties_ &&
562 get_properties_->GetStringWithoutPathExpansion(key, result))
563 return true;
564 return false;
567 scoped_ptr<NetworkStateHandler> network_state_handler_;
568 scoped_ptr<NetworkConfigurationHandler> network_configuration_handler_;
569 scoped_ptr<TestObserver> test_observer_;
570 base::MessageLoopForUI message_loop_;
571 std::string success_callback_name_;
572 std::string get_properties_path_;
573 scoped_ptr<base::DictionaryValue> get_properties_;
574 std::string create_service_path_;
577 TEST_F(NetworkConfigurationHandlerStubTest, StubSetAndClearProperties) {
578 // TODO(stevenjb): Remove dependency on default Stub service.
579 const std::string service_path("/service/wifi1");
580 const std::string test_identity("test_identity");
581 const std::string test_passphrase("test_passphrase");
583 // Set Properties
584 base::DictionaryValue properties_to_set;
585 properties_to_set.SetStringWithoutPathExpansion(shill::kIdentityProperty,
586 test_identity);
587 properties_to_set.SetStringWithoutPathExpansion(shill::kPassphraseProperty,
588 test_passphrase);
589 network_configuration_handler_->SetProperties(
590 service_path, properties_to_set,
591 NetworkConfigurationObserver::SOURCE_USER_ACTION,
592 base::Bind(&NetworkConfigurationHandlerStubTest::SuccessCallback,
593 base::Unretained(this), "SetProperties"),
594 base::Bind(&ErrorCallback, false, service_path));
595 message_loop_.RunUntilIdle();
597 EXPECT_EQ("SetProperties", success_callback_name_);
598 std::string identity, passphrase;
599 EXPECT_TRUE(GetServiceStringProperty(service_path, shill::kIdentityProperty,
600 &identity));
601 EXPECT_TRUE(GetServiceStringProperty(service_path, shill::kPassphraseProperty,
602 &passphrase));
603 EXPECT_EQ(test_identity, identity);
604 EXPECT_EQ(test_passphrase, passphrase);
605 EXPECT_EQ(1, test_observer_->PropertyUpdatesForService(service_path));
607 // Clear Properties
608 std::vector<std::string> properties_to_clear;
609 properties_to_clear.push_back(shill::kIdentityProperty);
610 properties_to_clear.push_back(shill::kPassphraseProperty);
611 network_configuration_handler_->ClearProperties(
612 service_path, properties_to_clear,
613 base::Bind(&NetworkConfigurationHandlerStubTest::SuccessCallback,
614 base::Unretained(this), "ClearProperties"),
615 base::Bind(&ErrorCallback, false, service_path));
616 message_loop_.RunUntilIdle();
618 EXPECT_EQ("ClearProperties", success_callback_name_);
619 EXPECT_FALSE(GetServiceStringProperty(service_path, shill::kIdentityProperty,
620 &identity));
621 EXPECT_FALSE(GetServiceStringProperty(service_path, shill::kIdentityProperty,
622 &passphrase));
623 EXPECT_EQ(2, test_observer_->PropertyUpdatesForService(service_path));
626 TEST_F(NetworkConfigurationHandlerStubTest, StubGetNameFromWifiHex) {
627 // TODO(stevenjb): Remove dependency on default Stub service.
628 const std::string service_path("/service/wifi1");
629 std::string wifi_hex = "5468697320697320484558205353494421";
630 std::string expected_name = "This is HEX SSID!";
632 // Set Properties
633 base::DictionaryValue properties_to_set;
634 properties_to_set.SetStringWithoutPathExpansion(shill::kWifiHexSsid,
635 wifi_hex);
636 network_configuration_handler_->SetProperties(
637 service_path, properties_to_set,
638 NetworkConfigurationObserver::SOURCE_USER_ACTION,
639 base::Bind(&base::DoNothing),
640 base::Bind(&ErrorCallback, false, service_path));
641 message_loop_.RunUntilIdle();
642 std::string wifi_hex_result;
643 EXPECT_TRUE(GetServiceStringProperty(service_path, shill::kWifiHexSsid,
644 &wifi_hex_result));
645 EXPECT_EQ(wifi_hex, wifi_hex_result);
647 // Get Properties
648 network_configuration_handler_->GetProperties(
649 service_path,
650 base::Bind(&NetworkConfigurationHandlerStubTest::GetPropertiesCallback,
651 base::Unretained(this)),
652 base::Bind(&ErrorCallback, false, service_path));
653 message_loop_.RunUntilIdle();
655 EXPECT_EQ(service_path, get_properties_path_);
656 std::string name_result;
657 EXPECT_TRUE(GetReceivedStringProperty(service_path, shill::kNameProperty,
658 &name_result));
659 EXPECT_EQ(expected_name, name_result);
662 TEST_F(NetworkConfigurationHandlerStubTest, StubCreateConfiguration) {
663 const std::string service_path("/service/test_wifi");
664 CreateTestConfiguration(service_path, shill::kTypeWifi);
666 EXPECT_FALSE(create_service_path_.empty());
668 std::string guid;
669 EXPECT_TRUE(GetServiceStringProperty(create_service_path_,
670 shill::kGuidProperty, &guid));
671 EXPECT_EQ(service_path, guid);
673 std::string actual_profile;
674 EXPECT_TRUE(GetServiceStringProperty(
675 create_service_path_, shill::kProfileProperty, &actual_profile));
676 EXPECT_EQ(NetworkProfileHandler::GetSharedProfilePath(), actual_profile);
679 TEST_F(NetworkConfigurationHandlerStubTest, NetworkConfigurationObserver) {
680 const std::string service_path("/service/test_wifi");
681 const std::string test_passphrase("test_passphrase");
683 scoped_ptr<TestNetworkConfigurationObserver> test_observer(
684 new TestNetworkConfigurationObserver);
685 network_configuration_handler_->AddObserver(test_observer.get());
686 CreateTestConfiguration(service_path, shill::kTypeWifi);
688 EXPECT_TRUE(test_observer->HasConfiguration(service_path));
689 EXPECT_TRUE(test_observer->HasConfigurationInProfile(
690 service_path, NetworkProfileHandler::GetSharedProfilePath()));
691 EXPECT_EQ(shill::kTypeWifi, test_observer->GetStringProperty(
692 service_path, shill::kTypeProperty));
694 base::DictionaryValue properties_to_set;
695 properties_to_set.SetStringWithoutPathExpansion(shill::kPassphraseProperty,
696 test_passphrase);
697 network_configuration_handler_->SetProperties(
698 service_path, properties_to_set,
699 NetworkConfigurationObserver::SOURCE_USER_ACTION,
700 base::Bind(&base::DoNothing),
701 base::Bind(&ErrorCallback, false, service_path));
702 message_loop_.RunUntilIdle();
703 EXPECT_EQ(test_passphrase, test_observer->GetStringProperty(
704 service_path, shill::kPassphraseProperty));
706 std::string user_profile = "/profiles/user1";
707 std::string userhash = "user1";
708 DBusThreadManager::Get()
709 ->GetShillProfileClient()
710 ->GetTestInterface()
711 ->AddProfile(user_profile, userhash);
713 network_configuration_handler_->SetNetworkProfile(
714 service_path, user_profile,
715 NetworkConfigurationObserver::SOURCE_USER_ACTION,
716 base::Bind(&base::DoNothing),
717 base::Bind(&ErrorCallback, false, service_path));
718 message_loop_.RunUntilIdle();
719 EXPECT_TRUE(test_observer->HasConfiguration(service_path));
720 EXPECT_FALSE(test_observer->HasConfigurationInProfile(
721 service_path, NetworkProfileHandler::GetSharedProfilePath()));
722 EXPECT_TRUE(
723 test_observer->HasConfigurationInProfile(service_path, user_profile));
725 network_configuration_handler_->RemoveConfiguration(
726 service_path, NetworkConfigurationObserver::SOURCE_USER_ACTION,
727 base::Bind(&base::DoNothing),
728 base::Bind(&ErrorCallback, false, service_path));
729 message_loop_.RunUntilIdle();
731 EXPECT_FALSE(test_observer->HasConfiguration(service_path));
732 EXPECT_FALSE(test_observer->HasConfigurationInProfile(
733 service_path, NetworkProfileHandler::GetSharedProfilePath()));
734 EXPECT_FALSE(
735 test_observer->HasConfigurationInProfile(service_path, user_profile));
737 network_configuration_handler_->RemoveObserver(test_observer.get());
740 } // namespace chromeos