blob: 348c32a04e88d64df2070605981343be2bc41ed8 [file] [log] [blame]
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001/*
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 Lesinski9ba47d82015-10-13 11:37:10 -070021#include "Debug.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070022#include "ResourceTable.h"
23#include "ResourceUtils.h"
24#include "ValueVisitor.h"
Adam Lesinskia6fe3452015-12-09 15:20:52 -080025#include "io/File.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070026#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
40namespace aapt {
41namespace test {
42
43struct 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
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -080055inline IDiagnostics* getDiagnostics() {
56 static DummyDiagnosticsImpl diag;
57 return &diag;
58}
59
Adam Lesinski1ab598f2015-08-14 14:26:04 -070060inline ResourceName parseNameOrDie(const StringPiece16& str) {
61 ResourceNameRef ref;
62 bool result = ResourceUtils::tryParseReference(str, &ref);
63 assert(result && "invalid resource name");
64 return ref.toResourceName();
65}
66
67inline ConfigDescription parseConfigOrDie(const StringPiece& str) {
68 ConfigDescription config;
69 bool result = ConfigDescription::parse(str, &config);
70 assert(result && "invalid configuration");
71 return config;
72}
73
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -080074template <typename T> T* getValueForConfigAndProduct(ResourceTable* table,
75 const StringPiece16& resName,
76 const ConfigDescription& config,
77 const StringPiece& product) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -070078 Maybe<ResourceTable::SearchResult> result = table->findResource(parseNameOrDie(resName));
79 if (result) {
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -080080 ResourceConfigValue* configValue = result.value().entry->findValue(config, product);
81 if (configValue) {
82 return valueCast<T>(configValue->value.get());
Adam Lesinski1ab598f2015-08-14 14:26:04 -070083 }
84 }
85 return nullptr;
86}
87
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -080088template <typename T> T* getValueForConfig(ResourceTable* table, const StringPiece16& resName,
89 const ConfigDescription& config) {
90 return getValueForConfigAndProduct<T>(table, resName, config, {});
91}
92
Adam Lesinski1ab598f2015-08-14 14:26:04 -070093template <typename T> T* getValue(ResourceTable* table, const StringPiece16& resName) {
94 return getValueForConfig<T>(table, resName, {});
95}
96
Adam Lesinskia6fe3452015-12-09 15:20:52 -080097class TestFile : public io::IFile {
98private:
99 Source mSource;
100
101public:
102 TestFile(const StringPiece& path) : mSource(path) {}
103
104 std::unique_ptr<io::IData> openAsData() override {
105 return {};
106 }
107
108 const Source& getSource() const override {
109 return mSource;
110 }
111};
112
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700113} // namespace test
114} // namespace aapt
115
116#endif /* AAPT_TEST_COMMON_H */