1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
|
/*
* Copyright (C) 2015 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef AAPT_TEST_BUILDERS_H
#define AAPT_TEST_BUILDERS_H
#include "ResourceTable.h"
#include "ResourceValues.h"
#include "test/Common.h"
#include "util/Util.h"
#include "xml/XmlDom.h"
#include <memory>
namespace aapt {
namespace test {
class ResourceTableBuilder {
private:
DummyDiagnosticsImpl mDiagnostics;
std::unique_ptr<ResourceTable> mTable = util::make_unique<ResourceTable>();
public:
ResourceTableBuilder() = default;
StringPool* getStringPool() {
return &mTable->stringPool;
}
ResourceTableBuilder& setPackageId(const StringPiece16& packageName, uint8_t id) {
ResourceTablePackage* package = mTable->createPackage(packageName, id);
assert(package);
return *this;
}
ResourceTableBuilder& addSimple(const StringPiece16& name, const ResourceId id = {}) {
return addValue(name, id, util::make_unique<Id>());
}
ResourceTableBuilder& addReference(const StringPiece16& name, const StringPiece16& ref) {
return addReference(name, {}, ref);
}
ResourceTableBuilder& addReference(const StringPiece16& name, const ResourceId id,
const StringPiece16& ref) {
return addValue(name, id, util::make_unique<Reference>(parseNameOrDie(ref)));
}
ResourceTableBuilder& addString(const StringPiece16& name, const StringPiece16& str) {
return addString(name, {}, str);
}
ResourceTableBuilder& addString(const StringPiece16& name, const ResourceId id,
const StringPiece16& str) {
return addValue(name, id, util::make_unique<String>(mTable->stringPool.makeRef(str)));
}
ResourceTableBuilder& addString(const StringPiece16& name, const ResourceId id,
const ConfigDescription& config, const StringPiece16& str) {
return addValue(name, id, config,
util::make_unique<String>(mTable->stringPool.makeRef(str)));
}
ResourceTableBuilder& addFileReference(const StringPiece16& name, const StringPiece16& path) {
return addFileReference(name, {}, path);
}
ResourceTableBuilder& addFileReference(const StringPiece16& name, const ResourceId id,
const StringPiece16& path) {
return addValue(name, id,
util::make_unique<FileReference>(mTable->stringPool.makeRef(path)));
}
ResourceTableBuilder& addFileReference(const StringPiece16& name, const StringPiece16& path,
const ConfigDescription& config) {
return addValue(name, {}, config,
util::make_unique<FileReference>(mTable->stringPool.makeRef(path)));
}
ResourceTableBuilder& addValue(const StringPiece16& name,
std::unique_ptr<Value> value) {
return addValue(name, {}, std::move(value));
}
ResourceTableBuilder& addValue(const StringPiece16& name, const ResourceId id,
std::unique_ptr<Value> value) {
return addValue(name, id, {}, std::move(value));
}
ResourceTableBuilder& addValue(const StringPiece16& name, const ResourceId id,
const ConfigDescription& config,
std::unique_ptr<Value> value) {
ResourceName resName = parseNameOrDie(name);
bool result = mTable->addResourceAllowMangled(resName, id, config, std::string(),
std::move(value), &mDiagnostics);
assert(result);
return *this;
}
ResourceTableBuilder& setSymbolState(const StringPiece16& name, ResourceId id,
SymbolState state) {
ResourceName resName = parseNameOrDie(name);
Symbol symbol;
symbol.state = state;
bool result = mTable->setSymbolStateAllowMangled(resName, id, symbol, &mDiagnostics);
assert(result);
return *this;
}
std::unique_ptr<ResourceTable> build() {
return std::move(mTable);
}
};
inline std::unique_ptr<Reference> buildReference(const StringPiece16& ref,
Maybe<ResourceId> id = {}) {
std::unique_ptr<Reference> reference = util::make_unique<Reference>(parseNameOrDie(ref));
reference->id = id;
return reference;
}
inline std::unique_ptr<BinaryPrimitive> buildPrimitive(uint8_t type, uint32_t data) {
android::Res_value value = {};
value.size = sizeof(value);
value.dataType = type;
value.data = data;
return util::make_unique<BinaryPrimitive>(value);
}
template <typename T>
class ValueBuilder {
private:
std::unique_ptr<Value> mValue;
public:
template <typename... Args>
ValueBuilder(Args&&... args) : mValue(new T{ std::forward<Args>(args)... }) {
}
template <typename... Args>
ValueBuilder& setSource(Args&&... args) {
mValue->setSource(Source{ std::forward<Args>(args)... });
return *this;
}
ValueBuilder& setComment(const StringPiece16& str) {
mValue->setComment(str);
return *this;
}
std::unique_ptr<Value> build() {
return std::move(mValue);
}
};
class AttributeBuilder {
private:
std::unique_ptr<Attribute> mAttr;
public:
AttributeBuilder(bool weak = false) : mAttr(util::make_unique<Attribute>(weak)) {
mAttr->typeMask = android::ResTable_map::TYPE_ANY;
}
AttributeBuilder& setTypeMask(uint32_t typeMask) {
mAttr->typeMask = typeMask;
return *this;
}
AttributeBuilder& addItem(const StringPiece16& name, uint32_t value) {
mAttr->symbols.push_back(Attribute::Symbol{
Reference(ResourceName{ {}, ResourceType::kId, name.toString()}),
value});
return *this;
}
std::unique_ptr<Attribute> build() {
return std::move(mAttr);
}
};
class StyleBuilder {
private:
std::unique_ptr<Style> mStyle = util::make_unique<Style>();
public:
StyleBuilder& setParent(const StringPiece16& str) {
mStyle->parent = Reference(parseNameOrDie(str));
return *this;
}
StyleBuilder& addItem(const StringPiece16& str, std::unique_ptr<Item> value) {
mStyle->entries.push_back(Style::Entry{ Reference(parseNameOrDie(str)), std::move(value) });
return *this;
}
StyleBuilder& addItem(const StringPiece16& str, ResourceId id, std::unique_ptr<Item> value) {
addItem(str, std::move(value));
mStyle->entries.back().key.id = id;
return *this;
}
std::unique_ptr<Style> build() {
return std::move(mStyle);
}
};
class StyleableBuilder {
private:
std::unique_ptr<Styleable> mStyleable = util::make_unique<Styleable>();
public:
StyleableBuilder& addItem(const StringPiece16& str, Maybe<ResourceId> id = {}) {
mStyleable->entries.push_back(Reference(parseNameOrDie(str)));
mStyleable->entries.back().id = id;
return *this;
}
std::unique_ptr<Styleable> build() {
return std::move(mStyleable);
}
};
inline std::unique_ptr<xml::XmlResource> buildXmlDom(const StringPiece& str) {
std::stringstream in;
in << "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" << str;
StdErrDiagnostics diag;
std::unique_ptr<xml::XmlResource> doc = xml::inflate(&in, &diag, Source("test.xml"));
assert(doc);
return doc;
}
inline std::unique_ptr<xml::XmlResource> buildXmlDomForPackageName(IAaptContext* context,
const StringPiece& str) {
std::unique_ptr<xml::XmlResource> doc = buildXmlDom(str);
doc->file.name.package = context->getCompilationPackage();
return doc;
}
} // namespace test
} // namespace aapt
#endif /* AAPT_TEST_BUILDERS_H */
|