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