blob: f5fd0f20d5808ede1953a1fe85d5776bec45892e [file] [log] [blame]
Adam Lesinski31245b42014-08-22 19:10:56 -07001/*
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 Lesinski4c67a472016-11-10 16:43:59 -080017#include "androidfw/ResourceTypes.h"
18
19#include "utils/Log.h"
20#include "utils/String8.h"
21#include "utils/Vector.h"
Adam Lesinski31245b42014-08-22 19:10:56 -070022
23#include "TestHelpers.h"
Adam Lesinski4c67a472016-11-10 16:43:59 -080024#include "gtest/gtest.h"
Adam Lesinski31245b42014-08-22 19:10:56 -070025
26namespace android {
27
28static ResTable_config selectBest(const ResTable_config& target,
Adam Lesinski4c67a472016-11-10 16:43:59 -080029 const Vector<ResTable_config>& configs) {
Jeremy Meyer5ac5c9b2021-10-27 17:35:32 +000030 Vector<ResTable_config> matchedConfigs;
Adam Lesinski4c67a472016-11-10 16:43:59 -080031 const size_t configCount = configs.size();
32 for (size_t i = 0; i < configCount; i++) {
33 const ResTable_config& thisConfig = configs[i];
Jeremy Meyer5ac5c9b2021-10-27 17:35:32 +000034 if (thisConfig.match(target)) {
35 matchedConfigs.add(thisConfig);
Adam Lesinski31245b42014-08-22 19:10:56 -070036 }
Jeremy Meyer5ac5c9b2021-10-27 17:35:32 +000037 }
Adam Lesinski4c67a472016-11-10 16:43:59 -080038
Jeremy Meyer5ac5c9b2021-10-27 17:35:32 +000039 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 Lesinski4c67a472016-11-10 16:43:59 -080043 if (thisConfig.isBetterThan(bestConfig, &target)) {
44 bestConfig = thisConfig;
45 }
46 }
47 return bestConfig;
Adam Lesinski31245b42014-08-22 19:10:56 -070048}
49
50static ResTable_config buildDensityConfig(int density) {
Adam Lesinski4c67a472016-11-10 16:43:59 -080051 ResTable_config config;
52 memset(&config, 0, sizeof(config));
53 config.density = uint16_t(density);
54 config.sdkVersion = 4;
55 return config;
Adam Lesinski31245b42014-08-22 19:10:56 -070056}
57
58TEST(ConfigTest, shouldSelectBestDensity) {
Adam Lesinski4c67a472016-11-10 16:43:59 -080059 ResTable_config deviceConfig;
60 memset(&deviceConfig, 0, sizeof(deviceConfig));
61 deviceConfig.density = ResTable_config::DENSITY_XHIGH;
62 deviceConfig.sdkVersion = 21;
Adam Lesinski31245b42014-08-22 19:10:56 -070063
Adam Lesinski4c67a472016-11-10 16:43:59 -080064 Vector<ResTable_config> configs;
Adam Lesinski31245b42014-08-22 19:10:56 -070065
Adam Lesinski4c67a472016-11-10 16:43:59 -080066 ResTable_config expectedBest =
67 buildDensityConfig(ResTable_config::DENSITY_HIGH);
68 configs.add(expectedBest);
69 ASSERT_EQ(expectedBest, selectBest(deviceConfig, configs));
Adam Lesinski31245b42014-08-22 19:10:56 -070070
Adam Lesinski4c67a472016-11-10 16:43:59 -080071 expectedBest = buildDensityConfig(ResTable_config::DENSITY_XXHIGH);
72 configs.add(expectedBest);
73 ASSERT_EQ(expectedBest, selectBest(deviceConfig, configs));
Adam Lesinski31245b42014-08-22 19:10:56 -070074
Adam Lesinski4c67a472016-11-10 16:43:59 -080075 expectedBest = buildDensityConfig(int(ResTable_config::DENSITY_XXHIGH) - 20);
76 configs.add(expectedBest);
77 ASSERT_EQ(expectedBest, selectBest(deviceConfig, configs));
Adam Lesinski31245b42014-08-22 19:10:56 -070078
Adam Lesinski4c67a472016-11-10 16:43:59 -080079 configs.add(buildDensityConfig(int(ResTable_config::DENSITY_HIGH) + 20));
80 ASSERT_EQ(expectedBest, selectBest(deviceConfig, configs));
Adam Lesinski31245b42014-08-22 19:10:56 -070081
Jeremy Meyer5ac5c9b2021-10-27 17:35:32 +000082 configs.add(buildDensityConfig(int(ResTable_config::DENSITY_XHIGH) - 1));
83 ASSERT_EQ(expectedBest, selectBest(deviceConfig, configs));
84
Adam Lesinski4c67a472016-11-10 16:43:59 -080085 expectedBest = buildDensityConfig(ResTable_config::DENSITY_XHIGH);
86 configs.add(expectedBest);
87 ASSERT_EQ(expectedBest, selectBest(deviceConfig, configs));
Adam Lesinski31245b42014-08-22 19:10:56 -070088
Adam Lesinski4c67a472016-11-10 16:43:59 -080089 expectedBest = buildDensityConfig(ResTable_config::DENSITY_ANY);
90 expectedBest.sdkVersion = 21;
91 configs.add(expectedBest);
92 ASSERT_EQ(expectedBest, selectBest(deviceConfig, configs));
Adam Lesinski31245b42014-08-22 19:10:56 -070093}
94
95TEST(ConfigTest, shouldSelectBestDensityWhenNoneSpecified) {
Adam Lesinski4c67a472016-11-10 16:43:59 -080096 ResTable_config deviceConfig;
97 memset(&deviceConfig, 0, sizeof(deviceConfig));
98 deviceConfig.sdkVersion = 21;
Adam Lesinski31245b42014-08-22 19:10:56 -070099
Adam Lesinski4c67a472016-11-10 16:43:59 -0800100 Vector<ResTable_config> configs;
101 configs.add(buildDensityConfig(ResTable_config::DENSITY_HIGH));
Adam Lesinski31245b42014-08-22 19:10:56 -0700102
Adam Lesinski4c67a472016-11-10 16:43:59 -0800103 ResTable_config expectedBest =
104 buildDensityConfig(ResTable_config::DENSITY_MEDIUM);
105 configs.add(expectedBest);
106 ASSERT_EQ(expectedBest, selectBest(deviceConfig, configs));
Adam Lesinski31245b42014-08-22 19:10:56 -0700107
Adam Lesinski4c67a472016-11-10 16:43:59 -0800108 expectedBest = buildDensityConfig(ResTable_config::DENSITY_ANY);
109 configs.add(expectedBest);
110 ASSERT_EQ(expectedBest, selectBest(deviceConfig, configs));
Adam Lesinski31245b42014-08-22 19:10:56 -0700111}
112
Adam Lesinski2738c962015-05-14 14:25:36 -0700113TEST(ConfigTest, shouldMatchRoundQualifier) {
Adam Lesinski4c67a472016-11-10 16:43:59 -0800114 ResTable_config deviceConfig;
115 memset(&deviceConfig, 0, sizeof(deviceConfig));
Adam Lesinski2738c962015-05-14 14:25:36 -0700116
Adam Lesinski4c67a472016-11-10 16:43:59 -0800117 ResTable_config roundConfig;
118 memset(&roundConfig, 0, sizeof(roundConfig));
119 roundConfig.screenLayout2 = ResTable_config::SCREENROUND_YES;
Adam Lesinski2738c962015-05-14 14:25:36 -0700120
Adam Lesinski4c67a472016-11-10 16:43:59 -0800121 EXPECT_FALSE(roundConfig.match(deviceConfig));
Adam Lesinski2738c962015-05-14 14:25:36 -0700122
Adam Lesinski4c67a472016-11-10 16:43:59 -0800123 deviceConfig.screenLayout2 = ResTable_config::SCREENROUND_YES;
Adam Lesinski2738c962015-05-14 14:25:36 -0700124
Adam Lesinski4c67a472016-11-10 16:43:59 -0800125 EXPECT_TRUE(roundConfig.match(deviceConfig));
Adam Lesinski2738c962015-05-14 14:25:36 -0700126
Adam Lesinski4c67a472016-11-10 16:43:59 -0800127 deviceConfig.screenLayout2 = ResTable_config::SCREENROUND_NO;
Adam Lesinski2738c962015-05-14 14:25:36 -0700128
Adam Lesinski4c67a472016-11-10 16:43:59 -0800129 EXPECT_FALSE(roundConfig.match(deviceConfig));
Adam Lesinski2738c962015-05-14 14:25:36 -0700130
Adam Lesinski4c67a472016-11-10 16:43:59 -0800131 ResTable_config notRoundConfig;
132 memset(&notRoundConfig, 0, sizeof(notRoundConfig));
133 notRoundConfig.screenLayout2 = ResTable_config::SCREENROUND_NO;
Adam Lesinski2738c962015-05-14 14:25:36 -0700134
Adam Lesinski4c67a472016-11-10 16:43:59 -0800135 EXPECT_TRUE(notRoundConfig.match(deviceConfig));
Adam Lesinski2738c962015-05-14 14:25:36 -0700136}
137
138TEST(ConfigTest, RoundQualifierShouldHaveStableSortOrder) {
Adam Lesinski4c67a472016-11-10 16:43:59 -0800139 ResTable_config defaultConfig;
140 memset(&defaultConfig, 0, sizeof(defaultConfig));
Adam Lesinski2738c962015-05-14 14:25:36 -0700141
Adam Lesinski4c67a472016-11-10 16:43:59 -0800142 ResTable_config longConfig = defaultConfig;
143 longConfig.screenLayout = ResTable_config::SCREENLONG_YES;
Adam Lesinski2738c962015-05-14 14:25:36 -0700144
Adam Lesinski4c67a472016-11-10 16:43:59 -0800145 ResTable_config longRoundConfig = longConfig;
146 longRoundConfig.screenLayout2 = ResTable_config::SCREENROUND_YES;
Adam Lesinski2738c962015-05-14 14:25:36 -0700147
Adam Lesinski4c67a472016-11-10 16:43:59 -0800148 ResTable_config longRoundPortConfig = longConfig;
149 longRoundPortConfig.orientation = ResTable_config::ORIENTATION_PORT;
Adam Lesinski2738c962015-05-14 14:25:36 -0700150
Adam Lesinski4c67a472016-11-10 16:43:59 -0800151 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 Lesinski2738c962015-05-14 14:25:36 -0700155
Adam Lesinski4c67a472016-11-10 16:43:59 -0800156 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 Lesinski2738c962015-05-14 14:25:36 -0700160}
161
162TEST(ConfigTest, ScreenShapeHasCorrectDiff) {
Adam Lesinski4c67a472016-11-10 16:43:59 -0800163 ResTable_config defaultConfig;
164 memset(&defaultConfig, 0, sizeof(defaultConfig));
Adam Lesinski2738c962015-05-14 14:25:36 -0700165
Adam Lesinski4c67a472016-11-10 16:43:59 -0800166 ResTable_config roundConfig = defaultConfig;
167 roundConfig.screenLayout2 = ResTable_config::SCREENROUND_YES;
Adam Lesinski2738c962015-05-14 14:25:36 -0700168
Adam Lesinski4c67a472016-11-10 16:43:59 -0800169 EXPECT_EQ(defaultConfig.diff(roundConfig),
170 ResTable_config::CONFIG_SCREEN_ROUND);
Adam Lesinski2738c962015-05-14 14:25:36 -0700171}
172
173TEST(ConfigTest, RoundIsMoreSpecific) {
Adam Lesinski4c67a472016-11-10 16:43:59 -0800174 ResTable_config deviceConfig;
175 memset(&deviceConfig, 0, sizeof(deviceConfig));
176 deviceConfig.screenLayout2 = ResTable_config::SCREENROUND_YES;
177 deviceConfig.screenLayout = ResTable_config::SCREENLONG_YES;
Adam Lesinski2738c962015-05-14 14:25:36 -0700178
Adam Lesinski4c67a472016-11-10 16:43:59 -0800179 ResTable_config targetConfigA;
180 memset(&targetConfigA, 0, sizeof(targetConfigA));
Adam Lesinski2738c962015-05-14 14:25:36 -0700181
Adam Lesinski4c67a472016-11-10 16:43:59 -0800182 ResTable_config targetConfigB = targetConfigA;
183 targetConfigB.screenLayout = ResTable_config::SCREENLONG_YES;
Adam Lesinski2738c962015-05-14 14:25:36 -0700184
Adam Lesinski4c67a472016-11-10 16:43:59 -0800185 ResTable_config targetConfigC = targetConfigB;
186 targetConfigC.screenLayout2 = ResTable_config::SCREENROUND_YES;
Adam Lesinski2738c962015-05-14 14:25:36 -0700187
Adam Lesinski4c67a472016-11-10 16:43:59 -0800188 EXPECT_TRUE(targetConfigB.isBetterThan(targetConfigA, &deviceConfig));
189 EXPECT_TRUE(targetConfigC.isBetterThan(targetConfigB, &deviceConfig));
Adam Lesinski2738c962015-05-14 14:25:36 -0700190}
191
Romain Guyc9ba5592017-01-18 16:34:42 -0800192TEST(ConfigTest, ScreenIsWideGamut) {
193 ResTable_config defaultConfig;
194 memset(&defaultConfig, 0, sizeof(defaultConfig));
195
196 ResTable_config wideGamutConfig = defaultConfig;
Romain Guy4832745b2017-01-23 17:03:35 -0800197 wideGamutConfig.colorMode = ResTable_config::WIDE_COLOR_GAMUT_YES;
Romain Guyc9ba5592017-01-18 16:34:42 -0800198
Romain Guy4832745b2017-01-23 17:03:35 -0800199 EXPECT_EQ(defaultConfig.diff(wideGamutConfig), ResTable_config::CONFIG_COLOR_MODE);
Romain Guyc9ba5592017-01-18 16:34:42 -0800200}
201
202TEST(ConfigTest, ScreenIsHdr) {
203 ResTable_config defaultConfig;
204 memset(&defaultConfig, 0, sizeof(defaultConfig));
205
206 ResTable_config hdrConfig = defaultConfig;
Romain Guy4832745b2017-01-23 17:03:35 -0800207 hdrConfig.colorMode = ResTable_config::HDR_YES;
Romain Guyc9ba5592017-01-18 16:34:42 -0800208
Romain Guy4832745b2017-01-23 17:03:35 -0800209 EXPECT_EQ(defaultConfig.diff(hdrConfig), ResTable_config::CONFIG_COLOR_MODE);
Romain Guyc9ba5592017-01-18 16:34:42 -0800210}
211
Adam Lesinski31245b42014-08-22 19:10:56 -0700212} // namespace android.