chromeos-dbus-bindings: Added proxy mock generation

Now it is possible to generate Mocks for D-Bus proxy objects to
mock out the D-Bus interfaces in tests.

A new option --mock=<file> is added to the generator to specify
the output file for the mock objects. If used with --proxy,
the mock header file will include the proxy header file to share
the definition of the common interface proxy abstract interface.
Otherwise, the mock header file will provide its own definition
of that abstract interface.

As a result, the proxy generation has been changed to include
the base abstract interface for the D-Bus interface proxy class
which the proxy and/or mock objects derive from.

The code using the proxy objects can be updated to pass around
the abstract interface instead of concrete proxy classes which
will make it possible to substruture proxies with mocks and
facilitate testing of the code using D-Bus proxies.

GYP action now has another variable 'mock_output_file' which
can be used along with 'proxy_output_file' to control the
location of the generated mock header file:

'actions': [
  {
    'action_name': 'generate-buffet-proxies',
    'variables': {
      'dbus_service_config': 'dbus_bindings/dbus-service-config.json',
      'proxy_output_file': 'include/buffet/dbus-proxies.h'
      'mock_output_file': 'include/buffet/dbus-mocks.h'
    },
    'sources': [
      'dbus_bindings/org.chromium.Buffet.Command.xml',
      'dbus_bindings/org.chromium.Buffet.Manager.xml',
    ],
    'includes': ['../common-mk/generate-dbus-proxies.gypi'],
  },
],

BUG=chromium:449176
TEST=FEATURES=test emerge-link chromeos-dbus-bindings
     Manually tested the generated proxy and mock classes with
     buffet.

