blob: 34461c6b467d27acf0fd24e4b02f140c6ff95280 [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
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 Lesinskice5e56e2016-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 Lesinskice5e56e2016-10-21 17:56:45 -0700186 .Build();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700187 std::unique_ptr<ResourceTable> overlay =
188 test::ResourceTableBuilder()
Adam Lesinskice5e56e2016-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 Lesinskice5e56e2016-10-21 17:56:45 -0700191 .Build();
Alexandria Cornwall6a1f8db2016-08-11 13:49:44 -0700192
Adam Lesinskice5e56e2016-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 Lesinskice5e56e2016-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 Lesinskice5e56e2016-10-21 17:56:45 -0700207 .Build();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700208 std::unique_ptr<ResourceTable> overlay =
209 test::ResourceTableBuilder()
Adam Lesinskice5e56e2016-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 Lesinskice5e56e2016-10-21 17:56:45 -0700212 .Build();
Alexandria Cornwall6a1f8db2016-08-11 13:49:44 -0700213
Adam Lesinskice5e56e2016-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 Lesinskice5e56e2016-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 Lesinskice5e56e2016-10-21 17:56:45 -0700228 .Build();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700229 std::unique_ptr<ResourceTable> overlay =
230 test::ResourceTableBuilder()
Adam Lesinskice5e56e2016-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 Lesinskice5e56e2016-10-21 17:56:45 -0700233 .Build();
Alexandria Cornwall6a1f8db2016-08-11 13:49:44 -0700234
Adam Lesinskice5e56e2016-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
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800244TEST_F(TableMergerTest, MergeAddResourceFromOverlay) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700245 std::unique_ptr<ResourceTable> table_a =
Adam Lesinski4488f1c2017-05-26 17:33:38 -0700246 test::ResourceTableBuilder().SetPackageId("", 0x7f).Build();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700247 std::unique_ptr<ResourceTable> table_b =
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700248 test::ResourceTableBuilder()
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700249 .SetPackageId("", 0x7f)
Adam Lesinski71be7052017-12-12 16:48:07 -0800250 .SetSymbolState("bool/foo", {}, Visibility::Level::kUndefined, true /*allow new overlay*/)
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700251 .AddValue("bool/foo", ResourceUtils::TryParseBool("true"))
252 .Build();
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800253
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700254 ResourceTable final_table;
Adam Lesinski4488f1c2017-05-26 17:33:38 -0700255 TableMergerOptions options;
256 options.auto_add_overlay = false;
257 TableMerger merger(context_.get(), &final_table, options);
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800258
Adam Lesinski00451162017-10-03 07:44:08 -0700259 ASSERT_TRUE(merger.Merge({}, table_a.get(), false /*overlay*/));
260 ASSERT_TRUE(merger.Merge({}, table_b.get(), false /*overlay*/));
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800261}
262
263TEST_F(TableMergerTest, MergeAddResourceFromOverlayWithAutoAddOverlay) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700264 std::unique_ptr<ResourceTable> table_a =
265 test::ResourceTableBuilder().SetPackageId("", 0x7f).Build();
266 std::unique_ptr<ResourceTable> table_b =
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700267 test::ResourceTableBuilder()
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700268 .SetPackageId("", 0x7f)
269 .AddValue("bool/foo", ResourceUtils::TryParseBool("true"))
270 .Build();
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800271
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700272 ResourceTable final_table;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700273 TableMergerOptions options;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700274 options.auto_add_overlay = true;
275 TableMerger merger(context_.get(), &final_table, options);
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800276
Adam Lesinski00451162017-10-03 07:44:08 -0700277 ASSERT_TRUE(merger.Merge({}, table_a.get(), false /*overlay*/));
278 ASSERT_TRUE(merger.Merge({}, table_b.get(), false /*overlay*/));
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800279}
280
281TEST_F(TableMergerTest, FailToMergeNewResourceWithoutAutoAddOverlay) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700282 std::unique_ptr<ResourceTable> table_a =
283 test::ResourceTableBuilder().SetPackageId("", 0x7f).Build();
284 std::unique_ptr<ResourceTable> table_b =
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700285 test::ResourceTableBuilder()
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700286 .SetPackageId("", 0x7f)
287 .AddValue("bool/foo", ResourceUtils::TryParseBool("true"))
288 .Build();
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800289
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700290 ResourceTable final_table;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700291 TableMergerOptions options;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700292 options.auto_add_overlay = false;
293 TableMerger merger(context_.get(), &final_table, options);
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800294
Adam Lesinski00451162017-10-03 07:44:08 -0700295 ASSERT_TRUE(merger.Merge({}, table_a.get(), false /*overlay*/));
296 ASSERT_FALSE(merger.Merge({}, table_b.get(), true /*overlay*/));
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700297}
298
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700299TEST_F(TableMergerTest, OverlaidStyleablesAndStylesShouldBeMerged) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700300 std::unique_ptr<ResourceTable> table_a =
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700301 test::ResourceTableBuilder()
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700302 .SetPackageId("com.app.a", 0x7f)
303 .AddValue("com.app.a:styleable/Foo",
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700304 test::StyleableBuilder()
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700305 .AddItem("com.app.a:attr/bar")
306 .AddItem("com.app.a:attr/foo", ResourceId(0x01010000))
307 .Build())
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700308 .AddValue("com.app.a:style/Theme",
309 test::StyleBuilder()
310 .SetParent("com.app.a:style/Parent")
311 .AddItem("com.app.a:attr/bar", util::make_unique<Id>())
312 .AddItem("com.app.a:attr/foo", ResourceUtils::MakeBool(false))
313 .Build())
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700314 .Build();
Adam Lesinski5c3464c2016-08-24 16:03:48 -0700315
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700316 std::unique_ptr<ResourceTable> table_b =
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700317 test::ResourceTableBuilder()
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700318 .SetPackageId("com.app.a", 0x7f)
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700319 .AddValue("com.app.a:styleable/Foo", test::StyleableBuilder()
320 .AddItem("com.app.a:attr/bat")
321 .AddItem("com.app.a:attr/foo")
322 .Build())
323 .AddValue("com.app.a:style/Theme",
324 test::StyleBuilder()
325 .SetParent("com.app.a:style/OverlayParent")
326 .AddItem("com.app.a:attr/bat", util::make_unique<Id>())
327 .AddItem("com.app.a:attr/foo", ResourceId(0x01010000),
328 ResourceUtils::MakeBool(true))
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700329 .Build())
330 .Build();
Adam Lesinski5c3464c2016-08-24 16:03:48 -0700331
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700332 ResourceTable final_table;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700333 TableMergerOptions options;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700334 options.auto_add_overlay = true;
335 TableMerger merger(context_.get(), &final_table, options);
Adam Lesinski5c3464c2016-08-24 16:03:48 -0700336
Adam Lesinski00451162017-10-03 07:44:08 -0700337 ASSERT_TRUE(merger.Merge({}, table_a.get(), false /*overlay*/));
338 ASSERT_TRUE(merger.Merge({}, table_b.get(), true /*overlay*/));
Adam Lesinski5c3464c2016-08-24 16:03:48 -0700339
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700340 Styleable* styleable = test::GetValue<Styleable>(&final_table, "com.app.a:styleable/Foo");
341 ASSERT_THAT(styleable, NotNull());
Adam Lesinski5c3464c2016-08-24 16:03:48 -0700342
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700343 std::vector<Reference> expected_refs = {
344 Reference(test::ParseNameOrDie("com.app.a:attr/bar")),
345 Reference(test::ParseNameOrDie("com.app.a:attr/bat")),
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700346 Reference(test::ParseNameOrDie("com.app.a:attr/foo"), ResourceId(0x01010000)),
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700347 };
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700348 EXPECT_THAT(styleable->entries, UnorderedElementsAreArray(expected_refs));
Adam Lesinski5c3464c2016-08-24 16:03:48 -0700349
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700350 Style* style = test::GetValue<Style>(&final_table, "com.app.a:style/Theme");
351 ASSERT_THAT(style, NotNull());
352
353 std::vector<Reference> extracted_refs;
354 for (const auto& entry : style->entries) {
355 extracted_refs.push_back(entry.key);
356 }
357 EXPECT_THAT(extracted_refs, UnorderedElementsAreArray(expected_refs));
358
359 const auto expected = ResourceUtils::MakeBool(true);
360 EXPECT_THAT(style->entries, Contains(Field(&Style::Entry::value, Pointee(ValueEq(*expected)))));
361 EXPECT_THAT(style->parent,
362 Eq(make_value(Reference(test::ParseNameOrDie("com.app.a:style/OverlayParent")))));
Adam Lesinski5c3464c2016-08-24 16:03:48 -0700363}
364
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700365} // namespace aapt