Home | History | Annotate | Download | only in dbus
      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_ipconfig_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 using testing::_;
     15 using testing::ByRef;
     16 
     17 namespace chromeos {
     18 
     19 namespace {
     20 
     21 const char kExampleIPConfigPath[] = "/foo/bar";
     22 
     23 }  // namespace
     24 
     25 class ShillIPConfigClientTest : public ShillClientUnittestBase {
     26  public:
     27   ShillIPConfigClientTest()
     28       : ShillClientUnittestBase(
     29           flimflam::kFlimflamIPConfigInterface,
     30           dbus::ObjectPath(kExampleIPConfigPath)) {
     31   }
     32 
     33   virtual void SetUp() {
     34     ShillClientUnittestBase::SetUp();
     35     // Create a client with the mock bus.
     36     client_.reset(ShillIPConfigClient::Create(REAL_DBUS_CLIENT_IMPLEMENTATION,
     37                                               mock_bus_.get()));
     38     // Run the message loop to run the signal connection result callback.
     39     message_loop_.RunUntilIdle();
     40   }
     41 
     42   virtual void TearDown() {
     43     ShillClientUnittestBase::TearDown();
     44   }
     45 
     46  protected:
     47   scoped_ptr<ShillIPConfigClient> client_;
     48 };
     49 
     50 TEST_F(ShillIPConfigClientTest, PropertyChanged) {
     51   // Create a signal.
     52   const base::FundamentalValue kConnected(true);
     53   dbus::Signal signal(flimflam::kFlimflamIPConfigInterface,
     54                       flimflam::kMonitorPropertyChanged);
     55   dbus::MessageWriter writer(&signal);
     56   writer.AppendString(flimflam::kConnectedProperty);
     57   dbus::AppendBasicTypeValueDataAsVariant(&writer, kConnected);
     58 
     59   // Set expectations.
     60   MockPropertyChangeObserver observer;
     61   EXPECT_CALL(observer, OnPropertyChanged(flimflam::kConnectedProperty,
     62                                           ValueEq(ByRef(kConnected)))).Times(1);
     63 
     64   // Add the observer
     65   client_->AddPropertyChangedObserver(
     66       dbus::ObjectPath(kExampleIPConfigPath),
     67       &observer);
     68 
     69   // Run the signal callback.
     70   SendPropertyChangedSignal(&signal);
     71 
     72   // Remove the observer.
     73   client_->RemovePropertyChangedObserver(
     74       dbus::ObjectPath(kExampleIPConfigPath),
     75       &observer);
     76 
     77   EXPECT_CALL(observer, OnPropertyChanged(_, _)).Times(0);
     78 
     79   // Run the signal callback again and make sure the observer isn't called.
     80   SendPropertyChangedSignal(&signal);
     81 }
     82 
     83 TEST_F(ShillIPConfigClientTest, GetProperties) {
     84   const char kAddress[] = "address";
     85   const int32 kMtu = 68;
     86 
     87   // Create response.
     88   scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty());
     89   dbus::MessageWriter writer(response.get());
     90   dbus::MessageWriter array_writer(NULL);
     91   writer.OpenArray("{sv}", &array_writer);
     92   dbus::MessageWriter entry_writer(NULL);
     93   // Append address.
     94   array_writer.OpenDictEntry(&entry_writer);
     95   entry_writer.AppendString(flimflam::kAddressProperty);
     96   entry_writer.AppendVariantOfString(kAddress);
     97   array_writer.CloseContainer(&entry_writer);
     98   // Append MTU.
     99   array_writer.OpenDictEntry(&entry_writer);
    100   entry_writer.AppendString(flimflam::kMtuProperty);
    101   entry_writer.AppendVariantOfInt32(kMtu);
    102   array_writer.CloseContainer(&entry_writer);
    103   writer.CloseContainer(&array_writer);
    104 
    105   // Create the expected value.
    106   base::DictionaryValue value;
    107   value.SetWithoutPathExpansion(flimflam::kAddressProperty,
    108                                 base::Value::CreateStringValue(kAddress));
    109   value.SetWithoutPathExpansion(flimflam::kMtuProperty,
    110                                 base::Value::CreateIntegerValue(kMtu));
    111 
    112   // Set expectations.
    113   PrepareForMethodCall(flimflam::kGetPropertiesFunction,
    114                        base::Bind(&ExpectNoArgument),
    115                        response.get());
    116   // Call method.
    117   client_->GetProperties(dbus::ObjectPath(kExampleIPConfigPath),
    118                          base::Bind(&ExpectDictionaryValueResult, &value));
    119   // Run the message loop.
    120   message_loop_.RunUntilIdle();
    121 }
    122 
    123 TEST_F(ShillIPConfigClientTest, CallGetPropertiesAndBlock) {
    124   const char kAddress[] = "address";
    125   const int32 kMtu = 68;
    126 
    127   // Create response.
    128   scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty());
    129   dbus::MessageWriter writer(response.get());
    130   dbus::MessageWriter array_writer(NULL);
    131   writer.OpenArray("{sv}", &array_writer);
    132   dbus::MessageWriter entry_writer(NULL);
    133   // Append address.
    134   array_writer.OpenDictEntry(&entry_writer);
    135   entry_writer.AppendString(flimflam::kAddressProperty);
    136   entry_writer.AppendVariantOfString(kAddress);
    137   array_writer.CloseContainer(&entry_writer);
    138   // Append MTU.
    139   array_writer.OpenDictEntry(&entry_writer);
    140   entry_writer.AppendString(flimflam::kMtuProperty);
    141   entry_writer.AppendVariantOfInt32(kMtu);
    142   array_writer.CloseContainer(&entry_writer);
    143   writer.CloseContainer(&array_writer);
    144 
    145   // Create the expected value.
    146   base::DictionaryValue value;
    147   value.SetWithoutPathExpansion(flimflam::kAddressProperty,
    148                                 base::Value::CreateStringValue(kAddress));
    149   value.SetWithoutPathExpansion(flimflam::kMtuProperty,
    150                                 base::Value::CreateIntegerValue(kMtu));
    151   // Set expectations.
    152   PrepareForMethodCall(flimflam::kGetPropertiesFunction,
    153                        base::Bind(&ExpectNoArgument),
    154                        response.get());
    155   // Call method.
    156   scoped_ptr<base::DictionaryValue> result(
    157       client_->CallGetPropertiesAndBlock(
    158           dbus::ObjectPath(kExampleIPConfigPath)));
    159 
    160   ASSERT_TRUE(result.get());
    161   EXPECT_TRUE(result->Equals(&value));
    162 }
    163 
    164 TEST_F(ShillIPConfigClientTest, SetProperty) {
    165   const char kAddress[] = "address";
    166 
    167   // Create response.
    168   scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty());
    169 
    170   // Set expectations.
    171   base::StringValue value(kAddress);
    172   PrepareForMethodCall(flimflam::kSetPropertyFunction,
    173                        base::Bind(&ExpectStringAndValueArguments,
    174                                   flimflam::kAddressProperty,
    175                                   &value),
    176                        response.get());
    177   // Call method.
    178   client_->SetProperty(dbus::ObjectPath(kExampleIPConfigPath),
    179                        flimflam::kAddressProperty,
    180                        value,
    181                        base::Bind(&ExpectNoResultValue));
    182   // Run the message loop.
    183   message_loop_.RunUntilIdle();
    184 }
    185 
    186 TEST_F(ShillIPConfigClientTest, ClearProperty) {
    187   // Create response.
    188   scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty());
    189 
    190   // Set expectations.
    191   PrepareForMethodCall(flimflam::kClearPropertyFunction,
    192                        base::Bind(&ExpectStringArgument,
    193                                   flimflam::kAddressProperty),
    194                        response.get());
    195   // Call method.
    196   client_->ClearProperty(dbus::ObjectPath(kExampleIPConfigPath),
    197                        flimflam::kAddressProperty,
    198                        base::Bind(&ExpectNoResultValue));
    199   // Run the message loop.
    200   message_loop_.RunUntilIdle();
    201 }
    202 
    203 TEST_F(ShillIPConfigClientTest, Remove) {
    204   // Create response.
    205   scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty());
    206 
    207   // Set expectations.
    208   PrepareForMethodCall(flimflam::kRemoveConfigFunction,
    209                        base::Bind(&ExpectNoArgument),
    210                        response.get());
    211   // Call method.
    212   client_->Remove(dbus::ObjectPath(kExampleIPConfigPath),
    213                   base::Bind(&ExpectNoResultValue));
    214 
    215   // Run the message loop.
    216   message_loop_.RunUntilIdle();
    217 }
    218 
    219 }  // namespace chromeos
    220