blob: 0383c443d4c10aefdce5a3da8949786c909ecc1d [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"
22#include "util/Util.h"
23#include "XmlDom.h"
24
25#include "test/Common.h"
26
27#include <memory>
28
29namespace aapt {
30namespace test {
31
32class ResourceTableBuilder {
33private:
34 DummyDiagnosticsImpl mDiagnostics;
35 std::unique_ptr<ResourceTable> mTable = util::make_unique<ResourceTable>();
36
37public:
38 ResourceTableBuilder() = default;
39
40 ResourceTableBuilder& setPackageId(const StringPiece16& packageName, uint8_t id) {
41 ResourceTablePackage* package = mTable->createPackage(packageName, id);
42 assert(package);
43 return *this;
44 }
45
46 ResourceTableBuilder& addSimple(const StringPiece16& name, ResourceId id = {}) {
47 return addValue(name, id, util::make_unique<Id>());
48 }
49
50 ResourceTableBuilder& addReference(const StringPiece16& name, const StringPiece16& ref) {
51 return addReference(name, {}, ref);
52 }
53
54 ResourceTableBuilder& addReference(const StringPiece16& name, ResourceId id,
55 const StringPiece16& ref) {
56 return addValue(name, id, util::make_unique<Reference>(parseNameOrDie(ref)));
57 }
58
59 ResourceTableBuilder& addString(const StringPiece16& name, const StringPiece16& str) {
60 return addString(name, {}, str);
61 }
62
63 ResourceTableBuilder& addString(const StringPiece16& name, ResourceId id,
64 const StringPiece16& str) {
65 return addValue(name, id, util::make_unique<String>(mTable->stringPool.makeRef(str)));
66 }
67
68 ResourceTableBuilder& addFileReference(const StringPiece16& name, const StringPiece16& path) {
69 return addFileReference(name, {}, path);
70 }
71
72 ResourceTableBuilder& addFileReference(const StringPiece16& name, ResourceId id,
73 const StringPiece16& path) {
74 return addValue(name, id,
75 util::make_unique<FileReference>(mTable->stringPool.makeRef(path)));
76 }
77
78
79 ResourceTableBuilder& addValue(const StringPiece16& name, std::unique_ptr<Value> value) {
80 return addValue(name, {}, std::move(value));
81 }
82
83 ResourceTableBuilder& addValue(const StringPiece16& name, ResourceId id,
84 std::unique_ptr<Value> value) {
85 return addValue(name, id, {}, std::move(value));
86 }
87
88 ResourceTableBuilder& addValue(const StringPiece16& name, ResourceId id,
89 const ConfigDescription& config, std::unique_ptr<Value> value) {
Adam Lesinski9e10ac72015-10-16 14:37:48 -070090 ResourceName resName = parseNameOrDie(name);
91 bool result = mTable->addResourceAllowMangled(resName, id, config, {}, std::move(value),
92 &mDiagnostics);
Adam Lesinski1ab598f2015-08-14 14:26:04 -070093 assert(result);
94 return *this;
95 }
96
97 std::unique_ptr<ResourceTable> build() {
98 return std::move(mTable);
99 }
100};
101
102inline std::unique_ptr<Reference> buildReference(const StringPiece16& ref,
103 Maybe<ResourceId> id = {}) {
104 std::unique_ptr<Reference> reference = util::make_unique<Reference>(parseNameOrDie(ref));
105 reference->id = id;
106 return reference;
107}
108
109class AttributeBuilder {
110private:
111 std::unique_ptr<Attribute> mAttr;
112
113public:
114 AttributeBuilder(bool weak = false) : mAttr(util::make_unique<Attribute>(weak)) {
115 mAttr->typeMask = android::ResTable_map::TYPE_ANY;
116 }
117
118 AttributeBuilder& setTypeMask(uint32_t typeMask) {
119 mAttr->typeMask = typeMask;
120 return *this;
121 }
122
123 AttributeBuilder& addItem(const StringPiece16& name, uint32_t value) {
124 mAttr->symbols.push_back(Attribute::Symbol{
125 Reference(ResourceName{ {}, ResourceType::kId, name.toString()}),
126 value});
127 return *this;
128 }
129
130 std::unique_ptr<Attribute> build() {
131 return std::move(mAttr);
132 }
133};
134
135class StyleBuilder {
136private:
137 std::unique_ptr<Style> mStyle = util::make_unique<Style>();
138
139public:
140 StyleBuilder& setParent(const StringPiece16& str) {
141 mStyle->parent = Reference(parseNameOrDie(str));
142 return *this;
143 }
144
145 StyleBuilder& addItem(const StringPiece16& str, std::unique_ptr<Item> value) {
146 mStyle->entries.push_back(Style::Entry{ Reference(parseNameOrDie(str)), std::move(value) });
147 return *this;
148 }
149
150 StyleBuilder& addItem(const StringPiece16& str, ResourceId id, std::unique_ptr<Item> value) {
151 addItem(str, std::move(value));
152 mStyle->entries.back().key.id = id;
153 return *this;
154 }
155
156 std::unique_ptr<Style> build() {
157 return std::move(mStyle);
158 }
159};
160
161class StyleableBuilder {
162private:
163 std::unique_ptr<Styleable> mStyleable = util::make_unique<Styleable>();
164
165public:
166 StyleableBuilder& addItem(const StringPiece16& str, Maybe<ResourceId> id = {}) {
167 mStyleable->entries.push_back(Reference(parseNameOrDie(str)));
168 mStyleable->entries.back().id = id;
169 return *this;
170 }
171
172 std::unique_ptr<Styleable> build() {
173 return std::move(mStyleable);
174 }
175};
176
177inline std::unique_ptr<XmlResource> buildXmlDom(const StringPiece& str) {
178 std::stringstream in;
179 in << "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" << str;
180 StdErrDiagnostics diag;
181 std::unique_ptr<XmlResource> doc = xml::inflate(&in, &diag, {});
182 assert(doc);
183 return doc;
184}
185
186} // namespace test
187} // namespace aapt
188
189#endif /* AAPT_TEST_BUILDERS_H */