blob: 5150e82b6d93b79a7f604fd422299983a7abc76a [file] [log] [blame]
Adam Lesinski355f2852016-02-13 20:26:45 -08001/*
2 * Copyright (C) 2016 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
17#include "split/TableSplitter.h"
Adam Lesinskifb6312f2016-06-28 14:40:32 -070018#include "test/Test.h"
Adam Lesinski355f2852016-02-13 20:26:45 -080019
20namespace aapt {
21
22TEST(TableSplitterTest, NoSplitPreferredDensity) {
23 std::unique_ptr<ResourceTable> table = test::ResourceTableBuilder()
Adam Lesinski58a20a62016-07-25 17:56:58 -070024 .addFileReference("android:drawable/icon", "res/drawable-mdpi/icon.png",
Adam Lesinski355f2852016-02-13 20:26:45 -080025 test::parseConfigOrDie("mdpi"))
Adam Lesinski58a20a62016-07-25 17:56:58 -070026 .addFileReference("android:drawable/icon", "res/drawable-hdpi/icon.png",
Adam Lesinski355f2852016-02-13 20:26:45 -080027 test::parseConfigOrDie("hdpi"))
Adam Lesinski58a20a62016-07-25 17:56:58 -070028 .addFileReference("android:drawable/icon", "res/drawable-xhdpi/icon.png",
Adam Lesinski355f2852016-02-13 20:26:45 -080029 test::parseConfigOrDie("xhdpi"))
Adam Lesinski58a20a62016-07-25 17:56:58 -070030 .addFileReference("android:drawable/icon", "res/drawable-xxhdpi/icon.png",
Adam Lesinski355f2852016-02-13 20:26:45 -080031 test::parseConfigOrDie("xxhdpi"))
Adam Lesinski58a20a62016-07-25 17:56:58 -070032 .addSimple("android:string/one")
Adam Lesinski355f2852016-02-13 20:26:45 -080033 .build();
34
35 TableSplitterOptions options;
36 options.preferredDensity = ConfigDescription::DENSITY_XHIGH;
37 TableSplitter splitter({}, options);
38 splitter.splitTable(table.get());
39
40 EXPECT_EQ(nullptr, test::getValueForConfig<FileReference>(table.get(),
Adam Lesinski58a20a62016-07-25 17:56:58 -070041 "android:drawable/icon",
Adam Lesinski355f2852016-02-13 20:26:45 -080042 test::parseConfigOrDie("mdpi")));
43 EXPECT_EQ(nullptr, test::getValueForConfig<FileReference>(table.get(),
Adam Lesinski58a20a62016-07-25 17:56:58 -070044 "android:drawable/icon",
Adam Lesinski355f2852016-02-13 20:26:45 -080045 test::parseConfigOrDie("hdpi")));
46 EXPECT_NE(nullptr, test::getValueForConfig<FileReference>(table.get(),
Adam Lesinski58a20a62016-07-25 17:56:58 -070047 "android:drawable/icon",
Adam Lesinski355f2852016-02-13 20:26:45 -080048 test::parseConfigOrDie("xhdpi")));
49 EXPECT_EQ(nullptr, test::getValueForConfig<FileReference>(table.get(),
Adam Lesinski58a20a62016-07-25 17:56:58 -070050 "android:drawable/icon",
Adam Lesinski355f2852016-02-13 20:26:45 -080051 test::parseConfigOrDie("xxhdpi")));
Adam Lesinski58a20a62016-07-25 17:56:58 -070052 EXPECT_NE(nullptr, test::getValue<Id>(table.get(), "android:string/one"));
Adam Lesinski355f2852016-02-13 20:26:45 -080053}
54
Adam Lesinski36c73a52016-08-11 13:39:24 -070055TEST(TableSplitterTest, SplitTableByDensity) {
56 std::unique_ptr<ResourceTable> table = test::ResourceTableBuilder()
57 .addFileReference("android:drawable/foo", "res/drawable-mdpi/foo.png",
58 test::parseConfigOrDie("mdpi"))
59 .addFileReference("android:drawable/foo", "res/drawable-hdpi/foo.png",
60 test::parseConfigOrDie("hdpi"))
61 .addFileReference("android:drawable/foo", "res/drawable-xhdpi/foo.png",
62 test::parseConfigOrDie("xhdpi"))
63 .addFileReference("android:drawable/foo", "res/drawable-xxhdpi/foo.png",
64 test::parseConfigOrDie("xxhdpi"))
65 .build();
66
67 std::vector<SplitConstraints> constraints;
68 constraints.push_back(SplitConstraints{ { test::parseConfigOrDie("mdpi") } });
69 constraints.push_back(SplitConstraints{ { test::parseConfigOrDie("hdpi") } });
70 constraints.push_back(SplitConstraints{ { test::parseConfigOrDie("xhdpi") } });
71
72 TableSplitter splitter(constraints, TableSplitterOptions{});
73 splitter.splitTable(table.get());
74
75 ASSERT_EQ(3u, splitter.getSplits().size());
76
77 ResourceTable* splitOne = splitter.getSplits()[0].get();
78 ResourceTable* splitTwo = splitter.getSplits()[1].get();
79 ResourceTable* splitThree = splitter.getSplits()[2].get();
80
81 // Just xxhdpi should be in the base.
82 EXPECT_EQ(nullptr, test::getValueForConfig<FileReference>(table.get(), "android:drawable/foo",
83 test::parseConfigOrDie("mdpi")));
84 EXPECT_EQ(nullptr, test::getValueForConfig<FileReference>(table.get(), "android:drawable/foo",
85 test::parseConfigOrDie("hdpi")));
86 EXPECT_EQ(nullptr, test::getValueForConfig<FileReference>(table.get(), "android:drawable/foo",
87 test::parseConfigOrDie("xhdpi")));
88 EXPECT_NE(nullptr, test::getValueForConfig<FileReference>(table.get(), "android:drawable/foo",
89 test::parseConfigOrDie("xxhdpi")));
90
91 // Each split should have one and only one drawable.
92 EXPECT_NE(nullptr, test::getValueForConfig<FileReference>(splitOne, "android:drawable/foo",
93 test::parseConfigOrDie("mdpi")));
94 EXPECT_EQ(nullptr, test::getValueForConfig<FileReference>(splitOne, "android:drawable/foo",
95 test::parseConfigOrDie("hdpi")));
96 EXPECT_EQ(nullptr, test::getValueForConfig<FileReference>(splitOne, "android:drawable/foo",
97 test::parseConfigOrDie("xhdpi")));
98 EXPECT_EQ(nullptr, test::getValueForConfig<FileReference>(splitOne, "android:drawable/foo",
99 test::parseConfigOrDie("xxhdpi")));
100
101 EXPECT_EQ(nullptr, test::getValueForConfig<FileReference>(splitTwo, "android:drawable/foo",
102 test::parseConfigOrDie("mdpi")));
103 EXPECT_NE(nullptr, test::getValueForConfig<FileReference>(splitTwo, "android:drawable/foo",
104 test::parseConfigOrDie("hdpi")));
105 EXPECT_EQ(nullptr, test::getValueForConfig<FileReference>(splitTwo, "android:drawable/foo",
106 test::parseConfigOrDie("xhdpi")));
107 EXPECT_EQ(nullptr, test::getValueForConfig<FileReference>(splitTwo, "android:drawable/foo",
108 test::parseConfigOrDie("xxhdpi")));
109
110 EXPECT_EQ(nullptr, test::getValueForConfig<FileReference>(splitThree, "android:drawable/foo",
111 test::parseConfigOrDie("mdpi")));
112 EXPECT_EQ(nullptr, test::getValueForConfig<FileReference>(splitThree, "android:drawable/foo",
113 test::parseConfigOrDie("hdpi")));
114 EXPECT_NE(nullptr, test::getValueForConfig<FileReference>(splitThree, "android:drawable/foo",
115 test::parseConfigOrDie("xhdpi")));
116 EXPECT_EQ(nullptr, test::getValueForConfig<FileReference>(splitThree, "android:drawable/foo",
117 test::parseConfigOrDie("xxhdpi")));
118}
119
Adam Lesinski355f2852016-02-13 20:26:45 -0800120TEST(TableSplitterTest, SplitTableByConfigAndDensity) {
121 ResourceTable table;
122
Adam Lesinski58a20a62016-07-25 17:56:58 -0700123 const ResourceName foo = test::parseNameOrDie("android:string/foo");
Adam Lesinski355f2852016-02-13 20:26:45 -0800124 ASSERT_TRUE(table.addResource(foo, test::parseConfigOrDie("land-hdpi"), {},
125 util::make_unique<Id>(),
126 test::getDiagnostics()));
127 ASSERT_TRUE(table.addResource(foo, test::parseConfigOrDie("land-xhdpi"), {},
128 util::make_unique<Id>(),
129 test::getDiagnostics()));
130 ASSERT_TRUE(table.addResource(foo, test::parseConfigOrDie("land-xxhdpi"), {},
131 util::make_unique<Id>(),
132 test::getDiagnostics()));
133
134 std::vector<SplitConstraints> constraints;
135 constraints.push_back(SplitConstraints{ { test::parseConfigOrDie("land-mdpi") } });
136 constraints.push_back(SplitConstraints{ { test::parseConfigOrDie("land-xhdpi") } });
137
138 TableSplitter splitter(constraints, TableSplitterOptions{});
139 splitter.splitTable(&table);
140
141 ASSERT_EQ(2u, splitter.getSplits().size());
142
143 ResourceTable* splitOne = splitter.getSplits()[0].get();
144 ResourceTable* splitTwo = splitter.getSplits()[1].get();
145
Adam Lesinski36c73a52016-08-11 13:39:24 -0700146 // All but the xxhdpi resource should be gone, since there were closer matches in land-xhdpi.
Adam Lesinski58a20a62016-07-25 17:56:58 -0700147 EXPECT_EQ(nullptr, test::getValueForConfig<Id>(&table, "android:string/foo",
Adam Lesinski355f2852016-02-13 20:26:45 -0800148 test::parseConfigOrDie("land-hdpi")));
Adam Lesinski58a20a62016-07-25 17:56:58 -0700149 EXPECT_EQ(nullptr, test::getValueForConfig<Id>(&table, "android:string/foo",
Adam Lesinski355f2852016-02-13 20:26:45 -0800150 test::parseConfigOrDie("land-xhdpi")));
Adam Lesinski36c73a52016-08-11 13:39:24 -0700151 EXPECT_NE(nullptr, test::getValueForConfig<Id>(&table, "android:string/foo",
Adam Lesinski355f2852016-02-13 20:26:45 -0800152 test::parseConfigOrDie("land-xxhdpi")));
153
Adam Lesinski58a20a62016-07-25 17:56:58 -0700154 EXPECT_NE(nullptr, test::getValueForConfig<Id>(splitOne, "android:string/foo",
Adam Lesinski355f2852016-02-13 20:26:45 -0800155 test::parseConfigOrDie("land-hdpi")));
Adam Lesinski58a20a62016-07-25 17:56:58 -0700156 EXPECT_EQ(nullptr, test::getValueForConfig<Id>(splitOne, "android:string/foo",
Adam Lesinski355f2852016-02-13 20:26:45 -0800157 test::parseConfigOrDie("land-xhdpi")));
Adam Lesinski58a20a62016-07-25 17:56:58 -0700158 EXPECT_EQ(nullptr, test::getValueForConfig<Id>(splitOne, "android:string/foo",
Adam Lesinski355f2852016-02-13 20:26:45 -0800159 test::parseConfigOrDie("land-xxhdpi")));
160
Adam Lesinski58a20a62016-07-25 17:56:58 -0700161 EXPECT_EQ(nullptr, test::getValueForConfig<Id>(splitTwo, "android:string/foo",
Adam Lesinski355f2852016-02-13 20:26:45 -0800162 test::parseConfigOrDie("land-hdpi")));
Adam Lesinski58a20a62016-07-25 17:56:58 -0700163 EXPECT_NE(nullptr, test::getValueForConfig<Id>(splitTwo, "android:string/foo",
Adam Lesinski355f2852016-02-13 20:26:45 -0800164 test::parseConfigOrDie("land-xhdpi")));
Adam Lesinski58a20a62016-07-25 17:56:58 -0700165 EXPECT_EQ(nullptr, test::getValueForConfig<Id>(splitTwo, "android:string/foo",
Adam Lesinski355f2852016-02-13 20:26:45 -0800166 test::parseConfigOrDie("land-xxhdpi")));
167}
168
169} // namespace aapt