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; |
| 28 | using ::aapt::configuration::Artifact; |
Adam Lesinski | efeb7af | 2017-08-02 14:57:43 -0700 | [diff] [blame] | 29 | using ::aapt::io::StringInputStream; |
| 30 | using ::android::StringPiece; |
| 31 | |
| 32 | namespace aapt { |
| 33 | namespace test { |
| 34 | |
| 35 | ResourceTableBuilder& 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 | |
| 42 | ResourceTableBuilder& ResourceTableBuilder::AddSimple(const StringPiece& name, |
| 43 | const ResourceId& id) { |
| 44 | return AddValue(name, id, util::make_unique<Id>()); |
| 45 | } |
| 46 | |
| 47 | ResourceTableBuilder& 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 | |
| 53 | ResourceTableBuilder& ResourceTableBuilder::AddReference(const StringPiece& name, |
| 54 | const StringPiece& ref) { |
| 55 | return AddReference(name, {}, ref); |
| 56 | } |
| 57 | |
| 58 | ResourceTableBuilder& 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 | |
| 64 | ResourceTableBuilder& ResourceTableBuilder::AddString(const StringPiece& name, |
| 65 | const StringPiece& str) { |
| 66 | return AddString(name, {}, str); |
| 67 | } |
| 68 | |
| 69 | ResourceTableBuilder& 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 | |
| 74 | ResourceTableBuilder& 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 | |
| 80 | ResourceTableBuilder& ResourceTableBuilder::AddFileReference(const StringPiece& name, |
| 81 | const StringPiece& path) { |
| 82 | return AddFileReference(name, {}, path); |
| 83 | } |
| 84 | |
| 85 | ResourceTableBuilder& ResourceTableBuilder::AddFileReference(const StringPiece& name, |
| 86 | const ResourceId& id, |
| 87 | const StringPiece& path) { |
| 88 | return AddValue(name, id, util::make_unique<FileReference>(table_->string_pool.MakeRef(path))); |
| 89 | } |
| 90 | |
| 91 | ResourceTableBuilder& ResourceTableBuilder::AddFileReference(const StringPiece& name, |
| 92 | const StringPiece& path, |
| 93 | const ConfigDescription& config) { |
| 94 | return AddValue(name, config, {}, |
| 95 | util::make_unique<FileReference>(table_->string_pool.MakeRef(path))); |
| 96 | } |
| 97 | |
| 98 | ResourceTableBuilder& ResourceTableBuilder::AddValue(const StringPiece& name, |
| 99 | std::unique_ptr<Value> value) { |
| 100 | return AddValue(name, {}, std::move(value)); |
| 101 | } |
| 102 | |
| 103 | ResourceTableBuilder& ResourceTableBuilder::AddValue(const StringPiece& name, const ResourceId& id, |
| 104 | std::unique_ptr<Value> value) { |
| 105 | return AddValue(name, {}, id, std::move(value)); |
| 106 | } |
| 107 | |
| 108 | ResourceTableBuilder& ResourceTableBuilder::AddValue(const StringPiece& name, |
| 109 | const ConfigDescription& config, |
| 110 | const ResourceId& id, |
| 111 | std::unique_ptr<Value> value) { |
| 112 | ResourceName res_name = ParseNameOrDie(name); |
| 113 | CHECK(table_->AddResourceAllowMangled(res_name, id, config, {}, std::move(value), |
| 114 | GetDiagnostics())); |
| 115 | return *this; |
| 116 | } |
| 117 | |
| 118 | ResourceTableBuilder& ResourceTableBuilder::SetSymbolState(const StringPiece& name, |
| 119 | const ResourceId& id, SymbolState state, |
| 120 | bool allow_new) { |
| 121 | ResourceName res_name = ParseNameOrDie(name); |
| 122 | Symbol symbol; |
| 123 | symbol.state = state; |
| 124 | symbol.allow_new = allow_new; |
| 125 | CHECK(table_->SetSymbolStateAllowMangled(res_name, id, symbol, GetDiagnostics())); |
| 126 | return *this; |
| 127 | } |
| 128 | |
| 129 | StringPool* ResourceTableBuilder::string_pool() { |
| 130 | return &table_->string_pool; |
| 131 | } |
| 132 | |
| 133 | std::unique_ptr<ResourceTable> ResourceTableBuilder::Build() { |
| 134 | return std::move(table_); |
| 135 | } |
| 136 | |
| 137 | std::unique_ptr<Reference> BuildReference(const StringPiece& ref, const Maybe<ResourceId>& id) { |
| 138 | std::unique_ptr<Reference> reference = util::make_unique<Reference>(ParseNameOrDie(ref)); |
| 139 | reference->id = id; |
| 140 | return reference; |
| 141 | } |
| 142 | |
| 143 | std::unique_ptr<BinaryPrimitive> BuildPrimitive(uint8_t type, uint32_t data) { |
| 144 | android::Res_value value = {}; |
| 145 | value.size = sizeof(value); |
| 146 | value.dataType = type; |
| 147 | value.data = data; |
| 148 | return util::make_unique<BinaryPrimitive>(value); |
| 149 | } |
| 150 | |
| 151 | AttributeBuilder::AttributeBuilder(bool weak) : attr_(util::make_unique<Attribute>(weak)) { |
| 152 | attr_->type_mask = android::ResTable_map::TYPE_ANY; |
| 153 | } |
| 154 | |
| 155 | AttributeBuilder& AttributeBuilder::SetTypeMask(uint32_t typeMask) { |
| 156 | attr_->type_mask = typeMask; |
| 157 | return *this; |
| 158 | } |
| 159 | |
| 160 | AttributeBuilder& AttributeBuilder::AddItem(const StringPiece& name, uint32_t value) { |
| 161 | attr_->symbols.push_back( |
| 162 | Attribute::Symbol{Reference(ResourceName({}, ResourceType::kId, name)), value}); |
| 163 | return *this; |
| 164 | } |
| 165 | |
| 166 | std::unique_ptr<Attribute> AttributeBuilder::Build() { |
| 167 | return std::move(attr_); |
| 168 | } |
| 169 | |
| 170 | StyleBuilder& StyleBuilder::SetParent(const StringPiece& str) { |
| 171 | style_->parent = Reference(ParseNameOrDie(str)); |
| 172 | return *this; |
| 173 | } |
| 174 | |
| 175 | StyleBuilder& StyleBuilder::AddItem(const StringPiece& str, std::unique_ptr<Item> value) { |
| 176 | style_->entries.push_back(Style::Entry{Reference(ParseNameOrDie(str)), std::move(value)}); |
| 177 | return *this; |
| 178 | } |
| 179 | |
| 180 | StyleBuilder& StyleBuilder::AddItem(const StringPiece& str, const ResourceId& id, |
| 181 | std::unique_ptr<Item> value) { |
| 182 | AddItem(str, std::move(value)); |
| 183 | style_->entries.back().key.id = id; |
| 184 | return *this; |
| 185 | } |
| 186 | |
| 187 | std::unique_ptr<Style> StyleBuilder::Build() { |
| 188 | return std::move(style_); |
| 189 | } |
| 190 | |
| 191 | StyleableBuilder& StyleableBuilder::AddItem(const StringPiece& str, const Maybe<ResourceId>& id) { |
| 192 | styleable_->entries.push_back(Reference(ParseNameOrDie(str))); |
| 193 | styleable_->entries.back().id = id; |
| 194 | return *this; |
| 195 | } |
| 196 | |
| 197 | std::unique_ptr<Styleable> StyleableBuilder::Build() { |
| 198 | return std::move(styleable_); |
| 199 | } |
| 200 | |
| 201 | std::unique_ptr<xml::XmlResource> BuildXmlDom(const StringPiece& str) { |
| 202 | std::string input = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"; |
| 203 | input.append(str.data(), str.size()); |
| 204 | StringInputStream in(input); |
| 205 | StdErrDiagnostics diag; |
| 206 | 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] | 207 | CHECK(doc != nullptr && doc->root != nullptr) << "failed to parse inline XML string"; |
Adam Lesinski | efeb7af | 2017-08-02 14:57:43 -0700 | [diff] [blame] | 208 | return doc; |
| 209 | } |
| 210 | |
| 211 | std::unique_ptr<xml::XmlResource> BuildXmlDomForPackageName(IAaptContext* context, |
| 212 | const StringPiece& str) { |
| 213 | std::unique_ptr<xml::XmlResource> doc = BuildXmlDom(str); |
| 214 | doc->file.name.package = context->GetCompilationPackage(); |
| 215 | return doc; |
| 216 | } |
| 217 | |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 218 | PostProcessingConfigurationBuilder& PostProcessingConfigurationBuilder::SetAbiGroup( |
Shane Farmer | efe4539 | 2017-08-21 14:39:28 -0700 | [diff] [blame] | 219 | const std::string& name, const std::vector<Abi>& abis) { |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 220 | config_.abi_groups[name] = abis; |
| 221 | return *this; |
| 222 | } |
| 223 | |
| 224 | PostProcessingConfigurationBuilder& PostProcessingConfigurationBuilder::SetLocaleGroup( |
| 225 | const std::string& name, const std::vector<std::string>& locales) { |
| 226 | auto& group = config_.locale_groups[name]; |
| 227 | for (const auto& locale : locales) { |
| 228 | group.push_back(ParseConfigOrDie(locale)); |
| 229 | } |
| 230 | return *this; |
| 231 | } |
| 232 | |
| 233 | PostProcessingConfigurationBuilder& PostProcessingConfigurationBuilder::SetDensityGroup( |
| 234 | const std::string& name, const std::vector<std::string>& densities) { |
| 235 | auto& group = config_.screen_density_groups[name]; |
| 236 | for (const auto& density : densities) { |
| 237 | group.push_back(ParseConfigOrDie(density)); |
| 238 | } |
| 239 | return *this; |
| 240 | } |
| 241 | |
Shane Farmer | efe4539 | 2017-08-21 14:39:28 -0700 | [diff] [blame] | 242 | PostProcessingConfigurationBuilder& PostProcessingConfigurationBuilder::SetAndroidSdk( |
| 243 | const std::string& name, const AndroidSdk& sdk) { |
| 244 | config_.android_sdk_groups[name] = sdk; |
| 245 | return *this; |
| 246 | } |
| 247 | |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 248 | PostProcessingConfigurationBuilder& PostProcessingConfigurationBuilder::AddArtifact( |
Shane Farmer | efe4539 | 2017-08-21 14:39:28 -0700 | [diff] [blame] | 249 | const Artifact& artifact) { |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 250 | config_.artifacts.push_back(artifact); |
| 251 | return *this; |
| 252 | } |
| 253 | |
| 254 | configuration::PostProcessingConfiguration PostProcessingConfigurationBuilder::Build() { |
| 255 | return config_; |
| 256 | } |
| 257 | |
| 258 | ArtifactBuilder& ArtifactBuilder::SetName(const std::string& name) { |
| 259 | artifact_.name = {name}; |
| 260 | return *this; |
| 261 | } |
| 262 | |
| 263 | ArtifactBuilder& ArtifactBuilder::SetAbiGroup(const std::string& name) { |
| 264 | artifact_.abi_group = {name}; |
| 265 | return *this; |
| 266 | } |
| 267 | |
| 268 | ArtifactBuilder& ArtifactBuilder::SetDensityGroup(const std::string& name) { |
| 269 | artifact_.screen_density_group = {name}; |
| 270 | return *this; |
| 271 | } |
| 272 | |
| 273 | ArtifactBuilder& ArtifactBuilder::SetLocaleGroup(const std::string& name) { |
| 274 | artifact_.locale_group = {name}; |
| 275 | return *this; |
| 276 | } |
| 277 | |
Shane Farmer | efe4539 | 2017-08-21 14:39:28 -0700 | [diff] [blame] | 278 | ArtifactBuilder& ArtifactBuilder::SetAndroidSdk(const std::string& name) { |
| 279 | artifact_.android_sdk_group = {name}; |
| 280 | return *this; |
| 281 | } |
| 282 | |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 283 | configuration::Artifact ArtifactBuilder::Build() { |
| 284 | return artifact_; |
| 285 | } |
| 286 | |
Adam Lesinski | efeb7af | 2017-08-02 14:57:43 -0700 | [diff] [blame] | 287 | } // namespace test |
| 288 | } // namespace aapt |