blob: 248921f3afeaca1be72ee093eb74280fc8657d66 [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"
Adam Lesinskid5083f62017-01-16 15:07:21 -080024#include "androidfw/StringPiece.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070025#include "gtest/gtest.h"
26
Adam Lesinski1ab598f2015-08-14 14:26:04 -070027#include "ConfigDescription.h"
Adam Lesinski9ba47d82015-10-13 11:37:10 -070028#include "Debug.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070029#include "ResourceTable.h"
30#include "ResourceUtils.h"
31#include "ValueVisitor.h"
Adam Lesinskia6fe3452015-12-09 15:20:52 -080032#include "io/File.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070033#include "process/IResourceTableConsumer.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070034
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 Lesinskid5083f62017-01-16 15:07:21 -080071inline ResourceName ParseNameOrDie(const android::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 Lesinskid5083f62017-01-16 15:07:21 -080077inline ConfigDescription ParseConfigOrDie(const android::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 Lesinskid5083f62017-01-16 15:07:21 -080084T* GetValueForConfigAndProduct(ResourceTable* table, const android::StringPiece& res_name,
Adam Lesinskicacb28f2016-10-19 12:18:14 -070085 const ConfigDescription& config,
Adam Lesinskid5083f62017-01-16 15:07:21 -080086 const android::StringPiece& product) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070087 Maybe<ResourceTable::SearchResult> result =
Adam Lesinskice5e56e2016-10-21 17:56:45 -070088 table->FindResource(ParseNameOrDie(res_name));
Adam Lesinskicacb28f2016-10-19 12:18:14 -070089 if (result) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070090 ResourceConfigValue* config_value =
91 result.value().entry->FindValue(config, product);
92 if (config_value) {
93 return ValueCast<T>(config_value->value.get());
Adam Lesinski1ab598f2015-08-14 14:26:04 -070094 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -070095 }
96 return nullptr;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070097}
98
Adam Lesinskicacb28f2016-10-19 12:18:14 -070099template <typename T>
Adam Lesinskid5083f62017-01-16 15:07:21 -0800100T* GetValueForConfig(ResourceTable* table, const android::StringPiece& res_name,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700101 const ConfigDescription& config) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700102 return GetValueForConfigAndProduct<T>(table, res_name, config, {});
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800103}
104
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700105template <typename T>
Adam Lesinskid5083f62017-01-16 15:07:21 -0800106T* GetValue(ResourceTable* table, const android::StringPiece& res_name) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700107 return GetValueForConfig<T>(table, res_name, {});
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700108}
109
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800110class TestFile : public io::IFile {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700111 public:
Adam Lesinskid5083f62017-01-16 15:07:21 -0800112 explicit TestFile(const android::StringPiece& path) : source_(path) {}
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800113
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700114 std::unique_ptr<io::IData> OpenAsData() override { return {}; }
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800115
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700116 const Source& GetSource() const override { return source_; }
117
118 private:
119 DISALLOW_COPY_AND_ASSIGN(TestFile);
120
121 Source source_;
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800122};
123
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700124} // namespace test
125} // namespace aapt
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700126
127#endif /* AAPT_TEST_COMMON_H */