1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
|
/*
* Copyright (C) 2014 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "androidfw/ResourceTypes.h"
#include "utils/Log.h"
#include "utils/String8.h"
#include "utils/Vector.h"
#include "TestHelpers.h"
#include "gtest/gtest.h"
namespace android {
static ResTable_config selectBest(const ResTable_config& target,
const Vector<ResTable_config>& configs) {
ResTable_config bestConfig;
memset(&bestConfig, 0, sizeof(bestConfig));
const size_t configCount = configs.size();
for (size_t i = 0; i < configCount; i++) {
const ResTable_config& thisConfig = configs[i];
if (!thisConfig.match(target)) {
continue;
}
if (thisConfig.isBetterThan(bestConfig, &target)) {
bestConfig = thisConfig;
}
}
return bestConfig;
}
static ResTable_config buildDensityConfig(int density) {
ResTable_config config;
memset(&config, 0, sizeof(config));
config.density = uint16_t(density);
config.sdkVersion = 4;
return config;
}
TEST(ConfigTest, shouldSelectBestDensity) {
ResTable_config deviceConfig;
memset(&deviceConfig, 0, sizeof(deviceConfig));
deviceConfig.density = ResTable_config::DENSITY_XHIGH;
deviceConfig.sdkVersion = 21;
Vector<ResTable_config> configs;
ResTable_config expectedBest =
buildDensityConfig(ResTable_config::DENSITY_HIGH);
configs.add(expectedBest);
ASSERT_EQ(expectedBest, selectBest(deviceConfig, configs));
expectedBest = buildDensityConfig(ResTable_config::DENSITY_XXHIGH);
configs.add(expectedBest);
ASSERT_EQ(expectedBest, selectBest(deviceConfig, configs));
expectedBest = buildDensityConfig(int(ResTable_config::DENSITY_XXHIGH) - 20);
configs.add(expectedBest);
ASSERT_EQ(expectedBest, selectBest(deviceConfig, configs));
configs.add(buildDensityConfig(int(ResTable_config::DENSITY_HIGH) + 20));
ASSERT_EQ(expectedBest, selectBest(deviceConfig, configs));
configs.add(buildDensityConfig(int(ResTable_config::DENSITY_XHIGH) - 1));
ASSERT_EQ(expectedBest, selectBest(deviceConfig, configs));
expectedBest = buildDensityConfig(ResTable_config::DENSITY_XHIGH);
configs.add(expectedBest);
ASSERT_EQ(expectedBest, selectBest(deviceConfig, configs));
expectedBest = buildDensityConfig(ResTable_config::DENSITY_ANY);
expectedBest.sdkVersion = 21;
configs.add(expectedBest);
ASSERT_EQ(expectedBest, selectBest(deviceConfig, configs));
}
TEST(ConfigTest, shouldSelectBestDensityWhenNoneSpecified) {
ResTable_config deviceConfig;
memset(&deviceConfig, 0, sizeof(deviceConfig));
deviceConfig.sdkVersion = 21;
Vector<ResTable_config> configs;
configs.add(buildDensityConfig(ResTable_config::DENSITY_HIGH));
ResTable_config expectedBest =
buildDensityConfig(ResTable_config::DENSITY_MEDIUM);
configs.add(expectedBest);
ASSERT_EQ(expectedBest, selectBest(deviceConfig, configs));
expectedBest = buildDensityConfig(ResTable_config::DENSITY_ANY);
configs.add(expectedBest);
ASSERT_EQ(expectedBest, selectBest(deviceConfig, configs));
}
TEST(ConfigTest, shouldMatchRoundQualifier) {
ResTable_config deviceConfig;
memset(&deviceConfig, 0, sizeof(deviceConfig));
ResTable_config roundConfig;
memset(&roundConfig, 0, sizeof(roundConfig));
roundConfig.screenLayout2 = ResTable_config::SCREENROUND_YES;
EXPECT_FALSE(roundConfig.match(deviceConfig));
deviceConfig.screenLayout2 = ResTable_config::SCREENROUND_YES;
EXPECT_TRUE(roundConfig.match(deviceConfig));
deviceConfig.screenLayout2 = ResTable_config::SCREENROUND_NO;
EXPECT_FALSE(roundConfig.match(deviceConfig));
ResTable_config notRoundConfig;
memset(¬RoundConfig, 0, sizeof(notRoundConfig));
notRoundConfig.screenLayout2 = ResTable_config::SCREENROUND_NO;
EXPECT_TRUE(notRoundConfig.match(deviceConfig));
}
TEST(ConfigTest, RoundQualifierShouldHaveStableSortOrder) {
ResTable_config defaultConfig;
memset(&defaultConfig, 0, sizeof(defaultConfig));
ResTable_config longConfig = defaultConfig;
longConfig.screenLayout = ResTable_config::SCREENLONG_YES;
ResTable_config longRoundConfig = longConfig;
longRoundConfig.screenLayout2 = ResTable_config::SCREENROUND_YES;
ResTable_config longRoundPortConfig = longConfig;
longRoundPortConfig.orientation = ResTable_config::ORIENTATION_PORT;
EXPECT_TRUE(longConfig.compare(longRoundConfig) < 0);
EXPECT_TRUE(longConfig.compareLogical(longRoundConfig) < 0);
EXPECT_TRUE(longRoundConfig.compare(longConfig) > 0);
EXPECT_TRUE(longRoundConfig.compareLogical(longConfig) > 0);
EXPECT_TRUE(longRoundConfig.compare(longRoundPortConfig) < 0);
EXPECT_TRUE(longRoundConfig.compareLogical(longRoundPortConfig) < 0);
EXPECT_TRUE(longRoundPortConfig.compare(longRoundConfig) > 0);
EXPECT_TRUE(longRoundPortConfig.compareLogical(longRoundConfig) > 0);
}
TEST(ConfigTest, ScreenShapeHasCorrectDiff) {
ResTable_config defaultConfig;
memset(&defaultConfig, 0, sizeof(defaultConfig));
ResTable_config roundConfig = defaultConfig;
roundConfig.screenLayout2 = ResTable_config::SCREENROUND_YES;
EXPECT_EQ(defaultConfig.diff(roundConfig),
ResTable_config::CONFIG_SCREEN_ROUND);
}
TEST(ConfigTest, RoundIsMoreSpecific) {
ResTable_config deviceConfig;
memset(&deviceConfig, 0, sizeof(deviceConfig));
deviceConfig.screenLayout2 = ResTable_config::SCREENROUND_YES;
deviceConfig.screenLayout = ResTable_config::SCREENLONG_YES;
ResTable_config targetConfigA;
memset(&targetConfigA, 0, sizeof(targetConfigA));
ResTable_config targetConfigB = targetConfigA;
targetConfigB.screenLayout = ResTable_config::SCREENLONG_YES;
ResTable_config targetConfigC = targetConfigB;
targetConfigC.screenLayout2 = ResTable_config::SCREENROUND_YES;
EXPECT_TRUE(targetConfigB.isBetterThan(targetConfigA, &deviceConfig));
EXPECT_TRUE(targetConfigC.isBetterThan(targetConfigB, &deviceConfig));
}
TEST(ConfigTest, ScreenIsWideGamut) {
ResTable_config defaultConfig;
memset(&defaultConfig, 0, sizeof(defaultConfig));
ResTable_config wideGamutConfig = defaultConfig;
wideGamutConfig.colorMode = ResTable_config::WIDE_COLOR_GAMUT_YES;
EXPECT_EQ(defaultConfig.diff(wideGamutConfig), ResTable_config::CONFIG_COLOR_MODE);
}
TEST(ConfigTest, ScreenIsHdr) {
ResTable_config defaultConfig;
memset(&defaultConfig, 0, sizeof(defaultConfig));
ResTable_config hdrConfig = defaultConfig;
hdrConfig.colorMode = ResTable_config::HDR_YES;
EXPECT_EQ(defaultConfig.diff(hdrConfig), ResTable_config::CONFIG_COLOR_MODE);
}
} // namespace android.
|