Adam Lesinski | 31245b4 | 2014-08-22 19:10:56 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 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 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 17 | #include "androidfw/ResourceTypes.h" |
| 18 | |
| 19 | #include "utils/Log.h" |
| 20 | #include "utils/String8.h" |
| 21 | #include "utils/Vector.h" |
Adam Lesinski | 31245b4 | 2014-08-22 19:10:56 -0700 | [diff] [blame] | 22 | |
| 23 | #include "TestHelpers.h" |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 24 | #include "gtest/gtest.h" |
Adam Lesinski | 31245b4 | 2014-08-22 19:10:56 -0700 | [diff] [blame] | 25 | |
| 26 | namespace android { |
| 27 | |
| 28 | static ResTable_config selectBest(const ResTable_config& target, |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 29 | const Vector<ResTable_config>& configs) { |
| 30 | ResTable_config bestConfig; |
| 31 | memset(&bestConfig, 0, sizeof(bestConfig)); |
| 32 | const size_t configCount = configs.size(); |
| 33 | for (size_t i = 0; i < configCount; i++) { |
| 34 | const ResTable_config& thisConfig = configs[i]; |
| 35 | if (!thisConfig.match(target)) { |
| 36 | continue; |
Adam Lesinski | 31245b4 | 2014-08-22 19:10:56 -0700 | [diff] [blame] | 37 | } |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 38 | |
| 39 | if (thisConfig.isBetterThan(bestConfig, &target)) { |
| 40 | bestConfig = thisConfig; |
| 41 | } |
| 42 | } |
| 43 | return bestConfig; |
Adam Lesinski | 31245b4 | 2014-08-22 19:10:56 -0700 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | static ResTable_config buildDensityConfig(int density) { |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 47 | ResTable_config config; |
| 48 | memset(&config, 0, sizeof(config)); |
| 49 | config.density = uint16_t(density); |
| 50 | config.sdkVersion = 4; |
| 51 | return config; |
Adam Lesinski | 31245b4 | 2014-08-22 19:10:56 -0700 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | TEST(ConfigTest, shouldSelectBestDensity) { |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 55 | ResTable_config deviceConfig; |
| 56 | memset(&deviceConfig, 0, sizeof(deviceConfig)); |
| 57 | deviceConfig.density = ResTable_config::DENSITY_XHIGH; |
| 58 | deviceConfig.sdkVersion = 21; |
Adam Lesinski | 31245b4 | 2014-08-22 19:10:56 -0700 | [diff] [blame] | 59 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 60 | Vector<ResTable_config> configs; |
Adam Lesinski | 31245b4 | 2014-08-22 19:10:56 -0700 | [diff] [blame] | 61 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 62 | ResTable_config expectedBest = |
| 63 | buildDensityConfig(ResTable_config::DENSITY_HIGH); |
| 64 | configs.add(expectedBest); |
| 65 | ASSERT_EQ(expectedBest, selectBest(deviceConfig, configs)); |
Adam Lesinski | 31245b4 | 2014-08-22 19:10:56 -0700 | [diff] [blame] | 66 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 67 | expectedBest = buildDensityConfig(ResTable_config::DENSITY_XXHIGH); |
| 68 | configs.add(expectedBest); |
| 69 | ASSERT_EQ(expectedBest, selectBest(deviceConfig, configs)); |
Adam Lesinski | 31245b4 | 2014-08-22 19:10:56 -0700 | [diff] [blame] | 70 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 71 | expectedBest = buildDensityConfig(int(ResTable_config::DENSITY_XXHIGH) - 20); |
| 72 | configs.add(expectedBest); |
| 73 | ASSERT_EQ(expectedBest, selectBest(deviceConfig, configs)); |
Adam Lesinski | 31245b4 | 2014-08-22 19:10:56 -0700 | [diff] [blame] | 74 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 75 | configs.add(buildDensityConfig(int(ResTable_config::DENSITY_HIGH) + 20)); |
| 76 | ASSERT_EQ(expectedBest, selectBest(deviceConfig, configs)); |
Adam Lesinski | 31245b4 | 2014-08-22 19:10:56 -0700 | [diff] [blame] | 77 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 78 | expectedBest = buildDensityConfig(ResTable_config::DENSITY_XHIGH); |
| 79 | configs.add(expectedBest); |
| 80 | ASSERT_EQ(expectedBest, selectBest(deviceConfig, configs)); |
Adam Lesinski | 31245b4 | 2014-08-22 19:10:56 -0700 | [diff] [blame] | 81 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 82 | expectedBest = buildDensityConfig(ResTable_config::DENSITY_ANY); |
| 83 | expectedBest.sdkVersion = 21; |
| 84 | configs.add(expectedBest); |
| 85 | ASSERT_EQ(expectedBest, selectBest(deviceConfig, configs)); |
Adam Lesinski | 31245b4 | 2014-08-22 19:10:56 -0700 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | TEST(ConfigTest, shouldSelectBestDensityWhenNoneSpecified) { |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 89 | ResTable_config deviceConfig; |
| 90 | memset(&deviceConfig, 0, sizeof(deviceConfig)); |
| 91 | deviceConfig.sdkVersion = 21; |
Adam Lesinski | 31245b4 | 2014-08-22 19:10:56 -0700 | [diff] [blame] | 92 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 93 | Vector<ResTable_config> configs; |
| 94 | configs.add(buildDensityConfig(ResTable_config::DENSITY_HIGH)); |
Adam Lesinski | 31245b4 | 2014-08-22 19:10:56 -0700 | [diff] [blame] | 95 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 96 | ResTable_config expectedBest = |
| 97 | buildDensityConfig(ResTable_config::DENSITY_MEDIUM); |
| 98 | configs.add(expectedBest); |
| 99 | ASSERT_EQ(expectedBest, selectBest(deviceConfig, configs)); |
Adam Lesinski | 31245b4 | 2014-08-22 19:10:56 -0700 | [diff] [blame] | 100 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 101 | expectedBest = buildDensityConfig(ResTable_config::DENSITY_ANY); |
| 102 | configs.add(expectedBest); |
| 103 | ASSERT_EQ(expectedBest, selectBest(deviceConfig, configs)); |
Adam Lesinski | 31245b4 | 2014-08-22 19:10:56 -0700 | [diff] [blame] | 104 | } |
| 105 | |
Adam Lesinski | 2738c96 | 2015-05-14 14:25:36 -0700 | [diff] [blame] | 106 | TEST(ConfigTest, shouldMatchRoundQualifier) { |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 107 | ResTable_config deviceConfig; |
| 108 | memset(&deviceConfig, 0, sizeof(deviceConfig)); |
Adam Lesinski | 2738c96 | 2015-05-14 14:25:36 -0700 | [diff] [blame] | 109 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 110 | ResTable_config roundConfig; |
| 111 | memset(&roundConfig, 0, sizeof(roundConfig)); |
| 112 | roundConfig.screenLayout2 = ResTable_config::SCREENROUND_YES; |
Adam Lesinski | 2738c96 | 2015-05-14 14:25:36 -0700 | [diff] [blame] | 113 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 114 | EXPECT_FALSE(roundConfig.match(deviceConfig)); |
Adam Lesinski | 2738c96 | 2015-05-14 14:25:36 -0700 | [diff] [blame] | 115 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 116 | deviceConfig.screenLayout2 = ResTable_config::SCREENROUND_YES; |
Adam Lesinski | 2738c96 | 2015-05-14 14:25:36 -0700 | [diff] [blame] | 117 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 118 | EXPECT_TRUE(roundConfig.match(deviceConfig)); |
Adam Lesinski | 2738c96 | 2015-05-14 14:25:36 -0700 | [diff] [blame] | 119 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 120 | deviceConfig.screenLayout2 = ResTable_config::SCREENROUND_NO; |
Adam Lesinski | 2738c96 | 2015-05-14 14:25:36 -0700 | [diff] [blame] | 121 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 122 | EXPECT_FALSE(roundConfig.match(deviceConfig)); |
Adam Lesinski | 2738c96 | 2015-05-14 14:25:36 -0700 | [diff] [blame] | 123 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 124 | ResTable_config notRoundConfig; |
| 125 | memset(¬RoundConfig, 0, sizeof(notRoundConfig)); |
| 126 | notRoundConfig.screenLayout2 = ResTable_config::SCREENROUND_NO; |
Adam Lesinski | 2738c96 | 2015-05-14 14:25:36 -0700 | [diff] [blame] | 127 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 128 | EXPECT_TRUE(notRoundConfig.match(deviceConfig)); |
Adam Lesinski | 2738c96 | 2015-05-14 14:25:36 -0700 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | TEST(ConfigTest, RoundQualifierShouldHaveStableSortOrder) { |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 132 | ResTable_config defaultConfig; |
| 133 | memset(&defaultConfig, 0, sizeof(defaultConfig)); |
Adam Lesinski | 2738c96 | 2015-05-14 14:25:36 -0700 | [diff] [blame] | 134 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 135 | ResTable_config longConfig = defaultConfig; |
| 136 | longConfig.screenLayout = ResTable_config::SCREENLONG_YES; |
Adam Lesinski | 2738c96 | 2015-05-14 14:25:36 -0700 | [diff] [blame] | 137 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 138 | ResTable_config longRoundConfig = longConfig; |
| 139 | longRoundConfig.screenLayout2 = ResTable_config::SCREENROUND_YES; |
Adam Lesinski | 2738c96 | 2015-05-14 14:25:36 -0700 | [diff] [blame] | 140 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 141 | ResTable_config longRoundPortConfig = longConfig; |
| 142 | longRoundPortConfig.orientation = ResTable_config::ORIENTATION_PORT; |
Adam Lesinski | 2738c96 | 2015-05-14 14:25:36 -0700 | [diff] [blame] | 143 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 144 | EXPECT_TRUE(longConfig.compare(longRoundConfig) < 0); |
| 145 | EXPECT_TRUE(longConfig.compareLogical(longRoundConfig) < 0); |
| 146 | EXPECT_TRUE(longRoundConfig.compare(longConfig) > 0); |
| 147 | EXPECT_TRUE(longRoundConfig.compareLogical(longConfig) > 0); |
Adam Lesinski | 2738c96 | 2015-05-14 14:25:36 -0700 | [diff] [blame] | 148 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 149 | EXPECT_TRUE(longRoundConfig.compare(longRoundPortConfig) < 0); |
| 150 | EXPECT_TRUE(longRoundConfig.compareLogical(longRoundPortConfig) < 0); |
| 151 | EXPECT_TRUE(longRoundPortConfig.compare(longRoundConfig) > 0); |
| 152 | EXPECT_TRUE(longRoundPortConfig.compareLogical(longRoundConfig) > 0); |
Adam Lesinski | 2738c96 | 2015-05-14 14:25:36 -0700 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | TEST(ConfigTest, ScreenShapeHasCorrectDiff) { |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 156 | ResTable_config defaultConfig; |
| 157 | memset(&defaultConfig, 0, sizeof(defaultConfig)); |
Adam Lesinski | 2738c96 | 2015-05-14 14:25:36 -0700 | [diff] [blame] | 158 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 159 | ResTable_config roundConfig = defaultConfig; |
| 160 | roundConfig.screenLayout2 = ResTable_config::SCREENROUND_YES; |
Adam Lesinski | 2738c96 | 2015-05-14 14:25:36 -0700 | [diff] [blame] | 161 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 162 | EXPECT_EQ(defaultConfig.diff(roundConfig), |
| 163 | ResTable_config::CONFIG_SCREEN_ROUND); |
Adam Lesinski | 2738c96 | 2015-05-14 14:25:36 -0700 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | TEST(ConfigTest, RoundIsMoreSpecific) { |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 167 | ResTable_config deviceConfig; |
| 168 | memset(&deviceConfig, 0, sizeof(deviceConfig)); |
| 169 | deviceConfig.screenLayout2 = ResTable_config::SCREENROUND_YES; |
| 170 | deviceConfig.screenLayout = ResTable_config::SCREENLONG_YES; |
Adam Lesinski | 2738c96 | 2015-05-14 14:25:36 -0700 | [diff] [blame] | 171 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 172 | ResTable_config targetConfigA; |
| 173 | memset(&targetConfigA, 0, sizeof(targetConfigA)); |
Adam Lesinski | 2738c96 | 2015-05-14 14:25:36 -0700 | [diff] [blame] | 174 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 175 | ResTable_config targetConfigB = targetConfigA; |
| 176 | targetConfigB.screenLayout = ResTable_config::SCREENLONG_YES; |
Adam Lesinski | 2738c96 | 2015-05-14 14:25:36 -0700 | [diff] [blame] | 177 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 178 | ResTable_config targetConfigC = targetConfigB; |
| 179 | targetConfigC.screenLayout2 = ResTable_config::SCREENROUND_YES; |
Adam Lesinski | 2738c96 | 2015-05-14 14:25:36 -0700 | [diff] [blame] | 180 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 181 | EXPECT_TRUE(targetConfigB.isBetterThan(targetConfigA, &deviceConfig)); |
| 182 | EXPECT_TRUE(targetConfigC.isBetterThan(targetConfigB, &deviceConfig)); |
Adam Lesinski | 2738c96 | 2015-05-14 14:25:36 -0700 | [diff] [blame] | 183 | } |
| 184 | |
Romain Guy | c9ba559 | 2017-01-18 16:34:42 -0800 | [diff] [blame] | 185 | TEST(ConfigTest, ScreenIsWideGamut) { |
| 186 | ResTable_config defaultConfig; |
| 187 | memset(&defaultConfig, 0, sizeof(defaultConfig)); |
| 188 | |
| 189 | ResTable_config wideGamutConfig = defaultConfig; |
Romain Guy | 4832745 | 2017-01-23 17:03:35 -0800 | [diff] [blame] | 190 | wideGamutConfig.colorMode = ResTable_config::WIDE_COLOR_GAMUT_YES; |
Romain Guy | c9ba559 | 2017-01-18 16:34:42 -0800 | [diff] [blame] | 191 | |
Romain Guy | 4832745 | 2017-01-23 17:03:35 -0800 | [diff] [blame] | 192 | EXPECT_EQ(defaultConfig.diff(wideGamutConfig), ResTable_config::CONFIG_COLOR_MODE); |
Romain Guy | c9ba559 | 2017-01-18 16:34:42 -0800 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | TEST(ConfigTest, ScreenIsHdr) { |
| 196 | ResTable_config defaultConfig; |
| 197 | memset(&defaultConfig, 0, sizeof(defaultConfig)); |
| 198 | |
| 199 | ResTable_config hdrConfig = defaultConfig; |
Romain Guy | 4832745 | 2017-01-23 17:03:35 -0800 | [diff] [blame] | 200 | hdrConfig.colorMode = ResTable_config::HDR_YES; |
Romain Guy | c9ba559 | 2017-01-18 16:34:42 -0800 | [diff] [blame] | 201 | |
Romain Guy | 4832745 | 2017-01-23 17:03:35 -0800 | [diff] [blame] | 202 | EXPECT_EQ(defaultConfig.diff(hdrConfig), ResTable_config::CONFIG_COLOR_MODE); |
Romain Guy | c9ba559 | 2017-01-18 16:34:42 -0800 | [diff] [blame] | 203 | } |
| 204 | |
Adam Lesinski | 31245b4 | 2014-08-22 19:10:56 -0700 | [diff] [blame] | 205 | } // namespace android. |