blob: 6aab8ded24a53eda115587a9b544d4be1a51774f [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)
185 .SetSymbolState("bool/foo", ResourceId(0x7f, 0x01, 0x0001),
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700186 SymbolState::kPublic)
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700187 .Build();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700188 std::unique_ptr<ResourceTable> overlay =
189 test::ResourceTableBuilder()
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700190 .SetPackageId("", 0x7f)
191 .SetSymbolState("bool/foo", ResourceId(0x7f, 0x01, 0x0001),
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700192 SymbolState::kPublic)
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700193 .Build();
Alexandria Cornwall6a1f8db2016-08-11 13:49:44 -0700194
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700195 ResourceTable final_table;
196 TableMergerOptions options;
197 options.auto_add_overlay = false;
198 TableMerger merger(context_.get(), &final_table, options);
Alexandria Cornwall6a1f8db2016-08-11 13:49:44 -0700199
Adam Lesinski00451162017-10-03 07:44:08 -0700200 ASSERT_TRUE(merger.Merge({}, base.get(), false /*overlay*/));
201 ASSERT_TRUE(merger.Merge({}, overlay.get(), true /*overlay*/));
Alexandria Cornwall6a1f8db2016-08-11 13:49:44 -0700202}
203
204TEST_F(TableMergerTest, FailToOverrideConflictingTypeIdsWithOverlay) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700205 std::unique_ptr<ResourceTable> base =
206 test::ResourceTableBuilder()
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700207 .SetPackageId("", 0x7f)
208 .SetSymbolState("bool/foo", ResourceId(0x7f, 0x01, 0x0001),
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700209 SymbolState::kPublic)
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700210 .Build();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700211 std::unique_ptr<ResourceTable> overlay =
212 test::ResourceTableBuilder()
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700213 .SetPackageId("", 0x7f)
214 .SetSymbolState("bool/foo", ResourceId(0x7f, 0x02, 0x0001),
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700215 SymbolState::kPublic)
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700216 .Build();
Alexandria Cornwall6a1f8db2016-08-11 13:49:44 -0700217
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700218 ResourceTable final_table;
219 TableMergerOptions options;
220 options.auto_add_overlay = false;
221 TableMerger merger(context_.get(), &final_table, options);
Alexandria Cornwall6a1f8db2016-08-11 13:49:44 -0700222
Adam Lesinski00451162017-10-03 07:44:08 -0700223 ASSERT_TRUE(merger.Merge({}, base.get(), false /*overlay*/));
224 ASSERT_FALSE(merger.Merge({}, overlay.get(), true /*overlay*/));
Alexandria Cornwall6a1f8db2016-08-11 13:49:44 -0700225}
226
227TEST_F(TableMergerTest, FailToOverrideConflictingEntryIdsWithOverlay) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700228 std::unique_ptr<ResourceTable> base =
229 test::ResourceTableBuilder()
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700230 .SetPackageId("", 0x7f)
231 .SetSymbolState("bool/foo", ResourceId(0x7f, 0x01, 0x0001),
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700232 SymbolState::kPublic)
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700233 .Build();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700234 std::unique_ptr<ResourceTable> overlay =
235 test::ResourceTableBuilder()
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700236 .SetPackageId("", 0x7f)
237 .SetSymbolState("bool/foo", ResourceId(0x7f, 0x01, 0x0002),
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700238 SymbolState::kPublic)
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700239 .Build();
Alexandria Cornwall6a1f8db2016-08-11 13:49:44 -0700240
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700241 ResourceTable final_table;
242 TableMergerOptions options;
243 options.auto_add_overlay = false;
244 TableMerger merger(context_.get(), &final_table, options);
Alexandria Cornwall6a1f8db2016-08-11 13:49:44 -0700245
Adam Lesinski00451162017-10-03 07:44:08 -0700246 ASSERT_TRUE(merger.Merge({}, base.get(), false /*overlay*/));
247 ASSERT_FALSE(merger.Merge({}, overlay.get(), true /*overlay*/));
Alexandria Cornwall6a1f8db2016-08-11 13:49:44 -0700248}
249
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800250TEST_F(TableMergerTest, MergeAddResourceFromOverlay) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700251 std::unique_ptr<ResourceTable> table_a =
Adam Lesinski4488f1c2017-05-26 17:33:38 -0700252 test::ResourceTableBuilder().SetPackageId("", 0x7f).Build();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700253 std::unique_ptr<ResourceTable> table_b =
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700254 test::ResourceTableBuilder()
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700255 .SetPackageId("", 0x7f)
Adam Lesinski4488f1c2017-05-26 17:33:38 -0700256 .SetSymbolState("bool/foo", {}, SymbolState::kUndefined, true /*allow new overlay*/)
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700257 .AddValue("bool/foo", ResourceUtils::TryParseBool("true"))
258 .Build();
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800259
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700260 ResourceTable final_table;
Adam Lesinski4488f1c2017-05-26 17:33:38 -0700261 TableMergerOptions options;
262 options.auto_add_overlay = false;
263 TableMerger merger(context_.get(), &final_table, options);
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800264
Adam Lesinski00451162017-10-03 07:44:08 -0700265 ASSERT_TRUE(merger.Merge({}, table_a.get(), false /*overlay*/));
266 ASSERT_TRUE(merger.Merge({}, table_b.get(), false /*overlay*/));
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800267}
268
269TEST_F(TableMergerTest, MergeAddResourceFromOverlayWithAutoAddOverlay) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700270 std::unique_ptr<ResourceTable> table_a =
271 test::ResourceTableBuilder().SetPackageId("", 0x7f).Build();
272 std::unique_ptr<ResourceTable> table_b =
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700273 test::ResourceTableBuilder()
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700274 .SetPackageId("", 0x7f)
275 .AddValue("bool/foo", ResourceUtils::TryParseBool("true"))
276 .Build();
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800277
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700278 ResourceTable final_table;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700279 TableMergerOptions options;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700280 options.auto_add_overlay = true;
281 TableMerger merger(context_.get(), &final_table, options);
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800282
Adam Lesinski00451162017-10-03 07:44:08 -0700283 ASSERT_TRUE(merger.Merge({}, table_a.get(), false /*overlay*/));
284 ASSERT_TRUE(merger.Merge({}, table_b.get(), false /*overlay*/));
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800285}
286
287TEST_F(TableMergerTest, FailToMergeNewResourceWithoutAutoAddOverlay) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700288 std::unique_ptr<ResourceTable> table_a =
289 test::ResourceTableBuilder().SetPackageId("", 0x7f).Build();
290 std::unique_ptr<ResourceTable> table_b =
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700291 test::ResourceTableBuilder()
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700292 .SetPackageId("", 0x7f)
293 .AddValue("bool/foo", ResourceUtils::TryParseBool("true"))
294 .Build();
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800295
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700296 ResourceTable final_table;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700297 TableMergerOptions options;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700298 options.auto_add_overlay = false;
299 TableMerger merger(context_.get(), &final_table, options);
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800300
Adam Lesinski00451162017-10-03 07:44:08 -0700301 ASSERT_TRUE(merger.Merge({}, table_a.get(), false /*overlay*/));
302 ASSERT_FALSE(merger.Merge({}, table_b.get(), true /*overlay*/));
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700303}
304
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700305TEST_F(TableMergerTest, OverlaidStyleablesAndStylesShouldBeMerged) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700306 std::unique_ptr<ResourceTable> table_a =
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700307 test::ResourceTableBuilder()
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700308 .SetPackageId("com.app.a", 0x7f)
309 .AddValue("com.app.a:styleable/Foo",
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700310 test::StyleableBuilder()
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700311 .AddItem("com.app.a:attr/bar")
312 .AddItem("com.app.a:attr/foo", ResourceId(0x01010000))
313 .Build())
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700314 .AddValue("com.app.a:style/Theme",
315 test::StyleBuilder()
316 .SetParent("com.app.a:style/Parent")
317 .AddItem("com.app.a:attr/bar", util::make_unique<Id>())
318 .AddItem("com.app.a:attr/foo", ResourceUtils::MakeBool(false))
319 .Build())
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700320 .Build();
Adam Lesinski5c3464c2016-08-24 16:03:48 -0700321
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700322 std::unique_ptr<ResourceTable> table_b =
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700323 test::ResourceTableBuilder()
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700324 .SetPackageId("com.app.a", 0x7f)
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700325 .AddValue("com.app.a:styleable/Foo", test::StyleableBuilder()
326 .AddItem("com.app.a:attr/bat")
327 .AddItem("com.app.a:attr/foo")
328 .Build())
329 .AddValue("com.app.a:style/Theme",
330 test::StyleBuilder()
331 .SetParent("com.app.a:style/OverlayParent")
332 .AddItem("com.app.a:attr/bat", util::make_unique<Id>())
333 .AddItem("com.app.a:attr/foo", ResourceId(0x01010000),
334 ResourceUtils::MakeBool(true))
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700335 .Build())
336 .Build();
Adam Lesinski5c3464c2016-08-24 16:03:48 -0700337
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700338 ResourceTable final_table;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700339 TableMergerOptions options;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700340 options.auto_add_overlay = true;
341 TableMerger merger(context_.get(), &final_table, options);
Adam Lesinski5c3464c2016-08-24 16:03:48 -0700342
Adam Lesinski00451162017-10-03 07:44:08 -0700343 ASSERT_TRUE(merger.Merge({}, table_a.get(), false /*overlay*/));
344 ASSERT_TRUE(merger.Merge({}, table_b.get(), true /*overlay*/));
Adam Lesinski5c3464c2016-08-24 16:03:48 -0700345
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700346 Styleable* styleable = test::GetValue<Styleable>(&final_table, "com.app.a:styleable/Foo");
347 ASSERT_THAT(styleable, NotNull());
Adam Lesinski5c3464c2016-08-24 16:03:48 -0700348
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700349 std::vector<Reference> expected_refs = {
350 Reference(test::ParseNameOrDie("com.app.a:attr/bar")),
351 Reference(test::ParseNameOrDie("com.app.a:attr/bat")),
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700352 Reference(test::ParseNameOrDie("com.app.a:attr/foo"), ResourceId(0x01010000)),
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700353 };
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700354 EXPECT_THAT(styleable->entries, UnorderedElementsAreArray(expected_refs));
Adam Lesinski5c3464c2016-08-24 16:03:48 -0700355
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700356 Style* style = test::GetValue<Style>(&final_table, "com.app.a:style/Theme");
357 ASSERT_THAT(style, NotNull());
358
359 std::vector<Reference> extracted_refs;
360 for (const auto& entry : style->entries) {
361 extracted_refs.push_back(entry.key);
362 }
363 EXPECT_THAT(extracted_refs, UnorderedElementsAreArray(expected_refs));
364
365 const auto expected = ResourceUtils::MakeBool(true);
366 EXPECT_THAT(style->entries, Contains(Field(&Style::Entry::value, Pointee(ValueEq(*expected)))));
367 EXPECT_THAT(style->parent,
368 Eq(make_value(Reference(test::ParseNameOrDie("com.app.a:style/OverlayParent")))));
Adam Lesinski5c3464c2016-08-24 16:03:48 -0700369}
370
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700371} // namespace aapt