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 | |
| 23 | namespace aapt { |
| 24 | |
| 25 | struct TableMergerTest : public ::testing::Test { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 26 | std::unique_ptr<IAaptContext> context_; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 27 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 28 | void SetUp() override { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 29 | context_ = |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 30 | test::ContextBuilder() |
| 31 | // We are compiling this package. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 32 | .SetCompilationPackage("com.app.a") |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 33 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 34 | // Merge all packages that have this package ID. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 35 | .SetPackageId(0x7f) |
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 | // Mangle all packages that do not have this package name. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 38 | .SetNameManglerPolicy(NameManglerPolicy{"com.app.a", {"com.app.b"}}) |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 39 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 40 | .Build(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 41 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 42 | }; |
| 43 | |
| 44 | TEST_F(TableMergerTest, SimpleMerge) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 45 | std::unique_ptr<ResourceTable> table_a = |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 46 | test::ResourceTableBuilder() |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 47 | .SetPackageId("com.app.a", 0x7f) |
| 48 | .AddReference("com.app.a:id/foo", "com.app.a:id/bar") |
| 49 | .AddReference("com.app.a:id/bar", "com.app.b:id/foo") |
| 50 | .AddValue( |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 51 | "com.app.a:styleable/view", |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 52 | test::StyleableBuilder().AddItem("com.app.b:id/foo").Build()) |
| 53 | .Build(); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 54 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 55 | std::unique_ptr<ResourceTable> table_b = test::ResourceTableBuilder() |
| 56 | .SetPackageId("com.app.b", 0x7f) |
| 57 | .AddSimple("com.app.b:id/foo") |
| 58 | .Build(); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 59 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 60 | ResourceTable final_table; |
| 61 | TableMerger merger(context_.get(), &final_table, TableMergerOptions{}); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 62 | io::FileCollection collection; |
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 | ASSERT_TRUE(merger.Merge({}, table_a.get())); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 65 | ASSERT_TRUE( |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 66 | merger.MergeAndMangle({}, "com.app.b", table_b.get(), &collection)); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 67 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 68 | EXPECT_TRUE(merger.merged_packages().count("com.app.b") != 0); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 69 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 70 | // Entries from com.app.a should not be mangled. |
| 71 | AAPT_EXPECT_TRUE( |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 72 | final_table.FindResource(test::ParseNameOrDie("com.app.a:id/foo"))); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 73 | AAPT_EXPECT_TRUE( |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 74 | final_table.FindResource(test::ParseNameOrDie("com.app.a:id/bar"))); |
| 75 | AAPT_EXPECT_TRUE(final_table.FindResource( |
| 76 | test::ParseNameOrDie("com.app.a:styleable/view"))); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 77 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 78 | // The unmangled name should not be present. |
| 79 | AAPT_EXPECT_FALSE( |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 80 | final_table.FindResource(test::ParseNameOrDie("com.app.b:id/foo"))); |
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 | // Look for the mangled name. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 83 | AAPT_EXPECT_TRUE(final_table.FindResource( |
| 84 | test::ParseNameOrDie("com.app.a:id/com.app.b$foo"))); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 85 | } |
| 86 | |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 87 | TEST_F(TableMergerTest, MergeFile) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 88 | ResourceTable final_table; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 89 | TableMergerOptions options; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 90 | options.auto_add_overlay = false; |
| 91 | TableMerger merger(context_.get(), &final_table, options); |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 92 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 93 | ResourceFile file_desc; |
| 94 | file_desc.config = test::ParseConfigOrDie("hdpi-v4"); |
| 95 | file_desc.name = test::ParseNameOrDie("layout/main"); |
| 96 | file_desc.source = Source("res/layout-hdpi/main.xml"); |
| 97 | test::TestFile test_file("path/to/res/layout-hdpi/main.xml.flat"); |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 98 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 99 | ASSERT_TRUE(merger.MergeFile(file_desc, &test_file)); |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 100 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 101 | FileReference* file = test::GetValueForConfig<FileReference>( |
| 102 | &final_table, "com.app.a:layout/main", test::ParseConfigOrDie("hdpi-v4")); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 103 | ASSERT_NE(nullptr, file); |
| 104 | EXPECT_EQ(std::string("res/layout-hdpi-v4/main.xml"), *file->path); |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | TEST_F(TableMergerTest, MergeFileOverlay) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 108 | ResourceTable final_table; |
| 109 | TableMergerOptions options; |
| 110 | options.auto_add_overlay = false; |
| 111 | TableMerger merger(context_.get(), &final_table, options); |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 112 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 113 | ResourceFile file_desc; |
| 114 | file_desc.name = test::ParseNameOrDie("xml/foo"); |
| 115 | test::TestFile file_a("path/to/fileA.xml.flat"); |
| 116 | test::TestFile file_b("path/to/fileB.xml.flat"); |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 117 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 118 | ASSERT_TRUE(merger.MergeFile(file_desc, &file_a)); |
| 119 | ASSERT_TRUE(merger.MergeFileOverlay(file_desc, &file_b)); |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 120 | } |
| 121 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 122 | TEST_F(TableMergerTest, MergeFileReferences) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 123 | std::unique_ptr<ResourceTable> table_a = |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 124 | test::ResourceTableBuilder() |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 125 | .SetPackageId("com.app.a", 0x7f) |
| 126 | .AddFileReference("com.app.a:xml/file", "res/xml/file.xml") |
| 127 | .Build(); |
| 128 | std::unique_ptr<ResourceTable> table_b = |
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.b", 0x7f) |
| 131 | .AddFileReference("com.app.b:xml/file", "res/xml/file.xml") |
| 132 | .Build(); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 133 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 134 | ResourceTable final_table; |
| 135 | TableMerger merger(context_.get(), &final_table, TableMergerOptions{}); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 136 | io::FileCollection collection; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 137 | collection.InsertFile("res/xml/file.xml"); |
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 | ASSERT_TRUE(merger.Merge({}, table_a.get())); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 140 | ASSERT_TRUE( |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 141 | merger.MergeAndMangle({}, "com.app.b", table_b.get(), &collection)); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 142 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 143 | FileReference* f = |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 144 | test::GetValue<FileReference>(&final_table, "com.app.a:xml/file"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 145 | ASSERT_NE(f, nullptr); |
| 146 | EXPECT_EQ(std::string("res/xml/file.xml"), *f->path); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 147 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 148 | f = test::GetValue<FileReference>(&final_table, |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 149 | "com.app.a:xml/com.app.b$file"); |
| 150 | ASSERT_NE(f, nullptr); |
| 151 | EXPECT_EQ(std::string("res/xml/com.app.b$file.xml"), *f->path); |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | TEST_F(TableMergerTest, OverrideResourceWithOverlay) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 155 | std::unique_ptr<ResourceTable> base = |
| 156 | test::ResourceTableBuilder() |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 157 | .SetPackageId("", 0x00) |
| 158 | .AddValue("bool/foo", ResourceUtils::TryParseBool("true")) |
| 159 | .Build(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 160 | std::unique_ptr<ResourceTable> overlay = |
| 161 | test::ResourceTableBuilder() |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 162 | .SetPackageId("", 0x00) |
| 163 | .AddValue("bool/foo", ResourceUtils::TryParseBool("false")) |
| 164 | .Build(); |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 165 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 166 | ResourceTable final_table; |
| 167 | TableMergerOptions options; |
| 168 | options.auto_add_overlay = false; |
| 169 | TableMerger merger(context_.get(), &final_table, options); |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 170 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 171 | ASSERT_TRUE(merger.Merge({}, base.get())); |
| 172 | ASSERT_TRUE(merger.MergeOverlay({}, overlay.get())); |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 173 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 174 | BinaryPrimitive* foo = |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 175 | test::GetValue<BinaryPrimitive>(&final_table, "com.app.a:bool/foo"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 176 | ASSERT_NE(nullptr, foo); |
| 177 | EXPECT_EQ(0x0u, foo->value.data); |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 178 | } |
| 179 | |
Alexandria Cornwall | 6a1f8db | 2016-08-11 13:49:44 -0700 | [diff] [blame] | 180 | TEST_F(TableMergerTest, OverrideSameResourceIdsWithOverlay) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 181 | std::unique_ptr<ResourceTable> base = |
| 182 | test::ResourceTableBuilder() |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 183 | .SetPackageId("", 0x7f) |
| 184 | .SetSymbolState("bool/foo", ResourceId(0x7f, 0x01, 0x0001), |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 185 | SymbolState::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) |
| 190 | .SetSymbolState("bool/foo", ResourceId(0x7f, 0x01, 0x0001), |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 191 | SymbolState::kPublic) |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 192 | .Build(); |
Alexandria Cornwall | 6a1f8db | 2016-08-11 13:49:44 -0700 | [diff] [blame] | 193 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 194 | ResourceTable final_table; |
| 195 | TableMergerOptions options; |
| 196 | options.auto_add_overlay = false; |
| 197 | TableMerger merger(context_.get(), &final_table, options); |
Alexandria Cornwall | 6a1f8db | 2016-08-11 13:49:44 -0700 | [diff] [blame] | 198 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 199 | ASSERT_TRUE(merger.Merge({}, base.get())); |
| 200 | ASSERT_TRUE(merger.MergeOverlay({}, overlay.get())); |
Alexandria Cornwall | 6a1f8db | 2016-08-11 13:49:44 -0700 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | TEST_F(TableMergerTest, FailToOverrideConflictingTypeIdsWithOverlay) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 204 | std::unique_ptr<ResourceTable> base = |
| 205 | test::ResourceTableBuilder() |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 206 | .SetPackageId("", 0x7f) |
| 207 | .SetSymbolState("bool/foo", ResourceId(0x7f, 0x01, 0x0001), |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 208 | SymbolState::kPublic) |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 209 | .Build(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 210 | std::unique_ptr<ResourceTable> overlay = |
| 211 | test::ResourceTableBuilder() |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 212 | .SetPackageId("", 0x7f) |
| 213 | .SetSymbolState("bool/foo", ResourceId(0x7f, 0x02, 0x0001), |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 214 | SymbolState::kPublic) |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 215 | .Build(); |
Alexandria Cornwall | 6a1f8db | 2016-08-11 13:49:44 -0700 | [diff] [blame] | 216 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 217 | ResourceTable final_table; |
| 218 | TableMergerOptions options; |
| 219 | options.auto_add_overlay = false; |
| 220 | TableMerger merger(context_.get(), &final_table, options); |
Alexandria Cornwall | 6a1f8db | 2016-08-11 13:49:44 -0700 | [diff] [blame] | 221 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 222 | ASSERT_TRUE(merger.Merge({}, base.get())); |
| 223 | ASSERT_FALSE(merger.MergeOverlay({}, overlay.get())); |
Alexandria Cornwall | 6a1f8db | 2016-08-11 13:49:44 -0700 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | TEST_F(TableMergerTest, FailToOverrideConflictingEntryIdsWithOverlay) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 227 | std::unique_ptr<ResourceTable> base = |
| 228 | test::ResourceTableBuilder() |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 229 | .SetPackageId("", 0x7f) |
| 230 | .SetSymbolState("bool/foo", ResourceId(0x7f, 0x01, 0x0001), |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 231 | SymbolState::kPublic) |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 232 | .Build(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 233 | std::unique_ptr<ResourceTable> overlay = |
| 234 | test::ResourceTableBuilder() |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 235 | .SetPackageId("", 0x7f) |
| 236 | .SetSymbolState("bool/foo", ResourceId(0x7f, 0x01, 0x0002), |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 237 | SymbolState::kPublic) |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 238 | .Build(); |
Alexandria Cornwall | 6a1f8db | 2016-08-11 13:49:44 -0700 | [diff] [blame] | 239 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 240 | ResourceTable final_table; |
| 241 | TableMergerOptions options; |
| 242 | options.auto_add_overlay = false; |
| 243 | TableMerger merger(context_.get(), &final_table, options); |
Alexandria Cornwall | 6a1f8db | 2016-08-11 13:49:44 -0700 | [diff] [blame] | 244 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 245 | ASSERT_TRUE(merger.Merge({}, base.get())); |
| 246 | ASSERT_FALSE(merger.MergeOverlay({}, overlay.get())); |
Alexandria Cornwall | 6a1f8db | 2016-08-11 13:49:44 -0700 | [diff] [blame] | 247 | } |
| 248 | |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 249 | TEST_F(TableMergerTest, MergeAddResourceFromOverlay) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 250 | std::unique_ptr<ResourceTable> table_a = |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 251 | test::ResourceTableBuilder() |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 252 | .SetPackageId("", 0x7f) |
| 253 | .SetSymbolState("bool/foo", {}, SymbolState::kUndefined) |
| 254 | .Build(); |
| 255 | std::unique_ptr<ResourceTable> table_b = |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 256 | test::ResourceTableBuilder() |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 257 | .SetPackageId("", 0x7f) |
| 258 | .AddValue("bool/foo", ResourceUtils::TryParseBool("true")) |
| 259 | .Build(); |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 260 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 261 | ResourceTable final_table; |
| 262 | TableMerger merger(context_.get(), &final_table, TableMergerOptions{}); |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 263 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 264 | ASSERT_TRUE(merger.Merge({}, table_a.get())); |
| 265 | ASSERT_TRUE(merger.MergeOverlay({}, table_b.get())); |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | TEST_F(TableMergerTest, MergeAddResourceFromOverlayWithAutoAddOverlay) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 269 | std::unique_ptr<ResourceTable> table_a = |
| 270 | test::ResourceTableBuilder().SetPackageId("", 0x7f).Build(); |
| 271 | std::unique_ptr<ResourceTable> table_b = |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 272 | test::ResourceTableBuilder() |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 273 | .SetPackageId("", 0x7f) |
| 274 | .AddValue("bool/foo", ResourceUtils::TryParseBool("true")) |
| 275 | .Build(); |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 276 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 277 | ResourceTable final_table; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 278 | TableMergerOptions options; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 279 | options.auto_add_overlay = true; |
| 280 | TableMerger merger(context_.get(), &final_table, options); |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 281 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 282 | ASSERT_TRUE(merger.Merge({}, table_a.get())); |
| 283 | ASSERT_TRUE(merger.MergeOverlay({}, table_b.get())); |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | TEST_F(TableMergerTest, FailToMergeNewResourceWithoutAutoAddOverlay) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 287 | std::unique_ptr<ResourceTable> table_a = |
| 288 | test::ResourceTableBuilder().SetPackageId("", 0x7f).Build(); |
| 289 | std::unique_ptr<ResourceTable> table_b = |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 290 | test::ResourceTableBuilder() |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 291 | .SetPackageId("", 0x7f) |
| 292 | .AddValue("bool/foo", ResourceUtils::TryParseBool("true")) |
| 293 | .Build(); |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 294 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 295 | ResourceTable final_table; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 296 | TableMergerOptions options; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 297 | options.auto_add_overlay = false; |
| 298 | TableMerger merger(context_.get(), &final_table, options); |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 299 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 300 | ASSERT_TRUE(merger.Merge({}, table_a.get())); |
| 301 | ASSERT_FALSE(merger.MergeOverlay({}, table_b.get())); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 302 | } |
| 303 | |
Adam Lesinski | 5c3464c | 2016-08-24 16:03:48 -0700 | [diff] [blame] | 304 | TEST_F(TableMergerTest, OverlaidStyleablesShouldBeMerged) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 305 | std::unique_ptr<ResourceTable> table_a = |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 306 | test::ResourceTableBuilder() |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 307 | .SetPackageId("com.app.a", 0x7f) |
| 308 | .AddValue("com.app.a:styleable/Foo", |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 309 | test::StyleableBuilder() |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 310 | .AddItem("com.app.a:attr/bar") |
| 311 | .AddItem("com.app.a:attr/foo", ResourceId(0x01010000)) |
| 312 | .Build()) |
| 313 | .Build(); |
Adam Lesinski | 5c3464c | 2016-08-24 16:03:48 -0700 | [diff] [blame] | 314 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 315 | std::unique_ptr<ResourceTable> table_b = |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 316 | test::ResourceTableBuilder() |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 317 | .SetPackageId("com.app.a", 0x7f) |
| 318 | .AddValue("com.app.a:styleable/Foo", |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 319 | test::StyleableBuilder() |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 320 | .AddItem("com.app.a:attr/bat") |
| 321 | .AddItem("com.app.a:attr/foo") |
| 322 | .Build()) |
| 323 | .Build(); |
Adam Lesinski | 5c3464c | 2016-08-24 16:03:48 -0700 | [diff] [blame] | 324 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 325 | ResourceTable final_table; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 326 | TableMergerOptions options; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 327 | options.auto_add_overlay = true; |
| 328 | TableMerger merger(context_.get(), &final_table, options); |
Adam Lesinski | 5c3464c | 2016-08-24 16:03:48 -0700 | [diff] [blame] | 329 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 330 | ASSERT_TRUE(merger.Merge({}, table_a.get())); |
| 331 | ASSERT_TRUE(merger.MergeOverlay({}, table_b.get())); |
Adam Lesinski | 5c3464c | 2016-08-24 16:03:48 -0700 | [diff] [blame] | 332 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 333 | Styleable* styleable = |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 334 | test::GetValue<Styleable>(&final_table, "com.app.a:styleable/Foo"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 335 | ASSERT_NE(nullptr, styleable); |
Adam Lesinski | 5c3464c | 2016-08-24 16:03:48 -0700 | [diff] [blame] | 336 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 337 | std::vector<Reference> expected_refs = { |
| 338 | Reference(test::ParseNameOrDie("com.app.a:attr/bar")), |
| 339 | Reference(test::ParseNameOrDie("com.app.a:attr/bat")), |
| 340 | Reference(test::ParseNameOrDie("com.app.a:attr/foo"), |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 341 | ResourceId(0x01010000)), |
| 342 | }; |
Adam Lesinski | 5c3464c | 2016-08-24 16:03:48 -0700 | [diff] [blame] | 343 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 344 | EXPECT_EQ(expected_refs, styleable->entries); |
Adam Lesinski | 5c3464c | 2016-08-24 16:03:48 -0700 | [diff] [blame] | 345 | } |
| 346 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 347 | } // namespace aapt |