Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 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" |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 18 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 19 | #include "test/Test.h" |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 20 | |
| 21 | namespace aapt { |
| 22 | |
| 23 | TEST(ConfigFilterTest, EmptyFilterMatchesAnything) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 24 | AxisConfigFilter filter; |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 25 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 26 | EXPECT_TRUE(filter.Match(test::ParseConfigOrDie("320dpi"))); |
| 27 | EXPECT_TRUE(filter.Match(test::ParseConfigOrDie("fr"))); |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 28 | } |
| 29 | |
| 30 | TEST(ConfigFilterTest, MatchesConfigWithUnrelatedAxis) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 31 | AxisConfigFilter filter; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 32 | filter.AddConfig(test::ParseConfigOrDie("fr")); |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 33 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 34 | EXPECT_TRUE(filter.Match(test::ParseConfigOrDie("320dpi"))); |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | TEST(ConfigFilterTest, MatchesConfigWithSameValueAxis) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 38 | AxisConfigFilter filter; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 39 | filter.AddConfig(test::ParseConfigOrDie("fr")); |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 40 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 41 | EXPECT_TRUE(filter.Match(test::ParseConfigOrDie("fr"))); |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | TEST(ConfigFilterTest, MatchesConfigWithSameValueAxisAndOtherUnrelatedAxis) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 45 | AxisConfigFilter filter; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 46 | filter.AddConfig(test::ParseConfigOrDie("fr")); |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 47 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 48 | EXPECT_TRUE(filter.Match(test::ParseConfigOrDie("fr-320dpi"))); |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | TEST(ConfigFilterTest, MatchesConfigWithOneMatchingAxis) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 52 | AxisConfigFilter filter; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 53 | filter.AddConfig(test::ParseConfigOrDie("fr-rFR")); |
| 54 | filter.AddConfig(test::ParseConfigOrDie("sw360dp")); |
| 55 | filter.AddConfig(test::ParseConfigOrDie("normal")); |
| 56 | filter.AddConfig(test::ParseConfigOrDie("en-rUS")); |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 57 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 58 | EXPECT_TRUE(filter.Match(test::ParseConfigOrDie("en"))); |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | TEST(ConfigFilterTest, DoesNotMatchConfigWithDifferentValueAxis) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 62 | AxisConfigFilter filter; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 63 | filter.AddConfig(test::ParseConfigOrDie("fr")); |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 64 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 65 | EXPECT_FALSE(filter.Match(test::ParseConfigOrDie("de"))); |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | TEST(ConfigFilterTest, DoesNotMatchWhenOneQualifierIsExplicitlyNotMatched) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 69 | AxisConfigFilter filter; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 70 | filter.AddConfig(test::ParseConfigOrDie("fr-rFR")); |
| 71 | filter.AddConfig(test::ParseConfigOrDie("en-rUS")); |
| 72 | filter.AddConfig(test::ParseConfigOrDie("normal")); |
| 73 | filter.AddConfig(test::ParseConfigOrDie("large")); |
| 74 | filter.AddConfig(test::ParseConfigOrDie("xxhdpi")); |
| 75 | filter.AddConfig(test::ParseConfigOrDie("sw320dp")); |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 76 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 77 | EXPECT_FALSE(filter.Match(test::ParseConfigOrDie("fr-sw600dp-v13"))); |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | TEST(ConfigFilterTest, MatchesSmallestWidthWhenSmaller) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 81 | AxisConfigFilter filter; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 82 | filter.AddConfig(test::ParseConfigOrDie("sw600dp")); |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 83 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 84 | EXPECT_TRUE(filter.Match(test::ParseConfigOrDie("fr-sw320dp-v13"))); |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | TEST(ConfigFilterTest, MatchesConfigWithSameLanguageButNoRegionSpecified) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 88 | AxisConfigFilter filter; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 89 | filter.AddConfig(test::ParseConfigOrDie("de-rDE")); |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 90 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 91 | EXPECT_TRUE(filter.Match(test::ParseConfigOrDie("de"))); |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | TEST(ConfigFilterTest, IgnoresVersion) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 95 | AxisConfigFilter filter; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 96 | filter.AddConfig(test::ParseConfigOrDie("normal-v4")); |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 97 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 98 | // The configs don't match on any axis besides version, which should be |
| 99 | // ignored. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 100 | EXPECT_TRUE(filter.Match(test::ParseConfigOrDie("sw600dp-v13"))); |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | TEST(ConfigFilterTest, MatchesConfigWithRegion) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 104 | AxisConfigFilter filter; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 105 | filter.AddConfig(test::ParseConfigOrDie("kok")); |
| 106 | filter.AddConfig(test::ParseConfigOrDie("kok-rIN")); |
| 107 | filter.AddConfig(test::ParseConfigOrDie("kok-v419")); |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 108 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 109 | EXPECT_TRUE(filter.Match(test::ParseConfigOrDie("kok-rIN"))); |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 110 | } |
| 111 | |
Adam Lesinski | 13308bc | 2017-05-16 15:26:54 -0700 | [diff] [blame^] | 112 | TEST(ConfigFilterTest, MatchesScripts) { |
| 113 | AxisConfigFilter filter; |
| 114 | |
| 115 | // "sr" gets automatically computed the script "Cyrl" |
| 116 | filter.AddConfig(test::ParseConfigOrDie("sr")); |
| 117 | |
| 118 | // "sr" -> "b+sr+Cyrl" |
| 119 | EXPECT_TRUE(filter.Match(test::ParseConfigOrDie("b+sr+Cyrl"))); |
| 120 | |
| 121 | // The incoming "sr" is also auto-computed to "b+sr+Cyrl". |
| 122 | EXPECT_TRUE(filter.Match(test::ParseConfigOrDie("sr"))); |
| 123 | |
| 124 | // "sr" -> "b+sr+Cyrl", which doesn't match "Latn". |
| 125 | EXPECT_FALSE(filter.Match(test::ParseConfigOrDie("b+sr+Latn"))); |
| 126 | } |
| 127 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 128 | } // namespace aapt |