Alexandria Cornwall | 77788eb | 2016-09-06 15:16:49 -0700 | [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 | |
Adam Lesinski | d48944a | 2017-02-21 14:22:30 -0800 | [diff] [blame] | 17 | #include "optimize/ResourceDeduper.h" |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 18 | |
| 19 | #include "ResourceTable.h" |
Alexandria Cornwall | 77788eb | 2016-09-06 15:16:49 -0700 | [diff] [blame] | 20 | #include "test/Test.h" |
| 21 | |
Adam Lesinski | 9073991 | 2017-06-12 14:55:58 -0700 | [diff] [blame] | 22 | using ::aapt::test::HasValue; |
| 23 | using ::testing::Not; |
| 24 | |
Alexandria Cornwall | 77788eb | 2016-09-06 15:16:49 -0700 | [diff] [blame] | 25 | namespace aapt { |
| 26 | |
| 27 | TEST(ResourceDeduperTest, SameValuesAreDeduped) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 28 | std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build(); |
| 29 | const ConfigDescription default_config = {}; |
Adam Lesinski | 9073991 | 2017-06-12 14:55:58 -0700 | [diff] [blame] | 30 | const ConfigDescription ldrtl_config = test::ParseConfigOrDie("ldrtl"); |
| 31 | const ConfigDescription ldrtl_v21_config = test::ParseConfigOrDie("ldrtl-v21"); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 32 | const ConfigDescription en_config = test::ParseConfigOrDie("en"); |
| 33 | const ConfigDescription en_v21_config = test::ParseConfigOrDie("en-v21"); |
Adam Lesinski | 9073991 | 2017-06-12 14:55:58 -0700 | [diff] [blame] | 34 | // Chosen because this configuration is compatible with ldrtl/en. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 35 | const ConfigDescription land_config = test::ParseConfigOrDie("land"); |
Alexandria Cornwall | 77788eb | 2016-09-06 15:16:49 -0700 | [diff] [blame] | 36 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 37 | std::unique_ptr<ResourceTable> table = |
| 38 | test::ResourceTableBuilder() |
Adam Lesinski | 9073991 | 2017-06-12 14:55:58 -0700 | [diff] [blame] | 39 | .AddString("android:string/dedupe", ResourceId{}, default_config, "dedupe") |
| 40 | .AddString("android:string/dedupe", ResourceId{}, ldrtl_config, "dedupe") |
| 41 | .AddString("android:string/dedupe", ResourceId{}, land_config, "dedupe") |
| 42 | |
| 43 | .AddString("android:string/dedupe2", ResourceId{}, default_config, "dedupe") |
| 44 | .AddString("android:string/dedupe2", ResourceId{}, ldrtl_config, "dedupe") |
| 45 | .AddString("android:string/dedupe2", ResourceId{}, ldrtl_v21_config, "keep") |
| 46 | .AddString("android:string/dedupe2", ResourceId{}, land_config, "dedupe") |
| 47 | |
| 48 | .AddString("android:string/dedupe3", ResourceId{}, default_config, "dedupe") |
| 49 | .AddString("android:string/dedupe3", ResourceId{}, en_config, "dedupe") |
| 50 | .AddString("android:string/dedupe3", ResourceId{}, en_v21_config, "dedupe") |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 51 | .Build(); |
Alexandria Cornwall | 77788eb | 2016-09-06 15:16:49 -0700 | [diff] [blame] | 52 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 53 | ASSERT_TRUE(ResourceDeduper().Consume(context.get(), table.get())); |
Adam Lesinski | 9073991 | 2017-06-12 14:55:58 -0700 | [diff] [blame] | 54 | EXPECT_THAT(table, Not(HasValue("android:string/dedupe", ldrtl_config))); |
| 55 | EXPECT_THAT(table, Not(HasValue("android:string/dedupe", land_config))); |
| 56 | |
| 57 | EXPECT_THAT(table, HasValue("android:string/dedupe2", ldrtl_v21_config)); |
| 58 | EXPECT_THAT(table, Not(HasValue("android:string/dedupe2", ldrtl_config))); |
| 59 | |
| 60 | EXPECT_THAT(table, HasValue("android:string/dedupe3", default_config)); |
| 61 | EXPECT_THAT(table, HasValue("android:string/dedupe3", en_config)); |
| 62 | EXPECT_THAT(table, Not(HasValue("android:string/dedupe3", en_v21_config))); |
Alexandria Cornwall | 77788eb | 2016-09-06 15:16:49 -0700 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | TEST(ResourceDeduperTest, DifferentValuesAreKept) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 66 | std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build(); |
| 67 | const ConfigDescription default_config = {}; |
Adam Lesinski | 9073991 | 2017-06-12 14:55:58 -0700 | [diff] [blame] | 68 | const ConfigDescription ldrtl_config = test::ParseConfigOrDie("ldrtl"); |
| 69 | const ConfigDescription ldrtl_v21_config = test::ParseConfigOrDie("ldrtl-v21"); |
| 70 | // Chosen because this configuration is compatible with ldrtl. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 71 | const ConfigDescription land_config = test::ParseConfigOrDie("land"); |
Alexandria Cornwall | 77788eb | 2016-09-06 15:16:49 -0700 | [diff] [blame] | 72 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 73 | std::unique_ptr<ResourceTable> table = |
| 74 | test::ResourceTableBuilder() |
Adam Lesinski | 9073991 | 2017-06-12 14:55:58 -0700 | [diff] [blame] | 75 | .AddString("android:string/keep", ResourceId{}, default_config, "keep") |
| 76 | .AddString("android:string/keep", ResourceId{}, ldrtl_config, "keep") |
| 77 | .AddString("android:string/keep", ResourceId{}, ldrtl_v21_config, "keep2") |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 78 | .AddString("android:string/keep", ResourceId{}, land_config, "keep2") |
| 79 | .Build(); |
Alexandria Cornwall | 77788eb | 2016-09-06 15:16:49 -0700 | [diff] [blame] | 80 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 81 | ASSERT_TRUE(ResourceDeduper().Consume(context.get(), table.get())); |
Adam Lesinski | 9073991 | 2017-06-12 14:55:58 -0700 | [diff] [blame] | 82 | EXPECT_THAT(table, HasValue("android:string/keep", ldrtl_config)); |
| 83 | EXPECT_THAT(table, HasValue("android:string/keep", ldrtl_v21_config)); |
| 84 | EXPECT_THAT(table, HasValue("android:string/keep", land_config)); |
| 85 | } |
| 86 | |
| 87 | TEST(ResourceDeduperTest, LocalesValuesAreKept) { |
| 88 | std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build(); |
| 89 | const ConfigDescription default_config = {}; |
| 90 | const ConfigDescription fr_config = test::ParseConfigOrDie("fr"); |
| 91 | const ConfigDescription fr_rCA_config = test::ParseConfigOrDie("fr-rCA"); |
| 92 | |
| 93 | std::unique_ptr<ResourceTable> table = |
| 94 | test::ResourceTableBuilder() |
| 95 | .AddString("android:string/keep", ResourceId{}, default_config, "keep") |
| 96 | .AddString("android:string/keep", ResourceId{}, fr_config, "keep") |
| 97 | .AddString("android:string/keep", ResourceId{}, fr_rCA_config, "keep") |
| 98 | .Build(); |
| 99 | |
| 100 | ASSERT_TRUE(ResourceDeduper().Consume(context.get(), table.get())); |
| 101 | EXPECT_THAT(table, HasValue("android:string/keep", default_config)); |
| 102 | EXPECT_THAT(table, HasValue("android:string/keep", fr_config)); |
| 103 | EXPECT_THAT(table, HasValue("android:string/keep", fr_rCA_config)); |
Alexandria Cornwall | 77788eb | 2016-09-06 15:16:49 -0700 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | } // namespace aapt |