blink-test: Add enterRunLoop() and exitRunLoop() in TestBlinkWebUnitTestSupport.
[chromium-blink-merge.git] / dbus / property_unittest.cc
blobc7df1ce9358ddba2f1c1d98a431edc09370dae3b
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/message_loop.h"
14 #include "base/run_loop.h"
15 #include "base/strings/string_number_conversions.h"
16 #include "base/threading/thread.h"
17 #include "base/threading/thread_restrictions.h"
18 #include "dbus/bus.h"
19 #include "dbus/object_path.h"
20 #include "dbus/object_proxy.h"
21 #include "dbus/test_service.h"
22 #include "testing/gtest/include/gtest/gtest.h"
24 namespace dbus {
26 // The property test exerises the asynchronous APIs in PropertySet and
27 // Property<>.
28 class PropertyTest : public testing::Test {
29 public:
30 PropertyTest() {
33 struct Properties : public PropertySet {
34 Property<std::string> name;
35 Property<int16> version;
36 Property<std::vector<std::string> > methods;
37 Property<std::vector<ObjectPath> > objects;
38 Property<std::vector<uint8> > bytes;
40 Properties(ObjectProxy* object_proxy,
41 PropertyChangedCallback property_changed_callback)
42 : PropertySet(object_proxy,
43 "org.chromium.TestInterface",
44 property_changed_callback) {
45 RegisterProperty("Name", &name);
46 RegisterProperty("Version", &version);
47 RegisterProperty("Methods", &methods);
48 RegisterProperty("Objects", &objects);
49 RegisterProperty("Bytes", &bytes);
53 void SetUp() override {
54 // Make the main thread not to allow IO.
55 base::ThreadRestrictions::SetIOAllowed(false);
57 // Start the D-Bus thread.
58 dbus_thread_.reset(new base::Thread("D-Bus Thread"));
59 base::Thread::Options thread_options;
60 thread_options.message_loop_type = base::MessageLoop::TYPE_IO;
61 ASSERT_TRUE(dbus_thread_->StartWithOptions(thread_options));
63 // Start the test service, using the D-Bus thread.
64 TestService::Options options;
65 options.dbus_task_runner = dbus_thread_->message_loop_proxy();
66 test_service_.reset(new TestService(options));
67 ASSERT_TRUE(test_service_->StartService());
68 ASSERT_TRUE(test_service_->WaitUntilServiceIsStarted());
69 ASSERT_TRUE(test_service_->HasDBusThread());
71 // Create the client, using the D-Bus thread.
72 Bus::Options bus_options;
73 bus_options.bus_type = Bus::SESSION;
74 bus_options.connection_type = Bus::PRIVATE;
75 bus_options.dbus_task_runner = dbus_thread_->message_loop_proxy();
76 bus_ = new Bus(bus_options);
77 object_proxy_ = bus_->GetObjectProxy(
78 "org.chromium.TestService",
79 ObjectPath("/org/chromium/TestObject"));
80 ASSERT_TRUE(bus_->HasDBusThread());
82 // Create the properties structure
83 properties_.reset(new Properties(
84 object_proxy_,
85 base::Bind(&PropertyTest::OnPropertyChanged,
86 base::Unretained(this))));
87 properties_->ConnectSignals();
88 properties_->GetAll();
91 void TearDown() override {
92 bus_->ShutdownOnDBusThreadAndBlock();
94 // Shut down the service.
95 test_service_->ShutdownAndBlock();
97 // Reset to the default.
98 base::ThreadRestrictions::SetIOAllowed(true);
100 // Stopping a thread is considered an IO operation, so do this after
101 // allowing IO.
102 test_service_->Stop();
105 // Generic callback, bind with a string |id| for passing to
106 // WaitForCallback() to ensure the callback for the right method is
107 // waited for.
108 void PropertyCallback(const std::string& id, bool success) {
109 last_callback_ = id;
110 run_loop_->Quit();
113 protected:
114 // Called when a property value is updated.
115 void OnPropertyChanged(const std::string& name) {
116 updated_properties_.push_back(name);
117 run_loop_->Quit();
120 // Waits for the given number of updates.
121 void WaitForUpdates(size_t num_updates) {
122 while (updated_properties_.size() < num_updates) {
123 run_loop_.reset(new base::RunLoop);
124 run_loop_->Run();
126 for (size_t i = 0; i < num_updates; ++i)
127 updated_properties_.erase(updated_properties_.begin());
130 // Name, Version, Methods, Objects
131 static const int kExpectedSignalUpdates = 5;
133 // Waits for initial values to be set.
134 void WaitForGetAll() {
135 WaitForUpdates(kExpectedSignalUpdates);
138 // Waits for the callback. |id| is the string bound to the callback when
139 // the method call is made that identifies it and distinguishes from any
140 // other; you can set this to whatever you wish.
141 void WaitForCallback(const std::string& id) {
142 while (last_callback_ != id) {
143 run_loop_.reset(new base::RunLoop);
144 run_loop_->Run();
148 base::MessageLoop message_loop_;
149 scoped_ptr<base::RunLoop> run_loop_;
150 scoped_ptr<base::Thread> dbus_thread_;
151 scoped_refptr<Bus> bus_;
152 ObjectProxy* object_proxy_;
153 scoped_ptr<Properties> properties_;
154 scoped_ptr<TestService> test_service_;
155 // Properties updated.
156 std::vector<std::string> updated_properties_;
157 // Last callback received.
158 std::string last_callback_;
161 TEST_F(PropertyTest, InitialValues) {
162 WaitForGetAll();
164 EXPECT_EQ("TestService", properties_->name.value());
165 EXPECT_EQ(10, properties_->version.value());
167 std::vector<std::string> methods = properties_->methods.value();
168 ASSERT_EQ(4U, methods.size());
169 EXPECT_EQ("Echo", methods[0]);
170 EXPECT_EQ("SlowEcho", methods[1]);
171 EXPECT_EQ("AsyncEcho", methods[2]);
172 EXPECT_EQ("BrokenMethod", methods[3]);
174 std::vector<ObjectPath> objects = properties_->objects.value();
175 ASSERT_EQ(1U, objects.size());
176 EXPECT_EQ(ObjectPath("/TestObjectPath"), objects[0]);
178 std::vector<uint8> bytes = properties_->bytes.value();
179 ASSERT_EQ(4U, bytes.size());
180 EXPECT_EQ('T', bytes[0]);
181 EXPECT_EQ('e', bytes[1]);
182 EXPECT_EQ('s', bytes[2]);
183 EXPECT_EQ('t', bytes[3]);
186 TEST_F(PropertyTest, UpdatedValues) {
187 WaitForGetAll();
189 // Update the value of the "Name" property, this value should not change.
190 properties_->name.Get(base::Bind(&PropertyTest::PropertyCallback,
191 base::Unretained(this),
192 "Name"));
193 WaitForCallback("Name");
194 WaitForUpdates(1);
196 EXPECT_EQ("TestService", properties_->name.value());
198 // Update the value of the "Version" property, this value should be changed.
199 properties_->version.Get(base::Bind(&PropertyTest::PropertyCallback,
200 base::Unretained(this),
201 "Version"));
202 WaitForCallback("Version");
203 WaitForUpdates(1);
205 EXPECT_EQ(20, properties_->version.value());
207 // Update the value of the "Methods" property, this value should not change
208 // and should not grow to contain duplicate entries.
209 properties_->methods.Get(base::Bind(&PropertyTest::PropertyCallback,
210 base::Unretained(this),
211 "Methods"));
212 WaitForCallback("Methods");
213 WaitForUpdates(1);
215 std::vector<std::string> methods = properties_->methods.value();
216 ASSERT_EQ(4U, methods.size());
217 EXPECT_EQ("Echo", methods[0]);
218 EXPECT_EQ("SlowEcho", methods[1]);
219 EXPECT_EQ("AsyncEcho", methods[2]);
220 EXPECT_EQ("BrokenMethod", methods[3]);
222 // Update the value of the "Objects" property, this value should not change
223 // and should not grow to contain duplicate entries.
224 properties_->objects.Get(base::Bind(&PropertyTest::PropertyCallback,
225 base::Unretained(this),
226 "Objects"));
227 WaitForCallback("Objects");
228 WaitForUpdates(1);
230 std::vector<ObjectPath> objects = properties_->objects.value();
231 ASSERT_EQ(1U, objects.size());
232 EXPECT_EQ(ObjectPath("/TestObjectPath"), objects[0]);
234 // Update the value of the "Bytes" property, this value should not change
235 // and should not grow to contain duplicate entries.
236 properties_->bytes.Get(base::Bind(&PropertyTest::PropertyCallback,
237 base::Unretained(this),
238 "Bytes"));
239 WaitForCallback("Bytes");
240 WaitForUpdates(1);
242 std::vector<uint8> bytes = properties_->bytes.value();
243 ASSERT_EQ(4U, bytes.size());
244 EXPECT_EQ('T', bytes[0]);
245 EXPECT_EQ('e', bytes[1]);
246 EXPECT_EQ('s', bytes[2]);
247 EXPECT_EQ('t', bytes[3]);
250 TEST_F(PropertyTest, Get) {
251 WaitForGetAll();
253 // Ask for the new Version property.
254 properties_->version.Get(base::Bind(&PropertyTest::PropertyCallback,
255 base::Unretained(this),
256 "Get"));
257 WaitForCallback("Get");
259 // Make sure we got a property update too.
260 WaitForUpdates(1);
262 EXPECT_EQ(20, properties_->version.value());
265 TEST_F(PropertyTest, Set) {
266 WaitForGetAll();
268 // Set a new name.
269 properties_->name.Set("NewService",
270 base::Bind(&PropertyTest::PropertyCallback,
271 base::Unretained(this),
272 "Set"));
273 WaitForCallback("Set");
275 // TestService sends a property update.
276 WaitForUpdates(1);
278 EXPECT_EQ("NewService", properties_->name.value());
281 TEST(PropertyTestStatic, ReadWriteStringMap) {
282 scoped_ptr<Response> message(Response::CreateEmpty());
283 MessageWriter writer(message.get());
284 MessageWriter variant_writer(NULL);
285 MessageWriter variant_array_writer(NULL);
286 MessageWriter struct_entry_writer(NULL);
288 writer.OpenVariant("a{ss}", &variant_writer);
289 variant_writer.OpenArray("{ss}", &variant_array_writer);
290 const char* items[] = {"One", "Two", "Three", "Four"};
291 for (unsigned i = 0; i < arraysize(items); ++i) {
292 variant_array_writer.OpenDictEntry(&struct_entry_writer);
293 struct_entry_writer.AppendString(items[i]);
294 struct_entry_writer.AppendString(base::UintToString(i + 1));
295 variant_array_writer.CloseContainer(&struct_entry_writer);
297 variant_writer.CloseContainer(&variant_array_writer);
298 writer.CloseContainer(&variant_writer);
300 MessageReader reader(message.get());
301 Property<std::map<std::string, std::string>> string_map;
302 EXPECT_TRUE(string_map.PopValueFromReader(&reader));
303 ASSERT_EQ(4U, string_map.value().size());
304 EXPECT_EQ("1", string_map.value().at("One"));
305 EXPECT_EQ("2", string_map.value().at("Two"));
306 EXPECT_EQ("3", string_map.value().at("Three"));
307 EXPECT_EQ("4", string_map.value().at("Four"));
310 TEST(PropertyTestStatic, SerializeStringMap) {
311 std::map<std::string, std::string> test_map;
312 test_map["Hi"] = "There";
313 test_map["Map"] = "Test";
314 test_map["Random"] = "Text";
316 scoped_ptr<Response> message(Response::CreateEmpty());
317 MessageWriter writer(message.get());
319 Property<std::map<std::string, std::string>> string_map;
320 string_map.ReplaceSetValueForTesting(test_map);
321 string_map.AppendSetValueToWriter(&writer);
323 MessageReader reader(message.get());
324 EXPECT_TRUE(string_map.PopValueFromReader(&reader));
325 EXPECT_EQ(test_map, string_map.value());
328 TEST(PropertyTestStatic, ReadWriteNetAddressArray) {
329 scoped_ptr<Response> message(Response::CreateEmpty());
330 MessageWriter writer(message.get());
331 MessageWriter variant_writer(NULL);
332 MessageWriter variant_array_writer(NULL);
333 MessageWriter struct_entry_writer(NULL);
335 writer.OpenVariant("a(ayq)", &variant_writer);
336 variant_writer.OpenArray("(ayq)", &variant_array_writer);
337 uint8 ip_bytes[] = {0x54, 0x65, 0x73, 0x74, 0x30};
338 for (uint16 i = 0; i < 5; ++i) {
339 variant_array_writer.OpenStruct(&struct_entry_writer);
340 ip_bytes[4] = 0x30 + i;
341 struct_entry_writer.AppendArrayOfBytes(ip_bytes, arraysize(ip_bytes));
342 struct_entry_writer.AppendUint16(i);
343 variant_array_writer.CloseContainer(&struct_entry_writer);
345 variant_writer.CloseContainer(&variant_array_writer);
346 writer.CloseContainer(&variant_writer);
348 MessageReader reader(message.get());
349 Property<std::vector<std::pair<std::vector<uint8>, uint16>>> ip_list;
350 EXPECT_TRUE(ip_list.PopValueFromReader(&reader));
352 ASSERT_EQ(5U, ip_list.value().size());
353 size_t item_index = 0;
354 for (auto& item : ip_list.value()) {
355 ASSERT_EQ(5U, item.first.size());
356 ip_bytes[4] = 0x30 + item_index;
357 EXPECT_EQ(0, memcmp(ip_bytes, item.first.data(), 5U));
358 EXPECT_EQ(item_index, item.second);
359 ++item_index;
363 TEST(PropertyTestStatic, SerializeNetAddressArray) {
364 std::vector<std::pair<std::vector<uint8>, uint16>> test_list;
366 uint8 ip_bytes[] = {0x54, 0x65, 0x73, 0x74, 0x30};
367 for (uint16 i = 0; i < 5; ++i) {
368 ip_bytes[4] = 0x30 + i;
369 std::vector<uint8> bytes(ip_bytes, ip_bytes + arraysize(ip_bytes));
370 test_list.push_back(make_pair(bytes, 16));
373 scoped_ptr<Response> message(Response::CreateEmpty());
374 MessageWriter writer(message.get());
376 Property<std::vector<std::pair<std::vector<uint8>, uint16>>> ip_list;
377 ip_list.ReplaceSetValueForTesting(test_list);
378 ip_list.AppendSetValueToWriter(&writer);
380 MessageReader reader(message.get());
381 EXPECT_TRUE(ip_list.PopValueFromReader(&reader));
382 EXPECT_EQ(test_list, ip_list.value());
385 } // namespace dbus