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