blob: 884ec38290f8498cdb3f0dd577e556e70464fe0e [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;
Shane Farmer78c43d72017-12-04 09:08:38 -080029using ::aapt::configuration::GetOrCreateGroup;
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070030using ::aapt::io::StringInputStream;
MÃ¥rten Kongstad24c9aa62018-06-20 08:46:41 +020031using ::android::ConfigDescription;
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070032using ::android::StringPiece;
33
34namespace aapt {
35namespace test {
36
37ResourceTableBuilder& ResourceTableBuilder::SetPackageId(const StringPiece& package_name,
38 uint8_t id) {
39 ResourceTablePackage* package = table_->CreatePackage(package_name, id);
40 CHECK(package != nullptr);
41 return *this;
42}
43
44ResourceTableBuilder& ResourceTableBuilder::AddSimple(const StringPiece& name,
45 const ResourceId& id) {
46 return AddValue(name, id, util::make_unique<Id>());
47}
48
49ResourceTableBuilder& ResourceTableBuilder::AddSimple(const StringPiece& name,
50 const ConfigDescription& config,
51 const ResourceId& id) {
52 return AddValue(name, config, id, util::make_unique<Id>());
53}
54
55ResourceTableBuilder& ResourceTableBuilder::AddReference(const StringPiece& name,
56 const StringPiece& ref) {
57 return AddReference(name, {}, ref);
58}
59
60ResourceTableBuilder& ResourceTableBuilder::AddReference(const StringPiece& name,
61 const ResourceId& id,
62 const StringPiece& ref) {
63 return AddValue(name, id, util::make_unique<Reference>(ParseNameOrDie(ref)));
64}
65
66ResourceTableBuilder& ResourceTableBuilder::AddString(const StringPiece& name,
67 const StringPiece& str) {
68 return AddString(name, {}, str);
69}
70
71ResourceTableBuilder& ResourceTableBuilder::AddString(const StringPiece& name, const ResourceId& id,
72 const StringPiece& str) {
73 return AddValue(name, id, util::make_unique<String>(table_->string_pool.MakeRef(str)));
74}
75
76ResourceTableBuilder& ResourceTableBuilder::AddString(const StringPiece& name, const ResourceId& id,
77 const ConfigDescription& config,
78 const StringPiece& str) {
79 return AddValue(name, config, id, util::make_unique<String>(table_->string_pool.MakeRef(str)));
80}
81
82ResourceTableBuilder& ResourceTableBuilder::AddFileReference(const StringPiece& name,
Adam Lesinski8780eb62017-10-31 17:44:39 -070083 const StringPiece& path,
84 io::IFile* file) {
85 return AddFileReference(name, {}, path, file);
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070086}
87
88ResourceTableBuilder& ResourceTableBuilder::AddFileReference(const StringPiece& name,
89 const ResourceId& id,
Adam Lesinski8780eb62017-10-31 17:44:39 -070090 const StringPiece& path,
91 io::IFile* file) {
92 auto file_ref = util::make_unique<FileReference>(table_->string_pool.MakeRef(path));
93 file_ref->file = file;
94 return AddValue(name, id, std::move(file_ref));
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070095}
96
97ResourceTableBuilder& ResourceTableBuilder::AddFileReference(const StringPiece& name,
98 const StringPiece& path,
Adam Lesinski8780eb62017-10-31 17:44:39 -070099 const ConfigDescription& config,
100 io::IFile* file) {
101 auto file_ref = util::make_unique<FileReference>(table_->string_pool.MakeRef(path));
102 file_ref->file = file;
103 return AddValue(name, config, {}, std::move(file_ref));
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700104}
105
106ResourceTableBuilder& ResourceTableBuilder::AddValue(const StringPiece& name,
107 std::unique_ptr<Value> value) {
108 return AddValue(name, {}, std::move(value));
109}
110
111ResourceTableBuilder& ResourceTableBuilder::AddValue(const StringPiece& name, const ResourceId& id,
112 std::unique_ptr<Value> value) {
113 return AddValue(name, {}, id, std::move(value));
114}
115
116ResourceTableBuilder& ResourceTableBuilder::AddValue(const StringPiece& name,
117 const ConfigDescription& config,
118 const ResourceId& id,
119 std::unique_ptr<Value> value) {
120 ResourceName res_name = ParseNameOrDie(name);
Adam Lesinski71be7052017-12-12 16:48:07 -0800121 CHECK(table_->AddResourceWithIdMangled(res_name, id, config, {}, std::move(value),
122 GetDiagnostics()));
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700123 return *this;
124}
125
126ResourceTableBuilder& ResourceTableBuilder::SetSymbolState(const StringPiece& name,
Adam Lesinski71be7052017-12-12 16:48:07 -0800127 const ResourceId& id,
128 Visibility::Level level,
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700129 bool allow_new) {
130 ResourceName res_name = ParseNameOrDie(name);
Adam Lesinski71be7052017-12-12 16:48:07 -0800131 Visibility visibility;
132 visibility.level = level;
133 CHECK(table_->SetVisibilityWithIdMangled(res_name, visibility, id, GetDiagnostics()));
134 CHECK(table_->SetAllowNewMangled(res_name, AllowNew{}, GetDiagnostics()));
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700135 return *this;
136}
137
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -0800138ResourceTableBuilder& ResourceTableBuilder::SetOverlayable(const StringPiece& name,
139 const Overlayable& overlayable) {
140
Ryan Mitchelle4e989c2018-10-29 02:21:50 -0700141 ResourceName res_name = ParseNameOrDie(name);
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -0800142 CHECK(table_->SetOverlayable(res_name, overlayable, GetDiagnostics()));
Ryan Mitchelle4e989c2018-10-29 02:21:50 -0700143 return *this;
144}
145
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700146StringPool* ResourceTableBuilder::string_pool() {
147 return &table_->string_pool;
148}
149
150std::unique_ptr<ResourceTable> ResourceTableBuilder::Build() {
151 return std::move(table_);
152}
153
154std::unique_ptr<Reference> BuildReference(const StringPiece& ref, const Maybe<ResourceId>& id) {
155 std::unique_ptr<Reference> reference = util::make_unique<Reference>(ParseNameOrDie(ref));
156 reference->id = id;
157 return reference;
158}
159
160std::unique_ptr<BinaryPrimitive> BuildPrimitive(uint8_t type, uint32_t data) {
161 android::Res_value value = {};
162 value.size = sizeof(value);
163 value.dataType = type;
164 value.data = data;
165 return util::make_unique<BinaryPrimitive>(value);
166}
167
Adam Lesinski73bff1e2017-12-08 16:06:10 -0800168AttributeBuilder::AttributeBuilder()
169 : attr_(util::make_unique<Attribute>(android::ResTable_map::TYPE_ANY)) {
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700170}
171
172AttributeBuilder& AttributeBuilder::SetTypeMask(uint32_t typeMask) {
173 attr_->type_mask = typeMask;
174 return *this;
175}
176
Adam Lesinski73bff1e2017-12-08 16:06:10 -0800177AttributeBuilder& AttributeBuilder::SetWeak(bool weak) {
178 attr_->SetWeak(weak);
179 return *this;
180}
181
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700182AttributeBuilder& AttributeBuilder::AddItem(const StringPiece& name, uint32_t value) {
183 attr_->symbols.push_back(
184 Attribute::Symbol{Reference(ResourceName({}, ResourceType::kId, name)), value});
185 return *this;
186}
187
188std::unique_ptr<Attribute> AttributeBuilder::Build() {
189 return std::move(attr_);
190}
191
192StyleBuilder& StyleBuilder::SetParent(const StringPiece& str) {
193 style_->parent = Reference(ParseNameOrDie(str));
194 return *this;
195}
196
197StyleBuilder& StyleBuilder::AddItem(const StringPiece& str, std::unique_ptr<Item> value) {
198 style_->entries.push_back(Style::Entry{Reference(ParseNameOrDie(str)), std::move(value)});
199 return *this;
200}
201
202StyleBuilder& StyleBuilder::AddItem(const StringPiece& str, const ResourceId& id,
203 std::unique_ptr<Item> value) {
204 AddItem(str, std::move(value));
205 style_->entries.back().key.id = id;
206 return *this;
207}
208
209std::unique_ptr<Style> StyleBuilder::Build() {
210 return std::move(style_);
211}
212
213StyleableBuilder& StyleableBuilder::AddItem(const StringPiece& str, const Maybe<ResourceId>& id) {
214 styleable_->entries.push_back(Reference(ParseNameOrDie(str)));
215 styleable_->entries.back().id = id;
216 return *this;
217}
218
219std::unique_ptr<Styleable> StyleableBuilder::Build() {
220 return std::move(styleable_);
221}
222
223std::unique_ptr<xml::XmlResource> BuildXmlDom(const StringPiece& str) {
224 std::string input = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
225 input.append(str.data(), str.size());
226 StringInputStream in(input);
227 StdErrDiagnostics diag;
228 std::unique_ptr<xml::XmlResource> doc = xml::Inflate(&in, &diag, Source("test.xml"));
Adam Lesinski6b372992017-08-09 10:54:23 -0700229 CHECK(doc != nullptr && doc->root != nullptr) << "failed to parse inline XML string";
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700230 return doc;
231}
232
233std::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();
237 return doc;
238}
239
Shane Farmer0a5b2012017-06-22 12:24:12 -0700240ArtifactBuilder& ArtifactBuilder::SetName(const std::string& name) {
Shane Farmercb6c3f92017-11-27 13:19:36 -0800241 artifact_.name = name;
Shane Farmer0a5b2012017-06-22 12:24:12 -0700242 return *this;
243}
244
Shane Farmer78c43d72017-12-04 09:08:38 -0800245ArtifactBuilder& ArtifactBuilder::SetVersion(int version) {
246 artifact_.version = version;
247 return *this;
248}
249
Shane Farmercb6c3f92017-11-27 13:19:36 -0800250ArtifactBuilder& ArtifactBuilder::AddAbi(configuration::Abi abi) {
251 artifact_.abis.push_back(abi);
Shane Farmer0a5b2012017-06-22 12:24:12 -0700252 return *this;
253}
254
Shane Farmercb6c3f92017-11-27 13:19:36 -0800255ArtifactBuilder& ArtifactBuilder::AddDensity(const ConfigDescription& density) {
256 artifact_.screen_densities.push_back(density);
Shane Farmer0a5b2012017-06-22 12:24:12 -0700257 return *this;
258}
259
Shane Farmercb6c3f92017-11-27 13:19:36 -0800260ArtifactBuilder& ArtifactBuilder::AddLocale(const ConfigDescription& locale) {
261 artifact_.locales.push_back(locale);
Shane Farmer0a5b2012017-06-22 12:24:12 -0700262 return *this;
263}
264
Shane Farmercb6c3f92017-11-27 13:19:36 -0800265ArtifactBuilder& ArtifactBuilder::SetAndroidSdk(int min_sdk) {
266 artifact_.android_sdk = {AndroidSdk::ForMinSdk(min_sdk)};
Shane Farmerefe45392017-08-21 14:39:28 -0700267 return *this;
268}
269
Shane Farmercb6c3f92017-11-27 13:19:36 -0800270configuration::OutputArtifact ArtifactBuilder::Build() {
Shane Farmer0a5b2012017-06-22 12:24:12 -0700271 return artifact_;
272}
273
Shane Farmer78c43d72017-12-04 09:08:38 -0800274PostProcessingConfigurationBuilder& PostProcessingConfigurationBuilder::AddAbiGroup(
275 const std::string& label, std::vector<configuration::Abi> abis) {
276 return AddGroup(label, &config_.abi_groups, std::move(abis));
277}
278
279PostProcessingConfigurationBuilder& PostProcessingConfigurationBuilder::AddDensityGroup(
280 const std::string& label, std::vector<std::string> densities) {
281 std::vector<ConfigDescription> configs;
282 for (const auto& density : densities) {
283 configs.push_back(test::ParseConfigOrDie(density));
284 }
285 return AddGroup(label, &config_.screen_density_groups, configs);
286}
287
288PostProcessingConfigurationBuilder& PostProcessingConfigurationBuilder::AddLocaleGroup(
289 const std::string& label, std::vector<std::string> locales) {
290 std::vector<ConfigDescription> configs;
291 for (const auto& locale : locales) {
292 configs.push_back(test::ParseConfigOrDie(locale));
293 }
294 return AddGroup(label, &config_.locale_groups, configs);
295}
296
297PostProcessingConfigurationBuilder& PostProcessingConfigurationBuilder::AddDeviceFeatureGroup(
298 const std::string& label) {
299 return AddGroup(label, &config_.device_feature_groups);
300}
301
302PostProcessingConfigurationBuilder& PostProcessingConfigurationBuilder::AddGlTextureGroup(
303 const std::string& label) {
304 return AddGroup(label, &config_.gl_texture_groups);
305}
306
307PostProcessingConfigurationBuilder& PostProcessingConfigurationBuilder::AddAndroidSdk(
308 std::string label, int min_sdk) {
309 config_.android_sdks[label] = AndroidSdk::ForMinSdk(min_sdk);
310 return *this;
311}
312
313PostProcessingConfigurationBuilder& PostProcessingConfigurationBuilder::AddArtifact(
314 configuration::ConfiguredArtifact artifact) {
315 config_.artifacts.push_back(std::move(artifact));
316 return *this;
317}
318
319configuration::PostProcessingConfiguration PostProcessingConfigurationBuilder::Build() {
320 return config_;
321}
322
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700323} // namespace test
324} // namespace aapt