blob: 4e318a92f3fa0f0a26aebe38613b45ee68a15323 [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"
Adam Lesinskid5083f62017-01-16 15:07:21 -080024#include "androidfw/StringPiece.h"
Adam Lesinski5924d8c2017-05-30 15:15:58 -070025#include "gmock/gmock.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070026#include "gtest/gtest.h"
27
Adam Lesinski1ab598f2015-08-14 14:26:04 -070028#include "ConfigDescription.h"
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
Adam Lesinskid5083f62017-01-16 15:07:21 -080048inline ConfigDescription ParseConfigOrDie(const android::StringPiece& str) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070049 ConfigDescription config;
Shane Farmerefe45392017-08-21 14:39:28 -070050 CHECK(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,
Adam Lesinskicacb28f2016-10-19 12:18:14 -070056 const 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,
71 const ConfigDescription& config,
72 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,
Adam Lesinskicacb28f2016-10-19 12:18:14 -070076 const 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 Lesinski6b372992017-08-09 10:54:23 -0700149class ValueEq {
150 public:
151 template <typename arg_type>
152 class BaseImpl : public ::testing::MatcherInterface<arg_type> {
153 BaseImpl(const BaseImpl&) = default;
154
155 void DescribeTo(::std::ostream* os) const override {
156 *os << "is equal to " << *expected_;
157 }
158
159 void DescribeNegationTo(::std::ostream* os) const override {
160 *os << "is not equal to " << *expected_;
161 }
162
163 protected:
164 BaseImpl(const Value* expected) : expected_(expected) {
165 }
166
167 const Value* expected_;
168 };
169
170 template <typename T, bool>
171 class Impl {};
172
173 template <typename T>
174 class Impl<T, false> : public ::testing::MatcherInterface<T> {
175 public:
176 explicit Impl(const Value* expected) : expected_(expected) {
177 }
178
179 bool MatchAndExplain(T x, ::testing::MatchResultListener* listener) const override {
180 return expected_->Equals(&x);
181 }
182
183 void DescribeTo(::std::ostream* os) const override {
184 *os << "is equal to " << *expected_;
185 }
186
187 void DescribeNegationTo(::std::ostream* os) const override {
188 *os << "is not equal to " << *expected_;
189 }
190
191 private:
192 DISALLOW_COPY_AND_ASSIGN(Impl);
193
194 const Value* expected_;
195 };
196
197 template <typename T>
198 class Impl<T, true> : public ::testing::MatcherInterface<T> {
199 public:
200 explicit Impl(const Value* expected) : expected_(expected) {
201 }
202
203 bool MatchAndExplain(T x, ::testing::MatchResultListener* listener) const override {
204 return expected_->Equals(x);
205 }
206
207 void DescribeTo(::std::ostream* os) const override {
208 *os << "is equal to " << *expected_;
209 }
210
211 void DescribeNegationTo(::std::ostream* os) const override {
212 *os << "is not equal to " << *expected_;
213 }
214
215 private:
216 DISALLOW_COPY_AND_ASSIGN(Impl);
217
218 const Value* expected_;
219 };
220
221 ValueEq(const Value& expected) : expected_(&expected) {
222 }
223 ValueEq(const Value* expected) : expected_(expected) {
224 }
225 ValueEq(const ValueEq&) = default;
226
227 template <typename T>
228 operator ::testing::Matcher<T>() const {
229 return ::testing::Matcher<T>(new Impl<T, std::is_pointer<T>::value>(expected_));
230 }
231
232 private:
233 const Value* expected_;
234};
235
236// MATCHER_P(ValueEq, a,
237// std::string(negation ? "isn't" : "is") + " equal to " + ::testing::PrintToString(a)) {
238// return arg.Equals(&a);
239//}
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700240
Adam Lesinskie3856742017-06-12 14:55:58 -0700241MATCHER_P(StrValueEq, a,
242 std::string(negation ? "isn't" : "is") + " equal to " + ::testing::PrintToString(a)) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700243 return *(arg.value) == a;
244}
245
Adam Lesinskie3856742017-06-12 14:55:58 -0700246MATCHER_P(HasValue, name,
247 std::string(negation ? "does not have" : "has") + " value " +
248 ::testing::PrintToString(name)) {
249 return GetValueForConfig<Value>(&(*arg), name, {}) != nullptr;
250}
251
252MATCHER_P2(HasValue, name, config,
253 std::string(negation ? "does not have" : "has") + " value " +
254 ::testing::PrintToString(name) + " for config " + ::testing::PrintToString(config)) {
255 return GetValueForConfig<Value>(&(*arg), name, config) != nullptr;
256}
257
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700258} // namespace test
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700259} // namespace aapt
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700260
261#endif /* AAPT_TEST_COMMON_H */