blob: f12b8ec87c8aebcb77646d2e5f2083d8aa4c1744 [file] [log] [blame]
Alex Vakulenko008e19d2015-01-21 10:37:00 -08001// Copyright 2015 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
10#include <base/files/file_path.h>
11#include <base/files/file_util.h>
12#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
Alex Vakulenkob69be602015-02-23 14:40:42 -080025const char kDBusTypeArryOfObjects[] = "ao";
26const char kDBusTypeArryOfStrings[] = "as";
27const char kDBusTypeBool[] = "b";
28const char kDBusTypeByte[] = "y";
29const char kDBusTypeInt32[] = "i";
30const char kDBusTypeInt64[] = "x";
31const char kDBusTypeString[] = "s";
32
Alex Vakulenko008e19d2015-01-21 10:37:00 -080033const char kExpectedContent[] = R"literal_string(
34#include <string>
35#include <vector>
36
37#include <base/callback_forward.h>
38#include <base/macros.h>
39#include <chromeos/any.h>
40#include <chromeos/errors/error.h>
41#include <chromeos/variant_dictionary.h>
42#include <gmock/gmock.h>
43
44#include "proxies.h"
45
46namespace org {
47namespace chromium {
48
49// Mock object for TestInterfaceProxyInterface.
50class TestInterfaceProxyMock final : public TestInterfaceProxyInterface {
51 public:
52 TestInterfaceProxyMock() = default;
53
54 MOCK_METHOD5(Elements,
55 bool(const std::string& /*in_space_walk*/,
56 const std::vector<dbus::ObjectPath>& /*in_ramblin_man*/,
57 std::string*,
58 chromeos::ErrorPtr* /*error*/,
59 int /*timeout_ms*/));
60 MOCK_METHOD5(ElementsAsync,
61 void(const std::string& /*in_space_walk*/,
62 const std::vector<dbus::ObjectPath>& /*in_ramblin_man*/,
63 const base::Callback<void(const std::string&)>& /*success_callback*/,
64 const base::Callback<void(chromeos::Error*)>& /*error_callback*/,
65 int /*timeout_ms*/));
66 MOCK_METHOD3(ReturnToPatagonia,
67 bool(int64_t*,
68 chromeos::ErrorPtr* /*error*/,
69 int /*timeout_ms*/));
70 MOCK_METHOD3(ReturnToPatagoniaAsync,
71 void(const base::Callback<void(int64_t)>& /*success_callback*/,
72 const base::Callback<void(chromeos::Error*)>& /*error_callback*/,
73 int /*timeout_ms*/));
74 MOCK_METHOD3(NiceWeatherForDucks,
75 bool(bool,
76 chromeos::ErrorPtr* /*error*/,
77 int /*timeout_ms*/));
78 MOCK_METHOD4(NiceWeatherForDucksAsync,
79 void(bool,
80 const base::Callback<void()>& /*success_callback*/,
81 const base::Callback<void(chromeos::Error*)>& /*error_callback*/,
82 int /*timeout_ms*/));
83 MOCK_METHOD2(ExperimentNumberSix,
84 bool(chromeos::ErrorPtr* /*error*/,
85 int /*timeout_ms*/));
86 MOCK_METHOD3(ExperimentNumberSixAsync,
87 void(const base::Callback<void()>& /*success_callback*/,
88 const base::Callback<void(chromeos::Error*)>& /*error_callback*/,
89 int /*timeout_ms*/));
90
91 private:
92 DISALLOW_COPY_AND_ASSIGN(TestInterfaceProxyMock);
93};
94} // namespace chromium
95} // namespace org
96
97namespace org {
98namespace chromium {
99
100// Mock object for TestInterface2ProxyInterface.
101class TestInterface2ProxyMock final : public TestInterface2ProxyInterface {
102 public:
103 TestInterface2ProxyMock() = default;
104
105 MOCK_METHOD4(GetPersonInfo,
106 bool(std::string* /*out_name*/,
107 int32_t* /*out_age*/,
108 chromeos::ErrorPtr* /*error*/,
109 int /*timeout_ms*/));
110 MOCK_METHOD3(GetPersonInfoAsync,
111 void(const base::Callback<void(const std::string& /*name*/, int32_t /*age*/)>& /*success_callback*/,
112 const base::Callback<void(chromeos::Error*)>& /*error_callback*/,
113 int /*timeout_ms*/));
114
115 private:
116 DISALLOW_COPY_AND_ASSIGN(TestInterface2ProxyMock);
117};
118} // namespace chromium
119} // namespace org
120)literal_string";
121
122} // namespace
123
124class ProxyGeneratorMockTest : public Test {
125 public:
126 void SetUp() override {
127 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
128 }
129
130 protected:
131 base::FilePath CreateInputFile(const string& contents) {
132 base::FilePath path;
133 EXPECT_TRUE(base::CreateTemporaryFileInDir(temp_dir_.path(), &path));
134 EXPECT_EQ(contents.size(),
135 base::WriteFile(path, contents.c_str(), contents.size()));
136 return path;
137 }
138
139 base::ScopedTempDir temp_dir_;
140};
141
142TEST_F(ProxyGeneratorMockTest, GenerateMocks) {
143 Interface interface;
Alex Vakulenkob69be602015-02-23 14:40:42 -0800144 interface.name = "org.chromium.TestInterface";
Alex Vakulenko008e19d2015-01-21 10:37:00 -0800145 interface.path = "/org/chromium/Test";
146 interface.methods.emplace_back(
Alex Vakulenkob69be602015-02-23 14:40:42 -0800147 "Elements",
Alex Vakulenko008e19d2015-01-21 10:37:00 -0800148 vector<Interface::Argument>{
Alex Vakulenkob69be602015-02-23 14:40:42 -0800149 {"space_walk", kDBusTypeString},
150 {"ramblin_man", kDBusTypeArryOfObjects}},
151 vector<Interface::Argument>{{"", kDBusTypeString}});
Alex Vakulenko008e19d2015-01-21 10:37:00 -0800152 interface.methods.emplace_back(
Alex Vakulenkob69be602015-02-23 14:40:42 -0800153 "ReturnToPatagonia",
Alex Vakulenko008e19d2015-01-21 10:37:00 -0800154 vector<Interface::Argument>{},
Alex Vakulenkob69be602015-02-23 14:40:42 -0800155 vector<Interface::Argument>{{"", kDBusTypeInt64}});
Alex Vakulenko008e19d2015-01-21 10:37:00 -0800156 interface.methods.emplace_back(
Alex Vakulenkob69be602015-02-23 14:40:42 -0800157 "NiceWeatherForDucks",
158 vector<Interface::Argument>{{"", kDBusTypeBool}},
Alex Vakulenko008e19d2015-01-21 10:37:00 -0800159 vector<Interface::Argument>{});
Alex Vakulenkob69be602015-02-23 14:40:42 -0800160 interface.methods.emplace_back("ExperimentNumberSix");
161 interface.signals.emplace_back("Closer");
Alex Vakulenko008e19d2015-01-21 10:37:00 -0800162 interface.signals.emplace_back(
Alex Vakulenkob69be602015-02-23 14:40:42 -0800163 "TheCurseOfKaZar",
Alex Vakulenko008e19d2015-01-21 10:37:00 -0800164 vector<Interface::Argument>{
Alex Vakulenkob69be602015-02-23 14:40:42 -0800165 {"", kDBusTypeArryOfStrings},
166 {"", kDBusTypeByte}});
Alex Vakulenko008e19d2015-01-21 10:37:00 -0800167 interface.methods.back().doc_string = "Comment line1\nline2";
168 Interface interface2;
Alex Vakulenkob69be602015-02-23 14:40:42 -0800169 interface2.name = "org.chromium.TestInterface2";
Alex Vakulenko008e19d2015-01-21 10:37:00 -0800170 interface2.methods.emplace_back(
Alex Vakulenkob69be602015-02-23 14:40:42 -0800171 "GetPersonInfo",
Alex Vakulenko008e19d2015-01-21 10:37:00 -0800172 vector<Interface::Argument>{},
173 vector<Interface::Argument>{
Alex Vakulenkob69be602015-02-23 14:40:42 -0800174 {"name", kDBusTypeString},
175 {"age", kDBusTypeInt32}});
Alex Vakulenko008e19d2015-01-21 10:37:00 -0800176 vector<Interface> interfaces{interface, interface2};
177 base::FilePath output_path = temp_dir_.path().Append("output.h");
178 base::FilePath proxy_path = temp_dir_.path().Append("proxies.h");
179 ServiceConfig config;
180 EXPECT_TRUE(ProxyGenerator::GenerateMocks(config, interfaces, output_path,
181 proxy_path));
182 string contents;
183 EXPECT_TRUE(base::ReadFileToString(output_path, &contents));
184 // The header guards contain the (temporary) filename, so we search for
185 // the content we need within the string.
186 EXPECT_NE(string::npos, contents.find(kExpectedContent))
187 << "Expected to find the following content...\n"
188 << kExpectedContent << "...within content...\n" << contents;
189}
190
191} // namespace chromeos_dbus_bindings