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.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/property.h"
20 #include "dbus/test_service.h"
21 #include "testing/gtest/include/gtest/gtest.h"
23 // The property test exerises the asynchronous APIs in PropertySet and
25 class PropertyTest
: public testing::Test
{
30 struct Properties
: public dbus::PropertySet
{
31 dbus::Property
<std::string
> name
;
32 dbus::Property
<int16
> version
;
33 dbus::Property
<std::vector
<std::string
> > methods
;
34 dbus::Property
<std::vector
<dbus::ObjectPath
> > objects
;
36 Properties(dbus::ObjectProxy
* object_proxy
,
37 PropertyChangedCallback property_changed_callback
)
38 : dbus::PropertySet(object_proxy
,
39 "org.chromium.TestService",
40 property_changed_callback
) {
41 RegisterProperty("Name", &name
);
42 RegisterProperty("Version", &version
);
43 RegisterProperty("Methods", &methods
);
44 RegisterProperty("Objects", &objects
);
48 virtual void SetUp() {
49 // Make the main thread not to allow IO.
50 base::ThreadRestrictions::SetIOAllowed(false);
52 // Start the D-Bus thread.
53 dbus_thread_
.reset(new base::Thread("D-Bus Thread"));
54 base::Thread::Options thread_options
;
55 thread_options
.message_loop_type
= MessageLoop::TYPE_IO
;
56 ASSERT_TRUE(dbus_thread_
->StartWithOptions(thread_options
));
58 // Start the test service, using the D-Bus thread.
59 dbus::TestService::Options options
;
60 options
.dbus_thread_message_loop_proxy
= dbus_thread_
->message_loop_proxy();
61 test_service_
.reset(new dbus::TestService(options
));
62 ASSERT_TRUE(test_service_
->StartService());
63 ASSERT_TRUE(test_service_
->WaitUntilServiceIsStarted());
64 ASSERT_TRUE(test_service_
->HasDBusThread());
66 // Create the client, using the D-Bus thread.
67 dbus::Bus::Options bus_options
;
68 bus_options
.bus_type
= dbus::Bus::SESSION
;
69 bus_options
.connection_type
= dbus::Bus::PRIVATE
;
70 bus_options
.dbus_thread_message_loop_proxy
=
71 dbus_thread_
->message_loop_proxy();
72 bus_
= new dbus::Bus(bus_options
);
73 object_proxy_
= bus_
->GetObjectProxy(
74 "org.chromium.TestService",
75 dbus::ObjectPath("/org/chromium/TestObject"));
76 ASSERT_TRUE(bus_
->HasDBusThread());
78 // Create the properties structure
79 properties_
.reset(new Properties(
81 base::Bind(&PropertyTest::OnPropertyChanged
,
82 base::Unretained(this))));
83 properties_
->ConnectSignals();
84 properties_
->GetAll();
87 virtual void TearDown() {
88 bus_
->ShutdownOnDBusThreadAndBlock();
90 // Shut down the service.
91 test_service_
->ShutdownAndBlock();
93 // Reset to the default.
94 base::ThreadRestrictions::SetIOAllowed(true);
96 // Stopping a thread is considered an IO operation, so do this after
98 test_service_
->Stop();
101 // Generic callback, bind with a string |id| for passing to
102 // WaitForCallback() to ensure the callback for the right method is
104 void PropertyCallback(const std::string
& id
, bool success
) {
106 message_loop_
.Quit();
110 // Called when a property value is updated.
111 void OnPropertyChanged(const std::string
& name
) {
112 updated_properties_
.push_back(name
);
113 message_loop_
.Quit();
116 // Waits for the given number of updates.
117 void WaitForUpdates(size_t num_updates
) {
118 while (updated_properties_
.size() < num_updates
)
120 for (size_t i
= 0; i
< num_updates
; ++i
)
121 updated_properties_
.erase(updated_properties_
.begin());
124 // Name, Version, Methods, Objects
125 static const int kExpectedSignalUpdates
= 4;
127 // Waits for initial values to be set.
128 void WaitForGetAll() {
129 WaitForUpdates(kExpectedSignalUpdates
);
132 // Waits for the callback. |id| is the string bound to the callback when
133 // the method call is made that identifies it and distinguishes from any
134 // other; you can set this to whatever you wish.
135 void WaitForCallback(const std::string
& id
) {
136 while (last_callback_
!= id
) {
141 MessageLoop message_loop_
;
142 scoped_ptr
<base::Thread
> dbus_thread_
;
143 scoped_refptr
<dbus::Bus
> bus_
;
144 dbus::ObjectProxy
* object_proxy_
;
145 scoped_ptr
<Properties
> properties_
;
146 scoped_ptr
<dbus::TestService
> test_service_
;
147 // Properties updated.
148 std::vector
<std::string
> updated_properties_
;
149 // Last callback received.
150 std::string last_callback_
;
153 TEST_F(PropertyTest
, InitialValues
) {
156 EXPECT_EQ("TestService", properties_
->name
.value());
157 EXPECT_EQ(10, properties_
->version
.value());
159 std::vector
<std::string
> methods
= properties_
->methods
.value();
160 ASSERT_EQ(4U, methods
.size());
161 EXPECT_EQ("Echo", methods
[0]);
162 EXPECT_EQ("SlowEcho", methods
[1]);
163 EXPECT_EQ("AsyncEcho", methods
[2]);
164 EXPECT_EQ("BrokenMethod", methods
[3]);
166 std::vector
<dbus::ObjectPath
> objects
= properties_
->objects
.value();
167 ASSERT_EQ(1U, objects
.size());
168 EXPECT_EQ(dbus::ObjectPath("/TestObjectPath"), objects
[0]);
171 TEST_F(PropertyTest
, UpdatedValues
) {
174 // Update the value of the "Name" property, this value should not change.
175 properties_
->name
.Get(base::Bind(&PropertyTest::PropertyCallback
,
176 base::Unretained(this),
178 WaitForCallback("Name");
181 EXPECT_EQ("TestService", properties_
->name
.value());
183 // Update the value of the "Version" property, this value should be changed.
184 properties_
->version
.Get(base::Bind(&PropertyTest::PropertyCallback
,
185 base::Unretained(this),
187 WaitForCallback("Version");
190 EXPECT_EQ(20, properties_
->version
.value());
192 // Update the value of the "Methods" property, this value should not change
193 // and should not grow to contain duplicate entries.
194 properties_
->methods
.Get(base::Bind(&PropertyTest::PropertyCallback
,
195 base::Unretained(this),
197 WaitForCallback("Methods");
200 std::vector
<std::string
> methods
= properties_
->methods
.value();
201 ASSERT_EQ(4U, methods
.size());
202 EXPECT_EQ("Echo", methods
[0]);
203 EXPECT_EQ("SlowEcho", methods
[1]);
204 EXPECT_EQ("AsyncEcho", methods
[2]);
205 EXPECT_EQ("BrokenMethod", methods
[3]);
207 // Update the value of the "Objects" property, this value should not change
208 // and should not grow to contain duplicate entries.
209 properties_
->objects
.Get(base::Bind(&PropertyTest::PropertyCallback
,
210 base::Unretained(this),
212 WaitForCallback("Objects");
215 std::vector
<dbus::ObjectPath
> objects
= properties_
->objects
.value();
216 ASSERT_EQ(1U, objects
.size());
217 EXPECT_EQ(dbus::ObjectPath("/TestObjectPath"), objects
[0]);
220 TEST_F(PropertyTest
, Get
) {
223 // Ask for the new Version property.
224 properties_
->version
.Get(base::Bind(&PropertyTest::PropertyCallback
,
225 base::Unretained(this),
227 WaitForCallback("Get");
229 // Make sure we got a property update too.
232 EXPECT_EQ(20, properties_
->version
.value());
235 TEST_F(PropertyTest
, Set
) {
239 properties_
->name
.Set("NewService",
240 base::Bind(&PropertyTest::PropertyCallback
,
241 base::Unretained(this),
243 WaitForCallback("Set");
245 // TestService sends a property update.
248 EXPECT_EQ("NewService", properties_
->name
.value());