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