blob: a07d79f01dfed358f5b490589b9e60772d4fac92 [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_CONTEXT_H
18#define AAPT_TEST_CONTEXT_H
19
Adam Lesinskice5e56e2016-10-21 17:56:45 -070020#include <list>
21
22#include "android-base/logging.h"
23#include "android-base/macros.h"
24
Adam Lesinski1ab598f2015-08-14 14:26:04 -070025#include "NameMangler.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070026#include "process/IResourceTableConsumer.h"
27#include "process/SymbolTable.h"
28#include "test/Common.h"
Adam Lesinskicacb28f2016-10-19 12:18:14 -070029#include "util/Util.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070030
Adam Lesinski1ab598f2015-08-14 14:26:04 -070031namespace aapt {
32namespace test {
33
34class Context : public IAaptContext {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070035 public:
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -080036 Context() : name_mangler_({}), symbols_(&name_mangler_), min_sdk_version_(0) {}
Adam Lesinski1ab598f2015-08-14 14:26:04 -070037
Adam Lesinskib522f042017-04-21 16:57:59 -070038 PackageType GetPackageType() override {
39 return package_type_;
40 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -070041
Adam Lesinskib522f042017-04-21 16:57:59 -070042 SymbolTable* GetExternalSymbols() override {
43 return &symbols_;
44 }
45
46 IDiagnostics* GetDiagnostics() override {
47 return &diagnostics_;
48 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -070049
50 const std::string& GetCompilationPackage() override {
51 CHECK(bool(compilation_package_)) << "package name not set";
52 return compilation_package_.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -070053 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -070054
Adam Lesinski4ca56972017-04-26 21:49:53 -070055 void SetCompilationPackage(const android::StringPiece& package) {
56 compilation_package_ = package.to_string();
57 }
58
Adam Lesinskice5e56e2016-10-21 17:56:45 -070059 uint8_t GetPackageId() override {
60 CHECK(bool(package_id_)) << "package ID not set";
61 return package_id_.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -070062 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -070063
Adam Lesinski4ca56972017-04-26 21:49:53 -070064 void SetPackageId(uint8_t package_id) {
65 package_id_ = package_id;
66 }
67
Adam Lesinskib522f042017-04-21 16:57:59 -070068 NameMangler* GetNameMangler() override {
69 return &name_mangler_;
70 }
Adam Lesinski355f2852016-02-13 20:26:45 -080071
Adam Lesinski4ca56972017-04-26 21:49:53 -070072 void SetNameManglerPolicy(const NameManglerPolicy& policy) {
73 name_mangler_ = NameMangler(policy);
74 }
75
Adam Lesinskib522f042017-04-21 16:57:59 -070076 bool IsVerbose() override {
77 return false;
78 }
Adam Lesinski64587af2016-02-18 18:33:06 -080079
Adam Lesinskib522f042017-04-21 16:57:59 -070080 int GetMinSdkVersion() override {
81 return min_sdk_version_;
82 }
Adam Lesinskifb6312f2016-06-28 14:40:32 -070083
Chris Warrington481f0272018-02-06 14:03:39 +000084 bool IsAutoNamespace() override {
85 return auto_namespace_;
86 }
87
Adam Lesinskicacb28f2016-10-19 12:18:14 -070088 private:
Adam Lesinskice5e56e2016-10-21 17:56:45 -070089 DISALLOW_COPY_AND_ASSIGN(Context);
90
Adam Lesinskicacb28f2016-10-19 12:18:14 -070091 friend class ContextBuilder;
Adam Lesinski64587af2016-02-18 18:33:06 -080092
Adam Lesinskib522f042017-04-21 16:57:59 -070093 PackageType package_type_ = PackageType::kApp;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070094 Maybe<std::string> compilation_package_;
95 Maybe<uint8_t> package_id_;
96 StdErrDiagnostics diagnostics_;
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -080097 NameMangler name_mangler_;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070098 SymbolTable symbols_;
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -080099 int min_sdk_version_;
Chris Warrington481f0272018-02-06 14:03:39 +0000100 bool auto_namespace_;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700101};
102
103class ContextBuilder {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700104 public:
Adam Lesinskib522f042017-04-21 16:57:59 -0700105 ContextBuilder& SetPackageType(PackageType type) {
106 context_->package_type_ = type;
107 return *this;
108 }
109
Adam Lesinskid5083f62017-01-16 15:07:21 -0800110 ContextBuilder& SetCompilationPackage(const android::StringPiece& package) {
111 context_->compilation_package_ = package.to_string();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700112 return *this;
113 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700114
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700115 ContextBuilder& SetPackageId(uint8_t id) {
116 context_->package_id_ = id;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700117 return *this;
118 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700119
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700120 ContextBuilder& SetNameManglerPolicy(const NameManglerPolicy& policy) {
121 context_->name_mangler_ = NameMangler(policy);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700122 return *this;
123 }
Adam Lesinski64587af2016-02-18 18:33:06 -0800124
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700125 ContextBuilder& AddSymbolSource(std::unique_ptr<ISymbolSource> src) {
126 context_->GetExternalSymbols()->AppendSource(std::move(src));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700127 return *this;
128 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700129
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700130 ContextBuilder& SetMinSdkVersion(int min_sdk) {
131 context_->min_sdk_version_ = min_sdk;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700132 return *this;
133 }
Adam Lesinskifb6312f2016-06-28 14:40:32 -0700134
Chris Warrington481f0272018-02-06 14:03:39 +0000135 ContextBuilder& SetAutoNamespace(bool auto_namespace) {
136 context_->auto_namespace_ = auto_namespace;
137 return *this;
138 }
139
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700140 std::unique_ptr<Context> Build() { return std::move(context_); }
141
142 private:
143 std::unique_ptr<Context> context_ = std::unique_ptr<Context>(new Context());
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700144};
145
Adam Lesinski64587af2016-02-18 18:33:06 -0800146class StaticSymbolSourceBuilder {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700147 public:
Adam Lesinskid5083f62017-01-16 15:07:21 -0800148 StaticSymbolSourceBuilder& AddPublicSymbol(const android::StringPiece& name, ResourceId id,
149 std::unique_ptr<Attribute> attr = {}) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700150 std::unique_ptr<SymbolTable::Symbol> symbol =
151 util::make_unique<SymbolTable::Symbol>(id, std::move(attr), true);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700152 symbol_source_->name_map_[ParseNameOrDie(name)] = symbol.get();
153 symbol_source_->id_map_[id] = symbol.get();
154 symbol_source_->symbols_.push_back(std::move(symbol));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700155 return *this;
156 }
157
Adam Lesinskid5083f62017-01-16 15:07:21 -0800158 StaticSymbolSourceBuilder& AddSymbol(const android::StringPiece& name, ResourceId id,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700159 std::unique_ptr<Attribute> attr = {}) {
160 std::unique_ptr<SymbolTable::Symbol> symbol =
161 util::make_unique<SymbolTable::Symbol>(id, std::move(attr), false);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700162 symbol_source_->name_map_[ParseNameOrDie(name)] = symbol.get();
163 symbol_source_->id_map_[id] = symbol.get();
164 symbol_source_->symbols_.push_back(std::move(symbol));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700165 return *this;
166 }
167
Adam Lesinskib522f042017-04-21 16:57:59 -0700168 std::unique_ptr<ISymbolSource> Build() {
169 return std::move(symbol_source_);
170 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700171
172 private:
173 class StaticSymbolSource : public ISymbolSource {
174 public:
175 StaticSymbolSource() = default;
176
Adam Lesinskib522f042017-04-21 16:57:59 -0700177 std::unique_ptr<SymbolTable::Symbol> FindByName(const ResourceName& name) override {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700178 auto iter = name_map_.find(name);
179 if (iter != name_map_.end()) {
180 return CloneSymbol(iter->second);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700181 }
182 return nullptr;
Adam Lesinski64587af2016-02-18 18:33:06 -0800183 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700184
Chris Warrington481f0272018-02-06 14:03:39 +0000185 std::string GetPackageForSymbol(const ResourceName& name) override {
186 for (auto const& imap : name_map_) {
187 if (imap.first.type == name.type && imap.first.entry == name.entry) {
188 return imap.first.package;
189 }
190 }
191 return "";
192 }
193
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700194 std::unique_ptr<SymbolTable::Symbol> FindById(ResourceId id) override {
195 auto iter = id_map_.find(id);
196 if (iter != id_map_.end()) {
197 return CloneSymbol(iter->second);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700198 }
199 return nullptr;
Adam Lesinski64587af2016-02-18 18:33:06 -0800200 }
201
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700202 std::list<std::unique_ptr<SymbolTable::Symbol>> symbols_;
203 std::map<ResourceName, SymbolTable::Symbol*> name_map_;
204 std::map<ResourceId, SymbolTable::Symbol*> id_map_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700205
206 private:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700207 std::unique_ptr<SymbolTable::Symbol> CloneSymbol(SymbolTable::Symbol* sym) {
Adam Lesinskib522f042017-04-21 16:57:59 -0700208 std::unique_ptr<SymbolTable::Symbol> clone = util::make_unique<SymbolTable::Symbol>();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700209 clone->id = sym->id;
210 if (sym->attribute) {
Adam Lesinskib522f042017-04-21 16:57:59 -0700211 clone->attribute = std::unique_ptr<Attribute>(sym->attribute->Clone(nullptr));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700212 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700213 clone->is_public = sym->is_public;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700214 return clone;
Adam Lesinski64587af2016-02-18 18:33:06 -0800215 }
216
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700217 DISALLOW_COPY_AND_ASSIGN(StaticSymbolSource);
218 };
Adam Lesinski64587af2016-02-18 18:33:06 -0800219
Adam Lesinskib522f042017-04-21 16:57:59 -0700220 std::unique_ptr<StaticSymbolSource> symbol_source_ = util::make_unique<StaticSymbolSource>();
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700221};
222
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700223} // namespace test
224} // namespace aapt
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700225
226#endif /* AAPT_TEST_CONTEXT_H */