1 /*
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 
20 #include <iostream>
21 
22 #include "android-base/logging.h"
23 #include "android-base/macros.h"
24 #include "androidfw/ConfigDescription.h"
25 #include "androidfw/StringPiece.h"
26 #include "gmock/gmock.h"
27 #include "gtest/gtest.h"
28 
29 #include "Debug.h"
30 #include "ResourceTable.h"
31 #include "ResourceUtils.h"
32 #include "ResourceValues.h"
33 #include "ValueVisitor.h"
34 #include "io/File.h"
35 #include "process/IResourceTableConsumer.h"
36 
37 namespace aapt {
38 namespace test {
39 
40 android::IDiagnostics* GetDiagnostics();
41 
ParseNameOrDie(android::StringPiece str)42 inline ResourceName ParseNameOrDie(android::StringPiece str) {
43   ResourceNameRef ref;
44   CHECK(ResourceUtils::ParseResourceName(str, &ref)) << "invalid resource name: " << str;
45   return ref.ToResourceName();
46 }
47 
ParseConfigOrDie(android::StringPiece str)48 inline android::ConfigDescription ParseConfigOrDie(android::StringPiece str) {
49   android::ConfigDescription config;
50   CHECK(android::ConfigDescription::Parse(str, &config)) << "invalid configuration: " << str;
51   return config;
52 }
53 
54 template <typename T = Value>
GetValueForConfigAndProduct(ResourceTable * table,android::StringPiece res_name,const android::ConfigDescription & config,android::StringPiece product)55 T* GetValueForConfigAndProduct(ResourceTable* table, android::StringPiece res_name,
56                                const android::ConfigDescription& config,
57                                android::StringPiece product) {
58   std::optional<ResourceTable::SearchResult> result = table->FindResource(ParseNameOrDie(res_name));
59   if (result) {
60     ResourceConfigValue* config_value = result.value().entry->FindValue(config, product);
61     if (config_value) {
62       return ValueCast<T>(config_value->value.get());
63     }
64   }
65   return nullptr;
66 }
67 
68 template <>
69 Value* GetValueForConfigAndProduct<Value>(ResourceTable* table, android::StringPiece res_name,
70                                           const android::ConfigDescription& config,
71                                           android::StringPiece product);
72 
73 template <typename T = Value>
GetValueForConfig(ResourceTable * table,android::StringPiece res_name,const android::ConfigDescription & config)74 T* GetValueForConfig(ResourceTable* table, android::StringPiece res_name,
75                      const android::ConfigDescription& config) {
76   return GetValueForConfigAndProduct<T>(table, res_name, config, {});
77 }
78 
79 template <typename T = Value>
GetValue(ResourceTable * table,android::StringPiece res_name)80 T* GetValue(ResourceTable* table, android::StringPiece res_name) {
81   return GetValueForConfig<T>(table, res_name, {});
82 }
83 
84 class TestFile : public io::IFile {
85  public:
TestFile(android::StringPiece path)86   explicit TestFile(android::StringPiece path) : source_(path) {
87   }
88 
OpenAsData()89   std::unique_ptr<io::IData> OpenAsData() override {
90     return {};
91   }
92 
OpenInputStream()93   std::unique_ptr<android::InputStream> OpenInputStream() override {
94     return OpenAsData();
95   }
96 
GetSource()97   const android::Source& GetSource() const override {
98     return source_;
99   }
100 
GetModificationTime(struct tm * buf)101   bool GetModificationTime(struct tm* buf) const override {
102     return false;
103   };
104 
105  private:
106   DISALLOW_COPY_AND_ASSIGN(TestFile);
107 
108   android::Source source_;
109 };
110 
111 }  // namespace test
112 
113 // Workaround gtest bug (https://github.com/google/googletest/issues/443)
114 // that does not select base class operator<< for derived class T.
115 template <typename T>
116 typename 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 
122 template std::ostream& operator<<<Item>(std::ostream&, const Item&);
123 template std::ostream& operator<<<Reference>(std::ostream&, const Reference&);
124 template std::ostream& operator<<<Id>(std::ostream&, const Id&);
125 template std::ostream& operator<<<RawString>(std::ostream&, const RawString&);
126 template std::ostream& operator<<<String>(std::ostream&, const String&);
127 template std::ostream& operator<<<StyledString>(std::ostream&, const StyledString&);
128 template std::ostream& operator<<<FileReference>(std::ostream&, const FileReference&);
129 template std::ostream& operator<<<BinaryPrimitive>(std::ostream&, const BinaryPrimitive&);
130 template std::ostream& operator<<<Attribute>(std::ostream&, const Attribute&);
131 template std::ostream& operator<<<Style>(std::ostream&, const Style&);
132 template std::ostream& operator<<<Array>(std::ostream&, const Array&);
133 template std::ostream& operator<<<Plural>(std::ostream&, const Plural&);
134 
135 // Add a print method to Maybe.
136 template <typename T>
PrintTo(const std::optional<T> & value,std::ostream * out)137 void PrintTo(const std::optional<T>& value, std::ostream* out) {
138   if (value) {
139     *out << ::testing::PrintToString(value.value());
140   } else {
141     *out << "Nothing";
142   }
143 }
144 
145 namespace test {
146 
147 MATCHER_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 
153 template <typename T>
154 class ValueEqImpl : public ::testing::MatcherInterface<T> {
155  public:
ValueEqImpl(const Value * expected)156   explicit ValueEqImpl(const Value* expected) : expected_(expected) {
157   }
158 
MatchAndExplain(T x,::testing::MatchResultListener * listener)159   bool MatchAndExplain(T x, ::testing::MatchResultListener* listener) const override {
160     return expected_->Equals(&x);
161   }
162 
DescribeTo(::std::ostream * os)163   void DescribeTo(::std::ostream* os) const override {
164     *os << "is equal to " << *expected_;
165   }
166 
DescribeNegationTo(::std::ostream * os)167   void DescribeNegationTo(::std::ostream* os) const override {
168     *os << "is not equal to " << *expected_;
169   }
170 
171  private:
172   DISALLOW_COPY_AND_ASSIGN(ValueEqImpl);
173 
174   const Value* expected_;
175 };
176 
177 template <typename TValue>
178 class ValueEqMatcher {
179  public:
180   // NOLINTNEXTLINE(google-explicit-constructor)
ValueEqMatcher(TValue expected)181   ValueEqMatcher(TValue expected) : expected_(std::move(expected)) {
182   }
183 
184   template <typename T>
185   // NOLINTNEXTLINE(google-explicit-constructor)
186   operator ::testing::Matcher<T>() const {
187     return ::testing::Matcher<T>(new ValueEqImpl<T>(&expected_));
188   }
189 
190  private:
191   TValue expected_;
192 };
193 
194 template <typename TValue>
195 class ValueEqPointerMatcher {
196  public:
197   // NOLINTNEXTLINE(google-explicit-constructor)
ValueEqPointerMatcher(const TValue * expected)198   ValueEqPointerMatcher(const TValue* expected) : expected_(expected) {
199   }
200 
201   template <typename T>
202   // NOLINTNEXTLINE(google-explicit-constructor)
203   operator ::testing::Matcher<T>() const {
204     return ::testing::Matcher<T>(new ValueEqImpl<T>(expected_));
205   }
206 
207  private:
208   const TValue* expected_;
209 };
210 
211 template <typename TValue,
212           typename = typename std::enable_if<!std::is_pointer<TValue>::value, void>::type>
ValueEq(TValue value)213 inline ValueEqMatcher<TValue> ValueEq(TValue value) {
214   return ValueEqMatcher<TValue>(std::move(value));
215 }
216 
217 template <typename TValue>
ValueEq(const TValue * value)218 inline ValueEqPointerMatcher<TValue> ValueEq(const TValue* value) {
219   return ValueEqPointerMatcher<TValue>(value);
220 }
221 
222 MATCHER_P(StrValueEq, a,
223           std::string(negation ? "isn't" : "is") + " equal to " + ::testing::PrintToString(a)) {
224   return *(arg.value) == a;
225 }
226 
227 MATCHER_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 
233 MATCHER_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 
239 }  // namespace test
240 }  // namespace aapt
241 
242 #endif /* AAPT_TEST_COMMON_H */
243