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; |
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, |
Adam Lesinski | 8780eb6 | 2017-10-31 17:44:39 -0700 | [diff] [blame] | 81 | const StringPiece& path, |
| 82 | io::IFile* file) { |
| 83 | return AddFileReference(name, {}, path, file); |
Adam Lesinski | efeb7af | 2017-08-02 14:57:43 -0700 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | ResourceTableBuilder& ResourceTableBuilder::AddFileReference(const StringPiece& name, |
| 87 | const ResourceId& id, |
Adam Lesinski | 8780eb6 | 2017-10-31 17:44:39 -0700 | [diff] [blame] | 88 | 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 Lesinski | efeb7af | 2017-08-02 14:57:43 -0700 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | ResourceTableBuilder& ResourceTableBuilder::AddFileReference(const StringPiece& name, |
| 96 | const StringPiece& path, |
Adam Lesinski | 8780eb6 | 2017-10-31 17:44:39 -0700 | [diff] [blame] | 97 | 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 Lesinski | efeb7af | 2017-08-02 14:57:43 -0700 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | ResourceTableBuilder& ResourceTableBuilder::AddValue(const StringPiece& name, |
| 105 | std::unique_ptr<Value> value) { |
| 106 | return AddValue(name, {}, std::move(value)); |
| 107 | } |
| 108 | |
| 109 | ResourceTableBuilder& ResourceTableBuilder::AddValue(const StringPiece& name, const ResourceId& id, |
| 110 | std::unique_ptr<Value> value) { |
| 111 | return AddValue(name, {}, id, std::move(value)); |
| 112 | } |
| 113 | |
| 114 | ResourceTableBuilder& 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); |
| 119 | CHECK(table_->AddResourceAllowMangled(res_name, id, config, {}, std::move(value), |
| 120 | GetDiagnostics())); |
| 121 | return *this; |
| 122 | } |
| 123 | |
| 124 | ResourceTableBuilder& ResourceTableBuilder::SetSymbolState(const StringPiece& name, |
| 125 | const ResourceId& id, SymbolState state, |
| 126 | bool allow_new) { |
| 127 | ResourceName res_name = ParseNameOrDie(name); |
| 128 | Symbol symbol; |
| 129 | symbol.state = state; |
| 130 | symbol.allow_new = allow_new; |
| 131 | CHECK(table_->SetSymbolStateAllowMangled(res_name, id, symbol, GetDiagnostics())); |
| 132 | return *this; |
| 133 | } |
| 134 | |
| 135 | StringPool* ResourceTableBuilder::string_pool() { |
| 136 | return &table_->string_pool; |
| 137 | } |
| 138 | |
| 139 | std::unique_ptr<ResourceTable> ResourceTableBuilder::Build() { |
| 140 | return std::move(table_); |
| 141 | } |
| 142 | |
| 143 | std::unique_ptr<Reference> BuildReference(const StringPiece& ref, const Maybe<ResourceId>& id) { |
| 144 | std::unique_ptr<Reference> reference = util::make_unique<Reference>(ParseNameOrDie(ref)); |
| 145 | reference->id = id; |
| 146 | return reference; |
| 147 | } |
| 148 | |
| 149 | std::unique_ptr<BinaryPrimitive> BuildPrimitive(uint8_t type, uint32_t data) { |
| 150 | android::Res_value value = {}; |
| 151 | value.size = sizeof(value); |
| 152 | value.dataType = type; |
| 153 | value.data = data; |
| 154 | return util::make_unique<BinaryPrimitive>(value); |
| 155 | } |
| 156 | |
| 157 | AttributeBuilder::AttributeBuilder(bool weak) : attr_(util::make_unique<Attribute>(weak)) { |
| 158 | attr_->type_mask = android::ResTable_map::TYPE_ANY; |
| 159 | } |
| 160 | |
| 161 | AttributeBuilder& AttributeBuilder::SetTypeMask(uint32_t typeMask) { |
| 162 | attr_->type_mask = typeMask; |
| 163 | return *this; |
| 164 | } |
| 165 | |
| 166 | AttributeBuilder& AttributeBuilder::AddItem(const StringPiece& name, uint32_t value) { |
| 167 | attr_->symbols.push_back( |
| 168 | Attribute::Symbol{Reference(ResourceName({}, ResourceType::kId, name)), value}); |
| 169 | return *this; |
| 170 | } |
| 171 | |
| 172 | std::unique_ptr<Attribute> AttributeBuilder::Build() { |
| 173 | return std::move(attr_); |
| 174 | } |
| 175 | |
| 176 | StyleBuilder& StyleBuilder::SetParent(const StringPiece& str) { |
| 177 | style_->parent = Reference(ParseNameOrDie(str)); |
| 178 | return *this; |
| 179 | } |
| 180 | |
| 181 | StyleBuilder& StyleBuilder::AddItem(const StringPiece& str, std::unique_ptr<Item> value) { |
| 182 | style_->entries.push_back(Style::Entry{Reference(ParseNameOrDie(str)), std::move(value)}); |
| 183 | return *this; |
| 184 | } |
| 185 | |
| 186 | StyleBuilder& StyleBuilder::AddItem(const StringPiece& str, const ResourceId& id, |
| 187 | std::unique_ptr<Item> value) { |
| 188 | AddItem(str, std::move(value)); |
| 189 | style_->entries.back().key.id = id; |
| 190 | return *this; |
| 191 | } |
| 192 | |
| 193 | std::unique_ptr<Style> StyleBuilder::Build() { |
| 194 | return std::move(style_); |
| 195 | } |
| 196 | |
| 197 | StyleableBuilder& StyleableBuilder::AddItem(const StringPiece& str, const Maybe<ResourceId>& id) { |
| 198 | styleable_->entries.push_back(Reference(ParseNameOrDie(str))); |
| 199 | styleable_->entries.back().id = id; |
| 200 | return *this; |
| 201 | } |
| 202 | |
| 203 | std::unique_ptr<Styleable> StyleableBuilder::Build() { |
| 204 | return std::move(styleable_); |
| 205 | } |
| 206 | |
| 207 | std::unique_ptr<xml::XmlResource> BuildXmlDom(const StringPiece& str) { |
| 208 | std::string input = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"; |
| 209 | input.append(str.data(), str.size()); |
| 210 | StringInputStream in(input); |
| 211 | StdErrDiagnostics diag; |
| 212 | 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] | 213 | CHECK(doc != nullptr && doc->root != nullptr) << "failed to parse inline XML string"; |
Adam Lesinski | efeb7af | 2017-08-02 14:57:43 -0700 | [diff] [blame] | 214 | return doc; |
| 215 | } |
| 216 | |
| 217 | std::unique_ptr<xml::XmlResource> BuildXmlDomForPackageName(IAaptContext* context, |
| 218 | const StringPiece& str) { |
| 219 | std::unique_ptr<xml::XmlResource> doc = BuildXmlDom(str); |
| 220 | doc->file.name.package = context->GetCompilationPackage(); |
| 221 | return doc; |
| 222 | } |
| 223 | |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 224 | ArtifactBuilder& ArtifactBuilder::SetName(const std::string& name) { |
Shane Farmer | cb6c3f9 | 2017-11-27 13:19:36 -0800 | [diff] [blame] | 225 | artifact_.name = name; |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 226 | return *this; |
| 227 | } |
| 228 | |
Shane Farmer | cb6c3f9 | 2017-11-27 13:19:36 -0800 | [diff] [blame] | 229 | ArtifactBuilder& ArtifactBuilder::AddAbi(configuration::Abi abi) { |
| 230 | artifact_.abis.push_back(abi); |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 231 | return *this; |
| 232 | } |
| 233 | |
Shane Farmer | cb6c3f9 | 2017-11-27 13:19:36 -0800 | [diff] [blame] | 234 | ArtifactBuilder& ArtifactBuilder::AddDensity(const ConfigDescription& density) { |
| 235 | artifact_.screen_densities.push_back(density); |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 236 | return *this; |
| 237 | } |
| 238 | |
Shane Farmer | cb6c3f9 | 2017-11-27 13:19:36 -0800 | [diff] [blame] | 239 | ArtifactBuilder& ArtifactBuilder::AddLocale(const ConfigDescription& locale) { |
| 240 | artifact_.locales.push_back(locale); |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 241 | return *this; |
| 242 | } |
| 243 | |
Shane Farmer | cb6c3f9 | 2017-11-27 13:19:36 -0800 | [diff] [blame] | 244 | ArtifactBuilder& ArtifactBuilder::SetAndroidSdk(int min_sdk) { |
| 245 | artifact_.android_sdk = {AndroidSdk::ForMinSdk(min_sdk)}; |
Shane Farmer | efe4539 | 2017-08-21 14:39:28 -0700 | [diff] [blame] | 246 | return *this; |
| 247 | } |
| 248 | |
Shane Farmer | cb6c3f9 | 2017-11-27 13:19:36 -0800 | [diff] [blame] | 249 | configuration::OutputArtifact ArtifactBuilder::Build() { |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 250 | return artifact_; |
| 251 | } |
| 252 | |
Adam Lesinski | efeb7af | 2017-08-02 14:57:43 -0700 | [diff] [blame] | 253 | } // namespace test |
| 254 | } // namespace aapt |