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
103 } // namespace android.
104