blob: 93a11b9334a89a37d7a467c3657ede51344b2a5e [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
Adam Lesinski393b5f02015-12-17 13:03:11 -080071 ResourceTableBuilder& addString(const StringPiece16& name, const ResourceId id,
72 const ConfigDescription& config, const StringPiece16& str) {
73 return addValue(name, id, config,
74 util::make_unique<String>(mTable->stringPool.makeRef(str)));
75 }
76
Adam Lesinski1ab598f2015-08-14 14:26:04 -070077 ResourceTableBuilder& addFileReference(const StringPiece16& name, const StringPiece16& path) {
78 return addFileReference(name, {}, path);
79 }
80
Adam Lesinskie78fd612015-10-22 12:48:43 -070081 ResourceTableBuilder& addFileReference(const StringPiece16& name, const ResourceId id,
Adam Lesinski1ab598f2015-08-14 14:26:04 -070082 const StringPiece16& path) {
83 return addValue(name, id,
84 util::make_unique<FileReference>(mTable->stringPool.makeRef(path)));
85 }
86
87
Adam Lesinskie78fd612015-10-22 12:48:43 -070088 ResourceTableBuilder& addValue(const StringPiece16& name,
89 std::unique_ptr<Value> value) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -070090 return addValue(name, {}, std::move(value));
91 }
92
Adam Lesinskie78fd612015-10-22 12:48:43 -070093 ResourceTableBuilder& addValue(const StringPiece16& name, const ResourceId id,
Adam Lesinski1ab598f2015-08-14 14:26:04 -070094 std::unique_ptr<Value> value) {
95 return addValue(name, id, {}, std::move(value));
96 }
97
Adam Lesinskie78fd612015-10-22 12:48:43 -070098 ResourceTableBuilder& addValue(const StringPiece16& name, const ResourceId id,
99 const ConfigDescription& config,
100 std::unique_ptr<Value> value) {
Adam Lesinski9e10ac72015-10-16 14:37:48 -0700101 ResourceName resName = parseNameOrDie(name);
Adam Lesinskie78fd612015-10-22 12:48:43 -0700102 bool result = mTable->addResourceAllowMangled(resName, id, config, std::move(value),
Adam Lesinski9e10ac72015-10-16 14:37:48 -0700103 &mDiagnostics);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700104 assert(result);
105 return *this;
106 }
107
Adam Lesinskie78fd612015-10-22 12:48:43 -0700108 ResourceTableBuilder& setSymbolState(const StringPiece16& name, ResourceId id,
109 SymbolState state) {
110 ResourceName resName = parseNameOrDie(name);
111 Symbol symbol;
112 symbol.state = state;
113 bool result = mTable->setSymbolStateAllowMangled(resName, id, symbol, &mDiagnostics);
114 assert(result);
115 return *this;
116 }
117
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700118 std::unique_ptr<ResourceTable> build() {
119 return std::move(mTable);
120 }
121};
122
123inline std::unique_ptr<Reference> buildReference(const StringPiece16& ref,
124 Maybe<ResourceId> id = {}) {
125 std::unique_ptr<Reference> reference = util::make_unique<Reference>(parseNameOrDie(ref));
126 reference->id = id;
127 return reference;
128}
129
Adam Lesinskie78fd612015-10-22 12:48:43 -0700130template <typename T>
131class ValueBuilder {
132private:
133 std::unique_ptr<Value> mValue;
134
135public:
136 template <typename... Args>
137 ValueBuilder(Args&&... args) : mValue(new T{ std::forward<Args>(args)... }) {
138 }
139
140 template <typename... Args>
141 ValueBuilder& setSource(Args&&... args) {
142 mValue->setSource(Source{ std::forward<Args>(args)... });
143 return *this;
144 }
145
146 ValueBuilder& setComment(const StringPiece16& str) {
147 mValue->setComment(str);
148 return *this;
149 }
150
151 std::unique_ptr<Value> build() {
152 return std::move(mValue);
153 }
154};
155
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700156class AttributeBuilder {
157private:
158 std::unique_ptr<Attribute> mAttr;
159
160public:
161 AttributeBuilder(bool weak = false) : mAttr(util::make_unique<Attribute>(weak)) {
162 mAttr->typeMask = android::ResTable_map::TYPE_ANY;
163 }
164
165 AttributeBuilder& setTypeMask(uint32_t typeMask) {
166 mAttr->typeMask = typeMask;
167 return *this;
168 }
169
170 AttributeBuilder& addItem(const StringPiece16& name, uint32_t value) {
171 mAttr->symbols.push_back(Attribute::Symbol{
172 Reference(ResourceName{ {}, ResourceType::kId, name.toString()}),
173 value});
174 return *this;
175 }
176
177 std::unique_ptr<Attribute> build() {
178 return std::move(mAttr);
179 }
180};
181
182class StyleBuilder {
183private:
184 std::unique_ptr<Style> mStyle = util::make_unique<Style>();
185
186public:
187 StyleBuilder& setParent(const StringPiece16& str) {
188 mStyle->parent = Reference(parseNameOrDie(str));
189 return *this;
190 }
191
192 StyleBuilder& addItem(const StringPiece16& str, std::unique_ptr<Item> value) {
193 mStyle->entries.push_back(Style::Entry{ Reference(parseNameOrDie(str)), std::move(value) });
194 return *this;
195 }
196
197 StyleBuilder& addItem(const StringPiece16& str, ResourceId id, std::unique_ptr<Item> value) {
198 addItem(str, std::move(value));
199 mStyle->entries.back().key.id = id;
200 return *this;
201 }
202
203 std::unique_ptr<Style> build() {
204 return std::move(mStyle);
205 }
206};
207
208class StyleableBuilder {
209private:
210 std::unique_ptr<Styleable> mStyleable = util::make_unique<Styleable>();
211
212public:
213 StyleableBuilder& addItem(const StringPiece16& str, Maybe<ResourceId> id = {}) {
214 mStyleable->entries.push_back(Reference(parseNameOrDie(str)));
215 mStyleable->entries.back().id = id;
216 return *this;
217 }
218
219 std::unique_ptr<Styleable> build() {
220 return std::move(mStyleable);
221 }
222};
223
Adam Lesinski467f1712015-11-16 17:35:44 -0800224inline std::unique_ptr<xml::XmlResource> buildXmlDom(const StringPiece& str) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700225 std::stringstream in;
226 in << "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" << str;
227 StdErrDiagnostics diag;
Adam Lesinski467f1712015-11-16 17:35:44 -0800228 std::unique_ptr<xml::XmlResource> doc = xml::inflate(&in, &diag, {});
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700229 assert(doc);
230 return doc;
231}
232
Adam Lesinski467f1712015-11-16 17:35:44 -0800233inline std::unique_ptr<xml::XmlResource> buildXmlDomForPackageName(IAaptContext* context,
234 const StringPiece& str) {
235 std::unique_ptr<xml::XmlResource> doc = buildXmlDom(str);
236 doc->file.name.package = context->getCompilationPackage().toString();
237 return doc;
238}
239
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700240} // namespace test
241} // namespace aapt
242
243#endif /* AAPT_TEST_BUILDERS_H */