blob: 879a9a606652facc312b2142b112477b29a187a2 [file] [log] [blame]
keybuk@chromium.org7e0c4932012-02-15 13:21:08 +09001// 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 "dbus/property.h"
6
7#include <string>
8#include <vector>
9
10#include "base/basictypes.h"
11#include "base/bind.h"
12#include "base/logging.h"
avi@chromium.orga29af562013-07-18 08:00:30 +090013#include "base/message_loop/message_loop.h"
earthdok64401d72014-09-03 19:32:36 +090014#include "base/run_loop.h"
keybuk@chromium.org7e0c4932012-02-15 13:21:08 +090015#include "base/threading/thread.h"
16#include "base/threading/thread_restrictions.h"
17#include "dbus/bus.h"
18#include "dbus/object_path.h"
19#include "dbus/object_proxy.h"
keybuk@chromium.org7e0c4932012-02-15 13:21:08 +090020#include "dbus/test_service.h"
21#include "testing/gtest/include/gtest/gtest.h"
22
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +090023namespace dbus {
24
keybuk@chromium.org7e0c4932012-02-15 13:21:08 +090025// The property test exerises the asynchronous APIs in PropertySet and
26// Property<>.
27class PropertyTest : public testing::Test {
28 public:
29 PropertyTest() {
30 }
31
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +090032 struct Properties : public PropertySet {
33 Property<std::string> name;
34 Property<int16> version;
35 Property<std::vector<std::string> > methods;
36 Property<std::vector<ObjectPath> > objects;
armansito@chromium.org11e12742014-03-15 16:40:49 +090037 Property<std::vector<uint8> > bytes;
keybuk@chromium.org7e0c4932012-02-15 13:21:08 +090038
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +090039 Properties(ObjectProxy* object_proxy,
keybuk@chromium.org7e0c4932012-02-15 13:21:08 +090040 PropertyChangedCallback property_changed_callback)
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +090041 : PropertySet(object_proxy,
42 "org.chromium.TestInterface",
43 property_changed_callback) {
keybuk@chromium.org7e0c4932012-02-15 13:21:08 +090044 RegisterProperty("Name", &name);
45 RegisterProperty("Version", &version);
46 RegisterProperty("Methods", &methods);
47 RegisterProperty("Objects", &objects);
armansito@chromium.org11e12742014-03-15 16:40:49 +090048 RegisterProperty("Bytes", &bytes);
keybuk@chromium.org7e0c4932012-02-15 13:21:08 +090049 }
50 };
51
52 virtual void SetUp() {
53 // Make the main thread not to allow IO.
54 base::ThreadRestrictions::SetIOAllowed(false);
55
56 // Start the D-Bus thread.
57 dbus_thread_.reset(new base::Thread("D-Bus Thread"));
58 base::Thread::Options thread_options;
xhwang@chromium.orgdff6b132013-05-02 01:10:30 +090059 thread_options.message_loop_type = base::MessageLoop::TYPE_IO;
keybuk@chromium.org7e0c4932012-02-15 13:21:08 +090060 ASSERT_TRUE(dbus_thread_->StartWithOptions(thread_options));
61
62 // Start the test service, using the D-Bus thread.
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +090063 TestService::Options options;
thestig@chromium.org074b1db2013-02-20 10:36:53 +090064 options.dbus_task_runner = dbus_thread_->message_loop_proxy();
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +090065 test_service_.reset(new TestService(options));
keybuk@chromium.org7e0c4932012-02-15 13:21:08 +090066 ASSERT_TRUE(test_service_->StartService());
67 ASSERT_TRUE(test_service_->WaitUntilServiceIsStarted());
68 ASSERT_TRUE(test_service_->HasDBusThread());
69
70 // Create the client, using the D-Bus thread.
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +090071 Bus::Options bus_options;
72 bus_options.bus_type = Bus::SESSION;
73 bus_options.connection_type = Bus::PRIVATE;
thestig@chromium.org074b1db2013-02-20 10:36:53 +090074 bus_options.dbus_task_runner = dbus_thread_->message_loop_proxy();
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +090075 bus_ = new Bus(bus_options);
keybuk@chromium.org7e0c4932012-02-15 13:21:08 +090076 object_proxy_ = bus_->GetObjectProxy(
77 "org.chromium.TestService",
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +090078 ObjectPath("/org/chromium/TestObject"));
keybuk@chromium.org7e0c4932012-02-15 13:21:08 +090079 ASSERT_TRUE(bus_->HasDBusThread());
80
81 // Create the properties structure
satorux@chromium.org5e612da2012-05-31 15:55:53 +090082 properties_.reset(new Properties(
83 object_proxy_,
84 base::Bind(&PropertyTest::OnPropertyChanged,
85 base::Unretained(this))));
keybuk@chromium.org7e0c4932012-02-15 13:21:08 +090086 properties_->ConnectSignals();
87 properties_->GetAll();
88 }
89
90 virtual void TearDown() {
91 bus_->ShutdownOnDBusThreadAndBlock();
92
93 // Shut down the service.
94 test_service_->ShutdownAndBlock();
95
96 // Reset to the default.
97 base::ThreadRestrictions::SetIOAllowed(true);
98
99 // Stopping a thread is considered an IO operation, so do this after
100 // allowing IO.
101 test_service_->Stop();
102 }
103
104 // Generic callback, bind with a string |id| for passing to
105 // WaitForCallback() to ensure the callback for the right method is
106 // waited for.
107 void PropertyCallback(const std::string& id, bool success) {
108 last_callback_ = id;
earthdok64401d72014-09-03 19:32:36 +0900109 run_loop_->Quit();
keybuk@chromium.org7e0c4932012-02-15 13:21:08 +0900110 }
111
112 protected:
113 // Called when a property value is updated.
114 void OnPropertyChanged(const std::string& name) {
115 updated_properties_.push_back(name);
earthdok64401d72014-09-03 19:32:36 +0900116 run_loop_->Quit();
keybuk@chromium.org7e0c4932012-02-15 13:21:08 +0900117 }
118
keybuk@chromium.org683dd8c2012-03-23 05:34:05 +0900119 // Waits for the given number of updates.
keybuk@chromium.org7e0c4932012-02-15 13:21:08 +0900120 void WaitForUpdates(size_t num_updates) {
earthdok64401d72014-09-03 19:32:36 +0900121 while (updated_properties_.size() < num_updates) {
122 run_loop_.reset(new base::RunLoop);
123 run_loop_->Run();
124 }
keybuk@chromium.org7e0c4932012-02-15 13:21:08 +0900125 for (size_t i = 0; i < num_updates; ++i)
126 updated_properties_.erase(updated_properties_.begin());
127 }
128
129 // Name, Version, Methods, Objects
armansito@chromium.org11e12742014-03-15 16:40:49 +0900130 static const int kExpectedSignalUpdates = 5;
keybuk@chromium.org7e0c4932012-02-15 13:21:08 +0900131
keybuk@chromium.org683dd8c2012-03-23 05:34:05 +0900132 // Waits for initial values to be set.
keybuk@chromium.org7e0c4932012-02-15 13:21:08 +0900133 void WaitForGetAll() {
134 WaitForUpdates(kExpectedSignalUpdates);
135 }
136
keybuk@chromium.org683dd8c2012-03-23 05:34:05 +0900137 // Waits for the callback. |id| is the string bound to the callback when
keybuk@chromium.org7e0c4932012-02-15 13:21:08 +0900138 // the method call is made that identifies it and distinguishes from any
139 // other; you can set this to whatever you wish.
140 void WaitForCallback(const std::string& id) {
141 while (last_callback_ != id) {
earthdok64401d72014-09-03 19:32:36 +0900142 run_loop_.reset(new base::RunLoop);
143 run_loop_->Run();
keybuk@chromium.org7e0c4932012-02-15 13:21:08 +0900144 }
145 }
146
xhwang@chromium.orgdff6b132013-05-02 01:10:30 +0900147 base::MessageLoop message_loop_;
earthdok64401d72014-09-03 19:32:36 +0900148 scoped_ptr<base::RunLoop> run_loop_;
keybuk@chromium.org7e0c4932012-02-15 13:21:08 +0900149 scoped_ptr<base::Thread> dbus_thread_;
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900150 scoped_refptr<Bus> bus_;
151 ObjectProxy* object_proxy_;
satorux@chromium.org5e612da2012-05-31 15:55:53 +0900152 scoped_ptr<Properties> properties_;
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900153 scoped_ptr<TestService> test_service_;
keybuk@chromium.org7e0c4932012-02-15 13:21:08 +0900154 // Properties updated.
155 std::vector<std::string> updated_properties_;
156 // Last callback received.
157 std::string last_callback_;
158};
159
160TEST_F(PropertyTest, InitialValues) {
161 WaitForGetAll();
162
keybuk@chromium.org683dd8c2012-03-23 05:34:05 +0900163 EXPECT_EQ("TestService", properties_->name.value());
164 EXPECT_EQ(10, properties_->version.value());
keybuk@chromium.org7e0c4932012-02-15 13:21:08 +0900165
166 std::vector<std::string> methods = properties_->methods.value();
167 ASSERT_EQ(4U, methods.size());
168 EXPECT_EQ("Echo", methods[0]);
169 EXPECT_EQ("SlowEcho", methods[1]);
170 EXPECT_EQ("AsyncEcho", methods[2]);
171 EXPECT_EQ("BrokenMethod", methods[3]);
172
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900173 std::vector<ObjectPath> objects = properties_->objects.value();
keybuk@chromium.org7e0c4932012-02-15 13:21:08 +0900174 ASSERT_EQ(1U, objects.size());
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900175 EXPECT_EQ(ObjectPath("/TestObjectPath"), objects[0]);
armansito@chromium.org11e12742014-03-15 16:40:49 +0900176
177 std::vector<uint8> bytes = properties_->bytes.value();
178 ASSERT_EQ(4U, bytes.size());
179 EXPECT_EQ('T', bytes[0]);
180 EXPECT_EQ('e', bytes[1]);
181 EXPECT_EQ('s', bytes[2]);
182 EXPECT_EQ('t', bytes[3]);
keybuk@chromium.org7e0c4932012-02-15 13:21:08 +0900183}
184
keybuk@google.comf056b962012-03-22 08:43:45 +0900185TEST_F(PropertyTest, UpdatedValues) {
186 WaitForGetAll();
187
188 // Update the value of the "Name" property, this value should not change.
189 properties_->name.Get(base::Bind(&PropertyTest::PropertyCallback,
190 base::Unretained(this),
191 "Name"));
192 WaitForCallback("Name");
193 WaitForUpdates(1);
194
keybuk@chromium.org683dd8c2012-03-23 05:34:05 +0900195 EXPECT_EQ("TestService", properties_->name.value());
keybuk@google.comf056b962012-03-22 08:43:45 +0900196
197 // Update the value of the "Version" property, this value should be changed.
198 properties_->version.Get(base::Bind(&PropertyTest::PropertyCallback,
199 base::Unretained(this),
200 "Version"));
201 WaitForCallback("Version");
202 WaitForUpdates(1);
203
keybuk@chromium.org683dd8c2012-03-23 05:34:05 +0900204 EXPECT_EQ(20, properties_->version.value());
keybuk@google.comf056b962012-03-22 08:43:45 +0900205
206 // Update the value of the "Methods" property, this value should not change
207 // and should not grow to contain duplicate entries.
208 properties_->methods.Get(base::Bind(&PropertyTest::PropertyCallback,
209 base::Unretained(this),
210 "Methods"));
211 WaitForCallback("Methods");
212 WaitForUpdates(1);
213
214 std::vector<std::string> methods = properties_->methods.value();
215 ASSERT_EQ(4U, methods.size());
216 EXPECT_EQ("Echo", methods[0]);
217 EXPECT_EQ("SlowEcho", methods[1]);
218 EXPECT_EQ("AsyncEcho", methods[2]);
219 EXPECT_EQ("BrokenMethod", methods[3]);
220
221 // Update the value of the "Objects" property, this value should not change
222 // and should not grow to contain duplicate entries.
223 properties_->objects.Get(base::Bind(&PropertyTest::PropertyCallback,
224 base::Unretained(this),
225 "Objects"));
226 WaitForCallback("Objects");
227 WaitForUpdates(1);
228
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900229 std::vector<ObjectPath> objects = properties_->objects.value();
keybuk@google.comf056b962012-03-22 08:43:45 +0900230 ASSERT_EQ(1U, objects.size());
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900231 EXPECT_EQ(ObjectPath("/TestObjectPath"), objects[0]);
armansito@chromium.org11e12742014-03-15 16:40:49 +0900232
233 // Update the value of the "Bytes" property, this value should not change
234 // and should not grow to contain duplicate entries.
235 properties_->bytes.Get(base::Bind(&PropertyTest::PropertyCallback,
236 base::Unretained(this),
237 "Bytes"));
238 WaitForCallback("Bytes");
239 WaitForUpdates(1);
240
241 std::vector<uint8> bytes = properties_->bytes.value();
242 ASSERT_EQ(4U, bytes.size());
243 EXPECT_EQ('T', bytes[0]);
244 EXPECT_EQ('e', bytes[1]);
245 EXPECT_EQ('s', bytes[2]);
246 EXPECT_EQ('t', bytes[3]);
keybuk@google.comf056b962012-03-22 08:43:45 +0900247}
248
keybuk@chromium.org7e0c4932012-02-15 13:21:08 +0900249TEST_F(PropertyTest, Get) {
250 WaitForGetAll();
251
252 // Ask for the new Version property.
253 properties_->version.Get(base::Bind(&PropertyTest::PropertyCallback,
254 base::Unretained(this),
255 "Get"));
256 WaitForCallback("Get");
257
258 // Make sure we got a property update too.
259 WaitForUpdates(1);
260
keybuk@chromium.org683dd8c2012-03-23 05:34:05 +0900261 EXPECT_EQ(20, properties_->version.value());
keybuk@chromium.org7e0c4932012-02-15 13:21:08 +0900262}
263
264TEST_F(PropertyTest, Set) {
265 WaitForGetAll();
266
267 // Set a new name.
268 properties_->name.Set("NewService",
269 base::Bind(&PropertyTest::PropertyCallback,
270 base::Unretained(this),
271 "Set"));
272 WaitForCallback("Set");
273
274 // TestService sends a property update.
275 WaitForUpdates(1);
276
keybuk@chromium.org683dd8c2012-03-23 05:34:05 +0900277 EXPECT_EQ("NewService", properties_->name.value());
keybuk@chromium.org7e0c4932012-02-15 13:21:08 +0900278}
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900279
280} // namespace dbus