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