OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "dbus/property.h" | 5 #include "dbus/property.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
11 #include "base/bind.h" | 11 #include "base/bind.h" |
12 #include "base/logging.h" | 12 #include "base/logging.h" |
13 #include "base/message_loop.h" | 13 #include "base/message_loop.h" |
14 #include "base/threading/thread.h" | 14 #include "base/threading/thread.h" |
15 #include "base/threading/thread_restrictions.h" | 15 #include "base/threading/thread_restrictions.h" |
16 #include "dbus/bus.h" | 16 #include "dbus/bus.h" |
17 #include "dbus/object_path.h" | 17 #include "dbus/object_path.h" |
18 #include "dbus/object_proxy.h" | 18 #include "dbus/object_proxy.h" |
19 #include "dbus/test_service.h" | 19 #include "dbus/test_service.h" |
20 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
21 | 21 |
| 22 namespace dbus { |
| 23 |
22 // The property test exerises the asynchronous APIs in PropertySet and | 24 // The property test exerises the asynchronous APIs in PropertySet and |
23 // Property<>. | 25 // Property<>. |
24 class PropertyTest : public testing::Test { | 26 class PropertyTest : public testing::Test { |
25 public: | 27 public: |
26 PropertyTest() { | 28 PropertyTest() { |
27 } | 29 } |
28 | 30 |
29 struct Properties : public dbus::PropertySet { | 31 struct Properties : public PropertySet { |
30 dbus::Property<std::string> name; | 32 Property<std::string> name; |
31 dbus::Property<int16> version; | 33 Property<int16> version; |
32 dbus::Property<std::vector<std::string> > methods; | 34 Property<std::vector<std::string> > methods; |
33 dbus::Property<std::vector<dbus::ObjectPath> > objects; | 35 Property<std::vector<ObjectPath> > objects; |
34 | 36 |
35 Properties(dbus::ObjectProxy* object_proxy, | 37 Properties(ObjectProxy* object_proxy, |
36 PropertyChangedCallback property_changed_callback) | 38 PropertyChangedCallback property_changed_callback) |
37 : dbus::PropertySet(object_proxy, | 39 : PropertySet(object_proxy, |
38 "org.chromium.TestInterface", | 40 "org.chromium.TestInterface", |
39 property_changed_callback) { | 41 property_changed_callback) { |
40 RegisterProperty("Name", &name); | 42 RegisterProperty("Name", &name); |
41 RegisterProperty("Version", &version); | 43 RegisterProperty("Version", &version); |
42 RegisterProperty("Methods", &methods); | 44 RegisterProperty("Methods", &methods); |
43 RegisterProperty("Objects", &objects); | 45 RegisterProperty("Objects", &objects); |
44 } | 46 } |
45 }; | 47 }; |
46 | 48 |
47 virtual void SetUp() { | 49 virtual void SetUp() { |
48 // Make the main thread not to allow IO. | 50 // Make the main thread not to allow IO. |
49 base::ThreadRestrictions::SetIOAllowed(false); | 51 base::ThreadRestrictions::SetIOAllowed(false); |
50 | 52 |
51 // Start the D-Bus thread. | 53 // Start the D-Bus thread. |
52 dbus_thread_.reset(new base::Thread("D-Bus Thread")); | 54 dbus_thread_.reset(new base::Thread("D-Bus Thread")); |
53 base::Thread::Options thread_options; | 55 base::Thread::Options thread_options; |
54 thread_options.message_loop_type = base::MessageLoop::TYPE_IO; | 56 thread_options.message_loop_type = base::MessageLoop::TYPE_IO; |
55 ASSERT_TRUE(dbus_thread_->StartWithOptions(thread_options)); | 57 ASSERT_TRUE(dbus_thread_->StartWithOptions(thread_options)); |
56 | 58 |
57 // Start the test service, using the D-Bus thread. | 59 // Start the test service, using the D-Bus thread. |
58 dbus::TestService::Options options; | 60 TestService::Options options; |
59 options.dbus_task_runner = dbus_thread_->message_loop_proxy(); | 61 options.dbus_task_runner = dbus_thread_->message_loop_proxy(); |
60 test_service_.reset(new dbus::TestService(options)); | 62 test_service_.reset(new TestService(options)); |
61 ASSERT_TRUE(test_service_->StartService()); | 63 ASSERT_TRUE(test_service_->StartService()); |
62 ASSERT_TRUE(test_service_->WaitUntilServiceIsStarted()); | 64 ASSERT_TRUE(test_service_->WaitUntilServiceIsStarted()); |
63 ASSERT_TRUE(test_service_->HasDBusThread()); | 65 ASSERT_TRUE(test_service_->HasDBusThread()); |
64 | 66 |
65 // Create the client, using the D-Bus thread. | 67 // Create the client, using the D-Bus thread. |
66 dbus::Bus::Options bus_options; | 68 Bus::Options bus_options; |
67 bus_options.bus_type = dbus::Bus::SESSION; | 69 bus_options.bus_type = Bus::SESSION; |
68 bus_options.connection_type = dbus::Bus::PRIVATE; | 70 bus_options.connection_type = Bus::PRIVATE; |
69 bus_options.dbus_task_runner = dbus_thread_->message_loop_proxy(); | 71 bus_options.dbus_task_runner = dbus_thread_->message_loop_proxy(); |
70 bus_ = new dbus::Bus(bus_options); | 72 bus_ = new Bus(bus_options); |
71 object_proxy_ = bus_->GetObjectProxy( | 73 object_proxy_ = bus_->GetObjectProxy( |
72 "org.chromium.TestService", | 74 "org.chromium.TestService", |
73 dbus::ObjectPath("/org/chromium/TestObject")); | 75 ObjectPath("/org/chromium/TestObject")); |
74 ASSERT_TRUE(bus_->HasDBusThread()); | 76 ASSERT_TRUE(bus_->HasDBusThread()); |
75 | 77 |
76 // Create the properties structure | 78 // Create the properties structure |
77 properties_.reset(new Properties( | 79 properties_.reset(new Properties( |
78 object_proxy_, | 80 object_proxy_, |
79 base::Bind(&PropertyTest::OnPropertyChanged, | 81 base::Bind(&PropertyTest::OnPropertyChanged, |
80 base::Unretained(this)))); | 82 base::Unretained(this)))); |
81 properties_->ConnectSignals(); | 83 properties_->ConnectSignals(); |
82 properties_->GetAll(); | 84 properties_->GetAll(); |
83 } | 85 } |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
131 // the method call is made that identifies it and distinguishes from any | 133 // the method call is made that identifies it and distinguishes from any |
132 // other; you can set this to whatever you wish. | 134 // other; you can set this to whatever you wish. |
133 void WaitForCallback(const std::string& id) { | 135 void WaitForCallback(const std::string& id) { |
134 while (last_callback_ != id) { | 136 while (last_callback_ != id) { |
135 message_loop_.Run(); | 137 message_loop_.Run(); |
136 } | 138 } |
137 } | 139 } |
138 | 140 |
139 base::MessageLoop message_loop_; | 141 base::MessageLoop message_loop_; |
140 scoped_ptr<base::Thread> dbus_thread_; | 142 scoped_ptr<base::Thread> dbus_thread_; |
141 scoped_refptr<dbus::Bus> bus_; | 143 scoped_refptr<Bus> bus_; |
142 dbus::ObjectProxy* object_proxy_; | 144 ObjectProxy* object_proxy_; |
143 scoped_ptr<Properties> properties_; | 145 scoped_ptr<Properties> properties_; |
144 scoped_ptr<dbus::TestService> test_service_; | 146 scoped_ptr<TestService> test_service_; |
145 // Properties updated. | 147 // Properties updated. |
146 std::vector<std::string> updated_properties_; | 148 std::vector<std::string> updated_properties_; |
147 // Last callback received. | 149 // Last callback received. |
148 std::string last_callback_; | 150 std::string last_callback_; |
149 }; | 151 }; |
150 | 152 |
151 TEST_F(PropertyTest, InitialValues) { | 153 TEST_F(PropertyTest, InitialValues) { |
152 WaitForGetAll(); | 154 WaitForGetAll(); |
153 | 155 |
154 EXPECT_EQ("TestService", properties_->name.value()); | 156 EXPECT_EQ("TestService", properties_->name.value()); |
155 EXPECT_EQ(10, properties_->version.value()); | 157 EXPECT_EQ(10, properties_->version.value()); |
156 | 158 |
157 std::vector<std::string> methods = properties_->methods.value(); | 159 std::vector<std::string> methods = properties_->methods.value(); |
158 ASSERT_EQ(4U, methods.size()); | 160 ASSERT_EQ(4U, methods.size()); |
159 EXPECT_EQ("Echo", methods[0]); | 161 EXPECT_EQ("Echo", methods[0]); |
160 EXPECT_EQ("SlowEcho", methods[1]); | 162 EXPECT_EQ("SlowEcho", methods[1]); |
161 EXPECT_EQ("AsyncEcho", methods[2]); | 163 EXPECT_EQ("AsyncEcho", methods[2]); |
162 EXPECT_EQ("BrokenMethod", methods[3]); | 164 EXPECT_EQ("BrokenMethod", methods[3]); |
163 | 165 |
164 std::vector<dbus::ObjectPath> objects = properties_->objects.value(); | 166 std::vector<ObjectPath> objects = properties_->objects.value(); |
165 ASSERT_EQ(1U, objects.size()); | 167 ASSERT_EQ(1U, objects.size()); |
166 EXPECT_EQ(dbus::ObjectPath("/TestObjectPath"), objects[0]); | 168 EXPECT_EQ(ObjectPath("/TestObjectPath"), objects[0]); |
167 } | 169 } |
168 | 170 |
169 TEST_F(PropertyTest, UpdatedValues) { | 171 TEST_F(PropertyTest, UpdatedValues) { |
170 WaitForGetAll(); | 172 WaitForGetAll(); |
171 | 173 |
172 // Update the value of the "Name" property, this value should not change. | 174 // Update the value of the "Name" property, this value should not change. |
173 properties_->name.Get(base::Bind(&PropertyTest::PropertyCallback, | 175 properties_->name.Get(base::Bind(&PropertyTest::PropertyCallback, |
174 base::Unretained(this), | 176 base::Unretained(this), |
175 "Name")); | 177 "Name")); |
176 WaitForCallback("Name"); | 178 WaitForCallback("Name"); |
(...skipping 26 matching lines...) Expand all Loading... |
203 EXPECT_EQ("BrokenMethod", methods[3]); | 205 EXPECT_EQ("BrokenMethod", methods[3]); |
204 | 206 |
205 // Update the value of the "Objects" property, this value should not change | 207 // Update the value of the "Objects" property, this value should not change |
206 // and should not grow to contain duplicate entries. | 208 // and should not grow to contain duplicate entries. |
207 properties_->objects.Get(base::Bind(&PropertyTest::PropertyCallback, | 209 properties_->objects.Get(base::Bind(&PropertyTest::PropertyCallback, |
208 base::Unretained(this), | 210 base::Unretained(this), |
209 "Objects")); | 211 "Objects")); |
210 WaitForCallback("Objects"); | 212 WaitForCallback("Objects"); |
211 WaitForUpdates(1); | 213 WaitForUpdates(1); |
212 | 214 |
213 std::vector<dbus::ObjectPath> objects = properties_->objects.value(); | 215 std::vector<ObjectPath> objects = properties_->objects.value(); |
214 ASSERT_EQ(1U, objects.size()); | 216 ASSERT_EQ(1U, objects.size()); |
215 EXPECT_EQ(dbus::ObjectPath("/TestObjectPath"), objects[0]); | 217 EXPECT_EQ(ObjectPath("/TestObjectPath"), objects[0]); |
216 } | 218 } |
217 | 219 |
218 TEST_F(PropertyTest, Get) { | 220 TEST_F(PropertyTest, Get) { |
219 WaitForGetAll(); | 221 WaitForGetAll(); |
220 | 222 |
221 // Ask for the new Version property. | 223 // Ask for the new Version property. |
222 properties_->version.Get(base::Bind(&PropertyTest::PropertyCallback, | 224 properties_->version.Get(base::Bind(&PropertyTest::PropertyCallback, |
223 base::Unretained(this), | 225 base::Unretained(this), |
224 "Get")); | 226 "Get")); |
225 WaitForCallback("Get"); | 227 WaitForCallback("Get"); |
(...skipping 12 matching lines...) Expand all Loading... |
238 base::Bind(&PropertyTest::PropertyCallback, | 240 base::Bind(&PropertyTest::PropertyCallback, |
239 base::Unretained(this), | 241 base::Unretained(this), |
240 "Set")); | 242 "Set")); |
241 WaitForCallback("Set"); | 243 WaitForCallback("Set"); |
242 | 244 |
243 // TestService sends a property update. | 245 // TestService sends a property update. |
244 WaitForUpdates(1); | 246 WaitForUpdates(1); |
245 | 247 |
246 EXPECT_EQ("NewService", properties_->name.value()); | 248 EXPECT_EQ("NewService", properties_->name.value()); |
247 } | 249 } |
| 250 |
| 251 } // namespace dbus |
OLD | NEW |