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 | 30715f3 | 2021-11-05 21:38:00 +0000 | [diff] [blame] | 30 | ResTable_config bestConfig; |
| 31 | memset(&bestConfig, 0, sizeof(bestConfig)); |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 32 | const size_t configCount = configs.size(); |
| 33 | for (size_t i = 0; i < configCount; i++) { |
| 34 | const ResTable_config& thisConfig = configs[i]; |
Jeremy Meyer | 30715f3 | 2021-11-05 21:38:00 +0000 | [diff] [blame] | 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 | |
Jeremy Meyer | 3a619c8 | 2021-11-05 22:44:59 +0000 | [diff] [blame] | 78 | configs.add(buildDensityConfig(int(ResTable_config::DENSITY_XHIGH) - 1)); |
| 79 | ASSERT_EQ(expectedBest, selectBest(deviceConfig, configs)); |
| 80 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 81 | expectedBest = buildDensityConfig(ResTable_config::DENSITY_XHIGH); |
| 82 | configs.add(expectedBest); |
| 83 | ASSERT_EQ(expectedBest, selectBest(deviceConfig, configs)); |
Adam Lesinski | 31245b4 | 2014-08-22 19:10:56 -0700 | [diff] [blame] | 84 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 85 | expectedBest = buildDensityConfig(ResTable_config::DENSITY_ANY); |
| 86 | expectedBest.sdkVersion = 21; |
| 87 | configs.add(expectedBest); |
| 88 | ASSERT_EQ(expectedBest, selectBest(deviceConfig, configs)); |
Adam Lesinski | 31245b4 | 2014-08-22 19:10:56 -0700 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | TEST(ConfigTest, shouldSelectBestDensityWhenNoneSpecified) { |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 92 | ResTable_config deviceConfig; |
| 93 | memset(&deviceConfig, 0, sizeof(deviceConfig)); |
| 94 | deviceConfig.sdkVersion = 21; |
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 | Vector<ResTable_config> configs; |
| 97 | configs.add(buildDensityConfig(ResTable_config::DENSITY_HIGH)); |
Adam Lesinski | 31245b4 | 2014-08-22 19:10:56 -0700 | [diff] [blame] | 98 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 99 | ResTable_config expectedBest = |
| 100 | buildDensityConfig(ResTable_config::DENSITY_MEDIUM); |
| 101 | configs.add(expectedBest); |
| 102 | ASSERT_EQ(expectedBest, selectBest(deviceConfig, configs)); |
Adam Lesinski | 31245b4 | 2014-08-22 19:10:56 -0700 | [diff] [blame] | 103 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 104 | expectedBest = buildDensityConfig(ResTable_config::DENSITY_ANY); |
| 105 | configs.add(expectedBest); |
| 106 | ASSERT_EQ(expectedBest, selectBest(deviceConfig, configs)); |
Adam Lesinski | 31245b4 | 2014-08-22 19:10:56 -0700 | [diff] [blame] | 107 | } |
| 108 | |
Adam Lesinski | 2738c96 | 2015-05-14 14:25:36 -0700 | [diff] [blame] | 109 | TEST(ConfigTest, shouldMatchRoundQualifier) { |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 110 | ResTable_config deviceConfig; |
| 111 | memset(&deviceConfig, 0, sizeof(deviceConfig)); |
Adam Lesinski | 2738c96 | 2015-05-14 14:25:36 -0700 | [diff] [blame] | 112 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 113 | ResTable_config roundConfig; |
| 114 | memset(&roundConfig, 0, sizeof(roundConfig)); |
| 115 | roundConfig.screenLayout2 = ResTable_config::SCREENROUND_YES; |
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 | EXPECT_FALSE(roundConfig.match(deviceConfig)); |
Adam Lesinski | 2738c96 | 2015-05-14 14:25:36 -0700 | [diff] [blame] | 118 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 119 | deviceConfig.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_TRUE(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_NO; |
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_FALSE(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 | ResTable_config notRoundConfig; |
| 128 | memset(¬RoundConfig, 0, sizeof(notRoundConfig)); |
| 129 | notRoundConfig.screenLayout2 = ResTable_config::SCREENROUND_NO; |
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 | EXPECT_TRUE(notRoundConfig.match(deviceConfig)); |
Adam Lesinski | 2738c96 | 2015-05-14 14:25:36 -0700 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | TEST(ConfigTest, RoundQualifierShouldHaveStableSortOrder) { |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 135 | ResTable_config defaultConfig; |
| 136 | memset(&defaultConfig, 0, sizeof(defaultConfig)); |
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 longConfig = defaultConfig; |
| 139 | longConfig.screenLayout = ResTable_config::SCREENLONG_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 longRoundConfig = longConfig; |
| 142 | longRoundConfig.screenLayout2 = ResTable_config::SCREENROUND_YES; |
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 | ResTable_config longRoundPortConfig = longConfig; |
| 145 | longRoundPortConfig.orientation = ResTable_config::ORIENTATION_PORT; |
Adam Lesinski | 2738c96 | 2015-05-14 14:25:36 -0700 | [diff] [blame] | 146 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 147 | EXPECT_TRUE(longConfig.compare(longRoundConfig) < 0); |
| 148 | EXPECT_TRUE(longConfig.compareLogical(longRoundConfig) < 0); |
| 149 | EXPECT_TRUE(longRoundConfig.compare(longConfig) > 0); |
| 150 | EXPECT_TRUE(longRoundConfig.compareLogical(longConfig) > 0); |
Adam Lesinski | 2738c96 | 2015-05-14 14:25:36 -0700 | [diff] [blame] | 151 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 152 | EXPECT_TRUE(longRoundConfig.compare(longRoundPortConfig) < 0); |
| 153 | EXPECT_TRUE(longRoundConfig.compareLogical(longRoundPortConfig) < 0); |
| 154 | EXPECT_TRUE(longRoundPortConfig.compare(longRoundConfig) > 0); |
| 155 | EXPECT_TRUE(longRoundPortConfig.compareLogical(longRoundConfig) > 0); |
Adam Lesinski | 2738c96 | 2015-05-14 14:25:36 -0700 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | TEST(ConfigTest, ScreenShapeHasCorrectDiff) { |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 159 | ResTable_config defaultConfig; |
| 160 | memset(&defaultConfig, 0, sizeof(defaultConfig)); |
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 | ResTable_config roundConfig = defaultConfig; |
| 163 | roundConfig.screenLayout2 = ResTable_config::SCREENROUND_YES; |
Adam Lesinski | 2738c96 | 2015-05-14 14:25:36 -0700 | [diff] [blame] | 164 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 165 | EXPECT_EQ(defaultConfig.diff(roundConfig), |
| 166 | ResTable_config::CONFIG_SCREEN_ROUND); |
Adam Lesinski | 2738c96 | 2015-05-14 14:25:36 -0700 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | TEST(ConfigTest, RoundIsMoreSpecific) { |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 170 | ResTable_config deviceConfig; |
| 171 | memset(&deviceConfig, 0, sizeof(deviceConfig)); |
| 172 | deviceConfig.screenLayout2 = ResTable_config::SCREENROUND_YES; |
| 173 | deviceConfig.screenLayout = ResTable_config::SCREENLONG_YES; |
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 targetConfigA; |
| 176 | memset(&targetConfigA, 0, sizeof(targetConfigA)); |
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 targetConfigB = targetConfigA; |
| 179 | targetConfigB.screenLayout = ResTable_config::SCREENLONG_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 | ResTable_config targetConfigC = targetConfigB; |
| 182 | targetConfigC.screenLayout2 = ResTable_config::SCREENROUND_YES; |
Adam Lesinski | 2738c96 | 2015-05-14 14:25:36 -0700 | [diff] [blame] | 183 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 184 | EXPECT_TRUE(targetConfigB.isBetterThan(targetConfigA, &deviceConfig)); |
| 185 | EXPECT_TRUE(targetConfigC.isBetterThan(targetConfigB, &deviceConfig)); |
Adam Lesinski | 2738c96 | 2015-05-14 14:25:36 -0700 | [diff] [blame] | 186 | } |
| 187 | |
Romain Guy | c9ba559 | 2017-01-18 16:34:42 -0800 | [diff] [blame] | 188 | TEST(ConfigTest, ScreenIsWideGamut) { |
| 189 | ResTable_config defaultConfig; |
| 190 | memset(&defaultConfig, 0, sizeof(defaultConfig)); |
| 191 | |
| 192 | ResTable_config wideGamutConfig = defaultConfig; |
Romain Guy | 4832745b | 2017-01-23 17:03:35 -0800 | [diff] [blame] | 193 | wideGamutConfig.colorMode = ResTable_config::WIDE_COLOR_GAMUT_YES; |
Romain Guy | c9ba559 | 2017-01-18 16:34:42 -0800 | [diff] [blame] | 194 | |
Romain Guy | 4832745b | 2017-01-23 17:03:35 -0800 | [diff] [blame] | 195 | EXPECT_EQ(defaultConfig.diff(wideGamutConfig), ResTable_config::CONFIG_COLOR_MODE); |
Romain Guy | c9ba559 | 2017-01-18 16:34:42 -0800 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | TEST(ConfigTest, ScreenIsHdr) { |
| 199 | ResTable_config defaultConfig; |
| 200 | memset(&defaultConfig, 0, sizeof(defaultConfig)); |
| 201 | |
| 202 | ResTable_config hdrConfig = defaultConfig; |
Romain Guy | 4832745b | 2017-01-23 17:03:35 -0800 | [diff] [blame] | 203 | hdrConfig.colorMode = ResTable_config::HDR_YES; |
Romain Guy | c9ba559 | 2017-01-18 16:34:42 -0800 | [diff] [blame] | 204 | |
Romain Guy | 4832745b | 2017-01-23 17:03:35 -0800 | [diff] [blame] | 205 | EXPECT_EQ(defaultConfig.diff(hdrConfig), ResTable_config::CONFIG_COLOR_MODE); |
Romain Guy | c9ba559 | 2017-01-18 16:34:42 -0800 | [diff] [blame] | 206 | } |
| 207 | |
Yurii Zubrytskyi | 02d8919 | 2023-01-04 08:44:34 -0800 | [diff] [blame] | 208 | TEST(ConfigTest, GrammaticalGender) { |
| 209 | ResTable_config defaultConfig = {}; |
| 210 | ResTable_config masculine = {}; |
| 211 | masculine.grammaticalInflection = ResTable_config::GRAMMATICAL_GENDER_MASCULINE; |
| 212 | |
| 213 | EXPECT_EQ(defaultConfig.diff(masculine), ResTable_config::CONFIG_GRAMMATICAL_GENDER); |
| 214 | |
| 215 | ResTable_config feminine = {}; |
| 216 | feminine.grammaticalInflection = ResTable_config::GRAMMATICAL_GENDER_FEMININE; |
| 217 | |
| 218 | EXPECT_EQ(defaultConfig.diff(feminine), ResTable_config::CONFIG_GRAMMATICAL_GENDER); |
| 219 | EXPECT_EQ(masculine.diff(feminine), ResTable_config::CONFIG_GRAMMATICAL_GENDER); |
| 220 | } |
| 221 | |
Adam Lesinski | 31245b4 | 2014-08-22 19:10:56 -0700 | [diff] [blame] | 222 | } // namespace android. |