Change-Id: I1fb84cfbfd751793df72ad5b050ee1cf00a64d62
Reviewed-on: https://chromium-review.googlesource.com/242276
Reviewed-by: Alex Vakulenko <avakulenko@chromium.org>
Commit-Queue: Alex Vakulenko <avakulenko@chromium.org>
Tested-by: Alex Vakulenko <avakulenko@chromium.org>
diff --git a/chromeos-dbus-bindings/proxy_generator_unittest.cc b/chromeos-dbus-bindings/proxy_generator_unittest.cc
index d374ee4..aed7703 100644
--- a/chromeos-dbus-bindings/proxy_generator_unittest.cc
+++ b/chromeos-dbus-bindings/proxy_generator_unittest.cc
@@ -69,8 +69,69 @@
 namespace org {
 namespace chromium {
 
+// Abstract interface proxy for org::chromium::TestInterface.
+class TestInterfaceProxyInterface {
+ public:
+  virtual bool Elements(
+      const std::string& in_space_walk,
+      const std::vector<dbus::ObjectPath>& in_ramblin_man,
+      std::string* out_3,
+      chromeos::ErrorPtr* error,
+      int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT) = 0;
+
+  virtual void ElementsAsync(
+      const std::string& in_space_walk,
+      const std::vector<dbus::ObjectPath>& in_ramblin_man,
+      const base::Callback<void(const std::string&)>& success_callback,
+      const base::Callback<void(chromeos::Error*)>& error_callback,
+      int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT) = 0;
+
+  virtual bool ReturnToPatagonia(
+      int64_t* out_1,
+      chromeos::ErrorPtr* error,
+      int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT) = 0;
+
+  virtual void ReturnToPatagoniaAsync(
+      const base::Callback<void(int64_t)>& success_callback,
+      const base::Callback<void(chromeos::Error*)>& error_callback,
+      int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT) = 0;
+
+  virtual bool NiceWeatherForDucks(
+      bool in_1,
+      chromeos::ErrorPtr* error,
+      int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT) = 0;
+
+  virtual void NiceWeatherForDucksAsync(
+      bool in_1,
+      const base::Callback<void()>& success_callback,
+      const base::Callback<void(chromeos::Error*)>& error_callback,
+      int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT) = 0;
+
+  // Comment line1
+  // line2
+  virtual bool ExperimentNumberSix(
+      chromeos::ErrorPtr* error,
+      int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT) = 0;
+
+  // Comment line1
+  // line2
+  virtual void ExperimentNumberSixAsync(
+      const base::Callback<void()>& success_callback,
+      const base::Callback<void(chromeos::Error*)>& error_callback,
+      int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT) = 0;
+
+ protected:
+  ~TestInterfaceProxyInterface() = default;
+};
+
+}  // namespace chromium
+}  // namespace org
+
+namespace org {
+namespace chromium {
+
 // Interface proxy for org::chromium::TestInterface.
-class TestInterfaceProxy final {
+class TestInterfaceProxy final : public TestInterfaceProxyInterface {
  public:
   TestInterfaceProxy(
       const scoped_refptr<dbus::Bus>& bus,
@@ -122,7 +183,7 @@
       const std::vector<dbus::ObjectPath>& in_ramblin_man,
       std::string* out_3,
       chromeos::ErrorPtr* error,
-      int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT) {
+      int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT) override {
     auto response = chromeos::dbus_utils::CallMethodAndBlockWithTimeout(
         timeout_ms,
         dbus_object_proxy_,
@@ -140,7 +201,7 @@
       const std::vector<dbus::ObjectPath>& in_ramblin_man,
       const base::Callback<void(const std::string&)>& success_callback,
       const base::Callback<void(chromeos::Error*)>& error_callback,
-      int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT) {
+      int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT) override {
     chromeos::dbus_utils::CallMethodWithTimeout(
         timeout_ms,
         dbus_object_proxy_,
@@ -155,7 +216,7 @@
   bool ReturnToPatagonia(
       int64_t* out_1,
       chromeos::ErrorPtr* error,
-      int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT) {
+      int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT) override {
     auto response = chromeos::dbus_utils::CallMethodAndBlockWithTimeout(
         timeout_ms,
         dbus_object_proxy_,
@@ -169,7 +230,7 @@
   void ReturnToPatagoniaAsync(
       const base::Callback<void(int64_t)>& success_callback,
       const base::Callback<void(chromeos::Error*)>& error_callback,
-      int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT) {
+      int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT) override {
     chromeos::dbus_utils::CallMethodWithTimeout(
         timeout_ms,
         dbus_object_proxy_,
@@ -182,7 +243,7 @@
   bool NiceWeatherForDucks(
       bool in_1,
       chromeos::ErrorPtr* error,
-      int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT) {
+      int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT) override {
     auto response = chromeos::dbus_utils::CallMethodAndBlockWithTimeout(
         timeout_ms,
         dbus_object_proxy_,
@@ -198,7 +259,7 @@
       bool in_1,
       const base::Callback<void()>& success_callback,
       const base::Callback<void(chromeos::Error*)>& error_callback,
-      int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT) {
+      int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT) override {
     chromeos::dbus_utils::CallMethodWithTimeout(
         timeout_ms,
         dbus_object_proxy_,
@@ -213,7 +274,7 @@
   // line2
   bool ExperimentNumberSix(
       chromeos::ErrorPtr* error,
-      int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT) {
+      int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT) override {
     auto response = chromeos::dbus_utils::CallMethodAndBlockWithTimeout(
         timeout_ms,
         dbus_object_proxy_,
@@ -229,7 +290,7 @@
   void ExperimentNumberSixAsync(
       const base::Callback<void()>& success_callback,
       const base::Callback<void(chromeos::Error*)>& error_callback,
-      int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT) {
+      int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT) override {
     chromeos::dbus_utils::CallMethodWithTimeout(
         timeout_ms,
         dbus_object_proxy_,
@@ -254,8 +315,32 @@
 namespace org {
 namespace chromium {
 
+// Abstract interface proxy for org::chromium::TestInterface2.
+class TestInterface2ProxyInterface {
+ public:
+  virtual bool GetPersonInfo(
+      std::string* out_name,
+      int32_t* out_age,
+      chromeos::ErrorPtr* error,
+      int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT) = 0;
+
+  virtual void GetPersonInfoAsync(
+      const base::Callback<void(const std::string& /*name*/, int32_t /*age*/)>& success_callback,
+      const base::Callback<void(chromeos::Error*)>& error_callback,
+      int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT) = 0;
+
+ protected:
+  ~TestInterface2ProxyInterface() = default;
+};
+
+}  // namespace chromium
+}  // namespace org
+
+namespace org {
+namespace chromium {
+
 // Interface proxy for org::chromium::TestInterface2.
-class TestInterface2Proxy final {
+class TestInterface2Proxy final : public TestInterface2ProxyInterface {
  public:
   TestInterface2Proxy(
       const scoped_refptr<dbus::Bus>& bus,
@@ -285,7 +370,7 @@
       std::string* out_name,
       int32_t* out_age,
       chromeos::ErrorPtr* error,
-      int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT) {
+      int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT) override {
     auto response = chromeos::dbus_utils::CallMethodAndBlockWithTimeout(
         timeout_ms,
         dbus_object_proxy_,
@@ -299,7 +384,7 @@
   void GetPersonInfoAsync(
       const base::Callback<void(const std::string& /*name*/, int32_t /*age*/)>& success_callback,
       const base::Callback<void(chromeos::Error*)>& error_callback,
-      int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT) {
+      int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT) override {
     chromeos::dbus_utils::CallMethodWithTimeout(
         timeout_ms,
         dbus_object_proxy_,
@@ -347,8 +432,21 @@
 namespace org {
 namespace chromium {
 
+// Abstract interface proxy for org::chromium::TestInterface.
+class TestInterfaceProxyInterface {
+ public:
+ protected:
+  ~TestInterfaceProxyInterface() = default;
+};
+
+}  // namespace chromium
+}  // namespace org
+
+namespace org {
+namespace chromium {
+
 // Interface proxy for org::chromium::TestInterface.
-class TestInterfaceProxy final {
+class TestInterfaceProxy final : public TestInterfaceProxyInterface {
  public:
   TestInterfaceProxy(const scoped_refptr<dbus::Bus>& bus) :
       bus_{bus},
@@ -395,8 +493,21 @@
 namespace org {
 namespace chromium {
 
+// Abstract interface proxy for org::chromium::TestInterface2.
+class TestInterface2ProxyInterface {
+ public:
+ protected:
+  ~TestInterface2ProxyInterface() = default;
+};
+
+}  // namespace chromium
+}  // namespace org
+
+namespace org {
+namespace chromium {
+
 // Interface proxy for org::chromium::TestInterface2.
-class TestInterface2Proxy final {
+class TestInterface2Proxy final : public TestInterface2ProxyInterface {
  public:
   TestInterface2Proxy(
       const scoped_refptr<dbus::Bus>& bus,
@@ -464,8 +575,23 @@
 namespace org {
 namespace chromium {
 
+// Abstract interface proxy for org::chromium::Itf1.
+class Itf1ProxyInterface {
+ public:
+  virtual const std::string& data() const = 0;
+
+ protected:
+  ~Itf1ProxyInterface() = default;
+};
+
+}  // namespace chromium
+}  // namespace org
+
+namespace org {
+namespace chromium {
+
 // Interface proxy for org::chromium::Itf1.
-class Itf1Proxy final {
+class Itf1Proxy final : public Itf1ProxyInterface {
  public:
   class PropertySet : public dbus::PropertySet {
    public:
@@ -526,7 +652,7 @@
   const PropertySet* GetProperties() const { return property_set_; }
   PropertySet* GetProperties() { return property_set_; }
 
-  const std::string& data() const {
+  const std::string& data() const override {
     return property_set_->data.value();
   }
 
@@ -553,8 +679,21 @@
 namespace org {
 namespace chromium {
 
+// Abstract interface proxy for org::chromium::Itf2.
+class Itf2ProxyInterface {
+ public:
+ protected:
+  ~Itf2ProxyInterface() = default;
+};
+
+}  // namespace chromium
+}  // namespace org
+
+namespace org {
+namespace chromium {
+
 // Interface proxy for org::chromium::Itf2.
-class Itf2Proxy final {
+class Itf2Proxy final : public Itf2ProxyInterface {
  public:
   class PropertySet : public dbus::PropertySet {
    public:
@@ -815,8 +954,21 @@
 namespace org {
 namespace chromium {
 
+// Abstract interface proxy for org::chromium::Itf1.
+class Itf1ProxyInterface {
+ public:
+ protected:
+  ~Itf1ProxyInterface() = default;
+};
+
+}  // namespace chromium
+}  // namespace org
+
+namespace org {
+namespace chromium {
+
 // Interface proxy for org::chromium::Itf1.
-class Itf1Proxy final {
+class Itf1Proxy final : public Itf1ProxyInterface {
  public:
   class PropertySet : public dbus::PropertySet {
    public:
@@ -877,8 +1029,21 @@
 namespace org {
 namespace chromium {
 
+// Abstract interface proxy for org::chromium::Itf2.
+class Itf2ProxyInterface {
+ public:
+ protected:
+  ~Itf2ProxyInterface() = default;
+};
+
+}  // namespace chromium
+}  // namespace org
+
+namespace org {
+namespace chromium {
+
 // Interface proxy for org::chromium::Itf2.
-class Itf2Proxy final {
+class Itf2Proxy final : public Itf2ProxyInterface {
  public:
   class PropertySet : public dbus::PropertySet {
    public: