Mohamed Heikal | d3c5fb6 | 2018-01-12 11:37:26 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 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 "optimize/ResourceFilter.h" |
| 18 | |
| 19 | #include "ResourceTable.h" |
| 20 | #include "test/Test.h" |
| 21 | |
| 22 | using ::aapt::test::HasValue; |
MÃ¥rten Kongstad | 24c9aa6 | 2018-06-20 08:46:41 +0200 | [diff] [blame] | 23 | using ::android::ConfigDescription; |
Mohamed Heikal | d3c5fb6 | 2018-01-12 11:37:26 -0500 | [diff] [blame] | 24 | using ::testing::Not; |
| 25 | |
| 26 | namespace aapt { |
| 27 | |
| 28 | TEST(ResourceFilterTest, SomeValuesAreFilteredOut) { |
| 29 | std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build(); |
| 30 | const ConfigDescription default_config = {}; |
| 31 | |
| 32 | std::unique_ptr<ResourceTable> table = |
| 33 | test::ResourceTableBuilder() |
| 34 | .AddString("android:string/notblacklisted", ResourceId{}, default_config, "value") |
| 35 | .AddString("android:string/blacklisted", ResourceId{}, default_config, "value") |
| 36 | .AddString("android:string/notblacklisted2", ResourceId{}, default_config, "value") |
| 37 | .AddString("android:string/blacklisted2", ResourceId{}, default_config, "value") |
| 38 | .Build(); |
| 39 | |
| 40 | std::unordered_set<ResourceName> blacklist = { |
| 41 | ResourceName({}, ResourceType::kString, "blacklisted"), |
| 42 | ResourceName({}, ResourceType::kString, "blacklisted2"), |
| 43 | }; |
| 44 | |
| 45 | ASSERT_TRUE(ResourceFilter(blacklist).Consume(context.get(), table.get())); |
| 46 | EXPECT_THAT(table, HasValue("android:string/notblacklisted", default_config)); |
| 47 | EXPECT_THAT(table, HasValue("android:string/notblacklisted2", default_config)); |
| 48 | EXPECT_THAT(table, Not(HasValue("android:string/blacklisted", default_config))); |
| 49 | EXPECT_THAT(table, Not(HasValue("android:string/blacklisted2", default_config))); |
| 50 | } |
| 51 | |
| 52 | TEST(ResourceFilterTest, TypeIsCheckedBeforeFiltering) { |
| 53 | std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build(); |
| 54 | const ConfigDescription default_config = {}; |
| 55 | |
| 56 | std::unique_ptr<ResourceTable> table = |
| 57 | test::ResourceTableBuilder() |
| 58 | .AddString("android:string/notblacklisted", ResourceId{}, default_config, "value") |
| 59 | .AddString("android:string/blacklisted", ResourceId{}, default_config, "value") |
| 60 | .AddString("android:drawable/notblacklisted", ResourceId{}, default_config, "value") |
| 61 | .AddString("android:drawable/blacklisted", ResourceId{}, default_config, "value") |
| 62 | .Build(); |
| 63 | |
| 64 | std::unordered_set<ResourceName> blacklist = { |
| 65 | ResourceName({}, ResourceType::kString, "blacklisted"), |
| 66 | }; |
| 67 | |
| 68 | ASSERT_TRUE(ResourceFilter(blacklist).Consume(context.get(), table.get())); |
| 69 | EXPECT_THAT(table, HasValue("android:string/notblacklisted", default_config)); |
| 70 | EXPECT_THAT(table, HasValue("android:drawable/blacklisted", default_config)); |
| 71 | EXPECT_THAT(table, HasValue("android:drawable/notblacklisted", default_config)); |
| 72 | EXPECT_THAT(table, Not(HasValue("android:string/blacklisted", default_config))); |
| 73 | } |
| 74 | |
| 75 | } // namespace aapt |