blob: a3e2f800931c65c7429eecf1db33c9b8eb6aaa52 [file] [log] [blame]
Adam Lesinskiefeb7af2017-08-02 14:57:43 -07001/*
2 * Copyright (C) 2017 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#include "test/Builders.h"
18
19#include "android-base/logging.h"
20#include "androidfw/StringPiece.h"
21
Adam Lesinski00451162017-10-03 07:44:08 -070022#include "io/StringStream.h"
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070023#include "test/Common.h"
24#include "util/Util.h"
25
Shane Farmerefe45392017-08-21 14:39:28 -070026using ::aapt::configuration::Abi;
27using ::aapt::configuration::AndroidSdk;
Shane Farmercb6c3f92017-11-27 13:19:36 -080028using ::aapt::configuration::ConfiguredArtifact;
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070029using ::aapt::io::StringInputStream;
30using ::android::StringPiece;
31
32namespace aapt {
33namespace test {
34
35ResourceTableBuilder& ResourceTableBuilder::SetPackageId(const StringPiece& package_name,
36 uint8_t id) {
37 ResourceTablePackage* package = table_->CreatePackage(package_name, id);
38 CHECK(package != nullptr);
39 return *this;
40}
41
42ResourceTableBuilder& ResourceTableBuilder::AddSimple(const StringPiece& name,
43 const ResourceId& id) {
44 return AddValue(name, id, util::make_unique<Id>());
45}
46
47ResourceTableBuilder& ResourceTableBuilder::AddSimple(const StringPiece& name,
48 const ConfigDescription& config,
49 const ResourceId& id) {
50 return AddValue(name, config, id, util::make_unique<Id>());
51}
52
53ResourceTableBuilder& ResourceTableBuilder::AddReference(const StringPiece& name,
54 const StringPiece& ref) {
55 return AddReference(name, {}, ref);
56}
57
58ResourceTableBuilder& ResourceTableBuilder::AddReference(const StringPiece& name,
59 const ResourceId& id,
60 const StringPiece& ref) {
61 return AddValue(name, id, util::make_unique<Reference>(ParseNameOrDie(ref)));
62}
63
64ResourceTableBuilder& ResourceTableBuilder::AddString(const StringPiece& name,
65 const StringPiece& str) {
66 return AddString(name, {}, str);
67}
68
69ResourceTableBuilder& ResourceTableBuilder::AddString(const StringPiece& name, const ResourceId& id,
70 const StringPiece& str) {
71 return AddValue(name, id, util::make_unique<String>(table_->string_pool.MakeRef(str)));
72}
73
74ResourceTableBuilder& ResourceTableBuilder::AddString(const StringPiece& name, const ResourceId& id,
75 const ConfigDescription& config,
76 const StringPiece& str) {
77 return AddValue(name, config, id, util::make_unique<String>(table_->string_pool.MakeRef(str)));
78}
79
80ResourceTableBuilder& ResourceTableBuilder::AddFileReference(const StringPiece& name,
Adam Lesinski8780eb62017-10-31 17:44:39 -070081 const StringPiece& path,
82 io::IFile* file) {
83 return AddFileReference(name, {}, path, file);
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070084}
85
86ResourceTableBuilder& ResourceTableBuilder::AddFileReference(const StringPiece& name,
87 const ResourceId& id,
Adam Lesinski8780eb62017-10-31 17:44:39 -070088 const StringPiece& path,
89 io::IFile* file) {
90 auto file_ref = util::make_unique<FileReference>(table_->string_pool.MakeRef(path));
91 file_ref->file = file;
92 return AddValue(name, id, std::move(file_ref));
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070093}
94
95ResourceTableBuilder& ResourceTableBuilder::AddFileReference(const StringPiece& name,
96 const StringPiece& path,
Adam Lesinski8780eb62017-10-31 17:44:39 -070097 const ConfigDescription& config,
98 io::IFile* file) {
99 auto file_ref = util::make_unique<FileReference>(table_->string_pool.MakeRef(path));
100 file_ref->file = file;
101 return AddValue(name, config, {}, std::move(file_ref));
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700102}
103
104ResourceTableBuilder& ResourceTableBuilder::AddValue(const StringPiece& name,
105 std::unique_ptr<Value> value) {
106 return AddValue(name, {}, std::move(value));
107}
108
109ResourceTableBuilder& ResourceTableBuilder::AddValue(const StringPiece& name, const ResourceId& id,
110 std::unique_ptr<Value> value) {
111 return AddValue(name, {}, id, std::move(value));
112}
113
114ResourceTableBuilder& ResourceTableBuilder::AddValue(const StringPiece& name,
115 const ConfigDescription& config,
116 const ResourceId& id,
117 std::unique_ptr<Value> value) {
118 ResourceName res_name = ParseNameOrDie(name);
Adam Lesinski71be7052017-12-12 16:48:07 -0800119 CHECK(table_->AddResourceWithIdMangled(res_name, id, config, {}, std::move(value),
120 GetDiagnostics()));
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700121 return *this;
122}
123
124ResourceTableBuilder& ResourceTableBuilder::SetSymbolState(const StringPiece& name,
Adam Lesinski71be7052017-12-12 16:48:07 -0800125 const ResourceId& id,
126 Visibility::Level level,
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700127 bool allow_new) {
128 ResourceName res_name = ParseNameOrDie(name);
Adam Lesinski71be7052017-12-12 16:48:07 -0800129 Visibility visibility;
130 visibility.level = level;
131 CHECK(table_->SetVisibilityWithIdMangled(res_name, visibility, id, GetDiagnostics()));
132 CHECK(table_->SetAllowNewMangled(res_name, AllowNew{}, GetDiagnostics()));
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700133 return *this;
134}
135
136StringPool* ResourceTableBuilder::string_pool() {
137 return &table_->string_pool;
138}
139
140std::unique_ptr<ResourceTable> ResourceTableBuilder::Build() {
141 return std::move(table_);
142}
143
144std::unique_ptr<Reference> BuildReference(const StringPiece& ref, const Maybe<ResourceId>& id) {
145 std::unique_ptr<Reference> reference = util::make_unique<Reference>(ParseNameOrDie(ref));
146 reference->id = id;
147 return reference;
148}
149
150std::unique_ptr<BinaryPrimitive> BuildPrimitive(uint8_t type, uint32_t data) {
151 android::Res_value value = {};
152 value.size = sizeof(value);
153 value.dataType = type;
154 value.data = data;
155 return util::make_unique<BinaryPrimitive>(value);
156}
157
158AttributeBuilder::AttributeBuilder(bool weak) : attr_(util::make_unique<Attribute>(weak)) {
159 attr_->type_mask = android::ResTable_map::TYPE_ANY;
160}
161
162AttributeBuilder& AttributeBuilder::SetTypeMask(uint32_t typeMask) {
163 attr_->type_mask = typeMask;
164 return *this;
165}
166
167AttributeBuilder& AttributeBuilder::AddItem(const StringPiece& name, uint32_t value) {
168 attr_->symbols.push_back(
169 Attribute::Symbol{Reference(ResourceName({}, ResourceType::kId, name)), value});
170 return *this;
171}
172
173std::unique_ptr<Attribute> AttributeBuilder::Build() {
174 return std::move(attr_);
175}
176
177StyleBuilder& StyleBuilder::SetParent(const StringPiece& str) {
178 style_->parent = Reference(ParseNameOrDie(str));
179 return *this;
180}
181
182StyleBuilder& StyleBuilder::AddItem(const StringPiece& str, std::unique_ptr<Item> value) {
183 style_->entries.push_back(Style::Entry{Reference(ParseNameOrDie(str)), std::move(value)});
184 return *this;
185}
186
187StyleBuilder& StyleBuilder::AddItem(const StringPiece& str, const ResourceId& id,
188 std::unique_ptr<Item> value) {
189 AddItem(str, std::move(value));
190 style_->entries.back().key.id = id;
191 return *this;
192}
193
194std::unique_ptr<Style> StyleBuilder::Build() {
195 return std::move(style_);
196}
197
198StyleableBuilder& StyleableBuilder::AddItem(const StringPiece& str, const Maybe<ResourceId>& id) {
199 styleable_->entries.push_back(Reference(ParseNameOrDie(str)));
200 styleable_->entries.back().id = id;
201 return *this;
202}
203
204std::unique_ptr<Styleable> StyleableBuilder::Build() {
205 return std::move(styleable_);
206}
207
208std::unique_ptr<xml::XmlResource> BuildXmlDom(const StringPiece& str) {
209 std::string input = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
210 input.append(str.data(), str.size());
211 StringInputStream in(input);
212 StdErrDiagnostics diag;
213 std::unique_ptr<xml::XmlResource> doc = xml::Inflate(&in, &diag, Source("test.xml"));
Adam Lesinski6b372992017-08-09 10:54:23 -0700214 CHECK(doc != nullptr && doc->root != nullptr) << "failed to parse inline XML string";
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700215 return doc;
216}
217
218std::unique_ptr<xml::XmlResource> BuildXmlDomForPackageName(IAaptContext* context,
219 const StringPiece& str) {
220 std::unique_ptr<xml::XmlResource> doc = BuildXmlDom(str);
221 doc->file.name.package = context->GetCompilationPackage();
222 return doc;
223}
224
Shane Farmer0a5b2012017-06-22 12:24:12 -0700225ArtifactBuilder& ArtifactBuilder::SetName(const std::string& name) {
Shane Farmercb6c3f92017-11-27 13:19:36 -0800226 artifact_.name = name;
Shane Farmer0a5b2012017-06-22 12:24:12 -0700227 return *this;
228}
229
Shane Farmercb6c3f92017-11-27 13:19:36 -0800230ArtifactBuilder& ArtifactBuilder::AddAbi(configuration::Abi abi) {
231 artifact_.abis.push_back(abi);
Shane Farmer0a5b2012017-06-22 12:24:12 -0700232 return *this;
233}
234
Shane Farmercb6c3f92017-11-27 13:19:36 -0800235ArtifactBuilder& ArtifactBuilder::AddDensity(const ConfigDescription& density) {
236 artifact_.screen_densities.push_back(density);
Shane Farmer0a5b2012017-06-22 12:24:12 -0700237 return *this;
238}
239
Shane Farmercb6c3f92017-11-27 13:19:36 -0800240ArtifactBuilder& ArtifactBuilder::AddLocale(const ConfigDescription& locale) {
241 artifact_.locales.push_back(locale);
Shane Farmer0a5b2012017-06-22 12:24:12 -0700242 return *this;
243}
244
Shane Farmercb6c3f92017-11-27 13:19:36 -0800245ArtifactBuilder& ArtifactBuilder::SetAndroidSdk(int min_sdk) {
246 artifact_.android_sdk = {AndroidSdk::ForMinSdk(min_sdk)};
Shane Farmerefe45392017-08-21 14:39:28 -0700247 return *this;
248}
249
Shane Farmercb6c3f92017-11-27 13:19:36 -0800250configuration::OutputArtifact ArtifactBuilder::Build() {
Shane Farmer0a5b2012017-06-22 12:24:12 -0700251 return artifact_;
252}
253
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700254} // namespace test
255} // namespace aapt