blob: ad3674e1677460e75d4d1d665b4b7f216fcb8a65 [file] [log] [blame]
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001/*
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 Lesinskicacb28f2016-10-19 12:18:14 -070017#include "link/TableMerger.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070018
Adam Lesinski6a008172016-02-02 17:02:58 -080019#include "filter/ConfigFilter.h"
Adam Lesinskia6fe3452015-12-09 15:20:52 -080020#include "io/FileSystem.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070021#include "test/Test.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070022
Adam Lesinski5924d8c2017-05-30 15:15:58 -070023using ::aapt::test::ValueEq;
24using ::testing::Contains;
Adam Lesinski5924d8c2017-05-30 15:15:58 -070025using ::testing::Eq;
Adam Lesinski8780eb62017-10-31 17:44:39 -070026using ::testing::Field;
27using ::testing::NotNull;
28using ::testing::Pointee;
29using ::testing::StrEq;
30using ::testing::UnorderedElementsAreArray;
Adam Lesinski5924d8c2017-05-30 15:15:58 -070031
Adam Lesinski1ab598f2015-08-14 14:26:04 -070032namespace aapt {
33
34struct TableMergerTest : public ::testing::Test {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070035 std::unique_ptr<IAaptContext> context_;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070036
Adam Lesinskicacb28f2016-10-19 12:18:14 -070037 void SetUp() override {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070038 context_ =
Adam Lesinskicacb28f2016-10-19 12:18:14 -070039 test::ContextBuilder()
40 // We are compiling this package.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070041 .SetCompilationPackage("com.app.a")
Adam Lesinski1ab598f2015-08-14 14:26:04 -070042
Adam Lesinskicacb28f2016-10-19 12:18:14 -070043 // Merge all packages that have this package ID.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070044 .SetPackageId(0x7f)
Adam Lesinski1ab598f2015-08-14 14:26:04 -070045
Adam Lesinskicacb28f2016-10-19 12:18:14 -070046 // Mangle all packages that do not have this package name.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070047 .SetNameManglerPolicy(NameManglerPolicy{"com.app.a", {"com.app.b"}})
Adam Lesinski1ab598f2015-08-14 14:26:04 -070048
Adam Lesinskice5e56e2016-10-21 17:56:45 -070049 .Build();
Adam Lesinskicacb28f2016-10-19 12:18:14 -070050 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -070051};
52
53TEST_F(TableMergerTest, SimpleMerge) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070054 std::unique_ptr<ResourceTable> table_a =
Adam Lesinskicacb28f2016-10-19 12:18:14 -070055 test::ResourceTableBuilder()
Adam Lesinskice5e56e2016-10-21 17:56:45 -070056 .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 Lesinskicacb28f2016-10-19 12:18:14 -070060 "com.app.a:styleable/view",
Adam Lesinskice5e56e2016-10-21 17:56:45 -070061 test::StyleableBuilder().AddItem("com.app.b:id/foo").Build())
62 .Build();
Adam Lesinski1ab598f2015-08-14 14:26:04 -070063
Adam Lesinskice5e56e2016-10-21 17:56:45 -070064 std::unique_ptr<ResourceTable> table_b = test::ResourceTableBuilder()
65 .SetPackageId("com.app.b", 0x7f)
66 .AddSimple("com.app.b:id/foo")
67 .Build();
Adam Lesinski1ab598f2015-08-14 14:26:04 -070068
Adam Lesinskice5e56e2016-10-21 17:56:45 -070069 ResourceTable final_table;
70 TableMerger merger(context_.get(), &final_table, TableMergerOptions{});
Adam Lesinski1ab598f2015-08-14 14:26:04 -070071
Adam Lesinski00451162017-10-03 07:44:08 -070072 ASSERT_TRUE(merger.Merge({}, table_a.get(), false /*overlay*/));
Adam Lesinski8780eb62017-10-31 17:44:39 -070073 ASSERT_TRUE(merger.MergeAndMangle({}, "com.app.b", table_b.get()));
Adam Lesinski1ab598f2015-08-14 14:26:04 -070074
Adam Lesinskice5e56e2016-10-21 17:56:45 -070075 EXPECT_TRUE(merger.merged_packages().count("com.app.b") != 0);
Adam Lesinski1ab598f2015-08-14 14:26:04 -070076
Adam Lesinskicacb28f2016-10-19 12:18:14 -070077 // Entries from com.app.a should not be mangled.
Adam Lesinski5924d8c2017-05-30 15:15:58 -070078 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 Lesinski1ab598f2015-08-14 14:26:04 -070081
Adam Lesinskicacb28f2016-10-19 12:18:14 -070082 // The unmangled name should not be present.
Adam Lesinski5924d8c2017-05-30 15:15:58 -070083 EXPECT_FALSE(final_table.FindResource(test::ParseNameOrDie("com.app.b:id/foo")));
Adam Lesinski1ab598f2015-08-14 14:26:04 -070084
Adam Lesinskicacb28f2016-10-19 12:18:14 -070085 // Look for the mangled name.
Adam Lesinski5924d8c2017-05-30 15:15:58 -070086 EXPECT_TRUE(final_table.FindResource(test::ParseNameOrDie("com.app.a:id/com.app.b$foo")));
Adam Lesinski1ab598f2015-08-14 14:26:04 -070087}
88
Adam Lesinskia6fe3452015-12-09 15:20:52 -080089TEST_F(TableMergerTest, MergeFile) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070090 ResourceTable final_table;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070091 TableMergerOptions options;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070092 options.auto_add_overlay = false;
93 TableMerger merger(context_.get(), &final_table, options);
Adam Lesinskia6fe3452015-12-09 15:20:52 -080094
Adam Lesinskice5e56e2016-10-21 17:56:45 -070095 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 Lesinskia6fe3452015-12-09 15:20:52 -0800100
Adam Lesinski00451162017-10-03 07:44:08 -0700101 ASSERT_TRUE(merger.MergeFile(file_desc, false /*overlay*/, &test_file));
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800102
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700103 FileReference* file = test::GetValueForConfig<FileReference>(
104 &final_table, "com.app.a:layout/main", test::ParseConfigOrDie("hdpi-v4"));
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700105 ASSERT_THAT(file, NotNull());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700106 EXPECT_EQ(std::string("res/layout-hdpi-v4/main.xml"), *file->path);
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800107}
108
109TEST_F(TableMergerTest, MergeFileOverlay) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700110 ResourceTable final_table;
111 TableMergerOptions options;
112 options.auto_add_overlay = false;
113 TableMerger merger(context_.get(), &final_table, options);
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800114
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700115 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 Lesinskia6fe3452015-12-09 15:20:52 -0800119
Adam Lesinski00451162017-10-03 07:44:08 -0700120 ASSERT_TRUE(merger.MergeFile(file_desc, false /*overlay*/, &file_a));
121 ASSERT_TRUE(merger.MergeFile(file_desc, true /*overlay*/, &file_b));
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800122}
123
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700124TEST_F(TableMergerTest, MergeFileReferences) {
Adam Lesinski8780eb62017-10-31 17:44:39 -0700125 test::TestFile file_a("res/xml/file.xml");
126 test::TestFile file_b("res/xml/file.xml");
127
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700128 std::unique_ptr<ResourceTable> table_a =
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700129 test::ResourceTableBuilder()
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700130 .SetPackageId("com.app.a", 0x7f)
Adam Lesinski8780eb62017-10-31 17:44:39 -0700131 .AddFileReference("com.app.a:xml/file", "res/xml/file.xml", &file_a)
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700132 .Build();
133 std::unique_ptr<ResourceTable> table_b =
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700134 test::ResourceTableBuilder()
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700135 .SetPackageId("com.app.b", 0x7f)
Adam Lesinski8780eb62017-10-31 17:44:39 -0700136 .AddFileReference("com.app.b:xml/file", "res/xml/file.xml", &file_b)
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700137 .Build();
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700138
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700139 ResourceTable final_table;
140 TableMerger merger(context_.get(), &final_table, TableMergerOptions{});
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700141
Adam Lesinski00451162017-10-03 07:44:08 -0700142 ASSERT_TRUE(merger.Merge({}, table_a.get(), false /*overlay*/));
Adam Lesinski8780eb62017-10-31 17:44:39 -0700143 ASSERT_TRUE(merger.MergeAndMangle({}, "com.app.b", table_b.get()));
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700144
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700145 FileReference* f = test::GetValue<FileReference>(&final_table, "com.app.a:xml/file");
146 ASSERT_THAT(f, NotNull());
Adam Lesinski8780eb62017-10-31 17:44:39 -0700147 EXPECT_THAT(*f->path, StrEq("res/xml/file.xml"));
148 EXPECT_THAT(f->file, Eq(&file_a));
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700149
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700150 f = test::GetValue<FileReference>(&final_table, "com.app.a:xml/com.app.b$file");
151 ASSERT_THAT(f, NotNull());
Adam Lesinski8780eb62017-10-31 17:44:39 -0700152 EXPECT_THAT(*f->path, StrEq("res/xml/com.app.b$file.xml"));
153 EXPECT_THAT(f->file, Eq(&file_b));
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800154}
155
156TEST_F(TableMergerTest, OverrideResourceWithOverlay) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700157 std::unique_ptr<ResourceTable> base =
158 test::ResourceTableBuilder()
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700159 .SetPackageId("", 0x00)
160 .AddValue("bool/foo", ResourceUtils::TryParseBool("true"))
161 .Build();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700162 std::unique_ptr<ResourceTable> overlay =
163 test::ResourceTableBuilder()
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700164 .SetPackageId("", 0x00)
165 .AddValue("bool/foo", ResourceUtils::TryParseBool("false"))
166 .Build();
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800167
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700168 ResourceTable final_table;
169 TableMergerOptions options;
170 options.auto_add_overlay = false;
171 TableMerger merger(context_.get(), &final_table, options);
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800172
Adam Lesinski00451162017-10-03 07:44:08 -0700173 ASSERT_TRUE(merger.Merge({}, base.get(), false /*overlay*/));
174 ASSERT_TRUE(merger.Merge({}, overlay.get(), true /*overlay*/));
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800175
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700176 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 Lesinskia6fe3452015-12-09 15:20:52 -0800179}
180
Ryan Mitchella9e31602018-06-28 16:41:38 -0700181TEST_F(TableMergerTest, DoNotOverrideResourceComment) {
182 std::unique_ptr<Value> foo_original = ResourceUtils::TryParseBool("true");
183 foo_original->SetComment(android::StringPiece("Original foo comment"));
184 std::unique_ptr<Value> bar_original = ResourceUtils::TryParseBool("true");
185
186 std::unique_ptr<Value> foo_overlay = ResourceUtils::TryParseBool("false");
187 foo_overlay->SetComment(android::StringPiece("Overlay foo comment"));
188 std::unique_ptr<Value> bar_overlay = ResourceUtils::TryParseBool("false");
189 bar_overlay->SetComment(android::StringPiece("Overlay bar comment"));
190 std::unique_ptr<Value> baz_overlay = ResourceUtils::TryParseBool("false");
191 baz_overlay->SetComment(android::StringPiece("Overlay baz comment"));
192
193 std::unique_ptr<ResourceTable> base =
194 test::ResourceTableBuilder()
195 .SetPackageId("", 0x00)
196 .AddValue("bool/foo", std::move(foo_original))
197 .AddValue("bool/bar", std::move(bar_original))
198 .Build();
199
200 std::unique_ptr<ResourceTable> overlay =
201 test::ResourceTableBuilder()
202 .SetPackageId("", 0x00)
203 .AddValue("bool/foo", std::move(foo_overlay))
204 .AddValue("bool/bar", std::move(bar_overlay))
205 .AddValue("bool/baz", std::move(baz_overlay))
206 .Build();
207
208 ResourceTable final_table;
209 TableMergerOptions options;
210 options.auto_add_overlay = true;
211 TableMerger merger(context_.get(), &final_table, options);
212
213 ASSERT_TRUE(merger.Merge({}, base.get(), false /*overlay*/));
214 ASSERT_TRUE(merger.Merge({}, overlay.get(), true /*overlay*/));
215
216 BinaryPrimitive* foo = test::GetValue<BinaryPrimitive>(&final_table, "com.app.a:bool/foo");
217 EXPECT_THAT(foo, Pointee(Property(&BinaryPrimitive::GetComment, StrEq("Original foo comment"))));
218 BinaryPrimitive* bar = test::GetValue<BinaryPrimitive>(&final_table, "com.app.a:bool/bar");
219 EXPECT_THAT(bar, Pointee(Property(&BinaryPrimitive::GetComment, StrEq(""))));
220 BinaryPrimitive* baz = test::GetValue<BinaryPrimitive>(&final_table, "com.app.a:bool/baz");
221 EXPECT_THAT(baz, Pointee(Property(&BinaryPrimitive::GetComment, StrEq("Overlay baz comment"))));
222}
223
Alexandria Cornwall6a1f8db2016-08-11 13:49:44 -0700224TEST_F(TableMergerTest, OverrideSameResourceIdsWithOverlay) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700225 std::unique_ptr<ResourceTable> base =
226 test::ResourceTableBuilder()
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700227 .SetPackageId("", 0x7f)
Adam Lesinski71be7052017-12-12 16:48:07 -0800228 .SetSymbolState("bool/foo", ResourceId(0x7f, 0x01, 0x0001), Visibility::Level::kPublic)
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700229 .Build();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700230 std::unique_ptr<ResourceTable> overlay =
231 test::ResourceTableBuilder()
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700232 .SetPackageId("", 0x7f)
Adam Lesinski71be7052017-12-12 16:48:07 -0800233 .SetSymbolState("bool/foo", ResourceId(0x7f, 0x01, 0x0001), Visibility::Level::kPublic)
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700234 .Build();
Alexandria Cornwall6a1f8db2016-08-11 13:49:44 -0700235
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700236 ResourceTable final_table;
237 TableMergerOptions options;
238 options.auto_add_overlay = false;
239 TableMerger merger(context_.get(), &final_table, options);
Alexandria Cornwall6a1f8db2016-08-11 13:49:44 -0700240
Adam Lesinski00451162017-10-03 07:44:08 -0700241 ASSERT_TRUE(merger.Merge({}, base.get(), false /*overlay*/));
242 ASSERT_TRUE(merger.Merge({}, overlay.get(), true /*overlay*/));
Alexandria Cornwall6a1f8db2016-08-11 13:49:44 -0700243}
244
245TEST_F(TableMergerTest, FailToOverrideConflictingTypeIdsWithOverlay) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700246 std::unique_ptr<ResourceTable> base =
247 test::ResourceTableBuilder()
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700248 .SetPackageId("", 0x7f)
Adam Lesinski71be7052017-12-12 16:48:07 -0800249 .SetSymbolState("bool/foo", ResourceId(0x7f, 0x01, 0x0001), Visibility::Level::kPublic)
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700250 .Build();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700251 std::unique_ptr<ResourceTable> overlay =
252 test::ResourceTableBuilder()
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700253 .SetPackageId("", 0x7f)
Adam Lesinski71be7052017-12-12 16:48:07 -0800254 .SetSymbolState("bool/foo", ResourceId(0x7f, 0x02, 0x0001), Visibility::Level::kPublic)
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700255 .Build();
Alexandria Cornwall6a1f8db2016-08-11 13:49:44 -0700256
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700257 ResourceTable final_table;
258 TableMergerOptions options;
259 options.auto_add_overlay = false;
260 TableMerger merger(context_.get(), &final_table, options);
Alexandria Cornwall6a1f8db2016-08-11 13:49:44 -0700261
Adam Lesinski00451162017-10-03 07:44:08 -0700262 ASSERT_TRUE(merger.Merge({}, base.get(), false /*overlay*/));
263 ASSERT_FALSE(merger.Merge({}, overlay.get(), true /*overlay*/));
Alexandria Cornwall6a1f8db2016-08-11 13:49:44 -0700264}
265
266TEST_F(TableMergerTest, FailToOverrideConflictingEntryIdsWithOverlay) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700267 std::unique_ptr<ResourceTable> base =
268 test::ResourceTableBuilder()
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700269 .SetPackageId("", 0x7f)
Adam Lesinski71be7052017-12-12 16:48:07 -0800270 .SetSymbolState("bool/foo", ResourceId(0x7f, 0x01, 0x0001), Visibility::Level::kPublic)
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700271 .Build();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700272 std::unique_ptr<ResourceTable> overlay =
273 test::ResourceTableBuilder()
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700274 .SetPackageId("", 0x7f)
Adam Lesinski71be7052017-12-12 16:48:07 -0800275 .SetSymbolState("bool/foo", ResourceId(0x7f, 0x01, 0x0002), Visibility::Level::kPublic)
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700276 .Build();
Alexandria Cornwall6a1f8db2016-08-11 13:49:44 -0700277
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700278 ResourceTable final_table;
279 TableMergerOptions options;
280 options.auto_add_overlay = false;
281 TableMerger merger(context_.get(), &final_table, options);
Alexandria Cornwall6a1f8db2016-08-11 13:49:44 -0700282
Adam Lesinski00451162017-10-03 07:44:08 -0700283 ASSERT_TRUE(merger.Merge({}, base.get(), false /*overlay*/));
284 ASSERT_FALSE(merger.Merge({}, overlay.get(), true /*overlay*/));
Alexandria Cornwall6a1f8db2016-08-11 13:49:44 -0700285}
286
Izabela Orlowskad51efe82018-04-24 18:18:29 +0100287TEST_F(TableMergerTest, FailConflictingVisibility) {
288 std::unique_ptr<ResourceTable> base =
289 test::ResourceTableBuilder()
290 .SetPackageId("", 0x7f)
291 .SetSymbolState("bool/foo", ResourceId(0x7f, 0x01, 0x0001), Visibility::Level::kPublic)
292 .Build();
293 std::unique_ptr<ResourceTable> overlay =
294 test::ResourceTableBuilder()
295 .SetPackageId("", 0x7f)
296 .SetSymbolState("bool/foo", ResourceId(0x7f, 0x01, 0x0001), Visibility::Level::kPrivate)
297 .Build();
298
299 // It should fail if the "--strict-visibility" flag is set.
300 ResourceTable final_table;
301 TableMergerOptions options;
302 options.auto_add_overlay = false;
303 options.strict_visibility = true;
304 TableMerger merger(context_.get(), &final_table, options);
305
306 ASSERT_TRUE(merger.Merge({}, base.get(), false /*overlay*/));
307 ASSERT_FALSE(merger.Merge({}, overlay.get(), true /*overlay*/));
308
309 // But it should still pass if the flag is not set.
310 ResourceTable final_table2;
311 options.strict_visibility = false;
312 TableMerger merger2(context_.get(), &final_table2, options);
313
314 ASSERT_TRUE(merger2.Merge({}, base.get(), false /*overlay*/));
315 ASSERT_TRUE(merger2.Merge({}, overlay.get(), true /*overlay*/));
316}
317
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800318TEST_F(TableMergerTest, MergeAddResourceFromOverlay) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700319 std::unique_ptr<ResourceTable> table_a =
Adam Lesinski4488f1c2017-05-26 17:33:38 -0700320 test::ResourceTableBuilder().SetPackageId("", 0x7f).Build();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700321 std::unique_ptr<ResourceTable> table_b =
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700322 test::ResourceTableBuilder()
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700323 .SetPackageId("", 0x7f)
Adam Lesinski71be7052017-12-12 16:48:07 -0800324 .SetSymbolState("bool/foo", {}, Visibility::Level::kUndefined, true /*allow new overlay*/)
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700325 .AddValue("bool/foo", ResourceUtils::TryParseBool("true"))
326 .Build();
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800327
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700328 ResourceTable final_table;
Adam Lesinski4488f1c2017-05-26 17:33:38 -0700329 TableMergerOptions options;
330 options.auto_add_overlay = false;
331 TableMerger merger(context_.get(), &final_table, options);
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800332
Adam Lesinski00451162017-10-03 07:44:08 -0700333 ASSERT_TRUE(merger.Merge({}, table_a.get(), false /*overlay*/));
334 ASSERT_TRUE(merger.Merge({}, table_b.get(), false /*overlay*/));
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800335}
336
337TEST_F(TableMergerTest, MergeAddResourceFromOverlayWithAutoAddOverlay) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700338 std::unique_ptr<ResourceTable> table_a =
339 test::ResourceTableBuilder().SetPackageId("", 0x7f).Build();
340 std::unique_ptr<ResourceTable> table_b =
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700341 test::ResourceTableBuilder()
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700342 .SetPackageId("", 0x7f)
343 .AddValue("bool/foo", ResourceUtils::TryParseBool("true"))
344 .Build();
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800345
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700346 ResourceTable final_table;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700347 TableMergerOptions options;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700348 options.auto_add_overlay = true;
349 TableMerger merger(context_.get(), &final_table, options);
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800350
Adam Lesinski00451162017-10-03 07:44:08 -0700351 ASSERT_TRUE(merger.Merge({}, table_a.get(), false /*overlay*/));
352 ASSERT_TRUE(merger.Merge({}, table_b.get(), false /*overlay*/));
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800353}
354
355TEST_F(TableMergerTest, FailToMergeNewResourceWithoutAutoAddOverlay) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700356 std::unique_ptr<ResourceTable> table_a =
357 test::ResourceTableBuilder().SetPackageId("", 0x7f).Build();
358 std::unique_ptr<ResourceTable> table_b =
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700359 test::ResourceTableBuilder()
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700360 .SetPackageId("", 0x7f)
361 .AddValue("bool/foo", ResourceUtils::TryParseBool("true"))
362 .Build();
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800363
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700364 ResourceTable final_table;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700365 TableMergerOptions options;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700366 options.auto_add_overlay = false;
367 TableMerger merger(context_.get(), &final_table, options);
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800368
Adam Lesinski00451162017-10-03 07:44:08 -0700369 ASSERT_TRUE(merger.Merge({}, table_a.get(), false /*overlay*/));
370 ASSERT_FALSE(merger.Merge({}, table_b.get(), true /*overlay*/));
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700371}
372
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700373TEST_F(TableMergerTest, OverlaidStyleablesAndStylesShouldBeMerged) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700374 std::unique_ptr<ResourceTable> table_a =
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700375 test::ResourceTableBuilder()
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700376 .SetPackageId("com.app.a", 0x7f)
377 .AddValue("com.app.a:styleable/Foo",
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700378 test::StyleableBuilder()
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700379 .AddItem("com.app.a:attr/bar")
380 .AddItem("com.app.a:attr/foo", ResourceId(0x01010000))
381 .Build())
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700382 .AddValue("com.app.a:style/Theme",
383 test::StyleBuilder()
384 .SetParent("com.app.a:style/Parent")
385 .AddItem("com.app.a:attr/bar", util::make_unique<Id>())
386 .AddItem("com.app.a:attr/foo", ResourceUtils::MakeBool(false))
387 .Build())
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700388 .Build();
Adam Lesinski5c3464c2016-08-24 16:03:48 -0700389
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700390 std::unique_ptr<ResourceTable> table_b =
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700391 test::ResourceTableBuilder()
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700392 .SetPackageId("com.app.a", 0x7f)
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700393 .AddValue("com.app.a:styleable/Foo", test::StyleableBuilder()
394 .AddItem("com.app.a:attr/bat")
395 .AddItem("com.app.a:attr/foo")
396 .Build())
397 .AddValue("com.app.a:style/Theme",
398 test::StyleBuilder()
399 .SetParent("com.app.a:style/OverlayParent")
400 .AddItem("com.app.a:attr/bat", util::make_unique<Id>())
401 .AddItem("com.app.a:attr/foo", ResourceId(0x01010000),
402 ResourceUtils::MakeBool(true))
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700403 .Build())
404 .Build();
Adam Lesinski5c3464c2016-08-24 16:03:48 -0700405
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700406 ResourceTable final_table;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700407 TableMergerOptions options;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700408 options.auto_add_overlay = true;
409 TableMerger merger(context_.get(), &final_table, options);
Adam Lesinski5c3464c2016-08-24 16:03:48 -0700410
Adam Lesinski00451162017-10-03 07:44:08 -0700411 ASSERT_TRUE(merger.Merge({}, table_a.get(), false /*overlay*/));
412 ASSERT_TRUE(merger.Merge({}, table_b.get(), true /*overlay*/));
Adam Lesinski5c3464c2016-08-24 16:03:48 -0700413
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700414 Styleable* styleable = test::GetValue<Styleable>(&final_table, "com.app.a:styleable/Foo");
415 ASSERT_THAT(styleable, NotNull());
Adam Lesinski5c3464c2016-08-24 16:03:48 -0700416
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700417 std::vector<Reference> expected_refs = {
418 Reference(test::ParseNameOrDie("com.app.a:attr/bar")),
419 Reference(test::ParseNameOrDie("com.app.a:attr/bat")),
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700420 Reference(test::ParseNameOrDie("com.app.a:attr/foo"), ResourceId(0x01010000)),
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700421 };
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700422 EXPECT_THAT(styleable->entries, UnorderedElementsAreArray(expected_refs));
Adam Lesinski5c3464c2016-08-24 16:03:48 -0700423
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700424 Style* style = test::GetValue<Style>(&final_table, "com.app.a:style/Theme");
425 ASSERT_THAT(style, NotNull());
426
427 std::vector<Reference> extracted_refs;
428 for (const auto& entry : style->entries) {
429 extracted_refs.push_back(entry.key);
430 }
431 EXPECT_THAT(extracted_refs, UnorderedElementsAreArray(expected_refs));
432
433 const auto expected = ResourceUtils::MakeBool(true);
434 EXPECT_THAT(style->entries, Contains(Field(&Style::Entry::value, Pointee(ValueEq(*expected)))));
435 EXPECT_THAT(style->parent,
436 Eq(make_value(Reference(test::ParseNameOrDie("com.app.a:style/OverlayParent")))));
Adam Lesinski5c3464c2016-08-24 16:03:48 -0700437}
438
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -0800439TEST_F(TableMergerTest, SetOverlayable) {
Ryan Mitchell54237ff2018-12-13 15:44:29 -0800440 auto overlayable = std::make_shared<Overlayable>("CustomizableResources",
441 "overlay://customization");
442 OverlayableItem overlayable_item(overlayable);
443 overlayable_item.policies |= OverlayableItem::Policy::kProduct;
444 overlayable_item.policies |= OverlayableItem::Policy::kVendor;
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -0800445
Ryan Mitchelle4e989c2018-10-29 02:21:50 -0700446 std::unique_ptr<ResourceTable> table_a =
447 test::ResourceTableBuilder()
448 .SetPackageId("com.app.a", 0x7f)
Ryan Mitchell54237ff2018-12-13 15:44:29 -0800449 .SetOverlayable("bool/foo", overlayable_item)
Ryan Mitchelle4e989c2018-10-29 02:21:50 -0700450 .Build();
451
452 std::unique_ptr<ResourceTable> table_b =
453 test::ResourceTableBuilder()
454 .SetPackageId("com.app.a", 0x7f)
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -0800455 .AddSimple("bool/foo")
Ryan Mitchelle4e989c2018-10-29 02:21:50 -0700456 .Build();
457
458 ResourceTable final_table;
459 TableMergerOptions options;
460 options.auto_add_overlay = true;
461 TableMerger merger(context_.get(), &final_table, options);
462 ASSERT_TRUE(merger.Merge({}, table_a.get(), false /*overlay*/));
463 ASSERT_TRUE(merger.Merge({}, table_b.get(), false /*overlay*/));
464
465 const ResourceName name = test::ParseNameOrDie("com.app.a:bool/foo");
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -0800466 Maybe<ResourceTable::SearchResult> search_result = final_table.FindResource(name);
467 ASSERT_TRUE(search_result);
Ryan Mitchell54237ff2018-12-13 15:44:29 -0800468 ASSERT_TRUE(search_result.value().entry->overlayable_item);
469 OverlayableItem& result_overlayable_item = search_result.value().entry->overlayable_item.value();
470 EXPECT_THAT(result_overlayable_item.overlayable->name, Eq("CustomizableResources"));
471 EXPECT_THAT(result_overlayable_item.overlayable->actor, Eq("overlay://customization"));
472 EXPECT_THAT(result_overlayable_item.policies, Eq(OverlayableItem::Policy::kProduct
473 | OverlayableItem::Policy::kVendor));
Ryan Mitchelle4e989c2018-10-29 02:21:50 -0700474}
475
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -0800476TEST_F(TableMergerTest, SetOverlayableLater) {
Ryan Mitchell54237ff2018-12-13 15:44:29 -0800477 auto overlayable = std::make_shared<Overlayable>("CustomizableResources",
478 "overlay://customization");
Ryan Mitchelle4e989c2018-10-29 02:21:50 -0700479 std::unique_ptr<ResourceTable> table_a =
480 test::ResourceTableBuilder()
481 .SetPackageId("com.app.a", 0x7f)
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -0800482 .AddSimple("bool/foo")
Ryan Mitchelle4e989c2018-10-29 02:21:50 -0700483 .Build();
484
Ryan Mitchell54237ff2018-12-13 15:44:29 -0800485 OverlayableItem overlayable_item(overlayable);
486 overlayable_item.policies |= OverlayableItem::Policy::kPublic;
Ryan Mitchell02d9c1e2019-01-11 16:36:58 -0800487 overlayable_item.policies |= OverlayableItem::Policy::kSystem;
Ryan Mitchelle4e989c2018-10-29 02:21:50 -0700488 std::unique_ptr<ResourceTable> table_b =
489 test::ResourceTableBuilder()
490 .SetPackageId("com.app.a", 0x7f)
Ryan Mitchell54237ff2018-12-13 15:44:29 -0800491 .SetOverlayable("bool/foo", overlayable_item)
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -0800492 .Build();
493
494 ResourceTable final_table;
495 TableMergerOptions options;
496 options.auto_add_overlay = true;
497 TableMerger merger(context_.get(), &final_table, options);
498 ASSERT_TRUE(merger.Merge({}, table_a.get(), false /*overlay*/));
499 ASSERT_TRUE(merger.Merge({}, table_b.get(), false /*overlay*/));
500
501 const ResourceName name = test::ParseNameOrDie("com.app.a:bool/foo");
502 Maybe<ResourceTable::SearchResult> search_result = final_table.FindResource(name);
503 ASSERT_TRUE(search_result);
Ryan Mitchell54237ff2018-12-13 15:44:29 -0800504 ASSERT_TRUE(search_result.value().entry->overlayable_item);
505 OverlayableItem& result_overlayable_item = search_result.value().entry->overlayable_item.value();
506 EXPECT_THAT(result_overlayable_item.overlayable->name, Eq("CustomizableResources"));
507 EXPECT_THAT(result_overlayable_item.overlayable->actor, Eq("overlay://customization"));
508 EXPECT_THAT(result_overlayable_item.policies, Eq(OverlayableItem::Policy::kPublic
Ryan Mitchell02d9c1e2019-01-11 16:36:58 -0800509 | OverlayableItem::Policy::kSystem));
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -0800510}
511
Ryan Mitchell54237ff2018-12-13 15:44:29 -0800512TEST_F(TableMergerTest, SameResourceDifferentNameFail) {
513 auto overlayable_first = std::make_shared<Overlayable>("CustomizableResources",
514 "overlay://customization");
515 OverlayableItem overlayable_item_first(overlayable_first);
516 overlayable_item_first.policies |= OverlayableItem::Policy::kProduct;
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -0800517 std::unique_ptr<ResourceTable> table_a =
518 test::ResourceTableBuilder()
519 .SetPackageId("com.app.a", 0x7f)
Ryan Mitchell54237ff2018-12-13 15:44:29 -0800520 .SetOverlayable("bool/foo", overlayable_item_first)
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -0800521 .Build();
522
Ryan Mitchell54237ff2018-12-13 15:44:29 -0800523 auto overlayable_second = std::make_shared<Overlayable>("ThemeResources",
524 "overlay://theme");
525 OverlayableItem overlayable_item_second(overlayable_second);
526 overlayable_item_second.policies |= OverlayableItem::Policy::kProduct;
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -0800527 std::unique_ptr<ResourceTable> table_b =
528 test::ResourceTableBuilder()
529 .SetPackageId("com.app.a", 0x7f)
Ryan Mitchell54237ff2018-12-13 15:44:29 -0800530 .SetOverlayable("bool/foo", overlayable_item_second)
Ryan Mitchelle4e989c2018-10-29 02:21:50 -0700531 .Build();
532
533 ResourceTable final_table;
534 TableMergerOptions options;
535 options.auto_add_overlay = true;
536 TableMerger merger(context_.get(), &final_table, options);
537 ASSERT_TRUE(merger.Merge({}, table_a.get(), false /*overlay*/));
538 ASSERT_FALSE(merger.Merge({}, table_b.get(), false /*overlay*/));
539}
540
Ryan Mitchell54237ff2018-12-13 15:44:29 -0800541TEST_F(TableMergerTest, SameResourceSameNameFail) {
542 auto overlayable = std::make_shared<Overlayable>("CustomizableResources",
543 "overlay://customization");
544
545 OverlayableItem overlayable_item_first(overlayable);
546 overlayable_item_first.policies |= OverlayableItem::Policy::kProduct;
Ryan Mitchelle4e989c2018-10-29 02:21:50 -0700547 std::unique_ptr<ResourceTable> table_a =
548 test::ResourceTableBuilder()
549 .SetPackageId("com.app.a", 0x7f)
Ryan Mitchell54237ff2018-12-13 15:44:29 -0800550 .SetOverlayable("bool/foo", overlayable_item_first)
Ryan Mitchelle4e989c2018-10-29 02:21:50 -0700551 .Build();
552
Ryan Mitchell54237ff2018-12-13 15:44:29 -0800553 OverlayableItem overlayable_item_second(overlayable);
554 overlayable_item_second.policies |= OverlayableItem::Policy::kSystem;
Ryan Mitchelle4e989c2018-10-29 02:21:50 -0700555 std::unique_ptr<ResourceTable> table_b =
556 test::ResourceTableBuilder()
557 .SetPackageId("com.app.a", 0x7f)
Ryan Mitchell54237ff2018-12-13 15:44:29 -0800558 .SetOverlayable("bool/foo", overlayable_item_second)
Ryan Mitchelle4e989c2018-10-29 02:21:50 -0700559 .Build();
560
561 ResourceTable final_table;
562 TableMergerOptions options;
563 options.auto_add_overlay = true;
564 TableMerger merger(context_.get(), &final_table, options);
565 ASSERT_TRUE(merger.Merge({}, table_a.get(), false /*overlay*/));
566 ASSERT_FALSE(merger.Merge({}, table_b.get(), false /*overlay*/));
567}
568
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700569} // namespace aapt