Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 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 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 17 | #include "Diagnostics.h" |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 18 | #include "ResourceTable.h" |
| 19 | #include "ResourceValues.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 20 | #include "util/Util.h" |
| 21 | |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame] | 22 | #include "test/Builders.h" |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 23 | |
| 24 | #include <algorithm> |
| 25 | #include <gtest/gtest.h> |
| 26 | #include <ostream> |
| 27 | #include <string> |
| 28 | |
| 29 | namespace aapt { |
| 30 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 31 | struct 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 | }; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 37 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 38 | EmptyDiagnostics mDiagnostics; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 39 | }; |
| 40 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 41 | TEST_F(ResourceTableTest, FailToAddResourceWithBadName) { |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 42 | ResourceTable table; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 43 | |
| 44 | EXPECT_FALSE(table.addResource( |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame] | 45 | ResourceNameRef(u"android", ResourceType::kId, u"hey,there"), |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 46 | ConfigDescription{}, "", |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame] | 47 | test::ValueBuilder<Id>().setSource("test.xml", 21u).build(), |
| 48 | &mDiagnostics)); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 49 | |
| 50 | EXPECT_FALSE(table.addResource( |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame] | 51 | ResourceNameRef(u"android", ResourceType::kId, u"hey:there"), |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 52 | ConfigDescription{}, "", |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame] | 53 | test::ValueBuilder<Id>().setSource("test.xml", 21u).build(), |
| 54 | &mDiagnostics)); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 55 | } |
| 56 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 57 | TEST_F(ResourceTableTest, AddOneResource) { |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 58 | ResourceTable table; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 59 | |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame] | 60 | EXPECT_TRUE(table.addResource(test::parseNameOrDie(u"@android:attr/id"), |
| 61 | ConfigDescription{}, |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 62 | "", |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame] | 63 | test::ValueBuilder<Id>() |
| 64 | .setSource("test/path/file.xml", 23u).build(), |
| 65 | &mDiagnostics)); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 66 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 67 | ASSERT_NE(nullptr, test::getValue<Id>(&table, u"@android:attr/id")); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 68 | } |
| 69 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 70 | TEST_F(ResourceTableTest, AddMultipleResources) { |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 71 | ResourceTable table; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 72 | |
| 73 | ConfigDescription config; |
| 74 | ConfigDescription languageConfig; |
| 75 | memcpy(languageConfig.language, "pl", sizeof(languageConfig.language)); |
| 76 | |
| 77 | EXPECT_TRUE(table.addResource( |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 78 | test::parseNameOrDie(u"@android:attr/layout_width"), |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame] | 79 | config, |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 80 | "", |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame] | 81 | test::ValueBuilder<Id>().setSource("test/path/file.xml", 10u).build(), |
| 82 | &mDiagnostics)); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 83 | |
| 84 | EXPECT_TRUE(table.addResource( |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 85 | test::parseNameOrDie(u"@android:attr/id"), |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame] | 86 | config, |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 87 | "", |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame] | 88 | test::ValueBuilder<Id>().setSource("test/path/file.xml", 12u).build(), |
| 89 | &mDiagnostics)); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 90 | |
| 91 | EXPECT_TRUE(table.addResource( |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 92 | test::parseNameOrDie(u"@android:string/ok"), |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame] | 93 | config, |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 94 | "", |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame] | 95 | test::ValueBuilder<Id>().setSource("test/path/file.xml", 14u).build(), |
| 96 | &mDiagnostics)); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 97 | |
| 98 | EXPECT_TRUE(table.addResource( |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 99 | test::parseNameOrDie(u"@android:string/ok"), |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame] | 100 | languageConfig, |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 101 | "", |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame] | 102 | test::ValueBuilder<BinaryPrimitive>(android::Res_value{}) |
| 103 | .setSource("test/path/file.xml", 20u) |
| 104 | .build(), |
| 105 | &mDiagnostics)); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 106 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 107 | ASSERT_NE(nullptr, test::getValue<Id>(&table, u"@android:attr/layout_width")); |
| 108 | ASSERT_NE(nullptr, test::getValue<Id>(&table, u"@android:attr/id")); |
| 109 | ASSERT_NE(nullptr, test::getValue<Id>(&table, u"@android:string/ok")); |
| 110 | ASSERT_NE(nullptr, test::getValueForConfig<BinaryPrimitive>(&table, u"@android:string/ok", |
| 111 | languageConfig)); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 112 | } |
| 113 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 114 | TEST_F(ResourceTableTest, OverrideWeakResourceValue) { |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 115 | ResourceTable table; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 116 | |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame] | 117 | ASSERT_TRUE(table.addResource(test::parseNameOrDie(u"@android:attr/foo"), ConfigDescription{}, |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 118 | "", util::make_unique<Attribute>(true), &mDiagnostics)); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 119 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 120 | Attribute* attr = test::getValue<Attribute>(&table, u"@android:attr/foo"); |
| 121 | ASSERT_NE(nullptr, attr); |
| 122 | EXPECT_TRUE(attr->isWeak()); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 123 | |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame] | 124 | ASSERT_TRUE(table.addResource(test::parseNameOrDie(u"@android:attr/foo"), ConfigDescription{}, |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 125 | "", util::make_unique<Attribute>(false), &mDiagnostics)); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 126 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 127 | attr = test::getValue<Attribute>(&table, u"@android:attr/foo"); |
| 128 | ASSERT_NE(nullptr, attr); |
| 129 | EXPECT_FALSE(attr->isWeak()); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 130 | } |
| 131 | |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 132 | TEST_F(ResourceTableTest, ProductVaryingValues) { |
| 133 | ResourceTable table; |
| 134 | |
| 135 | EXPECT_TRUE(table.addResource(test::parseNameOrDie(u"@android:string/foo"), |
| 136 | test::parseConfigOrDie("land"), |
| 137 | "tablet", |
| 138 | util::make_unique<Id>(), |
| 139 | &mDiagnostics)); |
| 140 | EXPECT_TRUE(table.addResource(test::parseNameOrDie(u"@android:string/foo"), |
| 141 | test::parseConfigOrDie("land"), |
| 142 | "phone", |
| 143 | util::make_unique<Id>(), |
| 144 | &mDiagnostics)); |
| 145 | |
| 146 | EXPECT_NE(nullptr, test::getValueForConfigAndProduct<Id>(&table, u"@android:string/foo", |
| 147 | test::parseConfigOrDie("land"), |
| 148 | "tablet")); |
| 149 | EXPECT_NE(nullptr, test::getValueForConfigAndProduct<Id>(&table, u"@android:string/foo", |
| 150 | test::parseConfigOrDie("land"), |
| 151 | "phone")); |
| 152 | |
| 153 | Maybe<ResourceTable::SearchResult> sr = table.findResource( |
| 154 | test::parseNameOrDie(u"@android:string/foo")); |
| 155 | AAPT_ASSERT_TRUE(sr); |
| 156 | std::vector<ResourceConfigValue*> values = sr.value().entry->findAllValues( |
| 157 | test::parseConfigOrDie("land")); |
| 158 | ASSERT_EQ(2u, values.size()); |
| 159 | EXPECT_EQ(std::string("phone"), values[0]->product); |
| 160 | EXPECT_EQ(std::string("tablet"), values[1]->product); |
| 161 | } |
| 162 | |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 163 | } // namespace aapt |