blob: fb1d8f864fef6308b4309ce52bfe5c0cd0613f97 [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
20#include "ResourceTable.h"
21#include "ResourceValues.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070022#include "test/Common.h"
Adam Lesinski467f1712015-11-16 17:35:44 -080023#include "util/Util.h"
24#include "xml/XmlDom.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070025
26#include <memory>
27
28namespace aapt {
29namespace test {
30
31class ResourceTableBuilder {
32private:
33 DummyDiagnosticsImpl mDiagnostics;
34 std::unique_ptr<ResourceTable> mTable = util::make_unique<ResourceTable>();
35
36public:
37 ResourceTableBuilder() = default;
38
Adam Lesinskia5870652015-11-20 15:32:30 -080039 StringPool* getStringPool() {
40 return &mTable->stringPool;
41 }
42
Adam Lesinski1ab598f2015-08-14 14:26:04 -070043 ResourceTableBuilder& setPackageId(const StringPiece16& packageName, uint8_t id) {
44 ResourceTablePackage* package = mTable->createPackage(packageName, id);
45 assert(package);
46 return *this;
47 }
48
Adam Lesinskie78fd612015-10-22 12:48:43 -070049 ResourceTableBuilder& addSimple(const StringPiece16& name, const ResourceId id = {}) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -070050 return addValue(name, id, util::make_unique<Id>());
51 }
52
Adam Lesinskifb6312f2016-06-28 14:40:32 -070053 ResourceTableBuilder& addSimple(const StringPiece16& name, const ConfigDescription& config,
54 const ResourceId id = {}) {
55 return addValue(name, config, id, util::make_unique<Id>());
56 }
57
Adam Lesinski1ab598f2015-08-14 14:26:04 -070058 ResourceTableBuilder& addReference(const StringPiece16& name, const StringPiece16& ref) {
59 return addReference(name, {}, ref);
60 }
61
Adam Lesinskie78fd612015-10-22 12:48:43 -070062 ResourceTableBuilder& addReference(const StringPiece16& name, const ResourceId id,
Adam Lesinski1ab598f2015-08-14 14:26:04 -070063 const StringPiece16& ref) {
64 return addValue(name, id, util::make_unique<Reference>(parseNameOrDie(ref)));
65 }
66
67 ResourceTableBuilder& addString(const StringPiece16& name, const StringPiece16& str) {
68 return addString(name, {}, str);
69 }
70
Adam Lesinskie78fd612015-10-22 12:48:43 -070071 ResourceTableBuilder& addString(const StringPiece16& name, const ResourceId id,
Adam Lesinski1ab598f2015-08-14 14:26:04 -070072 const StringPiece16& str) {
73 return addValue(name, id, util::make_unique<String>(mTable->stringPool.makeRef(str)));
74 }
75
Adam Lesinski393b5f02015-12-17 13:03:11 -080076 ResourceTableBuilder& addString(const StringPiece16& name, const ResourceId id,
77 const ConfigDescription& config, const StringPiece16& str) {
Adam Lesinskifb6312f2016-06-28 14:40:32 -070078 return addValue(name, config, id,
Adam Lesinski393b5f02015-12-17 13:03:11 -080079 util::make_unique<String>(mTable->stringPool.makeRef(str)));
80 }
81
Adam Lesinski1ab598f2015-08-14 14:26:04 -070082 ResourceTableBuilder& addFileReference(const StringPiece16& name, const StringPiece16& path) {
83 return addFileReference(name, {}, path);
84 }
85
Adam Lesinskie78fd612015-10-22 12:48:43 -070086 ResourceTableBuilder& addFileReference(const StringPiece16& name, const ResourceId id,
Adam Lesinski1ab598f2015-08-14 14:26:04 -070087 const StringPiece16& path) {
88 return addValue(name, id,
89 util::make_unique<FileReference>(mTable->stringPool.makeRef(path)));
90 }
91
Adam Lesinski6a008172016-02-02 17:02:58 -080092 ResourceTableBuilder& addFileReference(const StringPiece16& name, const StringPiece16& path,
93 const ConfigDescription& config) {
Adam Lesinskifb6312f2016-06-28 14:40:32 -070094 return addValue(name, config, {},
Adam Lesinski6a008172016-02-02 17:02:58 -080095 util::make_unique<FileReference>(mTable->stringPool.makeRef(path)));
96 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -070097
Adam Lesinskie78fd612015-10-22 12:48:43 -070098 ResourceTableBuilder& addValue(const StringPiece16& name,
99 std::unique_ptr<Value> value) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700100 return addValue(name, {}, std::move(value));
101 }
102
Adam Lesinskie78fd612015-10-22 12:48:43 -0700103 ResourceTableBuilder& addValue(const StringPiece16& name, const ResourceId id,
Adam Lesinskifb6312f2016-06-28 14:40:32 -0700104 std::unique_ptr<Value> value) {
105 return addValue(name, {}, id, std::move(value));
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700106 }
107
Adam Lesinskifb6312f2016-06-28 14:40:32 -0700108 ResourceTableBuilder& addValue(const StringPiece16& name, const ConfigDescription& config,
109 const ResourceId id, std::unique_ptr<Value> value) {
Adam Lesinski9e10ac72015-10-16 14:37:48 -0700110 ResourceName resName = parseNameOrDie(name);
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800111 bool result = mTable->addResourceAllowMangled(resName, id, config, std::string(),
112 std::move(value), &mDiagnostics);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700113 assert(result);
114 return *this;
115 }
116
Adam Lesinskie78fd612015-10-22 12:48:43 -0700117 ResourceTableBuilder& setSymbolState(const StringPiece16& name, ResourceId id,
118 SymbolState state) {
119 ResourceName resName = parseNameOrDie(name);
120 Symbol symbol;
121 symbol.state = state;
122 bool result = mTable->setSymbolStateAllowMangled(resName, id, symbol, &mDiagnostics);
123 assert(result);
124 return *this;
125 }
126
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700127 std::unique_ptr<ResourceTable> build() {
128 return std::move(mTable);
129 }
130};
131
132inline std::unique_ptr<Reference> buildReference(const StringPiece16& ref,
133 Maybe<ResourceId> id = {}) {
134 std::unique_ptr<Reference> reference = util::make_unique<Reference>(parseNameOrDie(ref));
135 reference->id = id;
136 return reference;
137}
138
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800139inline std::unique_ptr<BinaryPrimitive> buildPrimitive(uint8_t type, uint32_t data) {
140 android::Res_value value = {};
141 value.size = sizeof(value);
142 value.dataType = type;
143 value.data = data;
144 return util::make_unique<BinaryPrimitive>(value);
145}
146
Adam Lesinskie78fd612015-10-22 12:48:43 -0700147template <typename T>
148class ValueBuilder {
149private:
150 std::unique_ptr<Value> mValue;
151
152public:
153 template <typename... Args>
154 ValueBuilder(Args&&... args) : mValue(new T{ std::forward<Args>(args)... }) {
155 }
156
157 template <typename... Args>
158 ValueBuilder& setSource(Args&&... args) {
159 mValue->setSource(Source{ std::forward<Args>(args)... });
160 return *this;
161 }
162
163 ValueBuilder& setComment(const StringPiece16& str) {
164 mValue->setComment(str);
165 return *this;
166 }
167
168 std::unique_ptr<Value> build() {
169 return std::move(mValue);
170 }
171};
172
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700173class AttributeBuilder {
174private:
175 std::unique_ptr<Attribute> mAttr;
176
177public:
178 AttributeBuilder(bool weak = false) : mAttr(util::make_unique<Attribute>(weak)) {
179 mAttr->typeMask = android::ResTable_map::TYPE_ANY;
180 }
181
182 AttributeBuilder& setTypeMask(uint32_t typeMask) {
183 mAttr->typeMask = typeMask;
184 return *this;
185 }
186
187 AttributeBuilder& addItem(const StringPiece16& name, uint32_t value) {
188 mAttr->symbols.push_back(Attribute::Symbol{
189 Reference(ResourceName{ {}, ResourceType::kId, name.toString()}),
190 value});
191 return *this;
192 }
193
194 std::unique_ptr<Attribute> build() {
195 return std::move(mAttr);
196 }
197};
198
199class StyleBuilder {
200private:
201 std::unique_ptr<Style> mStyle = util::make_unique<Style>();
202
203public:
204 StyleBuilder& setParent(const StringPiece16& str) {
205 mStyle->parent = Reference(parseNameOrDie(str));
206 return *this;
207 }
208
209 StyleBuilder& addItem(const StringPiece16& str, std::unique_ptr<Item> value) {
210 mStyle->entries.push_back(Style::Entry{ Reference(parseNameOrDie(str)), std::move(value) });
211 return *this;
212 }
213
214 StyleBuilder& addItem(const StringPiece16& str, ResourceId id, std::unique_ptr<Item> value) {
215 addItem(str, std::move(value));
216 mStyle->entries.back().key.id = id;
217 return *this;
218 }
219
220 std::unique_ptr<Style> build() {
221 return std::move(mStyle);
222 }
223};
224
225class StyleableBuilder {
226private:
227 std::unique_ptr<Styleable> mStyleable = util::make_unique<Styleable>();
228
229public:
230 StyleableBuilder& addItem(const StringPiece16& str, Maybe<ResourceId> id = {}) {
231 mStyleable->entries.push_back(Reference(parseNameOrDie(str)));
232 mStyleable->entries.back().id = id;
233 return *this;
234 }
235
236 std::unique_ptr<Styleable> build() {
237 return std::move(mStyleable);
238 }
239};
240
Adam Lesinski467f1712015-11-16 17:35:44 -0800241inline std::unique_ptr<xml::XmlResource> buildXmlDom(const StringPiece& str) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700242 std::stringstream in;
243 in << "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" << str;
244 StdErrDiagnostics diag;
Adam Lesinskicc5609d2016-04-05 12:41:07 -0700245 std::unique_ptr<xml::XmlResource> doc = xml::inflate(&in, &diag, Source("test.xml"));
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700246 assert(doc);
247 return doc;
248}
249
Adam Lesinski467f1712015-11-16 17:35:44 -0800250inline std::unique_ptr<xml::XmlResource> buildXmlDomForPackageName(IAaptContext* context,
251 const StringPiece& str) {
252 std::unique_ptr<xml::XmlResource> doc = buildXmlDom(str);
Adam Lesinski64587af2016-02-18 18:33:06 -0800253 doc->file.name.package = context->getCompilationPackage();
Adam Lesinski467f1712015-11-16 17:35:44 -0800254 return doc;
255}
256
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700257} // namespace test
258} // namespace aapt
259
260#endif /* AAPT_TEST_BUILDERS_H */