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/StringPiece.h"
25 #include "gmock/gmock.h"
26 #include "gtest/gtest.h"
27
28 #include "ConfigDescription.h"
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 //
38 // GTEST 1.7 doesn't explicitly cast to bool, which causes explicit operators to
39 // fail to compile.
40 //
41 #define AAPT_ASSERT_TRUE(v) ASSERT_TRUE(bool(v))
42 #define AAPT_ASSERT_FALSE(v) ASSERT_FALSE(bool(v))
43 #define AAPT_EXPECT_TRUE(v) EXPECT_TRUE(bool(v))
44 #define AAPT_EXPECT_FALSE(v) EXPECT_FALSE(bool(v))
45
46 namespace aapt {
47 namespace test {
48
49 IDiagnostics* GetDiagnostics();
50
ParseNameOrDie(const android::StringPiece & str)51 inline ResourceName ParseNameOrDie(const android::StringPiece& str) {
52 ResourceNameRef ref;
53 CHECK(ResourceUtils::ParseResourceName(str, &ref)) << "invalid resource name";
54 return ref.ToResourceName();
55 }
56
ParseConfigOrDie(const android::StringPiece & str)57 inline ConfigDescription ParseConfigOrDie(const android::StringPiece& str) {
58 ConfigDescription config;
59 CHECK(ConfigDescription::Parse(str, &config)) << "invalid configuration";
60 return config;
61 }
62
63 template <typename T = Value>
GetValueForConfigAndProduct(ResourceTable * table,const android::StringPiece & res_name,const ConfigDescription & config,const android::StringPiece & product)64 T* GetValueForConfigAndProduct(ResourceTable* table, const android::StringPiece& res_name,
65 const ConfigDescription& config,
66 const android::StringPiece& product) {
67 Maybe<ResourceTable::SearchResult> result = table->FindResource(ParseNameOrDie(res_name));
68 if (result) {
69 ResourceConfigValue* config_value = result.value().entry->FindValue(config, product);
70 if (config_value) {
71 return ValueCast<T>(config_value->value.get());
72 }
73 }
74 return nullptr;
75 }
76
77 template <>
78 Value* GetValueForConfigAndProduct<Value>(ResourceTable* table,
79 const android::StringPiece& res_name,
80 const ConfigDescription& config,
81 const android::StringPiece& product);
82
83 template <typename T = Value>
GetValueForConfig(ResourceTable * table,const android::StringPiece & res_name,const ConfigDescription & config)84 T* GetValueForConfig(ResourceTable* table, const android::StringPiece& res_name,
85 const ConfigDescription& config) {
86 return GetValueForConfigAndProduct<T>(table, res_name, config, {});
87 }
88
89 template <typename T = Value>
GetValue(ResourceTable * table,const android::StringPiece & res_name)90 T* GetValue(ResourceTable* table, const android::StringPiece& res_name) {
91 return GetValueForConfig<T>(table, res_name, {});
92 }
93
94 class TestFile : public io::IFile {
95 public:
TestFile(const android::StringPiece & path)96 explicit TestFile(const android::StringPiece& path) : source_(path) {}
97
OpenAsData()98 std::unique_ptr<io::IData> OpenAsData() override {
99 return {};
100 }
101
GetSource()102 const Source& GetSource() const override {
103 return source_;
104 }
105
106 private:
107 DISALLOW_COPY_AND_ASSIGN(TestFile);
108
109 Source source_;
110 };
111
112 } // namespace test
113
114 // Workaround gtest bug (https://github.com/google/googletest/issues/443)
115 // that does not select base class operator<< for derived class T.
116 template <typename T>
117 typename std::enable_if<std::is_base_of<Value, T>::value, std::ostream&>::type operator<<(
118 std::ostream& out, const T& value) {
119 value.Print(&out);
120 return out;
121 }
122
123 template std::ostream& operator<<<Item>(std::ostream&, const Item&);
124 template std::ostream& operator<<<Reference>(std::ostream&, const Reference&);
125 template std::ostream& operator<<<Id>(std::ostream&, const Id&);
126 template std::ostream& operator<<<RawString>(std::ostream&, const RawString&);
127 template std::ostream& operator<<<String>(std::ostream&, const String&);
128 template std::ostream& operator<<<StyledString>(std::ostream&, const StyledString&);
129 template std::ostream& operator<<<FileReference>(std::ostream&, const FileReference&);
130 template std::ostream& operator<<<BinaryPrimitive>(std::ostream&, const BinaryPrimitive&);
131 template std::ostream& operator<<<Attribute>(std::ostream&, const Attribute&);
132 template std::ostream& operator<<<Style>(std::ostream&, const Style&);
133 template std::ostream& operator<<<Array>(std::ostream&, const Array&);
134 template std::ostream& operator<<<Plural>(std::ostream&, const Plural&);
135
136 // Add a print method to Maybe.
137 template <typename T>
PrintTo(const Maybe<T> & value,std::ostream * out)138 void PrintTo(const Maybe<T>& value, std::ostream* out) {
139 if (value) {
140 *out << ::testing::PrintToString(value.value());
141 } else {
142 *out << "Nothing";
143 }
144 }
145
146 namespace test {
147
148 MATCHER_P(ValueEq, a,
149 std::string(negation ? "isn't" : "is") + " equal to " + ::testing::PrintToString(a)) {
150 return arg.Equals(&a);
151 }
152
153 } // namespace test
154 } // namespace aapt
155
156 #endif /* AAPT_TEST_COMMON_H */
157