blob: f8e3d031fb671d9e4b7cfec13d5c02cbd0f87e43 [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
53 ResourceTableBuilder& addReference(const StringPiece16& name, const StringPiece16& ref) {
54 return addReference(name, {}, ref);
55 }
56
Adam Lesinskie78fd612015-10-22 12:48:43 -070057 ResourceTableBuilder& addReference(const StringPiece16& name, const ResourceId id,
Adam Lesinski1ab598f2015-08-14 14:26:04 -070058 const StringPiece16& ref) {
59 return addValue(name, id, util::make_unique<Reference>(parseNameOrDie(ref)));
60 }
61
62 ResourceTableBuilder& addString(const StringPiece16& name, const StringPiece16& str) {
63 return addString(name, {}, str);
64 }
65
Adam Lesinskie78fd612015-10-22 12:48:43 -070066 ResourceTableBuilder& addString(const StringPiece16& name, const ResourceId id,
Adam Lesinski1ab598f2015-08-14 14:26:04 -070067 const StringPiece16& str) {
68 return addValue(name, id, util::make_unique<String>(mTable->stringPool.makeRef(str)));
69 }
70
71 ResourceTableBuilder& addFileReference(const StringPiece16& name, const StringPiece16& path) {
72 return addFileReference(name, {}, path);
73 }
74
Adam Lesinskie78fd612015-10-22 12:48:43 -070075 ResourceTableBuilder& addFileReference(const StringPiece16& name, const ResourceId id,
Adam Lesinski1ab598f2015-08-14 14:26:04 -070076 const StringPiece16& path) {
77 return addValue(name, id,
78 util::make_unique<FileReference>(mTable->stringPool.makeRef(path)));
79 }
80
81
Adam Lesinskie78fd612015-10-22 12:48:43 -070082 ResourceTableBuilder& addValue(const StringPiece16& name,
83 std::unique_ptr<Value> value) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -070084 return addValue(name, {}, std::move(value));
85 }
86
Adam Lesinskie78fd612015-10-22 12:48:43 -070087 ResourceTableBuilder& addValue(const StringPiece16& name, const ResourceId id,
Adam Lesinski1ab598f2015-08-14 14:26:04 -070088 std::unique_ptr<Value> value) {
89 return addValue(name, id, {}, std::move(value));
90 }
91
Adam Lesinskie78fd612015-10-22 12:48:43 -070092 ResourceTableBuilder& addValue(const StringPiece16& name, const ResourceId id,
93 const ConfigDescription& config,
94 std::unique_ptr<Value> value) {
Adam Lesinski9e10ac72015-10-16 14:37:48 -070095 ResourceName resName = parseNameOrDie(name);
Adam Lesinskie78fd612015-10-22 12:48:43 -070096 bool result = mTable->addResourceAllowMangled(resName, id, config, std::move(value),
Adam Lesinski9e10ac72015-10-16 14:37:48 -070097 &mDiagnostics);
Adam Lesinski1ab598f2015-08-14 14:26:04 -070098 assert(result);
99 return *this;
100 }
101
Adam Lesinskie78fd612015-10-22 12:48:43 -0700102 ResourceTableBuilder& setSymbolState(const StringPiece16& name, ResourceId id,
103 SymbolState state) {
104 ResourceName resName = parseNameOrDie(name);
105 Symbol symbol;
106 symbol.state = state;
107 bool result = mTable->setSymbolStateAllowMangled(resName, id, symbol, &mDiagnostics);
108 assert(result);
109 return *this;
110 }
111
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700112 std::unique_ptr<ResourceTable> build() {
113 return std::move(mTable);
114 }
115};
116
117inline std::unique_ptr<Reference> buildReference(const StringPiece16& ref,
118 Maybe<ResourceId> id = {}) {
119 std::unique_ptr<Reference> reference = util::make_unique<Reference>(parseNameOrDie(ref));
120 reference->id = id;
121 return reference;
122}
123
Adam Lesinskie78fd612015-10-22 12:48:43 -0700124template <typename T>
125class ValueBuilder {
126private:
127 std::unique_ptr<Value> mValue;
128
129public:
130 template <typename... Args>
131 ValueBuilder(Args&&... args) : mValue(new T{ std::forward<Args>(args)... }) {
132 }
133
134 template <typename... Args>
135 ValueBuilder& setSource(Args&&... args) {
136 mValue->setSource(Source{ std::forward<Args>(args)... });
137 return *this;
138 }
139
140 ValueBuilder& setComment(const StringPiece16& str) {
141 mValue->setComment(str);
142 return *this;
143 }
144
145 std::unique_ptr<Value> build() {
146 return std::move(mValue);
147 }
148};
149
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700150class AttributeBuilder {
151private:
152 std::unique_ptr<Attribute> mAttr;
153
154public:
155 AttributeBuilder(bool weak = false) : mAttr(util::make_unique<Attribute>(weak)) {
156 mAttr->typeMask = android::ResTable_map::TYPE_ANY;
157 }
158
159 AttributeBuilder& setTypeMask(uint32_t typeMask) {
160 mAttr->typeMask = typeMask;
161 return *this;
162 }
163
164 AttributeBuilder& addItem(const StringPiece16& name, uint32_t value) {
165 mAttr->symbols.push_back(Attribute::Symbol{
166 Reference(ResourceName{ {}, ResourceType::kId, name.toString()}),
167 value});
168 return *this;
169 }
170
171 std::unique_ptr<Attribute> build() {
172 return std::move(mAttr);
173 }
174};
175
176class StyleBuilder {
177private:
178 std::unique_ptr<Style> mStyle = util::make_unique<Style>();
179
180public:
181 StyleBuilder& setParent(const StringPiece16& str) {
182 mStyle->parent = Reference(parseNameOrDie(str));
183 return *this;
184 }
185
186 StyleBuilder& addItem(const StringPiece16& str, std::unique_ptr<Item> value) {
187 mStyle->entries.push_back(Style::Entry{ Reference(parseNameOrDie(str)), std::move(value) });
188 return *this;
189 }
190
191 StyleBuilder& addItem(const StringPiece16& str, ResourceId id, std::unique_ptr<Item> value) {
192 addItem(str, std::move(value));
193 mStyle->entries.back().key.id = id;
194 return *this;
195 }
196
197 std::unique_ptr<Style> build() {
198 return std::move(mStyle);
199 }
200};
201
202class StyleableBuilder {
203private:
204 std::unique_ptr<Styleable> mStyleable = util::make_unique<Styleable>();
205
206public:
207 StyleableBuilder& addItem(const StringPiece16& str, Maybe<ResourceId> id = {}) {
208 mStyleable->entries.push_back(Reference(parseNameOrDie(str)));
209 mStyleable->entries.back().id = id;
210 return *this;
211 }
212
213 std::unique_ptr<Styleable> build() {
214 return std::move(mStyleable);
215 }
216};
217
Adam Lesinski467f1712015-11-16 17:35:44 -0800218inline std::unique_ptr<xml::XmlResource> buildXmlDom(const StringPiece& str) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700219 std::stringstream in;
220 in << "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" << str;
221 StdErrDiagnostics diag;
Adam Lesinski467f1712015-11-16 17:35:44 -0800222 std::unique_ptr<xml::XmlResource> doc = xml::inflate(&in, &diag, {});
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700223 assert(doc);
224 return doc;
225}
226
Adam Lesinski467f1712015-11-16 17:35:44 -0800227inline std::unique_ptr<xml::XmlResource> buildXmlDomForPackageName(IAaptContext* context,
228 const StringPiece& str) {
229 std::unique_ptr<xml::XmlResource> doc = buildXmlDom(str);
230 doc->file.name.package = context->getCompilationPackage().toString();
231 return doc;
232}
233
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700234} // namespace test
235} // namespace aapt
236
237#endif /* AAPT_TEST_BUILDERS_H */