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 | |
Alex Vakulenko | b69be60 | 2015-02-23 14:40:42 -0800 | [diff] [blame] | 25 | const char kDBusTypeArryOfObjects[] = "ao"; |
| 26 | const char kDBusTypeArryOfStrings[] = "as"; |
| 27 | const char kDBusTypeBool[] = "b"; |
| 28 | const char kDBusTypeByte[] = "y"; |
| 29 | const char kDBusTypeInt32[] = "i"; |
| 30 | const char kDBusTypeInt64[] = "x"; |
| 31 | const char kDBusTypeString[] = "s"; |
| 32 | |
Alex Vakulenko | 008e19d | 2015-01-21 10:37:00 -0800 | [diff] [blame] | 33 | const 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 | |
| 46 | namespace org { |
| 47 | namespace chromium { |
| 48 | |
| 49 | // Mock object for TestInterfaceProxyInterface. |
| 50 | class 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 | |
| 97 | namespace org { |
| 98 | namespace chromium { |
| 99 | |
| 100 | // Mock object for TestInterface2ProxyInterface. |
| 101 | class 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 | |
| 124 | class 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)); |
Christopher Wiley | d0cb3fe | 2015-07-27 17:32:58 -0700 | [diff] [blame^] | 134 | int written = base::WriteFile(path, contents.c_str(), contents.size()); |
| 135 | EXPECT_EQ(contents.size(), static_cast<size_t>(written)); |
Alex Vakulenko | 008e19d | 2015-01-21 10:37:00 -0800 | [diff] [blame] | 136 | return path; |
| 137 | } |
| 138 | |
| 139 | base::ScopedTempDir temp_dir_; |
| 140 | }; |
| 141 | |
| 142 | TEST_F(ProxyGeneratorMockTest, GenerateMocks) { |
| 143 | Interface interface; |
Alex Vakulenko | b69be60 | 2015-02-23 14:40:42 -0800 | [diff] [blame] | 144 | interface.name = "org.chromium.TestInterface"; |
Alex Vakulenko | 008e19d | 2015-01-21 10:37:00 -0800 | [diff] [blame] | 145 | interface.path = "/org/chromium/Test"; |
| 146 | interface.methods.emplace_back( |
Alex Vakulenko | b69be60 | 2015-02-23 14:40:42 -0800 | [diff] [blame] | 147 | "Elements", |
Alex Vakulenko | 008e19d | 2015-01-21 10:37:00 -0800 | [diff] [blame] | 148 | vector<Interface::Argument>{ |
Alex Vakulenko | b69be60 | 2015-02-23 14:40:42 -0800 | [diff] [blame] | 149 | {"space_walk", kDBusTypeString}, |
| 150 | {"ramblin_man", kDBusTypeArryOfObjects}}, |
| 151 | vector<Interface::Argument>{{"", kDBusTypeString}}); |
Alex Vakulenko | 008e19d | 2015-01-21 10:37:00 -0800 | [diff] [blame] | 152 | interface.methods.emplace_back( |
Alex Vakulenko | b69be60 | 2015-02-23 14:40:42 -0800 | [diff] [blame] | 153 | "ReturnToPatagonia", |
Alex Vakulenko | 008e19d | 2015-01-21 10:37:00 -0800 | [diff] [blame] | 154 | vector<Interface::Argument>{}, |
Alex Vakulenko | b69be60 | 2015-02-23 14:40:42 -0800 | [diff] [blame] | 155 | vector<Interface::Argument>{{"", kDBusTypeInt64}}); |
Alex Vakulenko | 008e19d | 2015-01-21 10:37:00 -0800 | [diff] [blame] | 156 | interface.methods.emplace_back( |
Alex Vakulenko | b69be60 | 2015-02-23 14:40:42 -0800 | [diff] [blame] | 157 | "NiceWeatherForDucks", |
| 158 | vector<Interface::Argument>{{"", kDBusTypeBool}}, |
Alex Vakulenko | 008e19d | 2015-01-21 10:37:00 -0800 | [diff] [blame] | 159 | vector<Interface::Argument>{}); |
Alex Vakulenko | b69be60 | 2015-02-23 14:40:42 -0800 | [diff] [blame] | 160 | interface.methods.emplace_back("ExperimentNumberSix"); |
| 161 | interface.signals.emplace_back("Closer"); |
Alex Vakulenko | 008e19d | 2015-01-21 10:37:00 -0800 | [diff] [blame] | 162 | interface.signals.emplace_back( |
Alex Vakulenko | b69be60 | 2015-02-23 14:40:42 -0800 | [diff] [blame] | 163 | "TheCurseOfKaZar", |
Alex Vakulenko | 008e19d | 2015-01-21 10:37:00 -0800 | [diff] [blame] | 164 | vector<Interface::Argument>{ |
Alex Vakulenko | b69be60 | 2015-02-23 14:40:42 -0800 | [diff] [blame] | 165 | {"", kDBusTypeArryOfStrings}, |
| 166 | {"", kDBusTypeByte}}); |
Alex Vakulenko | 008e19d | 2015-01-21 10:37:00 -0800 | [diff] [blame] | 167 | interface.methods.back().doc_string = "Comment line1\nline2"; |
| 168 | Interface interface2; |
Alex Vakulenko | b69be60 | 2015-02-23 14:40:42 -0800 | [diff] [blame] | 169 | interface2.name = "org.chromium.TestInterface2"; |
Alex Vakulenko | 008e19d | 2015-01-21 10:37:00 -0800 | [diff] [blame] | 170 | interface2.methods.emplace_back( |
Alex Vakulenko | b69be60 | 2015-02-23 14:40:42 -0800 | [diff] [blame] | 171 | "GetPersonInfo", |
Alex Vakulenko | 008e19d | 2015-01-21 10:37:00 -0800 | [diff] [blame] | 172 | vector<Interface::Argument>{}, |
| 173 | vector<Interface::Argument>{ |
Alex Vakulenko | b69be60 | 2015-02-23 14:40:42 -0800 | [diff] [blame] | 174 | {"name", kDBusTypeString}, |
| 175 | {"age", kDBusTypeInt32}}); |
Alex Vakulenko | 008e19d | 2015-01-21 10:37:00 -0800 | [diff] [blame] | 176 | 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 |