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 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 22 | #include "android-base/macros.h" |
| 23 | |
Adam Lesinski | 5b54ca2 | 2017-08-02 14:57:43 -0700 | [diff] [blame] | 24 | #include "Resource.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 25 | #include "ResourceTable.h" |
| 26 | #include "ResourceValues.h" |
Adam Lesinski | 5b54ca2 | 2017-08-02 14:57:43 -0700 | [diff] [blame] | 27 | #include "process/IResourceTableConsumer.h" |
| 28 | #include "util/Maybe.h" |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 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 | 5b54ca2 | 2017-08-02 14:57:43 -0700 | [diff] [blame] | 38 | ResourceTableBuilder& SetPackageId(const android::StringPiece& package_name, uint8_t id); |
| 39 | ResourceTableBuilder& AddSimple(const android::StringPiece& name, const ResourceId& id = {}); |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 40 | ResourceTableBuilder& AddSimple(const android::StringPiece& name, const ConfigDescription& config, |
Adam Lesinski | 5b54ca2 | 2017-08-02 14:57:43 -0700 | [diff] [blame] | 41 | const ResourceId& id = {}); |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 42 | ResourceTableBuilder& AddReference(const android::StringPiece& name, |
Adam Lesinski | 5b54ca2 | 2017-08-02 14:57:43 -0700 | [diff] [blame] | 43 | const android::StringPiece& ref); |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 44 | ResourceTableBuilder& AddReference(const android::StringPiece& name, const ResourceId& id, |
Adam Lesinski | 5b54ca2 | 2017-08-02 14:57:43 -0700 | [diff] [blame] | 45 | const android::StringPiece& ref); |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 46 | ResourceTableBuilder& AddString(const android::StringPiece& name, |
Adam Lesinski | 5b54ca2 | 2017-08-02 14:57:43 -0700 | [diff] [blame] | 47 | const android::StringPiece& str); |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 48 | ResourceTableBuilder& AddString(const android::StringPiece& name, const ResourceId& id, |
Adam Lesinski | 5b54ca2 | 2017-08-02 14:57:43 -0700 | [diff] [blame] | 49 | const android::StringPiece& str); |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 50 | ResourceTableBuilder& AddString(const android::StringPiece& name, const ResourceId& id, |
Adam Lesinski | 5b54ca2 | 2017-08-02 14:57:43 -0700 | [diff] [blame] | 51 | const ConfigDescription& config, const android::StringPiece& str); |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 52 | ResourceTableBuilder& AddFileReference(const android::StringPiece& name, |
Adam Lesinski | 5b54ca2 | 2017-08-02 14:57:43 -0700 | [diff] [blame] | 53 | const android::StringPiece& path); |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 54 | ResourceTableBuilder& AddFileReference(const android::StringPiece& name, const ResourceId& id, |
Adam Lesinski | 5b54ca2 | 2017-08-02 14:57:43 -0700 | [diff] [blame] | 55 | const android::StringPiece& path); |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 56 | ResourceTableBuilder& AddFileReference(const android::StringPiece& name, |
| 57 | const android::StringPiece& path, |
Adam Lesinski | 5b54ca2 | 2017-08-02 14:57:43 -0700 | [diff] [blame] | 58 | const ConfigDescription& config); |
| 59 | ResourceTableBuilder& AddValue(const android::StringPiece& name, std::unique_ptr<Value> value); |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 60 | ResourceTableBuilder& AddValue(const android::StringPiece& name, const ResourceId& id, |
Adam Lesinski | 5b54ca2 | 2017-08-02 14:57:43 -0700 | [diff] [blame] | 61 | std::unique_ptr<Value> value); |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 62 | ResourceTableBuilder& AddValue(const android::StringPiece& name, const ConfigDescription& config, |
Adam Lesinski | 5b54ca2 | 2017-08-02 14:57:43 -0700 | [diff] [blame] | 63 | const ResourceId& id, std::unique_ptr<Value> value); |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 64 | ResourceTableBuilder& SetSymbolState(const android::StringPiece& name, const ResourceId& id, |
Adam Lesinski | 5b54ca2 | 2017-08-02 14:57:43 -0700 | [diff] [blame] | 65 | SymbolState state, bool allow_new = false); |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame] | 66 | |
Adam Lesinski | 5b54ca2 | 2017-08-02 14:57:43 -0700 | [diff] [blame] | 67 | StringPool* string_pool(); |
| 68 | std::unique_ptr<ResourceTable> Build(); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 69 | |
| 70 | private: |
| 71 | DISALLOW_COPY_AND_ASSIGN(ResourceTableBuilder); |
| 72 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 73 | std::unique_ptr<ResourceTable> table_ = util::make_unique<ResourceTable>(); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 74 | }; |
| 75 | |
Adam Lesinski | 5b54ca2 | 2017-08-02 14:57:43 -0700 | [diff] [blame] | 76 | std::unique_ptr<Reference> BuildReference(const android::StringPiece& ref, |
| 77 | const Maybe<ResourceId>& id = {}); |
| 78 | std::unique_ptr<BinaryPrimitive> BuildPrimitive(uint8_t type, uint32_t data); |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 79 | |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame] | 80 | template <typename T> |
| 81 | class ValueBuilder { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 82 | public: |
| 83 | template <typename... Args> |
Adam Lesinski | 5b54ca2 | 2017-08-02 14:57:43 -0700 | [diff] [blame] | 84 | explicit ValueBuilder(Args&&... args) : value_(new T{std::forward<Args>(args)...}) { |
| 85 | } |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame] | 86 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 87 | template <typename... Args> |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 88 | ValueBuilder& SetSource(Args&&... args) { |
| 89 | value_->SetSource(Source{std::forward<Args>(args)...}); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 90 | return *this; |
| 91 | } |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame] | 92 | |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 93 | ValueBuilder& SetComment(const android::StringPiece& str) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 94 | value_->SetComment(str); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 95 | return *this; |
| 96 | } |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame] | 97 | |
Adam Lesinski | 5b54ca2 | 2017-08-02 14:57:43 -0700 | [diff] [blame] | 98 | std::unique_ptr<Value> Build() { |
| 99 | return std::move(value_); |
| 100 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 101 | |
| 102 | private: |
| 103 | DISALLOW_COPY_AND_ASSIGN(ValueBuilder); |
| 104 | |
| 105 | std::unique_ptr<Value> value_; |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame] | 106 | }; |
| 107 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 108 | class AttributeBuilder { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 109 | public: |
Adam Lesinski | 5b54ca2 | 2017-08-02 14:57:43 -0700 | [diff] [blame] | 110 | explicit AttributeBuilder(bool weak = false); |
| 111 | AttributeBuilder& SetTypeMask(uint32_t typeMask); |
| 112 | AttributeBuilder& AddItem(const android::StringPiece& name, uint32_t value); |
| 113 | std::unique_ptr<Attribute> Build(); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 114 | |
| 115 | private: |
| 116 | DISALLOW_COPY_AND_ASSIGN(AttributeBuilder); |
| 117 | |
| 118 | std::unique_ptr<Attribute> attr_; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 119 | }; |
| 120 | |
| 121 | class StyleBuilder { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 122 | public: |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 123 | StyleBuilder() = default; |
Adam Lesinski | 5b54ca2 | 2017-08-02 14:57:43 -0700 | [diff] [blame] | 124 | StyleBuilder& SetParent(const android::StringPiece& str); |
| 125 | StyleBuilder& AddItem(const android::StringPiece& str, std::unique_ptr<Item> value); |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 126 | StyleBuilder& AddItem(const android::StringPiece& str, const ResourceId& id, |
Adam Lesinski | 5b54ca2 | 2017-08-02 14:57:43 -0700 | [diff] [blame] | 127 | std::unique_ptr<Item> value); |
| 128 | std::unique_ptr<Style> Build(); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 129 | |
| 130 | private: |
| 131 | DISALLOW_COPY_AND_ASSIGN(StyleBuilder); |
| 132 | |
| 133 | std::unique_ptr<Style> style_ = util::make_unique<Style>(); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 134 | }; |
| 135 | |
| 136 | class StyleableBuilder { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 137 | public: |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 138 | StyleableBuilder() = default; |
Adam Lesinski | 5b54ca2 | 2017-08-02 14:57:43 -0700 | [diff] [blame] | 139 | StyleableBuilder& AddItem(const android::StringPiece& str, const Maybe<ResourceId>& id = {}); |
| 140 | std::unique_ptr<Styleable> Build(); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 141 | |
| 142 | private: |
| 143 | DISALLOW_COPY_AND_ASSIGN(StyleableBuilder); |
| 144 | |
| 145 | std::unique_ptr<Styleable> styleable_ = util::make_unique<Styleable>(); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 146 | }; |
| 147 | |
Adam Lesinski | 5b54ca2 | 2017-08-02 14:57:43 -0700 | [diff] [blame] | 148 | std::unique_ptr<xml::XmlResource> BuildXmlDom(const android::StringPiece& str); |
| 149 | std::unique_ptr<xml::XmlResource> BuildXmlDomForPackageName(IAaptContext* context, |
| 150 | const android::StringPiece& str); |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 151 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 152 | } // namespace test |
| 153 | } // namespace aapt |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 154 | |
| 155 | #endif /* AAPT_TEST_BUILDERS_H */ |