blob: a937de8869a62b032f4969907574dd1d5224a9b1 [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 Lesinski5924d8c2017-05-30 15:15:58 -070025#include "gmock/gmock.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070026#include "gtest/gtest.h"
27
Adam Lesinski1ab598f2015-08-14 14:26:04 -070028#include "ConfigDescription.h"
Adam Lesinski9ba47d82015-10-13 11:37:10 -070029#include "Debug.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070030#include "ResourceTable.h"
31#include "ResourceUtils.h"
Adam Lesinski5924d8c2017-05-30 15:15:58 -070032#include "ResourceValues.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070033#include "ValueVisitor.h"
Adam Lesinskia6fe3452015-12-09 15:20:52 -080034#include "io/File.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070035#include "process/IResourceTableConsumer.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070036
Adam Lesinski1ab598f2015-08-14 14:26:04 -070037//
Adam Lesinskicacb28f2016-10-19 12:18:14 -070038// GTEST 1.7 doesn't explicitly cast to bool, which causes explicit operators to
39// fail to compile.
Adam Lesinski1ab598f2015-08-14 14:26:04 -070040//
41#define AAPT_ASSERT_TRUE(v) ASSERT_TRUE(bool(v))
42#define AAPT_ASSERT_FALSE(v) ASSERT_FALSE(bool(v))
43#define AAPT_EXPECT_TRUE(v) EXPECT_TRUE(bool(v))
44#define AAPT_EXPECT_FALSE(v) EXPECT_FALSE(bool(v))
45
46namespace aapt {
47namespace test {
48
49struct DummyDiagnosticsImpl : public IDiagnostics {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070050 void Log(Level level, DiagMessageActual& actual_msg) override {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070051 switch (level) {
52 case Level::Note:
53 return;
Adam Lesinskicc5609d2016-04-05 12:41:07 -070054
Adam Lesinskicacb28f2016-10-19 12:18:14 -070055 case Level::Warn:
Adam Lesinski5924d8c2017-05-30 15:15:58 -070056 std::cerr << actual_msg.source << ": warn: " << actual_msg.message << "." << std::endl;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070057 break;
Adam Lesinskicc5609d2016-04-05 12:41:07 -070058
Adam Lesinskicacb28f2016-10-19 12:18:14 -070059 case Level::Error:
Adam Lesinski5924d8c2017-05-30 15:15:58 -070060 std::cerr << actual_msg.source << ": error: " << actual_msg.message << "." << 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 Lesinski5924d8c2017-05-30 15:15:58 -070087 Maybe<ResourceTable::SearchResult> result = table->FindResource(ParseNameOrDie(res_name));
Adam Lesinskicacb28f2016-10-19 12:18:14 -070088 if (result) {
Adam Lesinski5924d8c2017-05-30 15:15:58 -070089 ResourceConfigValue* config_value = result.value().entry->FindValue(config, product);
Adam Lesinskice5e56e2016-10-21 17:56:45 -070090 if (config_value) {
91 return ValueCast<T>(config_value->value.get());
Adam Lesinski1ab598f2015-08-14 14:26:04 -070092 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -070093 }
94 return nullptr;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070095}
96
Adam Lesinskicacb28f2016-10-19 12:18:14 -070097template <typename T>
Adam Lesinskid5083f62017-01-16 15:07:21 -080098T* GetValueForConfig(ResourceTable* table, const android::StringPiece& res_name,
Adam Lesinskicacb28f2016-10-19 12:18:14 -070099 const ConfigDescription& config) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700100 return GetValueForConfigAndProduct<T>(table, res_name, config, {});
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800101}
102
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700103template <typename T>
Adam Lesinskid5083f62017-01-16 15:07:21 -0800104T* GetValue(ResourceTable* table, const android::StringPiece& res_name) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700105 return GetValueForConfig<T>(table, res_name, {});
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700106}
107
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800108class TestFile : public io::IFile {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700109 public:
Adam Lesinskid5083f62017-01-16 15:07:21 -0800110 explicit TestFile(const android::StringPiece& path) : source_(path) {}
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800111
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700112 std::unique_ptr<io::IData> OpenAsData() override {
113 return {};
114 }
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800115
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700116 const Source& GetSource() const override {
117 return source_;
118 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700119
120 private:
121 DISALLOW_COPY_AND_ASSIGN(TestFile);
122
123 Source source_;
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800124};
125
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700126} // namespace test
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700127
128// Workaround gtest bug (https://github.com/google/googletest/issues/443)
129// that does not select base class operator<< for derived class T.
130template <typename T>
131typename std::enable_if<std::is_base_of<Value, T>::value, std::ostream&>::type operator<<(
132 std::ostream& out, const T& value) {
133 value.Print(&out);
134 return out;
135}
136
137template std::ostream& operator<<<Item>(std::ostream&, const Item&);
138template std::ostream& operator<<<Reference>(std::ostream&, const Reference&);
139template std::ostream& operator<<<Id>(std::ostream&, const Id&);
140template std::ostream& operator<<<RawString>(std::ostream&, const RawString&);
141template std::ostream& operator<<<String>(std::ostream&, const String&);
142template std::ostream& operator<<<StyledString>(std::ostream&, const StyledString&);
143template std::ostream& operator<<<FileReference>(std::ostream&, const FileReference&);
144template std::ostream& operator<<<BinaryPrimitive>(std::ostream&, const BinaryPrimitive&);
145template std::ostream& operator<<<Attribute>(std::ostream&, const Attribute&);
146template std::ostream& operator<<<Style>(std::ostream&, const Style&);
147template std::ostream& operator<<<Array>(std::ostream&, const Array&);
148template std::ostream& operator<<<Plural>(std::ostream&, const Plural&);
149
150// Add a print method to Maybe.
151template <typename T>
152void PrintTo(const Maybe<T>& value, std::ostream* out) {
153 if (value) {
154 *out << ::testing::PrintToString(value.value());
155 } else {
156 *out << "Nothing";
157 }
158}
159
160namespace test {
161
162MATCHER_P(ValueEq, a,
163 std::string(negation ? "isn't" : "is") + " equal to " + ::testing::PrintToString(a)) {
164 return arg.Equals(&a);
165}
166
167} // namespace test
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700168} // namespace aapt
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700169
170#endif /* AAPT_TEST_COMMON_H */