blob: 637e991a573f151880d070cc84b3f243ea3863d6 [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 Lesinskid0f116b2016-07-08 15:00:32 -070043 ResourceTableBuilder& setPackageId(const StringPiece& packageName, uint8_t id) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -070044 ResourceTablePackage* package = mTable->createPackage(packageName, id);
45 assert(package);
46 return *this;
47 }
48
Chih-Hung Hsieh470f8fc2016-08-15 12:32:51 -070049 ResourceTableBuilder& addSimple(const StringPiece& name, const ResourceId& id = {}) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -070050 return addValue(name, id, util::make_unique<Id>());
51 }
52
Adam Lesinskid0f116b2016-07-08 15:00:32 -070053 ResourceTableBuilder& addSimple(const StringPiece& name, const ConfigDescription& config,
Chih-Hung Hsieh470f8fc2016-08-15 12:32:51 -070054 const ResourceId& id = {}) {
Adam Lesinskifb6312f2016-06-28 14:40:32 -070055 return addValue(name, config, id, util::make_unique<Id>());
56 }
57
Adam Lesinskid0f116b2016-07-08 15:00:32 -070058 ResourceTableBuilder& addReference(const StringPiece& name, const StringPiece& ref) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -070059 return addReference(name, {}, ref);
60 }
61
Chih-Hung Hsieh470f8fc2016-08-15 12:32:51 -070062 ResourceTableBuilder& addReference(const StringPiece& name, const ResourceId& id,
Adam Lesinskid0f116b2016-07-08 15:00:32 -070063 const StringPiece& ref) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -070064 return addValue(name, id, util::make_unique<Reference>(parseNameOrDie(ref)));
65 }
66
Adam Lesinskid0f116b2016-07-08 15:00:32 -070067 ResourceTableBuilder& addString(const StringPiece& name, const StringPiece& str) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -070068 return addString(name, {}, str);
69 }
70
Chih-Hung Hsieh470f8fc2016-08-15 12:32:51 -070071 ResourceTableBuilder& addString(const StringPiece& name, const ResourceId& id,
Adam Lesinskid0f116b2016-07-08 15:00:32 -070072 const StringPiece& str) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -070073 return addValue(name, id, util::make_unique<String>(mTable->stringPool.makeRef(str)));
74 }
75
Chih-Hung Hsieh470f8fc2016-08-15 12:32:51 -070076 ResourceTableBuilder& addString(const StringPiece& name, const ResourceId& id,
Adam Lesinskid0f116b2016-07-08 15:00:32 -070077 const ConfigDescription& config, const StringPiece& 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 Lesinskid0f116b2016-07-08 15:00:32 -070082 ResourceTableBuilder& addFileReference(const StringPiece& name, const StringPiece& path) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -070083 return addFileReference(name, {}, path);
84 }
85
Chih-Hung Hsieh470f8fc2016-08-15 12:32:51 -070086 ResourceTableBuilder& addFileReference(const StringPiece& name, const ResourceId& id,
Adam Lesinskid0f116b2016-07-08 15:00:32 -070087 const StringPiece& path) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -070088 return addValue(name, id,
89 util::make_unique<FileReference>(mTable->stringPool.makeRef(path)));
90 }
91
Adam Lesinskid0f116b2016-07-08 15:00:32 -070092 ResourceTableBuilder& addFileReference(const StringPiece& name, const StringPiece& path,
Adam Lesinski6a008172016-02-02 17:02:58 -080093 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 Lesinskid0f116b2016-07-08 15:00:32 -070098 ResourceTableBuilder& addValue(const StringPiece& name,
Adam Lesinskie78fd612015-10-22 12:48:43 -070099 std::unique_ptr<Value> value) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700100 return addValue(name, {}, std::move(value));
101 }
102
Chih-Hung Hsieh470f8fc2016-08-15 12:32:51 -0700103 ResourceTableBuilder& addValue(const StringPiece& 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 Lesinskid0f116b2016-07-08 15:00:32 -0700108 ResourceTableBuilder& addValue(const StringPiece& name, const ConfigDescription& config,
Chih-Hung Hsieh470f8fc2016-08-15 12:32:51 -0700109 const ResourceId& id, std::unique_ptr<Value> value) {
Adam Lesinski9e10ac72015-10-16 14:37:48 -0700110 ResourceName resName = parseNameOrDie(name);
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700111 bool result = mTable->addResourceAllowMangled(resName, id, config, {},
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800112 std::move(value), &mDiagnostics);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700113 assert(result);
114 return *this;
115 }
116
Chih-Hung Hsieh470f8fc2016-08-15 12:32:51 -0700117 ResourceTableBuilder& setSymbolState(const StringPiece& name, const ResourceId& id,
Adam Lesinskie78fd612015-10-22 12:48:43 -0700118 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
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700132inline std::unique_ptr<Reference> buildReference(const StringPiece& ref,
Chih-Hung Hsieh470f8fc2016-08-15 12:32:51 -0700133 const Maybe<ResourceId>& id = {}) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700134 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>
Chih-Hung Hsieh470f8fc2016-08-15 12:32:51 -0700154 explicit ValueBuilder(Args&&... args) : mValue(new T{ std::forward<Args>(args)... }) {
Adam Lesinskie78fd612015-10-22 12:48:43 -0700155 }
156
157 template <typename... Args>
158 ValueBuilder& setSource(Args&&... args) {
159 mValue->setSource(Source{ std::forward<Args>(args)... });
160 return *this;
161 }
162
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700163 ValueBuilder& setComment(const StringPiece& str) {
Adam Lesinskie78fd612015-10-22 12:48:43 -0700164 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:
Chih-Hung Hsieh470f8fc2016-08-15 12:32:51 -0700178 explicit AttributeBuilder(bool weak = false) : mAttr(util::make_unique<Attribute>(weak)) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700179 mAttr->typeMask = android::ResTable_map::TYPE_ANY;
180 }
181
182 AttributeBuilder& setTypeMask(uint32_t typeMask) {
183 mAttr->typeMask = typeMask;
184 return *this;
185 }
186
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700187 AttributeBuilder& addItem(const StringPiece& name, uint32_t value) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700188 mAttr->symbols.push_back(Attribute::Symbol{
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700189 Reference(ResourceName({}, ResourceType::kId, name)),
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700190 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:
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700204 StyleBuilder& setParent(const StringPiece& str) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700205 mStyle->parent = Reference(parseNameOrDie(str));
206 return *this;
207 }
208
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700209 StyleBuilder& addItem(const StringPiece& str, std::unique_ptr<Item> value) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700210 mStyle->entries.push_back(Style::Entry{ Reference(parseNameOrDie(str)), std::move(value) });
211 return *this;
212 }
213
Chih-Hung Hsieh470f8fc2016-08-15 12:32:51 -0700214 StyleBuilder& addItem(const StringPiece& str, const ResourceId& id, std::unique_ptr<Item> value) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700215 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:
Chih-Hung Hsieh470f8fc2016-08-15 12:32:51 -0700230 StyleableBuilder& addItem(const StringPiece& str, const Maybe<ResourceId>& id = {}) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700231 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 */