blob: 4d00fa6efe37fc3fc8d207a099ca07dd0d9df523 [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
22namespace aapt {
23
24TEST(ResourceDeduperTest, SameValuesAreDeduped) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070025 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 Lesinskicacb28f2016-10-19 12:18:14 -070029 // Chosen because this configuration is compatible with en.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070030 const ConfigDescription land_config = test::ParseConfigOrDie("land");
Alexandria Cornwall77788eb2016-09-06 15:16:49 -070031
Adam Lesinskicacb28f2016-10-19 12:18:14 -070032 std::unique_ptr<ResourceTable> table =
33 test::ResourceTableBuilder()
Adam Lesinskice5e56e2016-10-21 17:56:45 -070034 .AddString("android:string/dedupe", ResourceId{}, default_config,
Adam Lesinskicacb28f2016-10-19 12:18:14 -070035 "dedupe")
Adam Lesinskice5e56e2016-10-21 17:56:45 -070036 .AddString("android:string/dedupe", ResourceId{}, en_config, "dedupe")
37 .AddString("android:string/dedupe", ResourceId{}, land_config,
Adam Lesinskicacb28f2016-10-19 12:18:14 -070038 "dedupe")
Adam Lesinskice5e56e2016-10-21 17:56:45 -070039 .AddString("android:string/dedupe2", ResourceId{}, default_config,
Adam Lesinskicacb28f2016-10-19 12:18:14 -070040 "dedupe")
Adam Lesinskice5e56e2016-10-21 17:56:45 -070041 .AddString("android:string/dedupe2", ResourceId{}, en_config,
42 "dedupe")
43 .AddString("android:string/dedupe2", ResourceId{}, en_v21_config,
Adam Lesinskicacb28f2016-10-19 12:18:14 -070044 "keep")
Adam Lesinskice5e56e2016-10-21 17:56:45 -070045 .AddString("android:string/dedupe2", ResourceId{}, land_config,
Adam Lesinskicacb28f2016-10-19 12:18:14 -070046 "dedupe")
Adam Lesinskice5e56e2016-10-21 17:56:45 -070047 .Build();
Alexandria Cornwall77788eb2016-09-06 15:16:49 -070048
Adam Lesinskice5e56e2016-10-21 17:56:45 -070049 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 Cornwall77788eb2016-09-06 15:16:49 -070058}
59
60TEST(ResourceDeduperTest, DifferentValuesAreKept) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070061 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 Lesinskicacb28f2016-10-19 12:18:14 -070065 // Chosen because this configuration is compatible with en.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070066 const ConfigDescription land_config = test::ParseConfigOrDie("land");
Alexandria Cornwall77788eb2016-09-06 15:16:49 -070067
Adam Lesinskicacb28f2016-10-19 12:18:14 -070068 std::unique_ptr<ResourceTable> table =
69 test::ResourceTableBuilder()
Adam Lesinskice5e56e2016-10-21 17:56:45 -070070 .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 Cornwall77788eb2016-09-06 15:16:49 -070077
Adam Lesinskice5e56e2016-10-21 17:56:45 -070078 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 Cornwall77788eb2016-09-06 15:16:49 -070085}
86
87} // namespace aapt