summaryrefslogtreecommitdiff
path: root/tools/aapt2/optimize/ResourceDeduper_test.cpp
blob: 4d00fa6efe37fc3fc8d207a099ca07dd0d9df523 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/*
 * Copyright (C) 2016 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "optimize/ResourceDeduper.h"

#include "ResourceTable.h"
#include "test/Test.h"

namespace aapt {

TEST(ResourceDeduperTest, SameValuesAreDeduped) {
  std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build();
  const ConfigDescription default_config = {};
  const ConfigDescription en_config = test::ParseConfigOrDie("en");
  const ConfigDescription en_v21_config = test::ParseConfigOrDie("en-v21");
  // Chosen because this configuration is compatible with en.
  const ConfigDescription land_config = test::ParseConfigOrDie("land");

  std::unique_ptr<ResourceTable> table =
      test::ResourceTableBuilder()
          .AddString("android:string/dedupe", ResourceId{}, default_config,
                     "dedupe")
          .AddString("android:string/dedupe", ResourceId{}, en_config, "dedupe")
          .AddString("android:string/dedupe", ResourceId{}, land_config,
                     "dedupe")
          .AddString("android:string/dedupe2", ResourceId{}, default_config,
                     "dedupe")
          .AddString("android:string/dedupe2", ResourceId{}, en_config,
                     "dedupe")
          .AddString("android:string/dedupe2", ResourceId{}, en_v21_config,
                     "keep")
          .AddString("android:string/dedupe2", ResourceId{}, land_config,
                     "dedupe")
          .Build();

  ASSERT_TRUE(ResourceDeduper().Consume(context.get(), table.get()));
  EXPECT_EQ(nullptr, test::GetValueForConfig<String>(
                         table.get(), "android:string/dedupe", en_config));
  EXPECT_EQ(nullptr, test::GetValueForConfig<String>(
                         table.get(), "android:string/dedupe", land_config));
  EXPECT_EQ(nullptr, test::GetValueForConfig<String>(
                         table.get(), "android:string/dedupe2", en_config));
  EXPECT_NE(nullptr, test::GetValueForConfig<String>(
                         table.get(), "android:string/dedupe2", en_v21_config));
}

TEST(ResourceDeduperTest, DifferentValuesAreKept) {
  std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build();
  const ConfigDescription default_config = {};
  const ConfigDescription en_config = test::ParseConfigOrDie("en");
  const ConfigDescription en_v21_config = test::ParseConfigOrDie("en-v21");
  // Chosen because this configuration is compatible with en.
  const ConfigDescription land_config = test::ParseConfigOrDie("land");

  std::unique_ptr<ResourceTable> table =
      test::ResourceTableBuilder()
          .AddString("android:string/keep", ResourceId{}, default_config,
                     "keep")
          .AddString("android:string/keep", ResourceId{}, en_config, "keep")
          .AddString("android:string/keep", ResourceId{}, en_v21_config,
                     "keep2")
          .AddString("android:string/keep", ResourceId{}, land_config, "keep2")
          .Build();

  ASSERT_TRUE(ResourceDeduper().Consume(context.get(), table.get()));
  EXPECT_NE(nullptr, test::GetValueForConfig<String>(
                         table.get(), "android:string/keep", en_config));
  EXPECT_NE(nullptr, test::GetValueForConfig<String>(
                         table.get(), "android:string/keep", en_v21_config));
  EXPECT_NE(nullptr, test::GetValueForConfig<String>(
                         table.get(), "android:string/keep", land_config));
}

}  // namespace aapt