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