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 | e385674 | 2017-06-12 14:55:58 -0700 | [diff] [blame] | 22 | using ::aapt::test::HasValue; |
MÃ¥rten Kongstad | 24c9aa6 | 2018-06-20 08:46:41 +0200 | [diff] [blame] | 23 | using ::android::ConfigDescription; |
Adam Lesinski | e385674 | 2017-06-12 14:55:58 -0700 | [diff] [blame] | 24 | using ::testing::Not; |
| 25 | |
Alexandria Cornwall | 77788eb | 2016-09-06 15:16:49 -0700 | [diff] [blame] | 26 | namespace aapt { |
| 27 | |
| 28 | TEST(ResourceDeduperTest, SameValuesAreDeduped) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 29 | std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build(); |
| 30 | const ConfigDescription default_config = {}; |
Adam Lesinski | e385674 | 2017-06-12 14:55:58 -0700 | [diff] [blame] | 31 | const ConfigDescription ldrtl_config = test::ParseConfigOrDie("ldrtl"); |
| 32 | const ConfigDescription ldrtl_v21_config = test::ParseConfigOrDie("ldrtl-v21"); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 33 | const ConfigDescription en_config = test::ParseConfigOrDie("en"); |
| 34 | const ConfigDescription en_v21_config = test::ParseConfigOrDie("en-v21"); |
Adam Lesinski | e385674 | 2017-06-12 14:55:58 -0700 | [diff] [blame] | 35 | // Chosen because this configuration is compatible with ldrtl/en. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 36 | const ConfigDescription land_config = test::ParseConfigOrDie("land"); |
Alexandria Cornwall | 77788eb | 2016-09-06 15:16:49 -0700 | [diff] [blame] | 37 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 38 | std::unique_ptr<ResourceTable> table = |
| 39 | test::ResourceTableBuilder() |
Adam Lesinski | e385674 | 2017-06-12 14:55:58 -0700 | [diff] [blame] | 40 | .AddString("android:string/dedupe", ResourceId{}, default_config, "dedupe") |
| 41 | .AddString("android:string/dedupe", ResourceId{}, ldrtl_config, "dedupe") |
| 42 | .AddString("android:string/dedupe", ResourceId{}, land_config, "dedupe") |
| 43 | |
| 44 | .AddString("android:string/dedupe2", ResourceId{}, default_config, "dedupe") |
| 45 | .AddString("android:string/dedupe2", ResourceId{}, ldrtl_config, "dedupe") |
| 46 | .AddString("android:string/dedupe2", ResourceId{}, ldrtl_v21_config, "keep") |
| 47 | .AddString("android:string/dedupe2", ResourceId{}, land_config, "dedupe") |
| 48 | |
| 49 | .AddString("android:string/dedupe3", ResourceId{}, default_config, "dedupe") |
| 50 | .AddString("android:string/dedupe3", ResourceId{}, en_config, "dedupe") |
| 51 | .AddString("android:string/dedupe3", ResourceId{}, en_v21_config, "dedupe") |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 52 | .Build(); |
Alexandria Cornwall | 77788eb | 2016-09-06 15:16:49 -0700 | [diff] [blame] | 53 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 54 | ASSERT_TRUE(ResourceDeduper().Consume(context.get(), table.get())); |
Ryan Mitchell | 527ebba | 2020-10-30 12:32:47 -0700 | [diff] [blame] | 55 | EXPECT_THAT(table, HasValue("android:string/dedupe", default_config)); |
Adam Lesinski | e385674 | 2017-06-12 14:55:58 -0700 | [diff] [blame] | 56 | EXPECT_THAT(table, Not(HasValue("android:string/dedupe", ldrtl_config))); |
| 57 | EXPECT_THAT(table, Not(HasValue("android:string/dedupe", land_config))); |
| 58 | |
Ryan Mitchell | 527ebba | 2020-10-30 12:32:47 -0700 | [diff] [blame] | 59 | EXPECT_THAT(table, HasValue("android:string/dedupe2", default_config)); |
Adam Lesinski | e385674 | 2017-06-12 14:55:58 -0700 | [diff] [blame] | 60 | EXPECT_THAT(table, HasValue("android:string/dedupe2", ldrtl_v21_config)); |
| 61 | EXPECT_THAT(table, Not(HasValue("android:string/dedupe2", ldrtl_config))); |
| 62 | |
| 63 | EXPECT_THAT(table, HasValue("android:string/dedupe3", default_config)); |
| 64 | EXPECT_THAT(table, HasValue("android:string/dedupe3", en_config)); |
| 65 | EXPECT_THAT(table, Not(HasValue("android:string/dedupe3", en_v21_config))); |
Alexandria Cornwall | 77788eb | 2016-09-06 15:16:49 -0700 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | TEST(ResourceDeduperTest, DifferentValuesAreKept) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 69 | std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build(); |
| 70 | const ConfigDescription default_config = {}; |
Adam Lesinski | e385674 | 2017-06-12 14:55:58 -0700 | [diff] [blame] | 71 | const ConfigDescription ldrtl_config = test::ParseConfigOrDie("ldrtl"); |
| 72 | const ConfigDescription ldrtl_v21_config = test::ParseConfigOrDie("ldrtl-v21"); |
| 73 | // Chosen because this configuration is compatible with ldrtl. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 74 | const ConfigDescription land_config = test::ParseConfigOrDie("land"); |
Alexandria Cornwall | 77788eb | 2016-09-06 15:16:49 -0700 | [diff] [blame] | 75 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 76 | std::unique_ptr<ResourceTable> table = |
| 77 | test::ResourceTableBuilder() |
Adam Lesinski | e385674 | 2017-06-12 14:55:58 -0700 | [diff] [blame] | 78 | .AddString("android:string/keep", ResourceId{}, default_config, "keep") |
| 79 | .AddString("android:string/keep", ResourceId{}, ldrtl_config, "keep") |
| 80 | .AddString("android:string/keep", ResourceId{}, ldrtl_v21_config, "keep2") |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 81 | .AddString("android:string/keep", ResourceId{}, land_config, "keep2") |
| 82 | .Build(); |
Alexandria Cornwall | 77788eb | 2016-09-06 15:16:49 -0700 | [diff] [blame] | 83 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 84 | ASSERT_TRUE(ResourceDeduper().Consume(context.get(), table.get())); |
Ryan Mitchell | f64b9d8 | 2019-08-13 15:41:49 -0700 | [diff] [blame] | 85 | EXPECT_THAT(table, HasValue("android:string/keep", default_config)); |
Adam Lesinski | e385674 | 2017-06-12 14:55:58 -0700 | [diff] [blame] | 86 | EXPECT_THAT(table, HasValue("android:string/keep", ldrtl_config)); |
| 87 | EXPECT_THAT(table, HasValue("android:string/keep", ldrtl_v21_config)); |
| 88 | EXPECT_THAT(table, HasValue("android:string/keep", land_config)); |
| 89 | } |
| 90 | |
Ryan Mitchell | f64b9d8 | 2019-08-13 15:41:49 -0700 | [diff] [blame] | 91 | TEST(ResourceDeduperTest, SameValuesAreDedupedIncompatibleSiblings) { |
| 92 | std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build(); |
| 93 | const ConfigDescription default_config = {}; |
| 94 | const ConfigDescription ldrtl_config = test::ParseConfigOrDie("ldrtl"); |
| 95 | const ConfigDescription ldrtl_night_config = test::ParseConfigOrDie("ldrtl-night"); |
| 96 | // Chosen because this configuration is not compatible with ldrtl-night. |
| 97 | const ConfigDescription ldrtl_notnight_config = test::ParseConfigOrDie("ldrtl-notnight"); |
| 98 | |
| 99 | std::unique_ptr<ResourceTable> table = |
| 100 | test::ResourceTableBuilder() |
| 101 | .AddString("android:string/keep", ResourceId{}, default_config, "keep") |
| 102 | .AddString("android:string/keep", ResourceId{}, ldrtl_config, "dedupe") |
| 103 | .AddString("android:string/keep", ResourceId{}, ldrtl_night_config, "dedupe") |
| 104 | .AddString("android:string/keep", ResourceId{}, ldrtl_notnight_config, "keep2") |
| 105 | .Build(); |
| 106 | |
| 107 | ASSERT_TRUE(ResourceDeduper().Consume(context.get(), table.get())); |
| 108 | EXPECT_THAT(table, HasValue("android:string/keep", default_config)); |
| 109 | EXPECT_THAT(table, HasValue("android:string/keep", ldrtl_config)); |
| 110 | EXPECT_THAT(table, Not(HasValue("android:string/keep", ldrtl_night_config))); |
| 111 | EXPECT_THAT(table, HasValue("android:string/keep", ldrtl_notnight_config)); |
| 112 | } |
| 113 | |
| 114 | TEST(ResourceDeduperTest, SameValuesAreDedupedCompatibleNonSiblings) { |
| 115 | std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build(); |
| 116 | const ConfigDescription default_config = {}; |
| 117 | const ConfigDescription ldrtl_config = test::ParseConfigOrDie("ldrtl"); |
| 118 | const ConfigDescription ldrtl_night_config = test::ParseConfigOrDie("ldrtl-night"); |
| 119 | // Chosen because this configuration is compatible with ldrtl. |
| 120 | const ConfigDescription land_config = test::ParseConfigOrDie("land"); |
| 121 | |
| 122 | std::unique_ptr<ResourceTable> table = |
| 123 | test::ResourceTableBuilder() |
| 124 | .AddString("android:string/keep", ResourceId{}, default_config, "keep") |
| 125 | .AddString("android:string/keep", ResourceId{}, ldrtl_config, "dedupe") |
| 126 | .AddString("android:string/keep", ResourceId{}, ldrtl_night_config, "dedupe") |
| 127 | .AddString("android:string/keep", ResourceId{}, land_config, "keep2") |
| 128 | .Build(); |
| 129 | |
| 130 | ASSERT_TRUE(ResourceDeduper().Consume(context.get(), table.get())); |
| 131 | EXPECT_THAT(table, HasValue("android:string/keep", default_config)); |
| 132 | EXPECT_THAT(table, HasValue("android:string/keep", ldrtl_config)); |
| 133 | EXPECT_THAT(table, Not(HasValue("android:string/keep", ldrtl_night_config))); |
| 134 | EXPECT_THAT(table, HasValue("android:string/keep", land_config)); |
| 135 | } |
| 136 | |
Adam Lesinski | e385674 | 2017-06-12 14:55:58 -0700 | [diff] [blame] | 137 | TEST(ResourceDeduperTest, LocalesValuesAreKept) { |
| 138 | std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build(); |
| 139 | const ConfigDescription default_config = {}; |
| 140 | const ConfigDescription fr_config = test::ParseConfigOrDie("fr"); |
| 141 | const ConfigDescription fr_rCA_config = test::ParseConfigOrDie("fr-rCA"); |
| 142 | |
| 143 | std::unique_ptr<ResourceTable> table = |
| 144 | test::ResourceTableBuilder() |
| 145 | .AddString("android:string/keep", ResourceId{}, default_config, "keep") |
| 146 | .AddString("android:string/keep", ResourceId{}, fr_config, "keep") |
| 147 | .AddString("android:string/keep", ResourceId{}, fr_rCA_config, "keep") |
| 148 | .Build(); |
| 149 | |
| 150 | ASSERT_TRUE(ResourceDeduper().Consume(context.get(), table.get())); |
| 151 | EXPECT_THAT(table, HasValue("android:string/keep", default_config)); |
| 152 | EXPECT_THAT(table, HasValue("android:string/keep", fr_config)); |
| 153 | EXPECT_THAT(table, HasValue("android:string/keep", fr_rCA_config)); |
Alexandria Cornwall | 77788eb | 2016-09-06 15:16:49 -0700 | [diff] [blame] | 154 | } |
| 155 | |
Ryan Mitchell | 527ebba | 2020-10-30 12:32:47 -0700 | [diff] [blame] | 156 | TEST(ResourceDeduperTest, MccMncValuesAreKept) { |
| 157 | std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build(); |
| 158 | const ConfigDescription default_config = {}; |
| 159 | const ConfigDescription mcc_config = test::ParseConfigOrDie("mcc262"); |
| 160 | const ConfigDescription mnc_config = test::ParseConfigOrDie("mnc2"); |
| 161 | |
| 162 | std::unique_ptr<ResourceTable> table = |
| 163 | test::ResourceTableBuilder() |
| 164 | .AddString("android:string/keep", ResourceId{}, default_config, "keep") |
| 165 | .AddString("android:string/keep", ResourceId{}, mcc_config, "keep") |
| 166 | .AddString("android:string/keep", ResourceId{}, mnc_config, "keep") |
| 167 | .Build(); |
| 168 | |
| 169 | ASSERT_TRUE(ResourceDeduper().Consume(context.get(), table.get())); |
| 170 | EXPECT_THAT(table, HasValue("android:string/keep", default_config)); |
| 171 | EXPECT_THAT(table, HasValue("android:string/keep", mcc_config)); |
| 172 | EXPECT_THAT(table, HasValue("android:string/keep", mnc_config)); |
| 173 | } |
| 174 | |
| 175 | |
Alexandria Cornwall | 77788eb | 2016-09-06 15:16:49 -0700 | [diff] [blame] | 176 | } // namespace aapt |