| OLD | NEW |
| (Empty) |
| 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/flimflam_client_unittest_base.h" | |
| 8 #include "chromeos/dbus/flimflam_manager_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 namespace chromeos { | |
| 16 | |
| 17 namespace { | |
| 18 | |
| 19 // Pops a string-to-string dictionary from the reader. | |
| 20 base::DictionaryValue* PopStringToStringDictionary( | |
| 21 dbus::MessageReader* reader) { | |
| 22 dbus::MessageReader array_reader(NULL); | |
| 23 if (!reader->PopArray(&array_reader)) | |
| 24 return NULL; | |
| 25 scoped_ptr<base::DictionaryValue> result(new base::DictionaryValue); | |
| 26 while (array_reader.HasMoreData()) { | |
| 27 dbus::MessageReader entry_reader(NULL); | |
| 28 std::string key; | |
| 29 std::string value; | |
| 30 if (!array_reader.PopDictEntry(&entry_reader) || | |
| 31 !entry_reader.PopString(&key) || | |
| 32 !entry_reader.PopString(&value)) | |
| 33 return NULL; | |
| 34 result->SetWithoutPathExpansion(key, | |
| 35 base::Value::CreateStringValue(value)); | |
| 36 } | |
| 37 return result.release(); | |
| 38 } | |
| 39 | |
| 40 // Expects the reader to have a string-to-variant dictionary. | |
| 41 void ExpectDictionaryValueArgument( | |
| 42 const base::DictionaryValue* expected_dictionary, | |
| 43 dbus::MessageReader* reader) { | |
| 44 dbus::MessageReader array_reader(NULL); | |
| 45 ASSERT_TRUE(reader->PopArray(&array_reader)); | |
| 46 while (array_reader.HasMoreData()) { | |
| 47 dbus::MessageReader entry_reader(NULL); | |
| 48 ASSERT_TRUE(array_reader.PopDictEntry(&entry_reader)); | |
| 49 std::string key; | |
| 50 ASSERT_TRUE(entry_reader.PopString(&key)); | |
| 51 dbus::MessageReader variant_reader(NULL); | |
| 52 ASSERT_TRUE(entry_reader.PopVariant(&variant_reader)); | |
| 53 scoped_ptr<base::Value> value; | |
| 54 // Variants in the dictionary can be basic types or string-to-string | |
| 55 // dictinoary. | |
| 56 switch (variant_reader.GetDataType()) { | |
| 57 case dbus::Message::ARRAY: | |
| 58 value.reset(PopStringToStringDictionary(&variant_reader)); | |
| 59 break; | |
| 60 case dbus::Message::BOOL: | |
| 61 case dbus::Message::INT32: | |
| 62 case dbus::Message::STRING: | |
| 63 value.reset(dbus::PopDataAsValue(&variant_reader)); | |
| 64 break; | |
| 65 default: | |
| 66 NOTREACHED(); | |
| 67 } | |
| 68 ASSERT_TRUE(value.get()); | |
| 69 const base::Value* expected_value = NULL; | |
| 70 EXPECT_TRUE(expected_dictionary->GetWithoutPathExpansion(key, | |
| 71 &expected_value)); | |
| 72 EXPECT_TRUE(value->Equals(expected_value)); | |
| 73 } | |
| 74 } | |
| 75 | |
| 76 // Creates a DictionaryValue with example properties. | |
| 77 base::DictionaryValue* CreateExampleProperties() { | |
| 78 base::DictionaryValue* properties = new base::DictionaryValue; | |
| 79 properties->SetWithoutPathExpansion( | |
| 80 flimflam::kGuidProperty, | |
| 81 base::Value::CreateStringValue("00000000-0000-0000-0000-000000000000")); | |
| 82 properties->SetWithoutPathExpansion( | |
| 83 flimflam::kModeProperty, | |
| 84 base::Value::CreateStringValue(flimflam::kModeManaged)); | |
| 85 properties->SetWithoutPathExpansion( | |
| 86 flimflam::kTypeProperty, | |
| 87 base::Value::CreateStringValue(flimflam::kTypeWifi)); | |
| 88 properties->SetWithoutPathExpansion( | |
| 89 flimflam::kSSIDProperty, | |
| 90 base::Value::CreateStringValue("testssid")); | |
| 91 properties->SetWithoutPathExpansion( | |
| 92 flimflam::kSecurityProperty, | |
| 93 base::Value::CreateStringValue(flimflam::kSecurityPsk)); | |
| 94 return properties; | |
| 95 } | |
| 96 | |
| 97 } // namespace | |
| 98 | |
| 99 class FlimflamManagerClientTest : public FlimflamClientUnittestBase { | |
| 100 public: | |
| 101 FlimflamManagerClientTest() | |
| 102 : FlimflamClientUnittestBase( | |
| 103 flimflam::kFlimflamManagerInterface, | |
| 104 dbus::ObjectPath(flimflam::kFlimflamServicePath)) { | |
| 105 } | |
| 106 | |
| 107 virtual void SetUp() { | |
| 108 FlimflamClientUnittestBase::SetUp(); | |
| 109 // Create a client with the mock bus. | |
| 110 client_.reset(FlimflamManagerClient::Create(REAL_DBUS_CLIENT_IMPLEMENTATION, | |
| 111 mock_bus_)); | |
| 112 // Run the message loop to run the signal connection result callback. | |
| 113 message_loop_.RunAllPending(); | |
| 114 } | |
| 115 | |
| 116 virtual void TearDown() { | |
| 117 FlimflamClientUnittestBase::TearDown(); | |
| 118 } | |
| 119 | |
| 120 protected: | |
| 121 scoped_ptr<FlimflamManagerClient> client_; | |
| 122 }; | |
| 123 | |
| 124 TEST_F(FlimflamManagerClientTest, PropertyChanged) { | |
| 125 // Create a signal. | |
| 126 base::FundamentalValue kOfflineMode(true); | |
| 127 dbus::Signal signal(flimflam::kFlimflamManagerInterface, | |
| 128 flimflam::kMonitorPropertyChanged); | |
| 129 dbus::MessageWriter writer(&signal); | |
| 130 writer.AppendString(flimflam::kOfflineModeProperty); | |
| 131 dbus::AppendBasicTypeValueData(&writer, kOfflineMode); | |
| 132 | |
| 133 // Set expectations. | |
| 134 client_->SetPropertyChangedHandler(base::Bind(&ExpectPropertyChanged, | |
| 135 flimflam::kOfflineModeProperty, | |
| 136 &kOfflineMode)); | |
| 137 // Run the signal callback. | |
| 138 SendPropertyChangedSignal(&signal); | |
| 139 | |
| 140 // Reset the handler. | |
| 141 client_->ResetPropertyChangedHandler(); | |
| 142 } | |
| 143 | |
| 144 TEST_F(FlimflamManagerClientTest, GetProperties) { | |
| 145 // Create response. | |
| 146 scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty()); | |
| 147 dbus::MessageWriter writer(response.get()); | |
| 148 dbus::MessageWriter array_writer(NULL); | |
| 149 writer.OpenArray("{sv}", &array_writer); | |
| 150 dbus::MessageWriter entry_writer(NULL); | |
| 151 array_writer.OpenDictEntry(&entry_writer); | |
| 152 entry_writer.AppendString(flimflam::kOfflineModeProperty); | |
| 153 entry_writer.AppendVariantOfBool(true); | |
| 154 array_writer.CloseContainer(&entry_writer); | |
| 155 writer.CloseContainer(&array_writer); | |
| 156 | |
| 157 // Create the expected value. | |
| 158 base::DictionaryValue value; | |
| 159 value.SetWithoutPathExpansion(flimflam::kOfflineModeProperty, | |
| 160 base::Value::CreateBooleanValue(true)); | |
| 161 // Set expectations. | |
| 162 PrepareForMethodCall(flimflam::kGetPropertiesFunction, | |
| 163 base::Bind(&ExpectNoArgument), | |
| 164 response.get()); | |
| 165 // Call method. | |
| 166 client_->GetProperties(base::Bind(&ExpectDictionaryValueResult, | |
| 167 &value)); | |
| 168 // Run the message loop. | |
| 169 message_loop_.RunAllPending(); | |
| 170 } | |
| 171 | |
| 172 TEST_F(FlimflamManagerClientTest, CallGetPropertiesAndBlock) { | |
| 173 // Create response. | |
| 174 scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty()); | |
| 175 dbus::MessageWriter writer(response.get()); | |
| 176 dbus::MessageWriter array_writer(NULL); | |
| 177 writer.OpenArray("{sv}", &array_writer); | |
| 178 dbus::MessageWriter entry_writer(NULL); | |
| 179 array_writer.OpenDictEntry(&entry_writer); | |
| 180 entry_writer.AppendString(flimflam::kOfflineModeProperty); | |
| 181 entry_writer.AppendVariantOfBool(true); | |
| 182 array_writer.CloseContainer(&entry_writer); | |
| 183 writer.CloseContainer(&array_writer); | |
| 184 | |
| 185 // Create the expected value. | |
| 186 base::DictionaryValue value; | |
| 187 value.SetWithoutPathExpansion(flimflam::kOfflineModeProperty, | |
| 188 base::Value::CreateBooleanValue(true)); | |
| 189 // Set expectations. | |
| 190 PrepareForMethodCall(flimflam::kGetPropertiesFunction, | |
| 191 base::Bind(&ExpectNoArgument), | |
| 192 response.get()); | |
| 193 // Call method. | |
| 194 scoped_ptr<base::DictionaryValue> result( | |
| 195 client_->CallGetPropertiesAndBlock()); | |
| 196 EXPECT_TRUE(value.Equals(result.get())); | |
| 197 } | |
| 198 | |
| 199 TEST_F(FlimflamManagerClientTest, SetProperty) { | |
| 200 // Create response. | |
| 201 scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty()); | |
| 202 // Set expectations. | |
| 203 base::StringValue value("portal list"); | |
| 204 PrepareForMethodCall(flimflam::kSetPropertyFunction, | |
| 205 base::Bind(ExpectStringAndValueArguments, | |
| 206 flimflam::kCheckPortalListProperty, | |
| 207 &value), | |
| 208 response.get()); | |
| 209 // Call method. | |
| 210 client_->SetProperty(flimflam::kCheckPortalListProperty, | |
| 211 value, | |
| 212 base::Bind(&ExpectNoResultValue)); | |
| 213 // Run the message loop. | |
| 214 message_loop_.RunAllPending(); | |
| 215 } | |
| 216 | |
| 217 TEST_F(FlimflamManagerClientTest, RequestScan) { | |
| 218 // Create response. | |
| 219 scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty()); | |
| 220 // Set expectations. | |
| 221 PrepareForMethodCall(flimflam::kRequestScanFunction, | |
| 222 base::Bind(&ExpectStringArgument, flimflam::kTypeWifi), | |
| 223 response.get()); | |
| 224 // Call method. | |
| 225 client_->RequestScan(flimflam::kTypeWifi, base::Bind(&ExpectNoResultValue)); | |
| 226 // Run the message loop. | |
| 227 message_loop_.RunAllPending(); | |
| 228 } | |
| 229 | |
| 230 TEST_F(FlimflamManagerClientTest, EnableTechnology) { | |
| 231 // Create response. | |
| 232 scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty()); | |
| 233 // Set expectations. | |
| 234 PrepareForMethodCall(flimflam::kEnableTechnologyFunction, | |
| 235 base::Bind(&ExpectStringArgument, flimflam::kTypeWifi), | |
| 236 response.get()); | |
| 237 // Call method. | |
| 238 client_->EnableTechnology(flimflam::kTypeWifi, | |
| 239 base::Bind(&ExpectNoResultValue)); | |
| 240 // Run the message loop. | |
| 241 message_loop_.RunAllPending(); | |
| 242 } | |
| 243 | |
| 244 TEST_F(FlimflamManagerClientTest, DisableTechnology) { | |
| 245 // Create response. | |
| 246 scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty()); | |
| 247 // Set expectations. | |
| 248 PrepareForMethodCall(flimflam::kDisableTechnologyFunction, | |
| 249 base::Bind(&ExpectStringArgument, flimflam::kTypeWifi), | |
| 250 response.get()); | |
| 251 // Call method. | |
| 252 client_->DisableTechnology(flimflam::kTypeWifi, | |
| 253 base::Bind(&ExpectNoResultValue)); | |
| 254 // Run the message loop. | |
| 255 message_loop_.RunAllPending(); | |
| 256 } | |
| 257 | |
| 258 TEST_F(FlimflamManagerClientTest, ConfigureService) { | |
| 259 // Create response. | |
| 260 scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty()); | |
| 261 // Create the argument dictionary. | |
| 262 scoped_ptr<base::DictionaryValue> arg(CreateExampleProperties()); | |
| 263 // Set expectations. | |
| 264 PrepareForMethodCall(flimflam::kConfigureServiceFunction, | |
| 265 base::Bind(&ExpectDictionaryValueArgument, arg.get()), | |
| 266 response.get()); | |
| 267 // Call method. | |
| 268 client_->ConfigureService(*arg, base::Bind(&ExpectNoResultValue)); | |
| 269 // Run the message loop. | |
| 270 message_loop_.RunAllPending(); | |
| 271 } | |
| 272 | |
| 273 TEST_F(FlimflamManagerClientTest, GetService) { | |
| 274 // Create response. | |
| 275 const dbus::ObjectPath object_path("/"); | |
| 276 scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty()); | |
| 277 dbus::MessageWriter writer(response.get()); | |
| 278 writer.AppendObjectPath(object_path); | |
| 279 // Create the argument dictionary. | |
| 280 scoped_ptr<base::DictionaryValue> arg(CreateExampleProperties()); | |
| 281 // Set expectations. | |
| 282 PrepareForMethodCall(flimflam::kGetServiceFunction, | |
| 283 base::Bind(&ExpectDictionaryValueArgument, arg.get()), | |
| 284 response.get()); | |
| 285 // Call method. | |
| 286 client_->GetService(*arg, base::Bind(&ExpectObjectPathResult, object_path)); | |
| 287 // Run the message loop. | |
| 288 message_loop_.RunAllPending(); | |
| 289 } | |
| 290 | |
| 291 } // namespace chromeos | |
| OLD | NEW |