summaryrefslogtreecommitdiff
path: root/chromeos-dbus-bindings/adaptor_generator_unittest.cc
blob: edd1e3ac89ce5dee26b571ce46dcafdc74bbed73 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// Copyright 2014 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chromeos-dbus-bindings/adaptor_generator.h"

#include <string>
#include <vector>

#include <base/file_util.h>
#include <base/files/file_path.h>
#include <base/files/scoped_temp_dir.h>
#include <gtest/gtest.h>

#include "chromeos-dbus-bindings/interface.h"

using std::string;
using std::vector;
using testing::Test;

namespace chromeos_dbus_bindings {

namespace {

const char kMethod0Name[] = "Kaneda";
const char kMethod0Return[] = "s";
const char kMethod0Argument0[] = "s";
const char kMethod0ArgumentName0[] = "iwata";
const char kMethod0Argument1[] = "ao";
const char kMethod0ArgumentName1[] = "clarke";
const char kMethod1Name[] = "Tetsuo";
const char kMethod1Argument1[] = "i";
const char kMethod1Return[] = "x";
const char kMethod2Name[] = "Kei";
const char kMethod3Name[] = "Kiyoko";
const char kMethod3Return0[] = "x";
const char kMethod3Return1[] = "s";
const char kInterfaceName[] = "org.chromium.TestInterface";
const char kExpectedContent[] = R"literal_string(
#include <string>
#include <vector>

#include <base/macros.h>
#include <dbus/object_path.h>
#include <chromeos/dbus/dbus_object.h>
#include <chromeos/dbus/exported_object_manager.h>
#include <chromeos/variant_dictionary.h>

namespace org {
namespace chromium {

class TestInterfaceAdaptor {
 public:
  class MethodInterface {
   public:
    virtual std::string Kaneda(
        chromeos::ErrorPtr* /* error */,
        const std::string& /* iwata */,
        const std::vector<dbus::ObjectPath>& /* clarke */) = 0;
    virtual int64_t Tetsuo(
        chromeos::ErrorPtr* /* error */,
        int32_t) = 0;
    virtual void Kei(
        chromeos::ErrorPtr* /* error */) = 0;
  };
  TestInterfaceAdaptor(
      chromeos::dbus_utils::ExportedObjectManager* object_manager,
      const std::string& object_path,
      MethodInterface* interface)  // Owned by caller.
      : interface_(interface),
        dbus_object_(
            object_manager,
            object_manager->GetBus(),
            dbus::ObjectPath(object_path)),
        dbus_interface_(
            dbus_object_.AddOrGetInterface("org.chromium.TestInterface")) {
    dbus_interface_->AddMethodHandler(
        "Kaneda",
        base::Unretained(interface_),
        &MethodInterface::Kaneda);
    dbus_interface_->AddMethodHandler(
        "Tetsuo",
        base::Unretained(interface_),
        &MethodInterface::Tetsuo);
    dbus_interface_->AddMethodHandler(
        "Kei",
        base::Unretained(interface_),
        &MethodInterface::Kei);
    dbus_object_.RegisterAsync(base::Bind(
        &TestInterfaceAdaptor::OnRegisterComplete, base::Unretained(this)));
  }
  virtual ~TestInterfaceAdaptor() = default;
  virtual void OnRegisterComplete(bool success) {}

 protected:
  chromeos::dbus_utils::DBusInterface* dbus_interface() {
    return dbus_interface_;
  }

 private:
  MethodInterface* interface_;  // Owned by caller.
  chromeos::dbus_utils::DBusObject dbus_object_;
  // Owned by |dbus_object_|.
  chromeos::dbus_utils::DBusInterface* dbus_interface_;
  DISALLOW_COPY_AND_ASSIGN(TestInterfaceAdaptor);
};

}  // namespace chromium
}  // namespace org
)literal_string";

}  // namespace
class AdaptorGeneratorTest : public Test {
 public:
  void SetUp() override {
    ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
  }

 protected:
  base::FilePath CreateInputFile(const string& contents) {
    base::FilePath path;
    EXPECT_TRUE(base::CreateTemporaryFileInDir(temp_dir_.path(), &path));
    EXPECT_EQ(contents.size(),
              base::WriteFile(path, contents.c_str(), contents.size()));
    return path;
  }

  base::ScopedTempDir temp_dir_;
  AdaptorGenerator generator_;
};

TEST_F(AdaptorGeneratorTest, GenerateAdaptors) {
  Interface interface;
  interface.name = kInterfaceName;
  interface.methods.emplace_back(
      kMethod0Name,
      vector<Interface::Argument>{
          {kMethod0ArgumentName0, kMethod0Argument0},
          {kMethod0ArgumentName1, kMethod0Argument1}},
      vector<Interface::Argument>{{"", kMethod0Return}});
  interface.methods.emplace_back(
      kMethod1Name,
      vector<Interface::Argument>{{"", kMethod1Argument1}},
      vector<Interface::Argument>{{"", kMethod1Return}});
  interface.methods.emplace_back(kMethod2Name);
  // Interface methods with more than one return argument should be ignored.
  interface.methods.emplace_back(
      kMethod3Name,
      vector<Interface::Argument>{},
      vector<Interface::Argument>{
          {"", kMethod3Return0},
          {"", kMethod3Return1}});
  base::FilePath output_path = temp_dir_.path().Append("output.h");
  EXPECT_TRUE(generator_.GenerateAdaptor(interface, output_path));
  string contents;
  EXPECT_TRUE(base::ReadFileToString(output_path, &contents));
  // The header guards contain the (temporary) filename, so we search for
  // the content we need within the string.
  EXPECT_NE(string::npos, contents.find(kExpectedContent))
      << "Expected to find the following content...\n"
      << kExpectedContent << "...within content...\n" << contents;
}

}  // namespace chromeos_dbus_bindings