blob: d9f3912fb4c6684c2883f5670836e049e266c58e [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
Adam Lesinskice5e56e2016-10-21 17:56:45 -070022#include "android-base/macros.h"
23
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070024#include "Resource.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070025#include "ResourceTable.h"
26#include "ResourceValues.h"
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070027#include "process/IResourceTableConsumer.h"
28#include "util/Maybe.h"
Adam Lesinski467f1712015-11-16 17:35:44 -080029#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 Lesinskiefeb7af2017-08-02 14:57:43 -070038 ResourceTableBuilder& SetPackageId(const android::StringPiece& package_name, uint8_t id);
39 ResourceTableBuilder& AddSimple(const android::StringPiece& name, const ResourceId& id = {});
Adam Lesinskid5083f62017-01-16 15:07:21 -080040 ResourceTableBuilder& AddSimple(const android::StringPiece& name, const ConfigDescription& config,
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070041 const ResourceId& id = {});
Adam Lesinskid5083f62017-01-16 15:07:21 -080042 ResourceTableBuilder& AddReference(const android::StringPiece& name,
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070043 const android::StringPiece& ref);
Adam Lesinskid5083f62017-01-16 15:07:21 -080044 ResourceTableBuilder& AddReference(const android::StringPiece& name, const ResourceId& id,
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070045 const android::StringPiece& ref);
Adam Lesinskid5083f62017-01-16 15:07:21 -080046 ResourceTableBuilder& AddString(const android::StringPiece& name,
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070047 const android::StringPiece& str);
Adam Lesinskid5083f62017-01-16 15:07:21 -080048 ResourceTableBuilder& AddString(const android::StringPiece& name, const ResourceId& id,
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070049 const android::StringPiece& str);
Adam Lesinskid5083f62017-01-16 15:07:21 -080050 ResourceTableBuilder& AddString(const android::StringPiece& name, const ResourceId& id,
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070051 const ConfigDescription& config, const android::StringPiece& str);
Adam Lesinskid5083f62017-01-16 15:07:21 -080052 ResourceTableBuilder& AddFileReference(const android::StringPiece& name,
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070053 const android::StringPiece& path);
Adam Lesinskid5083f62017-01-16 15:07:21 -080054 ResourceTableBuilder& AddFileReference(const android::StringPiece& name, const ResourceId& id,
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070055 const android::StringPiece& path);
Adam Lesinskid5083f62017-01-16 15:07:21 -080056 ResourceTableBuilder& AddFileReference(const android::StringPiece& name,
57 const android::StringPiece& path,
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070058 const ConfigDescription& config);
59 ResourceTableBuilder& AddValue(const android::StringPiece& name, std::unique_ptr<Value> value);
Adam Lesinskid5083f62017-01-16 15:07:21 -080060 ResourceTableBuilder& AddValue(const android::StringPiece& name, const ResourceId& id,
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070061 std::unique_ptr<Value> value);
Adam Lesinskid5083f62017-01-16 15:07:21 -080062 ResourceTableBuilder& AddValue(const android::StringPiece& name, const ConfigDescription& config,
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070063 const ResourceId& id, std::unique_ptr<Value> value);
Adam Lesinskid5083f62017-01-16 15:07:21 -080064 ResourceTableBuilder& SetSymbolState(const android::StringPiece& name, const ResourceId& id,
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070065 SymbolState state, bool allow_new = false);
Adam Lesinskie78fd612015-10-22 12:48:43 -070066
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070067 StringPool* string_pool();
68 std::unique_ptr<ResourceTable> Build();
Adam Lesinskice5e56e2016-10-21 17:56:45 -070069
70 private:
71 DISALLOW_COPY_AND_ASSIGN(ResourceTableBuilder);
72
Adam Lesinskice5e56e2016-10-21 17:56:45 -070073 std::unique_ptr<ResourceTable> table_ = util::make_unique<ResourceTable>();
Adam Lesinski1ab598f2015-08-14 14:26:04 -070074};
75
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070076std::unique_ptr<Reference> BuildReference(const android::StringPiece& ref,
77 const Maybe<ResourceId>& id = {});
78std::unique_ptr<BinaryPrimitive> BuildPrimitive(uint8_t type, uint32_t data);
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -080079
Adam Lesinskie78fd612015-10-22 12:48:43 -070080template <typename T>
81class ValueBuilder {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070082 public:
83 template <typename... Args>
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070084 explicit ValueBuilder(Args&&... args) : value_(new T{std::forward<Args>(args)...}) {
85 }
Adam Lesinskie78fd612015-10-22 12:48:43 -070086
Adam Lesinskicacb28f2016-10-19 12:18:14 -070087 template <typename... Args>
Adam Lesinskice5e56e2016-10-21 17:56:45 -070088 ValueBuilder& SetSource(Args&&... args) {
89 value_->SetSource(Source{std::forward<Args>(args)...});
Adam Lesinskicacb28f2016-10-19 12:18:14 -070090 return *this;
91 }
Adam Lesinskie78fd612015-10-22 12:48:43 -070092
Adam Lesinskid5083f62017-01-16 15:07:21 -080093 ValueBuilder& SetComment(const android::StringPiece& str) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070094 value_->SetComment(str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070095 return *this;
96 }
Adam Lesinskie78fd612015-10-22 12:48:43 -070097
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070098 std::unique_ptr<Value> Build() {
99 return std::move(value_);
100 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700101
102 private:
103 DISALLOW_COPY_AND_ASSIGN(ValueBuilder);
104
105 std::unique_ptr<Value> value_;
Adam Lesinskie78fd612015-10-22 12:48:43 -0700106};
107
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700108class AttributeBuilder {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700109 public:
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700110 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 Lesinskice5e56e2016-10-21 17:56:45 -0700114
115 private:
116 DISALLOW_COPY_AND_ASSIGN(AttributeBuilder);
117
118 std::unique_ptr<Attribute> attr_;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700119};
120
121class StyleBuilder {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700122 public:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700123 StyleBuilder() = default;
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700124 StyleBuilder& SetParent(const android::StringPiece& str);
125 StyleBuilder& AddItem(const android::StringPiece& str, std::unique_ptr<Item> value);
Adam Lesinskid5083f62017-01-16 15:07:21 -0800126 StyleBuilder& AddItem(const android::StringPiece& str, const ResourceId& id,
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700127 std::unique_ptr<Item> value);
128 std::unique_ptr<Style> Build();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700129
130 private:
131 DISALLOW_COPY_AND_ASSIGN(StyleBuilder);
132
133 std::unique_ptr<Style> style_ = util::make_unique<Style>();
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700134};
135
136class StyleableBuilder {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700137 public:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700138 StyleableBuilder() = default;
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700139 StyleableBuilder& AddItem(const android::StringPiece& str, const Maybe<ResourceId>& id = {});
140 std::unique_ptr<Styleable> Build();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700141
142 private:
143 DISALLOW_COPY_AND_ASSIGN(StyleableBuilder);
144
145 std::unique_ptr<Styleable> styleable_ = util::make_unique<Styleable>();
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700146};
147
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700148std::unique_ptr<xml::XmlResource> BuildXmlDom(const android::StringPiece& str);
149std::unique_ptr<xml::XmlResource> BuildXmlDomForPackageName(IAaptContext* context,
150 const android::StringPiece& str);
Adam Lesinski467f1712015-11-16 17:35:44 -0800151
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700152} // namespace test
153} // namespace aapt
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700154
155#endif /* AAPT_TEST_BUILDERS_H */