blob: faccd47783ff6ce3917d4371e17a42ebcf5a58e1 [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 {
Adam Lesinskicc5609d2016-04-05 12:41:07 -070044 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 Lesinski1ab598f2015-08-14 14:26:04 -070057 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -070058};
59
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -080060inline IDiagnostics* getDiagnostics() {
61 static DummyDiagnosticsImpl diag;
62 return &diag;
63}
64
Adam Lesinski1ab598f2015-08-14 14:26:04 -070065inline 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
72inline 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 Lesinskie4bb9eb2016-02-12 22:18:51 -080079template <typename T> T* getValueForConfigAndProduct(ResourceTable* table,
80 const StringPiece16& resName,
81 const ConfigDescription& config,
82 const StringPiece& product) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -070083 Maybe<ResourceTable::SearchResult> result = table->findResource(parseNameOrDie(resName));
84 if (result) {
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -080085 ResourceConfigValue* configValue = result.value().entry->findValue(config, product);
86 if (configValue) {
87 return valueCast<T>(configValue->value.get());
Adam Lesinski1ab598f2015-08-14 14:26:04 -070088 }
89 }
90 return nullptr;
91}
92
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -080093template <typename T> T* getValueForConfig(ResourceTable* table, const StringPiece16& resName,
94 const ConfigDescription& config) {
95 return getValueForConfigAndProduct<T>(table, resName, config, {});
96}
97
Adam Lesinski1ab598f2015-08-14 14:26:04 -070098template <typename T> T* getValue(ResourceTable* table, const StringPiece16& resName) {
99 return getValueForConfig<T>(table, resName, {});
100}
101
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800102class TestFile : public io::IFile {
103private:
104 Source mSource;
105
106public:
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 Lesinski1ab598f2015-08-14 14:26:04 -0700118} // namespace test
119} // namespace aapt
120
121#endif /* AAPT_TEST_COMMON_H */