blob: 6888cf32fab814dbfb0778bf58bead2e6a11fd14 [file] [log] [blame]
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001/*
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 Lesinskice5e56e2016-10-21 17:56:45 -070020#include <memory>
21
22#include "android-base/logging.h"
23#include "android-base/macros.h"
24
Adam Lesinski1ab598f2015-08-14 14:26:04 -070025#include "ResourceTable.h"
26#include "ResourceValues.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070027#include "test/Common.h"
Adam Lesinski467f1712015-11-16 17:35:44 -080028#include "util/Util.h"
29#include "xml/XmlDom.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070030
Adam Lesinski1ab598f2015-08-14 14:26:04 -070031namespace aapt {
32namespace test {
33
34class ResourceTableBuilder {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070035 public:
36 ResourceTableBuilder() = default;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070037
Adam Lesinskice5e56e2016-10-21 17:56:45 -070038 StringPool* string_pool() { return &table_->string_pool; }
Adam Lesinskia5870652015-11-20 15:32:30 -080039
Adam Lesinskid5083f62017-01-16 15:07:21 -080040 ResourceTableBuilder& SetPackageId(const android::StringPiece& package_name, uint8_t id) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070041 ResourceTablePackage* package = table_->CreatePackage(package_name, id);
42 CHECK(package != nullptr);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070043 return *this;
44 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -070045
Adam Lesinskid5083f62017-01-16 15:07:21 -080046 ResourceTableBuilder& AddSimple(const android::StringPiece& name, const ResourceId& id = {}) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070047 return AddValue(name, id, util::make_unique<Id>());
Adam Lesinskicacb28f2016-10-19 12:18:14 -070048 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -070049
Adam Lesinskid5083f62017-01-16 15:07:21 -080050 ResourceTableBuilder& AddSimple(const android::StringPiece& name, const ConfigDescription& config,
Adam Lesinskicacb28f2016-10-19 12:18:14 -070051 const ResourceId& id = {}) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070052 return AddValue(name, config, id, util::make_unique<Id>());
Adam Lesinskicacb28f2016-10-19 12:18:14 -070053 }
Adam Lesinskifb6312f2016-06-28 14:40:32 -070054
Adam Lesinskid5083f62017-01-16 15:07:21 -080055 ResourceTableBuilder& AddReference(const android::StringPiece& name,
56 const android::StringPiece& ref) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070057 return AddReference(name, {}, ref);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070058 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -070059
Adam Lesinskid5083f62017-01-16 15:07:21 -080060 ResourceTableBuilder& AddReference(const android::StringPiece& name, const ResourceId& id,
61 const android::StringPiece& ref) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070062 return AddValue(name, id,
63 util::make_unique<Reference>(ParseNameOrDie(ref)));
Adam Lesinskicacb28f2016-10-19 12:18:14 -070064 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -070065
Adam Lesinskid5083f62017-01-16 15:07:21 -080066 ResourceTableBuilder& AddString(const android::StringPiece& name,
67 const android::StringPiece& str) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070068 return AddString(name, {}, str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070069 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -070070
Adam Lesinskid5083f62017-01-16 15:07:21 -080071 ResourceTableBuilder& AddString(const android::StringPiece& name, const ResourceId& id,
72 const android::StringPiece& str) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070073 return AddValue(
74 name, id, util::make_unique<String>(table_->string_pool.MakeRef(str)));
Adam Lesinskicacb28f2016-10-19 12:18:14 -070075 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -070076
Adam Lesinskid5083f62017-01-16 15:07:21 -080077 ResourceTableBuilder& AddString(const android::StringPiece& name, const ResourceId& id,
Adam Lesinskicacb28f2016-10-19 12:18:14 -070078 const ConfigDescription& config,
Adam Lesinskid5083f62017-01-16 15:07:21 -080079 const android::StringPiece& str) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070080 return AddValue(name, config, id, util::make_unique<String>(
81 table_->string_pool.MakeRef(str)));
Adam Lesinskicacb28f2016-10-19 12:18:14 -070082 }
Adam Lesinski393b5f02015-12-17 13:03:11 -080083
Adam Lesinskid5083f62017-01-16 15:07:21 -080084 ResourceTableBuilder& AddFileReference(const android::StringPiece& name,
85 const android::StringPiece& path) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070086 return AddFileReference(name, {}, path);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070087 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -070088
Adam Lesinskid5083f62017-01-16 15:07:21 -080089 ResourceTableBuilder& AddFileReference(const android::StringPiece& name, const ResourceId& id,
90 const android::StringPiece& path) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070091 return AddValue(name, id, util::make_unique<FileReference>(
92 table_->string_pool.MakeRef(path)));
Adam Lesinskicacb28f2016-10-19 12:18:14 -070093 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -070094
Adam Lesinskid5083f62017-01-16 15:07:21 -080095 ResourceTableBuilder& AddFileReference(const android::StringPiece& name,
96 const android::StringPiece& path,
Adam Lesinskicacb28f2016-10-19 12:18:14 -070097 const ConfigDescription& config) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070098 return AddValue(name, config, {}, util::make_unique<FileReference>(
99 table_->string_pool.MakeRef(path)));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700100 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700101
Adam Lesinskid5083f62017-01-16 15:07:21 -0800102 ResourceTableBuilder& AddValue(const android::StringPiece& name, std::unique_ptr<Value> value) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700103 return AddValue(name, {}, std::move(value));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700104 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700105
Adam Lesinskid5083f62017-01-16 15:07:21 -0800106 ResourceTableBuilder& AddValue(const android::StringPiece& name, const ResourceId& id,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700107 std::unique_ptr<Value> value) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700108 return AddValue(name, {}, id, std::move(value));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700109 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700110
Adam Lesinskid5083f62017-01-16 15:07:21 -0800111 ResourceTableBuilder& AddValue(const android::StringPiece& name, const ConfigDescription& config,
112 const ResourceId& id, std::unique_ptr<Value> value) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700113 ResourceName res_name = ParseNameOrDie(name);
114 CHECK(table_->AddResourceAllowMangled(res_name, id, config, {},
115 std::move(value), &diagnostics_));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700116 return *this;
117 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700118
Adam Lesinskid5083f62017-01-16 15:07:21 -0800119 ResourceTableBuilder& SetSymbolState(const android::StringPiece& name, const ResourceId& id,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700120 SymbolState state) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700121 ResourceName res_name = ParseNameOrDie(name);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700122 Symbol symbol;
123 symbol.state = state;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700124 CHECK(table_->SetSymbolStateAllowMangled(res_name, id, symbol,
125 &diagnostics_));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700126 return *this;
127 }
Adam Lesinskie78fd612015-10-22 12:48:43 -0700128
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700129 std::unique_ptr<ResourceTable> Build() { return std::move(table_); }
130
131 private:
132 DISALLOW_COPY_AND_ASSIGN(ResourceTableBuilder);
133
134 DummyDiagnosticsImpl diagnostics_;
135 std::unique_ptr<ResourceTable> table_ = util::make_unique<ResourceTable>();
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700136};
137
Adam Lesinskid5083f62017-01-16 15:07:21 -0800138inline std::unique_ptr<Reference> BuildReference(const android::StringPiece& ref,
139 const Maybe<ResourceId>& id = {}) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700140 std::unique_ptr<Reference> reference =
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700141 util::make_unique<Reference>(ParseNameOrDie(ref));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700142 reference->id = id;
143 return reference;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700144}
145
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700146inline std::unique_ptr<BinaryPrimitive> BuildPrimitive(uint8_t type,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700147 uint32_t data) {
148 android::Res_value value = {};
149 value.size = sizeof(value);
150 value.dataType = type;
151 value.data = data;
152 return util::make_unique<BinaryPrimitive>(value);
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800153}
154
Adam Lesinskie78fd612015-10-22 12:48:43 -0700155template <typename T>
156class ValueBuilder {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700157 public:
158 template <typename... Args>
159 explicit ValueBuilder(Args&&... args)
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700160 : value_(new T{std::forward<Args>(args)...}) {}
Adam Lesinskie78fd612015-10-22 12:48:43 -0700161
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700162 template <typename... Args>
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700163 ValueBuilder& SetSource(Args&&... args) {
164 value_->SetSource(Source{std::forward<Args>(args)...});
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700165 return *this;
166 }
Adam Lesinskie78fd612015-10-22 12:48:43 -0700167
Adam Lesinskid5083f62017-01-16 15:07:21 -0800168 ValueBuilder& SetComment(const android::StringPiece& str) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700169 value_->SetComment(str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700170 return *this;
171 }
Adam Lesinskie78fd612015-10-22 12:48:43 -0700172
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700173 std::unique_ptr<Value> Build() { return std::move(value_); }
174
175 private:
176 DISALLOW_COPY_AND_ASSIGN(ValueBuilder);
177
178 std::unique_ptr<Value> value_;
Adam Lesinskie78fd612015-10-22 12:48:43 -0700179};
180
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700181class AttributeBuilder {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700182 public:
183 explicit AttributeBuilder(bool weak = false)
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700184 : attr_(util::make_unique<Attribute>(weak)) {
185 attr_->type_mask = android::ResTable_map::TYPE_ANY;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700186 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700187
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700188 AttributeBuilder& SetTypeMask(uint32_t typeMask) {
189 attr_->type_mask = typeMask;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700190 return *this;
191 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700192
Adam Lesinskid5083f62017-01-16 15:07:21 -0800193 AttributeBuilder& AddItem(const android::StringPiece& name, uint32_t value) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700194 attr_->symbols.push_back(Attribute::Symbol{
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700195 Reference(ResourceName({}, ResourceType::kId, name)), value});
196 return *this;
197 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700198
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700199 std::unique_ptr<Attribute> Build() { return std::move(attr_); }
200
201 private:
202 DISALLOW_COPY_AND_ASSIGN(AttributeBuilder);
203
204 std::unique_ptr<Attribute> attr_;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700205};
206
207class StyleBuilder {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700208 public:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700209 StyleBuilder() = default;
210
Adam Lesinskid5083f62017-01-16 15:07:21 -0800211 StyleBuilder& SetParent(const android::StringPiece& str) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700212 style_->parent = Reference(ParseNameOrDie(str));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700213 return *this;
214 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700215
Adam Lesinskid5083f62017-01-16 15:07:21 -0800216 StyleBuilder& AddItem(const android::StringPiece& str, std::unique_ptr<Item> value) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700217 style_->entries.push_back(
218 Style::Entry{Reference(ParseNameOrDie(str)), std::move(value)});
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700219 return *this;
220 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700221
Adam Lesinskid5083f62017-01-16 15:07:21 -0800222 StyleBuilder& AddItem(const android::StringPiece& str, const ResourceId& id,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700223 std::unique_ptr<Item> value) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700224 AddItem(str, std::move(value));
225 style_->entries.back().key.id = id;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700226 return *this;
227 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700228
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700229 std::unique_ptr<Style> Build() { return std::move(style_); }
230
231 private:
232 DISALLOW_COPY_AND_ASSIGN(StyleBuilder);
233
234 std::unique_ptr<Style> style_ = util::make_unique<Style>();
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700235};
236
237class StyleableBuilder {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700238 public:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700239 StyleableBuilder() = default;
240
Adam Lesinskid5083f62017-01-16 15:07:21 -0800241 StyleableBuilder& AddItem(const android::StringPiece& str, const Maybe<ResourceId>& id = {}) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700242 styleable_->entries.push_back(Reference(ParseNameOrDie(str)));
243 styleable_->entries.back().id = id;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700244 return *this;
245 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700246
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700247 std::unique_ptr<Styleable> Build() { return std::move(styleable_); }
248
249 private:
250 DISALLOW_COPY_AND_ASSIGN(StyleableBuilder);
251
252 std::unique_ptr<Styleable> styleable_ = util::make_unique<Styleable>();
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700253};
254
Adam Lesinskid5083f62017-01-16 15:07:21 -0800255inline std::unique_ptr<xml::XmlResource> BuildXmlDom(const android::StringPiece& str) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700256 std::stringstream in;
257 in << "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" << str;
258 StdErrDiagnostics diag;
259 std::unique_ptr<xml::XmlResource> doc =
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700260 xml::Inflate(&in, &diag, Source("test.xml"));
261 CHECK(doc != nullptr) << "failed to parse inline XML string";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700262 return doc;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700263}
264
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700265inline std::unique_ptr<xml::XmlResource> BuildXmlDomForPackageName(
Adam Lesinskid5083f62017-01-16 15:07:21 -0800266 IAaptContext* context, const android::StringPiece& str) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700267 std::unique_ptr<xml::XmlResource> doc = BuildXmlDom(str);
268 doc->file.name.package = context->GetCompilationPackage();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700269 return doc;
Adam Lesinski467f1712015-11-16 17:35:44 -0800270}
271
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700272} // namespace test
273} // namespace aapt
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700274
275#endif /* AAPT_TEST_BUILDERS_H */