blob: 557cd1b646e37d7fa2bb104f1ef0daaa2faf7ab3 [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 Lesinskice5e56e2016-10-21 17:56:45 -070038 SymbolTable* GetExternalSymbols() override { return &symbols_; }
Adam Lesinski1ab598f2015-08-14 14:26:04 -070039
Adam Lesinskice5e56e2016-10-21 17:56:45 -070040 IDiagnostics* GetDiagnostics() override { return &diagnostics_; }
41
42 const std::string& GetCompilationPackage() override {
43 CHECK(bool(compilation_package_)) << "package name not set";
44 return compilation_package_.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -070045 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -070046
Adam Lesinskice5e56e2016-10-21 17:56:45 -070047 uint8_t GetPackageId() override {
48 CHECK(bool(package_id_)) << "package ID not set";
49 return package_id_.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -070050 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -070051
Adam Lesinskice5e56e2016-10-21 17:56:45 -070052 NameMangler* GetNameMangler() override { return &name_mangler_; }
Adam Lesinski355f2852016-02-13 20:26:45 -080053
Adam Lesinskice5e56e2016-10-21 17:56:45 -070054 bool IsVerbose() override { return false; }
Adam Lesinski64587af2016-02-18 18:33:06 -080055
Adam Lesinskice5e56e2016-10-21 17:56:45 -070056 int GetMinSdkVersion() override { return min_sdk_version_; }
Adam Lesinskifb6312f2016-06-28 14:40:32 -070057
Adam Lesinskicacb28f2016-10-19 12:18:14 -070058 private:
Adam Lesinskice5e56e2016-10-21 17:56:45 -070059 DISALLOW_COPY_AND_ASSIGN(Context);
60
Adam Lesinskicacb28f2016-10-19 12:18:14 -070061 friend class ContextBuilder;
Adam Lesinski64587af2016-02-18 18:33:06 -080062
Adam Lesinskice5e56e2016-10-21 17:56:45 -070063 Maybe<std::string> compilation_package_;
64 Maybe<uint8_t> package_id_;
65 StdErrDiagnostics diagnostics_;
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -080066 NameMangler name_mangler_;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070067 SymbolTable symbols_;
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -080068 int min_sdk_version_;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070069};
70
71class ContextBuilder {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070072 public:
Adam Lesinskid5083f62017-01-16 15:07:21 -080073 ContextBuilder& SetCompilationPackage(const android::StringPiece& package) {
74 context_->compilation_package_ = package.to_string();
Adam Lesinskicacb28f2016-10-19 12:18:14 -070075 return *this;
76 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -070077
Adam Lesinskice5e56e2016-10-21 17:56:45 -070078 ContextBuilder& SetPackageId(uint8_t id) {
79 context_->package_id_ = id;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070080 return *this;
81 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -070082
Adam Lesinskice5e56e2016-10-21 17:56:45 -070083 ContextBuilder& SetNameManglerPolicy(const NameManglerPolicy& policy) {
84 context_->name_mangler_ = NameMangler(policy);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070085 return *this;
86 }
Adam Lesinski64587af2016-02-18 18:33:06 -080087
Adam Lesinskice5e56e2016-10-21 17:56:45 -070088 ContextBuilder& AddSymbolSource(std::unique_ptr<ISymbolSource> src) {
89 context_->GetExternalSymbols()->AppendSource(std::move(src));
Adam Lesinskicacb28f2016-10-19 12:18:14 -070090 return *this;
91 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -070092
Adam Lesinskice5e56e2016-10-21 17:56:45 -070093 ContextBuilder& SetMinSdkVersion(int min_sdk) {
94 context_->min_sdk_version_ = min_sdk;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070095 return *this;
96 }
Adam Lesinskifb6312f2016-06-28 14:40:32 -070097
Adam Lesinskice5e56e2016-10-21 17:56:45 -070098 std::unique_ptr<Context> Build() { return std::move(context_); }
99
100 private:
101 std::unique_ptr<Context> context_ = std::unique_ptr<Context>(new Context());
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700102};
103
Adam Lesinski64587af2016-02-18 18:33:06 -0800104class StaticSymbolSourceBuilder {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700105 public:
Adam Lesinskid5083f62017-01-16 15:07:21 -0800106 StaticSymbolSourceBuilder& AddPublicSymbol(const android::StringPiece& name, ResourceId id,
107 std::unique_ptr<Attribute> attr = {}) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700108 std::unique_ptr<SymbolTable::Symbol> symbol =
109 util::make_unique<SymbolTable::Symbol>(id, std::move(attr), true);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700110 symbol_source_->name_map_[ParseNameOrDie(name)] = symbol.get();
111 symbol_source_->id_map_[id] = symbol.get();
112 symbol_source_->symbols_.push_back(std::move(symbol));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700113 return *this;
114 }
115
Adam Lesinskid5083f62017-01-16 15:07:21 -0800116 StaticSymbolSourceBuilder& AddSymbol(const android::StringPiece& name, ResourceId id,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700117 std::unique_ptr<Attribute> attr = {}) {
118 std::unique_ptr<SymbolTable::Symbol> symbol =
119 util::make_unique<SymbolTable::Symbol>(id, std::move(attr), false);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700120 symbol_source_->name_map_[ParseNameOrDie(name)] = symbol.get();
121 symbol_source_->id_map_[id] = symbol.get();
122 symbol_source_->symbols_.push_back(std::move(symbol));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700123 return *this;
124 }
125
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700126 std::unique_ptr<ISymbolSource> Build() { return std::move(symbol_source_); }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700127
128 private:
129 class StaticSymbolSource : public ISymbolSource {
130 public:
131 StaticSymbolSource() = default;
132
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700133 std::unique_ptr<SymbolTable::Symbol> FindByName(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700134 const ResourceName& name) override {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700135 auto iter = name_map_.find(name);
136 if (iter != name_map_.end()) {
137 return CloneSymbol(iter->second);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700138 }
139 return nullptr;
Adam Lesinski64587af2016-02-18 18:33:06 -0800140 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700141
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700142 std::unique_ptr<SymbolTable::Symbol> FindById(ResourceId id) override {
143 auto iter = id_map_.find(id);
144 if (iter != id_map_.end()) {
145 return CloneSymbol(iter->second);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700146 }
147 return nullptr;
Adam Lesinski64587af2016-02-18 18:33:06 -0800148 }
149
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700150 std::list<std::unique_ptr<SymbolTable::Symbol>> symbols_;
151 std::map<ResourceName, SymbolTable::Symbol*> name_map_;
152 std::map<ResourceId, SymbolTable::Symbol*> id_map_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700153
154 private:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700155 std::unique_ptr<SymbolTable::Symbol> CloneSymbol(SymbolTable::Symbol* sym) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700156 std::unique_ptr<SymbolTable::Symbol> clone =
157 util::make_unique<SymbolTable::Symbol>();
158 clone->id = sym->id;
159 if (sym->attribute) {
160 clone->attribute =
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700161 std::unique_ptr<Attribute>(sym->attribute->Clone(nullptr));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700162 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700163 clone->is_public = sym->is_public;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700164 return clone;
Adam Lesinski64587af2016-02-18 18:33:06 -0800165 }
166
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700167 DISALLOW_COPY_AND_ASSIGN(StaticSymbolSource);
168 };
Adam Lesinski64587af2016-02-18 18:33:06 -0800169
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700170 std::unique_ptr<StaticSymbolSource> symbol_source_ =
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700171 util::make_unique<StaticSymbolSource>();
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700172};
173
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700174} // namespace test
175} // namespace aapt
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700176
177#endif /* AAPT_TEST_CONTEXT_H */