ResourceFilter_test.cpp revision a5cc002bfe7ca8a6536549189b44e7143c8611dc
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/String8.h>
19#include <gtest/gtest.h>
20
21#include "AaptConfig.h"
22#include "ResourceFilter.h"
23#include "ConfigDescription.h"
24
25using android::String8;
26
27// In this context, 'Axis' represents a particular field in the configuration,
28// such as language or density.
29
30TEST(WeakResourceFilterTest, EmptyFilterMatchesAnything) {
31    WeakResourceFilter filter;
32    ASSERT_EQ(NO_ERROR, filter.parse(String8("")));
33
34    ConfigDescription config;
35    config.density = 320;
36
37    EXPECT_TRUE(filter.match(config));
38
39    config.language[0] = 'f';
40    config.language[1] = 'r';
41
42    EXPECT_TRUE(filter.match(config));
43}
44
45TEST(WeakResourceFilterTest, MatchesConfigWithUnrelatedAxis) {
46    WeakResourceFilter filter;
47    ASSERT_EQ(NO_ERROR, filter.parse(String8("fr")));
48
49    ConfigDescription config;
50    config.density = 320;
51
52    EXPECT_TRUE(filter.match(config));
53}
54
55TEST(WeakResourceFilterTest, MatchesConfigWithSameValueAxis) {
56    WeakResourceFilter filter;
57    ASSERT_EQ(NO_ERROR, filter.parse(String8("fr")));
58
59    ConfigDescription config;
60    config.language[0] = 'f';
61    config.language[1] = 'r';
62
63    EXPECT_TRUE(filter.match(config));
64}
65
66TEST(WeakResourceFilterTest, MatchesConfigWithSameValueAxisAndOtherUnrelatedAxis) {
67    WeakResourceFilter filter;
68    ASSERT_EQ(NO_ERROR, filter.parse(String8("fr")));
69
70    ConfigDescription config;
71    config.language[0] = 'f';
72    config.language[1] = 'r';
73    config.density = 320;
74
75    EXPECT_TRUE(filter.match(config));
76}
77
78TEST(WeakResourceFilterTest, MatchesConfigWithOneMatchingAxis) {
79    WeakResourceFilter filter;
80    ASSERT_EQ(NO_ERROR, filter.parse(String8("fr_FR,sw360dp,normal,en_US")));
81
82    ConfigDescription config;
83    config.language[0] = 'e';
84    config.language[1] = 'n';
85
86    EXPECT_TRUE(filter.match(config));
87}
88
89TEST(WeakResourceFilterTest, DoesNotMatchConfigWithDifferentValueAxis) {
90    WeakResourceFilter filter;
91    ASSERT_EQ(NO_ERROR, filter.parse(String8("fr")));
92
93    ConfigDescription config;
94    config.language[0] = 'd';
95    config.language[1] = 'e';
96
97    EXPECT_FALSE(filter.match(config));
98}
99
100TEST(WeakResourceFilterTest, DoesNotMatchWhenOneQualifierIsExplicitlyNotMatched) {
101    WeakResourceFilter filter;
102    ASSERT_EQ(NO_ERROR, filter.parse(String8("fr_FR,en_US,normal,large,xxhdpi,sw320dp")));
103
104    ConfigDescription config;
105    config.language[0] = 'f';
106    config.language[1] = 'r';
107    config.smallestScreenWidthDp = 600;
108    config.version = 13;
109
110    EXPECT_FALSE(filter.match(config));
111}
112
113TEST(WeakResourceFilterTest, MatchesSmallestWidthWhenSmaller) {
114    WeakResourceFilter filter;
115    ASSERT_EQ(NO_ERROR, filter.parse(String8("sw600dp")));
116
117    ConfigDescription config;
118    config.language[0] = 'f';
119    config.language[1] = 'r';
120    config.smallestScreenWidthDp = 320;
121    config.version = 13;
122
123    EXPECT_TRUE(filter.match(config));
124}
125
126TEST(WeakResourceFilterTest, MatchesConfigWithSameLanguageButNoRegionSpecified) {
127    WeakResourceFilter filter;
128    ASSERT_EQ(NO_ERROR, filter.parse(String8("de-rDE")));
129
130    ConfigDescription config;
131    config.language[0] = 'd';
132    config.language[1] = 'e';
133
134    EXPECT_TRUE(filter.match(config));
135}
136
137TEST(WeakResourceFilterTest, ParsesStandardLocaleOnlyString) {
138    WeakResourceFilter filter;
139    EXPECT_EQ(NO_ERROR, filter.parse(String8("de_DE")));
140}
141
142TEST(WeakResourceFilterTest, IgnoresVersion) {
143    WeakResourceFilter filter;
144    ASSERT_EQ(NO_ERROR, filter.parse(String8("normal-v4")));
145
146    ConfigDescription config;
147    config.smallestScreenWidthDp = 600;
148    config.version = 13;
149
150    // The configs don't match on any axis besides version, which should be ignored.
151    EXPECT_TRUE(filter.match(config));
152}
153
154TEST(WeakResourceFilterTest, MatchesConfigWithRegion) {
155    WeakResourceFilter filter;
156    ASSERT_EQ(NO_ERROR, filter.parse(String8("kok,kok_IN,kok_419")));
157
158    ConfigDescription config;
159    AaptLocaleValue val;
160    ASSERT_TRUE(val.initFromFilterString(String8("kok_IN")));
161    val.writeTo(&config);
162
163    EXPECT_TRUE(filter.match(config));
164}
165
166TEST(StrongResourceFilterTest, MatchesDensities) {
167    ConfigDescription config;
168    config.density = 160;
169    config.version = 4;
170    std::set<ConfigDescription> configs;
171    configs.insert(config);
172
173    StrongResourceFilter filter(configs);
174
175    ConfigDescription expectedConfig;
176    expectedConfig.density = 160;
177    expectedConfig.version = 4;
178    ASSERT_TRUE(filter.match(expectedConfig));
179}
180
181TEST(StrongResourceFilterTest, MatchOnlyMdpiAndExcludeAllOthers) {
182    std::set<ConfigDescription> configsToMatch;
183    ConfigDescription config;
184    config.density = 160;
185    config.version = 4;
186    configsToMatch.insert(config);
187
188    std::set<ConfigDescription> configsToNotMatch;
189    config.density = 480;
190    configsToNotMatch.insert(config);
191
192    AndResourceFilter filter;
193    filter.addFilter(new InverseResourceFilter(new StrongResourceFilter(configsToNotMatch)));
194    filter.addFilter(new StrongResourceFilter(configsToMatch));
195
196    ConfigDescription expectedConfig;
197    expectedConfig.density = 160;
198    expectedConfig.version = 4;
199    ASSERT_TRUE(filter.match(expectedConfig));
200}
201