Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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 "ConfigDescription.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 18 | #include "link/Linkers.h" |
Adam Lesinski | fb6312f | 2016-06-28 14:40:32 -0700 | [diff] [blame^] | 19 | #include "test/Test.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 20 | |
| 21 | namespace aapt { |
| 22 | |
| 23 | TEST(AutoVersionerTest, GenerateVersionedResources) { |
| 24 | const ConfigDescription defaultConfig = {}; |
| 25 | const ConfigDescription landConfig = test::parseConfigOrDie("land"); |
| 26 | const ConfigDescription sw600dpLandConfig = test::parseConfigOrDie("sw600dp-land"); |
| 27 | |
| 28 | ResourceEntry entry(u"foo"); |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 29 | entry.values.push_back(util::make_unique<ResourceConfigValue>(defaultConfig, "")); |
| 30 | entry.values.push_back(util::make_unique<ResourceConfigValue>(landConfig, "")); |
| 31 | entry.values.push_back(util::make_unique<ResourceConfigValue>(sw600dpLandConfig, "")); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 32 | |
| 33 | EXPECT_TRUE(shouldGenerateVersionedResource(&entry, defaultConfig, 17)); |
| 34 | EXPECT_TRUE(shouldGenerateVersionedResource(&entry, landConfig, 17)); |
| 35 | } |
| 36 | |
| 37 | TEST(AutoVersionerTest, GenerateVersionedResourceWhenHigherVersionExists) { |
| 38 | const ConfigDescription defaultConfig = {}; |
| 39 | const ConfigDescription sw600dpV13Config = test::parseConfigOrDie("sw600dp-v13"); |
| 40 | const ConfigDescription v21Config = test::parseConfigOrDie("v21"); |
| 41 | |
| 42 | ResourceEntry entry(u"foo"); |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 43 | entry.values.push_back(util::make_unique<ResourceConfigValue>(defaultConfig, "")); |
| 44 | entry.values.push_back(util::make_unique<ResourceConfigValue>(sw600dpV13Config, "")); |
| 45 | entry.values.push_back(util::make_unique<ResourceConfigValue>(v21Config, "")); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 46 | |
| 47 | EXPECT_TRUE(shouldGenerateVersionedResource(&entry, defaultConfig, 17)); |
| 48 | EXPECT_FALSE(shouldGenerateVersionedResource(&entry, defaultConfig, 22)); |
| 49 | } |
| 50 | |
| 51 | TEST(AutoVersionerTest, VersionStylesForTable) { |
| 52 | std::unique_ptr<ResourceTable> table = test::ResourceTableBuilder() |
| 53 | .setPackageId(u"app", 0x7f) |
Adam Lesinski | fb6312f | 2016-06-28 14:40:32 -0700 | [diff] [blame^] | 54 | .addValue(u"@app:style/Foo", test::parseConfigOrDie("v4"), ResourceId(0x7f020000), |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 55 | test::StyleBuilder() |
| 56 | .addItem(u"@android:attr/onClick", ResourceId(0x0101026f), |
| 57 | util::make_unique<Id>()) |
| 58 | .addItem(u"@android:attr/paddingStart", ResourceId(0x010103b3), |
| 59 | util::make_unique<Id>()) |
| 60 | .addItem(u"@android:attr/requiresSmallestWidthDp", |
| 61 | ResourceId(0x01010364), util::make_unique<Id>()) |
| 62 | .addItem(u"@android:attr/colorAccent", ResourceId(0x01010435), |
| 63 | util::make_unique<Id>()) |
| 64 | .build()) |
Adam Lesinski | fb6312f | 2016-06-28 14:40:32 -0700 | [diff] [blame^] | 65 | .addValue(u"@app:style/Foo", test::parseConfigOrDie("v21"), ResourceId(0x7f020000), |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 66 | test::StyleBuilder() |
| 67 | .addItem(u"@android:attr/paddingEnd", ResourceId(0x010103b4), |
| 68 | util::make_unique<Id>()) |
| 69 | .build()) |
| 70 | .build(); |
| 71 | |
| 72 | std::unique_ptr<IAaptContext> context = test::ContextBuilder() |
| 73 | .setCompilationPackage(u"app") |
| 74 | .setPackageId(0x7f) |
| 75 | .build(); |
| 76 | |
| 77 | AutoVersioner versioner; |
| 78 | ASSERT_TRUE(versioner.consume(context.get(), table.get())); |
| 79 | |
| 80 | Style* style = test::getValueForConfig<Style>(table.get(), u"@app:style/Foo", |
| 81 | test::parseConfigOrDie("v4")); |
| 82 | ASSERT_NE(style, nullptr); |
| 83 | ASSERT_EQ(style->entries.size(), 1u); |
| 84 | AAPT_ASSERT_TRUE(style->entries.front().key.name); |
| 85 | EXPECT_EQ(style->entries.front().key.name.value(), |
| 86 | test::parseNameOrDie(u"@android:attr/onClick")); |
| 87 | |
| 88 | style = test::getValueForConfig<Style>(table.get(), u"@app:style/Foo", |
| 89 | test::parseConfigOrDie("v13")); |
| 90 | ASSERT_NE(style, nullptr); |
| 91 | ASSERT_EQ(style->entries.size(), 2u); |
| 92 | AAPT_ASSERT_TRUE(style->entries[0].key.name); |
| 93 | EXPECT_EQ(style->entries[0].key.name.value(), |
| 94 | test::parseNameOrDie(u"@android:attr/onClick")); |
| 95 | AAPT_ASSERT_TRUE(style->entries[1].key.name); |
| 96 | EXPECT_EQ(style->entries[1].key.name.value(), |
| 97 | test::parseNameOrDie(u"@android:attr/requiresSmallestWidthDp")); |
| 98 | |
| 99 | style = test::getValueForConfig<Style>(table.get(), u"@app:style/Foo", |
| 100 | test::parseConfigOrDie("v17")); |
| 101 | ASSERT_NE(style, nullptr); |
| 102 | ASSERT_EQ(style->entries.size(), 3u); |
| 103 | AAPT_ASSERT_TRUE(style->entries[0].key.name); |
| 104 | EXPECT_EQ(style->entries[0].key.name.value(), |
| 105 | test::parseNameOrDie(u"@android:attr/onClick")); |
| 106 | AAPT_ASSERT_TRUE(style->entries[1].key.name); |
| 107 | EXPECT_EQ(style->entries[1].key.name.value(), |
| 108 | test::parseNameOrDie(u"@android:attr/requiresSmallestWidthDp")); |
| 109 | AAPT_ASSERT_TRUE(style->entries[2].key.name); |
| 110 | EXPECT_EQ(style->entries[2].key.name.value(), |
| 111 | test::parseNameOrDie(u"@android:attr/paddingStart")); |
| 112 | |
| 113 | style = test::getValueForConfig<Style>(table.get(), u"@app:style/Foo", |
| 114 | test::parseConfigOrDie("v21")); |
| 115 | ASSERT_NE(style, nullptr); |
| 116 | ASSERT_EQ(style->entries.size(), 1u); |
| 117 | AAPT_ASSERT_TRUE(style->entries.front().key.name); |
| 118 | EXPECT_EQ(style->entries.front().key.name.value(), |
| 119 | test::parseNameOrDie(u"@android:attr/paddingEnd")); |
| 120 | } |
| 121 | |
| 122 | } // namespace aapt |