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/VersionCollapser.h"
18
19#include "test/Test.h"
20
21using android::StringPiece;
22
23namespace aapt {
24
25static std::unique_ptr<ResourceTable> BuildTableWithConfigs(
26    const StringPiece& name, std::initializer_list<std::string> list) {
27  test::ResourceTableBuilder builder;
28  for (const std::string& item : list) {
29    builder.AddSimple(name, test::ParseConfigOrDie(item));
30  }
31  return builder.Build();
32}
33
34TEST(VersionCollapserTest, CollapseVersions) {
35  std::unique_ptr<IAaptContext> context =
36      test::ContextBuilder().SetMinSdkVersion(7).Build();
37
38  const StringPiece res_name = "@android:string/foo";
39
40  std::unique_ptr<ResourceTable> table = BuildTableWithConfigs(
41      res_name,
42      {"land-v4", "land-v5", "sw600dp", "land-v6", "land-v14", "land-v21"});
43
44  VersionCollapser collapser;
45  ASSERT_TRUE(collapser.Consume(context.get(), table.get()));
46
47  // These should be removed.
48  EXPECT_EQ(nullptr,
49            test::GetValueForConfig<Id>(table.get(), res_name,
50                                        test::ParseConfigOrDie("land-v4")));
51  EXPECT_EQ(nullptr,
52            test::GetValueForConfig<Id>(table.get(), res_name,
53                                        test::ParseConfigOrDie("land-v5")));
54  // This one should be removed because it was renamed to 'land', with the
55  // version dropped.
56  EXPECT_EQ(nullptr,
57            test::GetValueForConfig<Id>(table.get(), res_name,
58                                        test::ParseConfigOrDie("land-v6")));
59
60  // These should remain.
61  EXPECT_NE(nullptr,
62            test::GetValueForConfig<Id>(table.get(), res_name,
63                                        test::ParseConfigOrDie("sw600dp")));
64
65  // 'land' should be present because it was renamed from 'land-v6'.
66  EXPECT_NE(nullptr,
67            test::GetValueForConfig<Id>(table.get(), res_name,
68                                        test::ParseConfigOrDie("land")));
69  EXPECT_NE(nullptr,
70            test::GetValueForConfig<Id>(table.get(), res_name,
71                                        test::ParseConfigOrDie("land-v14")));
72  EXPECT_NE(nullptr,
73            test::GetValueForConfig<Id>(table.get(), res_name,
74                                        test::ParseConfigOrDie("land-v21")));
75}
76
77TEST(VersionCollapserTest, CollapseVersionsWhenMinSdkIsHighest) {
78  std::unique_ptr<IAaptContext> context =
79      test::ContextBuilder().SetMinSdkVersion(21).Build();
80
81  const StringPiece res_name = "@android:string/foo";
82
83  std::unique_ptr<ResourceTable> table = BuildTableWithConfigs(
84      res_name, {"land-v4", "land-v5", "sw600dp", "land-v6", "land-v14",
85                 "land-v21", "land-v22"});
86  VersionCollapser collapser;
87  ASSERT_TRUE(collapser.Consume(context.get(), table.get()));
88
89  // These should all be removed.
90  EXPECT_EQ(nullptr,
91            test::GetValueForConfig<Id>(table.get(), res_name,
92                                        test::ParseConfigOrDie("land-v4")));
93  EXPECT_EQ(nullptr,
94            test::GetValueForConfig<Id>(table.get(), res_name,
95                                        test::ParseConfigOrDie("land-v5")));
96  EXPECT_EQ(nullptr,
97            test::GetValueForConfig<Id>(table.get(), res_name,
98                                        test::ParseConfigOrDie("land-v6")));
99  EXPECT_EQ(nullptr,
100            test::GetValueForConfig<Id>(table.get(), res_name,
101                                        test::ParseConfigOrDie("land-v14")));
102
103  // These should remain.
104  EXPECT_NE(nullptr,
105            test::GetValueForConfig<Id>(
106                table.get(), res_name,
107                test::ParseConfigOrDie("sw600dp").CopyWithoutSdkVersion()));
108
109  // land-v21 should have been converted to land.
110  EXPECT_NE(nullptr,
111            test::GetValueForConfig<Id>(table.get(), res_name,
112                                        test::ParseConfigOrDie("land")));
113  // land-v22 should remain as-is.
114  EXPECT_NE(nullptr,
115            test::GetValueForConfig<Id>(table.get(), res_name,
116                                        test::ParseConfigOrDie("land-v22")));
117}
118
119}  // namespace aapt
120