blob: e48668cf96a3609435eb7154ba0eac6a7f94f71d [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 Meyer56f36e82022-05-20 20:35:42 +000040android::IDiagnostics* GetDiagnostics();
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -080041
Yurii Zubrytskyia5775142022-11-02 17:49:49 -070042inline ResourceName ParseNameOrDie(android::StringPiece str) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070043 ResourceNameRef ref;
Shane Farmer0a5b2012017-06-22 12:24:12 -070044 CHECK(ResourceUtils::ParseResourceName(str, &ref)) << "invalid resource name: " << str;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070045 return ref.ToResourceName();
Adam Lesinski1ab598f2015-08-14 14:26:04 -070046}
47
Yurii Zubrytskyia5775142022-11-02 17:49:49 -070048inline android::ConfigDescription ParseConfigOrDie(android::StringPiece str) {
49 android::ConfigDescription config;
Mårten Kongstad5c541f62018-06-20 08:46:41 +020050 CHECK(android::ConfigDescription::Parse(str, &config)) << "invalid configuration: " << str;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070051 return config;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070052}
53
Adam Lesinskibab4ef52017-06-01 15:22:57 -070054template <typename T = Value>
Yurii Zubrytskyia5775142022-11-02 17:49:49 -070055T* GetValueForConfigAndProduct(ResourceTable* table, android::StringPiece res_name,
Mårten Kongstad5c541f62018-06-20 08:46:41 +020056 const android::ConfigDescription& config,
Yurii Zubrytskyia5775142022-11-02 17:49:49 -070057 android::StringPiece product) {
Ryan Mitchell4382e442021-07-14 12:53:01 -070058 std::optional<ResourceTable::SearchResult> result = table->FindResource(ParseNameOrDie(res_name));
Adam Lesinskicacb28f2016-10-19 12:18:14 -070059 if (result) {
Adam Lesinski5924d8c2017-05-30 15:15:58 -070060 ResourceConfigValue* config_value = result.value().entry->FindValue(config, product);
Adam Lesinskice5e56e2016-10-21 17:56:45 -070061 if (config_value) {
62 return ValueCast<T>(config_value->value.get());
Adam Lesinski1ab598f2015-08-14 14:26:04 -070063 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -070064 }
65 return nullptr;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070066}
67
Adam Lesinskibab4ef52017-06-01 15:22:57 -070068template <>
Yurii Zubrytskyia5775142022-11-02 17:49:49 -070069Value* GetValueForConfigAndProduct<Value>(ResourceTable* table, android::StringPiece res_name,
Mårten Kongstad5c541f62018-06-20 08:46:41 +020070 const android::ConfigDescription& config,
Yurii Zubrytskyia5775142022-11-02 17:49:49 -070071 android::StringPiece product);
Adam Lesinskibab4ef52017-06-01 15:22:57 -070072
73template <typename T = Value>
Yurii Zubrytskyia5775142022-11-02 17:49:49 -070074T* GetValueForConfig(ResourceTable* table, android::StringPiece res_name,
Mårten Kongstad5c541f62018-06-20 08:46:41 +020075 const android::ConfigDescription& config) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070076 return GetValueForConfigAndProduct<T>(table, res_name, config, {});
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -080077}
78
Adam Lesinskibab4ef52017-06-01 15:22:57 -070079template <typename T = Value>
Yurii Zubrytskyia5775142022-11-02 17:49:49 -070080T* GetValue(ResourceTable* table, android::StringPiece res_name) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070081 return GetValueForConfig<T>(table, res_name, {});
Adam Lesinski1ab598f2015-08-14 14:26:04 -070082}
83
Adam Lesinskia6fe3452015-12-09 15:20:52 -080084class TestFile : public io::IFile {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070085 public:
Yurii Zubrytskyia5775142022-11-02 17:49:49 -070086 explicit TestFile(android::StringPiece path) : source_(path) {
87 }
Adam Lesinskia6fe3452015-12-09 15:20:52 -080088
Adam Lesinski5924d8c2017-05-30 15:15:58 -070089 std::unique_ptr<io::IData> OpenAsData() override {
90 return {};
91 }
Adam Lesinskia6fe3452015-12-09 15:20:52 -080092
Adam Lesinski00451162017-10-03 07:44:08 -070093 std::unique_ptr<io::InputStream> OpenInputStream() override {
94 return OpenAsData();
95 }
96
Jeremy Meyer56f36e82022-05-20 20:35:42 +000097 const android::Source& GetSource() const override {
Adam Lesinski5924d8c2017-05-30 15:15:58 -070098 return source_;
99 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700100
Mark Punzalane5671592023-09-02 00:00:30 +0000101 bool GetModificationTime(struct tm* buf) const override {
102 return false;
103 };
104
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700105 private:
106 DISALLOW_COPY_AND_ASSIGN(TestFile);
107
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000108 android::Source source_;
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800109};
110
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700111} // namespace test
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700112
113// Workaround gtest bug (https://github.com/google/googletest/issues/443)
114// that does not select base class operator<< for derived class T.
115template <typename T>
116typename std::enable_if<std::is_base_of<Value, T>::value, std::ostream&>::type operator<<(
117 std::ostream& out, const T& value) {
118 value.Print(&out);
119 return out;
120}
121
122template std::ostream& operator<<<Item>(std::ostream&, const Item&);
123template std::ostream& operator<<<Reference>(std::ostream&, const Reference&);
124template std::ostream& operator<<<Id>(std::ostream&, const Id&);
125template std::ostream& operator<<<RawString>(std::ostream&, const RawString&);
126template std::ostream& operator<<<String>(std::ostream&, const String&);
127template std::ostream& operator<<<StyledString>(std::ostream&, const StyledString&);
128template std::ostream& operator<<<FileReference>(std::ostream&, const FileReference&);
129template std::ostream& operator<<<BinaryPrimitive>(std::ostream&, const BinaryPrimitive&);
130template std::ostream& operator<<<Attribute>(std::ostream&, const Attribute&);
131template std::ostream& operator<<<Style>(std::ostream&, const Style&);
132template std::ostream& operator<<<Array>(std::ostream&, const Array&);
133template std::ostream& operator<<<Plural>(std::ostream&, const Plural&);
134
135// Add a print method to Maybe.
136template <typename T>
Ryan Mitchell4382e442021-07-14 12:53:01 -0700137void PrintTo(const std::optional<T>& value, std::ostream* out) {
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700138 if (value) {
139 *out << ::testing::PrintToString(value.value());
140 } else {
141 *out << "Nothing";
142 }
143}
144
145namespace test {
146
Adam Lesinskifba0cf22017-06-29 17:53:36 -0700147MATCHER_P(StrEq, a,
148 std::string(negation ? "isn't" : "is") + " equal to " +
149 ::testing::PrintToString(android::StringPiece16(a))) {
150 return android::StringPiece16(arg) == a;
151}
152
Adam Lesinskibbf42972018-02-14 13:36:09 -0800153template <typename T>
154class ValueEqImpl : public ::testing::MatcherInterface<T> {
Adam Lesinski6b372992017-08-09 10:54:23 -0700155 public:
Adam Lesinskibbf42972018-02-14 13:36:09 -0800156 explicit ValueEqImpl(const Value* expected) : expected_(expected) {
Adam Lesinski6b372992017-08-09 10:54:23 -0700157 }
Adam Lesinski6b372992017-08-09 10:54:23 -0700158
Adam Lesinskibbf42972018-02-14 13:36:09 -0800159 bool MatchAndExplain(T x, ::testing::MatchResultListener* listener) const override {
160 return expected_->Equals(&x);
161 }
162
163 void DescribeTo(::std::ostream* os) const override {
164 *os << "is equal to " << *expected_;
165 }
166
167 void DescribeNegationTo(::std::ostream* os) const override {
168 *os << "is not equal to " << *expected_;
Adam Lesinski6b372992017-08-09 10:54:23 -0700169 }
170
171 private:
Adam Lesinskibbf42972018-02-14 13:36:09 -0800172 DISALLOW_COPY_AND_ASSIGN(ValueEqImpl);
173
Adam Lesinski6b372992017-08-09 10:54:23 -0700174 const Value* expected_;
175};
176
Adam Lesinskibbf42972018-02-14 13:36:09 -0800177template <typename TValue>
178class ValueEqMatcher {
179 public:
Chih-Hung Hsieh1fc78e12018-12-20 13:37:44 -0800180 // NOLINTNEXTLINE(google-explicit-constructor)
Adam Lesinskibbf42972018-02-14 13:36:09 -0800181 ValueEqMatcher(TValue expected) : expected_(std::move(expected)) {
182 }
183
184 template <typename T>
Chih-Hung Hsieh1fc78e12018-12-20 13:37:44 -0800185 // NOLINTNEXTLINE(google-explicit-constructor)
Adam Lesinskibbf42972018-02-14 13:36:09 -0800186 operator ::testing::Matcher<T>() const {
187 return ::testing::Matcher<T>(new ValueEqImpl<T>(&expected_));
188 }
189
190 private:
191 TValue expected_;
192};
193
194template <typename TValue>
195class ValueEqPointerMatcher {
196 public:
Chih-Hung Hsieh1fc78e12018-12-20 13:37:44 -0800197 // NOLINTNEXTLINE(google-explicit-constructor)
Adam Lesinskibbf42972018-02-14 13:36:09 -0800198 ValueEqPointerMatcher(const TValue* expected) : expected_(expected) {
199 }
200
201 template <typename T>
Chih-Hung Hsieh1fc78e12018-12-20 13:37:44 -0800202 // NOLINTNEXTLINE(google-explicit-constructor)
Adam Lesinskibbf42972018-02-14 13:36:09 -0800203 operator ::testing::Matcher<T>() const {
204 return ::testing::Matcher<T>(new ValueEqImpl<T>(expected_));
205 }
206
207 private:
208 const TValue* expected_;
209};
210
211template <typename TValue,
212 typename = typename std::enable_if<!std::is_pointer<TValue>::value, void>::type>
213inline ValueEqMatcher<TValue> ValueEq(TValue value) {
214 return ValueEqMatcher<TValue>(std::move(value));
215}
216
217template <typename TValue>
218inline ValueEqPointerMatcher<TValue> ValueEq(const TValue* value) {
219 return ValueEqPointerMatcher<TValue>(value);
220}
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700221
Adam Lesinskie3856742017-06-12 14:55:58 -0700222MATCHER_P(StrValueEq, a,
223 std::string(negation ? "isn't" : "is") + " equal to " + ::testing::PrintToString(a)) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700224 return *(arg.value) == a;
225}
226
Adam Lesinskie3856742017-06-12 14:55:58 -0700227MATCHER_P(HasValue, name,
228 std::string(negation ? "does not have" : "has") + " value " +
229 ::testing::PrintToString(name)) {
230 return GetValueForConfig<Value>(&(*arg), name, {}) != nullptr;
231}
232
233MATCHER_P2(HasValue, name, config,
234 std::string(negation ? "does not have" : "has") + " value " +
235 ::testing::PrintToString(name) + " for config " + ::testing::PrintToString(config)) {
236 return GetValueForConfig<Value>(&(*arg), name, config) != nullptr;
237}
238
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700239} // namespace test
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700240} // namespace aapt
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700241
242#endif /* AAPT_TEST_COMMON_H */