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 <memory> 21 22 #include "android-base/macros.h" 23 24 #include "Resource.h" 25 #include "ResourceTable.h" 26 #include "ResourceValues.h" 27 #include "process/IResourceTableConsumer.h" 28 #include "util/Maybe.h" 29 #include "xml/XmlDom.h" 30 31 namespace aapt { 32 namespace test { 33 34 class ResourceTableBuilder { 35 public: 36 ResourceTableBuilder() = default; 37 38 ResourceTableBuilder& SetPackageId(const android::StringPiece& package_name, uint8_t id); 39 ResourceTableBuilder& AddSimple(const android::StringPiece& name, const ResourceId& id = {}); 40 ResourceTableBuilder& AddSimple(const android::StringPiece& name, const ConfigDescription& config, 41 const ResourceId& id = {}); 42 ResourceTableBuilder& AddReference(const android::StringPiece& name, 43 const android::StringPiece& ref); 44 ResourceTableBuilder& AddReference(const android::StringPiece& name, const ResourceId& id, 45 const android::StringPiece& ref); 46 ResourceTableBuilder& AddString(const android::StringPiece& name, 47 const android::StringPiece& str); 48 ResourceTableBuilder& AddString(const android::StringPiece& name, const ResourceId& id, 49 const android::StringPiece& str); 50 ResourceTableBuilder& AddString(const android::StringPiece& name, const ResourceId& id, 51 const ConfigDescription& config, const android::StringPiece& str); 52 ResourceTableBuilder& AddFileReference(const android::StringPiece& name, 53 const android::StringPiece& path); 54 ResourceTableBuilder& AddFileReference(const android::StringPiece& name, const ResourceId& id, 55 const android::StringPiece& path); 56 ResourceTableBuilder& AddFileReference(const android::StringPiece& name, 57 const android::StringPiece& path, 58 const ConfigDescription& config); 59 ResourceTableBuilder& AddValue(const android::StringPiece& name, std::unique_ptr<Value> value); 60 ResourceTableBuilder& AddValue(const android::StringPiece& name, const ResourceId& id, 61 std::unique_ptr<Value> value); 62 ResourceTableBuilder& AddValue(const android::StringPiece& name, const ConfigDescription& config, 63 const ResourceId& id, std::unique_ptr<Value> value); 64 ResourceTableBuilder& SetSymbolState(const android::StringPiece& name, const ResourceId& id, 65 SymbolState state, bool allow_new = false); 66 67 StringPool* string_pool(); 68 std::unique_ptr<ResourceTable> Build(); 69 70 private: 71 DISALLOW_COPY_AND_ASSIGN(ResourceTableBuilder); 72 73 std::unique_ptr<ResourceTable> table_ = util::make_unique<ResourceTable>(); 74 }; 75 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); 79 80 template <typename T> 81 class ValueBuilder { 82 public: 83 template <typename... Args> ValueBuilder(Args &&...args)84 explicit ValueBuilder(Args&&... args) : value_(new T{std::forward<Args>(args)...}) { 85 } 86 87 template <typename... Args> SetSource(Args &&...args)88 ValueBuilder& SetSource(Args&&... args) { 89 value_->SetSource(Source{std::forward<Args>(args)...}); 90 return *this; 91 } 92 SetComment(const android::StringPiece & str)93 ValueBuilder& SetComment(const android::StringPiece& str) { 94 value_->SetComment(str); 95 return *this; 96 } 97 Build()98 std::unique_ptr<Value> Build() { 99 return std::move(value_); 100 } 101 102 private: 103 DISALLOW_COPY_AND_ASSIGN(ValueBuilder); 104 105 std::unique_ptr<Value> value_; 106 }; 107 108 class AttributeBuilder { 109 public: 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(); 114 115 private: 116 DISALLOW_COPY_AND_ASSIGN(AttributeBuilder); 117 118 std::unique_ptr<Attribute> attr_; 119 }; 120 121 class StyleBuilder { 122 public: 123 StyleBuilder() = default; 124 StyleBuilder& SetParent(const android::StringPiece& str); 125 StyleBuilder& AddItem(const android::StringPiece& str, std::unique_ptr<Item> value); 126 StyleBuilder& AddItem(const android::StringPiece& str, const ResourceId& id, 127 std::unique_ptr<Item> value); 128 std::unique_ptr<Style> Build(); 129 130 private: 131 DISALLOW_COPY_AND_ASSIGN(StyleBuilder); 132 133 std::unique_ptr<Style> style_ = util::make_unique<Style>(); 134 }; 135 136 class StyleableBuilder { 137 public: 138 StyleableBuilder() = default; 139 StyleableBuilder& AddItem(const android::StringPiece& str, const Maybe<ResourceId>& id = {}); 140 std::unique_ptr<Styleable> Build(); 141 142 private: 143 DISALLOW_COPY_AND_ASSIGN(StyleableBuilder); 144 145 std::unique_ptr<Styleable> styleable_ = util::make_unique<Styleable>(); 146 }; 147 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); 151 152 } // namespace test 153 } // namespace aapt 154 155 #endif /* AAPT_TEST_BUILDERS_H */ 156