| 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_network_client.h" | |
| 9 #include "dbus/message.h" | |
| 10 #include "dbus/values_util.h" | |
| 11 #include "testing/gtest/include/gtest/gtest.h" | |
| 12 #include "third_party/cros_system_api/dbus/service_constants.h" | |
| 13 | |
| 14 namespace chromeos { | |
| 15 | |
| 16 namespace { | |
| 17 | |
| 18 const char kExampleNetworkPath[] = "/foo/bar"; | |
| 19 | |
| 20 } // namespace | |
| 21 | |
| 22 class FlimflamNetworkClientTest : public FlimflamClientUnittestBase { | |
| 23 public: | |
| 24 FlimflamNetworkClientTest() | |
| 25 : FlimflamClientUnittestBase( | |
| 26 flimflam::kFlimflamNetworkInterface, | |
| 27 dbus::ObjectPath(kExampleNetworkPath)) { | |
| 28 } | |
| 29 | |
| 30 virtual void SetUp() { | |
| 31 FlimflamClientUnittestBase::SetUp(); | |
| 32 // Create a client with the mock bus. | |
| 33 client_.reset(FlimflamNetworkClient::Create(REAL_DBUS_CLIENT_IMPLEMENTATION, | |
| 34 mock_bus_)); | |
| 35 // Run the message loop to run the signal connection result callback. | |
| 36 message_loop_.RunAllPending(); | |
| 37 } | |
| 38 | |
| 39 virtual void TearDown() { | |
| 40 FlimflamClientUnittestBase::TearDown(); | |
| 41 } | |
| 42 | |
| 43 protected: | |
| 44 scoped_ptr<FlimflamNetworkClient> client_; | |
| 45 }; | |
| 46 | |
| 47 TEST_F(FlimflamNetworkClientTest, PropertyChanged) { | |
| 48 // Create a signal. | |
| 49 const base::FundamentalValue kConnected(true); | |
| 50 dbus::Signal signal(flimflam::kFlimflamNetworkInterface, | |
| 51 flimflam::kMonitorPropertyChanged); | |
| 52 dbus::MessageWriter writer(&signal); | |
| 53 writer.AppendString(flimflam::kConnectedProperty); | |
| 54 dbus::AppendBasicTypeValueDataAsVariant(&writer, kConnected); | |
| 55 | |
| 56 // Set expectations. | |
| 57 client_->SetPropertyChangedHandler(dbus::ObjectPath(kExampleNetworkPath), | |
| 58 base::Bind(&ExpectPropertyChanged, | |
| 59 flimflam::kConnectedProperty, | |
| 60 &kConnected)); | |
| 61 // Run the signal callback. | |
| 62 SendPropertyChangedSignal(&signal); | |
| 63 | |
| 64 // Reset the handler. | |
| 65 client_->ResetPropertyChangedHandler(dbus::ObjectPath(kExampleNetworkPath)); | |
| 66 } | |
| 67 | |
| 68 TEST_F(FlimflamNetworkClientTest, GetProperties) { | |
| 69 const char kAddress[] = "address"; | |
| 70 const char kName[] = "name"; | |
| 71 const uint8 kSignalStrength = 1; | |
| 72 const uint32 kWifiChannel = 1; | |
| 73 const bool kConnected = true; | |
| 74 | |
| 75 // Create response. | |
| 76 scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty()); | |
| 77 dbus::MessageWriter writer(response.get()); | |
| 78 dbus::MessageWriter array_writer(NULL); | |
| 79 writer.OpenArray("{sv}", &array_writer); | |
| 80 dbus::MessageWriter entry_writer(NULL); | |
| 81 // Append address. | |
| 82 array_writer.OpenDictEntry(&entry_writer); | |
| 83 entry_writer.AppendString(flimflam::kAddressProperty); | |
| 84 entry_writer.AppendVariantOfString(kAddress); | |
| 85 array_writer.CloseContainer(&entry_writer); | |
| 86 // Append name. | |
| 87 array_writer.OpenDictEntry(&entry_writer); | |
| 88 entry_writer.AppendString(flimflam::kNameProperty); | |
| 89 entry_writer.AppendVariantOfString(kName); | |
| 90 array_writer.CloseContainer(&entry_writer); | |
| 91 // Append signal strength. | |
| 92 array_writer.OpenDictEntry(&entry_writer); | |
| 93 entry_writer.AppendString(flimflam::kSignalStrengthProperty); | |
| 94 entry_writer.AppendVariantOfByte(kSignalStrength); | |
| 95 array_writer.CloseContainer(&entry_writer); | |
| 96 // Append Wifi channel. | |
| 97 array_writer.OpenDictEntry(&entry_writer); | |
| 98 entry_writer.AppendString(flimflam::kWifiChannelProperty); | |
| 99 entry_writer.AppendVariantOfUint32(kWifiChannel); | |
| 100 array_writer.CloseContainer(&entry_writer); | |
| 101 // Append connected. | |
| 102 array_writer.OpenDictEntry(&entry_writer); | |
| 103 entry_writer.AppendString(flimflam::kConnectedProperty); | |
| 104 entry_writer.AppendVariantOfBool(kConnected); | |
| 105 array_writer.CloseContainer(&entry_writer); | |
| 106 writer.CloseContainer(&array_writer); | |
| 107 | |
| 108 // Create the expected value. | |
| 109 base::DictionaryValue value; | |
| 110 value.SetWithoutPathExpansion(flimflam::kAddressProperty, | |
| 111 base::Value::CreateStringValue(kAddress)); | |
| 112 value.SetWithoutPathExpansion(flimflam::kNameProperty, | |
| 113 base::Value::CreateStringValue(kName)); | |
| 114 value.SetWithoutPathExpansion( | |
| 115 flimflam::kSignalStrengthProperty, | |
| 116 base::Value::CreateIntegerValue(kSignalStrength)); | |
| 117 // WiFi.Channel is set as a double because uint32 is larger than int32. | |
| 118 value.SetWithoutPathExpansion(flimflam::kWifiChannelProperty, | |
| 119 base::Value::CreateDoubleValue(kWifiChannel)); | |
| 120 value.SetWithoutPathExpansion(flimflam::kConnectedProperty, | |
| 121 base::Value::CreateBooleanValue(kConnected)); | |
| 122 | |
| 123 // Set expectations. | |
| 124 PrepareForMethodCall(flimflam::kGetPropertiesFunction, | |
| 125 base::Bind(&ExpectNoArgument), | |
| 126 response.get()); | |
| 127 // Call method. | |
| 128 client_->GetProperties(dbus::ObjectPath(kExampleNetworkPath), | |
| 129 base::Bind(&ExpectDictionaryValueResult, &value)); | |
| 130 // Run the message loop. | |
| 131 message_loop_.RunAllPending(); | |
| 132 } | |
| 133 | |
| 134 TEST_F(FlimflamNetworkClientTest, CallGetPropertiesAndBlock) { | |
| 135 const char kName[] = "name"; | |
| 136 | |
| 137 // Create response. | |
| 138 scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty()); | |
| 139 dbus::MessageWriter writer(response.get()); | |
| 140 dbus::MessageWriter array_writer(NULL); | |
| 141 writer.OpenArray("{sv}", &array_writer); | |
| 142 dbus::MessageWriter entry_writer(NULL); | |
| 143 array_writer.OpenDictEntry(&entry_writer); | |
| 144 entry_writer.AppendString(flimflam::kNameProperty); | |
| 145 entry_writer.AppendVariantOfString(kName); | |
| 146 array_writer.CloseContainer(&entry_writer); | |
| 147 writer.CloseContainer(&array_writer); | |
| 148 | |
| 149 // Create the expected value. | |
| 150 base::DictionaryValue value; | |
| 151 value.SetWithoutPathExpansion(flimflam::kNameProperty, | |
| 152 base::Value::CreateStringValue(kName)); | |
| 153 | |
| 154 // Set expectations. | |
| 155 PrepareForMethodCall(flimflam::kGetPropertiesFunction, | |
| 156 base::Bind(&ExpectNoArgument), | |
| 157 response.get()); | |
| 158 // Call method. | |
| 159 scoped_ptr<base::DictionaryValue> result( | |
| 160 client_->CallGetPropertiesAndBlock( | |
| 161 dbus::ObjectPath(kExampleNetworkPath))); | |
| 162 | |
| 163 ASSERT_TRUE(result.get()); | |
| 164 EXPECT_TRUE(result->Equals(&value)); | |
| 165 } | |
| 166 | |
| 167 } // namespace chromeos | |
| OLD | NEW |