blob: 30697bb52125042c5d64d1f346e463dd65cb71c8 [file] [log] [blame]
Adam Lesinskifab50872014-04-16 14:40:42 -07001/*
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, DoesNotMatchConfigWithDifferentValueAxis) {
79 WeakResourceFilter filter;
80 ASSERT_EQ(NO_ERROR, filter.parse(String8("fr")));
81
82 ConfigDescription config;
83 config.language[0] = 'd';
84 config.language[1] = 'e';
85
86 EXPECT_FALSE(filter.match(config));
87}
88
89TEST(WeakResourceFilterTest, MatchesConfigWithSameLanguageButNoRegionSpecified) {
90 WeakResourceFilter filter;
91 ASSERT_EQ(NO_ERROR, filter.parse(String8("de-rDE")));
92
93 ConfigDescription config;
94 config.language[0] = 'd';
95 config.language[1] = 'e';
96
97 EXPECT_TRUE(filter.match(config));
98}
99
100TEST(WeakResourceFilterTest, ParsesStandardLocaleOnlyString) {
101 WeakResourceFilter filter;
102 EXPECT_EQ(NO_ERROR, filter.parse(String8("de_DE")));
103}
104
105TEST(WeakResourceFilterTest, IgnoresVersion) {
106 WeakResourceFilter filter;
107 ASSERT_EQ(NO_ERROR, filter.parse(String8("normal-v4")));
108
109 ConfigDescription config;
110 config.smallestScreenWidthDp = 600;
111 config.version = 13;
112
113 // The configs don't match on any axis besides version, which should be ignored.
114 EXPECT_TRUE(filter.match(config));
115}
116
117TEST(WeakResourceFilterTest, MatchesConfigWithRegion) {
118 WeakResourceFilter filter;
119 ASSERT_EQ(NO_ERROR, filter.parse(String8("kok,kok_IN,kok_419")));
120
121 ConfigDescription config;
122 AaptLocaleValue val;
123 ASSERT_TRUE(val.initFromFilterString(String8("kok_IN")));
124 val.writeTo(&config);
125
126 EXPECT_TRUE(filter.match(config));
127}
128