blob: f3da780cacbdf3ec2d6e3f5794639e5f095ebe19 [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_BUILDERS_H
18#define AAPT_TEST_BUILDERS_H
19
Adam Lesinskice5e56e2016-10-21 17:56:45 -070020#include <memory>
21
22#include "android-base/logging.h"
23#include "android-base/macros.h"
24
Adam Lesinski1ab598f2015-08-14 14:26:04 -070025#include "ResourceTable.h"
26#include "ResourceValues.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070027#include "test/Common.h"
Adam Lesinski467f1712015-11-16 17:35:44 -080028#include "util/Util.h"
29#include "xml/XmlDom.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070030
Adam Lesinski1ab598f2015-08-14 14:26:04 -070031namespace aapt {
32namespace test {
33
34class ResourceTableBuilder {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070035 public:
36 ResourceTableBuilder() = default;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070037
Adam Lesinskice5e56e2016-10-21 17:56:45 -070038 StringPool* string_pool() { return &table_->string_pool; }
Adam Lesinskia5870652015-11-20 15:32:30 -080039
Adam Lesinskid5083f62017-01-16 15:07:21 -080040 ResourceTableBuilder& SetPackageId(const android::StringPiece& package_name, uint8_t id) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070041 ResourceTablePackage* package = table_->CreatePackage(package_name, id);
42 CHECK(package != nullptr);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070043 return *this;
44 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -070045
Adam Lesinskid5083f62017-01-16 15:07:21 -080046 ResourceTableBuilder& AddSimple(const android::StringPiece& name, const ResourceId& id = {}) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070047 return AddValue(name, id, util::make_unique<Id>());
Adam Lesinskicacb28f2016-10-19 12:18:14 -070048 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -070049
Adam Lesinskid5083f62017-01-16 15:07:21 -080050 ResourceTableBuilder& AddSimple(const android::StringPiece& name, const ConfigDescription& config,
Adam Lesinskicacb28f2016-10-19 12:18:14 -070051 const ResourceId& id = {}) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070052 return AddValue(name, config, id, util::make_unique<Id>());
Adam Lesinskicacb28f2016-10-19 12:18:14 -070053 }
Adam Lesinskifb6312f2016-06-28 14:40:32 -070054
Adam Lesinskid5083f62017-01-16 15:07:21 -080055 ResourceTableBuilder& AddReference(const android::StringPiece& name,
56 const android::StringPiece& ref) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070057 return AddReference(name, {}, ref);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070058 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -070059
Adam Lesinskid5083f62017-01-16 15:07:21 -080060 ResourceTableBuilder& AddReference(const android::StringPiece& name, const ResourceId& id,
61 const android::StringPiece& ref) {
Adam Lesinski90919972017-06-01 15:22:57 -070062 return AddValue(name, id, util::make_unique<Reference>(ParseNameOrDie(ref)));
Adam Lesinskicacb28f2016-10-19 12:18:14 -070063 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -070064
Adam Lesinskid5083f62017-01-16 15:07:21 -080065 ResourceTableBuilder& AddString(const android::StringPiece& name,
66 const android::StringPiece& str) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070067 return AddString(name, {}, str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070068 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -070069
Adam Lesinskid5083f62017-01-16 15:07:21 -080070 ResourceTableBuilder& AddString(const android::StringPiece& name, const ResourceId& id,
71 const android::StringPiece& str) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070072 return AddValue(
73 name, id, util::make_unique<String>(table_->string_pool.MakeRef(str)));
Adam Lesinskicacb28f2016-10-19 12:18:14 -070074 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -070075
Adam Lesinskid5083f62017-01-16 15:07:21 -080076 ResourceTableBuilder& AddString(const android::StringPiece& name, const ResourceId& id,
Adam Lesinskicacb28f2016-10-19 12:18:14 -070077 const ConfigDescription& config,
Adam Lesinskid5083f62017-01-16 15:07:21 -080078 const android::StringPiece& str) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070079 return AddValue(name, config, id, util::make_unique<String>(
80 table_->string_pool.MakeRef(str)));
Adam Lesinskicacb28f2016-10-19 12:18:14 -070081 }
Adam Lesinski393b5f02015-12-17 13:03:11 -080082
Adam Lesinskid5083f62017-01-16 15:07:21 -080083 ResourceTableBuilder& AddFileReference(const android::StringPiece& name,
84 const android::StringPiece& path) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070085 return AddFileReference(name, {}, path);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070086 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -070087
Adam Lesinskid5083f62017-01-16 15:07:21 -080088 ResourceTableBuilder& AddFileReference(const android::StringPiece& name, const ResourceId& id,
89 const android::StringPiece& path) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070090 return AddValue(name, id, util::make_unique<FileReference>(
91 table_->string_pool.MakeRef(path)));
Adam Lesinskicacb28f2016-10-19 12:18:14 -070092 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -070093
Adam Lesinskid5083f62017-01-16 15:07:21 -080094 ResourceTableBuilder& AddFileReference(const android::StringPiece& name,
95 const android::StringPiece& path,
Adam Lesinskicacb28f2016-10-19 12:18:14 -070096 const ConfigDescription& config) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070097 return AddValue(name, config, {}, util::make_unique<FileReference>(
98 table_->string_pool.MakeRef(path)));
Adam Lesinskicacb28f2016-10-19 12:18:14 -070099 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700100
Adam Lesinskid5083f62017-01-16 15:07:21 -0800101 ResourceTableBuilder& AddValue(const android::StringPiece& name, std::unique_ptr<Value> value) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700102 return AddValue(name, {}, std::move(value));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700103 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700104
Adam Lesinskid5083f62017-01-16 15:07:21 -0800105 ResourceTableBuilder& AddValue(const android::StringPiece& name, const ResourceId& id,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700106 std::unique_ptr<Value> value) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700107 return AddValue(name, {}, id, std::move(value));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700108 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700109
Adam Lesinskid5083f62017-01-16 15:07:21 -0800110 ResourceTableBuilder& AddValue(const android::StringPiece& name, const ConfigDescription& config,
111 const ResourceId& id, std::unique_ptr<Value> value) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700112 ResourceName res_name = ParseNameOrDie(name);
Adam Lesinski90919972017-06-01 15:22:57 -0700113 CHECK(table_->AddResourceAllowMangled(res_name, id, config, {}, std::move(value),
114 GetDiagnostics()));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700115 return *this;
116 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700117
Adam Lesinskid5083f62017-01-16 15:07:21 -0800118 ResourceTableBuilder& SetSymbolState(const android::StringPiece& name, const ResourceId& id,
Adam Lesinski4488f1c2017-05-26 17:33:38 -0700119 SymbolState state, bool allow_new = false) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700120 ResourceName res_name = ParseNameOrDie(name);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700121 Symbol symbol;
122 symbol.state = state;
Adam Lesinski4488f1c2017-05-26 17:33:38 -0700123 symbol.allow_new = allow_new;
Adam Lesinski90919972017-06-01 15:22:57 -0700124 CHECK(table_->SetSymbolStateAllowMangled(res_name, id, symbol, GetDiagnostics()));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700125 return *this;
126 }
Adam Lesinskie78fd612015-10-22 12:48:43 -0700127
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700128 std::unique_ptr<ResourceTable> Build() { return std::move(table_); }
129
130 private:
131 DISALLOW_COPY_AND_ASSIGN(ResourceTableBuilder);
132
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700133 std::unique_ptr<ResourceTable> table_ = util::make_unique<ResourceTable>();
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700134};
135
Adam Lesinskid5083f62017-01-16 15:07:21 -0800136inline std::unique_ptr<Reference> BuildReference(const android::StringPiece& ref,
137 const Maybe<ResourceId>& id = {}) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700138 std::unique_ptr<Reference> reference =
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700139 util::make_unique<Reference>(ParseNameOrDie(ref));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700140 reference->id = id;
141 return reference;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700142}
143
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700144inline std::unique_ptr<BinaryPrimitive> BuildPrimitive(uint8_t type,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700145 uint32_t data) {
146 android::Res_value value = {};
147 value.size = sizeof(value);
148 value.dataType = type;
149 value.data = data;
150 return util::make_unique<BinaryPrimitive>(value);
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800151}
152
Adam Lesinskie78fd612015-10-22 12:48:43 -0700153template <typename T>
154class ValueBuilder {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700155 public:
156 template <typename... Args>
157 explicit ValueBuilder(Args&&... args)
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700158 : value_(new T{std::forward<Args>(args)...}) {}
Adam Lesinskie78fd612015-10-22 12:48:43 -0700159
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700160 template <typename... Args>
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700161 ValueBuilder& SetSource(Args&&... args) {
162 value_->SetSource(Source{std::forward<Args>(args)...});
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700163 return *this;
164 }
Adam Lesinskie78fd612015-10-22 12:48:43 -0700165
Adam Lesinskid5083f62017-01-16 15:07:21 -0800166 ValueBuilder& SetComment(const android::StringPiece& str) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700167 value_->SetComment(str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700168 return *this;
169 }
Adam Lesinskie78fd612015-10-22 12:48:43 -0700170
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700171 std::unique_ptr<Value> Build() { return std::move(value_); }
172
173 private:
174 DISALLOW_COPY_AND_ASSIGN(ValueBuilder);
175
176 std::unique_ptr<Value> value_;
Adam Lesinskie78fd612015-10-22 12:48:43 -0700177};
178
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700179class AttributeBuilder {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700180 public:
181 explicit AttributeBuilder(bool weak = false)
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700182 : attr_(util::make_unique<Attribute>(weak)) {
183 attr_->type_mask = android::ResTable_map::TYPE_ANY;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700184 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700185
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700186 AttributeBuilder& SetTypeMask(uint32_t typeMask) {
187 attr_->type_mask = typeMask;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700188 return *this;
189 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700190
Adam Lesinskid5083f62017-01-16 15:07:21 -0800191 AttributeBuilder& AddItem(const android::StringPiece& name, uint32_t value) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700192 attr_->symbols.push_back(Attribute::Symbol{
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700193 Reference(ResourceName({}, ResourceType::kId, name)), value});
194 return *this;
195 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700196
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700197 std::unique_ptr<Attribute> Build() { return std::move(attr_); }
198
199 private:
200 DISALLOW_COPY_AND_ASSIGN(AttributeBuilder);
201
202 std::unique_ptr<Attribute> attr_;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700203};
204
205class StyleBuilder {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700206 public:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700207 StyleBuilder() = default;
208
Adam Lesinskid5083f62017-01-16 15:07:21 -0800209 StyleBuilder& SetParent(const android::StringPiece& str) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700210 style_->parent = Reference(ParseNameOrDie(str));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700211 return *this;
212 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700213
Adam Lesinskid5083f62017-01-16 15:07:21 -0800214 StyleBuilder& AddItem(const android::StringPiece& str, std::unique_ptr<Item> value) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700215 style_->entries.push_back(
216 Style::Entry{Reference(ParseNameOrDie(str)), std::move(value)});
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700217 return *this;
218 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700219
Adam Lesinskid5083f62017-01-16 15:07:21 -0800220 StyleBuilder& AddItem(const android::StringPiece& str, const ResourceId& id,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700221 std::unique_ptr<Item> value) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700222 AddItem(str, std::move(value));
223 style_->entries.back().key.id = id;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700224 return *this;
225 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700226
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700227 std::unique_ptr<Style> Build() { return std::move(style_); }
228
229 private:
230 DISALLOW_COPY_AND_ASSIGN(StyleBuilder);
231
232 std::unique_ptr<Style> style_ = util::make_unique<Style>();
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700233};
234
235class StyleableBuilder {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700236 public:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700237 StyleableBuilder() = default;
238
Adam Lesinskid5083f62017-01-16 15:07:21 -0800239 StyleableBuilder& AddItem(const android::StringPiece& str, const Maybe<ResourceId>& id = {}) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700240 styleable_->entries.push_back(Reference(ParseNameOrDie(str)));
241 styleable_->entries.back().id = id;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700242 return *this;
243 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700244
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700245 std::unique_ptr<Styleable> Build() { return std::move(styleable_); }
246
247 private:
248 DISALLOW_COPY_AND_ASSIGN(StyleableBuilder);
249
250 std::unique_ptr<Styleable> styleable_ = util::make_unique<Styleable>();
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700251};
252
Adam Lesinskid5083f62017-01-16 15:07:21 -0800253inline std::unique_ptr<xml::XmlResource> BuildXmlDom(const android::StringPiece& str) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700254 std::stringstream in;
255 in << "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" << str;
256 StdErrDiagnostics diag;
257 std::unique_ptr<xml::XmlResource> doc =
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700258 xml::Inflate(&in, &diag, Source("test.xml"));
259 CHECK(doc != nullptr) << "failed to parse inline XML string";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700260 return doc;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700261}
262
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700263inline std::unique_ptr<xml::XmlResource> BuildXmlDomForPackageName(
Adam Lesinskid5083f62017-01-16 15:07:21 -0800264 IAaptContext* context, const android::StringPiece& str) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700265 std::unique_ptr<xml::XmlResource> doc = BuildXmlDom(str);
266 doc->file.name.package = context->GetCompilationPackage();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700267 return doc;
Adam Lesinski467f1712015-11-16 17:35:44 -0800268}
269
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700270} // namespace test
271} // namespace aapt
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700272
273#endif /* AAPT_TEST_BUILDERS_H */