blob: cf504c4f4ce513a7498f74adee7dd1903292478c [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 Lesinskice5e56e22016-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 Lesinskice5e56e22016-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 Lesinskice5e56e22016-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 Lesinskice5e56e22016-10-21 17:56:45 -070038 context_ =
Adam Lesinskicacb28f2016-10-19 12:18:14 -070039 test::ContextBuilder()
40 // We are compiling this package.
Adam Lesinskice5e56e22016-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 Lesinskice5e56e22016-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 Lesinskice5e56e22016-10-21 17:56:45 -070047 .SetNameManglerPolicy(NameManglerPolicy{"com.app.a", {"com.app.b"}})
Adam Lesinski1ab598f2015-08-14 14:26:04 -070048
Adam Lesinskice5e56e22016-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 Lesinskice5e56e22016-10-21 17:56:45 -070054 std::unique_ptr<ResourceTable> table_a =
Adam Lesinskicacb28f2016-10-19 12:18:14 -070055 test::ResourceTableBuilder()
Adam Lesinskice5e56e22016-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 Lesinskice5e56e22016-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 Lesinskice5e56e22016-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 Lesinskice5e56e22016-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 Lesinskice5e56e22016-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 Lesinskice5e56e22016-10-21 17:56:45 -070090 ResourceTable final_table;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070091 TableMergerOptions options;
Adam Lesinskice5e56e22016-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 Lesinskice5e56e22016-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 Lesinskice5e56e22016-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 Lesinskice5e56e22016-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 Lesinskice5e56e22016-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 Lesinskice5e56e22016-10-21 17:56:45 -0700128 std::unique_ptr<ResourceTable> table_a =
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700129 test::ResourceTableBuilder()
Adam Lesinskice5e56e22016-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 Lesinskice5e56e22016-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 Lesinskice5e56e22016-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 Lesinskice5e56e22016-10-21 17:56:45 -0700137 .Build();
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700138
Adam Lesinskice5e56e22016-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 Lesinskice5e56e22016-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 Lesinskice5e56e22016-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 Lesinskice5e56e22016-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
Alexandria Cornwall6a1f8db2016-08-11 13:49:44 -0700181TEST_F(TableMergerTest, OverrideSameResourceIdsWithOverlay) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700182 std::unique_ptr<ResourceTable> base =
183 test::ResourceTableBuilder()
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700184 .SetPackageId("", 0x7f)
Adam Lesinski71be7052017-12-12 16:48:07 -0800185 .SetSymbolState("bool/foo", ResourceId(0x7f, 0x01, 0x0001), Visibility::Level::kPublic)
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700186 .Build();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700187 std::unique_ptr<ResourceTable> overlay =
188 test::ResourceTableBuilder()
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700189 .SetPackageId("", 0x7f)
Adam Lesinski71be7052017-12-12 16:48:07 -0800190 .SetSymbolState("bool/foo", ResourceId(0x7f, 0x01, 0x0001), Visibility::Level::kPublic)
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700191 .Build();
Alexandria Cornwall6a1f8db2016-08-11 13:49:44 -0700192
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700193 ResourceTable final_table;
194 TableMergerOptions options;
195 options.auto_add_overlay = false;
196 TableMerger merger(context_.get(), &final_table, options);
Alexandria Cornwall6a1f8db2016-08-11 13:49:44 -0700197
Adam Lesinski00451162017-10-03 07:44:08 -0700198 ASSERT_TRUE(merger.Merge({}, base.get(), false /*overlay*/));
199 ASSERT_TRUE(merger.Merge({}, overlay.get(), true /*overlay*/));
Alexandria Cornwall6a1f8db2016-08-11 13:49:44 -0700200}
201
202TEST_F(TableMergerTest, FailToOverrideConflictingTypeIdsWithOverlay) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700203 std::unique_ptr<ResourceTable> base =
204 test::ResourceTableBuilder()
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700205 .SetPackageId("", 0x7f)
Adam Lesinski71be7052017-12-12 16:48:07 -0800206 .SetSymbolState("bool/foo", ResourceId(0x7f, 0x01, 0x0001), Visibility::Level::kPublic)
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700207 .Build();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700208 std::unique_ptr<ResourceTable> overlay =
209 test::ResourceTableBuilder()
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700210 .SetPackageId("", 0x7f)
Adam Lesinski71be7052017-12-12 16:48:07 -0800211 .SetSymbolState("bool/foo", ResourceId(0x7f, 0x02, 0x0001), Visibility::Level::kPublic)
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700212 .Build();
Alexandria Cornwall6a1f8db2016-08-11 13:49:44 -0700213
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700214 ResourceTable final_table;
215 TableMergerOptions options;
216 options.auto_add_overlay = false;
217 TableMerger merger(context_.get(), &final_table, options);
Alexandria Cornwall6a1f8db2016-08-11 13:49:44 -0700218
Adam Lesinski00451162017-10-03 07:44:08 -0700219 ASSERT_TRUE(merger.Merge({}, base.get(), false /*overlay*/));
220 ASSERT_FALSE(merger.Merge({}, overlay.get(), true /*overlay*/));
Alexandria Cornwall6a1f8db2016-08-11 13:49:44 -0700221}
222
223TEST_F(TableMergerTest, FailToOverrideConflictingEntryIdsWithOverlay) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700224 std::unique_ptr<ResourceTable> base =
225 test::ResourceTableBuilder()
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700226 .SetPackageId("", 0x7f)
Adam Lesinski71be7052017-12-12 16:48:07 -0800227 .SetSymbolState("bool/foo", ResourceId(0x7f, 0x01, 0x0001), Visibility::Level::kPublic)
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700228 .Build();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700229 std::unique_ptr<ResourceTable> overlay =
230 test::ResourceTableBuilder()
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700231 .SetPackageId("", 0x7f)
Adam Lesinski71be7052017-12-12 16:48:07 -0800232 .SetSymbolState("bool/foo", ResourceId(0x7f, 0x01, 0x0002), Visibility::Level::kPublic)
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700233 .Build();
Alexandria Cornwall6a1f8db2016-08-11 13:49:44 -0700234
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700235 ResourceTable final_table;
236 TableMergerOptions options;
237 options.auto_add_overlay = false;
238 TableMerger merger(context_.get(), &final_table, options);
Alexandria Cornwall6a1f8db2016-08-11 13:49:44 -0700239
Adam Lesinski00451162017-10-03 07:44:08 -0700240 ASSERT_TRUE(merger.Merge({}, base.get(), false /*overlay*/));
241 ASSERT_FALSE(merger.Merge({}, overlay.get(), true /*overlay*/));
Alexandria Cornwall6a1f8db2016-08-11 13:49:44 -0700242}
243
Izabela Orlowskad51efe82018-04-24 18:18:29 +0100244TEST_F(TableMergerTest, FailConflictingVisibility) {
245 std::unique_ptr<ResourceTable> base =
246 test::ResourceTableBuilder()
247 .SetPackageId("", 0x7f)
248 .SetSymbolState("bool/foo", ResourceId(0x7f, 0x01, 0x0001), Visibility::Level::kPublic)
249 .Build();
250 std::unique_ptr<ResourceTable> overlay =
251 test::ResourceTableBuilder()
252 .SetPackageId("", 0x7f)
253 .SetSymbolState("bool/foo", ResourceId(0x7f, 0x01, 0x0001), Visibility::Level::kPrivate)
254 .Build();
255
256 // It should fail if the "--strict-visibility" flag is set.
257 ResourceTable final_table;
258 TableMergerOptions options;
259 options.auto_add_overlay = false;
260 options.strict_visibility = true;
261 TableMerger merger(context_.get(), &final_table, options);
262
263 ASSERT_TRUE(merger.Merge({}, base.get(), false /*overlay*/));
264 ASSERT_FALSE(merger.Merge({}, overlay.get(), true /*overlay*/));
265
266 // But it should still pass if the flag is not set.
267 ResourceTable final_table2;
268 options.strict_visibility = false;
269 TableMerger merger2(context_.get(), &final_table2, options);
270
271 ASSERT_TRUE(merger2.Merge({}, base.get(), false /*overlay*/));
272 ASSERT_TRUE(merger2.Merge({}, overlay.get(), true /*overlay*/));
273}
274
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800275TEST_F(TableMergerTest, MergeAddResourceFromOverlay) {
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700276 std::unique_ptr<ResourceTable> table_a =
Adam Lesinski4488f1c2017-05-26 17:33:38 -0700277 test::ResourceTableBuilder().SetPackageId("", 0x7f).Build();
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700278 std::unique_ptr<ResourceTable> table_b =
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700279 test::ResourceTableBuilder()
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700280 .SetPackageId("", 0x7f)
Adam Lesinski71be7052017-12-12 16:48:07 -0800281 .SetSymbolState("bool/foo", {}, Visibility::Level::kUndefined, true /*allow new overlay*/)
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700282 .AddValue("bool/foo", ResourceUtils::TryParseBool("true"))
283 .Build();
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800284
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700285 ResourceTable final_table;
Adam Lesinski4488f1c2017-05-26 17:33:38 -0700286 TableMergerOptions options;
287 options.auto_add_overlay = false;
288 TableMerger merger(context_.get(), &final_table, options);
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800289
Adam Lesinski00451162017-10-03 07:44:08 -0700290 ASSERT_TRUE(merger.Merge({}, table_a.get(), false /*overlay*/));
291 ASSERT_TRUE(merger.Merge({}, table_b.get(), false /*overlay*/));
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800292}
293
294TEST_F(TableMergerTest, MergeAddResourceFromOverlayWithAutoAddOverlay) {
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700295 std::unique_ptr<ResourceTable> table_a =
296 test::ResourceTableBuilder().SetPackageId("", 0x7f).Build();
297 std::unique_ptr<ResourceTable> table_b =
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700298 test::ResourceTableBuilder()
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700299 .SetPackageId("", 0x7f)
300 .AddValue("bool/foo", ResourceUtils::TryParseBool("true"))
301 .Build();
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800302
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700303 ResourceTable final_table;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700304 TableMergerOptions options;
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700305 options.auto_add_overlay = true;
306 TableMerger merger(context_.get(), &final_table, options);
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800307
Adam Lesinski00451162017-10-03 07:44:08 -0700308 ASSERT_TRUE(merger.Merge({}, table_a.get(), false /*overlay*/));
309 ASSERT_TRUE(merger.Merge({}, table_b.get(), false /*overlay*/));
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800310}
311
312TEST_F(TableMergerTest, FailToMergeNewResourceWithoutAutoAddOverlay) {
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700313 std::unique_ptr<ResourceTable> table_a =
314 test::ResourceTableBuilder().SetPackageId("", 0x7f).Build();
315 std::unique_ptr<ResourceTable> table_b =
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700316 test::ResourceTableBuilder()
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700317 .SetPackageId("", 0x7f)
318 .AddValue("bool/foo", ResourceUtils::TryParseBool("true"))
319 .Build();
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800320
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700321 ResourceTable final_table;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700322 TableMergerOptions options;
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700323 options.auto_add_overlay = false;
324 TableMerger merger(context_.get(), &final_table, options);
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800325
Adam Lesinski00451162017-10-03 07:44:08 -0700326 ASSERT_TRUE(merger.Merge({}, table_a.get(), false /*overlay*/));
327 ASSERT_FALSE(merger.Merge({}, table_b.get(), true /*overlay*/));
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700328}
329
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700330TEST_F(TableMergerTest, OverlaidStyleablesAndStylesShouldBeMerged) {
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700331 std::unique_ptr<ResourceTable> table_a =
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700332 test::ResourceTableBuilder()
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700333 .SetPackageId("com.app.a", 0x7f)
334 .AddValue("com.app.a:styleable/Foo",
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700335 test::StyleableBuilder()
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700336 .AddItem("com.app.a:attr/bar")
337 .AddItem("com.app.a:attr/foo", ResourceId(0x01010000))
338 .Build())
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700339 .AddValue("com.app.a:style/Theme",
340 test::StyleBuilder()
341 .SetParent("com.app.a:style/Parent")
342 .AddItem("com.app.a:attr/bar", util::make_unique<Id>())
343 .AddItem("com.app.a:attr/foo", ResourceUtils::MakeBool(false))
344 .Build())
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700345 .Build();
Adam Lesinski5c3464c2016-08-24 16:03:48 -0700346
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700347 std::unique_ptr<ResourceTable> table_b =
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700348 test::ResourceTableBuilder()
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700349 .SetPackageId("com.app.a", 0x7f)
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700350 .AddValue("com.app.a:styleable/Foo", test::StyleableBuilder()
351 .AddItem("com.app.a:attr/bat")
352 .AddItem("com.app.a:attr/foo")
353 .Build())
354 .AddValue("com.app.a:style/Theme",
355 test::StyleBuilder()
356 .SetParent("com.app.a:style/OverlayParent")
357 .AddItem("com.app.a:attr/bat", util::make_unique<Id>())
358 .AddItem("com.app.a:attr/foo", ResourceId(0x01010000),
359 ResourceUtils::MakeBool(true))
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700360 .Build())
361 .Build();
Adam Lesinski5c3464c2016-08-24 16:03:48 -0700362
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700363 ResourceTable final_table;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700364 TableMergerOptions options;
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700365 options.auto_add_overlay = true;
366 TableMerger merger(context_.get(), &final_table, options);
Adam Lesinski5c3464c2016-08-24 16:03:48 -0700367
Adam Lesinski00451162017-10-03 07:44:08 -0700368 ASSERT_TRUE(merger.Merge({}, table_a.get(), false /*overlay*/));
369 ASSERT_TRUE(merger.Merge({}, table_b.get(), true /*overlay*/));
Adam Lesinski5c3464c2016-08-24 16:03:48 -0700370
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700371 Styleable* styleable = test::GetValue<Styleable>(&final_table, "com.app.a:styleable/Foo");
372 ASSERT_THAT(styleable, NotNull());
Adam Lesinski5c3464c2016-08-24 16:03:48 -0700373
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700374 std::vector<Reference> expected_refs = {
375 Reference(test::ParseNameOrDie("com.app.a:attr/bar")),
376 Reference(test::ParseNameOrDie("com.app.a:attr/bat")),
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700377 Reference(test::ParseNameOrDie("com.app.a:attr/foo"), ResourceId(0x01010000)),
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700378 };
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700379 EXPECT_THAT(styleable->entries, UnorderedElementsAreArray(expected_refs));
Adam Lesinski5c3464c2016-08-24 16:03:48 -0700380
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700381 Style* style = test::GetValue<Style>(&final_table, "com.app.a:style/Theme");
382 ASSERT_THAT(style, NotNull());
383
384 std::vector<Reference> extracted_refs;
385 for (const auto& entry : style->entries) {
386 extracted_refs.push_back(entry.key);
387 }
388 EXPECT_THAT(extracted_refs, UnorderedElementsAreArray(expected_refs));
389
390 const auto expected = ResourceUtils::MakeBool(true);
391 EXPECT_THAT(style->entries, Contains(Field(&Style::Entry::value, Pointee(ValueEq(*expected)))));
392 EXPECT_THAT(style->parent,
393 Eq(make_value(Reference(test::ParseNameOrDie("com.app.a:style/OverlayParent")))));
Adam Lesinski5c3464c2016-08-24 16:03:48 -0700394}
395
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700396} // namespace aapt