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           shill::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());
     37     client_->Init(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(shill::kFlimflamIPConfigInterface,
     54                       shill::kMonitorPropertyChanged);
     55   dbus::MessageWriter writer(&signal);
     56   writer.AppendString(shill::kConnectedProperty);
     57   dbus::AppendBasicTypeValueDataAsVariant(&writer, kConnected);
     58 
     59   // Set expectations.
     60   MockPropertyChangeObserver observer;
     61   EXPECT_CALL(observer, OnPropertyChanged(shill::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(shill::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(shill::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(shill::kAddressProperty,
    108                                 new base::StringValue(kAddress));
    109   value.SetWithoutPathExpansion(shill::kMtuProperty,
    110                                 new base::FundamentalValue(kMtu));
    111 
    112   // Set expectations.
    113   PrepareForMethodCall(shill::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, SetProperty) {
    124   const char kAddress[] = "address";
    125 
    126   // Create response.
    127   scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty());
    128 
    129   // Set expectations.
    130   base::StringValue value(kAddress);
    131   PrepareForMethodCall(shill::kSetPropertyFunction,
    132                        base::Bind(&ExpectStringAndValueArguments,
    133                                   shill::kAddressProperty,
    134                                   &value),
    135                        response.get());
    136   // Call method.
    137   client_->SetProperty(dbus::ObjectPath(kExampleIPConfigPath),
    138                        shill::kAddressProperty,
    139                        value,
    140                        base::Bind(&ExpectNoResultValue));
    141   // Run the message loop.
    142   message_loop_.RunUntilIdle();
    143 }
    144 
    145 TEST_F(ShillIPConfigClientTest, ClearProperty) {
    146   // Create response.
    147   scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty());
    148 
    149   // Set expectations.
    150   PrepareForMethodCall(shill::kClearPropertyFunction,
    151                        base::Bind(&ExpectStringArgument,
    152                                   shill::kAddressProperty),
    153                        response.get());
    154   // Call method.
    155   client_->ClearProperty(dbus::ObjectPath(kExampleIPConfigPath),
    156                        shill::kAddressProperty,
    157                        base::Bind(&ExpectNoResultValue));
    158   // Run the message loop.
    159   message_loop_.RunUntilIdle();
    160 }
    161 
    162 TEST_F(ShillIPConfigClientTest, Remove) {
    163   // Create response.
    164   scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty());
    165 
    166   // Set expectations.
    167   PrepareForMethodCall(shill::kRemoveConfigFunction,
    168                        base::Bind(&ExpectNoArgument),
    169                        response.get());
    170   // Call method.
    171   client_->Remove(dbus::ObjectPath(kExampleIPConfigPath),
    172                   base::Bind(&ExpectNoResultValue));
    173 
    174   // Run the message loop.
    175   message_loop_.RunUntilIdle();
    176 }
    177 
    178 }  // namespace chromeos
    179