blob: aed770305ff6970551048d3dd8c4a36f6c8484cc [file] [log] [blame]
Paul Stewart1dce1ae2014-10-01 05:30:18 -07001// Copyright 2014 The Chromium OS 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.
4
5#include "chromeos-dbus-bindings/proxy_generator.h"
6
7#include <string>
8#include <vector>
9
Paul Stewart1dce1ae2014-10-01 05:30:18 -070010#include <base/files/file_path.h>
Alex Vakulenkof3c68972014-12-10 12:52:31 -080011#include <base/files/file_util.h>
Paul Stewart1dce1ae2014-10-01 05:30:18 -070012#include <base/files/scoped_temp_dir.h>
13#include <gtest/gtest.h>
14
15#include "chromeos-dbus-bindings/interface.h"
16
17using std::string;
18using std::vector;
19using testing::Test;
20
21namespace chromeos_dbus_bindings {
22
23namespace {
24
25const char kInterfaceName[] = "org.chromium.TestInterface";
Alex Vakulenkofafef132014-11-03 14:52:09 -080026const char kInterfaceName2[] = "org.chromium.TestInterface2";
Paul Stewart1dce1ae2014-10-01 05:30:18 -070027const char kMethod1Name[] = "Elements";
28const char kMethod1Return[] = "s";
29const char kMethod1Argument1[] = "s";
30const char kMethod1ArgumentName1[] = "space_walk";
31const char kMethod1Argument2[] = "ao";
32const char kMethod1ArgumentName2[] = "ramblin_man";
33const char kMethod2Name[] = "ReturnToPatagonia";
34const char kMethod2Return[] = "x";
35const char kMethod3Name[] = "NiceWeatherForDucks";
36const char kMethod3Argument1[] = "b";
37const char kMethod4Name[] = "ExperimentNumberSix";
Alex Vakulenkofafef132014-11-03 14:52:09 -080038const char kMethod5Name[] = "GetPersonInfo";
39const char kMethod5Argument1[] = "s";
40const char kMethod5ArgumentName1[] = "name";
41const char kMethod5Argument2[] = "i";
42const char kMethod5ArgumentName2[] = "age";
Paul Stewart1dce1ae2014-10-01 05:30:18 -070043const char kSignal1Name[] = "Closer";
44const char kSignal2Name[] = "TheCurseOfKaZar";
45const char kSignal2Argument1[] = "as";
46const char kSignal2Argument2[] = "y";
47const char kExpectedContent[] = R"literal_string(
Alex Vakulenko99e8fb02014-12-01 17:22:03 -080048#include <memory>
Paul Stewart1dce1ae2014-10-01 05:30:18 -070049#include <string>
50#include <vector>
51
52#include <base/bind.h>
53#include <base/callback.h>
54#include <base/logging.h>
55#include <base/macros.h>
56#include <base/memory/ref_counted.h>
57#include <chromeos/any.h>
58#include <chromeos/dbus/dbus_method_invoker.h>
Alex Vakulenko99e8fb02014-12-01 17:22:03 -080059#include <chromeos/dbus/dbus_property.h>
Paul Stewart1dce1ae2014-10-01 05:30:18 -070060#include <chromeos/dbus/dbus_signal_handler.h>
61#include <chromeos/errors/error.h>
Alex Vakulenko4f3c05e2014-11-12 15:01:46 -080062#include <chromeos/variant_dictionary.h>
Paul Stewart1dce1ae2014-10-01 05:30:18 -070063#include <dbus/bus.h>
64#include <dbus/message.h>
Alex Vakulenko99e8fb02014-12-01 17:22:03 -080065#include <dbus/object_manager.h>
Paul Stewart1dce1ae2014-10-01 05:30:18 -070066#include <dbus/object_path.h>
67#include <dbus/object_proxy.h>
68
69namespace org {
70namespace chromium {
71
Alex Vakulenko008e19d2015-01-21 10:37:00 -080072// Abstract interface proxy for org::chromium::TestInterface.
73class TestInterfaceProxyInterface {
74 public:
75 virtual bool Elements(
76 const std::string& in_space_walk,
77 const std::vector<dbus::ObjectPath>& in_ramblin_man,
78 std::string* out_3,
79 chromeos::ErrorPtr* error,
80 int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT) = 0;
81
82 virtual void ElementsAsync(
83 const std::string& in_space_walk,
84 const std::vector<dbus::ObjectPath>& in_ramblin_man,
85 const base::Callback<void(const std::string&)>& success_callback,
86 const base::Callback<void(chromeos::Error*)>& error_callback,
87 int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT) = 0;
88
89 virtual bool ReturnToPatagonia(
90 int64_t* out_1,
91 chromeos::ErrorPtr* error,
92 int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT) = 0;
93
94 virtual void ReturnToPatagoniaAsync(
95 const base::Callback<void(int64_t)>& success_callback,
96 const base::Callback<void(chromeos::Error*)>& error_callback,
97 int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT) = 0;
98
99 virtual bool NiceWeatherForDucks(
100 bool in_1,
101 chromeos::ErrorPtr* error,
102 int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT) = 0;
103
104 virtual void NiceWeatherForDucksAsync(
105 bool in_1,
106 const base::Callback<void()>& success_callback,
107 const base::Callback<void(chromeos::Error*)>& error_callback,
108 int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT) = 0;
109
110 // Comment line1
111 // line2
112 virtual bool ExperimentNumberSix(
113 chromeos::ErrorPtr* error,
114 int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT) = 0;
115
116 // Comment line1
117 // line2
118 virtual void ExperimentNumberSixAsync(
119 const base::Callback<void()>& success_callback,
120 const base::Callback<void(chromeos::Error*)>& error_callback,
121 int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT) = 0;
122
123 protected:
124 ~TestInterfaceProxyInterface() = default;
125};
126
127} // namespace chromium
128} // namespace org
129
130namespace org {
131namespace chromium {
132
Alex Vakulenkoc95f06b2014-11-20 15:06:09 -0800133// Interface proxy for org::chromium::TestInterface.
Alex Vakulenko008e19d2015-01-21 10:37:00 -0800134class TestInterfaceProxy final : public TestInterfaceProxyInterface {
Paul Stewart1dce1ae2014-10-01 05:30:18 -0700135 public:
Paul Stewart1dce1ae2014-10-01 05:30:18 -0700136 TestInterfaceProxy(
137 const scoped_refptr<dbus::Bus>& bus,
Alex Vakulenko6ef2d732014-11-25 14:04:27 -0800138 const std::string& service_name) :
Alex Vakulenko99e8fb02014-12-01 17:22:03 -0800139 bus_{bus},
140 service_name_{service_name},
141 dbus_object_proxy_{
142 bus_->GetObjectProxy(service_name_, object_path_)} {
Alex Vakulenkoc95f06b2014-11-20 15:06:09 -0800143 }
144
Christopher Wiley824b5242014-12-03 11:58:01 -0800145 ~TestInterfaceProxy() {
146 }
147
148 void RegisterCloserSignalHandler(
149 const base::Closure& signal_callback,
150 dbus::ObjectProxy::OnConnectedCallback on_connected_callback) {
Paul Stewart1dce1ae2014-10-01 05:30:18 -0700151 chromeos::dbus_utils::ConnectToSignal(
152 dbus_object_proxy_,
153 "org.chromium.TestInterface",
154 "Closer",
Christopher Wiley824b5242014-12-03 11:58:01 -0800155 signal_callback,
156 on_connected_callback);
157 }
158
159 void RegisterTheCurseOfKaZarSignalHandler(
160 const base::Callback<void(const std::vector<std::string>&,
161 uint8_t)>& signal_callback,
162 dbus::ObjectProxy::OnConnectedCallback on_connected_callback) {
Paul Stewart1dce1ae2014-10-01 05:30:18 -0700163 chromeos::dbus_utils::ConnectToSignal(
164 dbus_object_proxy_,
165 "org.chromium.TestInterface",
166 "TheCurseOfKaZar",
Christopher Wiley824b5242014-12-03 11:58:01 -0800167 signal_callback,
168 on_connected_callback);
Alex Vakulenkoc95f06b2014-11-20 15:06:09 -0800169 }
170
171 void ReleaseObjectProxy(const base::Closure& callback) {
172 bus_->RemoveObjectProxy(service_name_, object_path_, callback);
Paul Stewart1dce1ae2014-10-01 05:30:18 -0700173 }
Alex Vakulenko4f3c05e2014-11-12 15:01:46 -0800174
Alex Vakulenko99e8fb02014-12-01 17:22:03 -0800175 const dbus::ObjectPath& GetObjectPath() const {
176 return object_path_;
177 }
178
179 dbus::ObjectProxy* GetObjectProxy() const { return dbus_object_proxy_; }
180
Alex Vakulenko4f3c05e2014-11-12 15:01:46 -0800181 bool Elements(
Alex Vakulenkofafef132014-11-03 14:52:09 -0800182 const std::string& in_space_walk,
183 const std::vector<dbus::ObjectPath>& in_ramblin_man,
184 std::string* out_3,
Alex Vakulenkoc4e32f52014-12-04 08:28:49 -0800185 chromeos::ErrorPtr* error,
Alex Vakulenko008e19d2015-01-21 10:37:00 -0800186 int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT) override {
Alex Vakulenkoc4e32f52014-12-04 08:28:49 -0800187 auto response = chromeos::dbus_utils::CallMethodAndBlockWithTimeout(
188 timeout_ms,
Paul Stewart1dce1ae2014-10-01 05:30:18 -0700189 dbus_object_proxy_,
190 "org.chromium.TestInterface",
191 "Elements",
192 error,
Alex Vakulenkofafef132014-11-03 14:52:09 -0800193 in_space_walk,
194 in_ramblin_man);
195 return response && chromeos::dbus_utils::ExtractMethodCallResults(
196 response.get(), error, out_3);
Paul Stewart1dce1ae2014-10-01 05:30:18 -0700197 }
Alex Vakulenko4f3c05e2014-11-12 15:01:46 -0800198
Alex Vakulenkoc4e32f52014-12-04 08:28:49 -0800199 void ElementsAsync(
200 const std::string& in_space_walk,
201 const std::vector<dbus::ObjectPath>& in_ramblin_man,
202 const base::Callback<void(const std::string&)>& success_callback,
203 const base::Callback<void(chromeos::Error*)>& error_callback,
Alex Vakulenko008e19d2015-01-21 10:37:00 -0800204 int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT) override {
Alex Vakulenkoc4e32f52014-12-04 08:28:49 -0800205 chromeos::dbus_utils::CallMethodWithTimeout(
206 timeout_ms,
207 dbus_object_proxy_,
208 "org.chromium.TestInterface",
209 "Elements",
210 success_callback,
211 error_callback,
212 in_space_walk,
213 in_ramblin_man);
214 }
215
Alex Vakulenko4f3c05e2014-11-12 15:01:46 -0800216 bool ReturnToPatagonia(
Alex Vakulenkofafef132014-11-03 14:52:09 -0800217 int64_t* out_1,
Alex Vakulenkoc4e32f52014-12-04 08:28:49 -0800218 chromeos::ErrorPtr* error,
Alex Vakulenko008e19d2015-01-21 10:37:00 -0800219 int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT) override {
Alex Vakulenkoc4e32f52014-12-04 08:28:49 -0800220 auto response = chromeos::dbus_utils::CallMethodAndBlockWithTimeout(
221 timeout_ms,
Paul Stewart1dce1ae2014-10-01 05:30:18 -0700222 dbus_object_proxy_,
223 "org.chromium.TestInterface",
224 "ReturnToPatagonia",
225 error);
Alex Vakulenkofafef132014-11-03 14:52:09 -0800226 return response && chromeos::dbus_utils::ExtractMethodCallResults(
227 response.get(), error, out_1);
Paul Stewart1dce1ae2014-10-01 05:30:18 -0700228 }
Alex Vakulenko4f3c05e2014-11-12 15:01:46 -0800229
Alex Vakulenkoc4e32f52014-12-04 08:28:49 -0800230 void ReturnToPatagoniaAsync(
231 const base::Callback<void(int64_t)>& success_callback,
232 const base::Callback<void(chromeos::Error*)>& error_callback,
Alex Vakulenko008e19d2015-01-21 10:37:00 -0800233 int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT) override {
Alex Vakulenkoc4e32f52014-12-04 08:28:49 -0800234 chromeos::dbus_utils::CallMethodWithTimeout(
235 timeout_ms,
236 dbus_object_proxy_,
237 "org.chromium.TestInterface",
238 "ReturnToPatagonia",
239 success_callback,
240 error_callback);
241 }
242
Alex Vakulenko4f3c05e2014-11-12 15:01:46 -0800243 bool NiceWeatherForDucks(
Alex Vakulenkofafef132014-11-03 14:52:09 -0800244 bool in_1,
Alex Vakulenkoc4e32f52014-12-04 08:28:49 -0800245 chromeos::ErrorPtr* error,
Alex Vakulenko008e19d2015-01-21 10:37:00 -0800246 int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT) override {
Alex Vakulenkoc4e32f52014-12-04 08:28:49 -0800247 auto response = chromeos::dbus_utils::CallMethodAndBlockWithTimeout(
248 timeout_ms,
Paul Stewart1dce1ae2014-10-01 05:30:18 -0700249 dbus_object_proxy_,
250 "org.chromium.TestInterface",
251 "NiceWeatherForDucks",
252 error,
Alex Vakulenkofafef132014-11-03 14:52:09 -0800253 in_1);
254 return response && chromeos::dbus_utils::ExtractMethodCallResults(
Paul Stewart1dce1ae2014-10-01 05:30:18 -0700255 response.get(), error);
256 }
Alex Vakulenko4f3c05e2014-11-12 15:01:46 -0800257
Alex Vakulenkoc4e32f52014-12-04 08:28:49 -0800258 void NiceWeatherForDucksAsync(
259 bool in_1,
260 const base::Callback<void()>& success_callback,
261 const base::Callback<void(chromeos::Error*)>& error_callback,
Alex Vakulenko008e19d2015-01-21 10:37:00 -0800262 int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT) override {
Alex Vakulenkoc4e32f52014-12-04 08:28:49 -0800263 chromeos::dbus_utils::CallMethodWithTimeout(
264 timeout_ms,
265 dbus_object_proxy_,
266 "org.chromium.TestInterface",
267 "NiceWeatherForDucks",
268 success_callback,
269 error_callback,
270 in_1);
271 }
272
Alex Vakulenko4f3c05e2014-11-12 15:01:46 -0800273 // Comment line1
274 // line2
275 bool ExperimentNumberSix(
Alex Vakulenkoc4e32f52014-12-04 08:28:49 -0800276 chromeos::ErrorPtr* error,
Alex Vakulenko008e19d2015-01-21 10:37:00 -0800277 int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT) override {
Alex Vakulenkoc4e32f52014-12-04 08:28:49 -0800278 auto response = chromeos::dbus_utils::CallMethodAndBlockWithTimeout(
279 timeout_ms,
Paul Stewart1dce1ae2014-10-01 05:30:18 -0700280 dbus_object_proxy_,
281 "org.chromium.TestInterface",
282 "ExperimentNumberSix",
283 error);
Alex Vakulenkofafef132014-11-03 14:52:09 -0800284 return response && chromeos::dbus_utils::ExtractMethodCallResults(
Paul Stewart1dce1ae2014-10-01 05:30:18 -0700285 response.get(), error);
286 }
Alex Vakulenko4f3c05e2014-11-12 15:01:46 -0800287
Alex Vakulenkoc4e32f52014-12-04 08:28:49 -0800288 // Comment line1
289 // line2
290 void ExperimentNumberSixAsync(
291 const base::Callback<void()>& success_callback,
292 const base::Callback<void(chromeos::Error*)>& error_callback,
Alex Vakulenko008e19d2015-01-21 10:37:00 -0800293 int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT) override {
Alex Vakulenkoc4e32f52014-12-04 08:28:49 -0800294 chromeos::dbus_utils::CallMethodWithTimeout(
295 timeout_ms,
296 dbus_object_proxy_,
297 "org.chromium.TestInterface",
298 "ExperimentNumberSix",
299 success_callback,
300 error_callback);
301 }
302
Alex Vakulenkoc95f06b2014-11-20 15:06:09 -0800303 private:
304 scoped_refptr<dbus::Bus> bus_;
305 std::string service_name_;
Alex Vakulenko6ef2d732014-11-25 14:04:27 -0800306 const dbus::ObjectPath object_path_{"/org/chromium/Test"};
Alex Vakulenkoc95f06b2014-11-20 15:06:09 -0800307 dbus::ObjectProxy* dbus_object_proxy_;
308
309 DISALLOW_COPY_AND_ASSIGN(TestInterfaceProxy);
310};
311
312} // namespace chromium
313} // namespace org
314
315namespace org {
316namespace chromium {
317
Alex Vakulenko008e19d2015-01-21 10:37:00 -0800318// Abstract interface proxy for org::chromium::TestInterface2.
319class TestInterface2ProxyInterface {
320 public:
321 virtual bool GetPersonInfo(
322 std::string* out_name,
323 int32_t* out_age,
324 chromeos::ErrorPtr* error,
325 int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT) = 0;
326
327 virtual void GetPersonInfoAsync(
328 const base::Callback<void(const std::string& /*name*/, int32_t /*age*/)>& success_callback,
329 const base::Callback<void(chromeos::Error*)>& error_callback,
330 int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT) = 0;
331
332 protected:
333 ~TestInterface2ProxyInterface() = default;
334};
335
336} // namespace chromium
337} // namespace org
338
339namespace org {
340namespace chromium {
341
Alex Vakulenkoc95f06b2014-11-20 15:06:09 -0800342// Interface proxy for org::chromium::TestInterface2.
Alex Vakulenko008e19d2015-01-21 10:37:00 -0800343class TestInterface2Proxy final : public TestInterface2ProxyInterface {
Alex Vakulenkoc95f06b2014-11-20 15:06:09 -0800344 public:
345 TestInterface2Proxy(
346 const scoped_refptr<dbus::Bus>& bus,
347 const std::string& service_name,
Alex Vakulenko99e8fb02014-12-01 17:22:03 -0800348 const dbus::ObjectPath& object_path) :
349 bus_{bus},
350 service_name_{service_name},
351 object_path_{object_path},
352 dbus_object_proxy_{
353 bus_->GetObjectProxy(service_name_, object_path_)} {
Alex Vakulenkoc95f06b2014-11-20 15:06:09 -0800354 }
355
356 ~TestInterface2Proxy() {
357 }
358
359 void ReleaseObjectProxy(const base::Closure& callback) {
360 bus_->RemoveObjectProxy(service_name_, object_path_, callback);
361 }
362
Alex Vakulenko99e8fb02014-12-01 17:22:03 -0800363 const dbus::ObjectPath& GetObjectPath() const {
364 return object_path_;
365 }
366
367 dbus::ObjectProxy* GetObjectProxy() const { return dbus_object_proxy_; }
368
Alex Vakulenko4f3c05e2014-11-12 15:01:46 -0800369 bool GetPersonInfo(
Alex Vakulenkofafef132014-11-03 14:52:09 -0800370 std::string* out_name,
371 int32_t* out_age,
Alex Vakulenkoc4e32f52014-12-04 08:28:49 -0800372 chromeos::ErrorPtr* error,
Alex Vakulenko008e19d2015-01-21 10:37:00 -0800373 int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT) override {
Alex Vakulenkoc4e32f52014-12-04 08:28:49 -0800374 auto response = chromeos::dbus_utils::CallMethodAndBlockWithTimeout(
375 timeout_ms,
Alex Vakulenkofafef132014-11-03 14:52:09 -0800376 dbus_object_proxy_,
377 "org.chromium.TestInterface2",
378 "GetPersonInfo",
379 error);
380 return response && chromeos::dbus_utils::ExtractMethodCallResults(
381 response.get(), error, out_name, out_age);
382 }
Paul Stewart1dce1ae2014-10-01 05:30:18 -0700383
Alex Vakulenkoc4e32f52014-12-04 08:28:49 -0800384 void GetPersonInfoAsync(
385 const base::Callback<void(const std::string& /*name*/, int32_t /*age*/)>& success_callback,
386 const base::Callback<void(chromeos::Error*)>& error_callback,
Alex Vakulenko008e19d2015-01-21 10:37:00 -0800387 int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT) override {
Alex Vakulenkoc4e32f52014-12-04 08:28:49 -0800388 chromeos::dbus_utils::CallMethodWithTimeout(
389 timeout_ms,
390 dbus_object_proxy_,
391 "org.chromium.TestInterface2",
392 "GetPersonInfo",
393 success_callback,
394 error_callback);
395 }
396
Paul Stewart1dce1ae2014-10-01 05:30:18 -0700397 private:
398 scoped_refptr<dbus::Bus> bus_;
399 std::string service_name_;
400 dbus::ObjectPath object_path_;
401 dbus::ObjectProxy* dbus_object_proxy_;
402
Alex Vakulenkoc95f06b2014-11-20 15:06:09 -0800403 DISALLOW_COPY_AND_ASSIGN(TestInterface2Proxy);
Paul Stewart1dce1ae2014-10-01 05:30:18 -0700404};
405
406} // namespace chromium
407} // namespace org
408)literal_string";
409
Alex Vakulenko6ef2d732014-11-25 14:04:27 -0800410const char kExpectedContentWithService[] = R"literal_string(
Alex Vakulenko99e8fb02014-12-01 17:22:03 -0800411#include <memory>
Alex Vakulenko6ef2d732014-11-25 14:04:27 -0800412#include <string>
413#include <vector>
414
415#include <base/bind.h>
416#include <base/callback.h>
417#include <base/logging.h>
418#include <base/macros.h>
419#include <base/memory/ref_counted.h>
420#include <chromeos/any.h>
421#include <chromeos/dbus/dbus_method_invoker.h>
Alex Vakulenko99e8fb02014-12-01 17:22:03 -0800422#include <chromeos/dbus/dbus_property.h>
Alex Vakulenko6ef2d732014-11-25 14:04:27 -0800423#include <chromeos/dbus/dbus_signal_handler.h>
424#include <chromeos/errors/error.h>
425#include <chromeos/variant_dictionary.h>
426#include <dbus/bus.h>
427#include <dbus/message.h>
Alex Vakulenko99e8fb02014-12-01 17:22:03 -0800428#include <dbus/object_manager.h>
Alex Vakulenko6ef2d732014-11-25 14:04:27 -0800429#include <dbus/object_path.h>
430#include <dbus/object_proxy.h>
431
432namespace org {
433namespace chromium {
434
Alex Vakulenko008e19d2015-01-21 10:37:00 -0800435// Abstract interface proxy for org::chromium::TestInterface.
436class TestInterfaceProxyInterface {
437 public:
438 protected:
439 ~TestInterfaceProxyInterface() = default;
440};
441
442} // namespace chromium
443} // namespace org
444
445namespace org {
446namespace chromium {
447
Alex Vakulenko6ef2d732014-11-25 14:04:27 -0800448// Interface proxy for org::chromium::TestInterface.
Alex Vakulenko008e19d2015-01-21 10:37:00 -0800449class TestInterfaceProxy final : public TestInterfaceProxyInterface {
Alex Vakulenko6ef2d732014-11-25 14:04:27 -0800450 public:
Alex Vakulenko6ef2d732014-11-25 14:04:27 -0800451 TestInterfaceProxy(const scoped_refptr<dbus::Bus>& bus) :
Alex Vakulenko99e8fb02014-12-01 17:22:03 -0800452 bus_{bus},
453 dbus_object_proxy_{
454 bus_->GetObjectProxy(service_name_, object_path_)} {
Alex Vakulenko6ef2d732014-11-25 14:04:27 -0800455 }
456
Christopher Wiley824b5242014-12-03 11:58:01 -0800457 ~TestInterfaceProxy() {
458 }
459
460 void RegisterCloserSignalHandler(
461 const base::Closure& signal_callback,
462 dbus::ObjectProxy::OnConnectedCallback on_connected_callback) {
Alex Vakulenko6ef2d732014-11-25 14:04:27 -0800463 chromeos::dbus_utils::ConnectToSignal(
464 dbus_object_proxy_,
465 "org.chromium.TestInterface",
466 "Closer",
Christopher Wiley824b5242014-12-03 11:58:01 -0800467 signal_callback,
468 on_connected_callback);
Alex Vakulenko6ef2d732014-11-25 14:04:27 -0800469 }
470
471 void ReleaseObjectProxy(const base::Closure& callback) {
472 bus_->RemoveObjectProxy(service_name_, object_path_, callback);
473 }
474
Alex Vakulenko99e8fb02014-12-01 17:22:03 -0800475 const dbus::ObjectPath& GetObjectPath() const {
476 return object_path_;
477 }
478
479 dbus::ObjectProxy* GetObjectProxy() const { return dbus_object_proxy_; }
480
Alex Vakulenko6ef2d732014-11-25 14:04:27 -0800481 private:
482 scoped_refptr<dbus::Bus> bus_;
483 const std::string service_name_{"org.chromium.Test"};
484 const dbus::ObjectPath object_path_{"/org/chromium/Test"};
485 dbus::ObjectProxy* dbus_object_proxy_;
486
487 DISALLOW_COPY_AND_ASSIGN(TestInterfaceProxy);
488};
489
490} // namespace chromium
491} // namespace org
492
493namespace org {
494namespace chromium {
495
Alex Vakulenko008e19d2015-01-21 10:37:00 -0800496// Abstract interface proxy for org::chromium::TestInterface2.
497class TestInterface2ProxyInterface {
498 public:
499 protected:
500 ~TestInterface2ProxyInterface() = default;
501};
502
503} // namespace chromium
504} // namespace org
505
506namespace org {
507namespace chromium {
508
Alex Vakulenko6ef2d732014-11-25 14:04:27 -0800509// Interface proxy for org::chromium::TestInterface2.
Alex Vakulenko008e19d2015-01-21 10:37:00 -0800510class TestInterface2Proxy final : public TestInterface2ProxyInterface {
Alex Vakulenko6ef2d732014-11-25 14:04:27 -0800511 public:
512 TestInterface2Proxy(
513 const scoped_refptr<dbus::Bus>& bus,
Alex Vakulenko99e8fb02014-12-01 17:22:03 -0800514 const dbus::ObjectPath& object_path) :
515 bus_{bus},
516 object_path_{object_path},
517 dbus_object_proxy_{
518 bus_->GetObjectProxy(service_name_, object_path_)} {
Alex Vakulenko6ef2d732014-11-25 14:04:27 -0800519 }
520
521 ~TestInterface2Proxy() {
522 }
523
524 void ReleaseObjectProxy(const base::Closure& callback) {
525 bus_->RemoveObjectProxy(service_name_, object_path_, callback);
526 }
527
Alex Vakulenko99e8fb02014-12-01 17:22:03 -0800528 const dbus::ObjectPath& GetObjectPath() const {
529 return object_path_;
530 }
531
532 dbus::ObjectProxy* GetObjectProxy() const { return dbus_object_proxy_; }
533
Alex Vakulenko6ef2d732014-11-25 14:04:27 -0800534 private:
535 scoped_refptr<dbus::Bus> bus_;
536 const std::string service_name_{"org.chromium.Test"};
537 dbus::ObjectPath object_path_;
538 dbus::ObjectProxy* dbus_object_proxy_;
539
540 DISALLOW_COPY_AND_ASSIGN(TestInterface2Proxy);
541};
542
543} // namespace chromium
544} // namespace org
Alex Vakulenko6ef2d732014-11-25 14:04:27 -0800545)literal_string";
546
Alex Vakulenko99e8fb02014-12-01 17:22:03 -0800547const char kExpectedContentWithObjectManager[] = R"literal_string(
548#include <memory>
549#include <string>
550#include <vector>
551
552#include <base/bind.h>
553#include <base/callback.h>
554#include <base/logging.h>
555#include <base/macros.h>
556#include <base/memory/ref_counted.h>
557#include <chromeos/any.h>
558#include <chromeos/dbus/dbus_method_invoker.h>
559#include <chromeos/dbus/dbus_property.h>
560#include <chromeos/dbus/dbus_signal_handler.h>
561#include <chromeos/errors/error.h>
562#include <chromeos/variant_dictionary.h>
563#include <dbus/bus.h>
564#include <dbus/message.h>
565#include <dbus/object_manager.h>
566#include <dbus/object_path.h>
567#include <dbus/object_proxy.h>
568
569namespace org {
570namespace chromium {
571class ObjectManagerProxy;
572} // namespace chromium
573} // namespace org
574
575namespace org {
576namespace chromium {
577
Alex Vakulenko008e19d2015-01-21 10:37:00 -0800578// Abstract interface proxy for org::chromium::Itf1.
579class Itf1ProxyInterface {
580 public:
581 virtual const std::string& data() const = 0;
582
583 protected:
584 ~Itf1ProxyInterface() = default;
585};
586
587} // namespace chromium
588} // namespace org
589
590namespace org {
591namespace chromium {
592
Alex Vakulenko99e8fb02014-12-01 17:22:03 -0800593// Interface proxy for org::chromium::Itf1.
Alex Vakulenko008e19d2015-01-21 10:37:00 -0800594class Itf1Proxy final : public Itf1ProxyInterface {
Alex Vakulenko99e8fb02014-12-01 17:22:03 -0800595 public:
Alex Vakulenko99e8fb02014-12-01 17:22:03 -0800596 class PropertySet : public dbus::PropertySet {
597 public:
598 PropertySet(dbus::ObjectProxy* object_proxy,
599 const PropertyChangedCallback& callback)
600 : dbus::PropertySet{object_proxy,
601 "org.chromium.Itf1",
602 callback} {
603 RegisterProperty("data", &data);
604 }
605
606 chromeos::dbus_utils::Property<std::string> data;
607
608 private:
609 DISALLOW_COPY_AND_ASSIGN(PropertySet);
610 };
611
612 Itf1Proxy(
613 const scoped_refptr<dbus::Bus>& bus,
614 const std::string& service_name,
615 PropertySet* property_set) :
616 bus_{bus},
617 service_name_{service_name},
618 property_set_{property_set},
619 dbus_object_proxy_{
620 bus_->GetObjectProxy(service_name_, object_path_)} {
621 }
622
Christopher Wiley824b5242014-12-03 11:58:01 -0800623 ~Itf1Proxy() {
624 }
625
626 void RegisterCloserSignalHandler(
627 const base::Closure& signal_callback,
628 dbus::ObjectProxy::OnConnectedCallback on_connected_callback) {
Alex Vakulenko99e8fb02014-12-01 17:22:03 -0800629 chromeos::dbus_utils::ConnectToSignal(
630 dbus_object_proxy_,
631 "org.chromium.Itf1",
632 "Closer",
Christopher Wiley824b5242014-12-03 11:58:01 -0800633 signal_callback,
634 on_connected_callback);
Alex Vakulenko99e8fb02014-12-01 17:22:03 -0800635 }
636
637 void ReleaseObjectProxy(const base::Closure& callback) {
638 bus_->RemoveObjectProxy(service_name_, object_path_, callback);
639 }
640
641 const dbus::ObjectPath& GetObjectPath() const {
642 return object_path_;
643 }
644
645 dbus::ObjectProxy* GetObjectProxy() const { return dbus_object_proxy_; }
646
647 void SetPropertyChangedCallback(
648 const base::Callback<void(Itf1Proxy*, const std::string&)>& callback) {
649 on_property_changed_ = callback;
650 }
651
652 const PropertySet* GetProperties() const { return property_set_; }
653 PropertySet* GetProperties() { return property_set_; }
654
Alex Vakulenko008e19d2015-01-21 10:37:00 -0800655 const std::string& data() const override {
Alex Vakulenko99e8fb02014-12-01 17:22:03 -0800656 return property_set_->data.value();
657 }
658
659 private:
660 void OnPropertyChanged(const std::string& property_name) {
661 if (!on_property_changed_.is_null())
662 on_property_changed_.Run(this, property_name);
663 }
664
665 scoped_refptr<dbus::Bus> bus_;
666 std::string service_name_;
667 const dbus::ObjectPath object_path_{"/org/chromium/Test/Object"};
668 PropertySet* property_set_;
669 base::Callback<void(Itf1Proxy*, const std::string&)> on_property_changed_;
670 dbus::ObjectProxy* dbus_object_proxy_;
671
672 friend class org::chromium::ObjectManagerProxy;
673 DISALLOW_COPY_AND_ASSIGN(Itf1Proxy);
674};
675
676} // namespace chromium
677} // namespace org
678
679namespace org {
680namespace chromium {
681
Alex Vakulenko008e19d2015-01-21 10:37:00 -0800682// Abstract interface proxy for org::chromium::Itf2.
683class Itf2ProxyInterface {
684 public:
685 protected:
686 ~Itf2ProxyInterface() = default;
687};
688
689} // namespace chromium
690} // namespace org
691
692namespace org {
693namespace chromium {
694
Alex Vakulenko99e8fb02014-12-01 17:22:03 -0800695// Interface proxy for org::chromium::Itf2.
Alex Vakulenko008e19d2015-01-21 10:37:00 -0800696class Itf2Proxy final : public Itf2ProxyInterface {
Alex Vakulenko99e8fb02014-12-01 17:22:03 -0800697 public:
698 class PropertySet : public dbus::PropertySet {
699 public:
700 PropertySet(dbus::ObjectProxy* object_proxy,
701 const PropertyChangedCallback& callback)
702 : dbus::PropertySet{object_proxy,
703 "org.chromium.Itf2",
704 callback} {
705 }
706
707
708 private:
709 DISALLOW_COPY_AND_ASSIGN(PropertySet);
710 };
711
712 Itf2Proxy(
713 const scoped_refptr<dbus::Bus>& bus,
714 const std::string& service_name,
715 const dbus::ObjectPath& object_path) :
716 bus_{bus},
717 service_name_{service_name},
718 object_path_{object_path},
719 dbus_object_proxy_{
720 bus_->GetObjectProxy(service_name_, object_path_)} {
721 }
722
723 ~Itf2Proxy() {
724 }
725
726 void ReleaseObjectProxy(const base::Closure& callback) {
727 bus_->RemoveObjectProxy(service_name_, object_path_, callback);
728 }
729
730 const dbus::ObjectPath& GetObjectPath() const {
731 return object_path_;
732 }
733
734 dbus::ObjectProxy* GetObjectProxy() const { return dbus_object_proxy_; }
735
736 private:
737 scoped_refptr<dbus::Bus> bus_;
738 std::string service_name_;
739 dbus::ObjectPath object_path_;
740 dbus::ObjectProxy* dbus_object_proxy_;
741
742 DISALLOW_COPY_AND_ASSIGN(Itf2Proxy);
743};
744
745} // namespace chromium
746} // namespace org
747
748namespace org {
749namespace chromium {
750
751class ObjectManagerProxy : public dbus::ObjectManager::Interface {
752 public:
753 ObjectManagerProxy(const scoped_refptr<dbus::Bus>& bus,
754 const std::string& service_name)
755 : bus_{bus},
756 dbus_object_manager_{bus->GetObjectManager(
757 service_name,
758 dbus::ObjectPath{"/org/chromium/Test"})} {
759 dbus_object_manager_->RegisterInterface("org.chromium.Itf1", this);
760 dbus_object_manager_->RegisterInterface("org.chromium.Itf2", this);
761 }
762
Christopher Wileye11e81c2014-12-19 13:10:52 -0800763 ~ObjectManagerProxy() override {
764 dbus_object_manager_->UnregisterInterface("org.chromium.Itf1");
765 dbus_object_manager_->UnregisterInterface("org.chromium.Itf2");
766 }
767
Alex Vakulenko99e8fb02014-12-01 17:22:03 -0800768 dbus::ObjectManager* GetObjectManagerProxy() const {
769 return dbus_object_manager_;
770 }
771
772 org::chromium::Itf1Proxy* GetItf1Proxy(
773 const dbus::ObjectPath& object_path) {
774 auto p = itf1_instances_.find(object_path);
775 if (p != itf1_instances_.end())
776 return p->second.get();
777 return nullptr;
778 }
779 std::vector<org::chromium::Itf1Proxy*> GetItf1Instances() const {
780 std::vector<org::chromium::Itf1Proxy*> values;
781 values.reserve(itf1_instances_.size());
782 for (const auto& pair : itf1_instances_)
783 values.push_back(pair.second.get());
784 return values;
785 }
786 void SetItf1AddedCallback(
787 const base::Callback<void(org::chromium::Itf1Proxy*)>& callback) {
788 on_itf1_added_ = callback;
789 }
790 void SetItf1RemovedCallback(
791 const base::Callback<void(const dbus::ObjectPath&)>& callback) {
792 on_itf1_removed_ = callback;
793 }
794
795 org::chromium::Itf2Proxy* GetItf2Proxy(
796 const dbus::ObjectPath& object_path) {
797 auto p = itf2_instances_.find(object_path);
798 if (p != itf2_instances_.end())
799 return p->second.get();
800 return nullptr;
801 }
802 std::vector<org::chromium::Itf2Proxy*> GetItf2Instances() const {
803 std::vector<org::chromium::Itf2Proxy*> values;
804 values.reserve(itf2_instances_.size());
805 for (const auto& pair : itf2_instances_)
806 values.push_back(pair.second.get());
807 return values;
808 }
809 void SetItf2AddedCallback(
810 const base::Callback<void(org::chromium::Itf2Proxy*)>& callback) {
811 on_itf2_added_ = callback;
812 }
813 void SetItf2RemovedCallback(
814 const base::Callback<void(const dbus::ObjectPath&)>& callback) {
815 on_itf2_removed_ = callback;
816 }
817
818 private:
819 void OnPropertyChanged(const dbus::ObjectPath& object_path,
820 const std::string& interface_name,
821 const std::string& property_name) {
822 if (interface_name == "org.chromium.Itf1") {
823 auto p = itf1_instances_.find(object_path);
824 if (p == itf1_instances_.end())
825 return;
826 p->second->OnPropertyChanged(property_name);
827 return;
828 }
829 }
830
831 void ObjectAdded(
832 const dbus::ObjectPath& object_path,
833 const std::string& interface_name) override {
834 if (interface_name == "org.chromium.Itf1") {
835 auto property_set =
836 static_cast<org::chromium::Itf1Proxy::PropertySet*>(
837 dbus_object_manager_->GetProperties(object_path, interface_name));
838 std::unique_ptr<org::chromium::Itf1Proxy> itf1_proxy{
839 new org::chromium::Itf1Proxy{bus_, property_set}
840 };
841 auto p = itf1_instances_.emplace(object_path, std::move(itf1_proxy));
842 if (!on_itf1_added_.is_null())
843 on_itf1_added_.Run(p.first->second.get());
844 return;
845 }
846 if (interface_name == "org.chromium.Itf2") {
847 std::unique_ptr<org::chromium::Itf2Proxy> itf2_proxy{
848 new org::chromium::Itf2Proxy{bus_, object_path}
849 };
850 auto p = itf2_instances_.emplace(object_path, std::move(itf2_proxy));
851 if (!on_itf2_added_.is_null())
852 on_itf2_added_.Run(p.first->second.get());
853 return;
854 }
855 }
856
857 void ObjectRemoved(
858 const dbus::ObjectPath& object_path,
859 const std::string& interface_name) override {
860 if (interface_name == "org.chromium.Itf1") {
861 auto p = itf1_instances_.find(object_path);
862 if (p != itf1_instances_.end()) {
863 if (!on_itf1_removed_.is_null())
864 on_itf1_removed_.Run(object_path);
865 itf1_instances_.erase(p);
866 }
867 return;
868 }
869 if (interface_name == "org.chromium.Itf2") {
870 auto p = itf2_instances_.find(object_path);
871 if (p != itf2_instances_.end()) {
872 if (!on_itf2_removed_.is_null())
873 on_itf2_removed_.Run(object_path);
874 itf2_instances_.erase(p);
875 }
876 return;
877 }
878 }
879
880 dbus::PropertySet* CreateProperties(
881 dbus::ObjectProxy* object_proxy,
882 const dbus::ObjectPath& object_path,
883 const std::string& interface_name) override {
884 if (interface_name == "org.chromium.Itf1") {
885 return new org::chromium::Itf1Proxy::PropertySet{
886 object_proxy,
887 base::Bind(&ObjectManagerProxy::OnPropertyChanged,
888 weak_ptr_factory_.GetWeakPtr(),
889 object_path,
890 interface_name)
891 };
892 }
893 if (interface_name == "org.chromium.Itf2") {
894 return new org::chromium::Itf2Proxy::PropertySet{
895 object_proxy,
896 base::Bind(&ObjectManagerProxy::OnPropertyChanged,
897 weak_ptr_factory_.GetWeakPtr(),
898 object_path,
899 interface_name)
900 };
901 }
902 LOG(FATAL) << "Creating properties for unsupported interface "
903 << interface_name;
904 return nullptr;
905 }
906
907 scoped_refptr<dbus::Bus> bus_;
908 dbus::ObjectManager* dbus_object_manager_;
909 std::map<dbus::ObjectPath,
910 std::unique_ptr<org::chromium::Itf1Proxy>> itf1_instances_;
911 base::Callback<void(org::chromium::Itf1Proxy*)> on_itf1_added_;
912 base::Callback<void(const dbus::ObjectPath&)> on_itf1_removed_;
913 std::map<dbus::ObjectPath,
914 std::unique_ptr<org::chromium::Itf2Proxy>> itf2_instances_;
915 base::Callback<void(org::chromium::Itf2Proxy*)> on_itf2_added_;
916 base::Callback<void(const dbus::ObjectPath&)> on_itf2_removed_;
917 base::WeakPtrFactory<ObjectManagerProxy> weak_ptr_factory_{this};
918
919 DISALLOW_COPY_AND_ASSIGN(ObjectManagerProxy);
920};
921
922} // namespace chromium
923} // namespace org
924)literal_string";
925
926const char kExpectedContentWithObjectManagerAndServiceName[] = R"literal_string(
927#include <memory>
928#include <string>
929#include <vector>
930
931#include <base/bind.h>
932#include <base/callback.h>
933#include <base/logging.h>
934#include <base/macros.h>
935#include <base/memory/ref_counted.h>
936#include <chromeos/any.h>
937#include <chromeos/dbus/dbus_method_invoker.h>
938#include <chromeos/dbus/dbus_property.h>
939#include <chromeos/dbus/dbus_signal_handler.h>
940#include <chromeos/errors/error.h>
941#include <chromeos/variant_dictionary.h>
942#include <dbus/bus.h>
943#include <dbus/message.h>
944#include <dbus/object_manager.h>
945#include <dbus/object_path.h>
946#include <dbus/object_proxy.h>
947
948namespace org {
949namespace chromium {
950class ObjectManagerProxy;
951} // namespace chromium
952} // namespace org
953
954namespace org {
955namespace chromium {
956
Alex Vakulenko008e19d2015-01-21 10:37:00 -0800957// Abstract interface proxy for org::chromium::Itf1.
958class Itf1ProxyInterface {
959 public:
960 protected:
961 ~Itf1ProxyInterface() = default;
962};
963
964} // namespace chromium
965} // namespace org
966
967namespace org {
968namespace chromium {
969
Alex Vakulenko99e8fb02014-12-01 17:22:03 -0800970// Interface proxy for org::chromium::Itf1.
Alex Vakulenko008e19d2015-01-21 10:37:00 -0800971class Itf1Proxy final : public Itf1ProxyInterface {
Alex Vakulenko99e8fb02014-12-01 17:22:03 -0800972 public:
Alex Vakulenko99e8fb02014-12-01 17:22:03 -0800973 class PropertySet : public dbus::PropertySet {
974 public:
975 PropertySet(dbus::ObjectProxy* object_proxy,
976 const PropertyChangedCallback& callback)
977 : dbus::PropertySet{object_proxy,
978 "org.chromium.Itf1",
979 callback} {
980 }
981
982
983 private:
984 DISALLOW_COPY_AND_ASSIGN(PropertySet);
985 };
986
987 Itf1Proxy(const scoped_refptr<dbus::Bus>& bus) :
988 bus_{bus},
989 dbus_object_proxy_{
990 bus_->GetObjectProxy(service_name_, object_path_)} {
991 }
992
Christopher Wiley824b5242014-12-03 11:58:01 -0800993 ~Itf1Proxy() {
994 }
995
996 void RegisterCloserSignalHandler(
997 const base::Closure& signal_callback,
998 dbus::ObjectProxy::OnConnectedCallback on_connected_callback) {
Alex Vakulenko99e8fb02014-12-01 17:22:03 -0800999 chromeos::dbus_utils::ConnectToSignal(
1000 dbus_object_proxy_,
1001 "org.chromium.Itf1",
1002 "Closer",
Christopher Wiley824b5242014-12-03 11:58:01 -08001003 signal_callback,
1004 on_connected_callback);
Alex Vakulenko99e8fb02014-12-01 17:22:03 -08001005 }
1006
1007 void ReleaseObjectProxy(const base::Closure& callback) {
1008 bus_->RemoveObjectProxy(service_name_, object_path_, callback);
1009 }
1010
1011 const dbus::ObjectPath& GetObjectPath() const {
1012 return object_path_;
1013 }
1014
1015 dbus::ObjectProxy* GetObjectProxy() const { return dbus_object_proxy_; }
1016
Alex Vakulenko99e8fb02014-12-01 17:22:03 -08001017 private:
1018 scoped_refptr<dbus::Bus> bus_;
1019 const std::string service_name_{"org.chromium.Test"};
1020 const dbus::ObjectPath object_path_{"/org/chromium/Test/Object"};
1021 dbus::ObjectProxy* dbus_object_proxy_;
1022
1023 DISALLOW_COPY_AND_ASSIGN(Itf1Proxy);
1024};
1025
1026} // namespace chromium
1027} // namespace org
1028
1029namespace org {
1030namespace chromium {
1031
Alex Vakulenko008e19d2015-01-21 10:37:00 -08001032// Abstract interface proxy for org::chromium::Itf2.
1033class Itf2ProxyInterface {
1034 public:
1035 protected:
1036 ~Itf2ProxyInterface() = default;
1037};
1038
1039} // namespace chromium
1040} // namespace org
1041
1042namespace org {
1043namespace chromium {
1044
Alex Vakulenko99e8fb02014-12-01 17:22:03 -08001045// Interface proxy for org::chromium::Itf2.
Alex Vakulenko008e19d2015-01-21 10:37:00 -08001046class Itf2Proxy final : public Itf2ProxyInterface {
Alex Vakulenko99e8fb02014-12-01 17:22:03 -08001047 public:
1048 class PropertySet : public dbus::PropertySet {
1049 public:
1050 PropertySet(dbus::ObjectProxy* object_proxy,
1051 const PropertyChangedCallback& callback)
1052 : dbus::PropertySet{object_proxy,
1053 "org.chromium.Itf2",
1054 callback} {
1055 }
1056
1057
1058 private:
1059 DISALLOW_COPY_AND_ASSIGN(PropertySet);
1060 };
1061
1062 Itf2Proxy(
1063 const scoped_refptr<dbus::Bus>& bus,
1064 const dbus::ObjectPath& object_path) :
1065 bus_{bus},
1066 object_path_{object_path},
1067 dbus_object_proxy_{
1068 bus_->GetObjectProxy(service_name_, object_path_)} {
1069 }
1070
1071 ~Itf2Proxy() {
1072 }
1073
1074 void ReleaseObjectProxy(const base::Closure& callback) {
1075 bus_->RemoveObjectProxy(service_name_, object_path_, callback);
1076 }
1077
1078 const dbus::ObjectPath& GetObjectPath() const {
1079 return object_path_;
1080 }
1081
1082 dbus::ObjectProxy* GetObjectProxy() const { return dbus_object_proxy_; }
1083
1084 private:
1085 scoped_refptr<dbus::Bus> bus_;
1086 const std::string service_name_{"org.chromium.Test"};
1087 dbus::ObjectPath object_path_;
1088 dbus::ObjectProxy* dbus_object_proxy_;
1089
1090 DISALLOW_COPY_AND_ASSIGN(Itf2Proxy);
1091};
1092
1093} // namespace chromium
1094} // namespace org
1095
1096namespace org {
1097namespace chromium {
1098
1099class ObjectManagerProxy : public dbus::ObjectManager::Interface {
1100 public:
1101 ObjectManagerProxy(const scoped_refptr<dbus::Bus>& bus)
1102 : bus_{bus},
1103 dbus_object_manager_{bus->GetObjectManager(
1104 "org.chromium.Test",
1105 dbus::ObjectPath{"/org/chromium/Test"})} {
1106 dbus_object_manager_->RegisterInterface("org.chromium.Itf1", this);
1107 dbus_object_manager_->RegisterInterface("org.chromium.Itf2", this);
1108 }
1109
Christopher Wileye11e81c2014-12-19 13:10:52 -08001110 ~ObjectManagerProxy() override {
1111 dbus_object_manager_->UnregisterInterface("org.chromium.Itf1");
1112 dbus_object_manager_->UnregisterInterface("org.chromium.Itf2");
1113 }
1114
Alex Vakulenko99e8fb02014-12-01 17:22:03 -08001115 dbus::ObjectManager* GetObjectManagerProxy() const {
1116 return dbus_object_manager_;
1117 }
1118
1119 org::chromium::Itf1Proxy* GetItf1Proxy(
1120 const dbus::ObjectPath& object_path) {
1121 auto p = itf1_instances_.find(object_path);
1122 if (p != itf1_instances_.end())
1123 return p->second.get();
1124 return nullptr;
1125 }
1126 std::vector<org::chromium::Itf1Proxy*> GetItf1Instances() const {
1127 std::vector<org::chromium::Itf1Proxy*> values;
1128 values.reserve(itf1_instances_.size());
1129 for (const auto& pair : itf1_instances_)
1130 values.push_back(pair.second.get());
1131 return values;
1132 }
1133 void SetItf1AddedCallback(
1134 const base::Callback<void(org::chromium::Itf1Proxy*)>& callback) {
1135 on_itf1_added_ = callback;
1136 }
1137 void SetItf1RemovedCallback(
1138 const base::Callback<void(const dbus::ObjectPath&)>& callback) {
1139 on_itf1_removed_ = callback;
1140 }
1141
1142 org::chromium::Itf2Proxy* GetItf2Proxy(
1143 const dbus::ObjectPath& object_path) {
1144 auto p = itf2_instances_.find(object_path);
1145 if (p != itf2_instances_.end())
1146 return p->second.get();
1147 return nullptr;
1148 }
1149 std::vector<org::chromium::Itf2Proxy*> GetItf2Instances() const {
1150 std::vector<org::chromium::Itf2Proxy*> values;
1151 values.reserve(itf2_instances_.size());
1152 for (const auto& pair : itf2_instances_)
1153 values.push_back(pair.second.get());
1154 return values;
1155 }
1156 void SetItf2AddedCallback(
1157 const base::Callback<void(org::chromium::Itf2Proxy*)>& callback) {
1158 on_itf2_added_ = callback;
1159 }
1160 void SetItf2RemovedCallback(
1161 const base::Callback<void(const dbus::ObjectPath&)>& callback) {
1162 on_itf2_removed_ = callback;
1163 }
1164
1165 private:
1166 void OnPropertyChanged(const dbus::ObjectPath& object_path,
1167 const std::string& interface_name,
1168 const std::string& property_name) {
1169 }
1170
1171 void ObjectAdded(
1172 const dbus::ObjectPath& object_path,
1173 const std::string& interface_name) override {
1174 if (interface_name == "org.chromium.Itf1") {
1175 std::unique_ptr<org::chromium::Itf1Proxy> itf1_proxy{
1176 new org::chromium::Itf1Proxy{bus_}
1177 };
1178 auto p = itf1_instances_.emplace(object_path, std::move(itf1_proxy));
1179 if (!on_itf1_added_.is_null())
1180 on_itf1_added_.Run(p.first->second.get());
1181 return;
1182 }
1183 if (interface_name == "org.chromium.Itf2") {
1184 std::unique_ptr<org::chromium::Itf2Proxy> itf2_proxy{
1185 new org::chromium::Itf2Proxy{bus_, object_path}
1186 };
1187 auto p = itf2_instances_.emplace(object_path, std::move(itf2_proxy));
1188 if (!on_itf2_added_.is_null())
1189 on_itf2_added_.Run(p.first->second.get());
1190 return;
1191 }
1192 }
1193
1194 void ObjectRemoved(
1195 const dbus::ObjectPath& object_path,
1196 const std::string& interface_name) override {
1197 if (interface_name == "org.chromium.Itf1") {
1198 auto p = itf1_instances_.find(object_path);
1199 if (p != itf1_instances_.end()) {
1200 if (!on_itf1_removed_.is_null())
1201 on_itf1_removed_.Run(object_path);
1202 itf1_instances_.erase(p);
1203 }
1204 return;
1205 }
1206 if (interface_name == "org.chromium.Itf2") {
1207 auto p = itf2_instances_.find(object_path);
1208 if (p != itf2_instances_.end()) {
1209 if (!on_itf2_removed_.is_null())
1210 on_itf2_removed_.Run(object_path);
1211 itf2_instances_.erase(p);
1212 }
1213 return;
1214 }
1215 }
1216
1217 dbus::PropertySet* CreateProperties(
1218 dbus::ObjectProxy* object_proxy,
1219 const dbus::ObjectPath& object_path,
1220 const std::string& interface_name) override {
1221 if (interface_name == "org.chromium.Itf1") {
1222 return new org::chromium::Itf1Proxy::PropertySet{
1223 object_proxy,
1224 base::Bind(&ObjectManagerProxy::OnPropertyChanged,
1225 weak_ptr_factory_.GetWeakPtr(),
1226 object_path,
1227 interface_name)
1228 };
1229 }
1230 if (interface_name == "org.chromium.Itf2") {
1231 return new org::chromium::Itf2Proxy::PropertySet{
1232 object_proxy,
1233 base::Bind(&ObjectManagerProxy::OnPropertyChanged,
1234 weak_ptr_factory_.GetWeakPtr(),
1235 object_path,
1236 interface_name)
1237 };
1238 }
1239 LOG(FATAL) << "Creating properties for unsupported interface "
1240 << interface_name;
1241 return nullptr;
1242 }
1243
1244 scoped_refptr<dbus::Bus> bus_;
1245 dbus::ObjectManager* dbus_object_manager_;
1246 std::map<dbus::ObjectPath,
1247 std::unique_ptr<org::chromium::Itf1Proxy>> itf1_instances_;
1248 base::Callback<void(org::chromium::Itf1Proxy*)> on_itf1_added_;
1249 base::Callback<void(const dbus::ObjectPath&)> on_itf1_removed_;
1250 std::map<dbus::ObjectPath,
1251 std::unique_ptr<org::chromium::Itf2Proxy>> itf2_instances_;
1252 base::Callback<void(org::chromium::Itf2Proxy*)> on_itf2_added_;
1253 base::Callback<void(const dbus::ObjectPath&)> on_itf2_removed_;
1254 base::WeakPtrFactory<ObjectManagerProxy> weak_ptr_factory_{this};
1255
1256 DISALLOW_COPY_AND_ASSIGN(ObjectManagerProxy);
1257};
1258
1259} // namespace chromium
1260} // namespace org
1261)literal_string";
Paul Stewart1dce1ae2014-10-01 05:30:18 -07001262} // namespace
1263
1264class ProxyGeneratorTest : public Test {
1265 public:
1266 void SetUp() override {
1267 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
1268 }
1269
1270 protected:
1271 base::FilePath CreateInputFile(const string& contents) {
1272 base::FilePath path;
1273 EXPECT_TRUE(base::CreateTemporaryFileInDir(temp_dir_.path(), &path));
1274 EXPECT_EQ(contents.size(),
1275 base::WriteFile(path, contents.c_str(), contents.size()));
1276 return path;
1277 }
1278
1279 base::ScopedTempDir temp_dir_;
1280};
1281
1282TEST_F(ProxyGeneratorTest, GenerateAdaptors) {
1283 Interface interface;
1284 interface.name = kInterfaceName;
Alex Vakulenko6ef2d732014-11-25 14:04:27 -08001285 interface.path = "/org/chromium/Test";
Paul Stewart1dce1ae2014-10-01 05:30:18 -07001286 interface.methods.emplace_back(
1287 kMethod1Name,
1288 vector<Interface::Argument>{
1289 {kMethod1ArgumentName1, kMethod1Argument1},
1290 {kMethod1ArgumentName2, kMethod1Argument2}},
1291 vector<Interface::Argument>{{"", kMethod1Return}});
1292 interface.methods.emplace_back(
1293 kMethod2Name,
1294 vector<Interface::Argument>{},
1295 vector<Interface::Argument>{{"", kMethod2Return}});
1296 interface.methods.emplace_back(
1297 kMethod3Name,
1298 vector<Interface::Argument>{{"", kMethod3Argument1}},
1299 vector<Interface::Argument>{});
1300 interface.methods.emplace_back(kMethod4Name);
1301 interface.signals.emplace_back(kSignal1Name);
1302 interface.signals.emplace_back(
1303 kSignal2Name,
1304 vector<Interface::Argument>{
1305 {"", kSignal2Argument1},
1306 {"", kSignal2Argument2}});
Alex Vakulenko6ef2d732014-11-25 14:04:27 -08001307 interface.methods.back().doc_string = "Comment line1\nline2";
Alex Vakulenkofafef132014-11-03 14:52:09 -08001308 Interface interface2;
1309 interface2.name = kInterfaceName2;
1310 interface2.methods.emplace_back(
1311 kMethod5Name,
1312 vector<Interface::Argument>{},
1313 vector<Interface::Argument>{
1314 {kMethod5ArgumentName1, kMethod5Argument1},
1315 {kMethod5ArgumentName2, kMethod5Argument2}});
1316 vector<Interface> interfaces{interface, interface2};
Paul Stewart1dce1ae2014-10-01 05:30:18 -07001317 base::FilePath output_path = temp_dir_.path().Append("output.h");
Alex Vakulenko6ef2d732014-11-25 14:04:27 -08001318 ServiceConfig config;
1319 EXPECT_TRUE(ProxyGenerator::GenerateProxies(config, interfaces, output_path));
Paul Stewart1dce1ae2014-10-01 05:30:18 -07001320 string contents;
1321 EXPECT_TRUE(base::ReadFileToString(output_path, &contents));
1322 // The header guards contain the (temporary) filename, so we search for
1323 // the content we need within the string.
1324 EXPECT_NE(string::npos, contents.find(kExpectedContent))
1325 << "Expected to find the following content...\n"
1326 << kExpectedContent << "...within content...\n" << contents;
1327}
1328
Alex Vakulenko6ef2d732014-11-25 14:04:27 -08001329TEST_F(ProxyGeneratorTest, GenerateAdaptorsWithServiceName) {
1330 Interface interface;
1331 interface.name = kInterfaceName;
1332 interface.path = "/org/chromium/Test";
1333 interface.signals.emplace_back(kSignal1Name);
1334 Interface interface2;
1335 interface2.name = kInterfaceName2;
1336 vector<Interface> interfaces{interface, interface2};
1337 base::FilePath output_path = temp_dir_.path().Append("output2.h");
1338 ServiceConfig config;
1339 config.service_name = "org.chromium.Test";
1340 EXPECT_TRUE(ProxyGenerator::GenerateProxies(config, interfaces, output_path));
1341 string contents;
1342 EXPECT_TRUE(base::ReadFileToString(output_path, &contents));
1343 // The header guards contain the (temporary) filename, so we search for
1344 // the content we need within the string.
1345 EXPECT_NE(string::npos, contents.find(kExpectedContentWithService))
1346 << "Expected to find the following content...\n"
1347 << kExpectedContentWithService << "...within content...\n" << contents;
1348}
1349
Alex Vakulenko99e8fb02014-12-01 17:22:03 -08001350TEST_F(ProxyGeneratorTest, GenerateAdaptorsWithObjectManager) {
1351 Interface interface;
1352 interface.name = "org.chromium.Itf1";
1353 interface.path = "/org/chromium/Test/Object";
1354 interface.signals.emplace_back(kSignal1Name);
1355 interface.properties.emplace_back("data", "s", "read");
1356 Interface interface2;
1357 interface2.name = "org.chromium.Itf2";
1358 vector<Interface> interfaces{interface, interface2};
1359 base::FilePath output_path = temp_dir_.path().Append("output3.h");
1360 ServiceConfig config;
1361 config.object_manager.name = "org.chromium.ObjectManager";
1362 config.object_manager.object_path = "/org/chromium/Test";
1363 EXPECT_TRUE(ProxyGenerator::GenerateProxies(config, interfaces, output_path));
1364 string contents;
1365 EXPECT_TRUE(base::ReadFileToString(output_path, &contents));
1366 // The header guards contain the (temporary) filename, so we search for
1367 // the content we need within the string.
1368 EXPECT_NE(string::npos, contents.find(kExpectedContentWithObjectManager))
1369 << "Expected to find the following content...\n"
1370 << kExpectedContentWithObjectManager << "...within content...\n"
1371 << contents;
1372}
1373
1374TEST_F(ProxyGeneratorTest, GenerateAdaptorsWithObjectManagerAndServiceName) {
1375 Interface interface;
1376 interface.name = "org.chromium.Itf1";
1377 interface.path = "/org/chromium/Test/Object";
1378 interface.signals.emplace_back(kSignal1Name);
1379 Interface interface2;
1380 interface2.name = "org.chromium.Itf2";
1381 vector<Interface> interfaces{interface, interface2};
1382 base::FilePath output_path = temp_dir_.path().Append("output4.h");
1383 ServiceConfig config;
1384 config.service_name = "org.chromium.Test";
1385 config.object_manager.name = "org.chromium.ObjectManager";
1386 config.object_manager.object_path = "/org/chromium/Test";
1387 EXPECT_TRUE(ProxyGenerator::GenerateProxies(config, interfaces, output_path));
1388 string contents;
1389 EXPECT_TRUE(base::ReadFileToString(output_path, &contents));
1390 // The header guards contain the (temporary) filename, so we search for
1391 // the content we need within the string.
1392 EXPECT_NE(string::npos,
1393 contents.find(kExpectedContentWithObjectManagerAndServiceName))
1394 << "Expected to find the following content...\n"
1395 << kExpectedContentWithObjectManagerAndServiceName
1396 << "...within content...\n" << contents;
1397}
1398
Paul Stewart1dce1ae2014-10-01 05:30:18 -07001399} // namespace chromeos_dbus_bindings