Fix style on iOS for distilled content.
[chromium-blink-merge.git] / chromeos / network / network_configuration_handler_unittest.cc
blob2772fc9b6d19d1faaa91c521f79459b754be00f9
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 ~NetworkConfigurationHandlerTest() override {}
179 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 NULL /* network_device_handler */);
203 message_loop_.RunUntilIdle();
206 void TearDown() override {
207 network_configuration_handler_.reset();
208 network_state_handler_.reset();
209 DBusThreadManager::Shutdown();
212 // Handles responses for GetProperties method calls.
213 void OnGetProperties(
214 const dbus::ObjectPath& path,
215 const ShillClientHelper::DictionaryValueCallback& callback) {
216 callback.Run(DBUS_METHOD_CALL_SUCCESS, *dictionary_value_result_);
219 // Handles responses for SetProperties method calls.
220 void OnSetProperties(const dbus::ObjectPath& service_path,
221 const base::DictionaryValue& properties,
222 const base::Closure& callback,
223 const ShillClientHelper::ErrorCallback& error_callback) {
224 callback.Run();
227 // Handles responses for ClearProperties method calls.
228 void OnClearProperties(
229 const dbus::ObjectPath& service_path,
230 const std::vector<std::string>& names,
231 const ShillClientHelper::ListValueCallback& callback,
232 const ShillClientHelper::ErrorCallback& error_callback) {
233 base::ListValue result;
234 result.AppendBoolean(true);
235 callback.Run(result);
238 // Handles responses for ClearProperties method calls, and simulates an error
239 // result.
240 void OnClearPropertiesError(
241 const dbus::ObjectPath& service_path,
242 const std::vector<std::string>& names,
243 const ShillClientHelper::ListValueCallback& callback,
244 const ShillClientHelper::ErrorCallback& error_callback) {
245 base::ListValue result;
246 result.AppendBoolean(false);
247 callback.Run(result);
250 void OnConfigureService(
251 const dbus::ObjectPath& profile_path,
252 const base::DictionaryValue& properties,
253 const ObjectPathCallback& callback,
254 const ShillClientHelper::ErrorCallback& error_callback) {
255 callback.Run(dbus::ObjectPath("/service/2"));
258 void OnGetLoadableProfileEntries(
259 const dbus::ObjectPath& service_path,
260 const ShillClientHelper::DictionaryValueCallback& callback) {
261 base::DictionaryValue entries;
262 entries.SetString("profile1", "entry1");
263 entries.SetString("profile2", "entry2");
264 callback.Run(DBUS_METHOD_CALL_SUCCESS, entries);
267 void OnDeleteEntry(const dbus::ObjectPath& profile_path,
268 const std::string& entry_path,
269 const base::Closure& callback,
270 const ShillClientHelper::ErrorCallback& error_callback) {
271 // Don't run the callback immediately to emulate actual behavior.
272 message_loop_.PostTask(FROM_HERE, callback);
275 bool PendingProfileEntryDeleterForTest(const std::string& service_path) {
276 return network_configuration_handler_->PendingProfileEntryDeleterForTest(
277 service_path);
280 void CreateConfiguration(const std::string& service_path,
281 const base::DictionaryValue& properties) {
282 network_configuration_handler_->CreateShillConfiguration(
283 properties, NetworkConfigurationObserver::SOURCE_USER_ACTION,
284 base::Bind(&StringResultCallback, service_path),
285 base::Bind(&ErrorCallback, false, std::string()));
288 protected:
289 MockShillManagerClient* mock_manager_client_;
290 MockShillProfileClient* mock_profile_client_;
291 MockShillServiceClient* mock_service_client_;
292 scoped_ptr<NetworkStateHandler> network_state_handler_;
293 scoped_ptr<NetworkConfigurationHandler> network_configuration_handler_;
294 base::MessageLoopForUI message_loop_;
295 base::DictionaryValue* dictionary_value_result_;
298 TEST_F(NetworkConfigurationHandlerTest, GetProperties) {
299 std::string service_path = "/service/1";
300 std::string expected_json = "{\n \"SSID\": \"MyNetwork\"\n}\n";
301 std::string networkName = "MyNetwork";
302 std::string key = "SSID";
303 scoped_ptr<base::StringValue> networkNameValue(
304 new base::StringValue(networkName));
306 base::DictionaryValue value;
307 value.Set(key, new base::StringValue(networkName));
308 dictionary_value_result_ = &value;
309 EXPECT_CALL(*mock_service_client_,
310 SetProperty(dbus::ObjectPath(service_path), key,
311 IsEqualTo(networkNameValue.get()), _, _)).Times(1);
312 mock_service_client_->SetProperty(
313 dbus::ObjectPath(service_path), key, *networkNameValue,
314 base::Bind(&base::DoNothing), base::Bind(&DBusErrorCallback));
315 message_loop_.RunUntilIdle();
317 ShillServiceClient::DictionaryValueCallback get_properties_callback;
318 EXPECT_CALL(*mock_service_client_, GetProperties(_, _))
319 .WillOnce(
320 Invoke(this, &NetworkConfigurationHandlerTest::OnGetProperties));
321 network_configuration_handler_->GetShillProperties(
322 service_path,
323 base::Bind(&DictionaryValueCallback, service_path, expected_json),
324 base::Bind(&ErrorCallback, false, service_path));
325 message_loop_.RunUntilIdle();
328 TEST_F(NetworkConfigurationHandlerTest, SetProperties) {
329 std::string service_path = "/service/1";
330 std::string networkName = "MyNetwork";
331 std::string key = "SSID";
332 scoped_ptr<base::StringValue> networkNameValue(
333 new base::StringValue(networkName));
335 base::DictionaryValue value;
336 value.Set(key, new base::StringValue(networkName));
337 dictionary_value_result_ = &value;
338 EXPECT_CALL(*mock_service_client_, SetProperties(_, _, _, _))
339 .WillOnce(
340 Invoke(this, &NetworkConfigurationHandlerTest::OnSetProperties));
341 network_configuration_handler_->SetShillProperties(
342 service_path, value, NetworkConfigurationObserver::SOURCE_USER_ACTION,
343 base::Bind(&base::DoNothing),
344 base::Bind(&ErrorCallback, false, service_path));
345 message_loop_.RunUntilIdle();
348 TEST_F(NetworkConfigurationHandlerTest, ClearProperties) {
349 std::string service_path = "/service/1";
350 std::string networkName = "MyNetwork";
351 std::string key = "SSID";
352 scoped_ptr<base::StringValue> networkNameValue(
353 new base::StringValue(networkName));
355 // First set up a value to clear.
356 base::DictionaryValue value;
357 value.Set(key, new base::StringValue(networkName));
358 dictionary_value_result_ = &value;
359 EXPECT_CALL(*mock_service_client_, SetProperties(_, _, _, _))
360 .WillOnce(
361 Invoke(this, &NetworkConfigurationHandlerTest::OnSetProperties));
362 network_configuration_handler_->SetShillProperties(
363 service_path, value, NetworkConfigurationObserver::SOURCE_USER_ACTION,
364 base::Bind(&base::DoNothing),
365 base::Bind(&ErrorCallback, false, service_path));
366 message_loop_.RunUntilIdle();
368 // Now clear it.
369 std::vector<std::string> values_to_clear;
370 values_to_clear.push_back(key);
371 EXPECT_CALL(*mock_service_client_, ClearProperties(_, _, _, _))
372 .WillOnce(
373 Invoke(this, &NetworkConfigurationHandlerTest::OnClearProperties));
374 network_configuration_handler_->ClearShillProperties(
375 service_path, values_to_clear, base::Bind(&base::DoNothing),
376 base::Bind(&ErrorCallback, false, service_path));
377 message_loop_.RunUntilIdle();
380 TEST_F(NetworkConfigurationHandlerTest, ClearPropertiesError) {
381 std::string service_path = "/service/1";
382 std::string networkName = "MyNetwork";
383 std::string key = "SSID";
384 scoped_ptr<base::StringValue> networkNameValue(
385 new base::StringValue(networkName));
387 // First set up a value to clear.
388 base::DictionaryValue value;
389 value.Set(key, new base::StringValue(networkName));
390 dictionary_value_result_ = &value;
391 EXPECT_CALL(*mock_service_client_, SetProperties(_, _, _, _))
392 .WillOnce(
393 Invoke(this, &NetworkConfigurationHandlerTest::OnSetProperties));
394 network_configuration_handler_->SetShillProperties(
395 service_path, value, NetworkConfigurationObserver::SOURCE_USER_ACTION,
396 base::Bind(&base::DoNothing),
397 base::Bind(&ErrorCallback, false, service_path));
398 message_loop_.RunUntilIdle();
400 // Now clear it.
401 std::vector<std::string> values_to_clear;
402 values_to_clear.push_back(key);
403 EXPECT_CALL(*mock_service_client_, ClearProperties(_, _, _, _))
404 .WillOnce(Invoke(
405 this, &NetworkConfigurationHandlerTest::OnClearPropertiesError));
406 network_configuration_handler_->ClearShillProperties(
407 service_path, values_to_clear, base::Bind(&base::DoNothing),
408 base::Bind(&ErrorCallback, true, service_path));
409 message_loop_.RunUntilIdle();
412 TEST_F(NetworkConfigurationHandlerTest, CreateConfiguration) {
413 std::string networkName = "MyNetwork";
414 std::string key = "SSID";
415 std::string type = "wifi";
416 std::string profile = "profile path";
417 base::DictionaryValue value;
418 shill_property_util::SetSSID(networkName, &value);
419 value.SetWithoutPathExpansion(shill::kTypeProperty,
420 new base::StringValue(type));
421 value.SetWithoutPathExpansion(shill::kProfileProperty,
422 new base::StringValue(profile));
424 EXPECT_CALL(*mock_manager_client_,
425 ConfigureServiceForProfile(dbus::ObjectPath(profile), _, _, _))
426 .WillOnce(
427 Invoke(this, &NetworkConfigurationHandlerTest::OnConfigureService));
428 CreateConfiguration("/service/2", value);
429 message_loop_.RunUntilIdle();
432 TEST_F(NetworkConfigurationHandlerTest, RemoveConfiguration) {
433 std::string service_path = "/service/1";
435 TestCallback test_callback;
436 EXPECT_CALL(*mock_service_client_, GetLoadableProfileEntries(_, _))
437 .WillOnce(Invoke(
438 this, &NetworkConfigurationHandlerTest::OnGetLoadableProfileEntries));
439 EXPECT_CALL(*mock_profile_client_, DeleteEntry(_, _, _, _))
440 .WillRepeatedly(
441 Invoke(this, &NetworkConfigurationHandlerTest::OnDeleteEntry));
443 network_configuration_handler_->RemoveConfiguration(
444 service_path, NetworkConfigurationObserver::SOURCE_USER_ACTION,
445 base::Bind(&TestCallback::Run, base::Unretained(&test_callback)),
446 base::Bind(&ErrorCallback, false, service_path));
447 message_loop_.RunUntilIdle();
448 EXPECT_EQ(1, test_callback.run_count());
449 EXPECT_FALSE(PendingProfileEntryDeleterForTest(service_path));
452 ////////////////////////////////////////////////////////////////////////////////
453 // Stub based tests
455 namespace {
457 class TestObserver : public chromeos::NetworkStateHandlerObserver {
458 public:
459 TestObserver() : network_list_changed_count_(0) {}
460 ~TestObserver() override {}
462 void NetworkListChanged() override { ++network_list_changed_count_; }
464 void NetworkPropertiesUpdated(const NetworkState* network) override {
465 property_updates_[network->path()]++;
468 size_t network_list_changed_count() { return network_list_changed_count_; }
470 int PropertyUpdatesForService(const std::string& service_path) {
471 return property_updates_[service_path];
474 void ClearPropertyUpdates() { property_updates_.clear(); }
476 private:
477 size_t network_list_changed_count_;
478 std::map<std::string, int> property_updates_;
480 DISALLOW_COPY_AND_ASSIGN(TestObserver);
483 } // namespace
485 class NetworkConfigurationHandlerStubTest : public testing::Test {
486 public:
487 NetworkConfigurationHandlerStubTest() {}
489 ~NetworkConfigurationHandlerStubTest() override {}
491 void SetUp() override {
492 DBusThreadManager::Initialize();
494 network_state_handler_.reset(NetworkStateHandler::InitializeForTest());
495 test_observer_.reset(new TestObserver());
496 network_state_handler_->AddObserver(test_observer_.get(), FROM_HERE);
498 network_configuration_handler_.reset(new NetworkConfigurationHandler());
499 network_configuration_handler_->Init(network_state_handler_.get(),
500 NULL /* network_device_handler */);
502 message_loop_.RunUntilIdle();
503 test_observer_->ClearPropertyUpdates();
506 void TearDown() override {
507 network_configuration_handler_.reset();
508 network_state_handler_->RemoveObserver(test_observer_.get(), FROM_HERE);
509 network_state_handler_.reset();
510 DBusThreadManager::Shutdown();
513 void SuccessCallback(const std::string& callback_name) {
514 success_callback_name_ = callback_name;
517 void GetPropertiesCallback(const std::string& service_path,
518 const base::DictionaryValue& dictionary) {
519 get_properties_path_ = service_path;
520 get_properties_.reset(dictionary.DeepCopy());
523 void CreateConfigurationCallback(const std::string& service_path) {
524 create_service_path_ = service_path;
527 void CreateTestConfiguration(const std::string& service_path,
528 const std::string& type) {
529 base::DictionaryValue properties;
530 shill_property_util::SetSSID(service_path, &properties);
531 properties.SetStringWithoutPathExpansion(shill::kNameProperty,
532 service_path);
533 properties.SetStringWithoutPathExpansion(shill::kGuidProperty,
534 service_path);
535 properties.SetStringWithoutPathExpansion(shill::kTypeProperty, type);
536 properties.SetStringWithoutPathExpansion(shill::kStateProperty,
537 shill::kStateIdle);
538 properties.SetStringWithoutPathExpansion(
539 shill::kProfileProperty, NetworkProfileHandler::GetSharedProfilePath());
541 network_configuration_handler_->CreateShillConfiguration(
542 properties, NetworkConfigurationObserver::SOURCE_USER_ACTION,
543 base::Bind(
544 &NetworkConfigurationHandlerStubTest::CreateConfigurationCallback,
545 base::Unretained(this)),
546 base::Bind(&ErrorCallback, false, service_path));
547 message_loop_.RunUntilIdle();
550 protected:
551 bool GetServiceStringProperty(const std::string& service_path,
552 const std::string& key,
553 std::string* result) {
554 ShillServiceClient::TestInterface* service_test =
555 DBusThreadManager::Get()->GetShillServiceClient()->GetTestInterface();
556 const base::DictionaryValue* properties =
557 service_test->GetServiceProperties(service_path);
558 if (properties && properties->GetStringWithoutPathExpansion(key, result))
559 return true;
560 return false;
563 bool GetReceivedStringProperty(const std::string& service_path,
564 const std::string& key,
565 std::string* result) {
566 if (get_properties_path_ != service_path)
567 return false;
568 if (get_properties_ &&
569 get_properties_->GetStringWithoutPathExpansion(key, result))
570 return true;
571 return false;
574 scoped_ptr<NetworkStateHandler> network_state_handler_;
575 scoped_ptr<NetworkConfigurationHandler> network_configuration_handler_;
576 scoped_ptr<TestObserver> test_observer_;
577 base::MessageLoopForUI message_loop_;
578 std::string success_callback_name_;
579 std::string get_properties_path_;
580 scoped_ptr<base::DictionaryValue> get_properties_;
581 std::string create_service_path_;
584 TEST_F(NetworkConfigurationHandlerStubTest, StubSetAndClearProperties) {
585 // TODO(stevenjb): Remove dependency on default Stub service.
586 const std::string service_path("/service/wifi1");
587 const std::string test_identity("test_identity");
588 const std::string test_passphrase("test_passphrase");
590 // Set Properties
591 base::DictionaryValue properties_to_set;
592 properties_to_set.SetStringWithoutPathExpansion(shill::kIdentityProperty,
593 test_identity);
594 properties_to_set.SetStringWithoutPathExpansion(shill::kPassphraseProperty,
595 test_passphrase);
596 network_configuration_handler_->SetShillProperties(
597 service_path, properties_to_set,
598 NetworkConfigurationObserver::SOURCE_USER_ACTION,
599 base::Bind(&NetworkConfigurationHandlerStubTest::SuccessCallback,
600 base::Unretained(this), "SetProperties"),
601 base::Bind(&ErrorCallback, false, service_path));
602 message_loop_.RunUntilIdle();
604 EXPECT_EQ("SetProperties", success_callback_name_);
605 std::string identity, passphrase;
606 EXPECT_TRUE(GetServiceStringProperty(service_path, shill::kIdentityProperty,
607 &identity));
608 EXPECT_TRUE(GetServiceStringProperty(service_path, shill::kPassphraseProperty,
609 &passphrase));
610 EXPECT_EQ(test_identity, identity);
611 EXPECT_EQ(test_passphrase, passphrase);
612 EXPECT_EQ(1, test_observer_->PropertyUpdatesForService(service_path));
614 // Clear Properties
615 std::vector<std::string> properties_to_clear;
616 properties_to_clear.push_back(shill::kIdentityProperty);
617 properties_to_clear.push_back(shill::kPassphraseProperty);
618 network_configuration_handler_->ClearShillProperties(
619 service_path, properties_to_clear,
620 base::Bind(&NetworkConfigurationHandlerStubTest::SuccessCallback,
621 base::Unretained(this), "ClearProperties"),
622 base::Bind(&ErrorCallback, false, service_path));
623 message_loop_.RunUntilIdle();
625 EXPECT_EQ("ClearProperties", success_callback_name_);
626 EXPECT_FALSE(GetServiceStringProperty(service_path, shill::kIdentityProperty,
627 &identity));
628 EXPECT_FALSE(GetServiceStringProperty(service_path, shill::kIdentityProperty,
629 &passphrase));
630 EXPECT_EQ(2, test_observer_->PropertyUpdatesForService(service_path));
633 TEST_F(NetworkConfigurationHandlerStubTest, StubGetNameFromWifiHex) {
634 // TODO(stevenjb): Remove dependency on default Stub service.
635 const std::string service_path("/service/wifi1");
636 std::string wifi_hex = "5468697320697320484558205353494421";
637 std::string expected_name = "This is HEX SSID!";
639 // Set Properties
640 base::DictionaryValue properties_to_set;
641 properties_to_set.SetStringWithoutPathExpansion(shill::kWifiHexSsid,
642 wifi_hex);
643 network_configuration_handler_->SetShillProperties(
644 service_path, properties_to_set,
645 NetworkConfigurationObserver::SOURCE_USER_ACTION,
646 base::Bind(&base::DoNothing),
647 base::Bind(&ErrorCallback, false, service_path));
648 message_loop_.RunUntilIdle();
649 std::string wifi_hex_result;
650 EXPECT_TRUE(GetServiceStringProperty(service_path, shill::kWifiHexSsid,
651 &wifi_hex_result));
652 EXPECT_EQ(wifi_hex, wifi_hex_result);
654 // Get Properties
655 network_configuration_handler_->GetShillProperties(
656 service_path,
657 base::Bind(&NetworkConfigurationHandlerStubTest::GetPropertiesCallback,
658 base::Unretained(this)),
659 base::Bind(&ErrorCallback, false, service_path));
660 message_loop_.RunUntilIdle();
662 EXPECT_EQ(service_path, get_properties_path_);
663 std::string name_result;
664 EXPECT_TRUE(GetReceivedStringProperty(service_path, shill::kNameProperty,
665 &name_result));
666 EXPECT_EQ(expected_name, name_result);
669 TEST_F(NetworkConfigurationHandlerStubTest, StubCreateConfiguration) {
670 const std::string service_path("/service/test_wifi");
671 CreateTestConfiguration(service_path, shill::kTypeWifi);
673 EXPECT_FALSE(create_service_path_.empty());
675 std::string guid;
676 EXPECT_TRUE(GetServiceStringProperty(create_service_path_,
677 shill::kGuidProperty, &guid));
678 EXPECT_EQ(service_path, guid);
680 std::string actual_profile;
681 EXPECT_TRUE(GetServiceStringProperty(
682 create_service_path_, shill::kProfileProperty, &actual_profile));
683 EXPECT_EQ(NetworkProfileHandler::GetSharedProfilePath(), actual_profile);
686 TEST_F(NetworkConfigurationHandlerStubTest, NetworkConfigurationObserver) {
687 const std::string service_path("/service/test_wifi");
688 const std::string test_passphrase("test_passphrase");
690 scoped_ptr<TestNetworkConfigurationObserver> test_observer(
691 new TestNetworkConfigurationObserver);
692 network_configuration_handler_->AddObserver(test_observer.get());
693 CreateTestConfiguration(service_path, shill::kTypeWifi);
695 EXPECT_TRUE(test_observer->HasConfiguration(service_path));
696 EXPECT_TRUE(test_observer->HasConfigurationInProfile(
697 service_path, NetworkProfileHandler::GetSharedProfilePath()));
698 EXPECT_EQ(shill::kTypeWifi, test_observer->GetStringProperty(
699 service_path, shill::kTypeProperty));
701 base::DictionaryValue properties_to_set;
702 properties_to_set.SetStringWithoutPathExpansion(shill::kPassphraseProperty,
703 test_passphrase);
704 network_configuration_handler_->SetShillProperties(
705 service_path, properties_to_set,
706 NetworkConfigurationObserver::SOURCE_USER_ACTION,
707 base::Bind(&base::DoNothing),
708 base::Bind(&ErrorCallback, false, service_path));
709 message_loop_.RunUntilIdle();
710 EXPECT_EQ(test_passphrase, test_observer->GetStringProperty(
711 service_path, shill::kPassphraseProperty));
713 std::string user_profile = "/profiles/user1";
714 std::string userhash = "user1";
715 DBusThreadManager::Get()
716 ->GetShillProfileClient()
717 ->GetTestInterface()
718 ->AddProfile(user_profile, userhash);
720 network_configuration_handler_->SetNetworkProfile(
721 service_path, user_profile,
722 NetworkConfigurationObserver::SOURCE_USER_ACTION,
723 base::Bind(&base::DoNothing),
724 base::Bind(&ErrorCallback, false, service_path));
725 message_loop_.RunUntilIdle();
726 EXPECT_TRUE(test_observer->HasConfiguration(service_path));
727 EXPECT_FALSE(test_observer->HasConfigurationInProfile(
728 service_path, NetworkProfileHandler::GetSharedProfilePath()));
729 EXPECT_TRUE(
730 test_observer->HasConfigurationInProfile(service_path, user_profile));
732 network_configuration_handler_->RemoveConfiguration(
733 service_path, NetworkConfigurationObserver::SOURCE_USER_ACTION,
734 base::Bind(&base::DoNothing),
735 base::Bind(&ErrorCallback, false, service_path));
736 message_loop_.RunUntilIdle();
738 EXPECT_FALSE(test_observer->HasConfiguration(service_path));
739 EXPECT_FALSE(test_observer->HasConfigurationInProfile(
740 service_path, NetworkProfileHandler::GetSharedProfilePath()));
741 EXPECT_FALSE(
742 test_observer->HasConfigurationInProfile(service_path, user_profile));
744 network_configuration_handler_->RemoveObserver(test_observer.get());
747 } // namespace chromeos