blob: 088dac3744583808d6d56b0628c497442ae8a410 [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 Lesinskice5e56e2016-10-21 17:56:45 -070018
Adam Lesinskifb6312f2016-06-28 14:40:32 -070019#include "test/Test.h"
Adam Lesinski355f2852016-02-13 20:26:45 -080020
21namespace aapt {
22
23TEST(TableSplitterTest, NoSplitPreferredDensity) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070024 std::unique_ptr<ResourceTable> table =
25 test::ResourceTableBuilder()
26 .AddFileReference("android:drawable/icon",
27 "res/drawable-mdpi/icon.png",
28 test::ParseConfigOrDie("mdpi"))
29 .AddFileReference("android:drawable/icon",
30 "res/drawable-hdpi/icon.png",
31 test::ParseConfigOrDie("hdpi"))
32 .AddFileReference("android:drawable/icon",
33 "res/drawable-xhdpi/icon.png",
34 test::ParseConfigOrDie("xhdpi"))
35 .AddFileReference("android:drawable/icon",
36 "res/drawable-xxhdpi/icon.png",
37 test::ParseConfigOrDie("xxhdpi"))
38 .AddSimple("android:string/one")
39 .Build();
Adam Lesinski355f2852016-02-13 20:26:45 -080040
Adam Lesinskice5e56e2016-10-21 17:56:45 -070041 TableSplitterOptions options;
42 options.preferred_density = ConfigDescription::DENSITY_XHIGH;
43 TableSplitter splitter({}, options);
44 splitter.SplitTable(table.get());
Adam Lesinski355f2852016-02-13 20:26:45 -080045
Adam Lesinskice5e56e2016-10-21 17:56:45 -070046 EXPECT_EQ(nullptr, test::GetValueForConfig<FileReference>(
47 table.get(), "android:drawable/icon",
48 test::ParseConfigOrDie("mdpi")));
49 EXPECT_EQ(nullptr, test::GetValueForConfig<FileReference>(
50 table.get(), "android:drawable/icon",
51 test::ParseConfigOrDie("hdpi")));
52 EXPECT_NE(nullptr, test::GetValueForConfig<FileReference>(
53 table.get(), "android:drawable/icon",
54 test::ParseConfigOrDie("xhdpi")));
55 EXPECT_EQ(nullptr, test::GetValueForConfig<FileReference>(
56 table.get(), "android:drawable/icon",
57 test::ParseConfigOrDie("xxhdpi")));
58 EXPECT_NE(nullptr, test::GetValue<Id>(table.get(), "android:string/one"));
Adam Lesinski355f2852016-02-13 20:26:45 -080059}
60
Adam Lesinski36c73a52016-08-11 13:39:24 -070061TEST(TableSplitterTest, SplitTableByDensity) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070062 std::unique_ptr<ResourceTable> table =
63 test::ResourceTableBuilder()
64 .AddFileReference("android:drawable/foo", "res/drawable-mdpi/foo.png",
65 test::ParseConfigOrDie("mdpi"))
66 .AddFileReference("android:drawable/foo", "res/drawable-hdpi/foo.png",
67 test::ParseConfigOrDie("hdpi"))
68 .AddFileReference("android:drawable/foo",
69 "res/drawable-xhdpi/foo.png",
70 test::ParseConfigOrDie("xhdpi"))
71 .AddFileReference("android:drawable/foo",
72 "res/drawable-xxhdpi/foo.png",
73 test::ParseConfigOrDie("xxhdpi"))
74 .Build();
Adam Lesinski36c73a52016-08-11 13:39:24 -070075
Adam Lesinskice5e56e2016-10-21 17:56:45 -070076 std::vector<SplitConstraints> constraints;
77 constraints.push_back(SplitConstraints{{test::ParseConfigOrDie("mdpi")}});
78 constraints.push_back(SplitConstraints{{test::ParseConfigOrDie("hdpi")}});
79 constraints.push_back(SplitConstraints{{test::ParseConfigOrDie("xhdpi")}});
Adam Lesinski36c73a52016-08-11 13:39:24 -070080
Adam Lesinskice5e56e2016-10-21 17:56:45 -070081 TableSplitter splitter(constraints, TableSplitterOptions{});
82 splitter.SplitTable(table.get());
Adam Lesinski36c73a52016-08-11 13:39:24 -070083
Adam Lesinskice5e56e2016-10-21 17:56:45 -070084 ASSERT_EQ(3u, splitter.splits().size());
Adam Lesinski36c73a52016-08-11 13:39:24 -070085
Adam Lesinskice5e56e2016-10-21 17:56:45 -070086 ResourceTable* split_one = splitter.splits()[0].get();
87 ResourceTable* split_two = splitter.splits()[1].get();
88 ResourceTable* split_three = splitter.splits()[2].get();
Adam Lesinski36c73a52016-08-11 13:39:24 -070089
Adam Lesinskice5e56e2016-10-21 17:56:45 -070090 // Just xxhdpi should be in the base.
91 EXPECT_EQ(nullptr, test::GetValueForConfig<FileReference>(
92 table.get(), "android:drawable/foo",
93 test::ParseConfigOrDie("mdpi")));
94 EXPECT_EQ(nullptr, test::GetValueForConfig<FileReference>(
95 table.get(), "android:drawable/foo",
96 test::ParseConfigOrDie("hdpi")));
97 EXPECT_EQ(nullptr, test::GetValueForConfig<FileReference>(
98 table.get(), "android:drawable/foo",
99 test::ParseConfigOrDie("xhdpi")));
100 EXPECT_NE(nullptr, test::GetValueForConfig<FileReference>(
101 table.get(), "android:drawable/foo",
102 test::ParseConfigOrDie("xxhdpi")));
Adam Lesinski36c73a52016-08-11 13:39:24 -0700103
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700104 // Each split should have one and only one drawable.
105 EXPECT_NE(nullptr, test::GetValueForConfig<FileReference>(
106 split_one, "android:drawable/foo",
107 test::ParseConfigOrDie("mdpi")));
108 EXPECT_EQ(nullptr, test::GetValueForConfig<FileReference>(
109 split_one, "android:drawable/foo",
110 test::ParseConfigOrDie("hdpi")));
111 EXPECT_EQ(nullptr, test::GetValueForConfig<FileReference>(
112 split_one, "android:drawable/foo",
113 test::ParseConfigOrDie("xhdpi")));
114 EXPECT_EQ(nullptr, test::GetValueForConfig<FileReference>(
115 split_one, "android:drawable/foo",
116 test::ParseConfigOrDie("xxhdpi")));
Adam Lesinski36c73a52016-08-11 13:39:24 -0700117
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700118 EXPECT_EQ(nullptr, test::GetValueForConfig<FileReference>(
119 split_two, "android:drawable/foo",
120 test::ParseConfigOrDie("mdpi")));
121 EXPECT_NE(nullptr, test::GetValueForConfig<FileReference>(
122 split_two, "android:drawable/foo",
123 test::ParseConfigOrDie("hdpi")));
124 EXPECT_EQ(nullptr, test::GetValueForConfig<FileReference>(
125 split_two, "android:drawable/foo",
126 test::ParseConfigOrDie("xhdpi")));
127 EXPECT_EQ(nullptr, test::GetValueForConfig<FileReference>(
128 split_two, "android:drawable/foo",
129 test::ParseConfigOrDie("xxhdpi")));
Adam Lesinski36c73a52016-08-11 13:39:24 -0700130
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700131 EXPECT_EQ(nullptr, test::GetValueForConfig<FileReference>(
132 split_three, "android:drawable/foo",
133 test::ParseConfigOrDie("mdpi")));
134 EXPECT_EQ(nullptr, test::GetValueForConfig<FileReference>(
135 split_three, "android:drawable/foo",
136 test::ParseConfigOrDie("hdpi")));
137 EXPECT_NE(nullptr, test::GetValueForConfig<FileReference>(
138 split_three, "android:drawable/foo",
139 test::ParseConfigOrDie("xhdpi")));
140 EXPECT_EQ(nullptr, test::GetValueForConfig<FileReference>(
141 split_three, "android:drawable/foo",
142 test::ParseConfigOrDie("xxhdpi")));
Adam Lesinski36c73a52016-08-11 13:39:24 -0700143}
144
Adam Lesinski355f2852016-02-13 20:26:45 -0800145TEST(TableSplitterTest, SplitTableByConfigAndDensity) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700146 ResourceTable table;
Adam Lesinski355f2852016-02-13 20:26:45 -0800147
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700148 const ResourceName foo = test::ParseNameOrDie("android:string/foo");
149 ASSERT_TRUE(table.AddResource(foo, test::ParseConfigOrDie("land-hdpi"), {},
150 util::make_unique<Id>(),
151 test::GetDiagnostics()));
152 ASSERT_TRUE(table.AddResource(foo, test::ParseConfigOrDie("land-xhdpi"), {},
153 util::make_unique<Id>(),
154 test::GetDiagnostics()));
155 ASSERT_TRUE(table.AddResource(foo, test::ParseConfigOrDie("land-xxhdpi"), {},
156 util::make_unique<Id>(),
157 test::GetDiagnostics()));
Adam Lesinski355f2852016-02-13 20:26:45 -0800158
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700159 std::vector<SplitConstraints> constraints;
160 constraints.push_back(
161 SplitConstraints{{test::ParseConfigOrDie("land-mdpi")}});
162 constraints.push_back(
163 SplitConstraints{{test::ParseConfigOrDie("land-xhdpi")}});
Adam Lesinski355f2852016-02-13 20:26:45 -0800164
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700165 TableSplitter splitter(constraints, TableSplitterOptions{});
166 splitter.SplitTable(&table);
Adam Lesinski355f2852016-02-13 20:26:45 -0800167
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700168 ASSERT_EQ(2u, splitter.splits().size());
Adam Lesinski355f2852016-02-13 20:26:45 -0800169
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700170 ResourceTable* split_one = splitter.splits()[0].get();
171 ResourceTable* split_two = splitter.splits()[1].get();
Adam Lesinski355f2852016-02-13 20:26:45 -0800172
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700173 // All but the xxhdpi resource should be gone, since there were closer matches
174 // in land-xhdpi.
175 EXPECT_EQ(nullptr,
176 test::GetValueForConfig<Id>(&table, "android:string/foo",
177 test::ParseConfigOrDie("land-hdpi")));
178 EXPECT_EQ(nullptr,
179 test::GetValueForConfig<Id>(&table, "android:string/foo",
180 test::ParseConfigOrDie("land-xhdpi")));
181 EXPECT_NE(nullptr,
182 test::GetValueForConfig<Id>(&table, "android:string/foo",
183 test::ParseConfigOrDie("land-xxhdpi")));
Adam Lesinski355f2852016-02-13 20:26:45 -0800184
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700185 EXPECT_NE(nullptr,
186 test::GetValueForConfig<Id>(split_one, "android:string/foo",
187 test::ParseConfigOrDie("land-hdpi")));
188 EXPECT_EQ(nullptr,
189 test::GetValueForConfig<Id>(split_one, "android:string/foo",
190 test::ParseConfigOrDie("land-xhdpi")));
191 EXPECT_EQ(nullptr,
192 test::GetValueForConfig<Id>(split_one, "android:string/foo",
193 test::ParseConfigOrDie("land-xxhdpi")));
Adam Lesinski355f2852016-02-13 20:26:45 -0800194
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700195 EXPECT_EQ(nullptr,
196 test::GetValueForConfig<Id>(split_two, "android:string/foo",
197 test::ParseConfigOrDie("land-hdpi")));
198 EXPECT_NE(nullptr,
199 test::GetValueForConfig<Id>(split_two, "android:string/foo",
200 test::ParseConfigOrDie("land-xhdpi")));
201 EXPECT_EQ(nullptr,
202 test::GetValueForConfig<Id>(split_two, "android:string/foo",
203 test::ParseConfigOrDie("land-xxhdpi")));
Adam Lesinski355f2852016-02-13 20:26:45 -0800204}
205
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700206} // namespace aapt