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 | |
| 17 | #include "ResourceTable.h" |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 18 | #include "Diagnostics.h" |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 19 | #include "ResourceValues.h" |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 20 | #include "test/Test.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 21 | #include "util/Util.h" |
| 22 | |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 23 | #include <algorithm> |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 24 | #include <ostream> |
| 25 | #include <string> |
| 26 | |
MÃ¥rten Kongstad | 24c9aa6 | 2018-06-20 08:46:41 +0200 | [diff] [blame] | 27 | using ::android::ConfigDescription; |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 28 | using ::android::StringPiece; |
| 29 | using ::testing::Eq; |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 30 | using ::testing::NotNull; |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 31 | using ::testing::StrEq; |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 32 | |
Winson | 62ac8b5 | 2019-12-04 08:36:48 -0800 | [diff] [blame^] | 33 | using PolicyFlags = android::ResTable_overlayable_policy_header::PolicyFlags; |
| 34 | |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 35 | namespace aapt { |
| 36 | |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 37 | TEST(ResourceTableTest, FailToAddResourceWithBadName) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 38 | ResourceTable table; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 39 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 40 | EXPECT_FALSE(table.AddResource( |
| 41 | test::ParseNameOrDie("android:id/hey,there"), ConfigDescription{}, "", |
| 42 | test::ValueBuilder<Id>().SetSource("test.xml", 21u).Build(), |
| 43 | test::GetDiagnostics())); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 44 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 45 | EXPECT_FALSE(table.AddResource( |
| 46 | test::ParseNameOrDie("android:id/hey:there"), ConfigDescription{}, "", |
| 47 | test::ValueBuilder<Id>().SetSource("test.xml", 21u).Build(), |
| 48 | test::GetDiagnostics())); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 49 | } |
| 50 | |
Adam Lesinski | b1afa07 | 2017-03-29 13:52:38 -0700 | [diff] [blame] | 51 | TEST(ResourceTableTest, AddResourceWithWeirdNameWhenAddingMangledResources) { |
| 52 | ResourceTable table; |
| 53 | |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 54 | EXPECT_TRUE(table.AddResourceMangled( |
Adam Lesinski | b1afa07 | 2017-03-29 13:52:38 -0700 | [diff] [blame] | 55 | test::ParseNameOrDie("android:id/heythere "), ConfigDescription{}, "", |
| 56 | test::ValueBuilder<Id>().SetSource("test.xml", 21u).Build(), test::GetDiagnostics())); |
| 57 | } |
| 58 | |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 59 | TEST(ResourceTableTest, AddOneResource) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 60 | ResourceTable table; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 61 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 62 | EXPECT_TRUE(table.AddResource( |
| 63 | test::ParseNameOrDie("android:attr/id"), ConfigDescription{}, "", |
| 64 | test::ValueBuilder<Id>().SetSource("test/path/file.xml", 23u).Build(), |
| 65 | test::GetDiagnostics())); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 66 | |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 67 | EXPECT_THAT(test::GetValue<Id>(&table, "android:attr/id"), NotNull()); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 68 | } |
| 69 | |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 70 | TEST(ResourceTableTest, AddMultipleResources) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 71 | ResourceTable table; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 72 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 73 | ConfigDescription config; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 74 | ConfigDescription language_config; |
| 75 | memcpy(language_config.language, "pl", sizeof(language_config.language)); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 76 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 77 | EXPECT_TRUE(table.AddResource( |
| 78 | test::ParseNameOrDie("android:attr/layout_width"), config, "", |
| 79 | test::ValueBuilder<Id>().SetSource("test/path/file.xml", 10u).Build(), |
| 80 | test::GetDiagnostics())); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 81 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 82 | EXPECT_TRUE(table.AddResource( |
| 83 | test::ParseNameOrDie("android:attr/id"), config, "", |
| 84 | test::ValueBuilder<Id>().SetSource("test/path/file.xml", 12u).Build(), |
| 85 | test::GetDiagnostics())); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 86 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 87 | EXPECT_TRUE(table.AddResource( |
| 88 | test::ParseNameOrDie("android:string/ok"), config, "", |
| 89 | test::ValueBuilder<Id>().SetSource("test/path/file.xml", 14u).Build(), |
| 90 | test::GetDiagnostics())); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 91 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 92 | EXPECT_TRUE(table.AddResource( |
| 93 | test::ParseNameOrDie("android:string/ok"), language_config, "", |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 94 | test::ValueBuilder<BinaryPrimitive>(android::Res_value{}) |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 95 | .SetSource("test/path/file.xml", 20u) |
| 96 | .Build(), |
| 97 | test::GetDiagnostics())); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 98 | |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 99 | EXPECT_THAT(test::GetValue<Id>(&table, "android:attr/layout_width"), NotNull()); |
| 100 | EXPECT_THAT(test::GetValue<Id>(&table, "android:attr/id"), NotNull()); |
| 101 | EXPECT_THAT(test::GetValue<Id>(&table, "android:string/ok"), NotNull()); |
| 102 | EXPECT_THAT(test::GetValueForConfig<BinaryPrimitive>(&table, "android:string/ok", language_config), NotNull()); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 103 | } |
| 104 | |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 105 | TEST(ResourceTableTest, OverrideWeakResourceValue) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 106 | ResourceTable table; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 107 | |
Adam Lesinski | 73bff1e | 2017-12-08 16:06:10 -0800 | [diff] [blame] | 108 | ASSERT_TRUE(table.AddResource(test::ParseNameOrDie("android:attr/foo"), ConfigDescription{}, "", |
| 109 | test::AttributeBuilder().SetWeak(true).Build(), |
| 110 | test::GetDiagnostics())); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 111 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 112 | Attribute* attr = test::GetValue<Attribute>(&table, "android:attr/foo"); |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 113 | ASSERT_THAT(attr, NotNull()); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 114 | EXPECT_TRUE(attr->IsWeak()); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 115 | |
Adam Lesinski | 73bff1e | 2017-12-08 16:06:10 -0800 | [diff] [blame] | 116 | ASSERT_TRUE(table.AddResource(test::ParseNameOrDie("android:attr/foo"), ConfigDescription{}, "", |
| 117 | util::make_unique<Attribute>(), test::GetDiagnostics())); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 118 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 119 | attr = test::GetValue<Attribute>(&table, "android:attr/foo"); |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 120 | ASSERT_THAT(attr, NotNull()); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 121 | EXPECT_FALSE(attr->IsWeak()); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 122 | } |
| 123 | |
Adam Lesinski | 73bff1e | 2017-12-08 16:06:10 -0800 | [diff] [blame] | 124 | TEST(ResourceTableTest, AllowCompatibleDuplicateAttributes) { |
| 125 | ResourceTable table; |
| 126 | |
| 127 | const ResourceName name = test::ParseNameOrDie("android:attr/foo"); |
| 128 | Attribute attr_one(android::ResTable_map::TYPE_STRING); |
| 129 | attr_one.SetWeak(true); |
| 130 | Attribute attr_two(android::ResTable_map::TYPE_STRING | android::ResTable_map::TYPE_REFERENCE); |
| 131 | attr_two.SetWeak(true); |
| 132 | |
| 133 | ASSERT_TRUE(table.AddResource(name, ConfigDescription{}, "", |
| 134 | util::make_unique<Attribute>(attr_one), test::GetDiagnostics())); |
| 135 | ASSERT_TRUE(table.AddResource(name, ConfigDescription{}, "", |
| 136 | util::make_unique<Attribute>(attr_two), test::GetDiagnostics())); |
| 137 | } |
| 138 | |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 139 | TEST(ResourceTableTest, ProductVaryingValues) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 140 | ResourceTable table; |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 141 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 142 | EXPECT_TRUE(table.AddResource(test::ParseNameOrDie("android:string/foo"), |
| 143 | test::ParseConfigOrDie("land"), "tablet", |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 144 | util::make_unique<Id>(), |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 145 | test::GetDiagnostics())); |
| 146 | EXPECT_TRUE(table.AddResource(test::ParseNameOrDie("android:string/foo"), |
| 147 | test::ParseConfigOrDie("land"), "phone", |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 148 | util::make_unique<Id>(), |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 149 | test::GetDiagnostics())); |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 150 | |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 151 | EXPECT_THAT(test::GetValueForConfigAndProduct<Id>(&table, "android:string/foo",test::ParseConfigOrDie("land"), "tablet"), NotNull()); |
| 152 | EXPECT_THAT(test::GetValueForConfigAndProduct<Id>(&table, "android:string/foo",test::ParseConfigOrDie("land"), "phone"), NotNull()); |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 153 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 154 | Maybe<ResourceTable::SearchResult> sr = |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 155 | table.FindResource(test::ParseNameOrDie("android:string/foo")); |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 156 | ASSERT_TRUE(sr); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 157 | std::vector<ResourceConfigValue*> values = |
Adam Lesinski | b1afa07 | 2017-03-29 13:52:38 -0700 | [diff] [blame] | 158 | sr.value().entry->FindAllValues(test::ParseConfigOrDie("land")); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 159 | ASSERT_EQ(2u, values.size()); |
| 160 | EXPECT_EQ(std::string("phone"), values[0]->product); |
| 161 | EXPECT_EQ(std::string("tablet"), values[1]->product); |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 162 | } |
| 163 | |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 164 | static StringPiece LevelToString(Visibility::Level level) { |
| 165 | switch (level) { |
| 166 | case Visibility::Level::kPrivate: |
| 167 | return "private"; |
| 168 | case Visibility::Level::kPublic: |
| 169 | return "private"; |
| 170 | default: |
| 171 | return "undefined"; |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | static ::testing::AssertionResult VisibilityOfResource(const ResourceTable& table, |
| 176 | const ResourceNameRef& name, |
| 177 | Visibility::Level level, |
| 178 | const StringPiece& comment) { |
| 179 | Maybe<ResourceTable::SearchResult> result = table.FindResource(name); |
| 180 | if (!result) { |
| 181 | return ::testing::AssertionFailure() << "no resource '" << name << "' found in table"; |
| 182 | } |
| 183 | |
| 184 | const Visibility& visibility = result.value().entry->visibility; |
| 185 | if (visibility.level != level) { |
| 186 | return ::testing::AssertionFailure() << "expected visibility " << LevelToString(level) |
| 187 | << " but got " << LevelToString(visibility.level); |
| 188 | } |
| 189 | |
| 190 | if (visibility.comment != comment) { |
| 191 | return ::testing::AssertionFailure() << "expected visibility comment '" << comment |
| 192 | << "' but got '" << visibility.comment << "'"; |
| 193 | } |
| 194 | return ::testing::AssertionSuccess(); |
| 195 | } |
| 196 | |
| 197 | TEST(ResourceTableTest, SetVisibility) { |
| 198 | using Level = Visibility::Level; |
| 199 | |
| 200 | ResourceTable table; |
| 201 | const ResourceName name = test::ParseNameOrDie("android:string/foo"); |
| 202 | |
| 203 | Visibility visibility; |
| 204 | visibility.level = Visibility::Level::kPrivate; |
| 205 | visibility.comment = "private"; |
| 206 | ASSERT_TRUE(table.SetVisibility(name, visibility, test::GetDiagnostics())); |
| 207 | ASSERT_TRUE(VisibilityOfResource(table, name, Level::kPrivate, "private")); |
| 208 | |
| 209 | visibility.level = Visibility::Level::kUndefined; |
| 210 | visibility.comment = "undefined"; |
| 211 | ASSERT_TRUE(table.SetVisibility(name, visibility, test::GetDiagnostics())); |
| 212 | ASSERT_TRUE(VisibilityOfResource(table, name, Level::kPrivate, "private")); |
| 213 | |
| 214 | visibility.level = Visibility::Level::kPublic; |
| 215 | visibility.comment = "public"; |
| 216 | ASSERT_TRUE(table.SetVisibility(name, visibility, test::GetDiagnostics())); |
| 217 | ASSERT_TRUE(VisibilityOfResource(table, name, Level::kPublic, "public")); |
| 218 | |
| 219 | visibility.level = Visibility::Level::kPrivate; |
| 220 | visibility.comment = "private"; |
| 221 | ASSERT_TRUE(table.SetVisibility(name, visibility, test::GetDiagnostics())); |
| 222 | ASSERT_TRUE(VisibilityOfResource(table, name, Level::kPublic, "public")); |
| 223 | } |
| 224 | |
| 225 | TEST(ResourceTableTest, SetAllowNew) { |
| 226 | ResourceTable table; |
| 227 | const ResourceName name = test::ParseNameOrDie("android:string/foo"); |
| 228 | |
| 229 | AllowNew allow_new; |
| 230 | Maybe<ResourceTable::SearchResult> result; |
| 231 | |
| 232 | allow_new.comment = "first"; |
| 233 | ASSERT_TRUE(table.SetAllowNew(name, allow_new, test::GetDiagnostics())); |
| 234 | result = table.FindResource(name); |
| 235 | ASSERT_TRUE(result); |
| 236 | ASSERT_TRUE(result.value().entry->allow_new); |
| 237 | ASSERT_THAT(result.value().entry->allow_new.value().comment, StrEq("first")); |
| 238 | |
| 239 | allow_new.comment = "second"; |
| 240 | ASSERT_TRUE(table.SetAllowNew(name, allow_new, test::GetDiagnostics())); |
| 241 | result = table.FindResource(name); |
| 242 | ASSERT_TRUE(result); |
| 243 | ASSERT_TRUE(result.value().entry->allow_new); |
| 244 | ASSERT_THAT(result.value().entry->allow_new.value().comment, StrEq("second")); |
| 245 | } |
| 246 | |
Ryan Mitchell | 1bb1fe0 | 2018-11-16 11:21:41 -0800 | [diff] [blame] | 247 | TEST(ResourceTableTest, SetOverlayable) { |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 248 | ResourceTable table; |
Ryan Mitchell | 54237ff | 2018-12-13 15:44:29 -0800 | [diff] [blame] | 249 | auto overlayable = std::make_shared<Overlayable>("Name", "overlay://theme", |
| 250 | Source("res/values/overlayable.xml", 40)); |
| 251 | OverlayableItem overlayable_item(overlayable); |
Winson | 62ac8b5 | 2019-12-04 08:36:48 -0800 | [diff] [blame^] | 252 | overlayable_item.policies |= PolicyFlags::PRODUCT_PARTITION; |
| 253 | overlayable_item.policies |= PolicyFlags::VENDOR_PARTITION; |
Ryan Mitchell | 54237ff | 2018-12-13 15:44:29 -0800 | [diff] [blame] | 254 | overlayable_item.comment = "comment"; |
| 255 | overlayable_item.source = Source("res/values/overlayable.xml", 42); |
Ryan Mitchell | 1bb1fe0 | 2018-11-16 11:21:41 -0800 | [diff] [blame] | 256 | |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 257 | const ResourceName name = test::ParseNameOrDie("android:string/foo"); |
Ryan Mitchell | 54237ff | 2018-12-13 15:44:29 -0800 | [diff] [blame] | 258 | ASSERT_TRUE(table.SetOverlayable(name, overlayable_item, test::GetDiagnostics())); |
Ryan Mitchell | 1bb1fe0 | 2018-11-16 11:21:41 -0800 | [diff] [blame] | 259 | Maybe<ResourceTable::SearchResult> search_result = table.FindResource(name); |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 260 | |
Ryan Mitchell | 1bb1fe0 | 2018-11-16 11:21:41 -0800 | [diff] [blame] | 261 | ASSERT_TRUE(search_result); |
Ryan Mitchell | 54237ff | 2018-12-13 15:44:29 -0800 | [diff] [blame] | 262 | ASSERT_TRUE(search_result.value().entry->overlayable_item); |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 263 | |
Ryan Mitchell | 54237ff | 2018-12-13 15:44:29 -0800 | [diff] [blame] | 264 | OverlayableItem& result_overlayable_item = search_result.value().entry->overlayable_item.value(); |
| 265 | EXPECT_THAT(result_overlayable_item.overlayable->name, Eq("Name")); |
| 266 | EXPECT_THAT(result_overlayable_item.overlayable->actor, Eq("overlay://theme")); |
| 267 | EXPECT_THAT(result_overlayable_item.overlayable->source.path, Eq("res/values/overlayable.xml")); |
| 268 | EXPECT_THAT(result_overlayable_item.overlayable->source.line, 40); |
Winson | 62ac8b5 | 2019-12-04 08:36:48 -0800 | [diff] [blame^] | 269 | EXPECT_THAT(result_overlayable_item.policies, Eq(PolicyFlags::PRODUCT_PARTITION |
| 270 | | PolicyFlags::VENDOR_PARTITION)); |
Ryan Mitchell | 54237ff | 2018-12-13 15:44:29 -0800 | [diff] [blame] | 271 | ASSERT_THAT(result_overlayable_item.comment, StrEq("comment")); |
| 272 | EXPECT_THAT(result_overlayable_item.source.path, Eq("res/values/overlayable.xml")); |
| 273 | EXPECT_THAT(result_overlayable_item.source.line, 42); |
Ryan Mitchell | e4e989c | 2018-10-29 02:21:50 -0700 | [diff] [blame] | 274 | } |
| 275 | |
Ryan Mitchell | 54237ff | 2018-12-13 15:44:29 -0800 | [diff] [blame] | 276 | TEST(ResourceTableTest, SetMultipleOverlayableResources) { |
| 277 | ResourceTable table; |
| 278 | |
| 279 | const ResourceName foo = test::ParseNameOrDie("android:string/foo"); |
| 280 | auto group = std::make_shared<Overlayable>("Name", "overlay://theme"); |
| 281 | OverlayableItem overlayable(group); |
Winson | 62ac8b5 | 2019-12-04 08:36:48 -0800 | [diff] [blame^] | 282 | overlayable.policies = PolicyFlags::PRODUCT_PARTITION; |
Ryan Mitchell | 54237ff | 2018-12-13 15:44:29 -0800 | [diff] [blame] | 283 | ASSERT_TRUE(table.SetOverlayable(foo, overlayable, test::GetDiagnostics())); |
| 284 | |
| 285 | const ResourceName bar = test::ParseNameOrDie("android:string/bar"); |
| 286 | OverlayableItem overlayable2(group); |
Winson | 62ac8b5 | 2019-12-04 08:36:48 -0800 | [diff] [blame^] | 287 | overlayable2.policies = PolicyFlags::PRODUCT_PARTITION; |
Ryan Mitchell | 54237ff | 2018-12-13 15:44:29 -0800 | [diff] [blame] | 288 | ASSERT_TRUE(table.SetOverlayable(bar, overlayable2, test::GetDiagnostics())); |
| 289 | |
| 290 | const ResourceName baz = test::ParseNameOrDie("android:string/baz"); |
| 291 | OverlayableItem overlayable3(group); |
Winson | 62ac8b5 | 2019-12-04 08:36:48 -0800 | [diff] [blame^] | 292 | overlayable3.policies = PolicyFlags::VENDOR_PARTITION; |
Ryan Mitchell | 54237ff | 2018-12-13 15:44:29 -0800 | [diff] [blame] | 293 | ASSERT_TRUE(table.SetOverlayable(baz, overlayable3, test::GetDiagnostics())); |
| 294 | } |
| 295 | |
| 296 | TEST(ResourceTableTest, SetOverlayableDifferentResourcesDifferentName) { |
| 297 | ResourceTable table; |
| 298 | |
| 299 | const ResourceName foo = test::ParseNameOrDie("android:string/foo"); |
| 300 | OverlayableItem overlayable_item(std::make_shared<Overlayable>("Name", "overlay://theme")); |
Winson | 62ac8b5 | 2019-12-04 08:36:48 -0800 | [diff] [blame^] | 301 | overlayable_item.policies = PolicyFlags::PRODUCT_PARTITION; |
Ryan Mitchell | 54237ff | 2018-12-13 15:44:29 -0800 | [diff] [blame] | 302 | ASSERT_TRUE(table.SetOverlayable(foo, overlayable_item, test::GetDiagnostics())); |
| 303 | |
| 304 | const ResourceName bar = test::ParseNameOrDie("android:string/bar"); |
| 305 | OverlayableItem overlayable_item2(std::make_shared<Overlayable>("Name2", "overlay://theme")); |
Winson | 62ac8b5 | 2019-12-04 08:36:48 -0800 | [diff] [blame^] | 306 | overlayable_item2.policies = PolicyFlags::PRODUCT_PARTITION; |
Ryan Mitchell | 54237ff | 2018-12-13 15:44:29 -0800 | [diff] [blame] | 307 | ASSERT_TRUE(table.SetOverlayable(bar, overlayable_item2, test::GetDiagnostics())); |
| 308 | } |
| 309 | |
| 310 | TEST(ResourceTableTest, SetOverlayableSameResourcesFail) { |
Ryan Mitchell | e4e989c | 2018-10-29 02:21:50 -0700 | [diff] [blame] | 311 | ResourceTable table; |
| 312 | const ResourceName name = test::ParseNameOrDie("android:string/foo"); |
| 313 | |
Ryan Mitchell | 54237ff | 2018-12-13 15:44:29 -0800 | [diff] [blame] | 314 | auto overlayable = std::make_shared<Overlayable>("Name", "overlay://theme"); |
| 315 | OverlayableItem overlayable_item(overlayable); |
| 316 | ASSERT_TRUE(table.SetOverlayable(name, overlayable_item, test::GetDiagnostics())); |
Ryan Mitchell | e4e989c | 2018-10-29 02:21:50 -0700 | [diff] [blame] | 317 | |
Ryan Mitchell | 54237ff | 2018-12-13 15:44:29 -0800 | [diff] [blame] | 318 | OverlayableItem overlayable_item2(overlayable); |
| 319 | ASSERT_FALSE(table.SetOverlayable(name, overlayable_item2, test::GetDiagnostics())); |
Ryan Mitchell | e4e989c | 2018-10-29 02:21:50 -0700 | [diff] [blame] | 320 | } |
| 321 | |
Ryan Mitchell | 54237ff | 2018-12-13 15:44:29 -0800 | [diff] [blame] | 322 | TEST(ResourceTableTest, SetOverlayableSameResourcesDifferentNameFail) { |
Ryan Mitchell | e4e989c | 2018-10-29 02:21:50 -0700 | [diff] [blame] | 323 | ResourceTable table; |
| 324 | const ResourceName name = test::ParseNameOrDie("android:string/foo"); |
| 325 | |
Ryan Mitchell | 54237ff | 2018-12-13 15:44:29 -0800 | [diff] [blame] | 326 | auto overlayable = std::make_shared<Overlayable>("Name", "overlay://theme"); |
| 327 | OverlayableItem overlayable_item(overlayable); |
| 328 | ASSERT_TRUE(table.SetOverlayable(name, overlayable_item, test::GetDiagnostics())); |
Ryan Mitchell | e4e989c | 2018-10-29 02:21:50 -0700 | [diff] [blame] | 329 | |
Ryan Mitchell | 54237ff | 2018-12-13 15:44:29 -0800 | [diff] [blame] | 330 | auto overlayable2 = std::make_shared<Overlayable>("Other", "overlay://theme"); |
| 331 | OverlayableItem overlayable_item2(overlayable2); |
| 332 | ASSERT_FALSE(table.SetOverlayable(name, overlayable_item2, test::GetDiagnostics())); |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 333 | } |
| 334 | |
Ryan Mitchell | 8d4ee97 | 2018-08-27 11:24:04 -0700 | [diff] [blame] | 335 | TEST(ResourceTableTest, AllowDuplictaeResourcesNames) { |
| 336 | ResourceTable table(/* validate_resources */ false); |
| 337 | |
| 338 | const ResourceName foo_name = test::ParseNameOrDie("android:bool/foo"); |
| 339 | ASSERT_TRUE(table.AddResourceWithId(foo_name, ResourceId(0x7f0100ff), ConfigDescription{} , "", |
| 340 | test::BuildPrimitive(android::Res_value::TYPE_INT_BOOLEAN, 0), |
| 341 | test::GetDiagnostics())); |
| 342 | ASSERT_TRUE(table.AddResourceWithId(foo_name, ResourceId(0x7f010100), ConfigDescription{} , "", |
| 343 | test::BuildPrimitive(android::Res_value::TYPE_INT_BOOLEAN, 1), |
| 344 | test::GetDiagnostics())); |
| 345 | |
| 346 | ASSERT_TRUE(table.SetVisibilityWithId(foo_name, Visibility{Visibility::Level::kPublic}, |
| 347 | ResourceId(0x7f0100ff), test::GetDiagnostics())); |
| 348 | ASSERT_TRUE(table.SetVisibilityWithId(foo_name, Visibility{Visibility::Level::kPrivate}, |
| 349 | ResourceId(0x7f010100), test::GetDiagnostics())); |
| 350 | |
| 351 | auto package = table.FindPackageById(0x7f); |
| 352 | ASSERT_THAT(package, NotNull()); |
| 353 | auto type = package->FindType(ResourceType::kBool); |
| 354 | ASSERT_THAT(type, NotNull()); |
| 355 | |
| 356 | auto entry1 = type->FindEntry("foo", 0x00ff); |
| 357 | ASSERT_THAT(entry1, NotNull()); |
| 358 | ASSERT_THAT(entry1->id, Eq(0x00ff)); |
| 359 | ASSERT_THAT(entry1->values[0], NotNull()); |
| 360 | ASSERT_THAT(entry1->values[0]->value, NotNull()); |
| 361 | ASSERT_THAT(ValueCast<BinaryPrimitive>(entry1->values[0]->value.get()), NotNull()); |
| 362 | ASSERT_THAT(ValueCast<BinaryPrimitive>(entry1->values[0]->value.get())->value.data, Eq(0u)); |
| 363 | ASSERT_THAT(entry1->visibility.level, Visibility::Level::kPublic); |
| 364 | |
| 365 | auto entry2 = type->FindEntry("foo", 0x0100); |
| 366 | ASSERT_THAT(entry2, NotNull()); |
| 367 | ASSERT_THAT(entry2->id, Eq(0x0100)); |
| 368 | ASSERT_THAT(entry2->values[0], NotNull()); |
| 369 | ASSERT_THAT(entry1->values[0]->value, NotNull()); |
| 370 | ASSERT_THAT(ValueCast<BinaryPrimitive>(entry2->values[0]->value.get()), NotNull()); |
| 371 | ASSERT_THAT(ValueCast<BinaryPrimitive>(entry2->values[0]->value.get())->value.data, Eq(1u)); |
| 372 | ASSERT_THAT(entry2->visibility.level, Visibility::Level::kPrivate); |
| 373 | } |
| 374 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 375 | } // namespace aapt |