• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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"
18 
19 #include "test/Test.h"
20 
21 using ::android::ConfigDescription;
22 
23 namespace aapt {
24 
TEST(TableSplitterTest,NoSplitPreferredDensity)25 TEST(TableSplitterTest, NoSplitPreferredDensity) {
26   std::unique_ptr<ResourceTable> table =
27       test::ResourceTableBuilder()
28           .AddFileReference("android:drawable/icon",
29                             "res/drawable-mdpi/icon.png",
30                             test::ParseConfigOrDie("mdpi"))
31           .AddFileReference("android:drawable/icon",
32                             "res/drawable-hdpi/icon.png",
33                             test::ParseConfigOrDie("hdpi"))
34           .AddFileReference("android:drawable/icon",
35                             "res/drawable-xhdpi/icon.png",
36                             test::ParseConfigOrDie("xhdpi"))
37           .AddFileReference("android:drawable/icon",
38                             "res/drawable-xxhdpi/icon.png",
39                             test::ParseConfigOrDie("xxhdpi"))
40           .AddSimple("android:string/one")
41           .Build();
42 
43   TableSplitterOptions options;
44   options.preferred_densities.push_back(ConfigDescription::DENSITY_XHIGH);
45   TableSplitter splitter({}, options);
46   splitter.SplitTable(table.get());
47 
48   EXPECT_EQ(nullptr, test::GetValueForConfig<FileReference>(
49                          table.get(), "android:drawable/icon",
50                          test::ParseConfigOrDie("mdpi")));
51   EXPECT_EQ(nullptr, test::GetValueForConfig<FileReference>(
52                          table.get(), "android:drawable/icon",
53                          test::ParseConfigOrDie("hdpi")));
54   EXPECT_NE(nullptr, test::GetValueForConfig<FileReference>(
55                          table.get(), "android:drawable/icon",
56                          test::ParseConfigOrDie("xhdpi")));
57   EXPECT_EQ(nullptr, test::GetValueForConfig<FileReference>(
58                          table.get(), "android:drawable/icon",
59                          test::ParseConfigOrDie("xxhdpi")));
60   EXPECT_NE(nullptr, test::GetValue<Id>(table.get(), "android:string/one"));
61 }
62 
TEST(TableSplitterTest,NoSplitMultiplePreferredDensities)63 TEST(TableSplitterTest, NoSplitMultiplePreferredDensities) {
64   std::unique_ptr<ResourceTable> table =
65       test::ResourceTableBuilder()
66           .AddFileReference("android:drawable/icon",
67                             "res/drawable-mdpi/icon.png",
68                             test::ParseConfigOrDie("mdpi"))
69           .AddFileReference("android:drawable/icon",
70                             "res/drawable-hdpi/icon.png",
71                             test::ParseConfigOrDie("hdpi"))
72           .AddFileReference("android:drawable/icon",
73                             "res/drawable-xhdpi/icon.png",
74                             test::ParseConfigOrDie("xhdpi"))
75           .AddFileReference("android:drawable/icon",
76                             "res/drawable-xxhdpi/icon.png",
77                             test::ParseConfigOrDie("xxhdpi"))
78           .AddSimple("android:string/one")
79           .Build();
80 
81   TableSplitterOptions options;
82   options.preferred_densities.push_back(ConfigDescription::DENSITY_LOW);
83   options.preferred_densities.push_back(ConfigDescription::DENSITY_XXXHIGH);
84   TableSplitter splitter({}, options);
85   splitter.SplitTable(table.get());
86 
87   // Densities remaining:
88   // "mdpi" is the closest available density for the requested "ldpi" density.
89   EXPECT_NE(nullptr, test::GetValueForConfig<FileReference>(
90                          table.get(), "android:drawable/icon",
91                          test::ParseConfigOrDie("mdpi")));
92   // "xxhdpi" is the closest available density for the requested "xxxhdpi" density.
93   EXPECT_NE(nullptr, test::GetValueForConfig<FileReference>(
94                          table.get(), "android:drawable/icon",
95                          test::ParseConfigOrDie("xxhdpi")));
96   EXPECT_NE(nullptr, test::GetValue<Id>(table.get(), "android:string/one"));
97 
98   // Removed densities:
99   EXPECT_EQ(nullptr, test::GetValueForConfig<FileReference>(
100                          table.get(), "android:drawable/icon",
101                          test::ParseConfigOrDie("hdpi")));
102   EXPECT_EQ(nullptr, test::GetValueForConfig<FileReference>(
103                          table.get(), "android:drawable/icon",
104                          test::ParseConfigOrDie("xhdpi")));
105 }
106 
107 
TEST(TableSplitterTest,SplitTableByDensity)108 TEST(TableSplitterTest, SplitTableByDensity) {
109   std::unique_ptr<ResourceTable> table =
110       test::ResourceTableBuilder()
111           .AddFileReference("android:drawable/foo", "res/drawable-mdpi/foo.png",
112                             test::ParseConfigOrDie("mdpi"))
113           .AddFileReference("android:drawable/foo", "res/drawable-hdpi/foo.png",
114                             test::ParseConfigOrDie("hdpi"))
115           .AddFileReference("android:drawable/foo",
116                             "res/drawable-xhdpi/foo.png",
117                             test::ParseConfigOrDie("xhdpi"))
118           .AddFileReference("android:drawable/foo",
119                             "res/drawable-xxhdpi/foo.png",
120                             test::ParseConfigOrDie("xxhdpi"))
121           .Build();
122 
123   std::vector<SplitConstraints> constraints;
124   constraints.push_back(SplitConstraints{{test::ParseConfigOrDie("mdpi")}});
125   constraints.push_back(SplitConstraints{{test::ParseConfigOrDie("hdpi")}});
126   constraints.push_back(SplitConstraints{{test::ParseConfigOrDie("xhdpi")}});
127 
128   TableSplitter splitter(constraints, TableSplitterOptions{});
129   splitter.SplitTable(table.get());
130 
131   ASSERT_EQ(3u, splitter.splits().size());
132 
133   ResourceTable* split_one = splitter.splits()[0].get();
134   ResourceTable* split_two = splitter.splits()[1].get();
135   ResourceTable* split_three = splitter.splits()[2].get();
136 
137   // Just xxhdpi should be in the base.
138   EXPECT_EQ(nullptr, test::GetValueForConfig<FileReference>(
139                          table.get(), "android:drawable/foo",
140                          test::ParseConfigOrDie("mdpi")));
141   EXPECT_EQ(nullptr, test::GetValueForConfig<FileReference>(
142                          table.get(), "android:drawable/foo",
143                          test::ParseConfigOrDie("hdpi")));
144   EXPECT_EQ(nullptr, test::GetValueForConfig<FileReference>(
145                          table.get(), "android:drawable/foo",
146                          test::ParseConfigOrDie("xhdpi")));
147   EXPECT_NE(nullptr, test::GetValueForConfig<FileReference>(
148                          table.get(), "android:drawable/foo",
149                          test::ParseConfigOrDie("xxhdpi")));
150 
151   // Each split should have one and only one drawable.
152   EXPECT_NE(nullptr, test::GetValueForConfig<FileReference>(
153                          split_one, "android:drawable/foo",
154                          test::ParseConfigOrDie("mdpi")));
155   EXPECT_EQ(nullptr, test::GetValueForConfig<FileReference>(
156                          split_one, "android:drawable/foo",
157                          test::ParseConfigOrDie("hdpi")));
158   EXPECT_EQ(nullptr, test::GetValueForConfig<FileReference>(
159                          split_one, "android:drawable/foo",
160                          test::ParseConfigOrDie("xhdpi")));
161   EXPECT_EQ(nullptr, test::GetValueForConfig<FileReference>(
162                          split_one, "android:drawable/foo",
163                          test::ParseConfigOrDie("xxhdpi")));
164 
165   EXPECT_EQ(nullptr, test::GetValueForConfig<FileReference>(
166                          split_two, "android:drawable/foo",
167                          test::ParseConfigOrDie("mdpi")));
168   EXPECT_NE(nullptr, test::GetValueForConfig<FileReference>(
169                          split_two, "android:drawable/foo",
170                          test::ParseConfigOrDie("hdpi")));
171   EXPECT_EQ(nullptr, test::GetValueForConfig<FileReference>(
172                          split_two, "android:drawable/foo",
173                          test::ParseConfigOrDie("xhdpi")));
174   EXPECT_EQ(nullptr, test::GetValueForConfig<FileReference>(
175                          split_two, "android:drawable/foo",
176                          test::ParseConfigOrDie("xxhdpi")));
177 
178   EXPECT_EQ(nullptr, test::GetValueForConfig<FileReference>(
179                          split_three, "android:drawable/foo",
180                          test::ParseConfigOrDie("mdpi")));
181   EXPECT_EQ(nullptr, test::GetValueForConfig<FileReference>(
182                          split_three, "android:drawable/foo",
183                          test::ParseConfigOrDie("hdpi")));
184   EXPECT_NE(nullptr, test::GetValueForConfig<FileReference>(
185                          split_three, "android:drawable/foo",
186                          test::ParseConfigOrDie("xhdpi")));
187   EXPECT_EQ(nullptr, test::GetValueForConfig<FileReference>(
188                          split_three, "android:drawable/foo",
189                          test::ParseConfigOrDie("xxhdpi")));
190 }
191 
TEST(TableSplitterTest,SplitTableByConfigAndDensity)192 TEST(TableSplitterTest, SplitTableByConfigAndDensity) {
193   ResourceTable table;
194 
195   const ResourceName foo = test::ParseNameOrDie("android:string/foo");
196   ASSERT_TRUE(table.AddResource(foo, test::ParseConfigOrDie("land-hdpi"), {},
197                                 util::make_unique<Id>(),
198                                 test::GetDiagnostics()));
199   ASSERT_TRUE(table.AddResource(foo, test::ParseConfigOrDie("land-xhdpi"), {},
200                                 util::make_unique<Id>(),
201                                 test::GetDiagnostics()));
202   ASSERT_TRUE(table.AddResource(foo, test::ParseConfigOrDie("land-xxhdpi"), {},
203                                 util::make_unique<Id>(),
204                                 test::GetDiagnostics()));
205 
206   std::vector<SplitConstraints> constraints;
207   constraints.push_back(
208       SplitConstraints{{test::ParseConfigOrDie("land-mdpi")}});
209   constraints.push_back(
210       SplitConstraints{{test::ParseConfigOrDie("land-xhdpi")}});
211 
212   TableSplitter splitter(constraints, TableSplitterOptions{});
213   splitter.SplitTable(&table);
214 
215   ASSERT_EQ(2u, splitter.splits().size());
216 
217   ResourceTable* split_one = splitter.splits()[0].get();
218   ResourceTable* split_two = splitter.splits()[1].get();
219 
220   // All but the xxhdpi resource should be gone, since there were closer matches
221   // in land-xhdpi.
222   EXPECT_EQ(nullptr,
223             test::GetValueForConfig<Id>(&table, "android:string/foo",
224                                         test::ParseConfigOrDie("land-hdpi")));
225   EXPECT_EQ(nullptr,
226             test::GetValueForConfig<Id>(&table, "android:string/foo",
227                                         test::ParseConfigOrDie("land-xhdpi")));
228   EXPECT_NE(nullptr,
229             test::GetValueForConfig<Id>(&table, "android:string/foo",
230                                         test::ParseConfigOrDie("land-xxhdpi")));
231 
232   EXPECT_NE(nullptr,
233             test::GetValueForConfig<Id>(split_one, "android:string/foo",
234                                         test::ParseConfigOrDie("land-hdpi")));
235   EXPECT_EQ(nullptr,
236             test::GetValueForConfig<Id>(split_one, "android:string/foo",
237                                         test::ParseConfigOrDie("land-xhdpi")));
238   EXPECT_EQ(nullptr,
239             test::GetValueForConfig<Id>(split_one, "android:string/foo",
240                                         test::ParseConfigOrDie("land-xxhdpi")));
241 
242   EXPECT_EQ(nullptr,
243             test::GetValueForConfig<Id>(split_two, "android:string/foo",
244                                         test::ParseConfigOrDie("land-hdpi")));
245   EXPECT_NE(nullptr,
246             test::GetValueForConfig<Id>(split_two, "android:string/foo",
247                                         test::ParseConfigOrDie("land-xhdpi")));
248   EXPECT_EQ(nullptr,
249             test::GetValueForConfig<Id>(split_two, "android:string/foo",
250                                         test::ParseConfigOrDie("land-xxhdpi")));
251 }
252 
253 }  // namespace aapt
254