ResourceTable_test.cpp revision e78fd617ec60139a973a01925fa7adad31febb39
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#include "Diagnostics.h" 18#include "ResourceTable.h" 19#include "ResourceValues.h" 20#include "util/Util.h" 21 22#include "test/Builders.h" 23 24#include <algorithm> 25#include <gtest/gtest.h> 26#include <ostream> 27#include <string> 28 29namespace aapt { 30 31struct ResourceTableTest : public ::testing::Test { 32 struct EmptyDiagnostics : public IDiagnostics { 33 void error(const DiagMessage& msg) override {} 34 void warn(const DiagMessage& msg) override {} 35 void note(const DiagMessage& msg) override {} 36 }; 37 38 EmptyDiagnostics mDiagnostics; 39}; 40 41TEST_F(ResourceTableTest, FailToAddResourceWithBadName) { 42 ResourceTable table; 43 44 EXPECT_FALSE(table.addResource( 45 ResourceNameRef(u"android", ResourceType::kId, u"hey,there"), 46 ConfigDescription{}, 47 test::ValueBuilder<Id>().setSource("test.xml", 21u).build(), 48 &mDiagnostics)); 49 50 EXPECT_FALSE(table.addResource( 51 ResourceNameRef(u"android", ResourceType::kId, u"hey:there"), 52 ConfigDescription{}, 53 test::ValueBuilder<Id>().setSource("test.xml", 21u).build(), 54 &mDiagnostics)); 55} 56 57TEST_F(ResourceTableTest, AddOneResource) { 58 ResourceTable table; 59 60 EXPECT_TRUE(table.addResource(test::parseNameOrDie(u"@android:attr/id"), 61 ConfigDescription{}, 62 test::ValueBuilder<Id>() 63 .setSource("test/path/file.xml", 23u).build(), 64 &mDiagnostics)); 65 66 ASSERT_NE(nullptr, test::getValue<Id>(&table, u"@android:attr/id")); 67} 68 69TEST_F(ResourceTableTest, AddMultipleResources) { 70 ResourceTable table; 71 72 ConfigDescription config; 73 ConfigDescription languageConfig; 74 memcpy(languageConfig.language, "pl", sizeof(languageConfig.language)); 75 76 EXPECT_TRUE(table.addResource( 77 test::parseNameOrDie(u"@android:attr/layout_width"), 78 config, 79 test::ValueBuilder<Id>().setSource("test/path/file.xml", 10u).build(), 80 &mDiagnostics)); 81 82 EXPECT_TRUE(table.addResource( 83 test::parseNameOrDie(u"@android:attr/id"), 84 config, 85 test::ValueBuilder<Id>().setSource("test/path/file.xml", 12u).build(), 86 &mDiagnostics)); 87 88 EXPECT_TRUE(table.addResource( 89 test::parseNameOrDie(u"@android:string/ok"), 90 config, 91 test::ValueBuilder<Id>().setSource("test/path/file.xml", 14u).build(), 92 &mDiagnostics)); 93 94 EXPECT_TRUE(table.addResource( 95 test::parseNameOrDie(u"@android:string/ok"), 96 languageConfig, 97 test::ValueBuilder<BinaryPrimitive>(android::Res_value{}) 98 .setSource("test/path/file.xml", 20u) 99 .build(), 100 &mDiagnostics)); 101 102 ASSERT_NE(nullptr, test::getValue<Id>(&table, u"@android:attr/layout_width")); 103 ASSERT_NE(nullptr, test::getValue<Id>(&table, u"@android:attr/id")); 104 ASSERT_NE(nullptr, test::getValue<Id>(&table, u"@android:string/ok")); 105 ASSERT_NE(nullptr, test::getValueForConfig<BinaryPrimitive>(&table, u"@android:string/ok", 106 languageConfig)); 107} 108 109TEST_F(ResourceTableTest, OverrideWeakResourceValue) { 110 ResourceTable table; 111 112 ASSERT_TRUE(table.addResource(test::parseNameOrDie(u"@android:attr/foo"), ConfigDescription{}, 113 util::make_unique<Attribute>(true), &mDiagnostics)); 114 115 Attribute* attr = test::getValue<Attribute>(&table, u"@android:attr/foo"); 116 ASSERT_NE(nullptr, attr); 117 EXPECT_TRUE(attr->isWeak()); 118 119 ASSERT_TRUE(table.addResource(test::parseNameOrDie(u"@android:attr/foo"), ConfigDescription{}, 120 util::make_unique<Attribute>(false), &mDiagnostics)); 121 122 attr = test::getValue<Attribute>(&table, u"@android:attr/foo"); 123 ASSERT_NE(nullptr, attr); 124 EXPECT_FALSE(attr->isWeak()); 125} 126 127} // namespace aapt 128