Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 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" |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 18 | |
Adam Lesinski | fb6312f | 2016-06-28 14:40:32 -0700 | [diff] [blame] | 19 | #include "test/Test.h" |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 20 | |
MÃ¥rten Kongstad | 24c9aa6 | 2018-06-20 08:46:41 +0200 | [diff] [blame] | 21 | using ::android::ConfigDescription; |
| 22 | |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 23 | namespace aapt { |
| 24 | |
| 25 | TEST(TableSplitterTest, NoSplitPreferredDensity) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 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(); |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 42 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 43 | TableSplitterOptions options; |
Pierre Lecesne | 672384b | 2017-02-06 10:29:02 +0000 | [diff] [blame] | 44 | options.preferred_densities.push_back(ConfigDescription::DENSITY_XHIGH); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 45 | TableSplitter splitter({}, options); |
| 46 | splitter.SplitTable(table.get()); |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 47 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 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")); |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 61 | } |
| 62 | |
Pierre Lecesne | 672384b | 2017-02-06 10:29:02 +0000 | [diff] [blame] | 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 | |
Adam Lesinski | 36c73a5 | 2016-08-11 13:39:24 -0700 | [diff] [blame] | 108 | TEST(TableSplitterTest, SplitTableByDensity) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 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(); |
Adam Lesinski | 36c73a5 | 2016-08-11 13:39:24 -0700 | [diff] [blame] | 122 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 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")}}); |
Adam Lesinski | 36c73a5 | 2016-08-11 13:39:24 -0700 | [diff] [blame] | 127 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 128 | TableSplitter splitter(constraints, TableSplitterOptions{}); |
| 129 | splitter.SplitTable(table.get()); |
Adam Lesinski | 36c73a5 | 2016-08-11 13:39:24 -0700 | [diff] [blame] | 130 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 131 | ASSERT_EQ(3u, splitter.splits().size()); |
Adam Lesinski | 36c73a5 | 2016-08-11 13:39:24 -0700 | [diff] [blame] | 132 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 133 | ResourceTable* split_one = splitter.splits()[0].get(); |
| 134 | ResourceTable* split_two = splitter.splits()[1].get(); |
| 135 | ResourceTable* split_three = splitter.splits()[2].get(); |
Adam Lesinski | 36c73a5 | 2016-08-11 13:39:24 -0700 | [diff] [blame] | 136 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 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"))); |
Adam Lesinski | 36c73a5 | 2016-08-11 13:39:24 -0700 | [diff] [blame] | 150 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 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"))); |
Adam Lesinski | 36c73a5 | 2016-08-11 13:39:24 -0700 | [diff] [blame] | 164 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 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"))); |
Adam Lesinski | 36c73a5 | 2016-08-11 13:39:24 -0700 | [diff] [blame] | 177 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 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"))); |
Adam Lesinski | 36c73a5 | 2016-08-11 13:39:24 -0700 | [diff] [blame] | 190 | } |
| 191 | |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 192 | TEST(TableSplitterTest, SplitTableByConfigAndDensity) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 193 | ResourceTable table; |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 194 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 195 | const ResourceName foo = test::ParseNameOrDie("android:string/foo"); |
Ryan Mitchell | 1d008d1 | 2021-03-19 14:54:17 -0700 | [diff] [blame] | 196 | ASSERT_TRUE( |
| 197 | table.AddResource(NewResourceBuilder(foo) |
| 198 | .SetValue(util::make_unique<Id>(), test::ParseConfigOrDie("land-hdpi")) |
| 199 | .Build(), |
| 200 | test::GetDiagnostics())); |
| 201 | |
| 202 | ASSERT_TRUE( |
| 203 | table.AddResource(NewResourceBuilder(foo) |
| 204 | .SetValue(util::make_unique<Id>(), test::ParseConfigOrDie("land-xhdpi")) |
| 205 | .Build(), |
| 206 | test::GetDiagnostics())); |
| 207 | |
| 208 | ASSERT_TRUE(table.AddResource( |
| 209 | NewResourceBuilder(foo) |
| 210 | .SetValue(util::make_unique<Id>(), test::ParseConfigOrDie("land-xxhdpi")) |
| 211 | .Build(), |
| 212 | test::GetDiagnostics())); |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 213 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 214 | std::vector<SplitConstraints> constraints; |
| 215 | constraints.push_back( |
| 216 | SplitConstraints{{test::ParseConfigOrDie("land-mdpi")}}); |
| 217 | constraints.push_back( |
| 218 | SplitConstraints{{test::ParseConfigOrDie("land-xhdpi")}}); |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 219 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 220 | TableSplitter splitter(constraints, TableSplitterOptions{}); |
| 221 | splitter.SplitTable(&table); |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 222 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 223 | ASSERT_EQ(2u, splitter.splits().size()); |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 224 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 225 | ResourceTable* split_one = splitter.splits()[0].get(); |
| 226 | ResourceTable* split_two = splitter.splits()[1].get(); |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 227 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 228 | // All but the xxhdpi resource should be gone, since there were closer matches |
| 229 | // in land-xhdpi. |
| 230 | EXPECT_EQ(nullptr, |
| 231 | test::GetValueForConfig<Id>(&table, "android:string/foo", |
| 232 | test::ParseConfigOrDie("land-hdpi"))); |
| 233 | EXPECT_EQ(nullptr, |
| 234 | test::GetValueForConfig<Id>(&table, "android:string/foo", |
| 235 | test::ParseConfigOrDie("land-xhdpi"))); |
| 236 | EXPECT_NE(nullptr, |
| 237 | test::GetValueForConfig<Id>(&table, "android:string/foo", |
| 238 | test::ParseConfigOrDie("land-xxhdpi"))); |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 239 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 240 | EXPECT_NE(nullptr, |
| 241 | test::GetValueForConfig<Id>(split_one, "android:string/foo", |
| 242 | test::ParseConfigOrDie("land-hdpi"))); |
| 243 | EXPECT_EQ(nullptr, |
| 244 | test::GetValueForConfig<Id>(split_one, "android:string/foo", |
| 245 | test::ParseConfigOrDie("land-xhdpi"))); |
| 246 | EXPECT_EQ(nullptr, |
| 247 | test::GetValueForConfig<Id>(split_one, "android:string/foo", |
| 248 | test::ParseConfigOrDie("land-xxhdpi"))); |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 249 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 250 | EXPECT_EQ(nullptr, |
| 251 | test::GetValueForConfig<Id>(split_two, "android:string/foo", |
| 252 | test::ParseConfigOrDie("land-hdpi"))); |
| 253 | EXPECT_NE(nullptr, |
| 254 | test::GetValueForConfig<Id>(split_two, "android:string/foo", |
| 255 | test::ParseConfigOrDie("land-xhdpi"))); |
| 256 | EXPECT_EQ(nullptr, |
| 257 | test::GetValueForConfig<Id>(split_two, "android:string/foo", |
| 258 | test::ParseConfigOrDie("land-xxhdpi"))); |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 259 | } |
| 260 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 261 | } // namespace aapt |