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