blob: d9f384c095cab604ae06c83d61bf68a8f3875243 [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 Lesinski90739912017-06-12 14:55:58 -070022using ::aapt::test::HasValue;
23using ::testing::Not;
24
Alexandria Cornwall77788eb2016-09-06 15:16:49 -070025namespace aapt {
26
27TEST(ResourceDeduperTest, SameValuesAreDeduped) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070028 std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build();
29 const ConfigDescription default_config = {};
Adam Lesinski90739912017-06-12 14:55:58 -070030 const ConfigDescription ldrtl_config = test::ParseConfigOrDie("ldrtl");
31 const ConfigDescription ldrtl_v21_config = test::ParseConfigOrDie("ldrtl-v21");
Adam Lesinskice5e56e2016-10-21 17:56:45 -070032 const ConfigDescription en_config = test::ParseConfigOrDie("en");
33 const ConfigDescription en_v21_config = test::ParseConfigOrDie("en-v21");
Adam Lesinski90739912017-06-12 14:55:58 -070034 // Chosen because this configuration is compatible with ldrtl/en.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070035 const ConfigDescription land_config = test::ParseConfigOrDie("land");
Alexandria Cornwall77788eb2016-09-06 15:16:49 -070036
Adam Lesinskicacb28f2016-10-19 12:18:14 -070037 std::unique_ptr<ResourceTable> table =
38 test::ResourceTableBuilder()
Adam Lesinski90739912017-06-12 14:55:58 -070039 .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 Lesinskice5e56e2016-10-21 17:56:45 -070051 .Build();
Alexandria Cornwall77788eb2016-09-06 15:16:49 -070052
Adam Lesinskice5e56e2016-10-21 17:56:45 -070053 ASSERT_TRUE(ResourceDeduper().Consume(context.get(), table.get()));
Adam Lesinski90739912017-06-12 14:55:58 -070054 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 Cornwall77788eb2016-09-06 15:16:49 -070063}
64
65TEST(ResourceDeduperTest, DifferentValuesAreKept) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070066 std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build();
67 const ConfigDescription default_config = {};
Adam Lesinski90739912017-06-12 14:55:58 -070068 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 Lesinskice5e56e2016-10-21 17:56:45 -070071 const ConfigDescription land_config = test::ParseConfigOrDie("land");
Alexandria Cornwall77788eb2016-09-06 15:16:49 -070072
Adam Lesinskicacb28f2016-10-19 12:18:14 -070073 std::unique_ptr<ResourceTable> table =
74 test::ResourceTableBuilder()
Adam Lesinski90739912017-06-12 14:55:58 -070075 .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 Lesinskice5e56e2016-10-21 17:56:45 -070078 .AddString("android:string/keep", ResourceId{}, land_config, "keep2")
79 .Build();
Alexandria Cornwall77788eb2016-09-06 15:16:49 -070080
Adam Lesinskice5e56e2016-10-21 17:56:45 -070081 ASSERT_TRUE(ResourceDeduper().Consume(context.get(), table.get()));
Adam Lesinski90739912017-06-12 14:55:58 -070082 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
87TEST(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 Cornwall77788eb2016-09-06 15:16:49 -0700104}
105
106} // namespace aapt