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" |
| 25 | |
| 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 { |
| 44 | void error(const DiagMessage& message) override { |
| 45 | DiagMessageActual actual = message.build(); |
| 46 | std::cerr << actual.source << ": error: " << actual.message << "." << std::endl; |
| 47 | } |
| 48 | void warn(const DiagMessage& message) override { |
| 49 | DiagMessageActual actual = message.build(); |
| 50 | std::cerr << actual.source << ": warn: " << actual.message << "." << std::endl; |
| 51 | } |
| 52 | void note(const DiagMessage& message) override {} |
| 53 | }; |
| 54 | |
| 55 | inline ResourceName parseNameOrDie(const StringPiece16& str) { |
| 56 | ResourceNameRef ref; |
| 57 | bool result = ResourceUtils::tryParseReference(str, &ref); |
| 58 | assert(result && "invalid resource name"); |
| 59 | return ref.toResourceName(); |
| 60 | } |
| 61 | |
| 62 | inline ConfigDescription parseConfigOrDie(const StringPiece& str) { |
| 63 | ConfigDescription config; |
| 64 | bool result = ConfigDescription::parse(str, &config); |
| 65 | assert(result && "invalid configuration"); |
| 66 | return config; |
| 67 | } |
| 68 | |
| 69 | template <typename T> T* getValueForConfig(ResourceTable* table, const StringPiece16& resName, |
| 70 | const ConfigDescription& config) { |
| 71 | Maybe<ResourceTable::SearchResult> result = table->findResource(parseNameOrDie(resName)); |
| 72 | if (result) { |
| 73 | ResourceEntry* entry = result.value().entry; |
| 74 | auto iter = std::lower_bound(entry->values.begin(), entry->values.end(), config, |
| 75 | [](const ResourceConfigValue& a, const ConfigDescription& b) |
| 76 | -> bool { |
| 77 | return a.config < b; |
| 78 | }); |
| 79 | if (iter != entry->values.end() && iter->config == config) { |
| 80 | return valueCast<T>(iter->value.get()); |
| 81 | } |
| 82 | } |
| 83 | return nullptr; |
| 84 | } |
| 85 | |
| 86 | template <typename T> T* getValue(ResourceTable* table, const StringPiece16& resName) { |
| 87 | return getValueForConfig<T>(table, resName, {}); |
| 88 | } |
| 89 | |
| 90 | } // namespace test |
| 91 | } // namespace aapt |
| 92 | |
| 93 | #endif /* AAPT_TEST_COMMON_H */ |