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