/* * 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 namespace aapt { namespace test { class ResourceTableBuilder { private: DummyDiagnosticsImpl mDiagnostics; std::unique_ptr mTable = util::make_unique(); public: ResourceTableBuilder() = default; 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()); } 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(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(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(mTable->stringPool.makeRef(path))); } ResourceTableBuilder& addValue(const StringPiece16& name, std::unique_ptr value) { return addValue(name, {}, std::move(value)); } ResourceTableBuilder& addValue(const StringPiece16& name, const ResourceId id, std::unique_ptr value) { return addValue(name, id, {}, std::move(value)); } ResourceTableBuilder& addValue(const StringPiece16& name, const ResourceId id, const ConfigDescription& config, std::unique_ptr value) { ResourceName resName = parseNameOrDie(name); bool result = mTable->addResourceAllowMangled(resName, id, config, 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 build() { return std::move(mTable); } }; inline std::unique_ptr buildReference(const StringPiece16& ref, Maybe id = {}) { std::unique_ptr reference = util::make_unique(parseNameOrDie(ref)); reference->id = id; return reference; } template class ValueBuilder { private: std::unique_ptr mValue; public: template ValueBuilder(Args&&... args) : mValue(new T{ std::forward(args)... }) { } template ValueBuilder& setSource(Args&&... args) { mValue->setSource(Source{ std::forward(args)... }); return *this; } ValueBuilder& setComment(const StringPiece16& str) { mValue->setComment(str); return *this; } std::unique_ptr build() { return std::move(mValue); } }; class AttributeBuilder { private: std::unique_ptr mAttr; public: AttributeBuilder(bool weak = false) : mAttr(util::make_unique(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 build() { return std::move(mAttr); } }; class StyleBuilder { private: std::unique_ptr