blob: 2d571e7624e5bdfbe68d03003f2fcd24dfa11180 [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//
Adam Lesinskicacb28f2016-10-19 12:18:14 -070033// GTEST 1.7 doesn't explicitly cast to bool, which causes explicit operators to
34// fail to compile.
Adam Lesinski1ab598f2015-08-14 14:26:04 -070035//
36#define AAPT_ASSERT_TRUE(v) ASSERT_TRUE(bool(v))
37#define AAPT_ASSERT_FALSE(v) ASSERT_FALSE(bool(v))
38#define AAPT_EXPECT_TRUE(v) EXPECT_TRUE(bool(v))
39#define AAPT_EXPECT_FALSE(v) EXPECT_FALSE(bool(v))
40
41namespace aapt {
42namespace test {
43
44struct DummyDiagnosticsImpl : public IDiagnostics {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070045 void log(Level level, DiagMessageActual& actualMsg) override {
46 switch (level) {
47 case Level::Note:
48 return;
Adam Lesinskicc5609d2016-04-05 12:41:07 -070049
Adam Lesinskicacb28f2016-10-19 12:18:14 -070050 case Level::Warn:
51 std::cerr << actualMsg.source << ": warn: " << actualMsg.message << "."
52 << std::endl;
53 break;
Adam Lesinskicc5609d2016-04-05 12:41:07 -070054
Adam Lesinskicacb28f2016-10-19 12:18:14 -070055 case Level::Error:
56 std::cerr << actualMsg.source << ": error: " << actualMsg.message << "."
57 << std::endl;
58 break;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070059 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -070060 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -070061};
62
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -080063inline IDiagnostics* getDiagnostics() {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070064 static DummyDiagnosticsImpl diag;
65 return &diag;
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -080066}
67
Adam Lesinskid0f116b2016-07-08 15:00:32 -070068inline ResourceName parseNameOrDie(const StringPiece& str) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070069 ResourceNameRef ref;
70 bool result = ResourceUtils::parseResourceName(str, &ref);
71 assert(result && "invalid resource name");
72 return ref.toResourceName();
Adam Lesinski1ab598f2015-08-14 14:26:04 -070073}
74
75inline ConfigDescription parseConfigOrDie(const StringPiece& str) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070076 ConfigDescription config;
77 bool result = ConfigDescription::parse(str, &config);
78 assert(result && "invalid configuration");
79 return config;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070080}
81
Adam Lesinskicacb28f2016-10-19 12:18:14 -070082template <typename T>
83T* getValueForConfigAndProduct(ResourceTable* table, const StringPiece& resName,
84 const ConfigDescription& config,
85 const StringPiece& product) {
86 Maybe<ResourceTable::SearchResult> result =
87 table->findResource(parseNameOrDie(resName));
88 if (result) {
89 ResourceConfigValue* configValue =
90 result.value().entry->findValue(config, product);
91 if (configValue) {
92 return valueCast<T>(configValue->value.get());
Adam Lesinski1ab598f2015-08-14 14:26:04 -070093 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -070094 }
95 return nullptr;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070096}
97
Adam Lesinskicacb28f2016-10-19 12:18:14 -070098template <typename T>
99T* getValueForConfig(ResourceTable* table, const StringPiece& resName,
100 const ConfigDescription& config) {
101 return getValueForConfigAndProduct<T>(table, resName, config, {});
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800102}
103
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700104template <typename T>
105T* getValue(ResourceTable* table, const StringPiece& resName) {
106 return getValueForConfig<T>(table, resName, {});
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700107}
108
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800109class TestFile : public io::IFile {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700110 private:
111 Source mSource;
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800112
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700113 public:
114 explicit TestFile(const StringPiece& path) : mSource(path) {}
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800115
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700116 std::unique_ptr<io::IData> openAsData() override { return {}; }
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800117
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700118 const Source& getSource() const override { return mSource; }
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800119};
120
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700121} // namespace test
122} // namespace aapt
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700123
124#endif /* AAPT_TEST_COMMON_H */