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" |
(...skipping 17 matching lines...) Expand all Loading... | |
28 public: | 28 public: |
29 PropertyTest() { | 29 PropertyTest() { |
30 } | 30 } |
31 | 31 |
32 struct Properties : public PropertySet { | 32 struct Properties : public PropertySet { |
33 Property<std::string> name; | 33 Property<std::string> name; |
34 Property<int16> version; | 34 Property<int16> version; |
35 Property<std::vector<std::string> > methods; | 35 Property<std::vector<std::string> > methods; |
36 Property<std::vector<ObjectPath> > objects; | 36 Property<std::vector<ObjectPath> > objects; |
37 Property<std::vector<uint8> > bytes; | 37 Property<std::vector<uint8> > bytes; |
38 Property<std::map<std::string, std::string>> string_map; | |
39 Property<std::vector<std::pair<std::vector<uint8>, uint16>>> ip_port_list; | |
38 | 40 |
39 Properties(ObjectProxy* object_proxy, | 41 Properties(ObjectProxy* object_proxy, |
40 PropertyChangedCallback property_changed_callback) | 42 PropertyChangedCallback property_changed_callback) |
41 : PropertySet(object_proxy, | 43 : PropertySet(object_proxy, |
42 "org.chromium.TestInterface", | 44 "org.chromium.TestInterface", |
43 property_changed_callback) { | 45 property_changed_callback) { |
44 RegisterProperty("Name", &name); | 46 RegisterProperty("Name", &name); |
45 RegisterProperty("Version", &version); | 47 RegisterProperty("Version", &version); |
46 RegisterProperty("Methods", &methods); | 48 RegisterProperty("Methods", &methods); |
47 RegisterProperty("Objects", &objects); | 49 RegisterProperty("Objects", &objects); |
48 RegisterProperty("Bytes", &bytes); | 50 RegisterProperty("Bytes", &bytes); |
51 RegisterProperty("StringMap", &string_map); | |
52 RegisterProperty("IPList", &ip_port_list); | |
49 } | 53 } |
50 }; | 54 }; |
51 | 55 |
52 void SetUp() override { | 56 void SetUp() override { |
53 // Make the main thread not to allow IO. | 57 // Make the main thread not to allow IO. |
54 base::ThreadRestrictions::SetIOAllowed(false); | 58 base::ThreadRestrictions::SetIOAllowed(false); |
55 | 59 |
56 // Start the D-Bus thread. | 60 // Start the D-Bus thread. |
57 dbus_thread_.reset(new base::Thread("D-Bus Thread")); | 61 dbus_thread_.reset(new base::Thread("D-Bus Thread")); |
58 base::Thread::Options thread_options; | 62 base::Thread::Options thread_options; |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
120 void WaitForUpdates(size_t num_updates) { | 124 void WaitForUpdates(size_t num_updates) { |
121 while (updated_properties_.size() < num_updates) { | 125 while (updated_properties_.size() < num_updates) { |
122 run_loop_.reset(new base::RunLoop); | 126 run_loop_.reset(new base::RunLoop); |
123 run_loop_->Run(); | 127 run_loop_->Run(); |
124 } | 128 } |
125 for (size_t i = 0; i < num_updates; ++i) | 129 for (size_t i = 0; i < num_updates; ++i) |
126 updated_properties_.erase(updated_properties_.begin()); | 130 updated_properties_.erase(updated_properties_.begin()); |
127 } | 131 } |
128 | 132 |
129 // Name, Version, Methods, Objects | 133 // Name, Version, Methods, Objects |
130 static const int kExpectedSignalUpdates = 5; | 134 static const int kExpectedSignalUpdates = 7; |
131 | 135 |
132 // Waits for initial values to be set. | 136 // Waits for initial values to be set. |
133 void WaitForGetAll() { | 137 void WaitForGetAll() { |
134 WaitForUpdates(kExpectedSignalUpdates); | 138 WaitForUpdates(kExpectedSignalUpdates); |
135 } | 139 } |
136 | 140 |
137 // Waits for the callback. |id| is the string bound to the callback when | 141 // Waits for the callback. |id| is the string bound to the callback when |
138 // the method call is made that identifies it and distinguishes from any | 142 // the method call is made that identifies it and distinguishes from any |
139 // other; you can set this to whatever you wish. | 143 // other; you can set this to whatever you wish. |
140 void WaitForCallback(const std::string& id) { | 144 void WaitForCallback(const std::string& id) { |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
173 std::vector<ObjectPath> objects = properties_->objects.value(); | 177 std::vector<ObjectPath> objects = properties_->objects.value(); |
174 ASSERT_EQ(1U, objects.size()); | 178 ASSERT_EQ(1U, objects.size()); |
175 EXPECT_EQ(ObjectPath("/TestObjectPath"), objects[0]); | 179 EXPECT_EQ(ObjectPath("/TestObjectPath"), objects[0]); |
176 | 180 |
177 std::vector<uint8> bytes = properties_->bytes.value(); | 181 std::vector<uint8> bytes = properties_->bytes.value(); |
178 ASSERT_EQ(4U, bytes.size()); | 182 ASSERT_EQ(4U, bytes.size()); |
179 EXPECT_EQ('T', bytes[0]); | 183 EXPECT_EQ('T', bytes[0]); |
180 EXPECT_EQ('e', bytes[1]); | 184 EXPECT_EQ('e', bytes[1]); |
181 EXPECT_EQ('s', bytes[2]); | 185 EXPECT_EQ('s', bytes[2]); |
182 EXPECT_EQ('t', bytes[3]); | 186 EXPECT_EQ('t', bytes[3]); |
187 | |
188 std::map<std::string, std::string> string_map = | |
189 properties_->string_map.value(); | |
190 ASSERT_EQ(4U, string_map.size()); | |
191 EXPECT_EQ("1", string_map["One"]); | |
192 EXPECT_EQ("2", string_map["Two"]); | |
193 EXPECT_EQ("3", string_map["Three"]); | |
194 EXPECT_EQ("4", string_map["Four"]); | |
195 | |
196 std::vector<std::pair<std::vector<uint8>, uint16>> ip_list = | |
197 properties_->ip_port_list.value(); | |
198 ASSERT_EQ(5U, ip_list.size()); | |
199 for (size_t i = 0; i < ip_list.size(); ++i) { | |
200 EXPECT_EQ(5U, ip_list[i].first.size()); | |
201 EXPECT_EQ('T', ip_list[i].first[0]); | |
202 EXPECT_EQ('e', ip_list[i].first[1]); | |
203 EXPECT_EQ('s', ip_list[i].first[2]); | |
204 EXPECT_EQ('t', ip_list[i].first[3]); | |
205 EXPECT_EQ('0' + i, ip_list[i].first[4]); | |
206 EXPECT_EQ(i, ip_list[i].second); | |
207 } | |
183 } | 208 } |
184 | 209 |
185 TEST_F(PropertyTest, UpdatedValues) { | 210 TEST_F(PropertyTest, UpdatedValues) { |
186 WaitForGetAll(); | 211 WaitForGetAll(); |
187 | 212 |
188 // Update the value of the "Name" property, this value should not change. | 213 // Update the value of the "Name" property, this value should not change. |
189 properties_->name.Get(base::Bind(&PropertyTest::PropertyCallback, | 214 properties_->name.Get(base::Bind(&PropertyTest::PropertyCallback, |
190 base::Unretained(this), | 215 base::Unretained(this), |
191 "Name")); | 216 "Name")); |
192 WaitForCallback("Name"); | 217 WaitForCallback("Name"); |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
237 "Bytes")); | 262 "Bytes")); |
238 WaitForCallback("Bytes"); | 263 WaitForCallback("Bytes"); |
239 WaitForUpdates(1); | 264 WaitForUpdates(1); |
240 | 265 |
241 std::vector<uint8> bytes = properties_->bytes.value(); | 266 std::vector<uint8> bytes = properties_->bytes.value(); |
242 ASSERT_EQ(4U, bytes.size()); | 267 ASSERT_EQ(4U, bytes.size()); |
243 EXPECT_EQ('T', bytes[0]); | 268 EXPECT_EQ('T', bytes[0]); |
244 EXPECT_EQ('e', bytes[1]); | 269 EXPECT_EQ('e', bytes[1]); |
245 EXPECT_EQ('s', bytes[2]); | 270 EXPECT_EQ('s', bytes[2]); |
246 EXPECT_EQ('t', bytes[3]); | 271 EXPECT_EQ('t', bytes[3]); |
272 | |
273 // Update the value of the "Bytes" property, this value should not change | |
274 // and should not grow to contain duplicate entries. | |
275 properties_->bytes.Get(base::Bind(&PropertyTest::PropertyCallback, | |
276 base::Unretained(this), "StringMap")); | |
277 WaitForCallback("StringMap"); | |
278 WaitForUpdates(1); | |
279 | |
280 std::map<std::string, std::string> string_map = | |
281 properties_->string_map.value(); | |
282 ASSERT_EQ(4U, string_map.size()); | |
283 EXPECT_EQ("1", string_map["One"]); | |
284 EXPECT_EQ("2", string_map["Two"]); | |
285 EXPECT_EQ("3", string_map["Three"]); | |
286 EXPECT_EQ("4", string_map["Four"]); | |
287 | |
288 // Update the value of the "Bytes" property, this value should not change | |
289 // and should not grow to contain duplicate entries. | |
290 properties_->bytes.Get(base::Bind(&PropertyTest::PropertyCallback, | |
291 base::Unretained(this), "IPList")); | |
292 WaitForCallback("IPList"); | |
293 WaitForUpdates(1); | |
294 | |
295 std::vector<std::pair<std::vector<uint8>, uint16>> ip_list = | |
296 properties_->ip_port_list.value(); | |
297 ASSERT_EQ(5U, ip_list.size()); | |
298 for (size_t i = 0; i < ip_list.size(); ++i) { | |
299 EXPECT_EQ(5U, ip_list[i].first.size()); | |
300 EXPECT_EQ('T', ip_list[i].first[0]); | |
301 EXPECT_EQ('e', ip_list[i].first[1]); | |
302 EXPECT_EQ('s', ip_list[i].first[2]); | |
303 EXPECT_EQ('t', ip_list[i].first[3]); | |
304 EXPECT_EQ('0' + i, ip_list[i].first[4]); | |
305 EXPECT_EQ(i, ip_list[i].second); | |
306 } | |
hashimoto
2015/02/06 07:45:12
Instead of changing test_service.cc, how about add
dtapuska
2015/02/06 15:56:01
Done.
| |
247 } | 307 } |
248 | 308 |
249 TEST_F(PropertyTest, Get) { | 309 TEST_F(PropertyTest, Get) { |
250 WaitForGetAll(); | 310 WaitForGetAll(); |
251 | 311 |
252 // Ask for the new Version property. | 312 // Ask for the new Version property. |
253 properties_->version.Get(base::Bind(&PropertyTest::PropertyCallback, | 313 properties_->version.Get(base::Bind(&PropertyTest::PropertyCallback, |
254 base::Unretained(this), | 314 base::Unretained(this), |
255 "Get")); | 315 "Get")); |
256 WaitForCallback("Get"); | 316 WaitForCallback("Get"); |
(...skipping 14 matching lines...) Expand all Loading... | |
271 "Set")); | 331 "Set")); |
272 WaitForCallback("Set"); | 332 WaitForCallback("Set"); |
273 | 333 |
274 // TestService sends a property update. | 334 // TestService sends a property update. |
275 WaitForUpdates(1); | 335 WaitForUpdates(1); |
276 | 336 |
277 EXPECT_EQ("NewService", properties_->name.value()); | 337 EXPECT_EQ("NewService", properties_->name.value()); |
278 } | 338 } |
279 | 339 |
280 } // namespace dbus | 340 } // namespace dbus |
OLD | NEW |