Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 17 | #include "link/TableMerger.h" |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 18 | |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 19 | #include "filter/ConfigFilter.h" |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 20 | #include "io/FileSystem.h" |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 21 | #include "test/Test.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 22 | |
Adam Lesinski | 5924d8c | 2017-05-30 15:15:58 -0700 | [diff] [blame] | 23 | using ::aapt::test::ValueEq; |
| 24 | using ::testing::Contains; |
Adam Lesinski | 5924d8c | 2017-05-30 15:15:58 -0700 | [diff] [blame] | 25 | using ::testing::Eq; |
Adam Lesinski | 8780eb6 | 2017-10-31 17:44:39 -0700 | [diff] [blame] | 26 | using ::testing::Field; |
| 27 | using ::testing::NotNull; |
| 28 | using ::testing::Pointee; |
| 29 | using ::testing::StrEq; |
| 30 | using ::testing::UnorderedElementsAreArray; |
Adam Lesinski | 5924d8c | 2017-05-30 15:15:58 -0700 | [diff] [blame] | 31 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 32 | namespace aapt { |
| 33 | |
| 34 | struct TableMergerTest : public ::testing::Test { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 35 | std::unique_ptr<IAaptContext> context_; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 36 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 37 | void SetUp() override { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 38 | context_ = |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 39 | test::ContextBuilder() |
| 40 | // We are compiling this package. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 41 | .SetCompilationPackage("com.app.a") |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 42 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 43 | // Merge all packages that have this package ID. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 44 | .SetPackageId(0x7f) |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 45 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 46 | // Mangle all packages that do not have this package name. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 47 | .SetNameManglerPolicy(NameManglerPolicy{"com.app.a", {"com.app.b"}}) |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 48 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 49 | .Build(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 50 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 51 | }; |
| 52 | |
| 53 | TEST_F(TableMergerTest, SimpleMerge) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 54 | std::unique_ptr<ResourceTable> table_a = |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 55 | test::ResourceTableBuilder() |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 56 | .SetPackageId("com.app.a", 0x7f) |
| 57 | .AddReference("com.app.a:id/foo", "com.app.a:id/bar") |
| 58 | .AddReference("com.app.a:id/bar", "com.app.b:id/foo") |
| 59 | .AddValue( |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 60 | "com.app.a:styleable/view", |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 61 | test::StyleableBuilder().AddItem("com.app.b:id/foo").Build()) |
| 62 | .Build(); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 63 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 64 | std::unique_ptr<ResourceTable> table_b = test::ResourceTableBuilder() |
| 65 | .SetPackageId("com.app.b", 0x7f) |
| 66 | .AddSimple("com.app.b:id/foo") |
| 67 | .Build(); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 68 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 69 | ResourceTable final_table; |
| 70 | TableMerger merger(context_.get(), &final_table, TableMergerOptions{}); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 71 | |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 72 | ASSERT_TRUE(merger.Merge({}, table_a.get(), false /*overlay*/)); |
Adam Lesinski | 8780eb6 | 2017-10-31 17:44:39 -0700 | [diff] [blame] | 73 | ASSERT_TRUE(merger.MergeAndMangle({}, "com.app.b", table_b.get())); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 74 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 75 | EXPECT_TRUE(merger.merged_packages().count("com.app.b") != 0); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 76 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 77 | // Entries from com.app.a should not be mangled. |
Adam Lesinski | 5924d8c | 2017-05-30 15:15:58 -0700 | [diff] [blame] | 78 | EXPECT_TRUE(final_table.FindResource(test::ParseNameOrDie("com.app.a:id/foo"))); |
| 79 | EXPECT_TRUE(final_table.FindResource(test::ParseNameOrDie("com.app.a:id/bar"))); |
| 80 | EXPECT_TRUE(final_table.FindResource(test::ParseNameOrDie("com.app.a:styleable/view"))); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 81 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 82 | // The unmangled name should not be present. |
Adam Lesinski | 5924d8c | 2017-05-30 15:15:58 -0700 | [diff] [blame] | 83 | EXPECT_FALSE(final_table.FindResource(test::ParseNameOrDie("com.app.b:id/foo"))); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 84 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 85 | // Look for the mangled name. |
Adam Lesinski | 5924d8c | 2017-05-30 15:15:58 -0700 | [diff] [blame] | 86 | EXPECT_TRUE(final_table.FindResource(test::ParseNameOrDie("com.app.a:id/com.app.b$foo"))); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 87 | } |
| 88 | |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 89 | TEST_F(TableMergerTest, MergeFile) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 90 | ResourceTable final_table; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 91 | TableMergerOptions options; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 92 | options.auto_add_overlay = false; |
| 93 | TableMerger merger(context_.get(), &final_table, options); |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 94 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 95 | ResourceFile file_desc; |
| 96 | file_desc.config = test::ParseConfigOrDie("hdpi-v4"); |
| 97 | file_desc.name = test::ParseNameOrDie("layout/main"); |
| 98 | file_desc.source = Source("res/layout-hdpi/main.xml"); |
| 99 | test::TestFile test_file("path/to/res/layout-hdpi/main.xml.flat"); |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 100 | |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 101 | ASSERT_TRUE(merger.MergeFile(file_desc, false /*overlay*/, &test_file)); |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 102 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 103 | FileReference* file = test::GetValueForConfig<FileReference>( |
| 104 | &final_table, "com.app.a:layout/main", test::ParseConfigOrDie("hdpi-v4")); |
Adam Lesinski | 5924d8c | 2017-05-30 15:15:58 -0700 | [diff] [blame] | 105 | ASSERT_THAT(file, NotNull()); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 106 | EXPECT_EQ(std::string("res/layout-hdpi-v4/main.xml"), *file->path); |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | TEST_F(TableMergerTest, MergeFileOverlay) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 110 | ResourceTable final_table; |
| 111 | TableMergerOptions options; |
| 112 | options.auto_add_overlay = false; |
| 113 | TableMerger merger(context_.get(), &final_table, options); |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 114 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 115 | ResourceFile file_desc; |
| 116 | file_desc.name = test::ParseNameOrDie("xml/foo"); |
| 117 | test::TestFile file_a("path/to/fileA.xml.flat"); |
| 118 | test::TestFile file_b("path/to/fileB.xml.flat"); |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 119 | |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 120 | ASSERT_TRUE(merger.MergeFile(file_desc, false /*overlay*/, &file_a)); |
| 121 | ASSERT_TRUE(merger.MergeFile(file_desc, true /*overlay*/, &file_b)); |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 122 | } |
| 123 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 124 | TEST_F(TableMergerTest, MergeFileReferences) { |
Adam Lesinski | 8780eb6 | 2017-10-31 17:44:39 -0700 | [diff] [blame] | 125 | test::TestFile file_a("res/xml/file.xml"); |
| 126 | test::TestFile file_b("res/xml/file.xml"); |
| 127 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 128 | std::unique_ptr<ResourceTable> table_a = |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 129 | test::ResourceTableBuilder() |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 130 | .SetPackageId("com.app.a", 0x7f) |
Adam Lesinski | 8780eb6 | 2017-10-31 17:44:39 -0700 | [diff] [blame] | 131 | .AddFileReference("com.app.a:xml/file", "res/xml/file.xml", &file_a) |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 132 | .Build(); |
| 133 | std::unique_ptr<ResourceTable> table_b = |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 134 | test::ResourceTableBuilder() |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 135 | .SetPackageId("com.app.b", 0x7f) |
Adam Lesinski | 8780eb6 | 2017-10-31 17:44:39 -0700 | [diff] [blame] | 136 | .AddFileReference("com.app.b:xml/file", "res/xml/file.xml", &file_b) |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 137 | .Build(); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 138 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 139 | ResourceTable final_table; |
| 140 | TableMerger merger(context_.get(), &final_table, TableMergerOptions{}); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 141 | |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 142 | ASSERT_TRUE(merger.Merge({}, table_a.get(), false /*overlay*/)); |
Adam Lesinski | 8780eb6 | 2017-10-31 17:44:39 -0700 | [diff] [blame] | 143 | ASSERT_TRUE(merger.MergeAndMangle({}, "com.app.b", table_b.get())); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 144 | |
Adam Lesinski | 5924d8c | 2017-05-30 15:15:58 -0700 | [diff] [blame] | 145 | FileReference* f = test::GetValue<FileReference>(&final_table, "com.app.a:xml/file"); |
| 146 | ASSERT_THAT(f, NotNull()); |
Adam Lesinski | 8780eb6 | 2017-10-31 17:44:39 -0700 | [diff] [blame] | 147 | EXPECT_THAT(*f->path, StrEq("res/xml/file.xml")); |
| 148 | EXPECT_THAT(f->file, Eq(&file_a)); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 149 | |
Adam Lesinski | 5924d8c | 2017-05-30 15:15:58 -0700 | [diff] [blame] | 150 | f = test::GetValue<FileReference>(&final_table, "com.app.a:xml/com.app.b$file"); |
| 151 | ASSERT_THAT(f, NotNull()); |
Adam Lesinski | 8780eb6 | 2017-10-31 17:44:39 -0700 | [diff] [blame] | 152 | EXPECT_THAT(*f->path, StrEq("res/xml/com.app.b$file.xml")); |
| 153 | EXPECT_THAT(f->file, Eq(&file_b)); |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | TEST_F(TableMergerTest, OverrideResourceWithOverlay) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 157 | std::unique_ptr<ResourceTable> base = |
| 158 | test::ResourceTableBuilder() |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 159 | .SetPackageId("", 0x00) |
| 160 | .AddValue("bool/foo", ResourceUtils::TryParseBool("true")) |
| 161 | .Build(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 162 | std::unique_ptr<ResourceTable> overlay = |
| 163 | test::ResourceTableBuilder() |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 164 | .SetPackageId("", 0x00) |
| 165 | .AddValue("bool/foo", ResourceUtils::TryParseBool("false")) |
| 166 | .Build(); |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 167 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 168 | ResourceTable final_table; |
| 169 | TableMergerOptions options; |
| 170 | options.auto_add_overlay = false; |
| 171 | TableMerger merger(context_.get(), &final_table, options); |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 172 | |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 173 | ASSERT_TRUE(merger.Merge({}, base.get(), false /*overlay*/)); |
| 174 | ASSERT_TRUE(merger.Merge({}, overlay.get(), true /*overlay*/)); |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 175 | |
Adam Lesinski | 5924d8c | 2017-05-30 15:15:58 -0700 | [diff] [blame] | 176 | BinaryPrimitive* foo = test::GetValue<BinaryPrimitive>(&final_table, "com.app.a:bool/foo"); |
| 177 | ASSERT_THAT(foo, |
| 178 | Pointee(Field(&BinaryPrimitive::value, Field(&android::Res_value::data, Eq(0u))))); |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 179 | } |
| 180 | |
Alexandria Cornwall | 6a1f8db | 2016-08-11 13:49:44 -0700 | [diff] [blame] | 181 | TEST_F(TableMergerTest, OverrideSameResourceIdsWithOverlay) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 182 | std::unique_ptr<ResourceTable> base = |
| 183 | test::ResourceTableBuilder() |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 184 | .SetPackageId("", 0x7f) |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 185 | .SetSymbolState("bool/foo", ResourceId(0x7f, 0x01, 0x0001), Visibility::Level::kPublic) |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 186 | .Build(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 187 | std::unique_ptr<ResourceTable> overlay = |
| 188 | test::ResourceTableBuilder() |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 189 | .SetPackageId("", 0x7f) |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 190 | .SetSymbolState("bool/foo", ResourceId(0x7f, 0x01, 0x0001), Visibility::Level::kPublic) |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 191 | .Build(); |
Alexandria Cornwall | 6a1f8db | 2016-08-11 13:49:44 -0700 | [diff] [blame] | 192 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 193 | ResourceTable final_table; |
| 194 | TableMergerOptions options; |
| 195 | options.auto_add_overlay = false; |
| 196 | TableMerger merger(context_.get(), &final_table, options); |
Alexandria Cornwall | 6a1f8db | 2016-08-11 13:49:44 -0700 | [diff] [blame] | 197 | |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 198 | ASSERT_TRUE(merger.Merge({}, base.get(), false /*overlay*/)); |
| 199 | ASSERT_TRUE(merger.Merge({}, overlay.get(), true /*overlay*/)); |
Alexandria Cornwall | 6a1f8db | 2016-08-11 13:49:44 -0700 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | TEST_F(TableMergerTest, FailToOverrideConflictingTypeIdsWithOverlay) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 203 | std::unique_ptr<ResourceTable> base = |
| 204 | test::ResourceTableBuilder() |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 205 | .SetPackageId("", 0x7f) |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 206 | .SetSymbolState("bool/foo", ResourceId(0x7f, 0x01, 0x0001), Visibility::Level::kPublic) |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 207 | .Build(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 208 | std::unique_ptr<ResourceTable> overlay = |
| 209 | test::ResourceTableBuilder() |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 210 | .SetPackageId("", 0x7f) |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 211 | .SetSymbolState("bool/foo", ResourceId(0x7f, 0x02, 0x0001), Visibility::Level::kPublic) |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 212 | .Build(); |
Alexandria Cornwall | 6a1f8db | 2016-08-11 13:49:44 -0700 | [diff] [blame] | 213 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 214 | ResourceTable final_table; |
| 215 | TableMergerOptions options; |
| 216 | options.auto_add_overlay = false; |
| 217 | TableMerger merger(context_.get(), &final_table, options); |
Alexandria Cornwall | 6a1f8db | 2016-08-11 13:49:44 -0700 | [diff] [blame] | 218 | |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 219 | ASSERT_TRUE(merger.Merge({}, base.get(), false /*overlay*/)); |
| 220 | ASSERT_FALSE(merger.Merge({}, overlay.get(), true /*overlay*/)); |
Alexandria Cornwall | 6a1f8db | 2016-08-11 13:49:44 -0700 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | TEST_F(TableMergerTest, FailToOverrideConflictingEntryIdsWithOverlay) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 224 | std::unique_ptr<ResourceTable> base = |
| 225 | test::ResourceTableBuilder() |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 226 | .SetPackageId("", 0x7f) |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 227 | .SetSymbolState("bool/foo", ResourceId(0x7f, 0x01, 0x0001), Visibility::Level::kPublic) |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 228 | .Build(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 229 | std::unique_ptr<ResourceTable> overlay = |
| 230 | test::ResourceTableBuilder() |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 231 | .SetPackageId("", 0x7f) |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 232 | .SetSymbolState("bool/foo", ResourceId(0x7f, 0x01, 0x0002), Visibility::Level::kPublic) |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 233 | .Build(); |
Alexandria Cornwall | 6a1f8db | 2016-08-11 13:49:44 -0700 | [diff] [blame] | 234 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 235 | ResourceTable final_table; |
| 236 | TableMergerOptions options; |
| 237 | options.auto_add_overlay = false; |
| 238 | TableMerger merger(context_.get(), &final_table, options); |
Alexandria Cornwall | 6a1f8db | 2016-08-11 13:49:44 -0700 | [diff] [blame] | 239 | |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 240 | ASSERT_TRUE(merger.Merge({}, base.get(), false /*overlay*/)); |
| 241 | ASSERT_FALSE(merger.Merge({}, overlay.get(), true /*overlay*/)); |
Alexandria Cornwall | 6a1f8db | 2016-08-11 13:49:44 -0700 | [diff] [blame] | 242 | } |
| 243 | |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 244 | TEST_F(TableMergerTest, MergeAddResourceFromOverlay) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 245 | std::unique_ptr<ResourceTable> table_a = |
Adam Lesinski | 4488f1c | 2017-05-26 17:33:38 -0700 | [diff] [blame] | 246 | test::ResourceTableBuilder().SetPackageId("", 0x7f).Build(); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 247 | std::unique_ptr<ResourceTable> table_b = |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 248 | test::ResourceTableBuilder() |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 249 | .SetPackageId("", 0x7f) |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 250 | .SetSymbolState("bool/foo", {}, Visibility::Level::kUndefined, true /*allow new overlay*/) |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 251 | .AddValue("bool/foo", ResourceUtils::TryParseBool("true")) |
| 252 | .Build(); |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 253 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 254 | ResourceTable final_table; |
Adam Lesinski | 4488f1c | 2017-05-26 17:33:38 -0700 | [diff] [blame] | 255 | TableMergerOptions options; |
| 256 | options.auto_add_overlay = false; |
| 257 | TableMerger merger(context_.get(), &final_table, options); |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 258 | |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 259 | ASSERT_TRUE(merger.Merge({}, table_a.get(), false /*overlay*/)); |
| 260 | ASSERT_TRUE(merger.Merge({}, table_b.get(), false /*overlay*/)); |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | TEST_F(TableMergerTest, MergeAddResourceFromOverlayWithAutoAddOverlay) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 264 | std::unique_ptr<ResourceTable> table_a = |
| 265 | test::ResourceTableBuilder().SetPackageId("", 0x7f).Build(); |
| 266 | std::unique_ptr<ResourceTable> table_b = |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 267 | test::ResourceTableBuilder() |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 268 | .SetPackageId("", 0x7f) |
| 269 | .AddValue("bool/foo", ResourceUtils::TryParseBool("true")) |
| 270 | .Build(); |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 271 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 272 | ResourceTable final_table; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 273 | TableMergerOptions options; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 274 | options.auto_add_overlay = true; |
| 275 | TableMerger merger(context_.get(), &final_table, options); |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 276 | |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 277 | ASSERT_TRUE(merger.Merge({}, table_a.get(), false /*overlay*/)); |
| 278 | ASSERT_TRUE(merger.Merge({}, table_b.get(), false /*overlay*/)); |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | TEST_F(TableMergerTest, FailToMergeNewResourceWithoutAutoAddOverlay) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 282 | std::unique_ptr<ResourceTable> table_a = |
| 283 | test::ResourceTableBuilder().SetPackageId("", 0x7f).Build(); |
| 284 | std::unique_ptr<ResourceTable> table_b = |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 285 | test::ResourceTableBuilder() |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 286 | .SetPackageId("", 0x7f) |
| 287 | .AddValue("bool/foo", ResourceUtils::TryParseBool("true")) |
| 288 | .Build(); |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 289 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 290 | ResourceTable final_table; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 291 | TableMergerOptions options; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 292 | options.auto_add_overlay = false; |
| 293 | TableMerger merger(context_.get(), &final_table, options); |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 294 | |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 295 | ASSERT_TRUE(merger.Merge({}, table_a.get(), false /*overlay*/)); |
| 296 | ASSERT_FALSE(merger.Merge({}, table_b.get(), true /*overlay*/)); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 297 | } |
| 298 | |
Adam Lesinski | 5924d8c | 2017-05-30 15:15:58 -0700 | [diff] [blame] | 299 | TEST_F(TableMergerTest, OverlaidStyleablesAndStylesShouldBeMerged) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 300 | std::unique_ptr<ResourceTable> table_a = |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 301 | test::ResourceTableBuilder() |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 302 | .SetPackageId("com.app.a", 0x7f) |
| 303 | .AddValue("com.app.a:styleable/Foo", |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 304 | test::StyleableBuilder() |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 305 | .AddItem("com.app.a:attr/bar") |
| 306 | .AddItem("com.app.a:attr/foo", ResourceId(0x01010000)) |
| 307 | .Build()) |
Adam Lesinski | 5924d8c | 2017-05-30 15:15:58 -0700 | [diff] [blame] | 308 | .AddValue("com.app.a:style/Theme", |
| 309 | test::StyleBuilder() |
| 310 | .SetParent("com.app.a:style/Parent") |
| 311 | .AddItem("com.app.a:attr/bar", util::make_unique<Id>()) |
| 312 | .AddItem("com.app.a:attr/foo", ResourceUtils::MakeBool(false)) |
| 313 | .Build()) |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 314 | .Build(); |
Adam Lesinski | 5c3464c | 2016-08-24 16:03:48 -0700 | [diff] [blame] | 315 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 316 | std::unique_ptr<ResourceTable> table_b = |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 317 | test::ResourceTableBuilder() |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 318 | .SetPackageId("com.app.a", 0x7f) |
Adam Lesinski | 5924d8c | 2017-05-30 15:15:58 -0700 | [diff] [blame] | 319 | .AddValue("com.app.a:styleable/Foo", test::StyleableBuilder() |
| 320 | .AddItem("com.app.a:attr/bat") |
| 321 | .AddItem("com.app.a:attr/foo") |
| 322 | .Build()) |
| 323 | .AddValue("com.app.a:style/Theme", |
| 324 | test::StyleBuilder() |
| 325 | .SetParent("com.app.a:style/OverlayParent") |
| 326 | .AddItem("com.app.a:attr/bat", util::make_unique<Id>()) |
| 327 | .AddItem("com.app.a:attr/foo", ResourceId(0x01010000), |
| 328 | ResourceUtils::MakeBool(true)) |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 329 | .Build()) |
| 330 | .Build(); |
Adam Lesinski | 5c3464c | 2016-08-24 16:03:48 -0700 | [diff] [blame] | 331 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 332 | ResourceTable final_table; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 333 | TableMergerOptions options; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 334 | options.auto_add_overlay = true; |
| 335 | TableMerger merger(context_.get(), &final_table, options); |
Adam Lesinski | 5c3464c | 2016-08-24 16:03:48 -0700 | [diff] [blame] | 336 | |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 337 | ASSERT_TRUE(merger.Merge({}, table_a.get(), false /*overlay*/)); |
| 338 | ASSERT_TRUE(merger.Merge({}, table_b.get(), true /*overlay*/)); |
Adam Lesinski | 5c3464c | 2016-08-24 16:03:48 -0700 | [diff] [blame] | 339 | |
Adam Lesinski | 5924d8c | 2017-05-30 15:15:58 -0700 | [diff] [blame] | 340 | Styleable* styleable = test::GetValue<Styleable>(&final_table, "com.app.a:styleable/Foo"); |
| 341 | ASSERT_THAT(styleable, NotNull()); |
Adam Lesinski | 5c3464c | 2016-08-24 16:03:48 -0700 | [diff] [blame] | 342 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 343 | std::vector<Reference> expected_refs = { |
| 344 | Reference(test::ParseNameOrDie("com.app.a:attr/bar")), |
| 345 | Reference(test::ParseNameOrDie("com.app.a:attr/bat")), |
Adam Lesinski | 5924d8c | 2017-05-30 15:15:58 -0700 | [diff] [blame] | 346 | Reference(test::ParseNameOrDie("com.app.a:attr/foo"), ResourceId(0x01010000)), |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 347 | }; |
Adam Lesinski | 5924d8c | 2017-05-30 15:15:58 -0700 | [diff] [blame] | 348 | EXPECT_THAT(styleable->entries, UnorderedElementsAreArray(expected_refs)); |
Adam Lesinski | 5c3464c | 2016-08-24 16:03:48 -0700 | [diff] [blame] | 349 | |
Adam Lesinski | 5924d8c | 2017-05-30 15:15:58 -0700 | [diff] [blame] | 350 | Style* style = test::GetValue<Style>(&final_table, "com.app.a:style/Theme"); |
| 351 | ASSERT_THAT(style, NotNull()); |
| 352 | |
| 353 | std::vector<Reference> extracted_refs; |
| 354 | for (const auto& entry : style->entries) { |
| 355 | extracted_refs.push_back(entry.key); |
| 356 | } |
| 357 | EXPECT_THAT(extracted_refs, UnorderedElementsAreArray(expected_refs)); |
| 358 | |
| 359 | const auto expected = ResourceUtils::MakeBool(true); |
| 360 | EXPECT_THAT(style->entries, Contains(Field(&Style::Entry::value, Pointee(ValueEq(*expected))))); |
| 361 | EXPECT_THAT(style->parent, |
| 362 | Eq(make_value(Reference(test::ParseNameOrDie("com.app.a:style/OverlayParent"))))); |
Adam Lesinski | 5c3464c | 2016-08-24 16:03:48 -0700 | [diff] [blame] | 363 | } |
| 364 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 365 | } // namespace aapt |