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" |
| 18 | #include "test/Test.h" |
| 19 | |
| 20 | namespace aapt { |
| 21 | |
| 22 | template <typename T> |
| 23 | using uptr = std::unique_ptr<T>; |
| 24 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 25 | static 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 Lesinski | fb6312f | 2016-06-28 14:40:32 -0700 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | TEST(VersionCollapserTest, CollapseVersions) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 35 | uptr<IAaptContext> context = |
| 36 | test::ContextBuilder().setMinSdkVersion(7).build(); |
Adam Lesinski | fb6312f | 2016-06-28 14:40:32 -0700 | [diff] [blame] | 37 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 38 | const StringPiece resName = "@android:string/foo"; |
Adam Lesinski | fb6312f | 2016-06-28 14:40:32 -0700 | [diff] [blame] | 39 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 40 | uptr<ResourceTable> table = buildTableWithConfigs( |
| 41 | resName, |
| 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; |
| 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, |
| 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 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, |
| 62 | test::getValueForConfig<Id>(table.get(), resName, |
| 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'. |
| 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 Lesinski | fb6312f | 2016-06-28 14:40:32 -0700 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | TEST(VersionCollapserTest, CollapseVersionsWhenMinSdkIsHighest) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 77 | uptr<IAaptContext> context = |
| 78 | test::ContextBuilder().setMinSdkVersion(21).build(); |
Adam Lesinski | fb6312f | 2016-06-28 14:40:32 -0700 | [diff] [blame] | 79 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 80 | const StringPiece resName = "@android:string/foo"; |
Adam Lesinski | fb6312f | 2016-06-28 14:40:32 -0700 | [diff] [blame] | 81 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 82 | 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 Lesinski | fb6312f | 2016-06-28 14:40:32 -0700 | [diff] [blame] | 87 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 88 | // 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 Lesinski | fb6312f | 2016-06-28 14:40:32 -0700 | [diff] [blame] | 101 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 102 | // These should remain. |
| 103 | EXPECT_NE(nullptr, |
| 104 | test::getValueForConfig<Id>( |
| 105 | table.get(), resName, |
| 106 | test::parseConfigOrDie("sw600dp").copyWithoutSdkVersion())); |
Adam Lesinski | 87675ad | 2016-07-15 17:03:03 -0700 | [diff] [blame] | 107 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 108 | // 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 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 |