Alex Vakulenko | 008e19d | 2015-01-21 10:37:00 -0800 | [diff] [blame^] | 1 | // 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 | |
| 17 | using std::string; |
| 18 | using std::vector; |
| 19 | using testing::Test; |
| 20 | |
| 21 | namespace chromeos_dbus_bindings { |
| 22 | |
| 23 | namespace { |
| 24 | |
| 25 | const char kInterfaceName[] = "org.chromium.TestInterface"; |
| 26 | const char kInterfaceName2[] = "org.chromium.TestInterface2"; |
| 27 | const char kMethod1Name[] = "Elements"; |
| 28 | const char kMethod1Return[] = "s"; |
| 29 | const char kMethod1Argument1[] = "s"; |
| 30 | const char kMethod1ArgumentName1[] = "space_walk"; |
| 31 | const char kMethod1Argument2[] = "ao"; |
| 32 | const char kMethod1ArgumentName2[] = "ramblin_man"; |
| 33 | const char kMethod2Name[] = "ReturnToPatagonia"; |
| 34 | const char kMethod2Return[] = "x"; |
| 35 | const char kMethod3Name[] = "NiceWeatherForDucks"; |
| 36 | const char kMethod3Argument1[] = "b"; |
| 37 | const char kMethod4Name[] = "ExperimentNumberSix"; |
| 38 | const char kMethod5Name[] = "GetPersonInfo"; |
| 39 | const char kMethod5Argument1[] = "s"; |
| 40 | const char kMethod5ArgumentName1[] = "name"; |
| 41 | const char kMethod5Argument2[] = "i"; |
| 42 | const char kMethod5ArgumentName2[] = "age"; |
| 43 | const char kSignal1Name[] = "Closer"; |
| 44 | const char kSignal2Name[] = "TheCurseOfKaZar"; |
| 45 | const char kSignal2Argument1[] = "as"; |
| 46 | const char kSignal2Argument2[] = "y"; |
| 47 | const char kExpectedContent[] = R"literal_string( |
| 48 | #include <string> |
| 49 | #include <vector> |
| 50 | |
| 51 | #include <base/callback_forward.h> |
| 52 | #include <base/macros.h> |
| 53 | #include <chromeos/any.h> |
| 54 | #include <chromeos/errors/error.h> |
| 55 | #include <chromeos/variant_dictionary.h> |
| 56 | #include <gmock/gmock.h> |
| 57 | |
| 58 | #include "proxies.h" |
| 59 | |
| 60 | namespace org { |
| 61 | namespace chromium { |
| 62 | |
| 63 | // Mock object for TestInterfaceProxyInterface. |
| 64 | class TestInterfaceProxyMock final : public TestInterfaceProxyInterface { |
| 65 | public: |
| 66 | TestInterfaceProxyMock() = default; |
| 67 | |
| 68 | MOCK_METHOD5(Elements, |
| 69 | bool(const std::string& /*in_space_walk*/, |
| 70 | const std::vector<dbus::ObjectPath>& /*in_ramblin_man*/, |
| 71 | std::string*, |
| 72 | chromeos::ErrorPtr* /*error*/, |
| 73 | int /*timeout_ms*/)); |
| 74 | MOCK_METHOD5(ElementsAsync, |
| 75 | void(const std::string& /*in_space_walk*/, |
| 76 | const std::vector<dbus::ObjectPath>& /*in_ramblin_man*/, |
| 77 | const base::Callback<void(const std::string&)>& /*success_callback*/, |
| 78 | const base::Callback<void(chromeos::Error*)>& /*error_callback*/, |
| 79 | int /*timeout_ms*/)); |
| 80 | MOCK_METHOD3(ReturnToPatagonia, |
| 81 | bool(int64_t*, |
| 82 | chromeos::ErrorPtr* /*error*/, |
| 83 | int /*timeout_ms*/)); |
| 84 | MOCK_METHOD3(ReturnToPatagoniaAsync, |
| 85 | void(const base::Callback<void(int64_t)>& /*success_callback*/, |
| 86 | const base::Callback<void(chromeos::Error*)>& /*error_callback*/, |
| 87 | int /*timeout_ms*/)); |
| 88 | MOCK_METHOD3(NiceWeatherForDucks, |
| 89 | bool(bool, |
| 90 | chromeos::ErrorPtr* /*error*/, |
| 91 | int /*timeout_ms*/)); |
| 92 | MOCK_METHOD4(NiceWeatherForDucksAsync, |
| 93 | void(bool, |
| 94 | const base::Callback<void()>& /*success_callback*/, |
| 95 | const base::Callback<void(chromeos::Error*)>& /*error_callback*/, |
| 96 | int /*timeout_ms*/)); |
| 97 | MOCK_METHOD2(ExperimentNumberSix, |
| 98 | bool(chromeos::ErrorPtr* /*error*/, |
| 99 | int /*timeout_ms*/)); |
| 100 | MOCK_METHOD3(ExperimentNumberSixAsync, |
| 101 | void(const base::Callback<void()>& /*success_callback*/, |
| 102 | const base::Callback<void(chromeos::Error*)>& /*error_callback*/, |
| 103 | int /*timeout_ms*/)); |
| 104 | |
| 105 | private: |
| 106 | DISALLOW_COPY_AND_ASSIGN(TestInterfaceProxyMock); |
| 107 | }; |
| 108 | } // namespace chromium |
| 109 | } // namespace org |
| 110 | |
| 111 | namespace org { |
| 112 | namespace chromium { |
| 113 | |
| 114 | // Mock object for TestInterface2ProxyInterface. |
| 115 | class TestInterface2ProxyMock final : public TestInterface2ProxyInterface { |
| 116 | public: |
| 117 | TestInterface2ProxyMock() = default; |
| 118 | |
| 119 | MOCK_METHOD4(GetPersonInfo, |
| 120 | bool(std::string* /*out_name*/, |
| 121 | int32_t* /*out_age*/, |
| 122 | chromeos::ErrorPtr* /*error*/, |
| 123 | int /*timeout_ms*/)); |
| 124 | MOCK_METHOD3(GetPersonInfoAsync, |
| 125 | void(const base::Callback<void(const std::string& /*name*/, int32_t /*age*/)>& /*success_callback*/, |
| 126 | const base::Callback<void(chromeos::Error*)>& /*error_callback*/, |
| 127 | int /*timeout_ms*/)); |
| 128 | |
| 129 | private: |
| 130 | DISALLOW_COPY_AND_ASSIGN(TestInterface2ProxyMock); |
| 131 | }; |
| 132 | } // namespace chromium |
| 133 | } // namespace org |
| 134 | )literal_string"; |
| 135 | |
| 136 | } // namespace |
| 137 | |
| 138 | class ProxyGeneratorMockTest : public Test { |
| 139 | public: |
| 140 | void SetUp() override { |
| 141 | ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 142 | } |
| 143 | |
| 144 | protected: |
| 145 | base::FilePath CreateInputFile(const string& contents) { |
| 146 | base::FilePath path; |
| 147 | EXPECT_TRUE(base::CreateTemporaryFileInDir(temp_dir_.path(), &path)); |
| 148 | EXPECT_EQ(contents.size(), |
| 149 | base::WriteFile(path, contents.c_str(), contents.size())); |
| 150 | return path; |
| 151 | } |
| 152 | |
| 153 | base::ScopedTempDir temp_dir_; |
| 154 | }; |
| 155 | |
| 156 | TEST_F(ProxyGeneratorMockTest, GenerateMocks) { |
| 157 | Interface interface; |
| 158 | interface.name = kInterfaceName; |
| 159 | interface.path = "/org/chromium/Test"; |
| 160 | interface.methods.emplace_back( |
| 161 | kMethod1Name, |
| 162 | vector<Interface::Argument>{ |
| 163 | {kMethod1ArgumentName1, kMethod1Argument1}, |
| 164 | {kMethod1ArgumentName2, kMethod1Argument2}}, |
| 165 | vector<Interface::Argument>{{"", kMethod1Return}}); |
| 166 | interface.methods.emplace_back( |
| 167 | kMethod2Name, |
| 168 | vector<Interface::Argument>{}, |
| 169 | vector<Interface::Argument>{{"", kMethod2Return}}); |
| 170 | interface.methods.emplace_back( |
| 171 | kMethod3Name, |
| 172 | vector<Interface::Argument>{{"", kMethod3Argument1}}, |
| 173 | vector<Interface::Argument>{}); |
| 174 | interface.methods.emplace_back(kMethod4Name); |
| 175 | interface.signals.emplace_back(kSignal1Name); |
| 176 | interface.signals.emplace_back( |
| 177 | kSignal2Name, |
| 178 | vector<Interface::Argument>{ |
| 179 | {"", kSignal2Argument1}, |
| 180 | {"", kSignal2Argument2}}); |
| 181 | interface.methods.back().doc_string = "Comment line1\nline2"; |
| 182 | Interface interface2; |
| 183 | interface2.name = kInterfaceName2; |
| 184 | interface2.methods.emplace_back( |
| 185 | kMethod5Name, |
| 186 | vector<Interface::Argument>{}, |
| 187 | vector<Interface::Argument>{ |
| 188 | {kMethod5ArgumentName1, kMethod5Argument1}, |
| 189 | {kMethod5ArgumentName2, kMethod5Argument2}}); |
| 190 | vector<Interface> interfaces{interface, interface2}; |
| 191 | base::FilePath output_path = temp_dir_.path().Append("output.h"); |
| 192 | base::FilePath proxy_path = temp_dir_.path().Append("proxies.h"); |
| 193 | ServiceConfig config; |
| 194 | EXPECT_TRUE(ProxyGenerator::GenerateMocks(config, interfaces, output_path, |
| 195 | proxy_path)); |
| 196 | string contents; |
| 197 | EXPECT_TRUE(base::ReadFileToString(output_path, &contents)); |
| 198 | // The header guards contain the (temporary) filename, so we search for |
| 199 | // the content we need within the string. |
| 200 | EXPECT_NE(string::npos, contents.find(kExpectedContent)) |
| 201 | << "Expected to find the following content...\n" |
| 202 | << kExpectedContent << "...within content...\n" << contents; |
| 203 | } |
| 204 | |
| 205 | } // namespace chromeos_dbus_bindings |