[ServiceWorker] Implement WebServiceWorkerContextClient::openWindow().
[chromium-blink-merge.git] / chromeos / dbus / shill_profile_client_unittest.cc
blobe9fa142bdbd63523160a0ac87628e920d2b201f4
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.
5 #include "base/bind.h"
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"
15 using testing::_;
17 namespace chromeos {
19 namespace {
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);
32 } // namespace
34 class ShillProfileClientTest : public ShillClientUnittestBase {
35 public:
36 ShillProfileClientTest()
37 : ShillClientUnittestBase(shill::kFlimflamProfileInterface,
38 dbus::ObjectPath(kDefaultProfilePath)) {
41 void SetUp() override {
42 ShillClientUnittestBase::SetUp();
43 // Create a client with the mock bus.
44 client_.reset(ShillProfileClient::Create());
45 client_->Init(mock_bus_.get());
46 // Run the message loop to run the signal connection result callback.
47 message_loop_.RunUntilIdle();
50 void TearDown() override { ShillClientUnittestBase::TearDown(); }
52 protected:
53 scoped_ptr<ShillProfileClient> client_;
56 TEST_F(ShillProfileClientTest, PropertyChanged) {
57 // Create a signal.
58 dbus::Signal signal(shill::kFlimflamProfileInterface,
59 shill::kMonitorPropertyChanged);
60 dbus::MessageWriter writer(&signal);
61 writer.AppendString(shill::kEntriesProperty);
62 AppendVariantOfArrayOfStrings(&writer,
63 std::vector<std::string>(1, kExampleEntryPath));
65 // Set expectations.
66 base::ListValue value;
67 value.Append(new base::StringValue(kExampleEntryPath));
68 MockPropertyChangeObserver observer;
69 EXPECT_CALL(observer,
70 OnPropertyChanged(
71 shill::kEntriesProperty,
72 ValueEq(value))).Times(1);
74 // Add the observer
75 client_->AddPropertyChangedObserver(
76 dbus::ObjectPath(kDefaultProfilePath),
77 &observer);
79 // Run the signal callback.
80 SendPropertyChangedSignal(&signal);
82 // Remove the observer.
83 client_->RemovePropertyChangedObserver(
84 dbus::ObjectPath(kDefaultProfilePath),
85 &observer);
87 EXPECT_CALL(observer, OnPropertyChanged(_, _)).Times(0);
89 // Run the signal callback again and make sure the observer isn't called.
90 SendPropertyChangedSignal(&signal);
93 TEST_F(ShillProfileClientTest, GetProperties) {
94 // Create response.
95 scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty());
96 dbus::MessageWriter writer(response.get());
97 dbus::MessageWriter array_writer(NULL);
98 writer.OpenArray("{sv}", &array_writer);
99 dbus::MessageWriter entry_writer(NULL);
100 array_writer.OpenDictEntry(&entry_writer);
101 entry_writer.AppendString(shill::kEntriesProperty);
102 AppendVariantOfArrayOfStrings(&entry_writer,
103 std::vector<std::string>(1, kExampleEntryPath));
104 array_writer.CloseContainer(&entry_writer);
105 writer.CloseContainer(&array_writer);
107 // Create the expected value.
108 base::ListValue* entries = new base::ListValue;
109 entries->Append(new base::StringValue(kExampleEntryPath));
110 base::DictionaryValue value;
111 value.SetWithoutPathExpansion(shill::kEntriesProperty, entries);
112 // Set expectations.
113 PrepareForMethodCall(shill::kGetPropertiesFunction,
114 base::Bind(&ExpectNoArgument),
115 response.get());
116 // Call method.
117 MockErrorCallback error_callback;
118 client_->GetProperties(dbus::ObjectPath(kDefaultProfilePath),
119 base::Bind(&ExpectDictionaryValueResultWithoutStatus,
120 &value),
121 error_callback.GetCallback());
122 EXPECT_CALL(error_callback, Run(_, _)).Times(0);
124 // Run the message loop.
125 message_loop_.RunUntilIdle();
128 TEST_F(ShillProfileClientTest, GetEntry) {
129 // Create response.
130 scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty());
131 dbus::MessageWriter writer(response.get());
132 dbus::MessageWriter array_writer(NULL);
133 writer.OpenArray("{sv}", &array_writer);
134 dbus::MessageWriter entry_writer(NULL);
135 array_writer.OpenDictEntry(&entry_writer);
136 entry_writer.AppendString(shill::kTypeProperty);
137 entry_writer.AppendVariantOfString(shill::kTypeWifi);
138 array_writer.CloseContainer(&entry_writer);
139 writer.CloseContainer(&array_writer);
141 // Create the expected value.
142 base::DictionaryValue value;
143 value.SetWithoutPathExpansion(shill::kTypeProperty,
144 new base::StringValue(shill::kTypeWifi));
145 // Set expectations.
146 PrepareForMethodCall(shill::kGetEntryFunction,
147 base::Bind(&ExpectStringArgument, kExampleEntryPath),
148 response.get());
149 // Call method.
150 MockErrorCallback error_callback;
151 client_->GetEntry(dbus::ObjectPath(kDefaultProfilePath),
152 kExampleEntryPath,
153 base::Bind(&ExpectDictionaryValueResultWithoutStatus,
154 &value),
155 error_callback.GetCallback());
156 EXPECT_CALL(error_callback, Run(_, _)).Times(0);
157 // Run the message loop.
158 message_loop_.RunUntilIdle();
161 TEST_F(ShillProfileClientTest, DeleteEntry) {
162 // Create response.
163 scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty());
165 // Create the expected value.
166 base::DictionaryValue value;
167 value.SetWithoutPathExpansion(shill::kOfflineModeProperty,
168 new base::FundamentalValue(true));
169 // Set expectations.
170 PrepareForMethodCall(shill::kDeleteEntryFunction,
171 base::Bind(&ExpectStringArgument, kExampleEntryPath),
172 response.get());
173 // Call method.
174 MockClosure mock_closure;
175 MockErrorCallback mock_error_callback;
176 client_->DeleteEntry(dbus::ObjectPath(kDefaultProfilePath),
177 kExampleEntryPath,
178 mock_closure.GetCallback(),
179 mock_error_callback.GetCallback());
180 EXPECT_CALL(mock_closure, Run()).Times(1);
181 EXPECT_CALL(mock_error_callback, Run(_, _)).Times(0);
183 // Run the message loop.
184 message_loop_.RunUntilIdle();
187 } // namespace chromeos