blob: 9377306dc1b6e23e1ae14bb09b9cf6493dd4898b [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 Lesinskice5e56e2016-10-21 17:56:45 -070040 ResourceTableBuilder& SetPackageId(const StringPiece& package_name,
Adam Lesinskicacb28f2016-10-19 12:18:14 -070041 uint8_t id) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070042 ResourceTablePackage* package = table_->CreatePackage(package_name, id);
43 CHECK(package != nullptr);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070044 return *this;
45 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -070046
Adam Lesinskice5e56e2016-10-21 17:56:45 -070047 ResourceTableBuilder& AddSimple(const StringPiece& name,
Adam Lesinskicacb28f2016-10-19 12:18:14 -070048 const ResourceId& id = {}) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070049 return AddValue(name, id, util::make_unique<Id>());
Adam Lesinskicacb28f2016-10-19 12:18:14 -070050 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -070051
Adam Lesinskice5e56e2016-10-21 17:56:45 -070052 ResourceTableBuilder& AddSimple(const StringPiece& name,
Adam Lesinskicacb28f2016-10-19 12:18:14 -070053 const ConfigDescription& config,
54 const ResourceId& id = {}) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070055 return AddValue(name, config, id, util::make_unique<Id>());
Adam Lesinskicacb28f2016-10-19 12:18:14 -070056 }
Adam Lesinskifb6312f2016-06-28 14:40:32 -070057
Adam Lesinskice5e56e2016-10-21 17:56:45 -070058 ResourceTableBuilder& AddReference(const StringPiece& name,
Adam Lesinskicacb28f2016-10-19 12:18:14 -070059 const StringPiece& ref) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070060 return AddReference(name, {}, ref);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070061 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -070062
Adam Lesinskice5e56e2016-10-21 17:56:45 -070063 ResourceTableBuilder& AddReference(const StringPiece& name,
Adam Lesinskicacb28f2016-10-19 12:18:14 -070064 const ResourceId& id,
65 const StringPiece& ref) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070066 return AddValue(name, id,
67 util::make_unique<Reference>(ParseNameOrDie(ref)));
Adam Lesinskicacb28f2016-10-19 12:18:14 -070068 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -070069
Adam Lesinskice5e56e2016-10-21 17:56:45 -070070 ResourceTableBuilder& AddString(const StringPiece& name,
Adam Lesinskicacb28f2016-10-19 12:18:14 -070071 const StringPiece& str) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070072 return AddString(name, {}, str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070073 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -070074
Adam Lesinskice5e56e2016-10-21 17:56:45 -070075 ResourceTableBuilder& AddString(const StringPiece& name, const ResourceId& id,
Adam Lesinskicacb28f2016-10-19 12:18:14 -070076 const StringPiece& str) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070077 return AddValue(
78 name, id, util::make_unique<String>(table_->string_pool.MakeRef(str)));
Adam Lesinskicacb28f2016-10-19 12:18:14 -070079 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -070080
Adam Lesinskice5e56e2016-10-21 17:56:45 -070081 ResourceTableBuilder& AddString(const StringPiece& name, const ResourceId& id,
Adam Lesinskicacb28f2016-10-19 12:18:14 -070082 const ConfigDescription& config,
83 const StringPiece& str) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070084 return AddValue(name, config, id, util::make_unique<String>(
85 table_->string_pool.MakeRef(str)));
Adam Lesinskicacb28f2016-10-19 12:18:14 -070086 }
Adam Lesinski393b5f02015-12-17 13:03:11 -080087
Adam Lesinskice5e56e2016-10-21 17:56:45 -070088 ResourceTableBuilder& AddFileReference(const StringPiece& name,
Adam Lesinskicacb28f2016-10-19 12:18:14 -070089 const StringPiece& path) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070090 return AddFileReference(name, {}, path);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070091 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -070092
Adam Lesinskice5e56e2016-10-21 17:56:45 -070093 ResourceTableBuilder& AddFileReference(const StringPiece& name,
Adam Lesinskicacb28f2016-10-19 12:18:14 -070094 const ResourceId& id,
95 const StringPiece& path) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070096 return AddValue(name, id, util::make_unique<FileReference>(
97 table_->string_pool.MakeRef(path)));
Adam Lesinskicacb28f2016-10-19 12:18:14 -070098 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -070099
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700100 ResourceTableBuilder& AddFileReference(const StringPiece& name,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700101 const StringPiece& path,
102 const ConfigDescription& config) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700103 return AddValue(name, config, {}, util::make_unique<FileReference>(
104 table_->string_pool.MakeRef(path)));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700105 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700106
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700107 ResourceTableBuilder& AddValue(const StringPiece& name,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700108 std::unique_ptr<Value> value) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700109 return AddValue(name, {}, std::move(value));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700110 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700111
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700112 ResourceTableBuilder& AddValue(const StringPiece& name, const ResourceId& id,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700113 std::unique_ptr<Value> value) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700114 return AddValue(name, {}, id, std::move(value));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700115 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700116
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700117 ResourceTableBuilder& AddValue(const StringPiece& name,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700118 const ConfigDescription& config,
119 const ResourceId& id,
120 std::unique_ptr<Value> value) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700121 ResourceName res_name = ParseNameOrDie(name);
122 CHECK(table_->AddResourceAllowMangled(res_name, id, config, {},
123 std::move(value), &diagnostics_));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700124 return *this;
125 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700126
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700127 ResourceTableBuilder& SetSymbolState(const StringPiece& name,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700128 const ResourceId& id,
129 SymbolState state) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700130 ResourceName res_name = ParseNameOrDie(name);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700131 Symbol symbol;
132 symbol.state = state;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700133 CHECK(table_->SetSymbolStateAllowMangled(res_name, id, symbol,
134 &diagnostics_));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700135 return *this;
136 }
Adam Lesinskie78fd612015-10-22 12:48:43 -0700137
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700138 std::unique_ptr<ResourceTable> Build() { return std::move(table_); }
139
140 private:
141 DISALLOW_COPY_AND_ASSIGN(ResourceTableBuilder);
142
143 DummyDiagnosticsImpl diagnostics_;
144 std::unique_ptr<ResourceTable> table_ = util::make_unique<ResourceTable>();
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700145};
146
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700147inline std::unique_ptr<Reference> BuildReference(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700148 const StringPiece& ref, const Maybe<ResourceId>& id = {}) {
149 std::unique_ptr<Reference> reference =
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700150 util::make_unique<Reference>(ParseNameOrDie(ref));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700151 reference->id = id;
152 return reference;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700153}
154
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700155inline std::unique_ptr<BinaryPrimitive> BuildPrimitive(uint8_t type,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700156 uint32_t data) {
157 android::Res_value value = {};
158 value.size = sizeof(value);
159 value.dataType = type;
160 value.data = data;
161 return util::make_unique<BinaryPrimitive>(value);
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800162}
163
Adam Lesinskie78fd612015-10-22 12:48:43 -0700164template <typename T>
165class ValueBuilder {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700166 public:
167 template <typename... Args>
168 explicit ValueBuilder(Args&&... args)
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700169 : value_(new T{std::forward<Args>(args)...}) {}
Adam Lesinskie78fd612015-10-22 12:48:43 -0700170
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700171 template <typename... Args>
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700172 ValueBuilder& SetSource(Args&&... args) {
173 value_->SetSource(Source{std::forward<Args>(args)...});
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700174 return *this;
175 }
Adam Lesinskie78fd612015-10-22 12:48:43 -0700176
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700177 ValueBuilder& SetComment(const StringPiece& str) {
178 value_->SetComment(str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700179 return *this;
180 }
Adam Lesinskie78fd612015-10-22 12:48:43 -0700181
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700182 std::unique_ptr<Value> Build() { return std::move(value_); }
183
184 private:
185 DISALLOW_COPY_AND_ASSIGN(ValueBuilder);
186
187 std::unique_ptr<Value> value_;
Adam Lesinskie78fd612015-10-22 12:48:43 -0700188};
189
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700190class AttributeBuilder {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700191 public:
192 explicit AttributeBuilder(bool weak = false)
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700193 : attr_(util::make_unique<Attribute>(weak)) {
194 attr_->type_mask = android::ResTable_map::TYPE_ANY;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700195 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700196
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700197 AttributeBuilder& SetTypeMask(uint32_t typeMask) {
198 attr_->type_mask = typeMask;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700199 return *this;
200 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700201
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700202 AttributeBuilder& AddItem(const StringPiece& name, uint32_t value) {
203 attr_->symbols.push_back(Attribute::Symbol{
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700204 Reference(ResourceName({}, ResourceType::kId, name)), value});
205 return *this;
206 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700207
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700208 std::unique_ptr<Attribute> Build() { return std::move(attr_); }
209
210 private:
211 DISALLOW_COPY_AND_ASSIGN(AttributeBuilder);
212
213 std::unique_ptr<Attribute> attr_;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700214};
215
216class StyleBuilder {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700217 public:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700218 StyleBuilder() = default;
219
220 StyleBuilder& SetParent(const StringPiece& str) {
221 style_->parent = Reference(ParseNameOrDie(str));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700222 return *this;
223 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700224
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700225 StyleBuilder& AddItem(const StringPiece& str, std::unique_ptr<Item> value) {
226 style_->entries.push_back(
227 Style::Entry{Reference(ParseNameOrDie(str)), std::move(value)});
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700228 return *this;
229 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700230
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700231 StyleBuilder& AddItem(const StringPiece& str, const ResourceId& id,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700232 std::unique_ptr<Item> value) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700233 AddItem(str, std::move(value));
234 style_->entries.back().key.id = id;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700235 return *this;
236 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700237
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700238 std::unique_ptr<Style> Build() { return std::move(style_); }
239
240 private:
241 DISALLOW_COPY_AND_ASSIGN(StyleBuilder);
242
243 std::unique_ptr<Style> style_ = util::make_unique<Style>();
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700244};
245
246class StyleableBuilder {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700247 public:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700248 StyleableBuilder() = default;
249
250 StyleableBuilder& AddItem(const StringPiece& str,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700251 const Maybe<ResourceId>& id = {}) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700252 styleable_->entries.push_back(Reference(ParseNameOrDie(str)));
253 styleable_->entries.back().id = id;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700254 return *this;
255 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700256
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700257 std::unique_ptr<Styleable> Build() { return std::move(styleable_); }
258
259 private:
260 DISALLOW_COPY_AND_ASSIGN(StyleableBuilder);
261
262 std::unique_ptr<Styleable> styleable_ = util::make_unique<Styleable>();
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700263};
264
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700265inline std::unique_ptr<xml::XmlResource> BuildXmlDom(const StringPiece& str) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700266 std::stringstream in;
267 in << "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" << str;
268 StdErrDiagnostics diag;
269 std::unique_ptr<xml::XmlResource> doc =
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700270 xml::Inflate(&in, &diag, Source("test.xml"));
271 CHECK(doc != nullptr) << "failed to parse inline XML string";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700272 return doc;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700273}
274
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700275inline std::unique_ptr<xml::XmlResource> BuildXmlDomForPackageName(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700276 IAaptContext* context, const StringPiece& str) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700277 std::unique_ptr<xml::XmlResource> doc = BuildXmlDom(str);
278 doc->file.name.package = context->GetCompilationPackage();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700279 return doc;
Adam Lesinski467f1712015-11-16 17:35:44 -0800280}
281
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700282} // namespace test
283} // namespace aapt
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700284
285#endif /* AAPT_TEST_BUILDERS_H */