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 "optimize/ResourceDeduper.h"
18
19 #include "ResourceTable.h"
20 #include "test/Test.h"
21
22 using ::aapt::test::HasValue;
23 using ::android::ConfigDescription;
24 using ::testing::Not;
25
26 namespace aapt {
27
TEST(ResourceDeduperTest,SameValuesAreDeduped)28 TEST(ResourceDeduperTest, SameValuesAreDeduped) {
29 std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build();
30 const ConfigDescription default_config = {};
31 const ConfigDescription ldrtl_config = test::ParseConfigOrDie("ldrtl");
32 const ConfigDescription ldrtl_v21_config = test::ParseConfigOrDie("ldrtl-v21");
33 const ConfigDescription en_config = test::ParseConfigOrDie("en");
34 const ConfigDescription en_v21_config = test::ParseConfigOrDie("en-v21");
35 // Chosen because this configuration is compatible with ldrtl/en.
36 const ConfigDescription land_config = test::ParseConfigOrDie("land");
37
38 std::unique_ptr<ResourceTable> table =
39 test::ResourceTableBuilder()
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")
52 .Build();
53
54 ASSERT_TRUE(ResourceDeduper().Consume(context.get(), table.get()));
55 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)));
64 }
65
TEST(ResourceDeduperTest,DifferentValuesAreKept)66 TEST(ResourceDeduperTest, DifferentValuesAreKept) {
67 std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build();
68 const ConfigDescription default_config = {};
69 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.
72 const ConfigDescription land_config = test::ParseConfigOrDie("land");
73
74 std::unique_ptr<ResourceTable> table =
75 test::ResourceTableBuilder()
76 .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")
79 .AddString("android:string/keep", ResourceId{}, land_config, "keep2")
80 .Build();
81
82 ASSERT_TRUE(ResourceDeduper().Consume(context.get(), table.get()));
83 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
TEST(ResourceDeduperTest,LocalesValuesAreKept)88 TEST(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));
105 }
106
107 } // namespace aapt
108