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