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(flimflam::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(REAL_DBUS_CLIENT_IMPLEMENTATION, 45 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(flimflam::kFlimflamProfileInterface, 61 flimflam::kMonitorPropertyChanged); 62 dbus::MessageWriter writer(&signal); 63 writer.AppendString(flimflam::kEntriesProperty); 64 AppendVariantOfArrayOfStrings(&writer, 65 std::vector<std::string>(1, kExampleEntryPath)); 66 67 // Set expectations. 68 base::ListValue value; 69 value.Append(base::Value::CreateStringValue(kExampleEntryPath)); 70 MockPropertyChangeObserver observer; 71 EXPECT_CALL(observer, 72 OnPropertyChanged( 73 flimflam::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(flimflam::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(base::Value::CreateStringValue(kExampleEntryPath)); 112 base::DictionaryValue value; 113 value.SetWithoutPathExpansion(flimflam::kEntriesProperty, entries); 114 // Set expectations. 115 PrepareForMethodCall(flimflam::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(flimflam::kTypeProperty); 139 entry_writer.AppendVariantOfString(flimflam::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( 146 flimflam::kTypeProperty, 147 base::Value::CreateStringValue(flimflam::kTypeWifi)); 148 // Set expectations. 149 PrepareForMethodCall(flimflam::kGetEntryFunction, 150 base::Bind(&ExpectStringArgument, kExampleEntryPath), 151 response.get()); 152 // Call method. 153 MockErrorCallback error_callback; 154 client_->GetEntry(dbus::ObjectPath(kDefaultProfilePath), 155 kExampleEntryPath, 156 base::Bind(&ExpectDictionaryValueResultWithoutStatus, 157 &value), 158 error_callback.GetCallback()); 159 EXPECT_CALL(error_callback, Run(_, _)).Times(0); 160 // Run the message loop. 161 message_loop_.RunUntilIdle(); 162 } 163 164 TEST_F(ShillProfileClientTest, DeleteEntry) { 165 // Create response. 166 scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty()); 167 168 // Create the expected value. 169 base::DictionaryValue value; 170 value.SetWithoutPathExpansion(flimflam::kOfflineModeProperty, 171 base::Value::CreateBooleanValue(true)); 172 // Set expectations. 173 PrepareForMethodCall(flimflam::kDeleteEntryFunction, 174 base::Bind(&ExpectStringArgument, kExampleEntryPath), 175 response.get()); 176 // Call method. 177 MockClosure mock_closure; 178 MockErrorCallback mock_error_callback; 179 client_->DeleteEntry(dbus::ObjectPath(kDefaultProfilePath), 180 kExampleEntryPath, 181 mock_closure.GetCallback(), 182 mock_error_callback.GetCallback()); 183 EXPECT_CALL(mock_closure, Run()).Times(1); 184 EXPECT_CALL(mock_error_callback, Run(_, _)).Times(0); 185 186 // Run the message loop. 187 message_loop_.RunUntilIdle(); 188 } 189 190 } // namespace chromeos 191