/* * Copyright (C) 2017 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. */ #include "test/Builders.h" #include "android-base/logging.h" #include "androidfw/StringPiece.h" #include "io/StringStream.h" #include "test/Common.h" #include "util/Util.h" using ::aapt::configuration::Abi; using ::aapt::configuration::AndroidSdk; using ::aapt::configuration::ConfiguredArtifact; using ::aapt::configuration::GetOrCreateGroup; using ::aapt::io::StringInputStream; using ::android::StringPiece; namespace aapt { namespace test { ResourceTableBuilder& ResourceTableBuilder::SetPackageId(const StringPiece& package_name, uint8_t id) { ResourceTablePackage* package = table_->CreatePackage(package_name, id); CHECK(package != nullptr); return *this; } ResourceTableBuilder& ResourceTableBuilder::AddSimple(const StringPiece& name, const ResourceId& id) { return AddValue(name, id, util::make_unique()); } ResourceTableBuilder& ResourceTableBuilder::AddSimple(const StringPiece& name, const ConfigDescription& config, const ResourceId& id) { return AddValue(name, config, id, util::make_unique()); } ResourceTableBuilder& ResourceTableBuilder::AddReference(const StringPiece& name, const StringPiece& ref) { return AddReference(name, {}, ref); } ResourceTableBuilder& ResourceTableBuilder::AddReference(const StringPiece& name, const ResourceId& id, const StringPiece& ref) { return AddValue(name, id, util::make_unique(ParseNameOrDie(ref))); } ResourceTableBuilder& ResourceTableBuilder::AddString(const StringPiece& name, const StringPiece& str) { return AddString(name, {}, str); } ResourceTableBuilder& ResourceTableBuilder::AddString(const StringPiece& name, const ResourceId& id, const StringPiece& str) { return AddValue(name, id, util::make_unique(table_->string_pool.MakeRef(str))); } ResourceTableBuilder& ResourceTableBuilder::AddString(const StringPiece& name, const ResourceId& id, const ConfigDescription& config, const StringPiece& str) { return AddValue(name, config, id, util::make_unique(table_->string_pool.MakeRef(str))); } ResourceTableBuilder& ResourceTableBuilder::AddFileReference(const StringPiece& name, const StringPiece& path, io::IFile* file) { return AddFileReference(name, {}, path, file); } ResourceTableBuilder& ResourceTableBuilder::AddFileReference(const StringPiece& name, const ResourceId& id, const StringPiece& path, io::IFile* file) { auto file_ref = util::make_unique(table_->string_pool.MakeRef(path)); file_ref->file = file; return AddValue(name, id, std::move(file_ref)); } ResourceTableBuilder& ResourceTableBuilder::AddFileReference(const StringPiece& name, const StringPiece& path, const ConfigDescription& config, io::IFile* file) { auto file_ref = util::make_unique(table_->string_pool.MakeRef(path)); file_ref->file = file; return AddValue(name, config, {}, std::move(file_ref)); } ResourceTableBuilder& ResourceTableBuilder::AddValue(const StringPiece& name, std::unique_ptr value) { return AddValue(name, {}, std::move(value)); } ResourceTableBuilder& ResourceTableBuilder::AddValue(const StringPiece& name, const ResourceId& id, std::unique_ptr value) { return AddValue(name, {}, id, std::move(value)); } ResourceTableBuilder& ResourceTableBuilder::AddValue(const StringPiece& name, const ConfigDescription& config, const ResourceId& id, std::unique_ptr value) { ResourceName res_name = ParseNameOrDie(name); CHECK(table_->AddResourceWithIdMangled(res_name, id, config, {}, std::move(value), GetDiagnostics())); return *this; } ResourceTableBuilder& ResourceTableBuilder::SetSymbolState(const StringPiece& name, const ResourceId& id, Visibility::Level level, bool allow_new) { ResourceName res_name = ParseNameOrDie(name); Visibility visibility; visibility.level = level; CHECK(table_->SetVisibilityWithIdMangled(res_name, visibility, id, GetDiagnostics())); CHECK(table_->SetAllowNewMangled(res_name, AllowNew{}, GetDiagnostics())); return *this; } StringPool* ResourceTableBuilder::string_pool() { return &table_->string_pool; } std::unique_ptr ResourceTableBuilder::Build() { return std::move(table_); } std::unique_ptr BuildReference(const StringPiece& ref, const Maybe& id) { std::unique_ptr reference = util::make_unique(ParseNameOrDie(ref)); reference->id = id; return reference; } std::unique_ptr 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(value); } AttributeBuilder::AttributeBuilder() : attr_(util::make_unique(android::ResTable_map::TYPE_ANY)) { } AttributeBuilder& AttributeBuilder::SetTypeMask(uint32_t typeMask) { attr_->type_mask = typeMask; return *this; } AttributeBuilder& AttributeBuilder::SetWeak(bool weak) { attr_->SetWeak(weak); return *this; } AttributeBuilder& AttributeBuilder::AddItem(const StringPiece& name, uint32_t value) { attr_->symbols.push_back( Attribute::Symbol{Reference(ResourceName({}, ResourceType::kId, name)), value}); return *this; } std::unique_ptr AttributeBuilder::Build() { return std::move(attr_); } StyleBuilder& StyleBuilder::SetParent(const StringPiece& str) { style_->parent = Reference(ParseNameOrDie(str)); return *this; } StyleBuilder& StyleBuilder::AddItem(const StringPiece& str, std::unique_ptr value) { style_->entries.push_back(Style::Entry{Reference(ParseNameOrDie(str)), std::move(value)}); return *this; } StyleBuilder& StyleBuilder::AddItem(const StringPiece& str, const ResourceId& id, std::unique_ptr value) { AddItem(str, std::move(value)); style_->entries.back().key.id = id; return *this; } std::unique_ptr