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) |
| 185 | .SetSymbolState("bool/foo", ResourceId(0x7f, 0x01, 0x0001), |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 186 | SymbolState::kPublic) |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 187 | .Build(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 188 | std::unique_ptr<ResourceTable> overlay = |
| 189 | test::ResourceTableBuilder() |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 190 | .SetPackageId("", 0x7f) |
| 191 | .SetSymbolState("bool/foo", ResourceId(0x7f, 0x01, 0x0001), |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 192 | SymbolState::kPublic) |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 193 | .Build(); |
Alexandria Cornwall | 6a1f8db | 2016-08-11 13:49:44 -0700 | [diff] [blame] | 194 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 195 | ResourceTable final_table; |
| 196 | TableMergerOptions options; |
| 197 | options.auto_add_overlay = false; |
| 198 | TableMerger merger(context_.get(), &final_table, options); |
Alexandria Cornwall | 6a1f8db | 2016-08-11 13:49:44 -0700 | [diff] [blame] | 199 | |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 200 | ASSERT_TRUE(merger.Merge({}, base.get(), false /*overlay*/)); |
| 201 | ASSERT_TRUE(merger.Merge({}, overlay.get(), true /*overlay*/)); |
Alexandria Cornwall | 6a1f8db | 2016-08-11 13:49:44 -0700 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | TEST_F(TableMergerTest, FailToOverrideConflictingTypeIdsWithOverlay) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 205 | std::unique_ptr<ResourceTable> base = |
| 206 | test::ResourceTableBuilder() |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 207 | .SetPackageId("", 0x7f) |
| 208 | .SetSymbolState("bool/foo", ResourceId(0x7f, 0x01, 0x0001), |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 209 | SymbolState::kPublic) |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 210 | .Build(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 211 | std::unique_ptr<ResourceTable> overlay = |
| 212 | test::ResourceTableBuilder() |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 213 | .SetPackageId("", 0x7f) |
| 214 | .SetSymbolState("bool/foo", ResourceId(0x7f, 0x02, 0x0001), |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 215 | SymbolState::kPublic) |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 216 | .Build(); |
Alexandria Cornwall | 6a1f8db | 2016-08-11 13:49:44 -0700 | [diff] [blame] | 217 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 218 | ResourceTable final_table; |
| 219 | TableMergerOptions options; |
| 220 | options.auto_add_overlay = false; |
| 221 | TableMerger merger(context_.get(), &final_table, options); |
Alexandria Cornwall | 6a1f8db | 2016-08-11 13:49:44 -0700 | [diff] [blame] | 222 | |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 223 | ASSERT_TRUE(merger.Merge({}, base.get(), false /*overlay*/)); |
| 224 | ASSERT_FALSE(merger.Merge({}, overlay.get(), true /*overlay*/)); |
Alexandria Cornwall | 6a1f8db | 2016-08-11 13:49:44 -0700 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | TEST_F(TableMergerTest, FailToOverrideConflictingEntryIdsWithOverlay) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 228 | std::unique_ptr<ResourceTable> base = |
| 229 | test::ResourceTableBuilder() |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 230 | .SetPackageId("", 0x7f) |
| 231 | .SetSymbolState("bool/foo", ResourceId(0x7f, 0x01, 0x0001), |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 232 | SymbolState::kPublic) |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 233 | .Build(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 234 | std::unique_ptr<ResourceTable> overlay = |
| 235 | test::ResourceTableBuilder() |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 236 | .SetPackageId("", 0x7f) |
| 237 | .SetSymbolState("bool/foo", ResourceId(0x7f, 0x01, 0x0002), |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 238 | SymbolState::kPublic) |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 239 | .Build(); |
Alexandria Cornwall | 6a1f8db | 2016-08-11 13:49:44 -0700 | [diff] [blame] | 240 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 241 | ResourceTable final_table; |
| 242 | TableMergerOptions options; |
| 243 | options.auto_add_overlay = false; |
| 244 | TableMerger merger(context_.get(), &final_table, options); |
Alexandria Cornwall | 6a1f8db | 2016-08-11 13:49:44 -0700 | [diff] [blame] | 245 | |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 246 | ASSERT_TRUE(merger.Merge({}, base.get(), false /*overlay*/)); |
| 247 | ASSERT_FALSE(merger.Merge({}, overlay.get(), true /*overlay*/)); |
Alexandria Cornwall | 6a1f8db | 2016-08-11 13:49:44 -0700 | [diff] [blame] | 248 | } |
| 249 | |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 250 | TEST_F(TableMergerTest, MergeAddResourceFromOverlay) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 251 | std::unique_ptr<ResourceTable> table_a = |
Adam Lesinski | 4488f1c | 2017-05-26 17:33:38 -0700 | [diff] [blame] | 252 | test::ResourceTableBuilder().SetPackageId("", 0x7f).Build(); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 253 | std::unique_ptr<ResourceTable> table_b = |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 254 | test::ResourceTableBuilder() |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 255 | .SetPackageId("", 0x7f) |
Adam Lesinski | 4488f1c | 2017-05-26 17:33:38 -0700 | [diff] [blame] | 256 | .SetSymbolState("bool/foo", {}, SymbolState::kUndefined, true /*allow new overlay*/) |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 257 | .AddValue("bool/foo", ResourceUtils::TryParseBool("true")) |
| 258 | .Build(); |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 259 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 260 | ResourceTable final_table; |
Adam Lesinski | 4488f1c | 2017-05-26 17:33:38 -0700 | [diff] [blame] | 261 | TableMergerOptions options; |
| 262 | options.auto_add_overlay = false; |
| 263 | TableMerger merger(context_.get(), &final_table, options); |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 264 | |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 265 | ASSERT_TRUE(merger.Merge({}, table_a.get(), false /*overlay*/)); |
| 266 | ASSERT_TRUE(merger.Merge({}, table_b.get(), false /*overlay*/)); |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | TEST_F(TableMergerTest, MergeAddResourceFromOverlayWithAutoAddOverlay) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 270 | std::unique_ptr<ResourceTable> table_a = |
| 271 | test::ResourceTableBuilder().SetPackageId("", 0x7f).Build(); |
| 272 | std::unique_ptr<ResourceTable> table_b = |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 273 | test::ResourceTableBuilder() |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 274 | .SetPackageId("", 0x7f) |
| 275 | .AddValue("bool/foo", ResourceUtils::TryParseBool("true")) |
| 276 | .Build(); |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 277 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 278 | ResourceTable final_table; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 279 | TableMergerOptions options; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 280 | options.auto_add_overlay = true; |
| 281 | TableMerger merger(context_.get(), &final_table, options); |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 282 | |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 283 | ASSERT_TRUE(merger.Merge({}, table_a.get(), false /*overlay*/)); |
| 284 | ASSERT_TRUE(merger.Merge({}, table_b.get(), false /*overlay*/)); |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | TEST_F(TableMergerTest, FailToMergeNewResourceWithoutAutoAddOverlay) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 288 | std::unique_ptr<ResourceTable> table_a = |
| 289 | test::ResourceTableBuilder().SetPackageId("", 0x7f).Build(); |
| 290 | std::unique_ptr<ResourceTable> table_b = |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 291 | test::ResourceTableBuilder() |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 292 | .SetPackageId("", 0x7f) |
| 293 | .AddValue("bool/foo", ResourceUtils::TryParseBool("true")) |
| 294 | .Build(); |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 295 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 296 | ResourceTable final_table; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 297 | TableMergerOptions options; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 298 | options.auto_add_overlay = false; |
| 299 | TableMerger merger(context_.get(), &final_table, options); |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 300 | |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 301 | ASSERT_TRUE(merger.Merge({}, table_a.get(), false /*overlay*/)); |
| 302 | ASSERT_FALSE(merger.Merge({}, table_b.get(), true /*overlay*/)); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 303 | } |
| 304 | |
Adam Lesinski | 5924d8c | 2017-05-30 15:15:58 -0700 | [diff] [blame] | 305 | TEST_F(TableMergerTest, OverlaidStyleablesAndStylesShouldBeMerged) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 306 | std::unique_ptr<ResourceTable> table_a = |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 307 | test::ResourceTableBuilder() |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 308 | .SetPackageId("com.app.a", 0x7f) |
| 309 | .AddValue("com.app.a:styleable/Foo", |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 310 | test::StyleableBuilder() |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 311 | .AddItem("com.app.a:attr/bar") |
| 312 | .AddItem("com.app.a:attr/foo", ResourceId(0x01010000)) |
| 313 | .Build()) |
Adam Lesinski | 5924d8c | 2017-05-30 15:15:58 -0700 | [diff] [blame] | 314 | .AddValue("com.app.a:style/Theme", |
| 315 | test::StyleBuilder() |
| 316 | .SetParent("com.app.a:style/Parent") |
| 317 | .AddItem("com.app.a:attr/bar", util::make_unique<Id>()) |
| 318 | .AddItem("com.app.a:attr/foo", ResourceUtils::MakeBool(false)) |
| 319 | .Build()) |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 320 | .Build(); |
Adam Lesinski | 5c3464c | 2016-08-24 16:03:48 -0700 | [diff] [blame] | 321 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 322 | std::unique_ptr<ResourceTable> table_b = |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 323 | test::ResourceTableBuilder() |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 324 | .SetPackageId("com.app.a", 0x7f) |
Adam Lesinski | 5924d8c | 2017-05-30 15:15:58 -0700 | [diff] [blame] | 325 | .AddValue("com.app.a:styleable/Foo", test::StyleableBuilder() |
| 326 | .AddItem("com.app.a:attr/bat") |
| 327 | .AddItem("com.app.a:attr/foo") |
| 328 | .Build()) |
| 329 | .AddValue("com.app.a:style/Theme", |
| 330 | test::StyleBuilder() |
| 331 | .SetParent("com.app.a:style/OverlayParent") |
| 332 | .AddItem("com.app.a:attr/bat", util::make_unique<Id>()) |
| 333 | .AddItem("com.app.a:attr/foo", ResourceId(0x01010000), |
| 334 | ResourceUtils::MakeBool(true)) |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 335 | .Build()) |
| 336 | .Build(); |
Adam Lesinski | 5c3464c | 2016-08-24 16:03:48 -0700 | [diff] [blame] | 337 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 338 | ResourceTable final_table; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 339 | TableMergerOptions options; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 340 | options.auto_add_overlay = true; |
| 341 | TableMerger merger(context_.get(), &final_table, options); |
Adam Lesinski | 5c3464c | 2016-08-24 16:03:48 -0700 | [diff] [blame] | 342 | |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 343 | ASSERT_TRUE(merger.Merge({}, table_a.get(), false /*overlay*/)); |
| 344 | ASSERT_TRUE(merger.Merge({}, table_b.get(), true /*overlay*/)); |
Adam Lesinski | 5c3464c | 2016-08-24 16:03:48 -0700 | [diff] [blame] | 345 | |
Adam Lesinski | 5924d8c | 2017-05-30 15:15:58 -0700 | [diff] [blame] | 346 | Styleable* styleable = test::GetValue<Styleable>(&final_table, "com.app.a:styleable/Foo"); |
| 347 | ASSERT_THAT(styleable, NotNull()); |
Adam Lesinski | 5c3464c | 2016-08-24 16:03:48 -0700 | [diff] [blame] | 348 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 349 | std::vector<Reference> expected_refs = { |
| 350 | Reference(test::ParseNameOrDie("com.app.a:attr/bar")), |
| 351 | Reference(test::ParseNameOrDie("com.app.a:attr/bat")), |
Adam Lesinski | 5924d8c | 2017-05-30 15:15:58 -0700 | [diff] [blame] | 352 | Reference(test::ParseNameOrDie("com.app.a:attr/foo"), ResourceId(0x01010000)), |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 353 | }; |
Adam Lesinski | 5924d8c | 2017-05-30 15:15:58 -0700 | [diff] [blame] | 354 | EXPECT_THAT(styleable->entries, UnorderedElementsAreArray(expected_refs)); |
Adam Lesinski | 5c3464c | 2016-08-24 16:03:48 -0700 | [diff] [blame] | 355 | |
Adam Lesinski | 5924d8c | 2017-05-30 15:15:58 -0700 | [diff] [blame] | 356 | Style* style = test::GetValue<Style>(&final_table, "com.app.a:style/Theme"); |
| 357 | ASSERT_THAT(style, NotNull()); |
| 358 | |
| 359 | std::vector<Reference> extracted_refs; |
| 360 | for (const auto& entry : style->entries) { |
| 361 | extracted_refs.push_back(entry.key); |
| 362 | } |
| 363 | EXPECT_THAT(extracted_refs, UnorderedElementsAreArray(expected_refs)); |
| 364 | |
| 365 | const auto expected = ResourceUtils::MakeBool(true); |
| 366 | EXPECT_THAT(style->entries, Contains(Field(&Style::Entry::value, Pointee(ValueEq(*expected))))); |
| 367 | EXPECT_THAT(style->parent, |
| 368 | Eq(make_value(Reference(test::ParseNameOrDie("com.app.a:style/OverlayParent"))))); |
Adam Lesinski | 5c3464c | 2016-08-24 16:03:48 -0700 | [diff] [blame] | 369 | } |
| 370 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 371 | } // namespace aapt |