blob: 2e098aec4f8da9f6f34666a818d9dc11da0bd5fa [file] [log] [blame]
Alexandria Cornwall77788eb2016-09-06 15:16:49 -07001/*
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 Lesinskid48944a2017-02-21 14:22:30 -080017#include "optimize/ResourceDeduper.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070018
19#include "ResourceTable.h"
Alexandria Cornwall77788eb2016-09-06 15:16:49 -070020#include "test/Test.h"
21
Adam Lesinskie3856742017-06-12 14:55:58 -070022using ::aapt::test::HasValue;
MÃ¥rten Kongstad24c9aa62018-06-20 08:46:41 +020023using ::android::ConfigDescription;
Adam Lesinskie3856742017-06-12 14:55:58 -070024using ::testing::Not;
25
Alexandria Cornwall77788eb2016-09-06 15:16:49 -070026namespace aapt {
27
28TEST(ResourceDeduperTest, SameValuesAreDeduped) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070029 std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build();
30 const ConfigDescription default_config = {};
Adam Lesinskie3856742017-06-12 14:55:58 -070031 const ConfigDescription ldrtl_config = test::ParseConfigOrDie("ldrtl");
32 const ConfigDescription ldrtl_v21_config = test::ParseConfigOrDie("ldrtl-v21");
Adam Lesinskice5e56e2016-10-21 17:56:45 -070033 const ConfigDescription en_config = test::ParseConfigOrDie("en");
34 const ConfigDescription en_v21_config = test::ParseConfigOrDie("en-v21");
Adam Lesinskie3856742017-06-12 14:55:58 -070035 // Chosen because this configuration is compatible with ldrtl/en.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070036 const ConfigDescription land_config = test::ParseConfigOrDie("land");
Alexandria Cornwall77788eb2016-09-06 15:16:49 -070037
Adam Lesinskicacb28f2016-10-19 12:18:14 -070038 std::unique_ptr<ResourceTable> table =
39 test::ResourceTableBuilder()
Adam Lesinskie3856742017-06-12 14:55:58 -070040 .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 Lesinskice5e56e2016-10-21 17:56:45 -070052 .Build();
Alexandria Cornwall77788eb2016-09-06 15:16:49 -070053
Adam Lesinskice5e56e2016-10-21 17:56:45 -070054 ASSERT_TRUE(ResourceDeduper().Consume(context.get(), table.get()));
Adam Lesinskie3856742017-06-12 14:55:58 -070055 EXPECT_THAT(table, Not(HasValue("android:string/dedupe", ldrtl_config)));
56 EXPECT_THAT(table, Not(HasValue("android:string/dedupe", land_config)));
57
58 EXPECT_THAT(table, HasValue("android:string/dedupe2", ldrtl_v21_config));
59 EXPECT_THAT(table, Not(HasValue("android:string/dedupe2", ldrtl_config)));
60
61 EXPECT_THAT(table, HasValue("android:string/dedupe3", default_config));
62 EXPECT_THAT(table, HasValue("android:string/dedupe3", en_config));
63 EXPECT_THAT(table, Not(HasValue("android:string/dedupe3", en_v21_config)));
Alexandria Cornwall77788eb2016-09-06 15:16:49 -070064}
65
66TEST(ResourceDeduperTest, DifferentValuesAreKept) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070067 std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build();
68 const ConfigDescription default_config = {};
Adam Lesinskie3856742017-06-12 14:55:58 -070069 const ConfigDescription ldrtl_config = test::ParseConfigOrDie("ldrtl");
70 const ConfigDescription ldrtl_v21_config = test::ParseConfigOrDie("ldrtl-v21");
71 // Chosen because this configuration is compatible with ldrtl.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070072 const ConfigDescription land_config = test::ParseConfigOrDie("land");
Alexandria Cornwall77788eb2016-09-06 15:16:49 -070073
Adam Lesinskicacb28f2016-10-19 12:18:14 -070074 std::unique_ptr<ResourceTable> table =
75 test::ResourceTableBuilder()
Adam Lesinskie3856742017-06-12 14:55:58 -070076 .AddString("android:string/keep", ResourceId{}, default_config, "keep")
77 .AddString("android:string/keep", ResourceId{}, ldrtl_config, "keep")
78 .AddString("android:string/keep", ResourceId{}, ldrtl_v21_config, "keep2")
Adam Lesinskice5e56e2016-10-21 17:56:45 -070079 .AddString("android:string/keep", ResourceId{}, land_config, "keep2")
80 .Build();
Alexandria Cornwall77788eb2016-09-06 15:16:49 -070081
Adam Lesinskice5e56e2016-10-21 17:56:45 -070082 ASSERT_TRUE(ResourceDeduper().Consume(context.get(), table.get()));
Adam Lesinskie3856742017-06-12 14:55:58 -070083 EXPECT_THAT(table, HasValue("android:string/keep", ldrtl_config));
84 EXPECT_THAT(table, HasValue("android:string/keep", ldrtl_v21_config));
85 EXPECT_THAT(table, HasValue("android:string/keep", land_config));
86}
87
88TEST(ResourceDeduperTest, LocalesValuesAreKept) {
89 std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build();
90 const ConfigDescription default_config = {};
91 const ConfigDescription fr_config = test::ParseConfigOrDie("fr");
92 const ConfigDescription fr_rCA_config = test::ParseConfigOrDie("fr-rCA");
93
94 std::unique_ptr<ResourceTable> table =
95 test::ResourceTableBuilder()
96 .AddString("android:string/keep", ResourceId{}, default_config, "keep")
97 .AddString("android:string/keep", ResourceId{}, fr_config, "keep")
98 .AddString("android:string/keep", ResourceId{}, fr_rCA_config, "keep")
99 .Build();
100
101 ASSERT_TRUE(ResourceDeduper().Consume(context.get(), table.get()));
102 EXPECT_THAT(table, HasValue("android:string/keep", default_config));
103 EXPECT_THAT(table, HasValue("android:string/keep", fr_config));
104 EXPECT_THAT(table, HasValue("android:string/keep", fr_rCA_config));
Alexandria Cornwall77788eb2016-09-06 15:16:49 -0700105}
106
107} // namespace aapt