blob: 9ca694a7ef6151dd14a002ce1d257552eb2abc14 [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
39 ResourceTableBuilder& setPackageId(const StringPiece16& packageName, uint8_t id) {
40 ResourceTablePackage* package = mTable->createPackage(packageName, id);
41 assert(package);
42 return *this;
43 }
44
Adam Lesinskie78fd612015-10-22 12:48:43 -070045 ResourceTableBuilder& addSimple(const StringPiece16& name, const ResourceId id = {}) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -070046 return addValue(name, id, util::make_unique<Id>());
47 }
48
49 ResourceTableBuilder& addReference(const StringPiece16& name, const StringPiece16& ref) {
50 return addReference(name, {}, ref);
51 }
52
Adam Lesinskie78fd612015-10-22 12:48:43 -070053 ResourceTableBuilder& addReference(const StringPiece16& name, const ResourceId id,
Adam Lesinski1ab598f2015-08-14 14:26:04 -070054 const StringPiece16& ref) {
55 return addValue(name, id, util::make_unique<Reference>(parseNameOrDie(ref)));
56 }
57
58 ResourceTableBuilder& addString(const StringPiece16& name, const StringPiece16& str) {
59 return addString(name, {}, str);
60 }
61
Adam Lesinskie78fd612015-10-22 12:48:43 -070062 ResourceTableBuilder& addString(const StringPiece16& name, const ResourceId id,
Adam Lesinski1ab598f2015-08-14 14:26:04 -070063 const StringPiece16& str) {
64 return addValue(name, id, util::make_unique<String>(mTable->stringPool.makeRef(str)));
65 }
66
67 ResourceTableBuilder& addFileReference(const StringPiece16& name, const StringPiece16& path) {
68 return addFileReference(name, {}, path);
69 }
70
Adam Lesinskie78fd612015-10-22 12:48:43 -070071 ResourceTableBuilder& addFileReference(const StringPiece16& name, const ResourceId id,
Adam Lesinski1ab598f2015-08-14 14:26:04 -070072 const StringPiece16& path) {
73 return addValue(name, id,
74 util::make_unique<FileReference>(mTable->stringPool.makeRef(path)));
75 }
76
77
Adam Lesinskie78fd612015-10-22 12:48:43 -070078 ResourceTableBuilder& addValue(const StringPiece16& name,
79 std::unique_ptr<Value> value) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -070080 return addValue(name, {}, std::move(value));
81 }
82
Adam Lesinskie78fd612015-10-22 12:48:43 -070083 ResourceTableBuilder& addValue(const StringPiece16& name, const ResourceId id,
Adam Lesinski1ab598f2015-08-14 14:26:04 -070084 std::unique_ptr<Value> value) {
85 return addValue(name, id, {}, std::move(value));
86 }
87
Adam Lesinskie78fd612015-10-22 12:48:43 -070088 ResourceTableBuilder& addValue(const StringPiece16& name, const ResourceId id,
89 const ConfigDescription& config,
90 std::unique_ptr<Value> value) {
Adam Lesinski9e10ac72015-10-16 14:37:48 -070091 ResourceName resName = parseNameOrDie(name);
Adam Lesinskie78fd612015-10-22 12:48:43 -070092 bool result = mTable->addResourceAllowMangled(resName, id, config, std::move(value),
Adam Lesinski9e10ac72015-10-16 14:37:48 -070093 &mDiagnostics);
Adam Lesinski1ab598f2015-08-14 14:26:04 -070094 assert(result);
95 return *this;
96 }
97
Adam Lesinskie78fd612015-10-22 12:48:43 -070098 ResourceTableBuilder& setSymbolState(const StringPiece16& name, ResourceId id,
99 SymbolState state) {
100 ResourceName resName = parseNameOrDie(name);
101 Symbol symbol;
102 symbol.state = state;
103 bool result = mTable->setSymbolStateAllowMangled(resName, id, symbol, &mDiagnostics);
104 assert(result);
105 return *this;
106 }
107
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700108 std::unique_ptr<ResourceTable> build() {
109 return std::move(mTable);
110 }
111};
112
113inline std::unique_ptr<Reference> buildReference(const StringPiece16& ref,
114 Maybe<ResourceId> id = {}) {
115 std::unique_ptr<Reference> reference = util::make_unique<Reference>(parseNameOrDie(ref));
116 reference->id = id;
117 return reference;
118}
119
Adam Lesinskie78fd612015-10-22 12:48:43 -0700120template <typename T>
121class ValueBuilder {
122private:
123 std::unique_ptr<Value> mValue;
124
125public:
126 template <typename... Args>
127 ValueBuilder(Args&&... args) : mValue(new T{ std::forward<Args>(args)... }) {
128 }
129
130 template <typename... Args>
131 ValueBuilder& setSource(Args&&... args) {
132 mValue->setSource(Source{ std::forward<Args>(args)... });
133 return *this;
134 }
135
136 ValueBuilder& setComment(const StringPiece16& str) {
137 mValue->setComment(str);
138 return *this;
139 }
140
141 std::unique_ptr<Value> build() {
142 return std::move(mValue);
143 }
144};
145
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700146class AttributeBuilder {
147private:
148 std::unique_ptr<Attribute> mAttr;
149
150public:
151 AttributeBuilder(bool weak = false) : mAttr(util::make_unique<Attribute>(weak)) {
152 mAttr->typeMask = android::ResTable_map::TYPE_ANY;
153 }
154
155 AttributeBuilder& setTypeMask(uint32_t typeMask) {
156 mAttr->typeMask = typeMask;
157 return *this;
158 }
159
160 AttributeBuilder& addItem(const StringPiece16& name, uint32_t value) {
161 mAttr->symbols.push_back(Attribute::Symbol{
162 Reference(ResourceName{ {}, ResourceType::kId, name.toString()}),
163 value});
164 return *this;
165 }
166
167 std::unique_ptr<Attribute> build() {
168 return std::move(mAttr);
169 }
170};
171
172class StyleBuilder {
173private:
174 std::unique_ptr<Style> mStyle = util::make_unique<Style>();
175
176public:
177 StyleBuilder& setParent(const StringPiece16& str) {
178 mStyle->parent = Reference(parseNameOrDie(str));
179 return *this;
180 }
181
182 StyleBuilder& addItem(const StringPiece16& str, std::unique_ptr<Item> value) {
183 mStyle->entries.push_back(Style::Entry{ Reference(parseNameOrDie(str)), std::move(value) });
184 return *this;
185 }
186
187 StyleBuilder& addItem(const StringPiece16& str, ResourceId id, std::unique_ptr<Item> value) {
188 addItem(str, std::move(value));
189 mStyle->entries.back().key.id = id;
190 return *this;
191 }
192
193 std::unique_ptr<Style> build() {
194 return std::move(mStyle);
195 }
196};
197
198class StyleableBuilder {
199private:
200 std::unique_ptr<Styleable> mStyleable = util::make_unique<Styleable>();
201
202public:
203 StyleableBuilder& addItem(const StringPiece16& str, Maybe<ResourceId> id = {}) {
204 mStyleable->entries.push_back(Reference(parseNameOrDie(str)));
205 mStyleable->entries.back().id = id;
206 return *this;
207 }
208
209 std::unique_ptr<Styleable> build() {
210 return std::move(mStyleable);
211 }
212};
213
Adam Lesinski467f1712015-11-16 17:35:44 -0800214inline std::unique_ptr<xml::XmlResource> buildXmlDom(const StringPiece& str) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700215 std::stringstream in;
216 in << "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" << str;
217 StdErrDiagnostics diag;
Adam Lesinski467f1712015-11-16 17:35:44 -0800218 std::unique_ptr<xml::XmlResource> doc = xml::inflate(&in, &diag, {});
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700219 assert(doc);
220 return doc;
221}
222
Adam Lesinski467f1712015-11-16 17:35:44 -0800223inline std::unique_ptr<xml::XmlResource> buildXmlDomForPackageName(IAaptContext* context,
224 const StringPiece& str) {
225 std::unique_ptr<xml::XmlResource> doc = buildXmlDom(str);
226 doc->file.name.package = context->getCompilationPackage().toString();
227 return doc;
228}
229
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700230} // namespace test
231} // namespace aapt
232
233#endif /* AAPT_TEST_BUILDERS_H */