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 "dbus/property.h"
10 #include "base/basictypes.h"
11 #include "base/bind.h"
12 #include "base/logging.h"
13 #include "base/message_loop/message_loop.h"
14 #include "base/threading/thread.h"
15 #include "base/threading/thread_restrictions.h"
17 #include "dbus/object_path.h"
18 #include "dbus/object_proxy.h"
19 #include "dbus/test_service.h"
20 #include "testing/gtest/include/gtest/gtest.h"
24 // The property test exerises the asynchronous APIs in PropertySet and
26 class PropertyTest : public testing::Test {
31 struct Properties : public PropertySet {
32 Property<std::string> name;
33 Property<int16> version;
34 Property<std::vector<std::string> > methods;
35 Property<std::vector<ObjectPath> > objects;
36 Property<std::vector<uint8> > bytes;
38 Properties(ObjectProxy* object_proxy,
39 PropertyChangedCallback property_changed_callback)
40 : PropertySet(object_proxy,
41 "org.chromium.TestInterface",
42 property_changed_callback) {
43 RegisterProperty("Name", &name);
44 RegisterProperty("Version", &version);
45 RegisterProperty("Methods", &methods);
46 RegisterProperty("Objects", &objects);
47 RegisterProperty("Bytes", &bytes);
51 virtual void SetUp() {
52 // Make the main thread not to allow IO.
53 base::ThreadRestrictions::SetIOAllowed(false);
55 // Start the D-Bus thread.
56 dbus_thread_.reset(new base::Thread("D-Bus Thread"));
57 base::Thread::Options thread_options;
58 thread_options.message_loop_type = base::MessageLoop::TYPE_IO;
59 ASSERT_TRUE(dbus_thread_->StartWithOptions(thread_options));
61 // Start the test service, using the D-Bus thread.
62 TestService::Options options;
63 options.dbus_task_runner = dbus_thread_->message_loop_proxy();
64 test_service_.reset(new TestService(options));
65 ASSERT_TRUE(test_service_->StartService());
66 ASSERT_TRUE(test_service_->WaitUntilServiceIsStarted());
67 ASSERT_TRUE(test_service_->HasDBusThread());
69 // Create the client, using the D-Bus thread.
70 Bus::Options bus_options;
71 bus_options.bus_type = Bus::SESSION;
72 bus_options.connection_type = Bus::PRIVATE;
73 bus_options.dbus_task_runner = dbus_thread_->message_loop_proxy();
74 bus_ = new Bus(bus_options);
75 object_proxy_ = bus_->GetObjectProxy(
76 "org.chromium.TestService",
77 ObjectPath("/org/chromium/TestObject"));
78 ASSERT_TRUE(bus_->HasDBusThread());
80 // Create the properties structure
81 properties_.reset(new Properties(
83 base::Bind(&PropertyTest::OnPropertyChanged,
84 base::Unretained(this))));
86 // GetAll is called once the signals are connected.
87 properties_->ConnectSignals();
90 virtual void TearDown() {
91 bus_->ShutdownOnDBusThreadAndBlock();
93 // Shut down the service.
94 test_service_->ShutdownAndBlock();
96 // Reset to the default.
97 base::ThreadRestrictions::SetIOAllowed(true);
99 // Stopping a thread is considered an IO operation, so do this after
101 test_service_->Stop();
104 // Generic callback, bind with a string |id| for passing to
105 // WaitForCallback() to ensure the callback for the right method is
107 void PropertyCallback(const std::string& id, bool success) {
109 message_loop_.Quit();
113 // Called when a property value is updated.
114 void OnPropertyChanged(const std::string& name) {
115 updated_properties_.push_back(name);
116 message_loop_.Quit();
119 // Waits for the given number of updates.
120 void WaitForUpdates(size_t num_updates) {
121 while (updated_properties_.size() < num_updates)
123 for (size_t i = 0; i < num_updates; ++i)
124 updated_properties_.erase(updated_properties_.begin());
127 // Name, Version, Methods, Objects
128 static const int kExpectedSignalUpdates = 5;
130 // Waits for initial values to be set.
131 void WaitForGetAll() {
132 WaitForUpdates(kExpectedSignalUpdates);
135 // Waits for the callback. |id| is the string bound to the callback when
136 // the method call is made that identifies it and distinguishes from any
137 // other; you can set this to whatever you wish.
138 void WaitForCallback(const std::string& id) {
139 while (last_callback_ != id) {
144 base::MessageLoop message_loop_;
145 scoped_ptr<base::Thread> dbus_thread_;
146 scoped_refptr<Bus> bus_;
147 ObjectProxy* object_proxy_;
148 scoped_ptr<Properties> properties_;
149 scoped_ptr<TestService> test_service_;
150 // Properties updated.
151 std::vector<std::string> updated_properties_;
152 // Last callback received.
153 std::string last_callback_;
156 TEST_F(PropertyTest, InitialValues) {
159 EXPECT_EQ("TestService", properties_->name.value());
160 EXPECT_EQ(10, properties_->version.value());
162 std::vector<std::string> methods = properties_->methods.value();
163 ASSERT_EQ(4U, methods.size());
164 EXPECT_EQ("Echo", methods[0]);
165 EXPECT_EQ("SlowEcho", methods[1]);
166 EXPECT_EQ("AsyncEcho", methods[2]);
167 EXPECT_EQ("BrokenMethod", methods[3]);
169 std::vector<ObjectPath> objects = properties_->objects.value();
170 ASSERT_EQ(1U, objects.size());
171 EXPECT_EQ(ObjectPath("/TestObjectPath"), objects[0]);
173 std::vector<uint8> bytes = properties_->bytes.value();
174 ASSERT_EQ(4U, bytes.size());
175 EXPECT_EQ('T', bytes[0]);
176 EXPECT_EQ('e', bytes[1]);
177 EXPECT_EQ('s', bytes[2]);
178 EXPECT_EQ('t', bytes[3]);
181 TEST_F(PropertyTest, UpdatedValues) {
184 // Update the value of the "Name" property, this value should not change.
185 properties_->name.Get(base::Bind(&PropertyTest::PropertyCallback,
186 base::Unretained(this),
188 WaitForCallback("Name");
191 EXPECT_EQ("TestService", properties_->name.value());
193 // Update the value of the "Version" property, this value should be changed.
194 properties_->version.Get(base::Bind(&PropertyTest::PropertyCallback,
195 base::Unretained(this),
197 WaitForCallback("Version");
200 EXPECT_EQ(20, properties_->version.value());
202 // Update the value of the "Methods" property, this value should not change
203 // and should not grow to contain duplicate entries.
204 properties_->methods.Get(base::Bind(&PropertyTest::PropertyCallback,
205 base::Unretained(this),
207 WaitForCallback("Methods");
210 std::vector<std::string> methods = properties_->methods.value();
211 ASSERT_EQ(4U, methods.size());
212 EXPECT_EQ("Echo", methods[0]);
213 EXPECT_EQ("SlowEcho", methods[1]);
214 EXPECT_EQ("AsyncEcho", methods[2]);
215 EXPECT_EQ("BrokenMethod", methods[3]);
217 // Update the value of the "Objects" property, this value should not change
218 // and should not grow to contain duplicate entries.
219 properties_->objects.Get(base::Bind(&PropertyTest::PropertyCallback,
220 base::Unretained(this),
222 WaitForCallback("Objects");
225 std::vector<ObjectPath> objects = properties_->objects.value();
226 ASSERT_EQ(1U, objects.size());
227 EXPECT_EQ(ObjectPath("/TestObjectPath"), objects[0]);
229 // Update the value of the "Bytes" property, this value should not change
230 // and should not grow to contain duplicate entries.
231 properties_->bytes.Get(base::Bind(&PropertyTest::PropertyCallback,
232 base::Unretained(this),
234 WaitForCallback("Bytes");
237 std::vector<uint8> bytes = properties_->bytes.value();
238 ASSERT_EQ(4U, bytes.size());
239 EXPECT_EQ('T', bytes[0]);
240 EXPECT_EQ('e', bytes[1]);
241 EXPECT_EQ('s', bytes[2]);
242 EXPECT_EQ('t', bytes[3]);
245 TEST_F(PropertyTest, Get) {
248 // Ask for the new Version property.
249 properties_->version.Get(base::Bind(&PropertyTest::PropertyCallback,
250 base::Unretained(this),
252 WaitForCallback("Get");
254 // Make sure we got a property update too.
257 EXPECT_EQ(20, properties_->version.value());
260 TEST_F(PropertyTest, Set) {
264 properties_->name.Set("NewService",
265 base::Bind(&PropertyTest::PropertyCallback,
266 base::Unretained(this),
268 WaitForCallback("Set");
270 // TestService sends a property update.
273 EXPECT_EQ("NewService", properties_->name.value());