blob: b54915f03c29987af6a095b6733c4ad79b569894 [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) {
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 Lesinski31245b42014-08-22 19:10:56 -070037 }
Adam Lesinski4c67a472016-11-10 16:43:59 -080038
39 if (thisConfig.isBetterThan(bestConfig, &target)) {
40 bestConfig = thisConfig;
41 }
42 }
43 return bestConfig;
Adam Lesinski31245b42014-08-22 19:10:56 -070044}
45
46static ResTable_config buildDensityConfig(int density) {
Adam Lesinski4c67a472016-11-10 16:43:59 -080047 ResTable_config config;
48 memset(&config, 0, sizeof(config));
49 config.density = uint16_t(density);
50 config.sdkVersion = 4;
51 return config;
Adam Lesinski31245b42014-08-22 19:10:56 -070052}
53
54TEST(ConfigTest, shouldSelectBestDensity) {
Adam Lesinski4c67a472016-11-10 16:43:59 -080055 ResTable_config deviceConfig;
56 memset(&deviceConfig, 0, sizeof(deviceConfig));
57 deviceConfig.density = ResTable_config::DENSITY_XHIGH;
58 deviceConfig.sdkVersion = 21;
Adam Lesinski31245b42014-08-22 19:10:56 -070059
Adam Lesinski4c67a472016-11-10 16:43:59 -080060 Vector<ResTable_config> configs;
Adam Lesinski31245b42014-08-22 19:10:56 -070061
Adam Lesinski4c67a472016-11-10 16:43:59 -080062 ResTable_config expectedBest =
63 buildDensityConfig(ResTable_config::DENSITY_HIGH);
64 configs.add(expectedBest);
65 ASSERT_EQ(expectedBest, selectBest(deviceConfig, configs));
Adam Lesinski31245b42014-08-22 19:10:56 -070066
Adam Lesinski4c67a472016-11-10 16:43:59 -080067 expectedBest = buildDensityConfig(ResTable_config::DENSITY_XXHIGH);
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(int(ResTable_config::DENSITY_XXHIGH) - 20);
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 configs.add(buildDensityConfig(int(ResTable_config::DENSITY_HIGH) + 20));
76 ASSERT_EQ(expectedBest, selectBest(deviceConfig, configs));
Adam Lesinski31245b42014-08-22 19:10:56 -070077
Adam Lesinski4c67a472016-11-10 16:43:59 -080078 expectedBest = buildDensityConfig(ResTable_config::DENSITY_XHIGH);
79 configs.add(expectedBest);
80 ASSERT_EQ(expectedBest, selectBest(deviceConfig, configs));
Adam Lesinski31245b42014-08-22 19:10:56 -070081
Adam Lesinski4c67a472016-11-10 16:43:59 -080082 expectedBest = buildDensityConfig(ResTable_config::DENSITY_ANY);
83 expectedBest.sdkVersion = 21;
84 configs.add(expectedBest);
85 ASSERT_EQ(expectedBest, selectBest(deviceConfig, configs));
Adam Lesinski31245b42014-08-22 19:10:56 -070086}
87
88TEST(ConfigTest, shouldSelectBestDensityWhenNoneSpecified) {
Adam Lesinski4c67a472016-11-10 16:43:59 -080089 ResTable_config deviceConfig;
90 memset(&deviceConfig, 0, sizeof(deviceConfig));
91 deviceConfig.sdkVersion = 21;
Adam Lesinski31245b42014-08-22 19:10:56 -070092
Adam Lesinski4c67a472016-11-10 16:43:59 -080093 Vector<ResTable_config> configs;
94 configs.add(buildDensityConfig(ResTable_config::DENSITY_HIGH));
Adam Lesinski31245b42014-08-22 19:10:56 -070095
Adam Lesinski4c67a472016-11-10 16:43:59 -080096 ResTable_config expectedBest =
97 buildDensityConfig(ResTable_config::DENSITY_MEDIUM);
98 configs.add(expectedBest);
99 ASSERT_EQ(expectedBest, selectBest(deviceConfig, configs));
Adam Lesinski31245b42014-08-22 19:10:56 -0700100
Adam Lesinski4c67a472016-11-10 16:43:59 -0800101 expectedBest = buildDensityConfig(ResTable_config::DENSITY_ANY);
102 configs.add(expectedBest);
103 ASSERT_EQ(expectedBest, selectBest(deviceConfig, configs));
Adam Lesinski31245b42014-08-22 19:10:56 -0700104}
105
Adam Lesinski2738c962015-05-14 14:25:36 -0700106TEST(ConfigTest, shouldMatchRoundQualifier) {
Adam Lesinski4c67a472016-11-10 16:43:59 -0800107 ResTable_config deviceConfig;
108 memset(&deviceConfig, 0, sizeof(deviceConfig));
Adam Lesinski2738c962015-05-14 14:25:36 -0700109
Adam Lesinski4c67a472016-11-10 16:43:59 -0800110 ResTable_config roundConfig;
111 memset(&roundConfig, 0, sizeof(roundConfig));
112 roundConfig.screenLayout2 = ResTable_config::SCREENROUND_YES;
Adam Lesinski2738c962015-05-14 14:25:36 -0700113
Adam Lesinski4c67a472016-11-10 16:43:59 -0800114 EXPECT_FALSE(roundConfig.match(deviceConfig));
Adam Lesinski2738c962015-05-14 14:25:36 -0700115
Adam Lesinski4c67a472016-11-10 16:43:59 -0800116 deviceConfig.screenLayout2 = ResTable_config::SCREENROUND_YES;
Adam Lesinski2738c962015-05-14 14:25:36 -0700117
Adam Lesinski4c67a472016-11-10 16:43:59 -0800118 EXPECT_TRUE(roundConfig.match(deviceConfig));
Adam Lesinski2738c962015-05-14 14:25:36 -0700119
Adam Lesinski4c67a472016-11-10 16:43:59 -0800120 deviceConfig.screenLayout2 = ResTable_config::SCREENROUND_NO;
Adam Lesinski2738c962015-05-14 14:25:36 -0700121
Adam Lesinski4c67a472016-11-10 16:43:59 -0800122 EXPECT_FALSE(roundConfig.match(deviceConfig));
Adam Lesinski2738c962015-05-14 14:25:36 -0700123
Adam Lesinski4c67a472016-11-10 16:43:59 -0800124 ResTable_config notRoundConfig;
125 memset(&notRoundConfig, 0, sizeof(notRoundConfig));
126 notRoundConfig.screenLayout2 = ResTable_config::SCREENROUND_NO;
Adam Lesinski2738c962015-05-14 14:25:36 -0700127
Adam Lesinski4c67a472016-11-10 16:43:59 -0800128 EXPECT_TRUE(notRoundConfig.match(deviceConfig));
Adam Lesinski2738c962015-05-14 14:25:36 -0700129}
130
131TEST(ConfigTest, RoundQualifierShouldHaveStableSortOrder) {
Adam Lesinski4c67a472016-11-10 16:43:59 -0800132 ResTable_config defaultConfig;
133 memset(&defaultConfig, 0, sizeof(defaultConfig));
Adam Lesinski2738c962015-05-14 14:25:36 -0700134
Adam Lesinski4c67a472016-11-10 16:43:59 -0800135 ResTable_config longConfig = defaultConfig;
136 longConfig.screenLayout = ResTable_config::SCREENLONG_YES;
Adam Lesinski2738c962015-05-14 14:25:36 -0700137
Adam Lesinski4c67a472016-11-10 16:43:59 -0800138 ResTable_config longRoundConfig = longConfig;
139 longRoundConfig.screenLayout2 = ResTable_config::SCREENROUND_YES;
Adam Lesinski2738c962015-05-14 14:25:36 -0700140
Adam Lesinski4c67a472016-11-10 16:43:59 -0800141 ResTable_config longRoundPortConfig = longConfig;
142 longRoundPortConfig.orientation = ResTable_config::ORIENTATION_PORT;
Adam Lesinski2738c962015-05-14 14:25:36 -0700143
Adam Lesinski4c67a472016-11-10 16:43:59 -0800144 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 Lesinski2738c962015-05-14 14:25:36 -0700148
Adam Lesinski4c67a472016-11-10 16:43:59 -0800149 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 Lesinski2738c962015-05-14 14:25:36 -0700153}
154
155TEST(ConfigTest, ScreenShapeHasCorrectDiff) {
Adam Lesinski4c67a472016-11-10 16:43:59 -0800156 ResTable_config defaultConfig;
157 memset(&defaultConfig, 0, sizeof(defaultConfig));
Adam Lesinski2738c962015-05-14 14:25:36 -0700158
Adam Lesinski4c67a472016-11-10 16:43:59 -0800159 ResTable_config roundConfig = defaultConfig;
160 roundConfig.screenLayout2 = ResTable_config::SCREENROUND_YES;
Adam Lesinski2738c962015-05-14 14:25:36 -0700161
Adam Lesinski4c67a472016-11-10 16:43:59 -0800162 EXPECT_EQ(defaultConfig.diff(roundConfig),
163 ResTable_config::CONFIG_SCREEN_ROUND);
Adam Lesinski2738c962015-05-14 14:25:36 -0700164}
165
166TEST(ConfigTest, RoundIsMoreSpecific) {
Adam Lesinski4c67a472016-11-10 16:43:59 -0800167 ResTable_config deviceConfig;
168 memset(&deviceConfig, 0, sizeof(deviceConfig));
169 deviceConfig.screenLayout2 = ResTable_config::SCREENROUND_YES;
170 deviceConfig.screenLayout = ResTable_config::SCREENLONG_YES;
Adam Lesinski2738c962015-05-14 14:25:36 -0700171
Adam Lesinski4c67a472016-11-10 16:43:59 -0800172 ResTable_config targetConfigA;
173 memset(&targetConfigA, 0, sizeof(targetConfigA));
Adam Lesinski2738c962015-05-14 14:25:36 -0700174
Adam Lesinski4c67a472016-11-10 16:43:59 -0800175 ResTable_config targetConfigB = targetConfigA;
176 targetConfigB.screenLayout = ResTable_config::SCREENLONG_YES;
Adam Lesinski2738c962015-05-14 14:25:36 -0700177
Adam Lesinski4c67a472016-11-10 16:43:59 -0800178 ResTable_config targetConfigC = targetConfigB;
179 targetConfigC.screenLayout2 = ResTable_config::SCREENROUND_YES;
Adam Lesinski2738c962015-05-14 14:25:36 -0700180
Adam Lesinski4c67a472016-11-10 16:43:59 -0800181 EXPECT_TRUE(targetConfigB.isBetterThan(targetConfigA, &deviceConfig));
182 EXPECT_TRUE(targetConfigC.isBetterThan(targetConfigB, &deviceConfig));
Adam Lesinski2738c962015-05-14 14:25:36 -0700183}
184
Romain Guyc9ba5592017-01-18 16:34:42 -0800185TEST(ConfigTest, ScreenIsWideGamut) {
186 ResTable_config defaultConfig;
187 memset(&defaultConfig, 0, sizeof(defaultConfig));
188
189 ResTable_config wideGamutConfig = defaultConfig;
Romain Guy48327452017-01-23 17:03:35 -0800190 wideGamutConfig.colorMode = ResTable_config::WIDE_COLOR_GAMUT_YES;
Romain Guyc9ba5592017-01-18 16:34:42 -0800191
Romain Guy48327452017-01-23 17:03:35 -0800192 EXPECT_EQ(defaultConfig.diff(wideGamutConfig), ResTable_config::CONFIG_COLOR_MODE);
Romain Guyc9ba5592017-01-18 16:34:42 -0800193}
194
195TEST(ConfigTest, ScreenIsHdr) {
196 ResTable_config defaultConfig;
197 memset(&defaultConfig, 0, sizeof(defaultConfig));
198
199 ResTable_config hdrConfig = defaultConfig;
Romain Guy48327452017-01-23 17:03:35 -0800200 hdrConfig.colorMode = ResTable_config::HDR_YES;
Romain Guyc9ba5592017-01-18 16:34:42 -0800201
Romain Guy48327452017-01-23 17:03:35 -0800202 EXPECT_EQ(defaultConfig.diff(hdrConfig), ResTable_config::CONFIG_COLOR_MODE);
Romain Guyc9ba5592017-01-18 16:34:42 -0800203}
204
Adam Lesinski31245b42014-08-22 19:10:56 -0700205} // namespace android.