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 shill::kFlimflamIPConfigInterface
,
30 dbus::ObjectPath(kExampleIPConfigPath
)) {
33 void SetUp() override
{
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();
42 void TearDown() override
{ ShillClientUnittestBase::TearDown(); }
45 scoped_ptr
<ShillIPConfigClient
> client_
;
48 TEST_F(ShillIPConfigClientTest
, PropertyChanged
) {
50 const base::FundamentalValue
kConnected(true);
51 dbus::Signal
signal(shill::kFlimflamIPConfigInterface
,
52 shill::kMonitorPropertyChanged
);
53 dbus::MessageWriter
writer(&signal
);
54 writer
.AppendString(shill::kConnectedProperty
);
55 dbus::AppendBasicTypeValueDataAsVariant(&writer
, kConnected
);
58 MockPropertyChangeObserver observer
;
59 EXPECT_CALL(observer
, OnPropertyChanged(shill::kConnectedProperty
,
60 ValueEq(ByRef(kConnected
)))).Times(1);
63 client_
->AddPropertyChangedObserver(
64 dbus::ObjectPath(kExampleIPConfigPath
),
67 // Run the signal callback.
68 SendPropertyChangedSignal(&signal
);
70 // Remove the observer.
71 client_
->RemovePropertyChangedObserver(
72 dbus::ObjectPath(kExampleIPConfigPath
),
75 EXPECT_CALL(observer
, OnPropertyChanged(_
, _
)).Times(0);
77 // Run the signal callback again and make sure the observer isn't called.
78 SendPropertyChangedSignal(&signal
);
81 TEST_F(ShillIPConfigClientTest
, GetProperties
) {
82 const char kAddress
[] = "address";
83 const int32 kMtu
= 68;
86 scoped_ptr
<dbus::Response
> response(dbus::Response::CreateEmpty());
87 dbus::MessageWriter
writer(response
.get());
88 dbus::MessageWriter
array_writer(NULL
);
89 writer
.OpenArray("{sv}", &array_writer
);
90 dbus::MessageWriter
entry_writer(NULL
);
92 array_writer
.OpenDictEntry(&entry_writer
);
93 entry_writer
.AppendString(shill::kAddressProperty
);
94 entry_writer
.AppendVariantOfString(kAddress
);
95 array_writer
.CloseContainer(&entry_writer
);
97 array_writer
.OpenDictEntry(&entry_writer
);
98 entry_writer
.AppendString(shill::kMtuProperty
);
99 entry_writer
.AppendVariantOfInt32(kMtu
);
100 array_writer
.CloseContainer(&entry_writer
);
101 writer
.CloseContainer(&array_writer
);
103 // Create the expected value.
104 base::DictionaryValue value
;
105 value
.SetWithoutPathExpansion(shill::kAddressProperty
,
106 new base::StringValue(kAddress
));
107 value
.SetWithoutPathExpansion(shill::kMtuProperty
,
108 new base::FundamentalValue(kMtu
));
111 PrepareForMethodCall(shill::kGetPropertiesFunction
,
112 base::Bind(&ExpectNoArgument
),
115 client_
->GetProperties(dbus::ObjectPath(kExampleIPConfigPath
),
116 base::Bind(&ExpectDictionaryValueResult
, &value
));
117 // Run the message loop.
118 message_loop_
.RunUntilIdle();
121 TEST_F(ShillIPConfigClientTest
, SetProperty
) {
122 const char kAddress
[] = "address";
125 scoped_ptr
<dbus::Response
> response(dbus::Response::CreateEmpty());
128 base::StringValue
value(kAddress
);
129 PrepareForMethodCall(shill::kSetPropertyFunction
,
130 base::Bind(&ExpectStringAndValueArguments
,
131 shill::kAddressProperty
,
135 client_
->SetProperty(dbus::ObjectPath(kExampleIPConfigPath
),
136 shill::kAddressProperty
,
138 base::Bind(&ExpectNoResultValue
));
139 // Run the message loop.
140 message_loop_
.RunUntilIdle();
143 TEST_F(ShillIPConfigClientTest
, ClearProperty
) {
145 scoped_ptr
<dbus::Response
> response(dbus::Response::CreateEmpty());
148 PrepareForMethodCall(shill::kClearPropertyFunction
,
149 base::Bind(&ExpectStringArgument
,
150 shill::kAddressProperty
),
153 client_
->ClearProperty(dbus::ObjectPath(kExampleIPConfigPath
),
154 shill::kAddressProperty
,
155 base::Bind(&ExpectNoResultValue
));
156 // Run the message loop.
157 message_loop_
.RunUntilIdle();
160 TEST_F(ShillIPConfigClientTest
, Remove
) {
162 scoped_ptr
<dbus::Response
> response(dbus::Response::CreateEmpty());
165 PrepareForMethodCall(shill::kRemoveConfigFunction
,
166 base::Bind(&ExpectNoArgument
),
169 client_
->Remove(dbus::ObjectPath(kExampleIPConfigPath
),
170 base::Bind(&ExpectNoResultValue
));
172 // Run the message loop.
173 message_loop_
.RunUntilIdle();
176 } // namespace chromeos