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_profile_client.h"
9 #include "dbus/message.h"
10 #include "dbus/object_path.h"
11 #include "dbus/values_util.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13 #include "third_party/cros_system_api/dbus/service_constants.h"
21 const char kDefaultProfilePath
[] = "/profile/default";
22 const char kExampleEntryPath
[] = "example_entry_path";
24 void AppendVariantOfArrayOfStrings(dbus::MessageWriter
* writer
,
25 const std::vector
<std::string
>& strings
) {
26 dbus::MessageWriter
variant_writer(NULL
);
27 writer
->OpenVariant("as", &variant_writer
);
28 variant_writer
.AppendArrayOfStrings(strings
);
29 writer
->CloseContainer(&variant_writer
);
34 class ShillProfileClientTest
: public ShillClientUnittestBase
{
36 ShillProfileClientTest()
37 : ShillClientUnittestBase(flimflam::kFlimflamProfileInterface
,
38 dbus::ObjectPath(kDefaultProfilePath
)) {
41 virtual void SetUp() {
42 ShillClientUnittestBase::SetUp();
43 // Create a client with the mock bus.
44 client_
.reset(ShillProfileClient::Create(REAL_DBUS_CLIENT_IMPLEMENTATION
,
46 // Run the message loop to run the signal connection result callback.
47 message_loop_
.RunUntilIdle();
50 virtual void TearDown() {
51 ShillClientUnittestBase::TearDown();
55 scoped_ptr
<ShillProfileClient
> client_
;
58 TEST_F(ShillProfileClientTest
, PropertyChanged
) {
60 dbus::Signal
signal(flimflam::kFlimflamProfileInterface
,
61 flimflam::kMonitorPropertyChanged
);
62 dbus::MessageWriter
writer(&signal
);
63 writer
.AppendString(flimflam::kEntriesProperty
);
64 AppendVariantOfArrayOfStrings(&writer
,
65 std::vector
<std::string
>(1, kExampleEntryPath
));
68 base::ListValue value
;
69 value
.Append(base::Value::CreateStringValue(kExampleEntryPath
));
70 MockPropertyChangeObserver observer
;
73 flimflam::kEntriesProperty
,
74 ValueEq(value
))).Times(1);
77 client_
->AddPropertyChangedObserver(
78 dbus::ObjectPath(kDefaultProfilePath
),
81 // Run the signal callback.
82 SendPropertyChangedSignal(&signal
);
84 // Remove the observer.
85 client_
->RemovePropertyChangedObserver(
86 dbus::ObjectPath(kDefaultProfilePath
),
89 EXPECT_CALL(observer
, OnPropertyChanged(_
, _
)).Times(0);
91 // Run the signal callback again and make sure the observer isn't called.
92 SendPropertyChangedSignal(&signal
);
95 TEST_F(ShillProfileClientTest
, GetProperties
) {
97 scoped_ptr
<dbus::Response
> response(dbus::Response::CreateEmpty());
98 dbus::MessageWriter
writer(response
.get());
99 dbus::MessageWriter
array_writer(NULL
);
100 writer
.OpenArray("{sv}", &array_writer
);
101 dbus::MessageWriter
entry_writer(NULL
);
102 array_writer
.OpenDictEntry(&entry_writer
);
103 entry_writer
.AppendString(flimflam::kEntriesProperty
);
104 AppendVariantOfArrayOfStrings(&entry_writer
,
105 std::vector
<std::string
>(1, kExampleEntryPath
));
106 array_writer
.CloseContainer(&entry_writer
);
107 writer
.CloseContainer(&array_writer
);
109 // Create the expected value.
110 base::ListValue
* entries
= new base::ListValue
;
111 entries
->Append(base::Value::CreateStringValue(kExampleEntryPath
));
112 base::DictionaryValue value
;
113 value
.SetWithoutPathExpansion(flimflam::kEntriesProperty
, entries
);
115 PrepareForMethodCall(flimflam::kGetPropertiesFunction
,
116 base::Bind(&ExpectNoArgument
),
119 MockErrorCallback error_callback
;
120 client_
->GetProperties(dbus::ObjectPath(kDefaultProfilePath
),
121 base::Bind(&ExpectDictionaryValueResultWithoutStatus
,
123 error_callback
.GetCallback());
124 EXPECT_CALL(error_callback
, Run(_
, _
)).Times(0);
126 // Run the message loop.
127 message_loop_
.RunUntilIdle();
130 TEST_F(ShillProfileClientTest
, GetEntry
) {
132 scoped_ptr
<dbus::Response
> response(dbus::Response::CreateEmpty());
133 dbus::MessageWriter
writer(response
.get());
134 dbus::MessageWriter
array_writer(NULL
);
135 writer
.OpenArray("{sv}", &array_writer
);
136 dbus::MessageWriter
entry_writer(NULL
);
137 array_writer
.OpenDictEntry(&entry_writer
);
138 entry_writer
.AppendString(flimflam::kTypeProperty
);
139 entry_writer
.AppendVariantOfString(flimflam::kTypeWifi
);
140 array_writer
.CloseContainer(&entry_writer
);
141 writer
.CloseContainer(&array_writer
);
143 // Create the expected value.
144 base::DictionaryValue value
;
145 value
.SetWithoutPathExpansion(
146 flimflam::kTypeProperty
,
147 base::Value::CreateStringValue(flimflam::kTypeWifi
));
149 PrepareForMethodCall(flimflam::kGetEntryFunction
,
150 base::Bind(&ExpectStringArgument
, kExampleEntryPath
),
153 MockErrorCallback error_callback
;
154 client_
->GetEntry(dbus::ObjectPath(kDefaultProfilePath
),
156 base::Bind(&ExpectDictionaryValueResultWithoutStatus
,
158 error_callback
.GetCallback());
159 EXPECT_CALL(error_callback
, Run(_
, _
)).Times(0);
160 // Run the message loop.
161 message_loop_
.RunUntilIdle();
164 TEST_F(ShillProfileClientTest
, DeleteEntry
) {
166 scoped_ptr
<dbus::Response
> response(dbus::Response::CreateEmpty());
168 // Create the expected value.
169 base::DictionaryValue value
;
170 value
.SetWithoutPathExpansion(flimflam::kOfflineModeProperty
,
171 base::Value::CreateBooleanValue(true));
173 PrepareForMethodCall(flimflam::kDeleteEntryFunction
,
174 base::Bind(&ExpectStringArgument
, kExampleEntryPath
),
177 MockClosure mock_closure
;
178 MockErrorCallback mock_error_callback
;
179 client_
->DeleteEntry(dbus::ObjectPath(kDefaultProfilePath
),
181 mock_closure
.GetCallback(),
182 mock_error_callback
.GetCallback());
183 EXPECT_CALL(mock_closure
, Run()).Times(1);
184 EXPECT_CALL(mock_error_callback
, Run(_
, _
)).Times(0);
186 // Run the message loop.
187 message_loop_
.RunUntilIdle();
190 } // namespace chromeos