blob: aa0d0c095f5738a8819e02bc5a469dc5291d2aa0 [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
Adam Lesinskid48944a2017-02-21 14:22:30 -080017#include "optimize/VersionCollapser.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070018
Adam Lesinskifb6312f2016-06-28 14:40:32 -070019#include "test/Test.h"
20
Adam Lesinskid5083f62017-01-16 15:07:21 -080021using android::StringPiece;
22
Adam Lesinskifb6312f2016-06-28 14:40:32 -070023namespace aapt {
24
Adam Lesinskice5e56e2016-10-21 17:56:45 -070025static std::unique_ptr<ResourceTable> BuildTableWithConfigs(
Adam Lesinskicacb28f2016-10-19 12:18:14 -070026 const StringPiece& name, std::initializer_list<std::string> list) {
27 test::ResourceTableBuilder builder;
28 for (const std::string& item : list) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070029 builder.AddSimple(name, test::ParseConfigOrDie(item));
Adam Lesinskicacb28f2016-10-19 12:18:14 -070030 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -070031 return builder.Build();
Adam Lesinskifb6312f2016-06-28 14:40:32 -070032}
33
34TEST(VersionCollapserTest, CollapseVersions) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070035 std::unique_ptr<IAaptContext> context =
36 test::ContextBuilder().SetMinSdkVersion(7).Build();
Adam Lesinskifb6312f2016-06-28 14:40:32 -070037
Adam Lesinskice5e56e2016-10-21 17:56:45 -070038 const StringPiece res_name = "@android:string/foo";
Adam Lesinskifb6312f2016-06-28 14:40:32 -070039
Adam Lesinskice5e56e2016-10-21 17:56:45 -070040 std::unique_ptr<ResourceTable> table = BuildTableWithConfigs(
41 res_name,
Adam Lesinskicacb28f2016-10-19 12:18:14 -070042 {"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;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070045 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,
Adam Lesinskice5e56e2016-10-21 17:56:45 -070049 test::GetValueForConfig<Id>(table.get(), res_name,
50 test::ParseConfigOrDie("land-v4")));
Adam Lesinskicacb28f2016-10-19 12:18:14 -070051 EXPECT_EQ(nullptr,
Adam Lesinskice5e56e2016-10-21 17:56:45 -070052 test::GetValueForConfig<Id>(table.get(), res_name,
53 test::ParseConfigOrDie("land-v5")));
Adam Lesinskicacb28f2016-10-19 12:18:14 -070054 // This one should be removed because it was renamed to 'land', with the
55 // version dropped.
56 EXPECT_EQ(nullptr,
Adam Lesinskice5e56e2016-10-21 17:56:45 -070057 test::GetValueForConfig<Id>(table.get(), res_name,
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,
Adam Lesinskice5e56e2016-10-21 17:56:45 -070062 test::GetValueForConfig<Id>(table.get(), res_name,
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'.
Adam Lesinskicacb28f2016-10-19 12:18:14 -070066 EXPECT_NE(nullptr,
Adam Lesinskice5e56e2016-10-21 17:56:45 -070067 test::GetValueForConfig<Id>(table.get(), res_name,
68 test::ParseConfigOrDie("land")));
Adam Lesinskicacb28f2016-10-19 12:18:14 -070069 EXPECT_NE(nullptr,
Adam Lesinskice5e56e2016-10-21 17:56:45 -070070 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")));
Adam Lesinskifb6312f2016-06-28 14:40:32 -070075}
76
77TEST(VersionCollapserTest, CollapseVersionsWhenMinSdkIsHighest) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070078 std::unique_ptr<IAaptContext> context =
79 test::ContextBuilder().SetMinSdkVersion(21).Build();
Adam Lesinskifb6312f2016-06-28 14:40:32 -070080
Adam Lesinskice5e56e2016-10-21 17:56:45 -070081 const StringPiece res_name = "@android:string/foo";
Adam Lesinskifb6312f2016-06-28 14:40:32 -070082
Adam Lesinskice5e56e2016-10-21 17:56:45 -070083 std::unique_ptr<ResourceTable> table = BuildTableWithConfigs(
84 res_name, {"land-v4", "land-v5", "sw600dp", "land-v6", "land-v14",
85 "land-v21", "land-v22"});
Adam Lesinskicacb28f2016-10-19 12:18:14 -070086 VersionCollapser collapser;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070087 ASSERT_TRUE(collapser.Consume(context.get(), table.get()));
Adam Lesinskifb6312f2016-06-28 14:40:32 -070088
Adam Lesinskicacb28f2016-10-19 12:18:14 -070089 // These should all be removed.
90 EXPECT_EQ(nullptr,
Adam Lesinskice5e56e2016-10-21 17:56:45 -070091 test::GetValueForConfig<Id>(table.get(), res_name,
92 test::ParseConfigOrDie("land-v4")));
Adam Lesinskicacb28f2016-10-19 12:18:14 -070093 EXPECT_EQ(nullptr,
Adam Lesinskice5e56e2016-10-21 17:56:45 -070094 test::GetValueForConfig<Id>(table.get(), res_name,
95 test::ParseConfigOrDie("land-v5")));
Adam Lesinskicacb28f2016-10-19 12:18:14 -070096 EXPECT_EQ(nullptr,
Adam Lesinskice5e56e2016-10-21 17:56:45 -070097 test::GetValueForConfig<Id>(table.get(), res_name,
98 test::ParseConfigOrDie("land-v6")));
Adam Lesinskicacb28f2016-10-19 12:18:14 -070099 EXPECT_EQ(nullptr,
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700100 test::GetValueForConfig<Id>(table.get(), res_name,
101 test::ParseConfigOrDie("land-v14")));
Adam Lesinskifb6312f2016-06-28 14:40:32 -0700102
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700103 // These should remain.
104 EXPECT_NE(nullptr,
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700105 test::GetValueForConfig<Id>(
106 table.get(), res_name,
107 test::ParseConfigOrDie("sw600dp").CopyWithoutSdkVersion()));
Adam Lesinski87675ad2016-07-15 17:03:03 -0700108
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700109 // land-v21 should have been converted to land.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700110 EXPECT_NE(nullptr,
111 test::GetValueForConfig<Id>(table.get(), res_name,
112 test::ParseConfigOrDie("land")));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700113 // land-v22 should remain as-is.
114 EXPECT_NE(nullptr,
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700115 test::GetValueForConfig<Id>(table.get(), res_name,
116 test::ParseConfigOrDie("land-v22")));
Adam Lesinskifb6312f2016-06-28 14:40:32 -0700117}
118
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700119} // namespace aapt