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 | |
| 20 | #include "ConfigDescription.h" |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 21 | #include "Debug.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 22 | #include "ResourceTable.h" |
| 23 | #include "ResourceUtils.h" |
| 24 | #include "ValueVisitor.h" |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 25 | #include "io/File.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 26 | #include "process/IResourceTableConsumer.h" |
| 27 | #include "util/StringPiece.h" |
| 28 | |
| 29 | #include <gtest/gtest.h> |
| 30 | #include <iostream> |
| 31 | |
| 32 | // |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 33 | // GTEST 1.7 doesn't explicitly cast to bool, which causes explicit operators to |
| 34 | // fail to compile. |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 35 | // |
| 36 | #define AAPT_ASSERT_TRUE(v) ASSERT_TRUE(bool(v)) |
| 37 | #define AAPT_ASSERT_FALSE(v) ASSERT_FALSE(bool(v)) |
| 38 | #define AAPT_EXPECT_TRUE(v) EXPECT_TRUE(bool(v)) |
| 39 | #define AAPT_EXPECT_FALSE(v) EXPECT_FALSE(bool(v)) |
| 40 | |
| 41 | namespace aapt { |
| 42 | namespace test { |
| 43 | |
| 44 | struct DummyDiagnosticsImpl : public IDiagnostics { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 45 | void log(Level level, DiagMessageActual& actualMsg) override { |
| 46 | switch (level) { |
| 47 | case Level::Note: |
| 48 | return; |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 49 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 50 | case Level::Warn: |
| 51 | std::cerr << actualMsg.source << ": warn: " << actualMsg.message << "." |
| 52 | << std::endl; |
| 53 | break; |
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::Error: |
| 56 | std::cerr << actualMsg.source << ": error: " << actualMsg.message << "." |
| 57 | << std::endl; |
| 58 | break; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 59 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 60 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 61 | }; |
| 62 | |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 63 | inline IDiagnostics* getDiagnostics() { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 64 | static DummyDiagnosticsImpl diag; |
| 65 | return &diag; |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 66 | } |
| 67 | |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 68 | inline ResourceName parseNameOrDie(const StringPiece& str) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 69 | ResourceNameRef ref; |
| 70 | bool result = ResourceUtils::parseResourceName(str, &ref); |
| 71 | assert(result && "invalid resource name"); |
| 72 | return ref.toResourceName(); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | inline ConfigDescription parseConfigOrDie(const StringPiece& str) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 76 | ConfigDescription config; |
| 77 | bool result = ConfigDescription::parse(str, &config); |
| 78 | assert(result && "invalid configuration"); |
| 79 | return config; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 80 | } |
| 81 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 82 | template <typename T> |
| 83 | T* getValueForConfigAndProduct(ResourceTable* table, const StringPiece& resName, |
| 84 | const ConfigDescription& config, |
| 85 | const StringPiece& product) { |
| 86 | Maybe<ResourceTable::SearchResult> result = |
| 87 | table->findResource(parseNameOrDie(resName)); |
| 88 | if (result) { |
| 89 | ResourceConfigValue* configValue = |
| 90 | result.value().entry->findValue(config, product); |
| 91 | if (configValue) { |
| 92 | return valueCast<T>(configValue->value.get()); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 93 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 94 | } |
| 95 | return nullptr; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 96 | } |
| 97 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 98 | template <typename T> |
| 99 | T* getValueForConfig(ResourceTable* table, const StringPiece& resName, |
| 100 | const ConfigDescription& config) { |
| 101 | return getValueForConfigAndProduct<T>(table, resName, config, {}); |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 102 | } |
| 103 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 104 | template <typename T> |
| 105 | T* getValue(ResourceTable* table, const StringPiece& resName) { |
| 106 | return getValueForConfig<T>(table, resName, {}); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 107 | } |
| 108 | |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 109 | class TestFile : public io::IFile { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 110 | private: |
| 111 | Source mSource; |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 112 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 113 | public: |
| 114 | explicit TestFile(const StringPiece& path) : mSource(path) {} |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 115 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 116 | std::unique_ptr<io::IData> openAsData() override { return {}; } |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 117 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 118 | const Source& getSource() const override { return mSource; } |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 119 | }; |
| 120 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 121 | } // namespace test |
| 122 | } // namespace aapt |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 123 | |
| 124 | #endif /* AAPT_TEST_COMMON_H */ |