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