Adam Lesinski | efeb7af | 2017-08-02 14:57:43 -0700 | [diff] [blame] | 1 | /* |
| 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 Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 22 | #include "io/StringStream.h" |
Adam Lesinski | efeb7af | 2017-08-02 14:57:43 -0700 | [diff] [blame] | 23 | #include "test/Common.h" |
| 24 | #include "util/Util.h" |
| 25 | |
Shane Farmer | efe4539 | 2017-08-21 14:39:28 -0700 | [diff] [blame] | 26 | using ::aapt::configuration::Abi; |
| 27 | using ::aapt::configuration::AndroidSdk; |
Shane Farmer | cb6c3f9 | 2017-11-27 13:19:36 -0800 | [diff] [blame] | 28 | using ::aapt::configuration::ConfiguredArtifact; |
Shane Farmer | 78c43d7 | 2017-12-04 09:08:38 -0800 | [diff] [blame] | 29 | using ::aapt::configuration::GetOrCreateGroup; |
Adam Lesinski | efeb7af | 2017-08-02 14:57:43 -0700 | [diff] [blame] | 30 | using ::aapt::io::StringInputStream; |
MÃ¥rten Kongstad | 24c9aa6 | 2018-06-20 08:46:41 +0200 | [diff] [blame^] | 31 | using ::android::ConfigDescription; |
Adam Lesinski | efeb7af | 2017-08-02 14:57:43 -0700 | [diff] [blame] | 32 | using ::android::StringPiece; |
| 33 | |
| 34 | namespace aapt { |
| 35 | namespace test { |
| 36 | |
| 37 | ResourceTableBuilder& 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 | |
| 44 | ResourceTableBuilder& ResourceTableBuilder::AddSimple(const StringPiece& name, |
| 45 | const ResourceId& id) { |
| 46 | return AddValue(name, id, util::make_unique<Id>()); |
| 47 | } |
| 48 | |
| 49 | ResourceTableBuilder& 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 | |
| 55 | ResourceTableBuilder& ResourceTableBuilder::AddReference(const StringPiece& name, |
| 56 | const StringPiece& ref) { |
| 57 | return AddReference(name, {}, ref); |
| 58 | } |
| 59 | |
| 60 | ResourceTableBuilder& 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 | |
| 66 | ResourceTableBuilder& ResourceTableBuilder::AddString(const StringPiece& name, |
| 67 | const StringPiece& str) { |
| 68 | return AddString(name, {}, str); |
| 69 | } |
| 70 | |
| 71 | ResourceTableBuilder& 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 | |
| 76 | ResourceTableBuilder& 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 | |
| 82 | ResourceTableBuilder& ResourceTableBuilder::AddFileReference(const StringPiece& name, |
Adam Lesinski | 8780eb6 | 2017-10-31 17:44:39 -0700 | [diff] [blame] | 83 | const StringPiece& path, |
| 84 | io::IFile* file) { |
| 85 | return AddFileReference(name, {}, path, file); |
Adam Lesinski | efeb7af | 2017-08-02 14:57:43 -0700 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | ResourceTableBuilder& ResourceTableBuilder::AddFileReference(const StringPiece& name, |
| 89 | const ResourceId& id, |
Adam Lesinski | 8780eb6 | 2017-10-31 17:44:39 -0700 | [diff] [blame] | 90 | 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 Lesinski | efeb7af | 2017-08-02 14:57:43 -0700 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | ResourceTableBuilder& ResourceTableBuilder::AddFileReference(const StringPiece& name, |
| 98 | const StringPiece& path, |
Adam Lesinski | 8780eb6 | 2017-10-31 17:44:39 -0700 | [diff] [blame] | 99 | 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 Lesinski | efeb7af | 2017-08-02 14:57:43 -0700 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | ResourceTableBuilder& ResourceTableBuilder::AddValue(const StringPiece& name, |
| 107 | std::unique_ptr<Value> value) { |
| 108 | return AddValue(name, {}, std::move(value)); |
| 109 | } |
| 110 | |
| 111 | ResourceTableBuilder& ResourceTableBuilder::AddValue(const StringPiece& name, const ResourceId& id, |
| 112 | std::unique_ptr<Value> value) { |
| 113 | return AddValue(name, {}, id, std::move(value)); |
| 114 | } |
| 115 | |
| 116 | ResourceTableBuilder& 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 Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 121 | CHECK(table_->AddResourceWithIdMangled(res_name, id, config, {}, std::move(value), |
| 122 | GetDiagnostics())); |
Adam Lesinski | efeb7af | 2017-08-02 14:57:43 -0700 | [diff] [blame] | 123 | return *this; |
| 124 | } |
| 125 | |
| 126 | ResourceTableBuilder& ResourceTableBuilder::SetSymbolState(const StringPiece& name, |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 127 | const ResourceId& id, |
| 128 | Visibility::Level level, |
Adam Lesinski | efeb7af | 2017-08-02 14:57:43 -0700 | [diff] [blame] | 129 | bool allow_new) { |
| 130 | ResourceName res_name = ParseNameOrDie(name); |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 131 | Visibility visibility; |
| 132 | visibility.level = level; |
| 133 | CHECK(table_->SetVisibilityWithIdMangled(res_name, visibility, id, GetDiagnostics())); |
| 134 | CHECK(table_->SetAllowNewMangled(res_name, AllowNew{}, GetDiagnostics())); |
Adam Lesinski | efeb7af | 2017-08-02 14:57:43 -0700 | [diff] [blame] | 135 | return *this; |
| 136 | } |
| 137 | |
| 138 | StringPool* ResourceTableBuilder::string_pool() { |
| 139 | return &table_->string_pool; |
| 140 | } |
| 141 | |
| 142 | std::unique_ptr<ResourceTable> ResourceTableBuilder::Build() { |
| 143 | return std::move(table_); |
| 144 | } |
| 145 | |
| 146 | std::unique_ptr<Reference> BuildReference(const StringPiece& ref, const Maybe<ResourceId>& id) { |
| 147 | std::unique_ptr<Reference> reference = util::make_unique<Reference>(ParseNameOrDie(ref)); |
| 148 | reference->id = id; |
| 149 | return reference; |
| 150 | } |
| 151 | |
| 152 | std::unique_ptr<BinaryPrimitive> BuildPrimitive(uint8_t type, uint32_t data) { |
| 153 | android::Res_value value = {}; |
| 154 | value.size = sizeof(value); |
| 155 | value.dataType = type; |
| 156 | value.data = data; |
| 157 | return util::make_unique<BinaryPrimitive>(value); |
| 158 | } |
| 159 | |
Adam Lesinski | 73bff1e | 2017-12-08 16:06:10 -0800 | [diff] [blame] | 160 | AttributeBuilder::AttributeBuilder() |
| 161 | : attr_(util::make_unique<Attribute>(android::ResTable_map::TYPE_ANY)) { |
Adam Lesinski | efeb7af | 2017-08-02 14:57:43 -0700 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | AttributeBuilder& AttributeBuilder::SetTypeMask(uint32_t typeMask) { |
| 165 | attr_->type_mask = typeMask; |
| 166 | return *this; |
| 167 | } |
| 168 | |
Adam Lesinski | 73bff1e | 2017-12-08 16:06:10 -0800 | [diff] [blame] | 169 | AttributeBuilder& AttributeBuilder::SetWeak(bool weak) { |
| 170 | attr_->SetWeak(weak); |
| 171 | return *this; |
| 172 | } |
| 173 | |
Adam Lesinski | efeb7af | 2017-08-02 14:57:43 -0700 | [diff] [blame] | 174 | AttributeBuilder& AttributeBuilder::AddItem(const StringPiece& name, uint32_t value) { |
| 175 | attr_->symbols.push_back( |
| 176 | Attribute::Symbol{Reference(ResourceName({}, ResourceType::kId, name)), value}); |
| 177 | return *this; |
| 178 | } |
| 179 | |
| 180 | std::unique_ptr<Attribute> AttributeBuilder::Build() { |
| 181 | return std::move(attr_); |
| 182 | } |
| 183 | |
| 184 | StyleBuilder& StyleBuilder::SetParent(const StringPiece& str) { |
| 185 | style_->parent = Reference(ParseNameOrDie(str)); |
| 186 | return *this; |
| 187 | } |
| 188 | |
| 189 | StyleBuilder& StyleBuilder::AddItem(const StringPiece& str, std::unique_ptr<Item> value) { |
| 190 | style_->entries.push_back(Style::Entry{Reference(ParseNameOrDie(str)), std::move(value)}); |
| 191 | return *this; |
| 192 | } |
| 193 | |
| 194 | StyleBuilder& StyleBuilder::AddItem(const StringPiece& str, const ResourceId& id, |
| 195 | std::unique_ptr<Item> value) { |
| 196 | AddItem(str, std::move(value)); |
| 197 | style_->entries.back().key.id = id; |
| 198 | return *this; |
| 199 | } |
| 200 | |
| 201 | std::unique_ptr<Style> StyleBuilder::Build() { |
| 202 | return std::move(style_); |
| 203 | } |
| 204 | |
| 205 | StyleableBuilder& StyleableBuilder::AddItem(const StringPiece& str, const Maybe<ResourceId>& id) { |
| 206 | styleable_->entries.push_back(Reference(ParseNameOrDie(str))); |
| 207 | styleable_->entries.back().id = id; |
| 208 | return *this; |
| 209 | } |
| 210 | |
| 211 | std::unique_ptr<Styleable> StyleableBuilder::Build() { |
| 212 | return std::move(styleable_); |
| 213 | } |
| 214 | |
| 215 | std::unique_ptr<xml::XmlResource> BuildXmlDom(const StringPiece& str) { |
| 216 | std::string input = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"; |
| 217 | input.append(str.data(), str.size()); |
| 218 | StringInputStream in(input); |
| 219 | StdErrDiagnostics diag; |
| 220 | std::unique_ptr<xml::XmlResource> doc = xml::Inflate(&in, &diag, Source("test.xml")); |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 221 | CHECK(doc != nullptr && doc->root != nullptr) << "failed to parse inline XML string"; |
Adam Lesinski | efeb7af | 2017-08-02 14:57:43 -0700 | [diff] [blame] | 222 | return doc; |
| 223 | } |
| 224 | |
| 225 | std::unique_ptr<xml::XmlResource> BuildXmlDomForPackageName(IAaptContext* context, |
| 226 | const StringPiece& str) { |
| 227 | std::unique_ptr<xml::XmlResource> doc = BuildXmlDom(str); |
| 228 | doc->file.name.package = context->GetCompilationPackage(); |
| 229 | return doc; |
| 230 | } |
| 231 | |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 232 | ArtifactBuilder& ArtifactBuilder::SetName(const std::string& name) { |
Shane Farmer | cb6c3f9 | 2017-11-27 13:19:36 -0800 | [diff] [blame] | 233 | artifact_.name = name; |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 234 | return *this; |
| 235 | } |
| 236 | |
Shane Farmer | 78c43d7 | 2017-12-04 09:08:38 -0800 | [diff] [blame] | 237 | ArtifactBuilder& ArtifactBuilder::SetVersion(int version) { |
| 238 | artifact_.version = version; |
| 239 | return *this; |
| 240 | } |
| 241 | |
Shane Farmer | cb6c3f9 | 2017-11-27 13:19:36 -0800 | [diff] [blame] | 242 | ArtifactBuilder& ArtifactBuilder::AddAbi(configuration::Abi abi) { |
| 243 | artifact_.abis.push_back(abi); |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 244 | return *this; |
| 245 | } |
| 246 | |
Shane Farmer | cb6c3f9 | 2017-11-27 13:19:36 -0800 | [diff] [blame] | 247 | ArtifactBuilder& ArtifactBuilder::AddDensity(const ConfigDescription& density) { |
| 248 | artifact_.screen_densities.push_back(density); |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 249 | return *this; |
| 250 | } |
| 251 | |
Shane Farmer | cb6c3f9 | 2017-11-27 13:19:36 -0800 | [diff] [blame] | 252 | ArtifactBuilder& ArtifactBuilder::AddLocale(const ConfigDescription& locale) { |
| 253 | artifact_.locales.push_back(locale); |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 254 | return *this; |
| 255 | } |
| 256 | |
Shane Farmer | cb6c3f9 | 2017-11-27 13:19:36 -0800 | [diff] [blame] | 257 | ArtifactBuilder& ArtifactBuilder::SetAndroidSdk(int min_sdk) { |
| 258 | artifact_.android_sdk = {AndroidSdk::ForMinSdk(min_sdk)}; |
Shane Farmer | efe4539 | 2017-08-21 14:39:28 -0700 | [diff] [blame] | 259 | return *this; |
| 260 | } |
| 261 | |
Shane Farmer | cb6c3f9 | 2017-11-27 13:19:36 -0800 | [diff] [blame] | 262 | configuration::OutputArtifact ArtifactBuilder::Build() { |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 263 | return artifact_; |
| 264 | } |
| 265 | |
Shane Farmer | 78c43d7 | 2017-12-04 09:08:38 -0800 | [diff] [blame] | 266 | PostProcessingConfigurationBuilder& PostProcessingConfigurationBuilder::AddAbiGroup( |
| 267 | const std::string& label, std::vector<configuration::Abi> abis) { |
| 268 | return AddGroup(label, &config_.abi_groups, std::move(abis)); |
| 269 | } |
| 270 | |
| 271 | PostProcessingConfigurationBuilder& PostProcessingConfigurationBuilder::AddDensityGroup( |
| 272 | const std::string& label, std::vector<std::string> densities) { |
| 273 | std::vector<ConfigDescription> configs; |
| 274 | for (const auto& density : densities) { |
| 275 | configs.push_back(test::ParseConfigOrDie(density)); |
| 276 | } |
| 277 | return AddGroup(label, &config_.screen_density_groups, configs); |
| 278 | } |
| 279 | |
| 280 | PostProcessingConfigurationBuilder& PostProcessingConfigurationBuilder::AddLocaleGroup( |
| 281 | const std::string& label, std::vector<std::string> locales) { |
| 282 | std::vector<ConfigDescription> configs; |
| 283 | for (const auto& locale : locales) { |
| 284 | configs.push_back(test::ParseConfigOrDie(locale)); |
| 285 | } |
| 286 | return AddGroup(label, &config_.locale_groups, configs); |
| 287 | } |
| 288 | |
| 289 | PostProcessingConfigurationBuilder& PostProcessingConfigurationBuilder::AddDeviceFeatureGroup( |
| 290 | const std::string& label) { |
| 291 | return AddGroup(label, &config_.device_feature_groups); |
| 292 | } |
| 293 | |
| 294 | PostProcessingConfigurationBuilder& PostProcessingConfigurationBuilder::AddGlTextureGroup( |
| 295 | const std::string& label) { |
| 296 | return AddGroup(label, &config_.gl_texture_groups); |
| 297 | } |
| 298 | |
| 299 | PostProcessingConfigurationBuilder& PostProcessingConfigurationBuilder::AddAndroidSdk( |
| 300 | std::string label, int min_sdk) { |
| 301 | config_.android_sdks[label] = AndroidSdk::ForMinSdk(min_sdk); |
| 302 | return *this; |
| 303 | } |
| 304 | |
| 305 | PostProcessingConfigurationBuilder& PostProcessingConfigurationBuilder::AddArtifact( |
| 306 | configuration::ConfiguredArtifact artifact) { |
| 307 | config_.artifacts.push_back(std::move(artifact)); |
| 308 | return *this; |
| 309 | } |
| 310 | |
| 311 | configuration::PostProcessingConfiguration PostProcessingConfigurationBuilder::Build() { |
| 312 | return config_; |
| 313 | } |
| 314 | |
Adam Lesinski | efeb7af | 2017-08-02 14:57:43 -0700 | [diff] [blame] | 315 | } // namespace test |
| 316 | } // namespace aapt |