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" |
| 21 | #include "ResourceTable.h" |
| 22 | #include "ResourceUtils.h" |
| 23 | #include "ValueVisitor.h" |
| 24 | |
| 25 | #include "process/IResourceTableConsumer.h" |
| 26 | #include "util/StringPiece.h" |
| 27 | |
| 28 | #include <gtest/gtest.h> |
| 29 | #include <iostream> |
| 30 | |
| 31 | // |
| 32 | // GTEST 1.7 doesn't explicitly cast to bool, which causes explicit operators to fail to compile. |
| 33 | // |
| 34 | #define AAPT_ASSERT_TRUE(v) ASSERT_TRUE(bool(v)) |
| 35 | #define AAPT_ASSERT_FALSE(v) ASSERT_FALSE(bool(v)) |
| 36 | #define AAPT_EXPECT_TRUE(v) EXPECT_TRUE(bool(v)) |
| 37 | #define AAPT_EXPECT_FALSE(v) EXPECT_FALSE(bool(v)) |
| 38 | |
| 39 | namespace aapt { |
| 40 | namespace test { |
| 41 | |
| 42 | struct DummyDiagnosticsImpl : public IDiagnostics { |
| 43 | void error(const DiagMessage& message) override { |
| 44 | DiagMessageActual actual = message.build(); |
| 45 | std::cerr << actual.source << ": error: " << actual.message << "." << std::endl; |
| 46 | } |
| 47 | void warn(const DiagMessage& message) override { |
| 48 | DiagMessageActual actual = message.build(); |
| 49 | std::cerr << actual.source << ": warn: " << actual.message << "." << std::endl; |
| 50 | } |
| 51 | void note(const DiagMessage& message) override {} |
| 52 | }; |
| 53 | |
| 54 | inline ResourceName parseNameOrDie(const StringPiece16& str) { |
| 55 | ResourceNameRef ref; |
| 56 | bool result = ResourceUtils::tryParseReference(str, &ref); |
| 57 | assert(result && "invalid resource name"); |
| 58 | return ref.toResourceName(); |
| 59 | } |
| 60 | |
| 61 | inline ConfigDescription parseConfigOrDie(const StringPiece& str) { |
| 62 | ConfigDescription config; |
| 63 | bool result = ConfigDescription::parse(str, &config); |
| 64 | assert(result && "invalid configuration"); |
| 65 | return config; |
| 66 | } |
| 67 | |
| 68 | template <typename T> T* getValueForConfig(ResourceTable* table, const StringPiece16& resName, |
| 69 | const ConfigDescription& config) { |
| 70 | Maybe<ResourceTable::SearchResult> result = table->findResource(parseNameOrDie(resName)); |
| 71 | if (result) { |
| 72 | ResourceEntry* entry = result.value().entry; |
| 73 | auto iter = std::lower_bound(entry->values.begin(), entry->values.end(), config, |
| 74 | [](const ResourceConfigValue& a, const ConfigDescription& b) |
| 75 | -> bool { |
| 76 | return a.config < b; |
| 77 | }); |
| 78 | if (iter != entry->values.end() && iter->config == config) { |
| 79 | return valueCast<T>(iter->value.get()); |
| 80 | } |
| 81 | } |
| 82 | return nullptr; |
| 83 | } |
| 84 | |
| 85 | template <typename T> T* getValue(ResourceTable* table, const StringPiece16& resName) { |
| 86 | return getValueForConfig<T>(table, resName, {}); |
| 87 | } |
| 88 | |
| 89 | } // namespace test |
| 90 | } // namespace aapt |
| 91 | |
| 92 | #endif /* AAPT_TEST_COMMON_H */ |