Adam Lesinski | fab5087 | 2014-04-16 14:40:42 -0700 | [diff] [blame] | 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 | |
| 25 | using android::String8; |
| 26 | |
| 27 | // In this context, 'Axis' represents a particular field in the configuration, |
| 28 | // such as language or density. |
| 29 | |
| 30 | TEST(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 | |
| 45 | TEST(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 | |
| 55 | TEST(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 | |
| 66 | TEST(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 | |
| 78 | TEST(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 | |
| 89 | TEST(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 | |
| 100 | TEST(WeakResourceFilterTest, ParsesStandardLocaleOnlyString) { |
| 101 | WeakResourceFilter filter; |
| 102 | EXPECT_EQ(NO_ERROR, filter.parse(String8("de_DE"))); |
| 103 | } |
| 104 | |
| 105 | TEST(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 | |
| 117 | TEST(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 | |
Adam Lesinski | 27f69f4 | 2014-08-21 13:19:12 -0700 | [diff] [blame^] | 129 | TEST(StrongResourceFilterTest, MatchesDensities) { |
| 130 | ConfigDescription config; |
| 131 | config.density = 160; |
| 132 | config.version = 4; |
| 133 | std::set<ConfigDescription> configs; |
| 134 | configs.insert(config); |
| 135 | |
| 136 | StrongResourceFilter filter(configs); |
| 137 | |
| 138 | ConfigDescription expectedConfig; |
| 139 | expectedConfig.density = 160; |
| 140 | expectedConfig.version = 4; |
| 141 | ASSERT_TRUE(filter.match(expectedConfig)); |
| 142 | } |
| 143 | |
| 144 | TEST(StrongResourceFilterTest, MatchOnlyMdpiAndExcludeAllOthers) { |
| 145 | std::set<ConfigDescription> configsToMatch; |
| 146 | ConfigDescription config; |
| 147 | config.density = 160; |
| 148 | config.version = 4; |
| 149 | configsToMatch.insert(config); |
| 150 | |
| 151 | std::set<ConfigDescription> configsToNotMatch; |
| 152 | config.density = 480; |
| 153 | configsToNotMatch.insert(config); |
| 154 | |
| 155 | AndResourceFilter filter; |
| 156 | filter.addFilter(new InverseResourceFilter(new StrongResourceFilter(configsToNotMatch))); |
| 157 | filter.addFilter(new StrongResourceFilter(configsToMatch)); |
| 158 | |
| 159 | ConfigDescription expectedConfig; |
| 160 | expectedConfig.density = 160; |
| 161 | expectedConfig.version = 4; |
| 162 | ASSERT_TRUE(filter.match(expectedConfig)); |
| 163 | } |