Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 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 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 20 | #include <iostream> |
| 21 | |
| 22 | #include "android-base/logging.h" |
| 23 | #include "android-base/macros.h" |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 24 | #include "androidfw/StringPiece.h" |
Adam Lesinski | 5924d8c | 2017-05-30 15:15:58 -0700 | [diff] [blame] | 25 | #include "gmock/gmock.h" |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 26 | #include "gtest/gtest.h" |
| 27 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 28 | #include "ConfigDescription.h" |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 29 | #include "Debug.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 30 | #include "ResourceTable.h" |
| 31 | #include "ResourceUtils.h" |
Adam Lesinski | 5924d8c | 2017-05-30 15:15:58 -0700 | [diff] [blame] | 32 | #include "ResourceValues.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 33 | #include "ValueVisitor.h" |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 34 | #include "io/File.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 35 | #include "process/IResourceTableConsumer.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 36 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 37 | namespace aapt { |
| 38 | namespace test { |
| 39 | |
Adam Lesinski | bab4ef5 | 2017-06-01 15:22:57 -0700 | [diff] [blame] | 40 | IDiagnostics* GetDiagnostics(); |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 41 | |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 42 | inline ResourceName ParseNameOrDie(const android::StringPiece& str) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 43 | ResourceNameRef ref; |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 44 | CHECK(ResourceUtils::ParseResourceName(str, &ref)) << "invalid resource name: " << str; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 45 | return ref.ToResourceName(); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 46 | } |
| 47 | |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 48 | inline ConfigDescription ParseConfigOrDie(const android::StringPiece& str) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 49 | ConfigDescription config; |
Shane Farmer | efe4539 | 2017-08-21 14:39:28 -0700 | [diff] [blame] | 50 | CHECK(ConfigDescription::Parse(str, &config)) << "invalid configuration: " << str; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 51 | return config; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 52 | } |
| 53 | |
Adam Lesinski | bab4ef5 | 2017-06-01 15:22:57 -0700 | [diff] [blame] | 54 | template <typename T = Value> |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 55 | T* GetValueForConfigAndProduct(ResourceTable* table, const android::StringPiece& res_name, |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 56 | const ConfigDescription& config, |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 57 | const android::StringPiece& product) { |
Adam Lesinski | 5924d8c | 2017-05-30 15:15:58 -0700 | [diff] [blame] | 58 | Maybe<ResourceTable::SearchResult> result = table->FindResource(ParseNameOrDie(res_name)); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 59 | if (result) { |
Adam Lesinski | 5924d8c | 2017-05-30 15:15:58 -0700 | [diff] [blame] | 60 | ResourceConfigValue* config_value = result.value().entry->FindValue(config, product); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 61 | if (config_value) { |
| 62 | return ValueCast<T>(config_value->value.get()); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 63 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 64 | } |
| 65 | return nullptr; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 66 | } |
| 67 | |
Adam Lesinski | bab4ef5 | 2017-06-01 15:22:57 -0700 | [diff] [blame] | 68 | template <> |
| 69 | Value* GetValueForConfigAndProduct<Value>(ResourceTable* table, |
| 70 | const android::StringPiece& res_name, |
| 71 | const ConfigDescription& config, |
| 72 | const android::StringPiece& product); |
| 73 | |
| 74 | template <typename T = Value> |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 75 | T* GetValueForConfig(ResourceTable* table, const android::StringPiece& res_name, |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 76 | const ConfigDescription& config) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 77 | return GetValueForConfigAndProduct<T>(table, res_name, config, {}); |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 78 | } |
| 79 | |
Adam Lesinski | bab4ef5 | 2017-06-01 15:22:57 -0700 | [diff] [blame] | 80 | template <typename T = Value> |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 81 | T* GetValue(ResourceTable* table, const android::StringPiece& res_name) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 82 | return GetValueForConfig<T>(table, res_name, {}); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 83 | } |
| 84 | |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 85 | class TestFile : public io::IFile { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 86 | public: |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 87 | explicit TestFile(const android::StringPiece& path) : source_(path) {} |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 88 | |
Adam Lesinski | 5924d8c | 2017-05-30 15:15:58 -0700 | [diff] [blame] | 89 | std::unique_ptr<io::IData> OpenAsData() override { |
| 90 | return {}; |
| 91 | } |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 92 | |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 93 | std::unique_ptr<io::InputStream> OpenInputStream() override { |
| 94 | return OpenAsData(); |
| 95 | } |
| 96 | |
Adam Lesinski | 5924d8c | 2017-05-30 15:15:58 -0700 | [diff] [blame] | 97 | const Source& GetSource() const override { |
| 98 | return source_; |
| 99 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 100 | |
| 101 | private: |
| 102 | DISALLOW_COPY_AND_ASSIGN(TestFile); |
| 103 | |
| 104 | Source source_; |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 105 | }; |
| 106 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 107 | } // namespace test |
Adam Lesinski | 5924d8c | 2017-05-30 15:15:58 -0700 | [diff] [blame] | 108 | |
| 109 | // Workaround gtest bug (https://github.com/google/googletest/issues/443) |
| 110 | // that does not select base class operator<< for derived class T. |
| 111 | template <typename T> |
| 112 | typename 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 | |
| 118 | template std::ostream& operator<<<Item>(std::ostream&, const Item&); |
| 119 | template std::ostream& operator<<<Reference>(std::ostream&, const Reference&); |
| 120 | template std::ostream& operator<<<Id>(std::ostream&, const Id&); |
| 121 | template std::ostream& operator<<<RawString>(std::ostream&, const RawString&); |
| 122 | template std::ostream& operator<<<String>(std::ostream&, const String&); |
| 123 | template std::ostream& operator<<<StyledString>(std::ostream&, const StyledString&); |
| 124 | template std::ostream& operator<<<FileReference>(std::ostream&, const FileReference&); |
| 125 | template std::ostream& operator<<<BinaryPrimitive>(std::ostream&, const BinaryPrimitive&); |
| 126 | template std::ostream& operator<<<Attribute>(std::ostream&, const Attribute&); |
| 127 | template std::ostream& operator<<<Style>(std::ostream&, const Style&); |
| 128 | template std::ostream& operator<<<Array>(std::ostream&, const Array&); |
| 129 | template std::ostream& operator<<<Plural>(std::ostream&, const Plural&); |
| 130 | |
| 131 | // Add a print method to Maybe. |
| 132 | template <typename T> |
| 133 | void 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 | |
| 141 | namespace test { |
| 142 | |
Adam Lesinski | fba0cf2 | 2017-06-29 17:53:36 -0700 | [diff] [blame] | 143 | MATCHER_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 Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 149 | class 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 Lesinski | 5924d8c | 2017-05-30 15:15:58 -0700 | [diff] [blame] | 240 | |
Adam Lesinski | e385674 | 2017-06-12 14:55:58 -0700 | [diff] [blame] | 241 | MATCHER_P(StrValueEq, a, |
| 242 | std::string(negation ? "isn't" : "is") + " equal to " + ::testing::PrintToString(a)) { |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 243 | return *(arg.value) == a; |
| 244 | } |
| 245 | |
Adam Lesinski | e385674 | 2017-06-12 14:55:58 -0700 | [diff] [blame] | 246 | MATCHER_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 | |
| 252 | MATCHER_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 Lesinski | 5924d8c | 2017-05-30 15:15:58 -0700 | [diff] [blame] | 258 | } // namespace test |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 259 | } // namespace aapt |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 260 | |
| 261 | #endif /* AAPT_TEST_COMMON_H */ |