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. 4 5 #include "base/bind.h" 6 #include "base/values.h" 7 #include "chromeos/dbus/shill_client_unittest_base.h" 8 #include "chromeos/dbus/shill_profile_client.h" 9 #include "dbus/message.h" 10 #include "dbus/object_path.h" 11 #include "dbus/values_util.h" 12 #include "testing/gtest/include/gtest/gtest.h" 13 #include "third_party/cros_system_api/dbus/service_constants.h" 14 15 using testing::_; 16 17 namespace chromeos { 18 19 namespace { 20 21 const char kDefaultProfilePath[] = "/profile/default"; 22 const char kExampleEntryPath[] = "example_entry_path"; 23 24 void AppendVariantOfArrayOfStrings(dbus::MessageWriter* writer, 25 const std::vector<std::string>& strings) { 26 dbus::MessageWriter variant_writer(NULL); 27 writer->OpenVariant("as", &variant_writer); 28 variant_writer.AppendArrayOfStrings(strings); 29 writer->CloseContainer(&variant_writer); 30 } 31 32 } // namespace 33 34 class ShillProfileClientTest : public ShillClientUnittestBase { 35 public: 36 ShillProfileClientTest() 37 : ShillClientUnittestBase(shill::kFlimflamProfileInterface, 38 dbus::ObjectPath(kDefaultProfilePath)) { 39 } 40 41 virtual void SetUp() { 42 ShillClientUnittestBase::SetUp(); 43 // Create a client with the mock bus. 44 client_.reset(ShillProfileClient::Create()); 45 client_->Init(mock_bus_.get()); 46 // Run the message loop to run the signal connection result callback. 47 message_loop_.RunUntilIdle(); 48 } 49 50 virtual void TearDown() { 51 ShillClientUnittestBase::TearDown(); 52 } 53 54 protected: 55 scoped_ptr<ShillProfileClient> client_; 56 }; 57 58 TEST_F(ShillProfileClientTest, PropertyChanged) { 59 // Create a signal. 60 dbus::Signal signal(shill::kFlimflamProfileInterface, 61 shill::kMonitorPropertyChanged); 62 dbus::MessageWriter writer(&signal); 63 writer.AppendString(shill::kEntriesProperty); 64 AppendVariantOfArrayOfStrings(&writer, 65 std::vector<std::string>(1, kExampleEntryPath)); 66 67 // Set expectations. 68 base::ListValue value; 69 value.Append(new base::StringValue(kExampleEntryPath)); 70 MockPropertyChangeObserver observer; 71 EXPECT_CALL(observer, 72 OnPropertyChanged( 73 shill::kEntriesProperty, 74 ValueEq(value))).Times(1); 75 76 // Add the observer 77 client_->AddPropertyChangedObserver( 78 dbus::ObjectPath(kDefaultProfilePath), 79 &observer); 80 81 // Run the signal callback. 82 SendPropertyChangedSignal(&signal); 83 84 // Remove the observer. 85 client_->RemovePropertyChangedObserver( 86 dbus::ObjectPath(kDefaultProfilePath), 87 &observer); 88 89 EXPECT_CALL(observer, OnPropertyChanged(_, _)).Times(0); 90 91 // Run the signal callback again and make sure the observer isn't called. 92 SendPropertyChangedSignal(&signal); 93 } 94 95 TEST_F(ShillProfileClientTest, GetProperties) { 96 // Create response. 97 scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty()); 98 dbus::MessageWriter writer(response.get()); 99 dbus::MessageWriter array_writer(NULL); 100 writer.OpenArray("{sv}", &array_writer); 101 dbus::MessageWriter entry_writer(NULL); 102 array_writer.OpenDictEntry(&entry_writer); 103 entry_writer.AppendString(shill::kEntriesProperty); 104 AppendVariantOfArrayOfStrings(&entry_writer, 105 std::vector<std::string>(1, kExampleEntryPath)); 106 array_writer.CloseContainer(&entry_writer); 107 writer.CloseContainer(&array_writer); 108 109 // Create the expected value. 110 base::ListValue* entries = new base::ListValue; 111 entries->Append(new base::StringValue(kExampleEntryPath)); 112 base::DictionaryValue value; 113 value.SetWithoutPathExpansion(shill::kEntriesProperty, entries); 114 // Set expectations. 115 PrepareForMethodCall(shill::kGetPropertiesFunction, 116 base::Bind(&ExpectNoArgument), 117 response.get()); 118 // Call method. 119 MockErrorCallback error_callback; 120 client_->GetProperties(dbus::ObjectPath(kDefaultProfilePath), 121 base::Bind(&ExpectDictionaryValueResultWithoutStatus, 122 &value), 123 error_callback.GetCallback()); 124 EXPECT_CALL(error_callback, Run(_, _)).Times(0); 125 126 // Run the message loop. 127 message_loop_.RunUntilIdle(); 128 } 129 130 TEST_F(ShillProfileClientTest, GetEntry) { 131 // Create response. 132 scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty()); 133 dbus::MessageWriter writer(response.get()); 134 dbus::MessageWriter array_writer(NULL); 135 writer.OpenArray("{sv}", &array_writer); 136 dbus::MessageWriter entry_writer(NULL); 137 array_writer.OpenDictEntry(&entry_writer); 138 entry_writer.AppendString(shill::kTypeProperty); 139 entry_writer.AppendVariantOfString(shill::kTypeWifi); 140 array_writer.CloseContainer(&entry_writer); 141 writer.CloseContainer(&array_writer); 142 143 // Create the expected value. 144 base::DictionaryValue value; 145 value.SetWithoutPathExpansion(shill::kTypeProperty, 146 new base::StringValue(shill::kTypeWifi)); 147 // Set expectations. 148 PrepareForMethodCall(shill::kGetEntryFunction, 149 base::Bind(&ExpectStringArgument, kExampleEntryPath), 150 response.get()); 151 // Call method. 152 MockErrorCallback error_callback; 153 client_->GetEntry(dbus::ObjectPath(kDefaultProfilePath), 154 kExampleEntryPath, 155 base::Bind(&ExpectDictionaryValueResultWithoutStatus, 156 &value), 157 error_callback.GetCallback()); 158 EXPECT_CALL(error_callback, Run(_, _)).Times(0); 159 // Run the message loop. 160 message_loop_.RunUntilIdle(); 161 } 162 163 TEST_F(ShillProfileClientTest, DeleteEntry) { 164 // Create response. 165 scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty()); 166 167 // Create the expected value. 168 base::DictionaryValue value; 169 value.SetWithoutPathExpansion(shill::kOfflineModeProperty, 170 new base::FundamentalValue(true)); 171 // Set expectations. 172 PrepareForMethodCall(shill::kDeleteEntryFunction, 173 base::Bind(&ExpectStringArgument, kExampleEntryPath), 174 response.get()); 175 // Call method. 176 MockClosure mock_closure; 177 MockErrorCallback mock_error_callback; 178 client_->DeleteEntry(dbus::ObjectPath(kDefaultProfilePath), 179 kExampleEntryPath, 180 mock_closure.GetCallback(), 181 mock_error_callback.GetCallback()); 182 EXPECT_CALL(mock_closure, Run()).Times(1); 183 EXPECT_CALL(mock_error_callback, Run(_, _)).Times(0); 184 185 // Run the message loop. 186 message_loop_.RunUntilIdle(); 187 } 188 189 } // namespace chromeos 190