xref: /aosp_15_r20/frameworks/base/tools/aapt2/test/Common.h (revision d57664e9bc4670b3ecf6748a746a57c557b6bc9e)
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 struct TestDiagnosticsImpl : public android::IDiagnostics {
LogTestDiagnosticsImpl41   void Log(Level level, android::DiagMessageActual& actual_msg) override {
42     switch (level) {
43       case Level::Note:
44         return;
45 
46       case Level::Warn:
47         std::cerr << actual_msg.source << ": warn: " << actual_msg.message << "." << std::endl;
48         log << actual_msg.source << ": warn: " << actual_msg.message << "." << std::endl;
49         break;
50 
51       case Level::Error:
52         std::cerr << actual_msg.source << ": error: " << actual_msg.message << "." << std::endl;
53         log << actual_msg.source << ": error: " << actual_msg.message << "." << std::endl;
54         break;
55     }
56   }
57 
GetLogTestDiagnosticsImpl58   std::string GetLog() {
59     return log.str();
60   }
61 
62  private:
63   std::ostringstream log;
64 };
65 
66 android::IDiagnostics* GetDiagnostics();
67 
ParseNameOrDie(android::StringPiece str)68 inline ResourceName ParseNameOrDie(android::StringPiece str) {
69   ResourceNameRef ref;
70   CHECK(ResourceUtils::ParseResourceName(str, &ref)) << "invalid resource name: " << str;
71   return ref.ToResourceName();
72 }
73 
ParseConfigOrDie(android::StringPiece str)74 inline android::ConfigDescription ParseConfigOrDie(android::StringPiece str) {
75   android::ConfigDescription config;
76   CHECK(android::ConfigDescription::Parse(str, &config)) << "invalid configuration: " << str;
77   return config;
78 }
79 
80 template <typename T = Value>
GetValueForConfigAndProduct(ResourceTable * table,android::StringPiece res_name,const android::ConfigDescription & config,android::StringPiece product)81 T* GetValueForConfigAndProduct(ResourceTable* table, android::StringPiece res_name,
82                                const android::ConfigDescription& config,
83                                android::StringPiece product) {
84   std::optional<ResourceTable::SearchResult> result = table->FindResource(ParseNameOrDie(res_name));
85   if (result) {
86     ResourceConfigValue* config_value = result.value().entry->FindValue(config, product);
87     if (config_value) {
88       return ValueCast<T>(config_value->value.get());
89     }
90   }
91   return nullptr;
92 }
93 
94 template <>
95 Value* GetValueForConfigAndProduct<Value>(ResourceTable* table, android::StringPiece res_name,
96                                           const android::ConfigDescription& config,
97                                           android::StringPiece product);
98 
99 template <typename T = Value>
GetValueForConfig(ResourceTable * table,android::StringPiece res_name,const android::ConfigDescription & config)100 T* GetValueForConfig(ResourceTable* table, android::StringPiece res_name,
101                      const android::ConfigDescription& config) {
102   return GetValueForConfigAndProduct<T>(table, res_name, config, {});
103 }
104 
105 template <typename T = Value>
GetValue(ResourceTable * table,android::StringPiece res_name)106 T* GetValue(ResourceTable* table, android::StringPiece res_name) {
107   return GetValueForConfig<T>(table, res_name, {});
108 }
109 
110 class TestFile : public io::IFile {
111  public:
TestFile(android::StringPiece path)112   explicit TestFile(android::StringPiece path) : source_(path) {
113   }
114 
OpenAsData()115   std::unique_ptr<io::IData> OpenAsData() override {
116     return {};
117   }
118 
OpenInputStream()119   std::unique_ptr<android::InputStream> OpenInputStream() override {
120     return OpenAsData();
121   }
122 
GetSource()123   const android::Source& GetSource() const override {
124     return source_;
125   }
126 
GetModificationTime(struct tm * buf)127   bool GetModificationTime(struct tm* buf) const override {
128     return false;
129   };
130 
131  private:
132   DISALLOW_COPY_AND_ASSIGN(TestFile);
133 
134   android::Source source_;
135 };
136 
137 }  // namespace test
138 
139 // Workaround gtest bug (https://github.com/google/googletest/issues/443)
140 // that does not select base class operator<< for derived class T.
141 template <typename T>
142 typename std::enable_if<std::is_base_of<Value, T>::value, std::ostream&>::type operator<<(
143     std::ostream& out, const T& value) {
144   value.Print(&out);
145   return out;
146 }
147 
148 template std::ostream& operator<<<Item>(std::ostream&, const Item&);
149 template std::ostream& operator<<<Reference>(std::ostream&, const Reference&);
150 template std::ostream& operator<<<Id>(std::ostream&, const Id&);
151 template std::ostream& operator<<<RawString>(std::ostream&, const RawString&);
152 template std::ostream& operator<<<String>(std::ostream&, const String&);
153 template std::ostream& operator<<<StyledString>(std::ostream&, const StyledString&);
154 template std::ostream& operator<<<FileReference>(std::ostream&, const FileReference&);
155 template std::ostream& operator<<<BinaryPrimitive>(std::ostream&, const BinaryPrimitive&);
156 template std::ostream& operator<<<Attribute>(std::ostream&, const Attribute&);
157 template std::ostream& operator<<<Style>(std::ostream&, const Style&);
158 template std::ostream& operator<<<Array>(std::ostream&, const Array&);
159 template std::ostream& operator<<<Plural>(std::ostream&, const Plural&);
160 
161 // Add a print method to Maybe.
162 template <typename T>
PrintTo(const std::optional<T> & value,std::ostream * out)163 void PrintTo(const std::optional<T>& value, std::ostream* out) {
164   if (value) {
165     *out << ::testing::PrintToString(value.value());
166   } else {
167     *out << "Nothing";
168   }
169 }
170 
171 namespace test {
172 
173 MATCHER_P(StrEq, a,
174           std::string(negation ? "isn't" : "is") + " equal to " +
175               ::testing::PrintToString(android::StringPiece16(a))) {
176   return android::StringPiece16(arg) == a;
177 }
178 
179 template <typename T>
180 class ValueEqImpl : public ::testing::MatcherInterface<T> {
181  public:
ValueEqImpl(const Value * expected)182   explicit ValueEqImpl(const Value* expected) : expected_(expected) {
183   }
184 
MatchAndExplain(T x,::testing::MatchResultListener * listener)185   bool MatchAndExplain(T x, ::testing::MatchResultListener* listener) const override {
186     return expected_->Equals(&x);
187   }
188 
DescribeTo(::std::ostream * os)189   void DescribeTo(::std::ostream* os) const override {
190     *os << "is equal to " << *expected_;
191   }
192 
DescribeNegationTo(::std::ostream * os)193   void DescribeNegationTo(::std::ostream* os) const override {
194     *os << "is not equal to " << *expected_;
195   }
196 
197  private:
198   DISALLOW_COPY_AND_ASSIGN(ValueEqImpl);
199 
200   const Value* expected_;
201 };
202 
203 template <typename TValue>
204 class ValueEqMatcher {
205  public:
206   // NOLINTNEXTLINE(google-explicit-constructor)
ValueEqMatcher(TValue expected)207   ValueEqMatcher(TValue expected) : expected_(std::move(expected)) {
208   }
209 
210   template <typename T>
211   // NOLINTNEXTLINE(google-explicit-constructor)
212   operator ::testing::Matcher<T>() const {
213     return ::testing::Matcher<T>(new ValueEqImpl<T>(&expected_));
214   }
215 
216  private:
217   TValue expected_;
218 };
219 
220 template <typename TValue>
221 class ValueEqPointerMatcher {
222  public:
223   // NOLINTNEXTLINE(google-explicit-constructor)
ValueEqPointerMatcher(const TValue * expected)224   ValueEqPointerMatcher(const TValue* expected) : expected_(expected) {
225   }
226 
227   template <typename T>
228   // NOLINTNEXTLINE(google-explicit-constructor)
229   operator ::testing::Matcher<T>() const {
230     return ::testing::Matcher<T>(new ValueEqImpl<T>(expected_));
231   }
232 
233  private:
234   const TValue* expected_;
235 };
236 
237 template <typename TValue,
238           typename = typename std::enable_if<!std::is_pointer<TValue>::value, void>::type>
ValueEq(TValue value)239 inline ValueEqMatcher<TValue> ValueEq(TValue value) {
240   return ValueEqMatcher<TValue>(std::move(value));
241 }
242 
243 template <typename TValue>
ValueEq(const TValue * value)244 inline ValueEqPointerMatcher<TValue> ValueEq(const TValue* value) {
245   return ValueEqPointerMatcher<TValue>(value);
246 }
247 
248 MATCHER_P(StrValueEq, a,
249           std::string(negation ? "isn't" : "is") + " equal to " + ::testing::PrintToString(a)) {
250   return *(arg.value) == a;
251 }
252 
253 MATCHER_P(HasValue, name,
254           std::string(negation ? "does not have" : "has") + " value " +
255               ::testing::PrintToString(name)) {
256   return GetValueForConfig<Value>(&(*arg), name, {}) != nullptr;
257 }
258 
259 MATCHER_P2(HasValue, name, config,
260            std::string(negation ? "does not have" : "has") + " value " +
261                ::testing::PrintToString(name) + " for config " + ::testing::PrintToString(config)) {
262   return GetValueForConfig<Value>(&(*arg), name, config) != nullptr;
263 }
264 
265 }  // namespace test
266 }  // namespace aapt
267 
268 #endif /* AAPT_TEST_COMMON_H */
269