blob: 05851485e21648a45b0a818d14e48a85b5eb0faf [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
Adam Lesinskibab4ef52017-06-01 15:22:57 -070049IDiagnostics* GetDiagnostics();
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -080050
Adam Lesinskid5083f62017-01-16 15:07:21 -080051inline ResourceName ParseNameOrDie(const android::StringPiece& str) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070052 ResourceNameRef ref;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070053 CHECK(ResourceUtils::ParseResourceName(str, &ref)) << "invalid resource name";
54 return ref.ToResourceName();
Adam Lesinski1ab598f2015-08-14 14:26:04 -070055}
56
Adam Lesinskid5083f62017-01-16 15:07:21 -080057inline ConfigDescription ParseConfigOrDie(const android::StringPiece& str) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070058 ConfigDescription config;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070059 CHECK(ConfigDescription::Parse(str, &config)) << "invalid configuration";
Adam Lesinskicacb28f2016-10-19 12:18:14 -070060 return config;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070061}
62
Adam Lesinskibab4ef52017-06-01 15:22:57 -070063template <typename T = Value>
Adam Lesinskid5083f62017-01-16 15:07:21 -080064T* GetValueForConfigAndProduct(ResourceTable* table, const android::StringPiece& res_name,
Adam Lesinskicacb28f2016-10-19 12:18:14 -070065 const ConfigDescription& config,
Adam Lesinskid5083f62017-01-16 15:07:21 -080066 const android::StringPiece& product) {
Adam Lesinski5924d8c2017-05-30 15:15:58 -070067 Maybe<ResourceTable::SearchResult> result = table->FindResource(ParseNameOrDie(res_name));
Adam Lesinskicacb28f2016-10-19 12:18:14 -070068 if (result) {
Adam Lesinski5924d8c2017-05-30 15:15:58 -070069 ResourceConfigValue* config_value = result.value().entry->FindValue(config, product);
Adam Lesinskice5e56e2016-10-21 17:56:45 -070070 if (config_value) {
71 return ValueCast<T>(config_value->value.get());
Adam Lesinski1ab598f2015-08-14 14:26:04 -070072 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -070073 }
74 return nullptr;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070075}
76
Adam Lesinskibab4ef52017-06-01 15:22:57 -070077template <>
78Value* GetValueForConfigAndProduct<Value>(ResourceTable* table,
79 const android::StringPiece& res_name,
80 const ConfigDescription& config,
81 const android::StringPiece& product);
82
83template <typename T = Value>
Adam Lesinskid5083f62017-01-16 15:07:21 -080084T* GetValueForConfig(ResourceTable* table, const android::StringPiece& res_name,
Adam Lesinskicacb28f2016-10-19 12:18:14 -070085 const ConfigDescription& config) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070086 return GetValueForConfigAndProduct<T>(table, res_name, config, {});
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -080087}
88
Adam Lesinskibab4ef52017-06-01 15:22:57 -070089template <typename T = Value>
Adam Lesinskid5083f62017-01-16 15:07:21 -080090T* GetValue(ResourceTable* table, const android::StringPiece& res_name) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070091 return GetValueForConfig<T>(table, res_name, {});
Adam Lesinski1ab598f2015-08-14 14:26:04 -070092}
93
Adam Lesinskia6fe3452015-12-09 15:20:52 -080094class TestFile : public io::IFile {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070095 public:
Adam Lesinskid5083f62017-01-16 15:07:21 -080096 explicit TestFile(const android::StringPiece& path) : source_(path) {}
Adam Lesinskia6fe3452015-12-09 15:20:52 -080097
Adam Lesinski5924d8c2017-05-30 15:15:58 -070098 std::unique_ptr<io::IData> OpenAsData() override {
99 return {};
100 }
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800101
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700102 const Source& GetSource() const override {
103 return source_;
104 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700105
106 private:
107 DISALLOW_COPY_AND_ASSIGN(TestFile);
108
109 Source source_;
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800110};
111
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700112} // namespace test
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700113
114// Workaround gtest bug (https://github.com/google/googletest/issues/443)
115// that does not select base class operator<< for derived class T.
116template <typename T>
117typename std::enable_if<std::is_base_of<Value, T>::value, std::ostream&>::type operator<<(
118 std::ostream& out, const T& value) {
119 value.Print(&out);
120 return out;
121}
122
123template std::ostream& operator<<<Item>(std::ostream&, const Item&);
124template std::ostream& operator<<<Reference>(std::ostream&, const Reference&);
125template std::ostream& operator<<<Id>(std::ostream&, const Id&);
126template std::ostream& operator<<<RawString>(std::ostream&, const RawString&);
127template std::ostream& operator<<<String>(std::ostream&, const String&);
128template std::ostream& operator<<<StyledString>(std::ostream&, const StyledString&);
129template std::ostream& operator<<<FileReference>(std::ostream&, const FileReference&);
130template std::ostream& operator<<<BinaryPrimitive>(std::ostream&, const BinaryPrimitive&);
131template std::ostream& operator<<<Attribute>(std::ostream&, const Attribute&);
132template std::ostream& operator<<<Style>(std::ostream&, const Style&);
133template std::ostream& operator<<<Array>(std::ostream&, const Array&);
134template std::ostream& operator<<<Plural>(std::ostream&, const Plural&);
135
136// Add a print method to Maybe.
137template <typename T>
138void PrintTo(const Maybe<T>& value, std::ostream* out) {
139 if (value) {
140 *out << ::testing::PrintToString(value.value());
141 } else {
142 *out << "Nothing";
143 }
144}
145
146namespace test {
147
148MATCHER_P(ValueEq, a,
149 std::string(negation ? "isn't" : "is") + " equal to " + ::testing::PrintToString(a)) {
150 return arg.Equals(&a);
151}
152
153} // namespace test
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700154} // namespace aapt
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700155
156#endif /* AAPT_TEST_COMMON_H */