blob: 7482b8b627d30d4e0a8447b2ab50bdfb20d0be19 [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;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070050 CHECK(ConfigDescription::Parse(str, &config)) << "invalid configuration";
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 Lesinski5924d8c2017-05-30 15:15:58 -070093 const Source& GetSource() const override {
94 return source_;
95 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -070096
97 private:
98 DISALLOW_COPY_AND_ASSIGN(TestFile);
99
100 Source source_;
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800101};
102
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700103} // namespace test
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700104
105// Workaround gtest bug (https://github.com/google/googletest/issues/443)
106// that does not select base class operator<< for derived class T.
107template <typename T>
108typename std::enable_if<std::is_base_of<Value, T>::value, std::ostream&>::type operator<<(
109 std::ostream& out, const T& value) {
110 value.Print(&out);
111 return out;
112}
113
114template std::ostream& operator<<<Item>(std::ostream&, const Item&);
115template std::ostream& operator<<<Reference>(std::ostream&, const Reference&);
116template std::ostream& operator<<<Id>(std::ostream&, const Id&);
117template std::ostream& operator<<<RawString>(std::ostream&, const RawString&);
118template std::ostream& operator<<<String>(std::ostream&, const String&);
119template std::ostream& operator<<<StyledString>(std::ostream&, const StyledString&);
120template std::ostream& operator<<<FileReference>(std::ostream&, const FileReference&);
121template std::ostream& operator<<<BinaryPrimitive>(std::ostream&, const BinaryPrimitive&);
122template std::ostream& operator<<<Attribute>(std::ostream&, const Attribute&);
123template std::ostream& operator<<<Style>(std::ostream&, const Style&);
124template std::ostream& operator<<<Array>(std::ostream&, const Array&);
125template std::ostream& operator<<<Plural>(std::ostream&, const Plural&);
126
127// Add a print method to Maybe.
128template <typename T>
129void PrintTo(const Maybe<T>& value, std::ostream* out) {
130 if (value) {
131 *out << ::testing::PrintToString(value.value());
132 } else {
133 *out << "Nothing";
134 }
135}
136
137namespace test {
138
Adam Lesinskifba0cf22017-06-29 17:53:36 -0700139MATCHER_P(StrEq, a,
140 std::string(negation ? "isn't" : "is") + " equal to " +
141 ::testing::PrintToString(android::StringPiece16(a))) {
142 return android::StringPiece16(arg) == a;
143}
144
Adam Lesinski6b372992017-08-09 10:54:23 -0700145class ValueEq {
146 public:
147 template <typename arg_type>
148 class BaseImpl : public ::testing::MatcherInterface<arg_type> {
149 BaseImpl(const BaseImpl&) = default;
150
151 void DescribeTo(::std::ostream* os) const override {
152 *os << "is equal to " << *expected_;
153 }
154
155 void DescribeNegationTo(::std::ostream* os) const override {
156 *os << "is not equal to " << *expected_;
157 }
158
159 protected:
160 BaseImpl(const Value* expected) : expected_(expected) {
161 }
162
163 const Value* expected_;
164 };
165
166 template <typename T, bool>
167 class Impl {};
168
169 template <typename T>
170 class Impl<T, false> : public ::testing::MatcherInterface<T> {
171 public:
172 explicit Impl(const Value* expected) : expected_(expected) {
173 }
174
175 bool MatchAndExplain(T x, ::testing::MatchResultListener* listener) const override {
176 return expected_->Equals(&x);
177 }
178
179 void DescribeTo(::std::ostream* os) const override {
180 *os << "is equal to " << *expected_;
181 }
182
183 void DescribeNegationTo(::std::ostream* os) const override {
184 *os << "is not equal to " << *expected_;
185 }
186
187 private:
188 DISALLOW_COPY_AND_ASSIGN(Impl);
189
190 const Value* expected_;
191 };
192
193 template <typename T>
194 class Impl<T, true> : public ::testing::MatcherInterface<T> {
195 public:
196 explicit Impl(const Value* expected) : expected_(expected) {
197 }
198
199 bool MatchAndExplain(T x, ::testing::MatchResultListener* listener) const override {
200 return expected_->Equals(x);
201 }
202
203 void DescribeTo(::std::ostream* os) const override {
204 *os << "is equal to " << *expected_;
205 }
206
207 void DescribeNegationTo(::std::ostream* os) const override {
208 *os << "is not equal to " << *expected_;
209 }
210
211 private:
212 DISALLOW_COPY_AND_ASSIGN(Impl);
213
214 const Value* expected_;
215 };
216
217 ValueEq(const Value& expected) : expected_(&expected) {
218 }
219 ValueEq(const Value* expected) : expected_(expected) {
220 }
221 ValueEq(const ValueEq&) = default;
222
223 template <typename T>
224 operator ::testing::Matcher<T>() const {
225 return ::testing::Matcher<T>(new Impl<T, std::is_pointer<T>::value>(expected_));
226 }
227
228 private:
229 const Value* expected_;
230};
231
232// MATCHER_P(ValueEq, a,
233// std::string(negation ? "isn't" : "is") + " equal to " + ::testing::PrintToString(a)) {
234// return arg.Equals(&a);
235//}
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700236
Adam Lesinskie3856742017-06-12 14:55:58 -0700237MATCHER_P(StrValueEq, a,
238 std::string(negation ? "isn't" : "is") + " equal to " + ::testing::PrintToString(a)) {
Adam Lesinskia45893a2017-05-30 15:19:02 -0700239 return *(arg.value) == a;
240}
241
Adam Lesinskie3856742017-06-12 14:55:58 -0700242MATCHER_P(HasValue, name,
243 std::string(negation ? "does not have" : "has") + " value " +
244 ::testing::PrintToString(name)) {
245 return GetValueForConfig<Value>(&(*arg), name, {}) != nullptr;
246}
247
248MATCHER_P2(HasValue, name, config,
249 std::string(negation ? "does not have" : "has") + " value " +
250 ::testing::PrintToString(name) + " for config " + ::testing::PrintToString(config)) {
251 return GetValueForConfig<Value>(&(*arg), name, config) != nullptr;
252}
253
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700254} // namespace test
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700255} // namespace aapt
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700256
257#endif /* AAPT_TEST_COMMON_H */