[Telemetry] Rename test methods in page_test_results_unittest to follow existing...
[chromium-blink-merge.git] / chromeos / network / network_configuration_handler_unittest.cc
blob1b5b3bb59df9cb593838e743788356ccac51e217
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 protected:
281 MockShillManagerClient* mock_manager_client_;
282 MockShillProfileClient* mock_profile_client_;
283 MockShillServiceClient* mock_service_client_;
284 scoped_ptr<NetworkStateHandler> network_state_handler_;
285 scoped_ptr<NetworkConfigurationHandler> network_configuration_handler_;
286 base::MessageLoopForUI message_loop_;
287 base::DictionaryValue* dictionary_value_result_;
290 TEST_F(NetworkConfigurationHandlerTest, GetProperties) {
291 std::string service_path = "/service/1";
292 std::string expected_json = "{\n \"SSID\": \"MyNetwork\"\n}\n";
293 std::string networkName = "MyNetwork";
294 std::string key = "SSID";
295 scoped_ptr<base::StringValue> networkNameValue(
296 new base::StringValue(networkName));
298 base::DictionaryValue value;
299 value.Set(key, new base::StringValue(networkName));
300 dictionary_value_result_ = &value;
301 EXPECT_CALL(*mock_service_client_,
302 SetProperty(dbus::ObjectPath(service_path), key,
303 IsEqualTo(networkNameValue.get()), _, _)).Times(1);
304 mock_service_client_->SetProperty(
305 dbus::ObjectPath(service_path), key, *networkNameValue,
306 base::Bind(&base::DoNothing), base::Bind(&DBusErrorCallback));
307 message_loop_.RunUntilIdle();
309 ShillServiceClient::DictionaryValueCallback get_properties_callback;
310 EXPECT_CALL(*mock_service_client_, GetProperties(_, _))
311 .WillOnce(
312 Invoke(this, &NetworkConfigurationHandlerTest::OnGetProperties));
313 network_configuration_handler_->GetProperties(
314 service_path,
315 base::Bind(&DictionaryValueCallback, service_path, expected_json),
316 base::Bind(&ErrorCallback, false, service_path));
317 message_loop_.RunUntilIdle();
320 TEST_F(NetworkConfigurationHandlerTest, SetProperties) {
321 std::string service_path = "/service/1";
322 std::string networkName = "MyNetwork";
323 std::string key = "SSID";
324 scoped_ptr<base::StringValue> networkNameValue(
325 new base::StringValue(networkName));
327 base::DictionaryValue value;
328 value.Set(key, new base::StringValue(networkName));
329 dictionary_value_result_ = &value;
330 EXPECT_CALL(*mock_service_client_, SetProperties(_, _, _, _))
331 .WillOnce(
332 Invoke(this, &NetworkConfigurationHandlerTest::OnSetProperties));
333 network_configuration_handler_->SetProperties(
334 service_path, value, NetworkConfigurationObserver::SOURCE_USER_ACTION,
335 base::Bind(&base::DoNothing),
336 base::Bind(&ErrorCallback, false, service_path));
337 message_loop_.RunUntilIdle();
340 TEST_F(NetworkConfigurationHandlerTest, ClearProperties) {
341 std::string service_path = "/service/1";
342 std::string networkName = "MyNetwork";
343 std::string key = "SSID";
344 scoped_ptr<base::StringValue> networkNameValue(
345 new base::StringValue(networkName));
347 // First set up a value to clear.
348 base::DictionaryValue value;
349 value.Set(key, new base::StringValue(networkName));
350 dictionary_value_result_ = &value;
351 EXPECT_CALL(*mock_service_client_, SetProperties(_, _, _, _))
352 .WillOnce(
353 Invoke(this, &NetworkConfigurationHandlerTest::OnSetProperties));
354 network_configuration_handler_->SetProperties(
355 service_path, value, NetworkConfigurationObserver::SOURCE_USER_ACTION,
356 base::Bind(&base::DoNothing),
357 base::Bind(&ErrorCallback, false, service_path));
358 message_loop_.RunUntilIdle();
360 // Now clear it.
361 std::vector<std::string> values_to_clear;
362 values_to_clear.push_back(key);
363 EXPECT_CALL(*mock_service_client_, ClearProperties(_, _, _, _))
364 .WillOnce(
365 Invoke(this, &NetworkConfigurationHandlerTest::OnClearProperties));
366 network_configuration_handler_->ClearProperties(
367 service_path, values_to_clear, base::Bind(&base::DoNothing),
368 base::Bind(&ErrorCallback, false, service_path));
369 message_loop_.RunUntilIdle();
372 TEST_F(NetworkConfigurationHandlerTest, ClearPropertiesError) {
373 std::string service_path = "/service/1";
374 std::string networkName = "MyNetwork";
375 std::string key = "SSID";
376 scoped_ptr<base::StringValue> networkNameValue(
377 new base::StringValue(networkName));
379 // First set up a value to clear.
380 base::DictionaryValue value;
381 value.Set(key, new base::StringValue(networkName));
382 dictionary_value_result_ = &value;
383 EXPECT_CALL(*mock_service_client_, SetProperties(_, _, _, _))
384 .WillOnce(
385 Invoke(this, &NetworkConfigurationHandlerTest::OnSetProperties));
386 network_configuration_handler_->SetProperties(
387 service_path, value, NetworkConfigurationObserver::SOURCE_USER_ACTION,
388 base::Bind(&base::DoNothing),
389 base::Bind(&ErrorCallback, false, service_path));
390 message_loop_.RunUntilIdle();
392 // Now clear it.
393 std::vector<std::string> values_to_clear;
394 values_to_clear.push_back(key);
395 EXPECT_CALL(*mock_service_client_, ClearProperties(_, _, _, _))
396 .WillOnce(Invoke(
397 this, &NetworkConfigurationHandlerTest::OnClearPropertiesError));
398 network_configuration_handler_->ClearProperties(
399 service_path, values_to_clear, base::Bind(&base::DoNothing),
400 base::Bind(&ErrorCallback, true, service_path));
401 message_loop_.RunUntilIdle();
404 TEST_F(NetworkConfigurationHandlerTest, CreateConfiguration) {
405 std::string networkName = "MyNetwork";
406 std::string key = "SSID";
407 std::string type = "wifi";
408 std::string profile = "profile path";
409 base::DictionaryValue value;
410 shill_property_util::SetSSID(networkName, &value);
411 value.SetWithoutPathExpansion(shill::kTypeProperty,
412 new base::StringValue(type));
413 value.SetWithoutPathExpansion(shill::kProfileProperty,
414 new base::StringValue(profile));
416 EXPECT_CALL(*mock_manager_client_,
417 ConfigureServiceForProfile(dbus::ObjectPath(profile), _, _, _))
418 .WillOnce(
419 Invoke(this, &NetworkConfigurationHandlerTest::OnConfigureService));
420 network_configuration_handler_->CreateConfiguration(
421 value, NetworkConfigurationObserver::SOURCE_USER_ACTION,
422 base::Bind(&StringResultCallback, std::string("/service/2")),
423 base::Bind(&ErrorCallback, false, std::string()));
424 message_loop_.RunUntilIdle();
427 TEST_F(NetworkConfigurationHandlerTest, RemoveConfiguration) {
428 std::string service_path = "/service/1";
430 TestCallback test_callback;
431 EXPECT_CALL(*mock_service_client_, GetLoadableProfileEntries(_, _))
432 .WillOnce(Invoke(
433 this, &NetworkConfigurationHandlerTest::OnGetLoadableProfileEntries));
434 EXPECT_CALL(*mock_profile_client_, DeleteEntry(_, _, _, _))
435 .WillRepeatedly(
436 Invoke(this, &NetworkConfigurationHandlerTest::OnDeleteEntry));
438 network_configuration_handler_->RemoveConfiguration(
439 service_path, NetworkConfigurationObserver::SOURCE_USER_ACTION,
440 base::Bind(&TestCallback::Run, base::Unretained(&test_callback)),
441 base::Bind(&ErrorCallback, false, service_path));
442 message_loop_.RunUntilIdle();
443 EXPECT_EQ(1, test_callback.run_count());
444 EXPECT_FALSE(PendingProfileEntryDeleterForTest(service_path));
447 ////////////////////////////////////////////////////////////////////////////////
448 // Stub based tests
450 namespace {
452 class TestObserver : public chromeos::NetworkStateHandlerObserver {
453 public:
454 TestObserver() : network_list_changed_count_(0) {}
455 ~TestObserver() override {}
457 void NetworkListChanged() override { ++network_list_changed_count_; }
459 void NetworkPropertiesUpdated(const NetworkState* network) override {
460 property_updates_[network->path()]++;
463 size_t network_list_changed_count() { return network_list_changed_count_; }
465 int PropertyUpdatesForService(const std::string& service_path) {
466 return property_updates_[service_path];
469 void ClearPropertyUpdates() { property_updates_.clear(); }
471 private:
472 size_t network_list_changed_count_;
473 std::map<std::string, int> property_updates_;
475 DISALLOW_COPY_AND_ASSIGN(TestObserver);
478 } // namespace
480 class NetworkConfigurationHandlerStubTest : public testing::Test {
481 public:
482 NetworkConfigurationHandlerStubTest() {}
484 ~NetworkConfigurationHandlerStubTest() override {}
486 void SetUp() override {
487 DBusThreadManager::Initialize();
489 network_state_handler_.reset(NetworkStateHandler::InitializeForTest());
490 test_observer_.reset(new TestObserver());
491 network_state_handler_->AddObserver(test_observer_.get(), FROM_HERE);
493 network_configuration_handler_.reset(new NetworkConfigurationHandler());
494 network_configuration_handler_->Init(network_state_handler_.get(),
495 NULL /* network_device_handler */);
497 message_loop_.RunUntilIdle();
498 test_observer_->ClearPropertyUpdates();
501 void TearDown() override {
502 network_configuration_handler_.reset();
503 network_state_handler_->RemoveObserver(test_observer_.get(), FROM_HERE);
504 network_state_handler_.reset();
505 DBusThreadManager::Shutdown();
508 void SuccessCallback(const std::string& callback_name) {
509 success_callback_name_ = callback_name;
512 void GetPropertiesCallback(const std::string& service_path,
513 const base::DictionaryValue& dictionary) {
514 get_properties_path_ = service_path;
515 get_properties_.reset(dictionary.DeepCopy());
518 void CreateConfigurationCallback(const std::string& service_path) {
519 create_service_path_ = service_path;
522 void CreateTestConfiguration(const std::string& service_path,
523 const std::string& type) {
524 base::DictionaryValue properties;
525 shill_property_util::SetSSID(service_path, &properties);
526 properties.SetStringWithoutPathExpansion(shill::kNameProperty,
527 service_path);
528 properties.SetStringWithoutPathExpansion(shill::kGuidProperty,
529 service_path);
530 properties.SetStringWithoutPathExpansion(shill::kTypeProperty, type);
531 properties.SetStringWithoutPathExpansion(shill::kStateProperty,
532 shill::kStateIdle);
533 properties.SetStringWithoutPathExpansion(
534 shill::kProfileProperty, NetworkProfileHandler::GetSharedProfilePath());
536 network_configuration_handler_->CreateConfiguration(
537 properties, NetworkConfigurationObserver::SOURCE_USER_ACTION,
538 base::Bind(
539 &NetworkConfigurationHandlerStubTest::CreateConfigurationCallback,
540 base::Unretained(this)),
541 base::Bind(&ErrorCallback, false, service_path));
542 message_loop_.RunUntilIdle();
545 protected:
546 bool GetServiceStringProperty(const std::string& service_path,
547 const std::string& key,
548 std::string* result) {
549 ShillServiceClient::TestInterface* service_test =
550 DBusThreadManager::Get()->GetShillServiceClient()->GetTestInterface();
551 const base::DictionaryValue* properties =
552 service_test->GetServiceProperties(service_path);
553 if (properties && properties->GetStringWithoutPathExpansion(key, result))
554 return true;
555 return false;
558 bool GetReceivedStringProperty(const std::string& service_path,
559 const std::string& key,
560 std::string* result) {
561 if (get_properties_path_ != service_path)
562 return false;
563 if (get_properties_ &&
564 get_properties_->GetStringWithoutPathExpansion(key, result))
565 return true;
566 return false;
569 scoped_ptr<NetworkStateHandler> network_state_handler_;
570 scoped_ptr<NetworkConfigurationHandler> network_configuration_handler_;
571 scoped_ptr<TestObserver> test_observer_;
572 base::MessageLoopForUI message_loop_;
573 std::string success_callback_name_;
574 std::string get_properties_path_;
575 scoped_ptr<base::DictionaryValue> get_properties_;
576 std::string create_service_path_;
579 TEST_F(NetworkConfigurationHandlerStubTest, StubSetAndClearProperties) {
580 // TODO(stevenjb): Remove dependency on default Stub service.
581 const std::string service_path("/service/wifi1");
582 const std::string test_identity("test_identity");
583 const std::string test_passphrase("test_passphrase");
585 // Set Properties
586 base::DictionaryValue properties_to_set;
587 properties_to_set.SetStringWithoutPathExpansion(shill::kIdentityProperty,
588 test_identity);
589 properties_to_set.SetStringWithoutPathExpansion(shill::kPassphraseProperty,
590 test_passphrase);
591 network_configuration_handler_->SetProperties(
592 service_path, properties_to_set,
593 NetworkConfigurationObserver::SOURCE_USER_ACTION,
594 base::Bind(&NetworkConfigurationHandlerStubTest::SuccessCallback,
595 base::Unretained(this), "SetProperties"),
596 base::Bind(&ErrorCallback, false, service_path));
597 message_loop_.RunUntilIdle();
599 EXPECT_EQ("SetProperties", success_callback_name_);
600 std::string identity, passphrase;
601 EXPECT_TRUE(GetServiceStringProperty(service_path, shill::kIdentityProperty,
602 &identity));
603 EXPECT_TRUE(GetServiceStringProperty(service_path, shill::kPassphraseProperty,
604 &passphrase));
605 EXPECT_EQ(test_identity, identity);
606 EXPECT_EQ(test_passphrase, passphrase);
607 EXPECT_EQ(1, test_observer_->PropertyUpdatesForService(service_path));
609 // Clear Properties
610 std::vector<std::string> properties_to_clear;
611 properties_to_clear.push_back(shill::kIdentityProperty);
612 properties_to_clear.push_back(shill::kPassphraseProperty);
613 network_configuration_handler_->ClearProperties(
614 service_path, properties_to_clear,
615 base::Bind(&NetworkConfigurationHandlerStubTest::SuccessCallback,
616 base::Unretained(this), "ClearProperties"),
617 base::Bind(&ErrorCallback, false, service_path));
618 message_loop_.RunUntilIdle();
620 EXPECT_EQ("ClearProperties", success_callback_name_);
621 EXPECT_FALSE(GetServiceStringProperty(service_path, shill::kIdentityProperty,
622 &identity));
623 EXPECT_FALSE(GetServiceStringProperty(service_path, shill::kIdentityProperty,
624 &passphrase));
625 EXPECT_EQ(2, test_observer_->PropertyUpdatesForService(service_path));
628 TEST_F(NetworkConfigurationHandlerStubTest, StubGetNameFromWifiHex) {
629 // TODO(stevenjb): Remove dependency on default Stub service.
630 const std::string service_path("/service/wifi1");
631 std::string wifi_hex = "5468697320697320484558205353494421";
632 std::string expected_name = "This is HEX SSID!";
634 // Set Properties
635 base::DictionaryValue properties_to_set;
636 properties_to_set.SetStringWithoutPathExpansion(shill::kWifiHexSsid,
637 wifi_hex);
638 network_configuration_handler_->SetProperties(
639 service_path, properties_to_set,
640 NetworkConfigurationObserver::SOURCE_USER_ACTION,
641 base::Bind(&base::DoNothing),
642 base::Bind(&ErrorCallback, false, service_path));
643 message_loop_.RunUntilIdle();
644 std::string wifi_hex_result;
645 EXPECT_TRUE(GetServiceStringProperty(service_path, shill::kWifiHexSsid,
646 &wifi_hex_result));
647 EXPECT_EQ(wifi_hex, wifi_hex_result);
649 // Get Properties
650 network_configuration_handler_->GetProperties(
651 service_path,
652 base::Bind(&NetworkConfigurationHandlerStubTest::GetPropertiesCallback,
653 base::Unretained(this)),
654 base::Bind(&ErrorCallback, false, service_path));
655 message_loop_.RunUntilIdle();
657 EXPECT_EQ(service_path, get_properties_path_);
658 std::string name_result;
659 EXPECT_TRUE(GetReceivedStringProperty(service_path, shill::kNameProperty,
660 &name_result));
661 EXPECT_EQ(expected_name, name_result);
664 TEST_F(NetworkConfigurationHandlerStubTest, StubCreateConfiguration) {
665 const std::string service_path("/service/test_wifi");
666 CreateTestConfiguration(service_path, shill::kTypeWifi);
668 EXPECT_FALSE(create_service_path_.empty());
670 std::string guid;
671 EXPECT_TRUE(GetServiceStringProperty(create_service_path_,
672 shill::kGuidProperty, &guid));
673 EXPECT_EQ(service_path, guid);
675 std::string actual_profile;
676 EXPECT_TRUE(GetServiceStringProperty(
677 create_service_path_, shill::kProfileProperty, &actual_profile));
678 EXPECT_EQ(NetworkProfileHandler::GetSharedProfilePath(), actual_profile);
681 TEST_F(NetworkConfigurationHandlerStubTest, NetworkConfigurationObserver) {
682 const std::string service_path("/service/test_wifi");
683 const std::string test_passphrase("test_passphrase");
685 scoped_ptr<TestNetworkConfigurationObserver> test_observer(
686 new TestNetworkConfigurationObserver);
687 network_configuration_handler_->AddObserver(test_observer.get());
688 CreateTestConfiguration(service_path, shill::kTypeWifi);
690 EXPECT_TRUE(test_observer->HasConfiguration(service_path));
691 EXPECT_TRUE(test_observer->HasConfigurationInProfile(
692 service_path, NetworkProfileHandler::GetSharedProfilePath()));
693 EXPECT_EQ(shill::kTypeWifi, test_observer->GetStringProperty(
694 service_path, shill::kTypeProperty));
696 base::DictionaryValue properties_to_set;
697 properties_to_set.SetStringWithoutPathExpansion(shill::kPassphraseProperty,
698 test_passphrase);
699 network_configuration_handler_->SetProperties(
700 service_path, properties_to_set,
701 NetworkConfigurationObserver::SOURCE_USER_ACTION,
702 base::Bind(&base::DoNothing),
703 base::Bind(&ErrorCallback, false, service_path));
704 message_loop_.RunUntilIdle();
705 EXPECT_EQ(test_passphrase, test_observer->GetStringProperty(
706 service_path, shill::kPassphraseProperty));
708 std::string user_profile = "/profiles/user1";
709 std::string userhash = "user1";
710 DBusThreadManager::Get()
711 ->GetShillProfileClient()
712 ->GetTestInterface()
713 ->AddProfile(user_profile, userhash);
715 network_configuration_handler_->SetNetworkProfile(
716 service_path, user_profile,
717 NetworkConfigurationObserver::SOURCE_USER_ACTION,
718 base::Bind(&base::DoNothing),
719 base::Bind(&ErrorCallback, false, service_path));
720 message_loop_.RunUntilIdle();
721 EXPECT_TRUE(test_observer->HasConfiguration(service_path));
722 EXPECT_FALSE(test_observer->HasConfigurationInProfile(
723 service_path, NetworkProfileHandler::GetSharedProfilePath()));
724 EXPECT_TRUE(
725 test_observer->HasConfigurationInProfile(service_path, user_profile));
727 network_configuration_handler_->RemoveConfiguration(
728 service_path, NetworkConfigurationObserver::SOURCE_USER_ACTION,
729 base::Bind(&base::DoNothing),
730 base::Bind(&ErrorCallback, false, service_path));
731 message_loop_.RunUntilIdle();
733 EXPECT_FALSE(test_observer->HasConfiguration(service_path));
734 EXPECT_FALSE(test_observer->HasConfigurationInProfile(
735 service_path, NetworkProfileHandler::GetSharedProfilePath()));
736 EXPECT_FALSE(
737 test_observer->HasConfigurationInProfile(service_path, user_profile));
739 network_configuration_handler_->RemoveObserver(test_observer.get());
742 } // namespace chromeos