blob: 0d8d8b5d49aa256ed04fd5f351b76d5308ae6ada [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) {
90 bool result = mTable->addResource(parseNameOrDie(name), id, config, {}, std::move(value),
91 &mDiagnostics);
92 assert(result);
93 return *this;
94 }
95
96 std::unique_ptr<ResourceTable> build() {
97 return std::move(mTable);
98 }
99};
100
101inline std::unique_ptr<Reference> buildReference(const StringPiece16& ref,
102 Maybe<ResourceId> id = {}) {
103 std::unique_ptr<Reference> reference = util::make_unique<Reference>(parseNameOrDie(ref));
104 reference->id = id;
105 return reference;
106}
107
108class AttributeBuilder {
109private:
110 std::unique_ptr<Attribute> mAttr;
111
112public:
113 AttributeBuilder(bool weak = false) : mAttr(util::make_unique<Attribute>(weak)) {
114 mAttr->typeMask = android::ResTable_map::TYPE_ANY;
115 }
116
117 AttributeBuilder& setTypeMask(uint32_t typeMask) {
118 mAttr->typeMask = typeMask;
119 return *this;
120 }
121
122 AttributeBuilder& addItem(const StringPiece16& name, uint32_t value) {
123 mAttr->symbols.push_back(Attribute::Symbol{
124 Reference(ResourceName{ {}, ResourceType::kId, name.toString()}),
125 value});
126 return *this;
127 }
128
129 std::unique_ptr<Attribute> build() {
130 return std::move(mAttr);
131 }
132};
133
134class StyleBuilder {
135private:
136 std::unique_ptr<Style> mStyle = util::make_unique<Style>();
137
138public:
139 StyleBuilder& setParent(const StringPiece16& str) {
140 mStyle->parent = Reference(parseNameOrDie(str));
141 return *this;
142 }
143
144 StyleBuilder& addItem(const StringPiece16& str, std::unique_ptr<Item> value) {
145 mStyle->entries.push_back(Style::Entry{ Reference(parseNameOrDie(str)), std::move(value) });
146 return *this;
147 }
148
149 StyleBuilder& addItem(const StringPiece16& str, ResourceId id, std::unique_ptr<Item> value) {
150 addItem(str, std::move(value));
151 mStyle->entries.back().key.id = id;
152 return *this;
153 }
154
155 std::unique_ptr<Style> build() {
156 return std::move(mStyle);
157 }
158};
159
160class StyleableBuilder {
161private:
162 std::unique_ptr<Styleable> mStyleable = util::make_unique<Styleable>();
163
164public:
165 StyleableBuilder& addItem(const StringPiece16& str, Maybe<ResourceId> id = {}) {
166 mStyleable->entries.push_back(Reference(parseNameOrDie(str)));
167 mStyleable->entries.back().id = id;
168 return *this;
169 }
170
171 std::unique_ptr<Styleable> build() {
172 return std::move(mStyleable);
173 }
174};
175
176inline std::unique_ptr<XmlResource> buildXmlDom(const StringPiece& str) {
177 std::stringstream in;
178 in << "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" << str;
179 StdErrDiagnostics diag;
180 std::unique_ptr<XmlResource> doc = xml::inflate(&in, &diag, {});
181 assert(doc);
182 return doc;
183}
184
185} // namespace test
186} // namespace aapt
187
188#endif /* AAPT_TEST_BUILDERS_H */