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 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 20 | #include <memory> |
| 21 | |
| 22 | #include "android-base/logging.h" |
| 23 | #include "android-base/macros.h" |
| 24 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 25 | #include "ResourceTable.h" |
| 26 | #include "ResourceValues.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 27 | #include "test/Common.h" |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 28 | #include "util/Util.h" |
| 29 | #include "xml/XmlDom.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 30 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 31 | namespace aapt { |
| 32 | namespace test { |
| 33 | |
| 34 | class ResourceTableBuilder { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 35 | public: |
| 36 | ResourceTableBuilder() = default; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 37 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 38 | StringPool* string_pool() { return &table_->string_pool; } |
Adam Lesinski | a587065 | 2015-11-20 15:32:30 -0800 | [diff] [blame] | 39 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 40 | ResourceTableBuilder& SetPackageId(const StringPiece& package_name, |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 41 | uint8_t id) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 42 | ResourceTablePackage* package = table_->CreatePackage(package_name, id); |
| 43 | CHECK(package != nullptr); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 44 | return *this; |
| 45 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 46 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 47 | ResourceTableBuilder& AddSimple(const StringPiece& name, |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 48 | const ResourceId& id = {}) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 49 | return AddValue(name, id, util::make_unique<Id>()); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 50 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 51 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 52 | ResourceTableBuilder& AddSimple(const StringPiece& name, |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 53 | const ConfigDescription& config, |
| 54 | const ResourceId& id = {}) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 55 | return AddValue(name, config, id, util::make_unique<Id>()); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 56 | } |
Adam Lesinski | fb6312f | 2016-06-28 14:40:32 -0700 | [diff] [blame] | 57 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 58 | ResourceTableBuilder& AddReference(const StringPiece& name, |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 59 | const StringPiece& ref) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 60 | return AddReference(name, {}, ref); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 61 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 62 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 63 | ResourceTableBuilder& AddReference(const StringPiece& name, |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 64 | const ResourceId& id, |
| 65 | const StringPiece& ref) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 66 | return AddValue(name, id, |
| 67 | util::make_unique<Reference>(ParseNameOrDie(ref))); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 68 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 69 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 70 | ResourceTableBuilder& AddString(const StringPiece& name, |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 71 | const StringPiece& str) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 72 | return AddString(name, {}, str); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 73 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 74 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 75 | ResourceTableBuilder& AddString(const StringPiece& name, const ResourceId& id, |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 76 | const StringPiece& str) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 77 | return AddValue( |
| 78 | name, id, util::make_unique<String>(table_->string_pool.MakeRef(str))); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 79 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 80 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 81 | ResourceTableBuilder& AddString(const StringPiece& name, const ResourceId& id, |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 82 | const ConfigDescription& config, |
| 83 | const StringPiece& str) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 84 | return AddValue(name, config, id, util::make_unique<String>( |
| 85 | table_->string_pool.MakeRef(str))); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 86 | } |
Adam Lesinski | 393b5f0 | 2015-12-17 13:03:11 -0800 | [diff] [blame] | 87 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 88 | ResourceTableBuilder& AddFileReference(const StringPiece& name, |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 89 | const StringPiece& path) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 90 | return AddFileReference(name, {}, path); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 91 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 92 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 93 | ResourceTableBuilder& AddFileReference(const StringPiece& name, |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 94 | const ResourceId& id, |
| 95 | const StringPiece& path) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 96 | return AddValue(name, id, util::make_unique<FileReference>( |
| 97 | table_->string_pool.MakeRef(path))); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 98 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 99 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 100 | ResourceTableBuilder& AddFileReference(const StringPiece& name, |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 101 | const StringPiece& path, |
| 102 | const ConfigDescription& config) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 103 | return AddValue(name, config, {}, util::make_unique<FileReference>( |
| 104 | table_->string_pool.MakeRef(path))); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 105 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 106 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 107 | ResourceTableBuilder& AddValue(const StringPiece& name, |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 108 | std::unique_ptr<Value> value) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 109 | return AddValue(name, {}, std::move(value)); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 110 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 111 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 112 | ResourceTableBuilder& AddValue(const StringPiece& name, const ResourceId& id, |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 113 | std::unique_ptr<Value> value) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 114 | return AddValue(name, {}, id, std::move(value)); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 115 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 116 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 117 | ResourceTableBuilder& AddValue(const StringPiece& name, |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 118 | const ConfigDescription& config, |
| 119 | const ResourceId& id, |
| 120 | std::unique_ptr<Value> value) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 121 | ResourceName res_name = ParseNameOrDie(name); |
| 122 | CHECK(table_->AddResourceAllowMangled(res_name, id, config, {}, |
| 123 | std::move(value), &diagnostics_)); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 124 | return *this; |
| 125 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 126 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 127 | ResourceTableBuilder& SetSymbolState(const StringPiece& name, |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 128 | const ResourceId& id, |
| 129 | SymbolState state) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 130 | ResourceName res_name = ParseNameOrDie(name); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 131 | Symbol symbol; |
| 132 | symbol.state = state; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 133 | CHECK(table_->SetSymbolStateAllowMangled(res_name, id, symbol, |
| 134 | &diagnostics_)); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 135 | return *this; |
| 136 | } |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame] | 137 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 138 | std::unique_ptr<ResourceTable> Build() { return std::move(table_); } |
| 139 | |
| 140 | private: |
| 141 | DISALLOW_COPY_AND_ASSIGN(ResourceTableBuilder); |
| 142 | |
| 143 | DummyDiagnosticsImpl diagnostics_; |
| 144 | std::unique_ptr<ResourceTable> table_ = util::make_unique<ResourceTable>(); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 145 | }; |
| 146 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 147 | inline std::unique_ptr<Reference> BuildReference( |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 148 | const StringPiece& ref, const Maybe<ResourceId>& id = {}) { |
| 149 | std::unique_ptr<Reference> reference = |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 150 | util::make_unique<Reference>(ParseNameOrDie(ref)); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 151 | reference->id = id; |
| 152 | return reference; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 153 | } |
| 154 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 155 | inline std::unique_ptr<BinaryPrimitive> BuildPrimitive(uint8_t type, |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 156 | uint32_t data) { |
| 157 | android::Res_value value = {}; |
| 158 | value.size = sizeof(value); |
| 159 | value.dataType = type; |
| 160 | value.data = data; |
| 161 | return util::make_unique<BinaryPrimitive>(value); |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 162 | } |
| 163 | |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame] | 164 | template <typename T> |
| 165 | class ValueBuilder { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 166 | public: |
| 167 | template <typename... Args> |
| 168 | explicit ValueBuilder(Args&&... args) |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 169 | : value_(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> |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 172 | ValueBuilder& SetSource(Args&&... args) { |
| 173 | value_->SetSource(Source{std::forward<Args>(args)...}); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 174 | return *this; |
| 175 | } |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame] | 176 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 177 | ValueBuilder& SetComment(const StringPiece& str) { |
| 178 | value_->SetComment(str); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 179 | return *this; |
| 180 | } |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame] | 181 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 182 | std::unique_ptr<Value> Build() { return std::move(value_); } |
| 183 | |
| 184 | private: |
| 185 | DISALLOW_COPY_AND_ASSIGN(ValueBuilder); |
| 186 | |
| 187 | std::unique_ptr<Value> value_; |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame] | 188 | }; |
| 189 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 190 | class AttributeBuilder { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 191 | public: |
| 192 | explicit AttributeBuilder(bool weak = false) |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 193 | : attr_(util::make_unique<Attribute>(weak)) { |
| 194 | attr_->type_mask = android::ResTable_map::TYPE_ANY; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 195 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 196 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 197 | AttributeBuilder& SetTypeMask(uint32_t typeMask) { |
| 198 | attr_->type_mask = typeMask; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 199 | return *this; |
| 200 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 201 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 202 | AttributeBuilder& AddItem(const StringPiece& name, uint32_t value) { |
| 203 | attr_->symbols.push_back(Attribute::Symbol{ |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 204 | Reference(ResourceName({}, ResourceType::kId, name)), value}); |
| 205 | return *this; |
| 206 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 207 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 208 | std::unique_ptr<Attribute> Build() { return std::move(attr_); } |
| 209 | |
| 210 | private: |
| 211 | DISALLOW_COPY_AND_ASSIGN(AttributeBuilder); |
| 212 | |
| 213 | std::unique_ptr<Attribute> attr_; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 214 | }; |
| 215 | |
| 216 | class StyleBuilder { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 217 | public: |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 218 | StyleBuilder() = default; |
| 219 | |
| 220 | StyleBuilder& SetParent(const StringPiece& str) { |
| 221 | style_->parent = Reference(ParseNameOrDie(str)); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 222 | return *this; |
| 223 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 224 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 225 | StyleBuilder& AddItem(const StringPiece& str, std::unique_ptr<Item> value) { |
| 226 | style_->entries.push_back( |
| 227 | Style::Entry{Reference(ParseNameOrDie(str)), std::move(value)}); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 228 | return *this; |
| 229 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 230 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 231 | StyleBuilder& AddItem(const StringPiece& str, const ResourceId& id, |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 232 | std::unique_ptr<Item> value) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 233 | AddItem(str, std::move(value)); |
| 234 | style_->entries.back().key.id = id; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 235 | return *this; |
| 236 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 237 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 238 | std::unique_ptr<Style> Build() { return std::move(style_); } |
| 239 | |
| 240 | private: |
| 241 | DISALLOW_COPY_AND_ASSIGN(StyleBuilder); |
| 242 | |
| 243 | std::unique_ptr<Style> style_ = util::make_unique<Style>(); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 244 | }; |
| 245 | |
| 246 | class StyleableBuilder { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 247 | public: |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 248 | StyleableBuilder() = default; |
| 249 | |
| 250 | StyleableBuilder& AddItem(const StringPiece& str, |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 251 | const Maybe<ResourceId>& id = {}) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 252 | styleable_->entries.push_back(Reference(ParseNameOrDie(str))); |
| 253 | styleable_->entries.back().id = id; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 254 | return *this; |
| 255 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 256 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 257 | std::unique_ptr<Styleable> Build() { return std::move(styleable_); } |
| 258 | |
| 259 | private: |
| 260 | DISALLOW_COPY_AND_ASSIGN(StyleableBuilder); |
| 261 | |
| 262 | std::unique_ptr<Styleable> styleable_ = util::make_unique<Styleable>(); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 263 | }; |
| 264 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 265 | inline std::unique_ptr<xml::XmlResource> BuildXmlDom(const StringPiece& str) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 266 | std::stringstream in; |
| 267 | in << "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" << str; |
| 268 | StdErrDiagnostics diag; |
| 269 | std::unique_ptr<xml::XmlResource> doc = |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 270 | xml::Inflate(&in, &diag, Source("test.xml")); |
| 271 | CHECK(doc != nullptr) << "failed to parse inline XML string"; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 272 | return doc; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 273 | } |
| 274 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 275 | inline std::unique_ptr<xml::XmlResource> BuildXmlDomForPackageName( |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 276 | IAaptContext* context, const StringPiece& str) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 277 | std::unique_ptr<xml::XmlResource> doc = BuildXmlDom(str); |
| 278 | doc->file.name.package = context->GetCompilationPackage(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 279 | return doc; |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 280 | } |
| 281 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 282 | } // namespace test |
| 283 | } // namespace aapt |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 284 | |
| 285 | #endif /* AAPT_TEST_BUILDERS_H */ |