Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 1 | /* |
| 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 Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 20 | #include <list> |
| 21 | |
| 22 | #include "android-base/logging.h" |
| 23 | #include "android-base/macros.h" |
| 24 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 25 | #include "NameMangler.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 26 | #include "process/IResourceTableConsumer.h" |
| 27 | #include "process/SymbolTable.h" |
| 28 | #include "test/Common.h" |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 29 | #include "util/Util.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 30 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 31 | namespace aapt { |
| 32 | namespace test { |
| 33 | |
| 34 | class Context : public IAaptContext { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 35 | public: |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 36 | Context() : name_mangler_({}), symbols_(&name_mangler_), min_sdk_version_(0) {} |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 37 | |
Adam Lesinski | b522f04 | 2017-04-21 16:57:59 -0700 | [diff] [blame] | 38 | PackageType GetPackageType() override { |
| 39 | return package_type_; |
| 40 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 41 | |
Adam Lesinski | b522f04 | 2017-04-21 16:57:59 -0700 | [diff] [blame] | 42 | SymbolTable* GetExternalSymbols() override { |
| 43 | return &symbols_; |
| 44 | } |
| 45 | |
| 46 | IDiagnostics* GetDiagnostics() override { |
| 47 | return &diagnostics_; |
| 48 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 49 | |
| 50 | const std::string& GetCompilationPackage() override { |
| 51 | CHECK(bool(compilation_package_)) << "package name not set"; |
| 52 | return compilation_package_.value(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 53 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 54 | |
Adam Lesinski | 4ca5697 | 2017-04-26 21:49:53 -0700 | [diff] [blame] | 55 | void SetCompilationPackage(const android::StringPiece& package) { |
| 56 | compilation_package_ = package.to_string(); |
| 57 | } |
| 58 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 59 | uint8_t GetPackageId() override { |
| 60 | CHECK(bool(package_id_)) << "package ID not set"; |
| 61 | return package_id_.value(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 62 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 63 | |
Adam Lesinski | 4ca5697 | 2017-04-26 21:49:53 -0700 | [diff] [blame] | 64 | void SetPackageId(uint8_t package_id) { |
| 65 | package_id_ = package_id; |
| 66 | } |
| 67 | |
Adam Lesinski | b522f04 | 2017-04-21 16:57:59 -0700 | [diff] [blame] | 68 | NameMangler* GetNameMangler() override { |
| 69 | return &name_mangler_; |
| 70 | } |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 71 | |
Adam Lesinski | 4ca5697 | 2017-04-26 21:49:53 -0700 | [diff] [blame] | 72 | void SetNameManglerPolicy(const NameManglerPolicy& policy) { |
| 73 | name_mangler_ = NameMangler(policy); |
| 74 | } |
| 75 | |
Adam Lesinski | b522f04 | 2017-04-21 16:57:59 -0700 | [diff] [blame] | 76 | bool IsVerbose() override { |
| 77 | return false; |
| 78 | } |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 79 | |
Adam Lesinski | b522f04 | 2017-04-21 16:57:59 -0700 | [diff] [blame] | 80 | int GetMinSdkVersion() override { |
| 81 | return min_sdk_version_; |
| 82 | } |
Adam Lesinski | fb6312f | 2016-06-28 14:40:32 -0700 | [diff] [blame] | 83 | |
Chris Warrington | 481f027 | 2018-02-06 14:03:39 +0000 | [diff] [blame] | 84 | bool IsAutoNamespace() override { |
| 85 | return auto_namespace_; |
| 86 | } |
| 87 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 88 | private: |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 89 | DISALLOW_COPY_AND_ASSIGN(Context); |
| 90 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 91 | friend class ContextBuilder; |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 92 | |
Adam Lesinski | b522f04 | 2017-04-21 16:57:59 -0700 | [diff] [blame] | 93 | PackageType package_type_ = PackageType::kApp; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 94 | Maybe<std::string> compilation_package_; |
| 95 | Maybe<uint8_t> package_id_; |
| 96 | StdErrDiagnostics diagnostics_; |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 97 | NameMangler name_mangler_; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 98 | SymbolTable symbols_; |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 99 | int min_sdk_version_; |
Chris Warrington | 481f027 | 2018-02-06 14:03:39 +0000 | [diff] [blame] | 100 | bool auto_namespace_; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 101 | }; |
| 102 | |
| 103 | class ContextBuilder { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 104 | public: |
Adam Lesinski | b522f04 | 2017-04-21 16:57:59 -0700 | [diff] [blame] | 105 | ContextBuilder& SetPackageType(PackageType type) { |
| 106 | context_->package_type_ = type; |
| 107 | return *this; |
| 108 | } |
| 109 | |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 110 | ContextBuilder& SetCompilationPackage(const android::StringPiece& package) { |
| 111 | context_->compilation_package_ = package.to_string(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 112 | return *this; |
| 113 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 114 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 115 | ContextBuilder& SetPackageId(uint8_t id) { |
| 116 | context_->package_id_ = id; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 117 | return *this; |
| 118 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 119 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 120 | ContextBuilder& SetNameManglerPolicy(const NameManglerPolicy& policy) { |
| 121 | context_->name_mangler_ = NameMangler(policy); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 122 | return *this; |
| 123 | } |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 124 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 125 | ContextBuilder& AddSymbolSource(std::unique_ptr<ISymbolSource> src) { |
| 126 | context_->GetExternalSymbols()->AppendSource(std::move(src)); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 127 | return *this; |
| 128 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 129 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 130 | ContextBuilder& SetMinSdkVersion(int min_sdk) { |
| 131 | context_->min_sdk_version_ = min_sdk; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 132 | return *this; |
| 133 | } |
Adam Lesinski | fb6312f | 2016-06-28 14:40:32 -0700 | [diff] [blame] | 134 | |
Chris Warrington | 481f027 | 2018-02-06 14:03:39 +0000 | [diff] [blame] | 135 | ContextBuilder& SetAutoNamespace(bool auto_namespace) { |
| 136 | context_->auto_namespace_ = auto_namespace; |
| 137 | return *this; |
| 138 | } |
| 139 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 140 | 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 Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 144 | }; |
| 145 | |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 146 | class StaticSymbolSourceBuilder { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 147 | public: |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 148 | StaticSymbolSourceBuilder& AddPublicSymbol(const android::StringPiece& name, ResourceId id, |
| 149 | std::unique_ptr<Attribute> attr = {}) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 150 | std::unique_ptr<SymbolTable::Symbol> symbol = |
| 151 | util::make_unique<SymbolTable::Symbol>(id, std::move(attr), true); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 152 | 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 Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 155 | return *this; |
| 156 | } |
| 157 | |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 158 | StaticSymbolSourceBuilder& AddSymbol(const android::StringPiece& name, ResourceId id, |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 159 | std::unique_ptr<Attribute> attr = {}) { |
| 160 | std::unique_ptr<SymbolTable::Symbol> symbol = |
| 161 | util::make_unique<SymbolTable::Symbol>(id, std::move(attr), false); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 162 | 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 Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 165 | return *this; |
| 166 | } |
| 167 | |
Adam Lesinski | b522f04 | 2017-04-21 16:57:59 -0700 | [diff] [blame] | 168 | std::unique_ptr<ISymbolSource> Build() { |
| 169 | return std::move(symbol_source_); |
| 170 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 171 | |
| 172 | private: |
| 173 | class StaticSymbolSource : public ISymbolSource { |
| 174 | public: |
| 175 | StaticSymbolSource() = default; |
| 176 | |
Adam Lesinski | b522f04 | 2017-04-21 16:57:59 -0700 | [diff] [blame] | 177 | std::unique_ptr<SymbolTable::Symbol> FindByName(const ResourceName& name) override { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 178 | auto iter = name_map_.find(name); |
| 179 | if (iter != name_map_.end()) { |
| 180 | return CloneSymbol(iter->second); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 181 | } |
| 182 | return nullptr; |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 183 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 184 | |
Chris Warrington | 481f027 | 2018-02-06 14:03:39 +0000 | [diff] [blame] | 185 | 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 Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 194 | 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 Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 198 | } |
| 199 | return nullptr; |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 200 | } |
| 201 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 202 | 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 Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 205 | |
| 206 | private: |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 207 | std::unique_ptr<SymbolTable::Symbol> CloneSymbol(SymbolTable::Symbol* sym) { |
Adam Lesinski | b522f04 | 2017-04-21 16:57:59 -0700 | [diff] [blame] | 208 | std::unique_ptr<SymbolTable::Symbol> clone = util::make_unique<SymbolTable::Symbol>(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 209 | clone->id = sym->id; |
| 210 | if (sym->attribute) { |
Adam Lesinski | b522f04 | 2017-04-21 16:57:59 -0700 | [diff] [blame] | 211 | clone->attribute = std::unique_ptr<Attribute>(sym->attribute->Clone(nullptr)); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 212 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 213 | clone->is_public = sym->is_public; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 214 | return clone; |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 215 | } |
| 216 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 217 | DISALLOW_COPY_AND_ASSIGN(StaticSymbolSource); |
| 218 | }; |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 219 | |
Adam Lesinski | b522f04 | 2017-04-21 16:57:59 -0700 | [diff] [blame] | 220 | std::unique_ptr<StaticSymbolSource> symbol_source_ = util::make_unique<StaticSymbolSource>(); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 221 | }; |
| 222 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 223 | } // namespace test |
| 224 | } // namespace aapt |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 225 | |
| 226 | #endif /* AAPT_TEST_CONTEXT_H */ |