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 | |
| 21 | namespace aapt { |
| 22 | |
| 23 | TEST(TableSplitterTest, NoSplitPreferredDensity) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 24 | 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 Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 40 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 41 | TableSplitterOptions options; |
| 42 | options.preferred_density = ConfigDescription::DENSITY_XHIGH; |
| 43 | TableSplitter splitter({}, options); |
| 44 | splitter.SplitTable(table.get()); |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 45 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 46 | 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 Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 59 | } |
| 60 | |
Adam Lesinski | 36c73a5 | 2016-08-11 13:39:24 -0700 | [diff] [blame] | 61 | TEST(TableSplitterTest, SplitTableByDensity) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 62 | 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 Lesinski | 36c73a5 | 2016-08-11 13:39:24 -0700 | [diff] [blame] | 75 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 76 | 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 Lesinski | 36c73a5 | 2016-08-11 13:39:24 -0700 | [diff] [blame] | 80 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 81 | TableSplitter splitter(constraints, TableSplitterOptions{}); |
| 82 | splitter.SplitTable(table.get()); |
Adam Lesinski | 36c73a5 | 2016-08-11 13:39:24 -0700 | [diff] [blame] | 83 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 84 | ASSERT_EQ(3u, splitter.splits().size()); |
Adam Lesinski | 36c73a5 | 2016-08-11 13:39:24 -0700 | [diff] [blame] | 85 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 86 | ResourceTable* split_one = splitter.splits()[0].get(); |
| 87 | ResourceTable* split_two = splitter.splits()[1].get(); |
| 88 | ResourceTable* split_three = splitter.splits()[2].get(); |
Adam Lesinski | 36c73a5 | 2016-08-11 13:39:24 -0700 | [diff] [blame] | 89 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 90 | // 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 Lesinski | 36c73a5 | 2016-08-11 13:39:24 -0700 | [diff] [blame] | 103 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 104 | // 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 Lesinski | 36c73a5 | 2016-08-11 13:39:24 -0700 | [diff] [blame] | 117 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 118 | 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 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 | 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 Lesinski | 36c73a5 | 2016-08-11 13:39:24 -0700 | [diff] [blame] | 143 | } |
| 144 | |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 145 | TEST(TableSplitterTest, SplitTableByConfigAndDensity) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 146 | ResourceTable table; |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 147 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 148 | 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 Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 158 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 159 | 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 Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 164 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 165 | TableSplitter splitter(constraints, TableSplitterOptions{}); |
| 166 | splitter.SplitTable(&table); |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 167 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 168 | ASSERT_EQ(2u, splitter.splits().size()); |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 169 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 170 | ResourceTable* split_one = splitter.splits()[0].get(); |
| 171 | ResourceTable* split_two = splitter.splits()[1].get(); |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 172 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 173 | // 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 Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 184 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 185 | 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 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 | 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 Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 204 | } |
| 205 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 206 | } // namespace aapt |