Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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 | #ifndef AAPT_TEST_BUILDERS_H |
| 18 | #define AAPT_TEST_BUILDERS_H |
| 19 | |
| 20 | #include "ResourceTable.h" |
| 21 | #include "ResourceValues.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 22 | #include "test/Common.h" |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 23 | #include "util/Util.h" |
| 24 | #include "xml/XmlDom.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 25 | |
| 26 | #include <memory> |
| 27 | |
| 28 | namespace aapt { |
| 29 | namespace test { |
| 30 | |
| 31 | class ResourceTableBuilder { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 32 | private: |
| 33 | DummyDiagnosticsImpl mDiagnostics; |
| 34 | std::unique_ptr<ResourceTable> mTable = util::make_unique<ResourceTable>(); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 35 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 36 | public: |
| 37 | ResourceTableBuilder() = default; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 38 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 39 | StringPool* getStringPool() { return &mTable->stringPool; } |
Adam Lesinski | a587065 | 2015-11-20 15:32:30 -0800 | [diff] [blame] | 40 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 41 | ResourceTableBuilder& setPackageId(const StringPiece& packageName, |
| 42 | uint8_t id) { |
| 43 | ResourceTablePackage* package = mTable->createPackage(packageName, id); |
| 44 | assert(package); |
| 45 | return *this; |
| 46 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 47 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 48 | ResourceTableBuilder& addSimple(const StringPiece& name, |
| 49 | const ResourceId& id = {}) { |
| 50 | return addValue(name, id, util::make_unique<Id>()); |
| 51 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 52 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 53 | ResourceTableBuilder& addSimple(const StringPiece& name, |
| 54 | const ConfigDescription& config, |
| 55 | const ResourceId& id = {}) { |
| 56 | return addValue(name, config, id, util::make_unique<Id>()); |
| 57 | } |
Adam Lesinski | fb6312f | 2016-06-28 14:40:32 -0700 | [diff] [blame] | 58 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 59 | ResourceTableBuilder& addReference(const StringPiece& name, |
| 60 | const StringPiece& ref) { |
| 61 | return addReference(name, {}, ref); |
| 62 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 63 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 64 | ResourceTableBuilder& addReference(const StringPiece& name, |
| 65 | const ResourceId& id, |
| 66 | const StringPiece& ref) { |
| 67 | return addValue(name, id, |
| 68 | util::make_unique<Reference>(parseNameOrDie(ref))); |
| 69 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 70 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 71 | ResourceTableBuilder& addString(const StringPiece& name, |
| 72 | const StringPiece& str) { |
| 73 | return addString(name, {}, str); |
| 74 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 75 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 76 | ResourceTableBuilder& addString(const StringPiece& name, const ResourceId& id, |
| 77 | const StringPiece& str) { |
| 78 | return addValue(name, id, |
| 79 | util::make_unique<String>(mTable->stringPool.makeRef(str))); |
| 80 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 81 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 82 | ResourceTableBuilder& addString(const StringPiece& name, const ResourceId& id, |
| 83 | const ConfigDescription& config, |
| 84 | const StringPiece& str) { |
| 85 | return addValue(name, config, id, |
| 86 | util::make_unique<String>(mTable->stringPool.makeRef(str))); |
| 87 | } |
Adam Lesinski | 393b5f0 | 2015-12-17 13:03:11 -0800 | [diff] [blame] | 88 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 89 | ResourceTableBuilder& addFileReference(const StringPiece& name, |
| 90 | const StringPiece& path) { |
| 91 | return addFileReference(name, {}, path); |
| 92 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 93 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 94 | ResourceTableBuilder& addFileReference(const StringPiece& name, |
| 95 | const ResourceId& id, |
| 96 | const StringPiece& path) { |
| 97 | return addValue(name, id, util::make_unique<FileReference>( |
| 98 | mTable->stringPool.makeRef(path))); |
| 99 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 100 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 101 | ResourceTableBuilder& addFileReference(const StringPiece& name, |
| 102 | const StringPiece& path, |
| 103 | const ConfigDescription& config) { |
| 104 | return addValue(name, config, {}, util::make_unique<FileReference>( |
| 105 | mTable->stringPool.makeRef(path))); |
| 106 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 107 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 108 | ResourceTableBuilder& addValue(const StringPiece& name, |
| 109 | std::unique_ptr<Value> value) { |
| 110 | return addValue(name, {}, std::move(value)); |
| 111 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 112 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 113 | ResourceTableBuilder& addValue(const StringPiece& name, const ResourceId& id, |
| 114 | std::unique_ptr<Value> value) { |
| 115 | return addValue(name, {}, id, std::move(value)); |
| 116 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 117 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 118 | ResourceTableBuilder& addValue(const StringPiece& name, |
| 119 | const ConfigDescription& config, |
| 120 | const ResourceId& id, |
| 121 | std::unique_ptr<Value> value) { |
| 122 | ResourceName resName = parseNameOrDie(name); |
| 123 | bool result = mTable->addResourceAllowMangled( |
| 124 | resName, id, config, {}, std::move(value), &mDiagnostics); |
| 125 | assert(result); |
| 126 | return *this; |
| 127 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 128 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 129 | ResourceTableBuilder& setSymbolState(const StringPiece& name, |
| 130 | const ResourceId& id, |
| 131 | SymbolState state) { |
| 132 | ResourceName resName = parseNameOrDie(name); |
| 133 | Symbol symbol; |
| 134 | symbol.state = state; |
| 135 | bool result = |
| 136 | mTable->setSymbolStateAllowMangled(resName, id, symbol, &mDiagnostics); |
| 137 | assert(result); |
| 138 | return *this; |
| 139 | } |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame] | 140 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 141 | std::unique_ptr<ResourceTable> build() { return std::move(mTable); } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 142 | }; |
| 143 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 144 | inline std::unique_ptr<Reference> buildReference( |
| 145 | const StringPiece& ref, const Maybe<ResourceId>& id = {}) { |
| 146 | std::unique_ptr<Reference> reference = |
| 147 | util::make_unique<Reference>(parseNameOrDie(ref)); |
| 148 | reference->id = id; |
| 149 | return reference; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 150 | } |
| 151 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 152 | inline std::unique_ptr<BinaryPrimitive> buildPrimitive(uint8_t type, |
| 153 | uint32_t data) { |
| 154 | android::Res_value value = {}; |
| 155 | value.size = sizeof(value); |
| 156 | value.dataType = type; |
| 157 | value.data = data; |
| 158 | return util::make_unique<BinaryPrimitive>(value); |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 159 | } |
| 160 | |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame] | 161 | template <typename T> |
| 162 | class ValueBuilder { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 163 | private: |
| 164 | std::unique_ptr<Value> mValue; |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame] | 165 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 166 | public: |
| 167 | template <typename... Args> |
| 168 | explicit ValueBuilder(Args&&... args) |
| 169 | : mValue(new T{std::forward<Args>(args)...}) {} |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame] | 170 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 171 | template <typename... Args> |
| 172 | ValueBuilder& setSource(Args&&... args) { |
| 173 | mValue->setSource(Source{std::forward<Args>(args)...}); |
| 174 | return *this; |
| 175 | } |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame] | 176 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 177 | ValueBuilder& setComment(const StringPiece& str) { |
| 178 | mValue->setComment(str); |
| 179 | return *this; |
| 180 | } |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame] | 181 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 182 | std::unique_ptr<Value> build() { return std::move(mValue); } |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame] | 183 | }; |
| 184 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 185 | class AttributeBuilder { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 186 | private: |
| 187 | std::unique_ptr<Attribute> mAttr; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 188 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 189 | public: |
| 190 | explicit AttributeBuilder(bool weak = false) |
| 191 | : mAttr(util::make_unique<Attribute>(weak)) { |
| 192 | mAttr->typeMask = android::ResTable_map::TYPE_ANY; |
| 193 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 194 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 195 | AttributeBuilder& setTypeMask(uint32_t typeMask) { |
| 196 | mAttr->typeMask = typeMask; |
| 197 | return *this; |
| 198 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 199 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 200 | AttributeBuilder& addItem(const StringPiece& name, uint32_t value) { |
| 201 | mAttr->symbols.push_back(Attribute::Symbol{ |
| 202 | Reference(ResourceName({}, ResourceType::kId, name)), value}); |
| 203 | return *this; |
| 204 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 205 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 206 | std::unique_ptr<Attribute> build() { return std::move(mAttr); } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 207 | }; |
| 208 | |
| 209 | class StyleBuilder { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 210 | private: |
| 211 | std::unique_ptr<Style> mStyle = util::make_unique<Style>(); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 212 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 213 | public: |
| 214 | StyleBuilder& setParent(const StringPiece& str) { |
| 215 | mStyle->parent = Reference(parseNameOrDie(str)); |
| 216 | return *this; |
| 217 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 218 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 219 | StyleBuilder& addItem(const StringPiece& str, std::unique_ptr<Item> value) { |
| 220 | mStyle->entries.push_back( |
| 221 | Style::Entry{Reference(parseNameOrDie(str)), std::move(value)}); |
| 222 | return *this; |
| 223 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 224 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 225 | StyleBuilder& addItem(const StringPiece& str, const ResourceId& id, |
| 226 | std::unique_ptr<Item> value) { |
| 227 | addItem(str, std::move(value)); |
| 228 | mStyle->entries.back().key.id = id; |
| 229 | return *this; |
| 230 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 231 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 232 | std::unique_ptr<Style> build() { return std::move(mStyle); } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 233 | }; |
| 234 | |
| 235 | class StyleableBuilder { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 236 | private: |
| 237 | std::unique_ptr<Styleable> mStyleable = util::make_unique<Styleable>(); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 238 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 239 | public: |
| 240 | StyleableBuilder& addItem(const StringPiece& str, |
| 241 | const Maybe<ResourceId>& id = {}) { |
| 242 | mStyleable->entries.push_back(Reference(parseNameOrDie(str))); |
| 243 | mStyleable->entries.back().id = id; |
| 244 | return *this; |
| 245 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 246 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 247 | std::unique_ptr<Styleable> build() { return std::move(mStyleable); } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 248 | }; |
| 249 | |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 250 | inline std::unique_ptr<xml::XmlResource> buildXmlDom(const StringPiece& str) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 251 | std::stringstream in; |
| 252 | in << "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" << str; |
| 253 | StdErrDiagnostics diag; |
| 254 | std::unique_ptr<xml::XmlResource> doc = |
| 255 | xml::inflate(&in, &diag, Source("test.xml")); |
| 256 | assert(doc); |
| 257 | return doc; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 258 | } |
| 259 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 260 | inline std::unique_ptr<xml::XmlResource> buildXmlDomForPackageName( |
| 261 | IAaptContext* context, const StringPiece& str) { |
| 262 | std::unique_ptr<xml::XmlResource> doc = buildXmlDom(str); |
| 263 | doc->file.name.package = context->getCompilationPackage(); |
| 264 | return doc; |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 265 | } |
| 266 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 267 | } // namespace test |
| 268 | } // namespace aapt |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 269 | |
| 270 | #endif /* AAPT_TEST_BUILDERS_H */ |