Adam Lesinski | 34a1687 | 2018-02-23 16:18:10 -0800 | [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 "link/NoDefaultResourceRemover.h" |
| 18 | |
| 19 | #include "test/Test.h" |
| 20 | |
| 21 | namespace aapt { |
| 22 | |
| 23 | TEST(NoDefaultResourceRemoverTest, RemoveEntryWithNoDefaultAndOnlyLocales) { |
Ryan Mitchell | 1d008d1 | 2021-03-19 14:54:17 -0700 | [diff] [blame] | 24 | std::unique_ptr<IAaptContext> context = test::ContextBuilder().SetPackageId(0x01).Build(); |
Adam Lesinski | 34a1687 | 2018-02-23 16:18:10 -0800 | [diff] [blame] | 25 | std::unique_ptr<ResourceTable> table = |
| 26 | test::ResourceTableBuilder() |
Adam Lesinski | 34a1687 | 2018-02-23 16:18:10 -0800 | [diff] [blame] | 27 | .AddSimple("android:string/foo") |
| 28 | .AddSimple("android:string/foo", test::ParseConfigOrDie("en-rGB")) |
| 29 | .AddSimple("android:string/foo", test::ParseConfigOrDie("fr-rFR")) |
| 30 | .AddSimple("android:string/bar", test::ParseConfigOrDie("en-rGB")) |
| 31 | .AddSimple("android:string/bar", test::ParseConfigOrDie("fr-rFR")) |
| 32 | .AddSimple("android:string/bat", test::ParseConfigOrDie("en-rGB-xhdpi")) |
| 33 | .AddSimple("android:string/bat", test::ParseConfigOrDie("fr-rFR-hdpi")) |
| 34 | .AddSimple("android:string/baz", test::ParseConfigOrDie("en-rGB")) |
| 35 | .AddSimple("android:string/baz", test::ParseConfigOrDie("fr-rFR")) |
| 36 | .SetSymbolState("android:string/baz", ResourceId(0x01020002), Visibility::Level::kPublic) |
| 37 | .Build(); |
| 38 | |
| 39 | NoDefaultResourceRemover remover; |
| 40 | ASSERT_TRUE(remover.Consume(context.get(), table.get())); |
| 41 | |
| 42 | EXPECT_TRUE(table->FindResource(test::ParseNameOrDie("android:string/foo"))); |
| 43 | EXPECT_FALSE(table->FindResource(test::ParseNameOrDie("android:string/bar"))); |
| 44 | EXPECT_TRUE(table->FindResource(test::ParseNameOrDie("android:string/bat"))); |
| 45 | EXPECT_TRUE(table->FindResource(test::ParseNameOrDie("android:string/baz"))); |
| 46 | } |
| 47 | |
Ryan Mitchell | a593605 | 2018-05-23 13:31:28 -0700 | [diff] [blame] | 48 | TEST(NoDefaultResourceRemoverTest, KeepEntryWithLocalesAndDensities) { |
Ryan Mitchell | 1d008d1 | 2021-03-19 14:54:17 -0700 | [diff] [blame] | 49 | std::unique_ptr<IAaptContext> context = |
| 50 | test::ContextBuilder().SetPackageId(0x01).SetMinSdkVersion(26).Build(); |
Ryan Mitchell | a593605 | 2018-05-23 13:31:28 -0700 | [diff] [blame] | 51 | std::unique_ptr<ResourceTable> table = |
| 52 | test::ResourceTableBuilder() |
Ryan Mitchell | a593605 | 2018-05-23 13:31:28 -0700 | [diff] [blame] | 53 | .AddSimple("android:drawable/keep1", test::ParseConfigOrDie("mdpi")) // v4 |
| 54 | .AddSimple("android:drawable/keep1", test::ParseConfigOrDie("en-rGB")) |
| 55 | .AddSimple("android:drawable/keep1", test::ParseConfigOrDie("fr-rFR")) |
| 56 | .AddSimple("android:drawable/keep2", test::ParseConfigOrDie("fr-rFR")) |
| 57 | .AddSimple("android:drawable/keep2", test::ParseConfigOrDie("en-rGB")) |
| 58 | .AddSimple("android:drawable/keep2", test::ParseConfigOrDie("xxxhdpi")) // v4 |
| 59 | .AddSimple("android:drawable/remove1", test::ParseConfigOrDie("fr-rFR")) |
| 60 | .AddSimple("android:drawable/remove1", test::ParseConfigOrDie("en-rGB")) |
| 61 | .AddSimple("android:drawable/remove1", test::ParseConfigOrDie("w600dp-xhdpi")) // v13 |
| 62 | .Build(); |
| 63 | |
| 64 | NoDefaultResourceRemover remover; |
| 65 | ASSERT_TRUE(remover.Consume(context.get(), table.get())); |
| 66 | |
| 67 | EXPECT_TRUE(table->FindResource(test::ParseNameOrDie("android:drawable/keep1"))); |
| 68 | EXPECT_TRUE(table->FindResource(test::ParseNameOrDie("android:drawable/keep2"))); |
| 69 | EXPECT_FALSE(table->FindResource(test::ParseNameOrDie("android:drawable/remove1"))); |
| 70 | } |
| 71 | |
| 72 | TEST(NoDefaultResourceRemoverTest, RemoveEntryWithLocalesAndDensitiesLowVersion) { |
Ryan Mitchell | 1d008d1 | 2021-03-19 14:54:17 -0700 | [diff] [blame] | 73 | std::unique_ptr<IAaptContext> context = |
| 74 | test::ContextBuilder().SetPackageId(0x01).SetMinSdkVersion(3).Build(); |
Ryan Mitchell | a593605 | 2018-05-23 13:31:28 -0700 | [diff] [blame] | 75 | std::unique_ptr<ResourceTable> table = |
| 76 | test::ResourceTableBuilder() |
Ryan Mitchell | a593605 | 2018-05-23 13:31:28 -0700 | [diff] [blame] | 77 | .AddSimple("android:drawable/remove1", test::ParseConfigOrDie("mdpi")) // v4 |
| 78 | .AddSimple("android:drawable/remove1", test::ParseConfigOrDie("en-rGB")) |
| 79 | .AddSimple("android:drawable/remove1", test::ParseConfigOrDie("fr-rFR")) |
| 80 | .Build(); |
| 81 | |
| 82 | NoDefaultResourceRemover remover; |
| 83 | ASSERT_TRUE(remover.Consume(context.get(), table.get())); |
| 84 | |
| 85 | EXPECT_FALSE(table->FindResource(test::ParseNameOrDie("android:drawable/remove1"))); |
| 86 | } |
| 87 | |
| 88 | TEST(NoDefaultResourceRemoverTest, KeepEntryWithVersion) { |
Ryan Mitchell | 1d008d1 | 2021-03-19 14:54:17 -0700 | [diff] [blame] | 89 | std::unique_ptr<IAaptContext> context = |
| 90 | test::ContextBuilder().SetPackageId(0x01).SetMinSdkVersion(8).Build(); |
Ryan Mitchell | a593605 | 2018-05-23 13:31:28 -0700 | [diff] [blame] | 91 | std::unique_ptr<ResourceTable> table = |
| 92 | test::ResourceTableBuilder() |
Ryan Mitchell | a593605 | 2018-05-23 13:31:28 -0700 | [diff] [blame] | 93 | .AddSimple("android:drawable/keep1", test::ParseConfigOrDie("v8")) |
| 94 | .AddSimple("android:drawable/keep1", test::ParseConfigOrDie("en-rGB")) |
| 95 | .AddSimple("android:drawable/keep1", test::ParseConfigOrDie("fr-rFR")) |
| 96 | .AddSimple("android:drawable/remove1", test::ParseConfigOrDie("v9")) |
| 97 | .AddSimple("android:drawable/remove1", test::ParseConfigOrDie("en-rGB")) |
| 98 | .AddSimple("android:drawable/remove1", test::ParseConfigOrDie("fr-rFR")) |
| 99 | .Build(); |
| 100 | |
| 101 | NoDefaultResourceRemover remover; |
| 102 | ASSERT_TRUE(remover.Consume(context.get(), table.get())); |
| 103 | |
| 104 | EXPECT_TRUE(table->FindResource(test::ParseNameOrDie("android:drawable/keep1"))); |
| 105 | EXPECT_FALSE(table->FindResource(test::ParseNameOrDie("android:drawable/remove1"))); |
| 106 | } |
| 107 | |
Adam Lesinski | 34a1687 | 2018-02-23 16:18:10 -0800 | [diff] [blame] | 108 | } // namespace aapt |