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 | |
| 22 | namespace aapt { |
| 23 | |
| 24 | TEST(ResourceDeduperTest, SameValuesAreDeduped) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 25 | std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build(); |
| 26 | const ConfigDescription default_config = {}; |
| 27 | const ConfigDescription en_config = test::ParseConfigOrDie("en"); |
| 28 | const ConfigDescription en_v21_config = test::ParseConfigOrDie("en-v21"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 29 | // Chosen because this configuration is compatible with en. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 30 | const ConfigDescription land_config = test::ParseConfigOrDie("land"); |
Alexandria Cornwall | 77788eb | 2016-09-06 15:16:49 -0700 | [diff] [blame] | 31 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 32 | std::unique_ptr<ResourceTable> table = |
| 33 | test::ResourceTableBuilder() |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 34 | .AddString("android:string/dedupe", ResourceId{}, default_config, |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 35 | "dedupe") |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 36 | .AddString("android:string/dedupe", ResourceId{}, en_config, "dedupe") |
| 37 | .AddString("android:string/dedupe", ResourceId{}, land_config, |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 38 | "dedupe") |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 39 | .AddString("android:string/dedupe2", ResourceId{}, default_config, |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 40 | "dedupe") |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 41 | .AddString("android:string/dedupe2", ResourceId{}, en_config, |
| 42 | "dedupe") |
| 43 | .AddString("android:string/dedupe2", ResourceId{}, en_v21_config, |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 44 | "keep") |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 45 | .AddString("android:string/dedupe2", ResourceId{}, land_config, |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 46 | "dedupe") |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 47 | .Build(); |
Alexandria Cornwall | 77788eb | 2016-09-06 15:16:49 -0700 | [diff] [blame] | 48 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 49 | ASSERT_TRUE(ResourceDeduper().Consume(context.get(), table.get())); |
| 50 | EXPECT_EQ(nullptr, test::GetValueForConfig<String>( |
| 51 | table.get(), "android:string/dedupe", en_config)); |
| 52 | EXPECT_EQ(nullptr, test::GetValueForConfig<String>( |
| 53 | table.get(), "android:string/dedupe", land_config)); |
| 54 | EXPECT_EQ(nullptr, test::GetValueForConfig<String>( |
| 55 | table.get(), "android:string/dedupe2", en_config)); |
| 56 | EXPECT_NE(nullptr, test::GetValueForConfig<String>( |
| 57 | table.get(), "android:string/dedupe2", en_v21_config)); |
Alexandria Cornwall | 77788eb | 2016-09-06 15:16:49 -0700 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | TEST(ResourceDeduperTest, DifferentValuesAreKept) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 61 | std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build(); |
| 62 | const ConfigDescription default_config = {}; |
| 63 | const ConfigDescription en_config = test::ParseConfigOrDie("en"); |
| 64 | const ConfigDescription en_v21_config = test::ParseConfigOrDie("en-v21"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 65 | // Chosen because this configuration is compatible with en. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 66 | const ConfigDescription land_config = test::ParseConfigOrDie("land"); |
Alexandria Cornwall | 77788eb | 2016-09-06 15:16:49 -0700 | [diff] [blame] | 67 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 68 | std::unique_ptr<ResourceTable> table = |
| 69 | test::ResourceTableBuilder() |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 70 | .AddString("android:string/keep", ResourceId{}, default_config, |
| 71 | "keep") |
| 72 | .AddString("android:string/keep", ResourceId{}, en_config, "keep") |
| 73 | .AddString("android:string/keep", ResourceId{}, en_v21_config, |
| 74 | "keep2") |
| 75 | .AddString("android:string/keep", ResourceId{}, land_config, "keep2") |
| 76 | .Build(); |
Alexandria Cornwall | 77788eb | 2016-09-06 15:16:49 -0700 | [diff] [blame] | 77 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 78 | ASSERT_TRUE(ResourceDeduper().Consume(context.get(), table.get())); |
| 79 | EXPECT_NE(nullptr, test::GetValueForConfig<String>( |
| 80 | table.get(), "android:string/keep", en_config)); |
| 81 | EXPECT_NE(nullptr, test::GetValueForConfig<String>( |
| 82 | table.get(), "android:string/keep", en_v21_config)); |
| 83 | EXPECT_NE(nullptr, test::GetValueForConfig<String>( |
| 84 | table.get(), "android:string/keep", land_config)); |
Alexandria Cornwall | 77788eb | 2016-09-06 15:16:49 -0700 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | } // namespace aapt |