Fix revert "[chromeos] Remove dependencies of StatisticsProvider on chrome."
[chromium-blink-merge.git] / dbus / property_unittest.cc
blobed5bfc3558a2133c077b67c5e1ad0f333c7083d2
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"
7 #include <string>
8 #include <vector>
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"
16 #include "dbus/bus.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"
22 // The property test exerises the asynchronous APIs in PropertySet and
23 // Property<>.
24 class PropertyTest : public testing::Test {
25 public:
26 PropertyTest() {
29 struct Properties : public dbus::PropertySet {
30 dbus::Property<std::string> name;
31 dbus::Property<int16> version;
32 dbus::Property<std::vector<std::string> > methods;
33 dbus::Property<std::vector<dbus::ObjectPath> > objects;
35 Properties(dbus::ObjectProxy* object_proxy,
36 PropertyChangedCallback property_changed_callback)
37 : dbus::PropertySet(object_proxy,
38 "org.chromium.TestInterface",
39 property_changed_callback) {
40 RegisterProperty("Name", &name);
41 RegisterProperty("Version", &version);
42 RegisterProperty("Methods", &methods);
43 RegisterProperty("Objects", &objects);
47 virtual void SetUp() {
48 // Make the main thread not to allow IO.
49 base::ThreadRestrictions::SetIOAllowed(false);
51 // Start the D-Bus thread.
52 dbus_thread_.reset(new base::Thread("D-Bus Thread"));
53 base::Thread::Options thread_options;
54 thread_options.message_loop_type = base::MessageLoop::TYPE_IO;
55 ASSERT_TRUE(dbus_thread_->StartWithOptions(thread_options));
57 // Start the test service, using the D-Bus thread.
58 dbus::TestService::Options options;
59 options.dbus_task_runner = dbus_thread_->message_loop_proxy();
60 test_service_.reset(new dbus::TestService(options));
61 ASSERT_TRUE(test_service_->StartService());
62 ASSERT_TRUE(test_service_->WaitUntilServiceIsStarted());
63 ASSERT_TRUE(test_service_->HasDBusThread());
65 // Create the client, using the D-Bus thread.
66 dbus::Bus::Options bus_options;
67 bus_options.bus_type = dbus::Bus::SESSION;
68 bus_options.connection_type = dbus::Bus::PRIVATE;
69 bus_options.dbus_task_runner = dbus_thread_->message_loop_proxy();
70 bus_ = new dbus::Bus(bus_options);
71 object_proxy_ = bus_->GetObjectProxy(
72 "org.chromium.TestService",
73 dbus::ObjectPath("/org/chromium/TestObject"));
74 ASSERT_TRUE(bus_->HasDBusThread());
76 // Create the properties structure
77 properties_.reset(new Properties(
78 object_proxy_,
79 base::Bind(&PropertyTest::OnPropertyChanged,
80 base::Unretained(this))));
81 properties_->ConnectSignals();
82 properties_->GetAll();
85 virtual void TearDown() {
86 bus_->ShutdownOnDBusThreadAndBlock();
88 // Shut down the service.
89 test_service_->ShutdownAndBlock();
91 // Reset to the default.
92 base::ThreadRestrictions::SetIOAllowed(true);
94 // Stopping a thread is considered an IO operation, so do this after
95 // allowing IO.
96 test_service_->Stop();
99 // Generic callback, bind with a string |id| for passing to
100 // WaitForCallback() to ensure the callback for the right method is
101 // waited for.
102 void PropertyCallback(const std::string& id, bool success) {
103 last_callback_ = id;
104 message_loop_.Quit();
107 protected:
108 // Called when a property value is updated.
109 void OnPropertyChanged(const std::string& name) {
110 updated_properties_.push_back(name);
111 message_loop_.Quit();
114 // Waits for the given number of updates.
115 void WaitForUpdates(size_t num_updates) {
116 while (updated_properties_.size() < num_updates)
117 message_loop_.Run();
118 for (size_t i = 0; i < num_updates; ++i)
119 updated_properties_.erase(updated_properties_.begin());
122 // Name, Version, Methods, Objects
123 static const int kExpectedSignalUpdates = 4;
125 // Waits for initial values to be set.
126 void WaitForGetAll() {
127 WaitForUpdates(kExpectedSignalUpdates);
130 // Waits for the callback. |id| is the string bound to the callback when
131 // the method call is made that identifies it and distinguishes from any
132 // other; you can set this to whatever you wish.
133 void WaitForCallback(const std::string& id) {
134 while (last_callback_ != id) {
135 message_loop_.Run();
139 base::MessageLoop message_loop_;
140 scoped_ptr<base::Thread> dbus_thread_;
141 scoped_refptr<dbus::Bus> bus_;
142 dbus::ObjectProxy* object_proxy_;
143 scoped_ptr<Properties> properties_;
144 scoped_ptr<dbus::TestService> test_service_;
145 // Properties updated.
146 std::vector<std::string> updated_properties_;
147 // Last callback received.
148 std::string last_callback_;
151 TEST_F(PropertyTest, InitialValues) {
152 WaitForGetAll();
154 EXPECT_EQ("TestService", properties_->name.value());
155 EXPECT_EQ(10, properties_->version.value());
157 std::vector<std::string> methods = properties_->methods.value();
158 ASSERT_EQ(4U, methods.size());
159 EXPECT_EQ("Echo", methods[0]);
160 EXPECT_EQ("SlowEcho", methods[1]);
161 EXPECT_EQ("AsyncEcho", methods[2]);
162 EXPECT_EQ("BrokenMethod", methods[3]);
164 std::vector<dbus::ObjectPath> objects = properties_->objects.value();
165 ASSERT_EQ(1U, objects.size());
166 EXPECT_EQ(dbus::ObjectPath("/TestObjectPath"), objects[0]);
169 TEST_F(PropertyTest, UpdatedValues) {
170 WaitForGetAll();
172 // Update the value of the "Name" property, this value should not change.
173 properties_->name.Get(base::Bind(&PropertyTest::PropertyCallback,
174 base::Unretained(this),
175 "Name"));
176 WaitForCallback("Name");
177 WaitForUpdates(1);
179 EXPECT_EQ("TestService", properties_->name.value());
181 // Update the value of the "Version" property, this value should be changed.
182 properties_->version.Get(base::Bind(&PropertyTest::PropertyCallback,
183 base::Unretained(this),
184 "Version"));
185 WaitForCallback("Version");
186 WaitForUpdates(1);
188 EXPECT_EQ(20, properties_->version.value());
190 // Update the value of the "Methods" property, this value should not change
191 // and should not grow to contain duplicate entries.
192 properties_->methods.Get(base::Bind(&PropertyTest::PropertyCallback,
193 base::Unretained(this),
194 "Methods"));
195 WaitForCallback("Methods");
196 WaitForUpdates(1);
198 std::vector<std::string> methods = properties_->methods.value();
199 ASSERT_EQ(4U, methods.size());
200 EXPECT_EQ("Echo", methods[0]);
201 EXPECT_EQ("SlowEcho", methods[1]);
202 EXPECT_EQ("AsyncEcho", methods[2]);
203 EXPECT_EQ("BrokenMethod", methods[3]);
205 // Update the value of the "Objects" property, this value should not change
206 // and should not grow to contain duplicate entries.
207 properties_->objects.Get(base::Bind(&PropertyTest::PropertyCallback,
208 base::Unretained(this),
209 "Objects"));
210 WaitForCallback("Objects");
211 WaitForUpdates(1);
213 std::vector<dbus::ObjectPath> objects = properties_->objects.value();
214 ASSERT_EQ(1U, objects.size());
215 EXPECT_EQ(dbus::ObjectPath("/TestObjectPath"), objects[0]);
218 TEST_F(PropertyTest, Get) {
219 WaitForGetAll();
221 // Ask for the new Version property.
222 properties_->version.Get(base::Bind(&PropertyTest::PropertyCallback,
223 base::Unretained(this),
224 "Get"));
225 WaitForCallback("Get");
227 // Make sure we got a property update too.
228 WaitForUpdates(1);
230 EXPECT_EQ(20, properties_->version.value());
233 TEST_F(PropertyTest, Set) {
234 WaitForGetAll();
236 // Set a new name.
237 properties_->name.Set("NewService",
238 base::Bind(&PropertyTest::PropertyCallback,
239 base::Unretained(this),
240 "Set"));
241 WaitForCallback("Set");
243 // TestService sends a property update.
244 WaitForUpdates(1);
246 EXPECT_EQ("NewService", properties_->name.value());