blob: c0e0ddb2d318fa57836ecf19c8c400e0602e9397 [file] [log] [blame]
Adam Lesinskifb6312f2016-06-28 14:40:32 -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
17#include "link/Linkers.h"
18#include "test/Test.h"
19
20namespace aapt {
21
22template <typename T>
23using uptr = std::unique_ptr<T>;
24
Adam Lesinskicacb28f2016-10-19 12:18:14 -070025static uptr<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();
Adam Lesinskifb6312f2016-06-28 14:40:32 -070032}
33
34TEST(VersionCollapserTest, CollapseVersions) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070035 uptr<IAaptContext> context =
36 test::ContextBuilder().setMinSdkVersion(7).build();
Adam Lesinskifb6312f2016-06-28 14:40:32 -070037
Adam Lesinskicacb28f2016-10-19 12:18:14 -070038 const StringPiece resName = "@android:string/foo";
Adam Lesinskifb6312f2016-06-28 14:40:32 -070039
Adam Lesinskicacb28f2016-10-19 12:18:14 -070040 uptr<ResourceTable> table = buildTableWithConfigs(
41 resName,
42 {"land-v4", "land-v5", "sw600dp", "land-v6", "land-v14", "land-v21"});
Adam Lesinskifb6312f2016-06-28 14:40:32 -070043
Adam Lesinskicacb28f2016-10-19 12:18:14 -070044 VersionCollapser collapser;
45 ASSERT_TRUE(collapser.consume(context.get(), table.get()));
Adam Lesinskifb6312f2016-06-28 14:40:32 -070046
Adam Lesinskicacb28f2016-10-19 12:18:14 -070047 // These should be removed.
48 EXPECT_EQ(nullptr,
49 test::getValueForConfig<Id>(table.get(), resName,
50 test::parseConfigOrDie("land-v4")));
51 EXPECT_EQ(nullptr,
52 test::getValueForConfig<Id>(table.get(), resName,
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(), resName,
58 test::parseConfigOrDie("land-v6")));
Adam Lesinskifb6312f2016-06-28 14:40:32 -070059
Adam Lesinskicacb28f2016-10-19 12:18:14 -070060 // These should remain.
61 EXPECT_NE(nullptr,
62 test::getValueForConfig<Id>(table.get(), resName,
63 test::parseConfigOrDie("sw600dp")));
Adam Lesinski87675ad2016-07-15 17:03:03 -070064
Adam Lesinskicacb28f2016-10-19 12:18:14 -070065 // 'land' should be present because it was renamed from 'land-v6'.
66 EXPECT_NE(nullptr, test::getValueForConfig<Id>(
67 table.get(), resName, test::parseConfigOrDie("land")));
68 EXPECT_NE(nullptr,
69 test::getValueForConfig<Id>(table.get(), resName,
70 test::parseConfigOrDie("land-v14")));
71 EXPECT_NE(nullptr,
72 test::getValueForConfig<Id>(table.get(), resName,
73 test::parseConfigOrDie("land-v21")));
Adam Lesinskifb6312f2016-06-28 14:40:32 -070074}
75
76TEST(VersionCollapserTest, CollapseVersionsWhenMinSdkIsHighest) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070077 uptr<IAaptContext> context =
78 test::ContextBuilder().setMinSdkVersion(21).build();
Adam Lesinskifb6312f2016-06-28 14:40:32 -070079
Adam Lesinskicacb28f2016-10-19 12:18:14 -070080 const StringPiece resName = "@android:string/foo";
Adam Lesinskifb6312f2016-06-28 14:40:32 -070081
Adam Lesinskicacb28f2016-10-19 12:18:14 -070082 uptr<ResourceTable> table = buildTableWithConfigs(
83 resName, {"land-v4", "land-v5", "sw600dp", "land-v6", "land-v14",
84 "land-v21", "land-v22"});
85 VersionCollapser collapser;
86 ASSERT_TRUE(collapser.consume(context.get(), table.get()));
Adam Lesinskifb6312f2016-06-28 14:40:32 -070087
Adam Lesinskicacb28f2016-10-19 12:18:14 -070088 // These should all be removed.
89 EXPECT_EQ(nullptr,
90 test::getValueForConfig<Id>(table.get(), resName,
91 test::parseConfigOrDie("land-v4")));
92 EXPECT_EQ(nullptr,
93 test::getValueForConfig<Id>(table.get(), resName,
94 test::parseConfigOrDie("land-v5")));
95 EXPECT_EQ(nullptr,
96 test::getValueForConfig<Id>(table.get(), resName,
97 test::parseConfigOrDie("land-v6")));
98 EXPECT_EQ(nullptr,
99 test::getValueForConfig<Id>(table.get(), resName,
100 test::parseConfigOrDie("land-v14")));
Adam Lesinskifb6312f2016-06-28 14:40:32 -0700101
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700102 // These should remain.
103 EXPECT_NE(nullptr,
104 test::getValueForConfig<Id>(
105 table.get(), resName,
106 test::parseConfigOrDie("sw600dp").copyWithoutSdkVersion()));
Adam Lesinski87675ad2016-07-15 17:03:03 -0700107
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700108 // land-v21 should have been converted to land.
109 EXPECT_NE(nullptr, test::getValueForConfig<Id>(
110 table.get(), resName, test::parseConfigOrDie("land")));
111 // land-v22 should remain as-is.
112 EXPECT_NE(nullptr,
113 test::getValueForConfig<Id>(table.get(), resName,
114 test::parseConfigOrDie("land-v22")));
Adam Lesinskifb6312f2016-06-28 14:40:32 -0700115}
116
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700117} // namespace aapt