summaryrefslogtreecommitdiffstats
path: root/libs/androidfw/tests/Config_test.cpp
blob: ef30df46d36c9175846653a4e630193ec052c9a3 (plain)
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
/*
 * 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));

    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));
}

}  // namespace android.