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 | // |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 38 | // GTEST 1.7 doesn't explicitly cast to bool, which causes explicit operators to |
| 39 | // fail to compile. |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 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 | struct DummyDiagnosticsImpl : public IDiagnostics { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 50 | void Log(Level level, DiagMessageActual& actual_msg) override { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 51 | switch (level) { |
| 52 | case Level::Note: |
| 53 | return; |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 54 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 55 | case Level::Warn: |
Adam Lesinski | 5924d8c | 2017-05-30 15:15:58 -0700 | [diff] [blame^] | 56 | std::cerr << actual_msg.source << ": warn: " << actual_msg.message << "." << std::endl; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 57 | break; |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 58 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 59 | case Level::Error: |
Adam Lesinski | 5924d8c | 2017-05-30 15:15:58 -0700 | [diff] [blame^] | 60 | std::cerr << actual_msg.source << ": error: " << actual_msg.message << "." << std::endl; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 61 | break; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 62 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 63 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 64 | }; |
| 65 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 66 | inline IDiagnostics* GetDiagnostics() { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 67 | static DummyDiagnosticsImpl diag; |
| 68 | return &diag; |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 69 | } |
| 70 | |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 71 | inline ResourceName ParseNameOrDie(const android::StringPiece& str) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 72 | ResourceNameRef ref; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 73 | CHECK(ResourceUtils::ParseResourceName(str, &ref)) << "invalid resource name"; |
| 74 | return ref.ToResourceName(); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 75 | } |
| 76 | |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 77 | inline ConfigDescription ParseConfigOrDie(const android::StringPiece& str) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 78 | ConfigDescription config; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 79 | CHECK(ConfigDescription::Parse(str, &config)) << "invalid configuration"; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 80 | return config; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 81 | } |
| 82 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 83 | template <typename T> |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 84 | T* GetValueForConfigAndProduct(ResourceTable* table, const android::StringPiece& res_name, |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 85 | const ConfigDescription& config, |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 86 | const android::StringPiece& product) { |
Adam Lesinski | 5924d8c | 2017-05-30 15:15:58 -0700 | [diff] [blame^] | 87 | Maybe<ResourceTable::SearchResult> result = table->FindResource(ParseNameOrDie(res_name)); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 88 | if (result) { |
Adam Lesinski | 5924d8c | 2017-05-30 15:15:58 -0700 | [diff] [blame^] | 89 | ResourceConfigValue* config_value = result.value().entry->FindValue(config, product); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 90 | if (config_value) { |
| 91 | return ValueCast<T>(config_value->value.get()); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 92 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 93 | } |
| 94 | return nullptr; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 95 | } |
| 96 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 97 | template <typename T> |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 98 | T* GetValueForConfig(ResourceTable* table, const android::StringPiece& res_name, |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 99 | const ConfigDescription& config) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 100 | return GetValueForConfigAndProduct<T>(table, res_name, config, {}); |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 101 | } |
| 102 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 103 | template <typename T> |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 104 | T* GetValue(ResourceTable* table, const android::StringPiece& res_name) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 105 | return GetValueForConfig<T>(table, res_name, {}); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 106 | } |
| 107 | |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 108 | class TestFile : public io::IFile { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 109 | public: |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 110 | explicit TestFile(const android::StringPiece& path) : source_(path) {} |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 111 | |
Adam Lesinski | 5924d8c | 2017-05-30 15:15:58 -0700 | [diff] [blame^] | 112 | std::unique_ptr<io::IData> OpenAsData() override { |
| 113 | return {}; |
| 114 | } |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 115 | |
Adam Lesinski | 5924d8c | 2017-05-30 15:15:58 -0700 | [diff] [blame^] | 116 | const Source& GetSource() const override { |
| 117 | return source_; |
| 118 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 119 | |
| 120 | private: |
| 121 | DISALLOW_COPY_AND_ASSIGN(TestFile); |
| 122 | |
| 123 | Source source_; |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 124 | }; |
| 125 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 126 | } // namespace test |
Adam Lesinski | 5924d8c | 2017-05-30 15:15:58 -0700 | [diff] [blame^] | 127 | |
| 128 | // Workaround gtest bug (https://github.com/google/googletest/issues/443) |
| 129 | // that does not select base class operator<< for derived class T. |
| 130 | template <typename T> |
| 131 | typename std::enable_if<std::is_base_of<Value, T>::value, std::ostream&>::type operator<<( |
| 132 | std::ostream& out, const T& value) { |
| 133 | value.Print(&out); |
| 134 | return out; |
| 135 | } |
| 136 | |
| 137 | template std::ostream& operator<<<Item>(std::ostream&, const Item&); |
| 138 | template std::ostream& operator<<<Reference>(std::ostream&, const Reference&); |
| 139 | template std::ostream& operator<<<Id>(std::ostream&, const Id&); |
| 140 | template std::ostream& operator<<<RawString>(std::ostream&, const RawString&); |
| 141 | template std::ostream& operator<<<String>(std::ostream&, const String&); |
| 142 | template std::ostream& operator<<<StyledString>(std::ostream&, const StyledString&); |
| 143 | template std::ostream& operator<<<FileReference>(std::ostream&, const FileReference&); |
| 144 | template std::ostream& operator<<<BinaryPrimitive>(std::ostream&, const BinaryPrimitive&); |
| 145 | template std::ostream& operator<<<Attribute>(std::ostream&, const Attribute&); |
| 146 | template std::ostream& operator<<<Style>(std::ostream&, const Style&); |
| 147 | template std::ostream& operator<<<Array>(std::ostream&, const Array&); |
| 148 | template std::ostream& operator<<<Plural>(std::ostream&, const Plural&); |
| 149 | |
| 150 | // Add a print method to Maybe. |
| 151 | template <typename T> |
| 152 | void PrintTo(const Maybe<T>& value, std::ostream* out) { |
| 153 | if (value) { |
| 154 | *out << ::testing::PrintToString(value.value()); |
| 155 | } else { |
| 156 | *out << "Nothing"; |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | namespace test { |
| 161 | |
| 162 | MATCHER_P(ValueEq, a, |
| 163 | std::string(negation ? "isn't" : "is") + " equal to " + ::testing::PrintToString(a)) { |
| 164 | return arg.Equals(&a); |
| 165 | } |
| 166 | |
| 167 | } // namespace test |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 168 | } // namespace aapt |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 169 | |
| 170 | #endif /* AAPT_TEST_COMMON_H */ |