blob: b06c4329488e2943c00e08f81e7c0eba36d97b4b [file] [log] [blame]
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001/*
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 Lesinskice5e56e2016-10-21 17:56:45 -070020#include <iostream>
21
22#include "android-base/logging.h"
23#include "android-base/macros.h"
Mårten Kongstad5c541f62018-06-20 08:46:41 +020024#include "androidfw/ConfigDescription.h"
Adam Lesinskid5083f62017-01-16 15:07:21 -080025#include "androidfw/StringPiece.h"
Adam Lesinski5924d8c2017-05-30 15:15:58 -070026#include "gmock/gmock.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070027#include "gtest/gtest.h"
28
Adam Lesinski9ba47d82015-10-13 11:37:10 -070029#include "Debug.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070030#include "ResourceTable.h"
31#include "ResourceUtils.h"
Adam Lesinski5924d8c2017-05-30 15:15:58 -070032#include "ResourceValues.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070033#include "ValueVisitor.h"
Adam Lesinskia6fe3452015-12-09 15:20:52 -080034#include "io/File.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070035#include "process/IResourceTableConsumer.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070036
Adam Lesinski1ab598f2015-08-14 14:26:04 -070037namespace aapt {
38namespace test {
39
Jeremy Meyer3d8d4a12024-08-23 17:29:03 -070040struct 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 Meyer56f36e82022-05-20 20:35:42 +000066android::IDiagnostics* GetDiagnostics();
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -080067
Yurii Zubrytskyia5775142022-11-02 17:49:49 -070068inline ResourceName ParseNameOrDie(android::StringPiece str) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070069 ResourceNameRef ref;
Shane Farmer0a5b2012017-06-22 12:24:12 -070070 CHECK(ResourceUtils::ParseResourceName(str, &ref)) << "invalid resource name: " << str;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070071 return ref.ToResourceName();
Adam Lesinski1ab598f2015-08-14 14:26:04 -070072}
73
Yurii Zubrytskyia5775142022-11-02 17:49:49 -070074inline android::ConfigDescription ParseConfigOrDie(android::StringPiece str) {
75 android::ConfigDescription config;
Mårten Kongstad5c541f62018-06-20 08:46:41 +020076 CHECK(android::ConfigDescription::Parse(str, &config)) << "invalid configuration: " << str;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070077 return config;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070078}
79
Adam Lesinskibab4ef52017-06-01 15:22:57 -070080template <typename T = Value>
Yurii Zubrytskyia5775142022-11-02 17:49:49 -070081T* GetValueForConfigAndProduct(ResourceTable* table, android::StringPiece res_name,
Mårten Kongstad5c541f62018-06-20 08:46:41 +020082 const android::ConfigDescription& config,
Yurii Zubrytskyia5775142022-11-02 17:49:49 -070083 android::StringPiece product) {
Ryan Mitchell4382e442021-07-14 12:53:01 -070084 std::optional<ResourceTable::SearchResult> result = table->FindResource(ParseNameOrDie(res_name));
Adam Lesinskicacb28f2016-10-19 12:18:14 -070085 if (result) {
Adam Lesinski5924d8c2017-05-30 15:15:58 -070086 ResourceConfigValue* config_value = result.value().entry->FindValue(config, product);
Adam Lesinskice5e56e2016-10-21 17:56:45 -070087 if (config_value) {
88 return ValueCast<T>(config_value->value.get());
Adam Lesinski1ab598f2015-08-14 14:26:04 -070089 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -070090 }
91 return nullptr;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070092}
93
Adam Lesinskibab4ef52017-06-01 15:22:57 -070094template <>
Yurii Zubrytskyia5775142022-11-02 17:49:49 -070095Value* GetValueForConfigAndProduct<Value>(ResourceTable* table, android::StringPiece res_name,
Mårten Kongstad5c541f62018-06-20 08:46:41 +020096 const android::ConfigDescription& config,
Yurii Zubrytskyia5775142022-11-02 17:49:49 -070097 android::StringPiece product);
Adam Lesinskibab4ef52017-06-01 15:22:57 -070098
99template <typename T = Value>
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700100T* GetValueForConfig(ResourceTable* table, android::StringPiece res_name,
Mårten Kongstad5c541f62018-06-20 08:46:41 +0200101 const android::ConfigDescription& config) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700102 return GetValueForConfigAndProduct<T>(table, res_name, config, {});
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800103}
104
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700105template <typename T = Value>
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700106T* GetValue(ResourceTable* table, android::StringPiece res_name) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700107 return GetValueForConfig<T>(table, res_name, {});
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700108}
109
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800110class TestFile : public io::IFile {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700111 public:
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700112 explicit TestFile(android::StringPiece path) : source_(path) {
113 }
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800114
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700115 std::unique_ptr<io::IData> OpenAsData() override {
116 return {};
117 }
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800118
Jeremy Meyerb4f83ff2023-11-30 19:29:50 +0000119 std::unique_ptr<android::InputStream> OpenInputStream() override {
Adam Lesinski00451162017-10-03 07:44:08 -0700120 return OpenAsData();
121 }
122
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000123 const android::Source& GetSource() const override {
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700124 return source_;
125 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700126
Mark Punzalane5671592023-09-02 00:00:30 +0000127 bool GetModificationTime(struct tm* buf) const override {
128 return false;
129 };
130
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700131 private:
132 DISALLOW_COPY_AND_ASSIGN(TestFile);
133
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000134 android::Source source_;
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800135};
136
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700137} // namespace test
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700138
139// Workaround gtest bug (https://github.com/google/googletest/issues/443)
140// that does not select base class operator<< for derived class T.
141template <typename T>
142typename 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
148template std::ostream& operator<<<Item>(std::ostream&, const Item&);
149template std::ostream& operator<<<Reference>(std::ostream&, const Reference&);
150template std::ostream& operator<<<Id>(std::ostream&, const Id&);
151template std::ostream& operator<<<RawString>(std::ostream&, const RawString&);
152template std::ostream& operator<<<String>(std::ostream&, const String&);
153template std::ostream& operator<<<StyledString>(std::ostream&, const StyledString&);
154template std::ostream& operator<<<FileReference>(std::ostream&, const FileReference&);
155template std::ostream& operator<<<BinaryPrimitive>(std::ostream&, const BinaryPrimitive&);
156template std::ostream& operator<<<Attribute>(std::ostream&, const Attribute&);
157template std::ostream& operator<<<Style>(std::ostream&, const Style&);
158template std::ostream& operator<<<Array>(std::ostream&, const Array&);
159template std::ostream& operator<<<Plural>(std::ostream&, const Plural&);
160
161// Add a print method to Maybe.
162template <typename T>
Ryan Mitchell4382e442021-07-14 12:53:01 -0700163void PrintTo(const std::optional<T>& value, std::ostream* out) {
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700164 if (value) {
165 *out << ::testing::PrintToString(value.value());
166 } else {
167 *out << "Nothing";
168 }
169}
170
171namespace test {
172
Adam Lesinskifba0cf22017-06-29 17:53:36 -0700173MATCHER_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 Lesinskibbf42972018-02-14 13:36:09 -0800179template <typename T>
180class ValueEqImpl : public ::testing::MatcherInterface<T> {
Adam Lesinski6b372992017-08-09 10:54:23 -0700181 public:
Adam Lesinskibbf42972018-02-14 13:36:09 -0800182 explicit ValueEqImpl(const Value* expected) : expected_(expected) {
Adam Lesinski6b372992017-08-09 10:54:23 -0700183 }
Adam Lesinski6b372992017-08-09 10:54:23 -0700184
Adam Lesinskibbf42972018-02-14 13:36:09 -0800185 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 Lesinski6b372992017-08-09 10:54:23 -0700195 }
196
197 private:
Adam Lesinskibbf42972018-02-14 13:36:09 -0800198 DISALLOW_COPY_AND_ASSIGN(ValueEqImpl);
199
Adam Lesinski6b372992017-08-09 10:54:23 -0700200 const Value* expected_;
201};
202
Adam Lesinskibbf42972018-02-14 13:36:09 -0800203template <typename TValue>
204class ValueEqMatcher {
205 public:
Chih-Hung Hsieh1fc78e12018-12-20 13:37:44 -0800206 // NOLINTNEXTLINE(google-explicit-constructor)
Adam Lesinskibbf42972018-02-14 13:36:09 -0800207 ValueEqMatcher(TValue expected) : expected_(std::move(expected)) {
208 }
209
210 template <typename T>
Chih-Hung Hsieh1fc78e12018-12-20 13:37:44 -0800211 // NOLINTNEXTLINE(google-explicit-constructor)
Adam Lesinskibbf42972018-02-14 13:36:09 -0800212 operator ::testing::Matcher<T>() const {
213 return ::testing::Matcher<T>(new ValueEqImpl<T>(&expected_));
214 }
215
216 private:
217 TValue expected_;
218};
219
220template <typename TValue>
221class ValueEqPointerMatcher {
222 public:
Chih-Hung Hsieh1fc78e12018-12-20 13:37:44 -0800223 // NOLINTNEXTLINE(google-explicit-constructor)
Adam Lesinskibbf42972018-02-14 13:36:09 -0800224 ValueEqPointerMatcher(const TValue* expected) : expected_(expected) {
225 }
226
227 template <typename T>
Chih-Hung Hsieh1fc78e12018-12-20 13:37:44 -0800228 // NOLINTNEXTLINE(google-explicit-constructor)
Adam Lesinskibbf42972018-02-14 13:36:09 -0800229 operator ::testing::Matcher<T>() const {
230 return ::testing::Matcher<T>(new ValueEqImpl<T>(expected_));
231 }
232
233 private:
234 const TValue* expected_;
235};
236
237template <typename TValue,
238 typename = typename std::enable_if<!std::is_pointer<TValue>::value, void>::type>
239inline ValueEqMatcher<TValue> ValueEq(TValue value) {
240 return ValueEqMatcher<TValue>(std::move(value));
241}
242
243template <typename TValue>
244inline ValueEqPointerMatcher<TValue> ValueEq(const TValue* value) {
245 return ValueEqPointerMatcher<TValue>(value);
246}
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700247
Adam Lesinskie3856742017-06-12 14:55:58 -0700248MATCHER_P(StrValueEq, a,
249 std::string(negation ? "isn't" : "is") + " equal to " + ::testing::PrintToString(a)) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700250 return *(arg.value) == a;
251}
252
Adam Lesinskie3856742017-06-12 14:55:58 -0700253MATCHER_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
259MATCHER_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 Lesinski5924d8c2017-05-30 15:15:58 -0700265} // namespace test
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700266} // namespace aapt
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700267
268#endif /* AAPT_TEST_COMMON_H */