blob: 3b791b5cf92bc4c2984ceb2ce24791dc36943b5a [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;
25using ::testing::NotNull;
26using ::testing::UnorderedElementsAreArray;
27using ::testing::Pointee;
28using ::testing::Field;
29using ::testing::Eq;
30
Adam Lesinski1ab598f2015-08-14 14:26:04 -070031namespace aapt {
32
33struct TableMergerTest : public ::testing::Test {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070034 std::unique_ptr<IAaptContext> context_;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070035
Adam Lesinskicacb28f2016-10-19 12:18:14 -070036 void SetUp() override {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070037 context_ =
Adam Lesinskicacb28f2016-10-19 12:18:14 -070038 test::ContextBuilder()
39 // We are compiling this package.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070040 .SetCompilationPackage("com.app.a")
Adam Lesinski1ab598f2015-08-14 14:26:04 -070041
Adam Lesinskicacb28f2016-10-19 12:18:14 -070042 // Merge all packages that have this package ID.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070043 .SetPackageId(0x7f)
Adam Lesinski1ab598f2015-08-14 14:26:04 -070044
Adam Lesinskicacb28f2016-10-19 12:18:14 -070045 // Mangle all packages that do not have this package name.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070046 .SetNameManglerPolicy(NameManglerPolicy{"com.app.a", {"com.app.b"}})
Adam Lesinski1ab598f2015-08-14 14:26:04 -070047
Adam Lesinskice5e56e2016-10-21 17:56:45 -070048 .Build();
Adam Lesinskicacb28f2016-10-19 12:18:14 -070049 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -070050};
51
52TEST_F(TableMergerTest, SimpleMerge) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070053 std::unique_ptr<ResourceTable> table_a =
Adam Lesinskicacb28f2016-10-19 12:18:14 -070054 test::ResourceTableBuilder()
Adam Lesinskice5e56e2016-10-21 17:56:45 -070055 .SetPackageId("com.app.a", 0x7f)
56 .AddReference("com.app.a:id/foo", "com.app.a:id/bar")
57 .AddReference("com.app.a:id/bar", "com.app.b:id/foo")
58 .AddValue(
Adam Lesinskicacb28f2016-10-19 12:18:14 -070059 "com.app.a:styleable/view",
Adam Lesinskice5e56e2016-10-21 17:56:45 -070060 test::StyleableBuilder().AddItem("com.app.b:id/foo").Build())
61 .Build();
Adam Lesinski1ab598f2015-08-14 14:26:04 -070062
Adam Lesinskice5e56e2016-10-21 17:56:45 -070063 std::unique_ptr<ResourceTable> table_b = test::ResourceTableBuilder()
64 .SetPackageId("com.app.b", 0x7f)
65 .AddSimple("com.app.b:id/foo")
66 .Build();
Adam Lesinski1ab598f2015-08-14 14:26:04 -070067
Adam Lesinskice5e56e2016-10-21 17:56:45 -070068 ResourceTable final_table;
69 TableMerger merger(context_.get(), &final_table, TableMergerOptions{});
Adam Lesinskicacb28f2016-10-19 12:18:14 -070070 io::FileCollection collection;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070071
Adam Lesinskice5e56e2016-10-21 17:56:45 -070072 ASSERT_TRUE(merger.Merge({}, table_a.get()));
Adam Lesinski5924d8c2017-05-30 15:15:58 -070073 ASSERT_TRUE(merger.MergeAndMangle({}, "com.app.b", table_b.get(), &collection));
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 Lesinskice5e56e2016-10-21 17:56:45 -0700101 ASSERT_TRUE(merger.MergeFile(file_desc, &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 Lesinskice5e56e2016-10-21 17:56:45 -0700120 ASSERT_TRUE(merger.MergeFile(file_desc, &file_a));
121 ASSERT_TRUE(merger.MergeFileOverlay(file_desc, &file_b));
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800122}
123
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700124TEST_F(TableMergerTest, MergeFileReferences) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700125 std::unique_ptr<ResourceTable> table_a =
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700126 test::ResourceTableBuilder()
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700127 .SetPackageId("com.app.a", 0x7f)
128 .AddFileReference("com.app.a:xml/file", "res/xml/file.xml")
129 .Build();
130 std::unique_ptr<ResourceTable> table_b =
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700131 test::ResourceTableBuilder()
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700132 .SetPackageId("com.app.b", 0x7f)
133 .AddFileReference("com.app.b:xml/file", "res/xml/file.xml")
134 .Build();
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700135
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700136 ResourceTable final_table;
137 TableMerger merger(context_.get(), &final_table, TableMergerOptions{});
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700138 io::FileCollection collection;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700139 collection.InsertFile("res/xml/file.xml");
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700140
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700141 ASSERT_TRUE(merger.Merge({}, table_a.get()));
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700142 ASSERT_TRUE(merger.MergeAndMangle({}, "com.app.b", table_b.get(), &collection));
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700143
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700144 FileReference* f = test::GetValue<FileReference>(&final_table, "com.app.a:xml/file");
145 ASSERT_THAT(f, NotNull());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700146 EXPECT_EQ(std::string("res/xml/file.xml"), *f->path);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700147
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700148 f = test::GetValue<FileReference>(&final_table, "com.app.a:xml/com.app.b$file");
149 ASSERT_THAT(f, NotNull());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700150 EXPECT_EQ(std::string("res/xml/com.app.b$file.xml"), *f->path);
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800151}
152
153TEST_F(TableMergerTest, OverrideResourceWithOverlay) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700154 std::unique_ptr<ResourceTable> base =
155 test::ResourceTableBuilder()
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700156 .SetPackageId("", 0x00)
157 .AddValue("bool/foo", ResourceUtils::TryParseBool("true"))
158 .Build();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700159 std::unique_ptr<ResourceTable> overlay =
160 test::ResourceTableBuilder()
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700161 .SetPackageId("", 0x00)
162 .AddValue("bool/foo", ResourceUtils::TryParseBool("false"))
163 .Build();
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800164
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700165 ResourceTable final_table;
166 TableMergerOptions options;
167 options.auto_add_overlay = false;
168 TableMerger merger(context_.get(), &final_table, options);
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800169
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700170 ASSERT_TRUE(merger.Merge({}, base.get()));
171 ASSERT_TRUE(merger.MergeOverlay({}, overlay.get()));
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800172
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700173 BinaryPrimitive* foo = test::GetValue<BinaryPrimitive>(&final_table, "com.app.a:bool/foo");
174 ASSERT_THAT(foo,
175 Pointee(Field(&BinaryPrimitive::value, Field(&android::Res_value::data, Eq(0u)))));
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800176}
177
Alexandria Cornwall6a1f8db2016-08-11 13:49:44 -0700178TEST_F(TableMergerTest, OverrideSameResourceIdsWithOverlay) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700179 std::unique_ptr<ResourceTable> base =
180 test::ResourceTableBuilder()
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700181 .SetPackageId("", 0x7f)
182 .SetSymbolState("bool/foo", ResourceId(0x7f, 0x01, 0x0001),
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700183 SymbolState::kPublic)
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700184 .Build();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700185 std::unique_ptr<ResourceTable> overlay =
186 test::ResourceTableBuilder()
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700187 .SetPackageId("", 0x7f)
188 .SetSymbolState("bool/foo", ResourceId(0x7f, 0x01, 0x0001),
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700189 SymbolState::kPublic)
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700190 .Build();
Alexandria Cornwall6a1f8db2016-08-11 13:49:44 -0700191
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700192 ResourceTable final_table;
193 TableMergerOptions options;
194 options.auto_add_overlay = false;
195 TableMerger merger(context_.get(), &final_table, options);
Alexandria Cornwall6a1f8db2016-08-11 13:49:44 -0700196
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700197 ASSERT_TRUE(merger.Merge({}, base.get()));
198 ASSERT_TRUE(merger.MergeOverlay({}, overlay.get()));
Alexandria Cornwall6a1f8db2016-08-11 13:49:44 -0700199}
200
201TEST_F(TableMergerTest, FailToOverrideConflictingTypeIdsWithOverlay) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700202 std::unique_ptr<ResourceTable> base =
203 test::ResourceTableBuilder()
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700204 .SetPackageId("", 0x7f)
205 .SetSymbolState("bool/foo", ResourceId(0x7f, 0x01, 0x0001),
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700206 SymbolState::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)
211 .SetSymbolState("bool/foo", ResourceId(0x7f, 0x02, 0x0001),
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700212 SymbolState::kPublic)
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700213 .Build();
Alexandria Cornwall6a1f8db2016-08-11 13:49:44 -0700214
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700215 ResourceTable final_table;
216 TableMergerOptions options;
217 options.auto_add_overlay = false;
218 TableMerger merger(context_.get(), &final_table, options);
Alexandria Cornwall6a1f8db2016-08-11 13:49:44 -0700219
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700220 ASSERT_TRUE(merger.Merge({}, base.get()));
221 ASSERT_FALSE(merger.MergeOverlay({}, overlay.get()));
Alexandria Cornwall6a1f8db2016-08-11 13:49:44 -0700222}
223
224TEST_F(TableMergerTest, FailToOverrideConflictingEntryIdsWithOverlay) {
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)
228 .SetSymbolState("bool/foo", ResourceId(0x7f, 0x01, 0x0001),
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700229 SymbolState::kPublic)
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700230 .Build();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700231 std::unique_ptr<ResourceTable> overlay =
232 test::ResourceTableBuilder()
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700233 .SetPackageId("", 0x7f)
234 .SetSymbolState("bool/foo", ResourceId(0x7f, 0x01, 0x0002),
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700235 SymbolState::kPublic)
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700236 .Build();
Alexandria Cornwall6a1f8db2016-08-11 13:49:44 -0700237
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700238 ResourceTable final_table;
239 TableMergerOptions options;
240 options.auto_add_overlay = false;
241 TableMerger merger(context_.get(), &final_table, options);
Alexandria Cornwall6a1f8db2016-08-11 13:49:44 -0700242
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700243 ASSERT_TRUE(merger.Merge({}, base.get()));
244 ASSERT_FALSE(merger.MergeOverlay({}, overlay.get()));
Alexandria Cornwall6a1f8db2016-08-11 13:49:44 -0700245}
246
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800247TEST_F(TableMergerTest, MergeAddResourceFromOverlay) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700248 std::unique_ptr<ResourceTable> table_a =
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700249 test::ResourceTableBuilder()
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700250 .SetPackageId("", 0x7f)
251 .SetSymbolState("bool/foo", {}, SymbolState::kUndefined)
252 .Build();
253 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)
256 .AddValue("bool/foo", ResourceUtils::TryParseBool("true"))
257 .Build();
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800258
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700259 ResourceTable final_table;
260 TableMerger merger(context_.get(), &final_table, TableMergerOptions{});
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800261
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700262 ASSERT_TRUE(merger.Merge({}, table_a.get()));
263 ASSERT_TRUE(merger.MergeOverlay({}, table_b.get()));
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800264}
265
266TEST_F(TableMergerTest, MergeAddResourceFromOverlayWithAutoAddOverlay) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700267 std::unique_ptr<ResourceTable> table_a =
268 test::ResourceTableBuilder().SetPackageId("", 0x7f).Build();
269 std::unique_ptr<ResourceTable> table_b =
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700270 test::ResourceTableBuilder()
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700271 .SetPackageId("", 0x7f)
272 .AddValue("bool/foo", ResourceUtils::TryParseBool("true"))
273 .Build();
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800274
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700275 ResourceTable final_table;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700276 TableMergerOptions options;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700277 options.auto_add_overlay = true;
278 TableMerger merger(context_.get(), &final_table, options);
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800279
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700280 ASSERT_TRUE(merger.Merge({}, table_a.get()));
281 ASSERT_TRUE(merger.MergeOverlay({}, table_b.get()));
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800282}
283
284TEST_F(TableMergerTest, FailToMergeNewResourceWithoutAutoAddOverlay) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700285 std::unique_ptr<ResourceTable> table_a =
286 test::ResourceTableBuilder().SetPackageId("", 0x7f).Build();
287 std::unique_ptr<ResourceTable> table_b =
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700288 test::ResourceTableBuilder()
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700289 .SetPackageId("", 0x7f)
290 .AddValue("bool/foo", ResourceUtils::TryParseBool("true"))
291 .Build();
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800292
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700293 ResourceTable final_table;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700294 TableMergerOptions options;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700295 options.auto_add_overlay = false;
296 TableMerger merger(context_.get(), &final_table, options);
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800297
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700298 ASSERT_TRUE(merger.Merge({}, table_a.get()));
299 ASSERT_FALSE(merger.MergeOverlay({}, table_b.get()));
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700300}
301
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700302TEST_F(TableMergerTest, OverlaidStyleablesAndStylesShouldBeMerged) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700303 std::unique_ptr<ResourceTable> table_a =
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700304 test::ResourceTableBuilder()
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700305 .SetPackageId("com.app.a", 0x7f)
306 .AddValue("com.app.a:styleable/Foo",
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700307 test::StyleableBuilder()
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700308 .AddItem("com.app.a:attr/bar")
309 .AddItem("com.app.a:attr/foo", ResourceId(0x01010000))
310 .Build())
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700311 .AddValue("com.app.a:style/Theme",
312 test::StyleBuilder()
313 .SetParent("com.app.a:style/Parent")
314 .AddItem("com.app.a:attr/bar", util::make_unique<Id>())
315 .AddItem("com.app.a:attr/foo", ResourceUtils::MakeBool(false))
316 .Build())
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700317 .Build();
Adam Lesinski5c3464c2016-08-24 16:03:48 -0700318
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700319 std::unique_ptr<ResourceTable> table_b =
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700320 test::ResourceTableBuilder()
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700321 .SetPackageId("com.app.a", 0x7f)
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700322 .AddValue("com.app.a:styleable/Foo", test::StyleableBuilder()
323 .AddItem("com.app.a:attr/bat")
324 .AddItem("com.app.a:attr/foo")
325 .Build())
326 .AddValue("com.app.a:style/Theme",
327 test::StyleBuilder()
328 .SetParent("com.app.a:style/OverlayParent")
329 .AddItem("com.app.a:attr/bat", util::make_unique<Id>())
330 .AddItem("com.app.a:attr/foo", ResourceId(0x01010000),
331 ResourceUtils::MakeBool(true))
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700332 .Build())
333 .Build();
Adam Lesinski5c3464c2016-08-24 16:03:48 -0700334
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700335 ResourceTable final_table;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700336 TableMergerOptions options;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700337 options.auto_add_overlay = true;
338 TableMerger merger(context_.get(), &final_table, options);
Adam Lesinski5c3464c2016-08-24 16:03:48 -0700339
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700340 ASSERT_TRUE(merger.Merge({}, table_a.get()));
341 ASSERT_TRUE(merger.MergeOverlay({}, table_b.get()));
Adam Lesinski5c3464c2016-08-24 16:03:48 -0700342
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700343 Styleable* styleable = test::GetValue<Styleable>(&final_table, "com.app.a:styleable/Foo");
344 ASSERT_THAT(styleable, NotNull());
Adam Lesinski5c3464c2016-08-24 16:03:48 -0700345
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700346 std::vector<Reference> expected_refs = {
347 Reference(test::ParseNameOrDie("com.app.a:attr/bar")),
348 Reference(test::ParseNameOrDie("com.app.a:attr/bat")),
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700349 Reference(test::ParseNameOrDie("com.app.a:attr/foo"), ResourceId(0x01010000)),
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700350 };
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700351 EXPECT_THAT(styleable->entries, UnorderedElementsAreArray(expected_refs));
Adam Lesinski5c3464c2016-08-24 16:03:48 -0700352
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700353 Style* style = test::GetValue<Style>(&final_table, "com.app.a:style/Theme");
354 ASSERT_THAT(style, NotNull());
355
356 std::vector<Reference> extracted_refs;
357 for (const auto& entry : style->entries) {
358 extracted_refs.push_back(entry.key);
359 }
360 EXPECT_THAT(extracted_refs, UnorderedElementsAreArray(expected_refs));
361
362 const auto expected = ResourceUtils::MakeBool(true);
363 EXPECT_THAT(style->entries, Contains(Field(&Style::Entry::value, Pointee(ValueEq(*expected)))));
364 EXPECT_THAT(style->parent,
365 Eq(make_value(Reference(test::ParseNameOrDie("com.app.a:style/OverlayParent")))));
Adam Lesinski5c3464c2016-08-24 16:03:48 -0700366}
367
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700368} // namespace aapt