blob: 36892017f2d30f0a821106e87dea525d86981e2b [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
Adam Lesinskice5e56e2016-10-21 17:56:45 -070020#include <iostream>
21
22#include "android-base/logging.h"
23#include "android-base/macros.h"
24#include "gtest/gtest.h"
25
Adam Lesinski1ab598f2015-08-14 14:26:04 -070026#include "ConfigDescription.h"
Adam Lesinski9ba47d82015-10-13 11:37:10 -070027#include "Debug.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070028#include "ResourceTable.h"
29#include "ResourceUtils.h"
30#include "ValueVisitor.h"
Adam Lesinskia6fe3452015-12-09 15:20:52 -080031#include "io/File.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070032#include "process/IResourceTableConsumer.h"
33#include "util/StringPiece.h"
34
Adam Lesinski1ab598f2015-08-14 14:26:04 -070035//
Adam Lesinskicacb28f2016-10-19 12:18:14 -070036// GTEST 1.7 doesn't explicitly cast to bool, which causes explicit operators to
37// fail to compile.
Adam Lesinski1ab598f2015-08-14 14:26:04 -070038//
39#define AAPT_ASSERT_TRUE(v) ASSERT_TRUE(bool(v))
40#define AAPT_ASSERT_FALSE(v) ASSERT_FALSE(bool(v))
41#define AAPT_EXPECT_TRUE(v) EXPECT_TRUE(bool(v))
42#define AAPT_EXPECT_FALSE(v) EXPECT_FALSE(bool(v))
43
44namespace aapt {
45namespace test {
46
47struct DummyDiagnosticsImpl : public IDiagnostics {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070048 void Log(Level level, DiagMessageActual& actual_msg) override {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070049 switch (level) {
50 case Level::Note:
51 return;
Adam Lesinskicc5609d2016-04-05 12:41:07 -070052
Adam Lesinskicacb28f2016-10-19 12:18:14 -070053 case Level::Warn:
Adam Lesinskice5e56e2016-10-21 17:56:45 -070054 std::cerr << actual_msg.source << ": warn: " << actual_msg.message
55 << "." << std::endl;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070056 break;
Adam Lesinskicc5609d2016-04-05 12:41:07 -070057
Adam Lesinskicacb28f2016-10-19 12:18:14 -070058 case Level::Error:
Adam Lesinskice5e56e2016-10-21 17:56:45 -070059 std::cerr << actual_msg.source << ": error: " << actual_msg.message
60 << "." << std::endl;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070061 break;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070062 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -070063 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -070064};
65
Adam Lesinskice5e56e2016-10-21 17:56:45 -070066inline IDiagnostics* GetDiagnostics() {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070067 static DummyDiagnosticsImpl diag;
68 return &diag;
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -080069}
70
Adam Lesinskice5e56e2016-10-21 17:56:45 -070071inline ResourceName ParseNameOrDie(const StringPiece& str) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070072 ResourceNameRef ref;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070073 CHECK(ResourceUtils::ParseResourceName(str, &ref)) << "invalid resource name";
74 return ref.ToResourceName();
Adam Lesinski1ab598f2015-08-14 14:26:04 -070075}
76
Adam Lesinskice5e56e2016-10-21 17:56:45 -070077inline ConfigDescription ParseConfigOrDie(const StringPiece& str) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070078 ConfigDescription config;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070079 CHECK(ConfigDescription::Parse(str, &config)) << "invalid configuration";
Adam Lesinskicacb28f2016-10-19 12:18:14 -070080 return config;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070081}
82
Adam Lesinskicacb28f2016-10-19 12:18:14 -070083template <typename T>
Adam Lesinskice5e56e2016-10-21 17:56:45 -070084T* GetValueForConfigAndProduct(ResourceTable* table,
85 const StringPiece& res_name,
Adam Lesinskicacb28f2016-10-19 12:18:14 -070086 const ConfigDescription& config,
87 const StringPiece& product) {
88 Maybe<ResourceTable::SearchResult> result =
Adam Lesinskice5e56e2016-10-21 17:56:45 -070089 table->FindResource(ParseNameOrDie(res_name));
Adam Lesinskicacb28f2016-10-19 12:18:14 -070090 if (result) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070091 ResourceConfigValue* config_value =
92 result.value().entry->FindValue(config, product);
93 if (config_value) {
94 return ValueCast<T>(config_value->value.get());
Adam Lesinski1ab598f2015-08-14 14:26:04 -070095 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -070096 }
97 return nullptr;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070098}
99
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700100template <typename T>
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700101T* GetValueForConfig(ResourceTable* table, const StringPiece& res_name,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700102 const ConfigDescription& config) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700103 return GetValueForConfigAndProduct<T>(table, res_name, config, {});
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800104}
105
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700106template <typename T>
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700107T* GetValue(ResourceTable* table, const StringPiece& res_name) {
108 return GetValueForConfig<T>(table, res_name, {});
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700109}
110
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800111class TestFile : public io::IFile {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700112 public:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700113 explicit TestFile(const StringPiece& path) : source_(path) {}
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800114
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700115 std::unique_ptr<io::IData> OpenAsData() override { return {}; }
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800116
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700117 const Source& GetSource() const override { return source_; }
118
119 private:
120 DISALLOW_COPY_AND_ASSIGN(TestFile);
121
122 Source source_;
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800123};
124
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700125} // namespace test
126} // namespace aapt
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700127
128#endif /* AAPT_TEST_COMMON_H */