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