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