Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #ifndef AAPT_TEST_COMMON_H |
| 18 | #define AAPT_TEST_COMMON_H |
| 19 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 20 | #include <iostream> |
| 21 | |
| 22 | #include "android-base/logging.h" |
| 23 | #include "android-base/macros.h" |
Mårten Kongstad | 5c541f6 | 2018-06-20 08:46:41 +0200 | [diff] [blame] | 24 | #include "androidfw/ConfigDescription.h" |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 25 | #include "androidfw/StringPiece.h" |
Adam Lesinski | 5924d8c | 2017-05-30 15:15:58 -0700 | [diff] [blame] | 26 | #include "gmock/gmock.h" |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 27 | #include "gtest/gtest.h" |
| 28 | |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 29 | #include "Debug.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 30 | #include "ResourceTable.h" |
| 31 | #include "ResourceUtils.h" |
Adam Lesinski | 5924d8c | 2017-05-30 15:15:58 -0700 | [diff] [blame] | 32 | #include "ResourceValues.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 33 | #include "ValueVisitor.h" |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 34 | #include "io/File.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 35 | #include "process/IResourceTableConsumer.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 36 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 37 | namespace aapt { |
| 38 | namespace test { |
| 39 | |
Jeremy Meyer | 3d8d4a1 | 2024-08-23 17:29:03 -0700 | [diff] [blame] | 40 | struct TestDiagnosticsImpl : public android::IDiagnostics { |
| 41 | void Log(Level level, android::DiagMessageActual& actual_msg) override { |
| 42 | switch (level) { |
| 43 | case Level::Note: |
| 44 | return; |
| 45 | |
| 46 | case Level::Warn: |
| 47 | std::cerr << actual_msg.source << ": warn: " << actual_msg.message << "." << std::endl; |
| 48 | log << actual_msg.source << ": warn: " << actual_msg.message << "." << std::endl; |
| 49 | break; |
| 50 | |
| 51 | case Level::Error: |
| 52 | std::cerr << actual_msg.source << ": error: " << actual_msg.message << "." << std::endl; |
| 53 | log << actual_msg.source << ": error: " << actual_msg.message << "." << std::endl; |
| 54 | break; |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | std::string GetLog() { |
| 59 | return log.str(); |
| 60 | } |
| 61 | |
| 62 | private: |
| 63 | std::ostringstream log; |
| 64 | }; |
| 65 | |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 66 | android::IDiagnostics* GetDiagnostics(); |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 67 | |
Yurii Zubrytskyi | a577514 | 2022-11-02 17:49:49 -0700 | [diff] [blame] | 68 | inline ResourceName ParseNameOrDie(android::StringPiece str) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 69 | ResourceNameRef ref; |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 70 | CHECK(ResourceUtils::ParseResourceName(str, &ref)) << "invalid resource name: " << str; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 71 | return ref.ToResourceName(); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 72 | } |
| 73 | |
Yurii Zubrytskyi | a577514 | 2022-11-02 17:49:49 -0700 | [diff] [blame] | 74 | inline android::ConfigDescription ParseConfigOrDie(android::StringPiece str) { |
| 75 | android::ConfigDescription config; |
Mårten Kongstad | 5c541f6 | 2018-06-20 08:46:41 +0200 | [diff] [blame] | 76 | CHECK(android::ConfigDescription::Parse(str, &config)) << "invalid configuration: " << str; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 77 | return config; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 78 | } |
| 79 | |
Adam Lesinski | bab4ef5 | 2017-06-01 15:22:57 -0700 | [diff] [blame] | 80 | template <typename T = Value> |
Yurii Zubrytskyi | a577514 | 2022-11-02 17:49:49 -0700 | [diff] [blame] | 81 | T* GetValueForConfigAndProduct(ResourceTable* table, android::StringPiece res_name, |
Mårten Kongstad | 5c541f6 | 2018-06-20 08:46:41 +0200 | [diff] [blame] | 82 | const android::ConfigDescription& config, |
Yurii Zubrytskyi | a577514 | 2022-11-02 17:49:49 -0700 | [diff] [blame] | 83 | android::StringPiece product) { |
Ryan Mitchell | 4382e44 | 2021-07-14 12:53:01 -0700 | [diff] [blame] | 84 | std::optional<ResourceTable::SearchResult> result = table->FindResource(ParseNameOrDie(res_name)); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 85 | if (result) { |
Adam Lesinski | 5924d8c | 2017-05-30 15:15:58 -0700 | [diff] [blame] | 86 | ResourceConfigValue* config_value = result.value().entry->FindValue(config, product); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 87 | if (config_value) { |
| 88 | return ValueCast<T>(config_value->value.get()); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 89 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 90 | } |
| 91 | return nullptr; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 92 | } |
| 93 | |
Adam Lesinski | bab4ef5 | 2017-06-01 15:22:57 -0700 | [diff] [blame] | 94 | template <> |
Yurii Zubrytskyi | a577514 | 2022-11-02 17:49:49 -0700 | [diff] [blame] | 95 | Value* GetValueForConfigAndProduct<Value>(ResourceTable* table, android::StringPiece res_name, |
Mårten Kongstad | 5c541f6 | 2018-06-20 08:46:41 +0200 | [diff] [blame] | 96 | const android::ConfigDescription& config, |
Yurii Zubrytskyi | a577514 | 2022-11-02 17:49:49 -0700 | [diff] [blame] | 97 | android::StringPiece product); |
Adam Lesinski | bab4ef5 | 2017-06-01 15:22:57 -0700 | [diff] [blame] | 98 | |
| 99 | template <typename T = Value> |
Yurii Zubrytskyi | a577514 | 2022-11-02 17:49:49 -0700 | [diff] [blame] | 100 | T* GetValueForConfig(ResourceTable* table, android::StringPiece res_name, |
Mårten Kongstad | 5c541f6 | 2018-06-20 08:46:41 +0200 | [diff] [blame] | 101 | const android::ConfigDescription& config) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 102 | return GetValueForConfigAndProduct<T>(table, res_name, config, {}); |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 103 | } |
| 104 | |
Adam Lesinski | bab4ef5 | 2017-06-01 15:22:57 -0700 | [diff] [blame] | 105 | template <typename T = Value> |
Yurii Zubrytskyi | a577514 | 2022-11-02 17:49:49 -0700 | [diff] [blame] | 106 | T* GetValue(ResourceTable* table, android::StringPiece res_name) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 107 | return GetValueForConfig<T>(table, res_name, {}); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 108 | } |
| 109 | |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 110 | class TestFile : public io::IFile { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 111 | public: |
Yurii Zubrytskyi | a577514 | 2022-11-02 17:49:49 -0700 | [diff] [blame] | 112 | explicit TestFile(android::StringPiece path) : source_(path) { |
| 113 | } |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 114 | |
Adam Lesinski | 5924d8c | 2017-05-30 15:15:58 -0700 | [diff] [blame] | 115 | std::unique_ptr<io::IData> OpenAsData() override { |
| 116 | return {}; |
| 117 | } |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 118 | |
Jeremy Meyer | b4f83ff | 2023-11-30 19:29:50 +0000 | [diff] [blame] | 119 | std::unique_ptr<android::InputStream> OpenInputStream() override { |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 120 | return OpenAsData(); |
| 121 | } |
| 122 | |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 123 | const android::Source& GetSource() const override { |
Adam Lesinski | 5924d8c | 2017-05-30 15:15:58 -0700 | [diff] [blame] | 124 | return source_; |
| 125 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 126 | |
Mark Punzalan | e567159 | 2023-09-02 00:00:30 +0000 | [diff] [blame] | 127 | bool GetModificationTime(struct tm* buf) const override { |
| 128 | return false; |
| 129 | }; |
| 130 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 131 | private: |
| 132 | DISALLOW_COPY_AND_ASSIGN(TestFile); |
| 133 | |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 134 | android::Source source_; |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 135 | }; |
| 136 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 137 | } // namespace test |
Adam Lesinski | 5924d8c | 2017-05-30 15:15:58 -0700 | [diff] [blame] | 138 | |
| 139 | // Workaround gtest bug (https://github.com/google/googletest/issues/443) |
| 140 | // that does not select base class operator<< for derived class T. |
| 141 | template <typename T> |
| 142 | typename std::enable_if<std::is_base_of<Value, T>::value, std::ostream&>::type operator<<( |
| 143 | std::ostream& out, const T& value) { |
| 144 | value.Print(&out); |
| 145 | return out; |
| 146 | } |
| 147 | |
| 148 | template std::ostream& operator<<<Item>(std::ostream&, const Item&); |
| 149 | template std::ostream& operator<<<Reference>(std::ostream&, const Reference&); |
| 150 | template std::ostream& operator<<<Id>(std::ostream&, const Id&); |
| 151 | template std::ostream& operator<<<RawString>(std::ostream&, const RawString&); |
| 152 | template std::ostream& operator<<<String>(std::ostream&, const String&); |
| 153 | template std::ostream& operator<<<StyledString>(std::ostream&, const StyledString&); |
| 154 | template std::ostream& operator<<<FileReference>(std::ostream&, const FileReference&); |
| 155 | template std::ostream& operator<<<BinaryPrimitive>(std::ostream&, const BinaryPrimitive&); |
| 156 | template std::ostream& operator<<<Attribute>(std::ostream&, const Attribute&); |
| 157 | template std::ostream& operator<<<Style>(std::ostream&, const Style&); |
| 158 | template std::ostream& operator<<<Array>(std::ostream&, const Array&); |
| 159 | template std::ostream& operator<<<Plural>(std::ostream&, const Plural&); |
| 160 | |
| 161 | // Add a print method to Maybe. |
| 162 | template <typename T> |
Ryan Mitchell | 4382e44 | 2021-07-14 12:53:01 -0700 | [diff] [blame] | 163 | void PrintTo(const std::optional<T>& value, std::ostream* out) { |
Adam Lesinski | 5924d8c | 2017-05-30 15:15:58 -0700 | [diff] [blame] | 164 | if (value) { |
| 165 | *out << ::testing::PrintToString(value.value()); |
| 166 | } else { |
| 167 | *out << "Nothing"; |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | namespace test { |
| 172 | |
Adam Lesinski | fba0cf2 | 2017-06-29 17:53:36 -0700 | [diff] [blame] | 173 | MATCHER_P(StrEq, a, |
| 174 | std::string(negation ? "isn't" : "is") + " equal to " + |
| 175 | ::testing::PrintToString(android::StringPiece16(a))) { |
| 176 | return android::StringPiece16(arg) == a; |
| 177 | } |
| 178 | |
Adam Lesinski | bbf4297 | 2018-02-14 13:36:09 -0800 | [diff] [blame] | 179 | template <typename T> |
| 180 | class ValueEqImpl : public ::testing::MatcherInterface<T> { |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 181 | public: |
Adam Lesinski | bbf4297 | 2018-02-14 13:36:09 -0800 | [diff] [blame] | 182 | explicit ValueEqImpl(const Value* expected) : expected_(expected) { |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 183 | } |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 184 | |
Adam Lesinski | bbf4297 | 2018-02-14 13:36:09 -0800 | [diff] [blame] | 185 | bool MatchAndExplain(T x, ::testing::MatchResultListener* listener) const override { |
| 186 | return expected_->Equals(&x); |
| 187 | } |
| 188 | |
| 189 | void DescribeTo(::std::ostream* os) const override { |
| 190 | *os << "is equal to " << *expected_; |
| 191 | } |
| 192 | |
| 193 | void DescribeNegationTo(::std::ostream* os) const override { |
| 194 | *os << "is not equal to " << *expected_; |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | private: |
Adam Lesinski | bbf4297 | 2018-02-14 13:36:09 -0800 | [diff] [blame] | 198 | DISALLOW_COPY_AND_ASSIGN(ValueEqImpl); |
| 199 | |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 200 | const Value* expected_; |
| 201 | }; |
| 202 | |
Adam Lesinski | bbf4297 | 2018-02-14 13:36:09 -0800 | [diff] [blame] | 203 | template <typename TValue> |
| 204 | class ValueEqMatcher { |
| 205 | public: |
Chih-Hung Hsieh | 1fc78e1 | 2018-12-20 13:37:44 -0800 | [diff] [blame] | 206 | // NOLINTNEXTLINE(google-explicit-constructor) |
Adam Lesinski | bbf4297 | 2018-02-14 13:36:09 -0800 | [diff] [blame] | 207 | ValueEqMatcher(TValue expected) : expected_(std::move(expected)) { |
| 208 | } |
| 209 | |
| 210 | template <typename T> |
Chih-Hung Hsieh | 1fc78e1 | 2018-12-20 13:37:44 -0800 | [diff] [blame] | 211 | // NOLINTNEXTLINE(google-explicit-constructor) |
Adam Lesinski | bbf4297 | 2018-02-14 13:36:09 -0800 | [diff] [blame] | 212 | operator ::testing::Matcher<T>() const { |
| 213 | return ::testing::Matcher<T>(new ValueEqImpl<T>(&expected_)); |
| 214 | } |
| 215 | |
| 216 | private: |
| 217 | TValue expected_; |
| 218 | }; |
| 219 | |
| 220 | template <typename TValue> |
| 221 | class ValueEqPointerMatcher { |
| 222 | public: |
Chih-Hung Hsieh | 1fc78e1 | 2018-12-20 13:37:44 -0800 | [diff] [blame] | 223 | // NOLINTNEXTLINE(google-explicit-constructor) |
Adam Lesinski | bbf4297 | 2018-02-14 13:36:09 -0800 | [diff] [blame] | 224 | ValueEqPointerMatcher(const TValue* expected) : expected_(expected) { |
| 225 | } |
| 226 | |
| 227 | template <typename T> |
Chih-Hung Hsieh | 1fc78e1 | 2018-12-20 13:37:44 -0800 | [diff] [blame] | 228 | // NOLINTNEXTLINE(google-explicit-constructor) |
Adam Lesinski | bbf4297 | 2018-02-14 13:36:09 -0800 | [diff] [blame] | 229 | operator ::testing::Matcher<T>() const { |
| 230 | return ::testing::Matcher<T>(new ValueEqImpl<T>(expected_)); |
| 231 | } |
| 232 | |
| 233 | private: |
| 234 | const TValue* expected_; |
| 235 | }; |
| 236 | |
| 237 | template <typename TValue, |
| 238 | typename = typename std::enable_if<!std::is_pointer<TValue>::value, void>::type> |
| 239 | inline ValueEqMatcher<TValue> ValueEq(TValue value) { |
| 240 | return ValueEqMatcher<TValue>(std::move(value)); |
| 241 | } |
| 242 | |
| 243 | template <typename TValue> |
| 244 | inline ValueEqPointerMatcher<TValue> ValueEq(const TValue* value) { |
| 245 | return ValueEqPointerMatcher<TValue>(value); |
| 246 | } |
Adam Lesinski | 5924d8c | 2017-05-30 15:15:58 -0700 | [diff] [blame] | 247 | |
Adam Lesinski | e385674 | 2017-06-12 14:55:58 -0700 | [diff] [blame] | 248 | MATCHER_P(StrValueEq, a, |
| 249 | std::string(negation ? "isn't" : "is") + " equal to " + ::testing::PrintToString(a)) { |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 250 | return *(arg.value) == a; |
| 251 | } |
| 252 | |
Adam Lesinski | e385674 | 2017-06-12 14:55:58 -0700 | [diff] [blame] | 253 | MATCHER_P(HasValue, name, |
| 254 | std::string(negation ? "does not have" : "has") + " value " + |
| 255 | ::testing::PrintToString(name)) { |
| 256 | return GetValueForConfig<Value>(&(*arg), name, {}) != nullptr; |
| 257 | } |
| 258 | |
| 259 | MATCHER_P2(HasValue, name, config, |
| 260 | std::string(negation ? "does not have" : "has") + " value " + |
| 261 | ::testing::PrintToString(name) + " for config " + ::testing::PrintToString(config)) { |
| 262 | return GetValueForConfig<Value>(&(*arg), name, config) != nullptr; |
| 263 | } |
| 264 | |
Adam Lesinski | 5924d8c | 2017-05-30 15:15:58 -0700 | [diff] [blame] | 265 | } // namespace test |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 266 | } // namespace aapt |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 267 | |
| 268 | #endif /* AAPT_TEST_COMMON_H */ |