blob: 755af0a1c6cc9907ae4cf8a09c2b848e42ee0317 [file] [log] [blame]
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001/*
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
Adam Lesinski1ab598f2015-08-14 14:26:04 -070017#include "link/Linkers.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070018
19#include "ConfigDescription.h"
Adam Lesinskifb6312f2016-06-28 14:40:32 -070020#include "test/Test.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070021
22namespace aapt {
23
24TEST(AutoVersionerTest, GenerateVersionedResources) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070025 const ConfigDescription land_config = test::ParseConfigOrDie("land");
26 const ConfigDescription sw600dp_land_config =
27 test::ParseConfigOrDie("sw600dp-land");
Adam Lesinski1ab598f2015-08-14 14:26:04 -070028
Adam Lesinskicacb28f2016-10-19 12:18:14 -070029 ResourceEntry entry("foo");
Adam Lesinskice5e56e2016-10-21 17:56:45 -070030 entry.values.push_back(util::make_unique<ResourceConfigValue>(
31 ConfigDescription::DefaultConfig(), ""));
Adam Lesinskicacb28f2016-10-19 12:18:14 -070032 entry.values.push_back(
Adam Lesinskice5e56e2016-10-21 17:56:45 -070033 util::make_unique<ResourceConfigValue>(land_config, ""));
Adam Lesinskicacb28f2016-10-19 12:18:14 -070034 entry.values.push_back(
Adam Lesinskice5e56e2016-10-21 17:56:45 -070035 util::make_unique<ResourceConfigValue>(sw600dp_land_config, ""));
Adam Lesinski1ab598f2015-08-14 14:26:04 -070036
Adam Lesinskice5e56e2016-10-21 17:56:45 -070037 EXPECT_TRUE(ShouldGenerateVersionedResource(
38 &entry, ConfigDescription::DefaultConfig(), 17));
39 EXPECT_TRUE(ShouldGenerateVersionedResource(&entry, land_config, 17));
Adam Lesinski1ab598f2015-08-14 14:26:04 -070040}
41
42TEST(AutoVersionerTest, GenerateVersionedResourceWhenHigherVersionExists) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070043 const ConfigDescription sw600dp_v13_config =
44 test::ParseConfigOrDie("sw600dp-v13");
45 const ConfigDescription v21_config = test::ParseConfigOrDie("v21");
Adam Lesinski1ab598f2015-08-14 14:26:04 -070046
Adam Lesinskicacb28f2016-10-19 12:18:14 -070047 ResourceEntry entry("foo");
Adam Lesinskice5e56e2016-10-21 17:56:45 -070048 entry.values.push_back(util::make_unique<ResourceConfigValue>(
49 ConfigDescription::DefaultConfig(), ""));
Adam Lesinskicacb28f2016-10-19 12:18:14 -070050 entry.values.push_back(
Adam Lesinskice5e56e2016-10-21 17:56:45 -070051 util::make_unique<ResourceConfigValue>(sw600dp_v13_config, ""));
Adam Lesinskicacb28f2016-10-19 12:18:14 -070052 entry.values.push_back(
Adam Lesinskice5e56e2016-10-21 17:56:45 -070053 util::make_unique<ResourceConfigValue>(v21_config, ""));
Adam Lesinski1ab598f2015-08-14 14:26:04 -070054
Adam Lesinskice5e56e2016-10-21 17:56:45 -070055 EXPECT_TRUE(ShouldGenerateVersionedResource(
56 &entry, ConfigDescription::DefaultConfig(), 17));
57 EXPECT_FALSE(ShouldGenerateVersionedResource(
58 &entry, ConfigDescription::DefaultConfig(), 22));
Adam Lesinski1ab598f2015-08-14 14:26:04 -070059}
60
61TEST(AutoVersionerTest, VersionStylesForTable) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070062 std::unique_ptr<ResourceTable> table =
63 test::ResourceTableBuilder()
Adam Lesinskice5e56e2016-10-21 17:56:45 -070064 .SetPackageId("app", 0x7f)
65 .AddValue(
66 "app:style/Foo", test::ParseConfigOrDie("v4"),
Adam Lesinskicacb28f2016-10-19 12:18:14 -070067 ResourceId(0x7f020000),
68 test::StyleBuilder()
Adam Lesinskice5e56e2016-10-21 17:56:45 -070069 .AddItem("android:attr/onClick", ResourceId(0x0101026f),
Adam Lesinskicacb28f2016-10-19 12:18:14 -070070 util::make_unique<Id>())
Adam Lesinskice5e56e2016-10-21 17:56:45 -070071 .AddItem("android:attr/paddingStart", ResourceId(0x010103b3),
Adam Lesinskicacb28f2016-10-19 12:18:14 -070072 util::make_unique<Id>())
Adam Lesinskice5e56e2016-10-21 17:56:45 -070073 .AddItem("android:attr/requiresSmallestWidthDp",
Adam Lesinskicacb28f2016-10-19 12:18:14 -070074 ResourceId(0x01010364), util::make_unique<Id>())
Adam Lesinskice5e56e2016-10-21 17:56:45 -070075 .AddItem("android:attr/colorAccent", ResourceId(0x01010435),
Adam Lesinskicacb28f2016-10-19 12:18:14 -070076 util::make_unique<Id>())
Adam Lesinskice5e56e2016-10-21 17:56:45 -070077 .Build())
78 .AddValue(
79 "app:style/Foo", test::ParseConfigOrDie("v21"),
Adam Lesinskicacb28f2016-10-19 12:18:14 -070080 ResourceId(0x7f020000),
81 test::StyleBuilder()
Adam Lesinskice5e56e2016-10-21 17:56:45 -070082 .AddItem("android:attr/paddingEnd", ResourceId(0x010103b4),
Adam Lesinskicacb28f2016-10-19 12:18:14 -070083 util::make_unique<Id>())
Adam Lesinskice5e56e2016-10-21 17:56:45 -070084 .Build())
85 .Build();
Adam Lesinski1ab598f2015-08-14 14:26:04 -070086
Adam Lesinskicacb28f2016-10-19 12:18:14 -070087 std::unique_ptr<IAaptContext> context = test::ContextBuilder()
Adam Lesinskice5e56e2016-10-21 17:56:45 -070088 .SetCompilationPackage("app")
89 .SetPackageId(0x7f)
90 .Build();
Adam Lesinski1ab598f2015-08-14 14:26:04 -070091
Adam Lesinskicacb28f2016-10-19 12:18:14 -070092 AutoVersioner versioner;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070093 ASSERT_TRUE(versioner.Consume(context.get(), table.get()));
Adam Lesinski1ab598f2015-08-14 14:26:04 -070094
Adam Lesinskice5e56e2016-10-21 17:56:45 -070095 Style* style = test::GetValueForConfig<Style>(table.get(), "app:style/Foo",
96 test::ParseConfigOrDie("v4"));
Adam Lesinskicacb28f2016-10-19 12:18:14 -070097 ASSERT_NE(style, nullptr);
98 ASSERT_EQ(style->entries.size(), 1u);
99 AAPT_ASSERT_TRUE(style->entries.front().key.name);
100 EXPECT_EQ(style->entries.front().key.name.value(),
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700101 test::ParseNameOrDie("android:attr/onClick"));
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700102
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700103 style = test::GetValueForConfig<Style>(table.get(), "app:style/Foo",
104 test::ParseConfigOrDie("v13"));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700105 ASSERT_NE(style, nullptr);
106 ASSERT_EQ(style->entries.size(), 2u);
107 AAPT_ASSERT_TRUE(style->entries[0].key.name);
108 EXPECT_EQ(style->entries[0].key.name.value(),
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700109 test::ParseNameOrDie("android:attr/onClick"));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700110 AAPT_ASSERT_TRUE(style->entries[1].key.name);
111 EXPECT_EQ(style->entries[1].key.name.value(),
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700112 test::ParseNameOrDie("android:attr/requiresSmallestWidthDp"));
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700113
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700114 style = test::GetValueForConfig<Style>(table.get(), "app:style/Foo",
115 test::ParseConfigOrDie("v17"));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700116 ASSERT_NE(style, nullptr);
117 ASSERT_EQ(style->entries.size(), 3u);
118 AAPT_ASSERT_TRUE(style->entries[0].key.name);
119 EXPECT_EQ(style->entries[0].key.name.value(),
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700120 test::ParseNameOrDie("android:attr/onClick"));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700121 AAPT_ASSERT_TRUE(style->entries[1].key.name);
122 EXPECT_EQ(style->entries[1].key.name.value(),
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700123 test::ParseNameOrDie("android:attr/requiresSmallestWidthDp"));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700124 AAPT_ASSERT_TRUE(style->entries[2].key.name);
125 EXPECT_EQ(style->entries[2].key.name.value(),
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700126 test::ParseNameOrDie("android:attr/paddingStart"));
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700127
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700128 style = test::GetValueForConfig<Style>(table.get(), "app:style/Foo",
129 test::ParseConfigOrDie("v21"));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700130 ASSERT_NE(style, nullptr);
131 ASSERT_EQ(style->entries.size(), 1u);
132 AAPT_ASSERT_TRUE(style->entries.front().key.name);
133 EXPECT_EQ(style->entries.front().key.name.value(),
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700134 test::ParseNameOrDie("android:attr/paddingEnd"));
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700135}
136
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700137} // namespace aapt