blob: 50b41f1cb1e8c856c0c47dfdf226601b8cb6292f [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 Kongstad24c9aa62018-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
Adam Lesinskibab4ef52017-06-01 15:22:57 -070040IDiagnostics* GetDiagnostics();
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -080041
Adam Lesinskid5083f62017-01-16 15:07:21 -080042inline ResourceName ParseNameOrDie(const 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
Mårten Kongstad24c9aa62018-06-20 08:46:41 +020048inline android::ConfigDescription ParseConfigOrDie(const android::StringPiece& str) {
49 android::ConfigDescription config;
50 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>
Adam Lesinskid5083f62017-01-16 15:07:21 -080055T* GetValueForConfigAndProduct(ResourceTable* table, const android::StringPiece& res_name,
Mårten Kongstad24c9aa62018-06-20 08:46:41 +020056 const android::ConfigDescription& config,
Adam Lesinskid5083f62017-01-16 15:07:21 -080057 const android::StringPiece& product) {
Adam Lesinski5924d8c2017-05-30 15:15:58 -070058 Maybe<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 <>
69Value* GetValueForConfigAndProduct<Value>(ResourceTable* table,
70 const android::StringPiece& res_name,
Mårten Kongstad24c9aa62018-06-20 08:46:41 +020071 const android::ConfigDescription& config,
Adam Lesinskibab4ef52017-06-01 15:22:57 -070072 const android::StringPiece& product);
73
74template <typename T = Value>
Adam Lesinskid5083f62017-01-16 15:07:21 -080075T* GetValueForConfig(ResourceTable* table, const android::StringPiece& res_name,
Mårten Kongstad24c9aa62018-06-20 08:46:41 +020076 const android::ConfigDescription& config) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070077 return GetValueForConfigAndProduct<T>(table, res_name, config, {});
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -080078}
79
Adam Lesinskibab4ef52017-06-01 15:22:57 -070080template <typename T = Value>
Adam Lesinskid5083f62017-01-16 15:07:21 -080081T* GetValue(ResourceTable* table, const android::StringPiece& res_name) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070082 return GetValueForConfig<T>(table, res_name, {});
Adam Lesinski1ab598f2015-08-14 14:26:04 -070083}
84
Adam Lesinskia6fe3452015-12-09 15:20:52 -080085class TestFile : public io::IFile {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070086 public:
Adam Lesinskid5083f62017-01-16 15:07:21 -080087 explicit TestFile(const android::StringPiece& path) : source_(path) {}
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
Adam Lesinski5924d8c2017-05-30 15:15:58 -070097 const Source& GetSource() const override {
98 return source_;
99 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700100
101 private:
102 DISALLOW_COPY_AND_ASSIGN(TestFile);
103
104 Source source_;
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800105};
106
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700107} // namespace test
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700108
109// Workaround gtest bug (https://github.com/google/googletest/issues/443)
110// that does not select base class operator<< for derived class T.
111template <typename T>
112typename std::enable_if<std::is_base_of<Value, T>::value, std::ostream&>::type operator<<(
113 std::ostream& out, const T& value) {
114 value.Print(&out);
115 return out;
116}
117
118template std::ostream& operator<<<Item>(std::ostream&, const Item&);
119template std::ostream& operator<<<Reference>(std::ostream&, const Reference&);
120template std::ostream& operator<<<Id>(std::ostream&, const Id&);
121template std::ostream& operator<<<RawString>(std::ostream&, const RawString&);
122template std::ostream& operator<<<String>(std::ostream&, const String&);
123template std::ostream& operator<<<StyledString>(std::ostream&, const StyledString&);
124template std::ostream& operator<<<FileReference>(std::ostream&, const FileReference&);
125template std::ostream& operator<<<BinaryPrimitive>(std::ostream&, const BinaryPrimitive&);
126template std::ostream& operator<<<Attribute>(std::ostream&, const Attribute&);
127template std::ostream& operator<<<Style>(std::ostream&, const Style&);
128template std::ostream& operator<<<Array>(std::ostream&, const Array&);
129template std::ostream& operator<<<Plural>(std::ostream&, const Plural&);
130
131// Add a print method to Maybe.
132template <typename T>
133void PrintTo(const Maybe<T>& value, std::ostream* out) {
134 if (value) {
135 *out << ::testing::PrintToString(value.value());
136 } else {
137 *out << "Nothing";
138 }
139}
140
141namespace test {
142
Adam Lesinskifba0cf22017-06-29 17:53:36 -0700143MATCHER_P(StrEq, a,
144 std::string(negation ? "isn't" : "is") + " equal to " +
145 ::testing::PrintToString(android::StringPiece16(a))) {
146 return android::StringPiece16(arg) == a;
147}
148
Adam Lesinskibbf42972018-02-14 13:36:09 -0800149template <typename T>
150class ValueEqImpl : public ::testing::MatcherInterface<T> {
Adam Lesinski6b372992017-08-09 10:54:23 -0700151 public:
Adam Lesinskibbf42972018-02-14 13:36:09 -0800152 explicit ValueEqImpl(const Value* expected) : expected_(expected) {
Adam Lesinski6b372992017-08-09 10:54:23 -0700153 }
Adam Lesinski6b372992017-08-09 10:54:23 -0700154
Adam Lesinskibbf42972018-02-14 13:36:09 -0800155 bool MatchAndExplain(T x, ::testing::MatchResultListener* listener) const override {
156 return expected_->Equals(&x);
157 }
158
159 void DescribeTo(::std::ostream* os) const override {
160 *os << "is equal to " << *expected_;
161 }
162
163 void DescribeNegationTo(::std::ostream* os) const override {
164 *os << "is not equal to " << *expected_;
Adam Lesinski6b372992017-08-09 10:54:23 -0700165 }
166
167 private:
Adam Lesinskibbf42972018-02-14 13:36:09 -0800168 DISALLOW_COPY_AND_ASSIGN(ValueEqImpl);
169
Adam Lesinski6b372992017-08-09 10:54:23 -0700170 const Value* expected_;
171};
172
Adam Lesinskibbf42972018-02-14 13:36:09 -0800173template <typename TValue>
174class ValueEqMatcher {
175 public:
176 ValueEqMatcher(TValue expected) : expected_(std::move(expected)) {
177 }
178
179 template <typename T>
180 operator ::testing::Matcher<T>() const {
181 return ::testing::Matcher<T>(new ValueEqImpl<T>(&expected_));
182 }
183
184 private:
185 TValue expected_;
186};
187
188template <typename TValue>
189class ValueEqPointerMatcher {
190 public:
191 ValueEqPointerMatcher(const TValue* expected) : expected_(expected) {
192 }
193
194 template <typename T>
195 operator ::testing::Matcher<T>() const {
196 return ::testing::Matcher<T>(new ValueEqImpl<T>(expected_));
197 }
198
199 private:
200 const TValue* expected_;
201};
202
203template <typename TValue,
204 typename = typename std::enable_if<!std::is_pointer<TValue>::value, void>::type>
205inline ValueEqMatcher<TValue> ValueEq(TValue value) {
206 return ValueEqMatcher<TValue>(std::move(value));
207}
208
209template <typename TValue>
210inline ValueEqPointerMatcher<TValue> ValueEq(const TValue* value) {
211 return ValueEqPointerMatcher<TValue>(value);
212}
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700213
Adam Lesinskie3856742017-06-12 14:55:58 -0700214MATCHER_P(StrValueEq, a,
215 std::string(negation ? "isn't" : "is") + " equal to " + ::testing::PrintToString(a)) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700216 return *(arg.value) == a;
217}
218
Adam Lesinskie3856742017-06-12 14:55:58 -0700219MATCHER_P(HasValue, name,
220 std::string(negation ? "does not have" : "has") + " value " +
221 ::testing::PrintToString(name)) {
222 return GetValueForConfig<Value>(&(*arg), name, {}) != nullptr;
223}
224
225MATCHER_P2(HasValue, name, config,
226 std::string(negation ? "does not have" : "has") + " value " +
227 ::testing::PrintToString(name) + " for config " + ::testing::PrintToString(config)) {
228 return GetValueForConfig<Value>(&(*arg), name, config) != nullptr;
229}
230
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700231} // namespace test
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700232} // namespace aapt
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700233
234#endif /* AAPT_TEST_COMMON_H */