blob: 4fa49186a09fe53f7b0d212a31158822c7491a98 [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
20#include "NameMangler.h"
21#include "util/Util.h"
22
23#include "process/IResourceTableConsumer.h"
24#include "process/SymbolTable.h"
25#include "test/Common.h"
26
27#include <cassert>
28#include <list>
29
30namespace aapt {
31namespace test {
32
33class Context : public IAaptContext {
34private:
35 friend class ContextBuilder;
36
37 Context() = default;
38
39 Maybe<std::u16string> mCompilationPackage;
40 Maybe<uint8_t> mPackageId;
41 std::unique_ptr<IDiagnostics> mDiagnostics = util::make_unique<StdErrDiagnostics>();
42 std::unique_ptr<ISymbolTable> mSymbols;
43 std::unique_ptr<NameMangler> mNameMangler;
44
45public:
46 ISymbolTable* getExternalSymbols() override {
47 assert(mSymbols && "test symbols not set");
48 return mSymbols.get();
49 }
50
51 void setSymbolTable(std::unique_ptr<ISymbolTable> symbols) {
52 mSymbols = std::move(symbols);
53 }
54
55 IDiagnostics* getDiagnostics() override {
56 assert(mDiagnostics && "test diagnostics not set");
57 return mDiagnostics.get();
58 }
59
60 StringPiece16 getCompilationPackage() override {
61 assert(mCompilationPackage && "package name not set");
62 return mCompilationPackage.value();
63 }
64
65 uint8_t getPackageId() override {
66 assert(mPackageId && "package ID not set");
67 return mPackageId.value();
68 }
69
70 NameMangler* getNameMangler() override {
71 assert(mNameMangler && "test name mangler not set");
72 return mNameMangler.get();
73 }
74};
75
76class ContextBuilder {
77private:
78 std::unique_ptr<Context> mContext = std::unique_ptr<Context>(new Context());
79
80public:
81 ContextBuilder& setCompilationPackage(const StringPiece16& package) {
82 mContext->mCompilationPackage = package.toString();
83 return *this;
84 }
85
86 ContextBuilder& setPackageId(uint8_t id) {
87 mContext->mPackageId = id;
88 return *this;
89 }
90
91 ContextBuilder& setSymbolTable(std::unique_ptr<ISymbolTable> symbols) {
92 mContext->mSymbols = std::move(symbols);
93 return *this;
94 }
95
96 ContextBuilder& setDiagnostics(std::unique_ptr<IDiagnostics> diag) {
97 mContext->mDiagnostics = std::move(diag);
98 return *this;
99 }
100
101 ContextBuilder& setNameManglerPolicy(NameManglerPolicy policy) {
102 mContext->mNameMangler = util::make_unique<NameMangler>(policy);
103 return *this;
104 }
105
106 std::unique_ptr<Context> build() {
107 return std::move(mContext);
108 }
109};
110
111class StaticSymbolTableBuilder {
112private:
113 struct SymbolTable : public ISymbolTable {
114 std::list<std::unique_ptr<Symbol>> mSymbols;
115 std::map<ResourceName, Symbol*> mNameMap;
116 std::map<ResourceId, Symbol*> mIdMap;
117
118 const Symbol* findByName(const ResourceName& name) override {
119 auto iter = mNameMap.find(name);
120 if (iter != mNameMap.end()) {
121 return iter->second;
122 }
123 return nullptr;
124 }
125
126 const Symbol* findById(ResourceId id) override {
127 auto iter = mIdMap.find(id);
128 if (iter != mIdMap.end()) {
129 return iter->second;
130 }
131 return nullptr;
132 }
133 };
134
135 std::unique_ptr<SymbolTable> mSymbolTable = util::make_unique<SymbolTable>();
136
137public:
138 StaticSymbolTableBuilder& addSymbol(const StringPiece16& name, ResourceId id,
139 std::unique_ptr<Attribute> attr = {}) {
140 std::unique_ptr<ISymbolTable::Symbol> symbol = util::make_unique<ISymbolTable::Symbol>(
141 id, std::move(attr));
142 mSymbolTable->mNameMap[parseNameOrDie(name)] = symbol.get();
143 mSymbolTable->mIdMap[id] = symbol.get();
144 mSymbolTable->mSymbols.push_back(std::move(symbol));
145 return *this;
146 }
147
148 std::unique_ptr<ISymbolTable> build() {
149 return std::move(mSymbolTable);
150 }
151};
152
153} // namespace test
154} // namespace aapt
155
156#endif /* AAPT_TEST_CONTEXT_H */