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