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.
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"
21 const char kExampleIPConfigPath
[] = "/foo/bar";
25 class ShillIPConfigClientTest
: public ShillClientUnittestBase
{
27 ShillIPConfigClientTest()
28 : ShillClientUnittestBase(
29 flimflam::kFlimflamIPConfigInterface
,
30 dbus::ObjectPath(kExampleIPConfigPath
)) {
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 client_
->Init(mock_bus_
.get());
38 // Run the message loop to run the signal connection result callback.
39 message_loop_
.RunUntilIdle();
42 virtual void TearDown() {
43 ShillClientUnittestBase::TearDown();
47 scoped_ptr
<ShillIPConfigClient
> client_
;
50 TEST_F(ShillIPConfigClientTest
, PropertyChanged
) {
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
);
60 MockPropertyChangeObserver observer
;
61 EXPECT_CALL(observer
, OnPropertyChanged(flimflam::kConnectedProperty
,
62 ValueEq(ByRef(kConnected
)))).Times(1);
65 client_
->AddPropertyChangedObserver(
66 dbus::ObjectPath(kExampleIPConfigPath
),
69 // Run the signal callback.
70 SendPropertyChangedSignal(&signal
);
72 // Remove the observer.
73 client_
->RemovePropertyChangedObserver(
74 dbus::ObjectPath(kExampleIPConfigPath
),
77 EXPECT_CALL(observer
, OnPropertyChanged(_
, _
)).Times(0);
79 // Run the signal callback again and make sure the observer isn't called.
80 SendPropertyChangedSignal(&signal
);
83 TEST_F(ShillIPConfigClientTest
, GetProperties
) {
84 const char kAddress
[] = "address";
85 const int32 kMtu
= 68;
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
);
94 array_writer
.OpenDictEntry(&entry_writer
);
95 entry_writer
.AppendString(flimflam::kAddressProperty
);
96 entry_writer
.AppendVariantOfString(kAddress
);
97 array_writer
.CloseContainer(&entry_writer
);
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
);
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
));
113 PrepareForMethodCall(flimflam::kGetPropertiesFunction
,
114 base::Bind(&ExpectNoArgument
),
117 client_
->GetProperties(dbus::ObjectPath(kExampleIPConfigPath
),
118 base::Bind(&ExpectDictionaryValueResult
, &value
));
119 // Run the message loop.
120 message_loop_
.RunUntilIdle();
123 TEST_F(ShillIPConfigClientTest
, SetProperty
) {
124 const char kAddress
[] = "address";
127 scoped_ptr
<dbus::Response
> response(dbus::Response::CreateEmpty());
130 base::StringValue
value(kAddress
);
131 PrepareForMethodCall(flimflam::kSetPropertyFunction
,
132 base::Bind(&ExpectStringAndValueArguments
,
133 flimflam::kAddressProperty
,
137 client_
->SetProperty(dbus::ObjectPath(kExampleIPConfigPath
),
138 flimflam::kAddressProperty
,
140 base::Bind(&ExpectNoResultValue
));
141 // Run the message loop.
142 message_loop_
.RunUntilIdle();
145 TEST_F(ShillIPConfigClientTest
, ClearProperty
) {
147 scoped_ptr
<dbus::Response
> response(dbus::Response::CreateEmpty());
150 PrepareForMethodCall(flimflam::kClearPropertyFunction
,
151 base::Bind(&ExpectStringArgument
,
152 flimflam::kAddressProperty
),
155 client_
->ClearProperty(dbus::ObjectPath(kExampleIPConfigPath
),
156 flimflam::kAddressProperty
,
157 base::Bind(&ExpectNoResultValue
));
158 // Run the message loop.
159 message_loop_
.RunUntilIdle();
162 TEST_F(ShillIPConfigClientTest
, Remove
) {
164 scoped_ptr
<dbus::Response
> response(dbus::Response::CreateEmpty());
167 PrepareForMethodCall(flimflam::kRemoveConfigFunction
,
168 base::Bind(&ExpectNoArgument
),
171 client_
->Remove(dbus::ObjectPath(kExampleIPConfigPath
),
172 base::Bind(&ExpectNoResultValue
));
174 // Run the message loop.
175 message_loop_
.RunUntilIdle();
178 } // namespace chromeos