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 | fb6312f | 2016-06-28 14:40:32 -0700 | [diff] [blame] | 18 | #include "test/Test.h" |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 19 | |
| 20 | namespace aapt { |
| 21 | |
| 22 | TEST(TableSplitterTest, NoSplitPreferredDensity) { |
| 23 | std::unique_ptr<ResourceTable> table = test::ResourceTableBuilder() |
Adam Lesinski | 58a20a6 | 2016-07-25 17:56:58 -0700 | [diff] [blame] | 24 | .addFileReference("android:drawable/icon", "res/drawable-mdpi/icon.png", |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 25 | test::parseConfigOrDie("mdpi")) |
Adam Lesinski | 58a20a6 | 2016-07-25 17:56:58 -0700 | [diff] [blame] | 26 | .addFileReference("android:drawable/icon", "res/drawable-hdpi/icon.png", |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 27 | test::parseConfigOrDie("hdpi")) |
Adam Lesinski | 58a20a6 | 2016-07-25 17:56:58 -0700 | [diff] [blame] | 28 | .addFileReference("android:drawable/icon", "res/drawable-xhdpi/icon.png", |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 29 | test::parseConfigOrDie("xhdpi")) |
Adam Lesinski | 58a20a6 | 2016-07-25 17:56:58 -0700 | [diff] [blame] | 30 | .addFileReference("android:drawable/icon", "res/drawable-xxhdpi/icon.png", |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 31 | test::parseConfigOrDie("xxhdpi")) |
Adam Lesinski | 58a20a6 | 2016-07-25 17:56:58 -0700 | [diff] [blame] | 32 | .addSimple("android:string/one") |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 33 | .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 Lesinski | 58a20a6 | 2016-07-25 17:56:58 -0700 | [diff] [blame] | 41 | "android:drawable/icon", |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 42 | test::parseConfigOrDie("mdpi"))); |
| 43 | EXPECT_EQ(nullptr, test::getValueForConfig<FileReference>(table.get(), |
Adam Lesinski | 58a20a6 | 2016-07-25 17:56:58 -0700 | [diff] [blame] | 44 | "android:drawable/icon", |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 45 | test::parseConfigOrDie("hdpi"))); |
| 46 | EXPECT_NE(nullptr, test::getValueForConfig<FileReference>(table.get(), |
Adam Lesinski | 58a20a6 | 2016-07-25 17:56:58 -0700 | [diff] [blame] | 47 | "android:drawable/icon", |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 48 | test::parseConfigOrDie("xhdpi"))); |
| 49 | EXPECT_EQ(nullptr, test::getValueForConfig<FileReference>(table.get(), |
Adam Lesinski | 58a20a6 | 2016-07-25 17:56:58 -0700 | [diff] [blame] | 50 | "android:drawable/icon", |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 51 | test::parseConfigOrDie("xxhdpi"))); |
Adam Lesinski | 58a20a6 | 2016-07-25 17:56:58 -0700 | [diff] [blame] | 52 | EXPECT_NE(nullptr, test::getValue<Id>(table.get(), "android:string/one")); |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 53 | } |
| 54 | |
Adam Lesinski | 36c73a5 | 2016-08-11 13:39:24 -0700 | [diff] [blame] | 55 | TEST(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 Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 120 | TEST(TableSplitterTest, SplitTableByConfigAndDensity) { |
| 121 | ResourceTable table; |
| 122 | |
Adam Lesinski | 58a20a6 | 2016-07-25 17:56:58 -0700 | [diff] [blame] | 123 | const ResourceName foo = test::parseNameOrDie("android:string/foo"); |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 124 | 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 Lesinski | 36c73a5 | 2016-08-11 13:39:24 -0700 | [diff] [blame] | 146 | // All but the xxhdpi resource should be gone, since there were closer matches in land-xhdpi. |
Adam Lesinski | 58a20a6 | 2016-07-25 17:56:58 -0700 | [diff] [blame] | 147 | EXPECT_EQ(nullptr, test::getValueForConfig<Id>(&table, "android:string/foo", |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 148 | test::parseConfigOrDie("land-hdpi"))); |
Adam Lesinski | 58a20a6 | 2016-07-25 17:56:58 -0700 | [diff] [blame] | 149 | EXPECT_EQ(nullptr, test::getValueForConfig<Id>(&table, "android:string/foo", |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 150 | test::parseConfigOrDie("land-xhdpi"))); |
Adam Lesinski | 36c73a5 | 2016-08-11 13:39:24 -0700 | [diff] [blame] | 151 | EXPECT_NE(nullptr, test::getValueForConfig<Id>(&table, "android:string/foo", |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 152 | test::parseConfigOrDie("land-xxhdpi"))); |
| 153 | |
Adam Lesinski | 58a20a6 | 2016-07-25 17:56:58 -0700 | [diff] [blame] | 154 | EXPECT_NE(nullptr, test::getValueForConfig<Id>(splitOne, "android:string/foo", |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 155 | test::parseConfigOrDie("land-hdpi"))); |
Adam Lesinski | 58a20a6 | 2016-07-25 17:56:58 -0700 | [diff] [blame] | 156 | EXPECT_EQ(nullptr, test::getValueForConfig<Id>(splitOne, "android:string/foo", |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 157 | test::parseConfigOrDie("land-xhdpi"))); |
Adam Lesinski | 58a20a6 | 2016-07-25 17:56:58 -0700 | [diff] [blame] | 158 | EXPECT_EQ(nullptr, test::getValueForConfig<Id>(splitOne, "android:string/foo", |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 159 | test::parseConfigOrDie("land-xxhdpi"))); |
| 160 | |
Adam Lesinski | 58a20a6 | 2016-07-25 17:56:58 -0700 | [diff] [blame] | 161 | EXPECT_EQ(nullptr, test::getValueForConfig<Id>(splitTwo, "android:string/foo", |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 162 | test::parseConfigOrDie("land-hdpi"))); |
Adam Lesinski | 58a20a6 | 2016-07-25 17:56:58 -0700 | [diff] [blame] | 163 | EXPECT_NE(nullptr, test::getValueForConfig<Id>(splitTwo, "android:string/foo", |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 164 | test::parseConfigOrDie("land-xhdpi"))); |
Adam Lesinski | 58a20a6 | 2016-07-25 17:56:58 -0700 | [diff] [blame] | 165 | EXPECT_EQ(nullptr, test::getValueForConfig<Id>(splitTwo, "android:string/foo", |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 166 | test::parseConfigOrDie("land-xxhdpi"))); |
| 167 | } |
| 168 | |
| 169 | } // namespace aapt |