Avoid creating 0-byte SharedMemory in OutputDeviceBacking::GetSharedMemory()
[chromium-blink-merge.git] / dbus / property_unittest.cc
blob5208f848915365f1d9f008bc045c522d6e3a435f
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() {}
32 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;
37 Property<std::vector<uint8> > bytes;
39 Properties(ObjectProxy* object_proxy,
40 PropertyChangedCallback property_changed_callback)
41 : PropertySet(object_proxy,
42 "org.chromium.TestInterface",
43 property_changed_callback) {
44 RegisterProperty("Name", &name);
45 RegisterProperty("Version", &version);
46 RegisterProperty("Methods", &methods);
47 RegisterProperty("Objects", &objects);
48 RegisterProperty("Bytes", &bytes);
52 void SetUp() override {
53 // Make the main thread not to allow IO.
54 base::ThreadRestrictions::SetIOAllowed(false);
56 // Start the D-Bus thread.
57 dbus_thread_.reset(new base::Thread("D-Bus Thread"));
58 base::Thread::Options thread_options;
59 thread_options.message_loop_type = base::MessageLoop::TYPE_IO;
60 ASSERT_TRUE(dbus_thread_->StartWithOptions(thread_options));
62 // Start the test service, using the D-Bus thread.
63 TestService::Options options;
64 options.dbus_task_runner = dbus_thread_->message_loop_proxy();
65 test_service_.reset(new TestService(options));
66 ASSERT_TRUE(test_service_->StartService());
67 ASSERT_TRUE(test_service_->WaitUntilServiceIsStarted());
68 ASSERT_TRUE(test_service_->HasDBusThread());
70 // Create the client, using the D-Bus thread.
71 Bus::Options bus_options;
72 bus_options.bus_type = Bus::SESSION;
73 bus_options.connection_type = Bus::PRIVATE;
74 bus_options.dbus_task_runner = dbus_thread_->message_loop_proxy();
75 bus_ = new Bus(bus_options);
76 object_proxy_ = bus_->GetObjectProxy(
77 "org.chromium.TestService",
78 ObjectPath("/org/chromium/TestObject"));
79 ASSERT_TRUE(bus_->HasDBusThread());
81 // Create the properties structure
82 properties_.reset(new Properties(
83 object_proxy_,
84 base::Bind(&PropertyTest::OnPropertyChanged,
85 base::Unretained(this))));
86 properties_->ConnectSignals();
87 properties_->GetAll();
90 void TearDown() override {
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
100 // allowing IO.
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
106 // waited for.
107 void PropertyCallback(const std::string& id, bool success) {
108 last_callback_ = id;
109 run_loop_->Quit();
112 // Generic method callback, that might be used together with
113 // WaitForMethodCallback to test wether method was succesfully called.
114 void MethodCallback(Response* response) { run_loop_->Quit(); }
116 protected:
117 // Called when a property value is updated.
118 void OnPropertyChanged(const std::string& name) {
119 updated_properties_.push_back(name);
120 run_loop_->Quit();
123 // Waits for the given number of updates.
124 void WaitForUpdates(size_t num_updates) {
125 while (updated_properties_.size() < num_updates) {
126 run_loop_.reset(new base::RunLoop);
127 run_loop_->Run();
129 for (size_t i = 0; i < num_updates; ++i)
130 updated_properties_.erase(updated_properties_.begin());
133 // Name, Version, Methods, Objects
134 static const int kExpectedSignalUpdates = 5;
136 // Waits for initial values to be set.
137 void WaitForGetAll() {
138 WaitForUpdates(kExpectedSignalUpdates);
141 // Waits until MethodCallback is called.
142 void WaitForMethodCallback() {
143 run_loop_.reset(new base::RunLoop);
144 run_loop_->Run();
147 // Waits for the callback. |id| is the string bound to the callback when
148 // the method call is made that identifies it and distinguishes from any
149 // other; you can set this to whatever you wish.
150 void WaitForCallback(const std::string& id) {
151 while (last_callback_ != id) {
152 run_loop_.reset(new base::RunLoop);
153 run_loop_->Run();
157 base::MessageLoop message_loop_;
158 scoped_ptr<base::RunLoop> run_loop_;
159 scoped_ptr<base::Thread> dbus_thread_;
160 scoped_refptr<Bus> bus_;
161 ObjectProxy* object_proxy_;
162 scoped_ptr<Properties> properties_;
163 scoped_ptr<TestService> test_service_;
164 // Properties updated.
165 std::vector<std::string> updated_properties_;
166 // Last callback received.
167 std::string last_callback_;
170 TEST_F(PropertyTest, InitialValues) {
171 EXPECT_FALSE(properties_->name.is_valid());
172 EXPECT_FALSE(properties_->version.is_valid());
174 WaitForGetAll();
176 EXPECT_TRUE(properties_->name.is_valid());
177 EXPECT_EQ("TestService", properties_->name.value());
178 EXPECT_TRUE(properties_->version.is_valid());
179 EXPECT_EQ(10, properties_->version.value());
181 std::vector<std::string> methods = properties_->methods.value();
182 ASSERT_EQ(4U, methods.size());
183 EXPECT_EQ("Echo", methods[0]);
184 EXPECT_EQ("SlowEcho", methods[1]);
185 EXPECT_EQ("AsyncEcho", methods[2]);
186 EXPECT_EQ("BrokenMethod", methods[3]);
188 std::vector<ObjectPath> objects = properties_->objects.value();
189 ASSERT_EQ(1U, objects.size());
190 EXPECT_EQ(ObjectPath("/TestObjectPath"), objects[0]);
192 std::vector<uint8> bytes = properties_->bytes.value();
193 ASSERT_EQ(4U, bytes.size());
194 EXPECT_EQ('T', bytes[0]);
195 EXPECT_EQ('e', bytes[1]);
196 EXPECT_EQ('s', bytes[2]);
197 EXPECT_EQ('t', bytes[3]);
200 TEST_F(PropertyTest, UpdatedValues) {
201 WaitForGetAll();
203 // Update the value of the "Name" property, this value should not change.
204 properties_->name.Get(base::Bind(&PropertyTest::PropertyCallback,
205 base::Unretained(this),
206 "Name"));
207 WaitForCallback("Name");
208 WaitForUpdates(1);
210 EXPECT_EQ("TestService", properties_->name.value());
212 // Update the value of the "Version" property, this value should be changed.
213 properties_->version.Get(base::Bind(&PropertyTest::PropertyCallback,
214 base::Unretained(this),
215 "Version"));
216 WaitForCallback("Version");
217 WaitForUpdates(1);
219 EXPECT_EQ(20, properties_->version.value());
221 // Update the value of the "Methods" property, this value should not change
222 // and should not grow to contain duplicate entries.
223 properties_->methods.Get(base::Bind(&PropertyTest::PropertyCallback,
224 base::Unretained(this),
225 "Methods"));
226 WaitForCallback("Methods");
227 WaitForUpdates(1);
229 std::vector<std::string> methods = properties_->methods.value();
230 ASSERT_EQ(4U, methods.size());
231 EXPECT_EQ("Echo", methods[0]);
232 EXPECT_EQ("SlowEcho", methods[1]);
233 EXPECT_EQ("AsyncEcho", methods[2]);
234 EXPECT_EQ("BrokenMethod", methods[3]);
236 // Update the value of the "Objects" property, this value should not change
237 // and should not grow to contain duplicate entries.
238 properties_->objects.Get(base::Bind(&PropertyTest::PropertyCallback,
239 base::Unretained(this),
240 "Objects"));
241 WaitForCallback("Objects");
242 WaitForUpdates(1);
244 std::vector<ObjectPath> objects = properties_->objects.value();
245 ASSERT_EQ(1U, objects.size());
246 EXPECT_EQ(ObjectPath("/TestObjectPath"), objects[0]);
248 // Update the value of the "Bytes" property, this value should not change
249 // and should not grow to contain duplicate entries.
250 properties_->bytes.Get(base::Bind(&PropertyTest::PropertyCallback,
251 base::Unretained(this),
252 "Bytes"));
253 WaitForCallback("Bytes");
254 WaitForUpdates(1);
256 std::vector<uint8> bytes = properties_->bytes.value();
257 ASSERT_EQ(4U, bytes.size());
258 EXPECT_EQ('T', bytes[0]);
259 EXPECT_EQ('e', bytes[1]);
260 EXPECT_EQ('s', bytes[2]);
261 EXPECT_EQ('t', bytes[3]);
264 TEST_F(PropertyTest, Get) {
265 WaitForGetAll();
267 // Ask for the new Version property.
268 properties_->version.Get(base::Bind(&PropertyTest::PropertyCallback,
269 base::Unretained(this),
270 "Get"));
271 WaitForCallback("Get");
273 // Make sure we got a property update too.
274 WaitForUpdates(1);
276 EXPECT_EQ(20, properties_->version.value());
279 TEST_F(PropertyTest, Set) {
280 WaitForGetAll();
282 // Set a new name.
283 properties_->name.Set("NewService",
284 base::Bind(&PropertyTest::PropertyCallback,
285 base::Unretained(this),
286 "Set"));
287 WaitForCallback("Set");
289 // TestService sends a property update.
290 WaitForUpdates(1);
292 EXPECT_EQ("NewService", properties_->name.value());
295 TEST_F(PropertyTest, Invalidate) {
296 WaitForGetAll();
298 EXPECT_TRUE(properties_->name.is_valid());
300 // Invalidate name.
301 MethodCall method_call("org.chromium.TestInterface", "PerformAction");
302 MessageWriter writer(&method_call);
303 writer.AppendString("InvalidateProperty");
304 writer.AppendObjectPath(ObjectPath("/org/chromium/TestService"));
305 object_proxy_->CallMethod(
306 &method_call, ObjectProxy::TIMEOUT_USE_DEFAULT,
307 base::Bind(&PropertyTest::MethodCallback, base::Unretained(this)));
308 WaitForMethodCallback();
310 // TestService sends a property update.
311 WaitForUpdates(1);
313 EXPECT_FALSE(properties_->name.is_valid());
315 // Set name to something valid.
316 properties_->name.Set("NewService",
317 base::Bind(&PropertyTest::PropertyCallback,
318 base::Unretained(this), "Set"));
319 WaitForCallback("Set");
321 // TestService sends a property update.
322 WaitForUpdates(1);
324 EXPECT_TRUE(properties_->name.is_valid());
327 TEST(PropertyTestStatic, ReadWriteStringMap) {
328 scoped_ptr<Response> message(Response::CreateEmpty());
329 MessageWriter writer(message.get());
330 MessageWriter variant_writer(NULL);
331 MessageWriter variant_array_writer(NULL);
332 MessageWriter struct_entry_writer(NULL);
334 writer.OpenVariant("a{ss}", &variant_writer);
335 variant_writer.OpenArray("{ss}", &variant_array_writer);
336 const char* items[] = {"One", "Two", "Three", "Four"};
337 for (unsigned i = 0; i < arraysize(items); ++i) {
338 variant_array_writer.OpenDictEntry(&struct_entry_writer);
339 struct_entry_writer.AppendString(items[i]);
340 struct_entry_writer.AppendString(base::UintToString(i + 1));
341 variant_array_writer.CloseContainer(&struct_entry_writer);
343 variant_writer.CloseContainer(&variant_array_writer);
344 writer.CloseContainer(&variant_writer);
346 MessageReader reader(message.get());
347 Property<std::map<std::string, std::string>> string_map;
348 EXPECT_TRUE(string_map.PopValueFromReader(&reader));
349 ASSERT_EQ(4U, string_map.value().size());
350 EXPECT_EQ("1", string_map.value().at("One"));
351 EXPECT_EQ("2", string_map.value().at("Two"));
352 EXPECT_EQ("3", string_map.value().at("Three"));
353 EXPECT_EQ("4", string_map.value().at("Four"));
356 TEST(PropertyTestStatic, SerializeStringMap) {
357 std::map<std::string, std::string> test_map;
358 test_map["Hi"] = "There";
359 test_map["Map"] = "Test";
360 test_map["Random"] = "Text";
362 scoped_ptr<Response> message(Response::CreateEmpty());
363 MessageWriter writer(message.get());
365 Property<std::map<std::string, std::string>> string_map;
366 string_map.ReplaceSetValueForTesting(test_map);
367 string_map.AppendSetValueToWriter(&writer);
369 MessageReader reader(message.get());
370 EXPECT_TRUE(string_map.PopValueFromReader(&reader));
371 EXPECT_EQ(test_map, string_map.value());
374 TEST(PropertyTestStatic, ReadWriteNetAddressArray) {
375 scoped_ptr<Response> message(Response::CreateEmpty());
376 MessageWriter writer(message.get());
377 MessageWriter variant_writer(NULL);
378 MessageWriter variant_array_writer(NULL);
379 MessageWriter struct_entry_writer(NULL);
381 writer.OpenVariant("a(ayq)", &variant_writer);
382 variant_writer.OpenArray("(ayq)", &variant_array_writer);
383 uint8 ip_bytes[] = {0x54, 0x65, 0x73, 0x74, 0x30};
384 for (uint16 i = 0; i < 5; ++i) {
385 variant_array_writer.OpenStruct(&struct_entry_writer);
386 ip_bytes[4] = 0x30 + i;
387 struct_entry_writer.AppendArrayOfBytes(ip_bytes, arraysize(ip_bytes));
388 struct_entry_writer.AppendUint16(i);
389 variant_array_writer.CloseContainer(&struct_entry_writer);
391 variant_writer.CloseContainer(&variant_array_writer);
392 writer.CloseContainer(&variant_writer);
394 MessageReader reader(message.get());
395 Property<std::vector<std::pair<std::vector<uint8>, uint16>>> ip_list;
396 EXPECT_TRUE(ip_list.PopValueFromReader(&reader));
398 ASSERT_EQ(5U, ip_list.value().size());
399 size_t item_index = 0;
400 for (auto& item : ip_list.value()) {
401 ASSERT_EQ(5U, item.first.size());
402 ip_bytes[4] = 0x30 + item_index;
403 EXPECT_EQ(0, memcmp(ip_bytes, item.first.data(), 5U));
404 EXPECT_EQ(item_index, item.second);
405 ++item_index;
409 TEST(PropertyTestStatic, SerializeNetAddressArray) {
410 std::vector<std::pair<std::vector<uint8>, uint16>> test_list;
412 uint8 ip_bytes[] = {0x54, 0x65, 0x73, 0x74, 0x30};
413 for (uint16 i = 0; i < 5; ++i) {
414 ip_bytes[4] = 0x30 + i;
415 std::vector<uint8> bytes(ip_bytes, ip_bytes + arraysize(ip_bytes));
416 test_list.push_back(make_pair(bytes, 16));
419 scoped_ptr<Response> message(Response::CreateEmpty());
420 MessageWriter writer(message.get());
422 Property<std::vector<std::pair<std::vector<uint8>, uint16>>> ip_list;
423 ip_list.ReplaceSetValueForTesting(test_list);
424 ip_list.AppendSetValueToWriter(&writer);
426 MessageReader reader(message.get());
427 EXPECT_TRUE(ip_list.PopValueFromReader(&reader));
428 EXPECT_EQ(test_list, ip_list.value());
431 } // namespace dbus