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