blob: 1b5592f717d9135fdd567aea7cbb1e48a0aea0c8 [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"
Adam Lesinskice5e56e22016-10-21 17:56:45 -070018
Adam Lesinskifb6312f2016-06-28 14:40:32 -070019#include "test/Test.h"
20
21namespace aapt {
22
Adam Lesinskice5e56e22016-10-21 17:56:45 -070023static std::unique_ptr<ResourceTable> BuildTableWithConfigs(
Adam Lesinskicacb28f2016-10-19 12:18:14 -070024 const StringPiece& name, std::initializer_list<std::string> list) {
25 test::ResourceTableBuilder builder;
26 for (const std::string& item : list) {
Adam Lesinskice5e56e22016-10-21 17:56:45 -070027 builder.AddSimple(name, test::ParseConfigOrDie(item));
Adam Lesinskicacb28f2016-10-19 12:18:14 -070028 }
Adam Lesinskice5e56e22016-10-21 17:56:45 -070029 return builder.Build();
Adam Lesinskifb6312f2016-06-28 14:40:32 -070030}
31
32TEST(VersionCollapserTest, CollapseVersions) {
Adam Lesinskice5e56e22016-10-21 17:56:45 -070033 std::unique_ptr<IAaptContext> context =
34 test::ContextBuilder().SetMinSdkVersion(7).Build();
Adam Lesinskifb6312f2016-06-28 14:40:32 -070035
Adam Lesinskice5e56e22016-10-21 17:56:45 -070036 const StringPiece res_name = "@android:string/foo";
Adam Lesinskifb6312f2016-06-28 14:40:32 -070037
Adam Lesinskice5e56e22016-10-21 17:56:45 -070038 std::unique_ptr<ResourceTable> table = BuildTableWithConfigs(
39 res_name,
Adam Lesinskicacb28f2016-10-19 12:18:14 -070040 {"land-v4", "land-v5", "sw600dp", "land-v6", "land-v14", "land-v21"});
Adam Lesinskifb6312f2016-06-28 14:40:32 -070041
Adam Lesinskicacb28f2016-10-19 12:18:14 -070042 VersionCollapser collapser;
Adam Lesinskice5e56e22016-10-21 17:56:45 -070043 ASSERT_TRUE(collapser.Consume(context.get(), table.get()));
Adam Lesinskifb6312f2016-06-28 14:40:32 -070044
Adam Lesinskicacb28f2016-10-19 12:18:14 -070045 // These should be removed.
46 EXPECT_EQ(nullptr,
Adam Lesinskice5e56e22016-10-21 17:56:45 -070047 test::GetValueForConfig<Id>(table.get(), res_name,
48 test::ParseConfigOrDie("land-v4")));
Adam Lesinskicacb28f2016-10-19 12:18:14 -070049 EXPECT_EQ(nullptr,
Adam Lesinskice5e56e22016-10-21 17:56:45 -070050 test::GetValueForConfig<Id>(table.get(), res_name,
51 test::ParseConfigOrDie("land-v5")));
Adam Lesinskicacb28f2016-10-19 12:18:14 -070052 // This one should be removed because it was renamed to 'land', with the
53 // version dropped.
54 EXPECT_EQ(nullptr,
Adam Lesinskice5e56e22016-10-21 17:56:45 -070055 test::GetValueForConfig<Id>(table.get(), res_name,
56 test::ParseConfigOrDie("land-v6")));
Adam Lesinskifb6312f2016-06-28 14:40:32 -070057
Adam Lesinskicacb28f2016-10-19 12:18:14 -070058 // These should remain.
59 EXPECT_NE(nullptr,
Adam Lesinskice5e56e22016-10-21 17:56:45 -070060 test::GetValueForConfig<Id>(table.get(), res_name,
61 test::ParseConfigOrDie("sw600dp")));
Adam Lesinski87675ad2016-07-15 17:03:03 -070062
Adam Lesinskicacb28f2016-10-19 12:18:14 -070063 // 'land' should be present because it was renamed from 'land-v6'.
Adam Lesinskicacb28f2016-10-19 12:18:14 -070064 EXPECT_NE(nullptr,
Adam Lesinskice5e56e22016-10-21 17:56:45 -070065 test::GetValueForConfig<Id>(table.get(), res_name,
66 test::ParseConfigOrDie("land")));
Adam Lesinskicacb28f2016-10-19 12:18:14 -070067 EXPECT_NE(nullptr,
Adam Lesinskice5e56e22016-10-21 17:56:45 -070068 test::GetValueForConfig<Id>(table.get(), res_name,
69 test::ParseConfigOrDie("land-v14")));
70 EXPECT_NE(nullptr,
71 test::GetValueForConfig<Id>(table.get(), res_name,
72 test::ParseConfigOrDie("land-v21")));
Adam Lesinskifb6312f2016-06-28 14:40:32 -070073}
74
75TEST(VersionCollapserTest, CollapseVersionsWhenMinSdkIsHighest) {
Adam Lesinskice5e56e22016-10-21 17:56:45 -070076 std::unique_ptr<IAaptContext> context =
77 test::ContextBuilder().SetMinSdkVersion(21).Build();
Adam Lesinskifb6312f2016-06-28 14:40:32 -070078
Adam Lesinskice5e56e22016-10-21 17:56:45 -070079 const StringPiece res_name = "@android:string/foo";
Adam Lesinskifb6312f2016-06-28 14:40:32 -070080
Adam Lesinskice5e56e22016-10-21 17:56:45 -070081 std::unique_ptr<ResourceTable> table = BuildTableWithConfigs(
82 res_name, {"land-v4", "land-v5", "sw600dp", "land-v6", "land-v14",
83 "land-v21", "land-v22"});
Adam Lesinskicacb28f2016-10-19 12:18:14 -070084 VersionCollapser collapser;
Adam Lesinskice5e56e22016-10-21 17:56:45 -070085 ASSERT_TRUE(collapser.Consume(context.get(), table.get()));
Adam Lesinskifb6312f2016-06-28 14:40:32 -070086
Adam Lesinskicacb28f2016-10-19 12:18:14 -070087 // These should all be removed.
88 EXPECT_EQ(nullptr,
Adam Lesinskice5e56e22016-10-21 17:56:45 -070089 test::GetValueForConfig<Id>(table.get(), res_name,
90 test::ParseConfigOrDie("land-v4")));
Adam Lesinskicacb28f2016-10-19 12:18:14 -070091 EXPECT_EQ(nullptr,
Adam Lesinskice5e56e22016-10-21 17:56:45 -070092 test::GetValueForConfig<Id>(table.get(), res_name,
93 test::ParseConfigOrDie("land-v5")));
Adam Lesinskicacb28f2016-10-19 12:18:14 -070094 EXPECT_EQ(nullptr,
Adam Lesinskice5e56e22016-10-21 17:56:45 -070095 test::GetValueForConfig<Id>(table.get(), res_name,
96 test::ParseConfigOrDie("land-v6")));
Adam Lesinskicacb28f2016-10-19 12:18:14 -070097 EXPECT_EQ(nullptr,
Adam Lesinskice5e56e22016-10-21 17:56:45 -070098 test::GetValueForConfig<Id>(table.get(), res_name,
99 test::ParseConfigOrDie("land-v14")));
Adam Lesinskifb6312f2016-06-28 14:40:32 -0700100
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700101 // These should remain.
102 EXPECT_NE(nullptr,
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700103 test::GetValueForConfig<Id>(
104 table.get(), res_name,
105 test::ParseConfigOrDie("sw600dp").CopyWithoutSdkVersion()));
Adam Lesinski87675ad2016-07-15 17:03:03 -0700106
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700107 // land-v21 should have been converted to land.
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700108 EXPECT_NE(nullptr,
109 test::GetValueForConfig<Id>(table.get(), res_name,
110 test::ParseConfigOrDie("land")));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700111 // land-v22 should remain as-is.
112 EXPECT_NE(nullptr,
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700113 test::GetValueForConfig<Id>(table.get(), res_name,
114 test::ParseConfigOrDie("land-v22")));
Adam Lesinskifb6312f2016-06-28 14:40:32 -0700115}
116
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700117} // namespace aapt