1 /*
2 * Copyright (C) 2016 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 "filter/ConfigFilter.h"
18 #include "test/Common.h"
19
20 #include <gtest/gtest.h>
21
22 namespace aapt {
23
TEST(ConfigFilterTest,EmptyFilterMatchesAnything)24 TEST(ConfigFilterTest, EmptyFilterMatchesAnything) {
25 AxisConfigFilter filter;
26
27 EXPECT_TRUE(filter.match(test::parseConfigOrDie("320dpi")));
28 EXPECT_TRUE(filter.match(test::parseConfigOrDie("fr")));
29 }
30
TEST(ConfigFilterTest,MatchesConfigWithUnrelatedAxis)31 TEST(ConfigFilterTest, MatchesConfigWithUnrelatedAxis) {
32 AxisConfigFilter filter;
33 filter.addConfig(test::parseConfigOrDie("fr"));
34
35 EXPECT_TRUE(filter.match(test::parseConfigOrDie("320dpi")));
36 }
37
TEST(ConfigFilterTest,MatchesConfigWithSameValueAxis)38 TEST(ConfigFilterTest, MatchesConfigWithSameValueAxis) {
39 AxisConfigFilter filter;
40 filter.addConfig(test::parseConfigOrDie("fr"));
41
42 EXPECT_TRUE(filter.match(test::parseConfigOrDie("fr")));
43 }
44
TEST(ConfigFilterTest,MatchesConfigWithSameValueAxisAndOtherUnrelatedAxis)45 TEST(ConfigFilterTest, MatchesConfigWithSameValueAxisAndOtherUnrelatedAxis) {
46 AxisConfigFilter filter;
47 filter.addConfig(test::parseConfigOrDie("fr"));
48
49 EXPECT_TRUE(filter.match(test::parseConfigOrDie("fr-320dpi")));
50 }
51
TEST(ConfigFilterTest,MatchesConfigWithOneMatchingAxis)52 TEST(ConfigFilterTest, MatchesConfigWithOneMatchingAxis) {
53 AxisConfigFilter filter;
54 filter.addConfig(test::parseConfigOrDie("fr-rFR"));
55 filter.addConfig(test::parseConfigOrDie("sw360dp"));
56 filter.addConfig(test::parseConfigOrDie("normal"));
57 filter.addConfig(test::parseConfigOrDie("en-rUS"));
58
59 EXPECT_TRUE(filter.match(test::parseConfigOrDie("en")));
60 }
61
TEST(ConfigFilterTest,DoesNotMatchConfigWithDifferentValueAxis)62 TEST(ConfigFilterTest, DoesNotMatchConfigWithDifferentValueAxis) {
63 AxisConfigFilter filter;
64 filter.addConfig(test::parseConfigOrDie("fr"));
65
66 EXPECT_FALSE(filter.match(test::parseConfigOrDie("de")));
67 }
68
TEST(ConfigFilterTest,DoesNotMatchWhenOneQualifierIsExplicitlyNotMatched)69 TEST(ConfigFilterTest, DoesNotMatchWhenOneQualifierIsExplicitlyNotMatched) {
70 AxisConfigFilter filter;
71 filter.addConfig(test::parseConfigOrDie("fr-rFR"));
72 filter.addConfig(test::parseConfigOrDie("en-rUS"));
73 filter.addConfig(test::parseConfigOrDie("normal"));
74 filter.addConfig(test::parseConfigOrDie("large"));
75 filter.addConfig(test::parseConfigOrDie("xxhdpi"));
76 filter.addConfig(test::parseConfigOrDie("sw320dp"));
77
78 EXPECT_FALSE(filter.match(test::parseConfigOrDie("fr-sw600dp-v13")));
79 }
80
TEST(ConfigFilterTest,MatchesSmallestWidthWhenSmaller)81 TEST(ConfigFilterTest, MatchesSmallestWidthWhenSmaller) {
82 AxisConfigFilter filter;
83 filter.addConfig(test::parseConfigOrDie("sw600dp"));
84
85 EXPECT_TRUE(filter.match(test::parseConfigOrDie("fr-sw320dp-v13")));
86 }
87
TEST(ConfigFilterTest,MatchesConfigWithSameLanguageButNoRegionSpecified)88 TEST(ConfigFilterTest, MatchesConfigWithSameLanguageButNoRegionSpecified) {
89 AxisConfigFilter filter;
90 filter.addConfig(test::parseConfigOrDie("de-rDE"));
91
92 EXPECT_TRUE(filter.match(test::parseConfigOrDie("de")));
93 }
94
TEST(ConfigFilterTest,IgnoresVersion)95 TEST(ConfigFilterTest, IgnoresVersion) {
96 AxisConfigFilter filter;
97 filter.addConfig(test::parseConfigOrDie("normal-v4"));
98
99 // The configs don't match on any axis besides version, which should be ignored.
100 EXPECT_TRUE(filter.match(test::parseConfigOrDie("sw600dp-v13")));
101 }
102
TEST(ConfigFilterTest,MatchesConfigWithRegion)103 TEST(ConfigFilterTest, MatchesConfigWithRegion) {
104 AxisConfigFilter filter;
105 filter.addConfig(test::parseConfigOrDie("kok"));
106 filter.addConfig(test::parseConfigOrDie("kok-rIN"));
107 filter.addConfig(test::parseConfigOrDie("kok-v419"));
108
109 EXPECT_TRUE(filter.match(test::parseConfigOrDie("kok-rIN")));
110 }
111
112 } // namespace aapt
113