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