diff options
author | Adam Lesinski <adamlesinski@google.com> | 2015-08-14 14:26:04 -0700 |
---|---|---|
committer | Adam Lesinski <adamlesinski@google.com> | 2015-10-16 12:13:08 -0700 |
commit | 1ab598f46c3ff520a67f9d80194847741f3467ab (patch) | |
tree | 4846790211599fdd7a9bb35ec94df4a6ec4839d6 /tools/aapt2/ResourceTable_test.cpp | |
parent | 547c346bb34878b691fd53e54aa3a88efcc5dc6f (diff) |
AAPT2: Separate out the various steps
An early refactor. Some ideas became clearer as
development continued. Now the various phases are much
clearer and more easily reusable.
Also added a ton of tests!
Change-Id: Ic8f0a70c8222370352e63533b329c40457c0903e
Diffstat (limited to 'tools/aapt2/ResourceTable_test.cpp')
-rw-r--r-- | tools/aapt2/ResourceTable_test.cpp | 221 |
1 files changed, 55 insertions, 166 deletions
diff --git a/tools/aapt2/ResourceTable_test.cpp b/tools/aapt2/ResourceTable_test.cpp index 06d8699730de..2055a80aaba6 100644 --- a/tools/aapt2/ResourceTable_test.cpp +++ b/tools/aapt2/ResourceTable_test.cpp @@ -14,9 +14,12 @@ * limitations under the License. */ +#include "Diagnostics.h" #include "ResourceTable.h" #include "ResourceValues.h" -#include "Util.h" +#include "util/Util.h" + +#include "test/Common.h" #include <algorithm> #include <gtest/gtest.h> @@ -25,204 +28,90 @@ namespace aapt { -struct TestValue : public Value { - std::u16string value; - - TestValue(StringPiece16 str) : value(str.toString()) { - } - - TestValue* clone(StringPool* /*newPool*/) const override { - return new TestValue(value); - } - - void print(std::ostream& out) const override { - out << "(test) " << value; - } - - virtual void accept(ValueVisitor&, ValueVisitorArgs&&) override {} - virtual void accept(ConstValueVisitor&, ValueVisitorArgs&&) const override {} -}; - -struct TestWeakValue : public Value { - bool isWeak() const override { - return true; - } - - TestWeakValue* clone(StringPool* /*newPool*/) const override { - return new TestWeakValue(); - } - - void print(std::ostream& out) const override { - out << "(test) [weak]"; - } +struct ResourceTableTest : public ::testing::Test { + struct EmptyDiagnostics : public IDiagnostics { + void error(const DiagMessage& msg) override {} + void warn(const DiagMessage& msg) override {} + void note(const DiagMessage& msg) override {} + }; - virtual void accept(ValueVisitor&, ValueVisitorArgs&&) override {} - virtual void accept(ConstValueVisitor&, ValueVisitorArgs&&) const override {} + EmptyDiagnostics mDiagnostics; }; -TEST(ResourceTableTest, FailToAddResourceWithBadName) { +TEST_F(ResourceTableTest, FailToAddResourceWithBadName) { ResourceTable table; - table.setPackage(u"android"); EXPECT_FALSE(table.addResource( ResourceNameRef{ u"android", ResourceType::kId, u"hey,there" }, - {}, SourceLine{ "test.xml", 21 }, - util::make_unique<TestValue>(u"rawValue"))); + {}, Source{ "test.xml", 21 }, + util::make_unique<Id>(), &mDiagnostics)); EXPECT_FALSE(table.addResource( ResourceNameRef{ u"android", ResourceType::kId, u"hey:there" }, - {}, SourceLine{ "test.xml", 21 }, - util::make_unique<TestValue>(u"rawValue"))); + {}, Source{ "test.xml", 21 }, + util::make_unique<Id>(), &mDiagnostics)); } -TEST(ResourceTableTest, AddOneResource) { - const std::u16string kAndroidPackage = u"android"; - +TEST_F(ResourceTableTest, AddOneResource) { ResourceTable table; - table.setPackage(kAndroidPackage); - - const ResourceName name = { kAndroidPackage, ResourceType::kAttr, u"id" }; - EXPECT_TRUE(table.addResource(name, {}, SourceLine{ "test/path/file.xml", 23 }, - util::make_unique<TestValue>(u"rawValue"))); + EXPECT_TRUE(table.addResource(test::parseNameOrDie(u"@android:attr/id"), {}, + Source{ "test/path/file.xml", 23 }, + util::make_unique<Id>(), &mDiagnostics)); - const ResourceTableType* type; - const ResourceEntry* entry; - std::tie(type, entry) = table.findResource(name); - ASSERT_NE(nullptr, type); - ASSERT_NE(nullptr, entry); - EXPECT_EQ(name.entry, entry->name); - - ASSERT_NE(std::end(entry->values), - std::find_if(std::begin(entry->values), std::end(entry->values), - [](const ResourceConfigValue& val) -> bool { - return val.config == ConfigDescription{}; - })); + ASSERT_NE(nullptr, test::getValue<Id>(&table, u"@android:attr/id")); } -TEST(ResourceTableTest, AddMultipleResources) { - const std::u16string kAndroidPackage = u"android"; +TEST_F(ResourceTableTest, AddMultipleResources) { ResourceTable table; - table.setPackage(kAndroidPackage); ConfigDescription config; ConfigDescription languageConfig; memcpy(languageConfig.language, "pl", sizeof(languageConfig.language)); EXPECT_TRUE(table.addResource( - ResourceName{ kAndroidPackage, ResourceType::kAttr, u"layout_width" }, - config, SourceLine{ "test/path/file.xml", 10 }, - util::make_unique<TestValue>(u"rawValue"))); + test::parseNameOrDie(u"@android:attr/layout_width"), + config, Source{ "test/path/file.xml", 10 }, + util::make_unique<Id>(), &mDiagnostics)); EXPECT_TRUE(table.addResource( - ResourceName{ kAndroidPackage, ResourceType::kAttr, u"id" }, - config, SourceLine{ "test/path/file.xml", 12 }, - util::make_unique<TestValue>(u"rawValue"))); + test::parseNameOrDie(u"@android:attr/id"), + config, Source{ "test/path/file.xml", 12 }, + util::make_unique<Id>(), &mDiagnostics)); EXPECT_TRUE(table.addResource( - ResourceName{ kAndroidPackage, ResourceType::kString, u"ok" }, - config, SourceLine{ "test/path/file.xml", 14 }, - util::make_unique<TestValue>(u"Ok"))); + test::parseNameOrDie(u"@android:string/ok"), + config, Source{ "test/path/file.xml", 14 }, + util::make_unique<Id>(), &mDiagnostics)); EXPECT_TRUE(table.addResource( - ResourceName{ kAndroidPackage, ResourceType::kString, u"ok" }, - languageConfig, SourceLine{ "test/path/file.xml", 20 }, - util::make_unique<TestValue>(u"Tak"))); - - const auto endTypeIter = std::end(table); - auto typeIter = std::begin(table); - - ASSERT_NE(endTypeIter, typeIter); - EXPECT_EQ(ResourceType::kAttr, (*typeIter)->type); - - { - const std::unique_ptr<ResourceTableType>& type = *typeIter; - const auto endEntryIter = std::end(type->entries); - auto entryIter = std::begin(type->entries); - ASSERT_NE(endEntryIter, entryIter); - EXPECT_EQ(std::u16string(u"id"), (*entryIter)->name); - - ++entryIter; - ASSERT_NE(endEntryIter, entryIter); - EXPECT_EQ(std::u16string(u"layout_width"), (*entryIter)->name); - - ++entryIter; - ASSERT_EQ(endEntryIter, entryIter); - } - - ++typeIter; - ASSERT_NE(endTypeIter, typeIter); - EXPECT_EQ(ResourceType::kString, (*typeIter)->type); - - { - const std::unique_ptr<ResourceTableType>& type = *typeIter; - const auto endEntryIter = std::end(type->entries); - auto entryIter = std::begin(type->entries); - ASSERT_NE(endEntryIter, entryIter); - EXPECT_EQ(std::u16string(u"ok"), (*entryIter)->name); - - { - const std::unique_ptr<ResourceEntry>& entry = *entryIter; - const auto endConfigIter = std::end(entry->values); - auto configIter = std::begin(entry->values); - - ASSERT_NE(endConfigIter, configIter); - EXPECT_EQ(config, configIter->config); - const TestValue* value = - dynamic_cast<const TestValue*>(configIter->value.get()); - ASSERT_NE(nullptr, value); - EXPECT_EQ(std::u16string(u"Ok"), value->value); - - ++configIter; - ASSERT_NE(endConfigIter, configIter); - EXPECT_EQ(languageConfig, configIter->config); - EXPECT_NE(nullptr, configIter->value); - - value = dynamic_cast<const TestValue*>(configIter->value.get()); - ASSERT_NE(nullptr, value); - EXPECT_EQ(std::u16string(u"Tak"), value->value); - - ++configIter; - EXPECT_EQ(endConfigIter, configIter); - } - - ++entryIter; - ASSERT_EQ(endEntryIter, entryIter); - } - - ++typeIter; - EXPECT_EQ(endTypeIter, typeIter); + test::parseNameOrDie(u"@android:string/ok"), + languageConfig, Source{ "test/path/file.xml", 20 }, + util::make_unique<BinaryPrimitive>(android::Res_value{}), &mDiagnostics)); + + ASSERT_NE(nullptr, test::getValue<Id>(&table, u"@android:attr/layout_width")); + ASSERT_NE(nullptr, test::getValue<Id>(&table, u"@android:attr/id")); + ASSERT_NE(nullptr, test::getValue<Id>(&table, u"@android:string/ok")); + ASSERT_NE(nullptr, test::getValueForConfig<BinaryPrimitive>(&table, u"@android:string/ok", + languageConfig)); } -TEST(ResourceTableTest, OverrideWeakResourceValue) { - const std::u16string kAndroid = u"android"; - +TEST_F(ResourceTableTest, OverrideWeakResourceValue) { ResourceTable table; - table.setPackage(kAndroid); - table.setPackageId(0x01); - - ASSERT_TRUE(table.addResource( - ResourceName{ kAndroid, ResourceType::kAttr, u"foo" }, - {}, {}, util::make_unique<TestWeakValue>())); - - const ResourceTableType* type; - const ResourceEntry* entry; - std::tie(type, entry) = table.findResource( - ResourceNameRef{ kAndroid, ResourceType::kAttr, u"foo" }); - ASSERT_NE(nullptr, type); - ASSERT_NE(nullptr, entry); - ASSERT_EQ(entry->values.size(), 1u); - EXPECT_TRUE(entry->values.front().value->isWeak()); - - ASSERT_TRUE(table.addResource(ResourceName{ kAndroid, ResourceType::kAttr, u"foo" }, {}, {}, - util::make_unique<TestValue>(u"bar"))); - - std::tie(type, entry) = table.findResource( - ResourceNameRef{ kAndroid, ResourceType::kAttr, u"foo" }); - ASSERT_NE(nullptr, type); - ASSERT_NE(nullptr, entry); - ASSERT_EQ(entry->values.size(), 1u); - EXPECT_FALSE(entry->values.front().value->isWeak()); + + ASSERT_TRUE(table.addResource(test::parseNameOrDie(u"@android:attr/foo"), {}, {}, + util::make_unique<Attribute>(true), &mDiagnostics)); + + Attribute* attr = test::getValue<Attribute>(&table, u"@android:attr/foo"); + ASSERT_NE(nullptr, attr); + EXPECT_TRUE(attr->isWeak()); + + ASSERT_TRUE(table.addResource(test::parseNameOrDie(u"@android:attr/foo"), {}, {}, + util::make_unique<Attribute>(false), &mDiagnostics)); + + attr = test::getValue<Attribute>(&table, u"@android:attr/foo"); + ASSERT_NE(nullptr, attr); + EXPECT_FALSE(attr->isWeak()); } } // namespace aapt |