blob: 04bf9cd7fc0bc853ed4ca02d759e44c640655c58 [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
17#include "ConfigDescription.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070018#include "link/Linkers.h"
Adam Lesinskifb6312f2016-06-28 14:40:32 -070019#include "test/Test.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070020
21namespace aapt {
22
23TEST(AutoVersionerTest, GenerateVersionedResources) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070024 const ConfigDescription defaultConfig = {};
25 const ConfigDescription landConfig = test::parseConfigOrDie("land");
26 const ConfigDescription sw600dpLandConfig =
27 test::parseConfigOrDie("sw600dp-land");
Adam Lesinski1ab598f2015-08-14 14:26:04 -070028
Adam Lesinskicacb28f2016-10-19 12:18:14 -070029 ResourceEntry entry("foo");
30 entry.values.push_back(
31 util::make_unique<ResourceConfigValue>(defaultConfig, ""));
32 entry.values.push_back(
33 util::make_unique<ResourceConfigValue>(landConfig, ""));
34 entry.values.push_back(
35 util::make_unique<ResourceConfigValue>(sw600dpLandConfig, ""));
Adam Lesinski1ab598f2015-08-14 14:26:04 -070036
Adam Lesinskicacb28f2016-10-19 12:18:14 -070037 EXPECT_TRUE(shouldGenerateVersionedResource(&entry, defaultConfig, 17));
38 EXPECT_TRUE(shouldGenerateVersionedResource(&entry, landConfig, 17));
Adam Lesinski1ab598f2015-08-14 14:26:04 -070039}
40
41TEST(AutoVersionerTest, GenerateVersionedResourceWhenHigherVersionExists) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070042 const ConfigDescription defaultConfig = {};
43 const ConfigDescription sw600dpV13Config =
44 test::parseConfigOrDie("sw600dp-v13");
45 const ConfigDescription v21Config = test::parseConfigOrDie("v21");
Adam Lesinski1ab598f2015-08-14 14:26:04 -070046
Adam Lesinskicacb28f2016-10-19 12:18:14 -070047 ResourceEntry entry("foo");
48 entry.values.push_back(
49 util::make_unique<ResourceConfigValue>(defaultConfig, ""));
50 entry.values.push_back(
51 util::make_unique<ResourceConfigValue>(sw600dpV13Config, ""));
52 entry.values.push_back(util::make_unique<ResourceConfigValue>(v21Config, ""));
Adam Lesinski1ab598f2015-08-14 14:26:04 -070053
Adam Lesinskicacb28f2016-10-19 12:18:14 -070054 EXPECT_TRUE(shouldGenerateVersionedResource(&entry, defaultConfig, 17));
55 EXPECT_FALSE(shouldGenerateVersionedResource(&entry, defaultConfig, 22));
Adam Lesinski1ab598f2015-08-14 14:26:04 -070056}
57
58TEST(AutoVersionerTest, VersionStylesForTable) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070059 std::unique_ptr<ResourceTable> table =
60 test::ResourceTableBuilder()
61 .setPackageId("app", 0x7f)
62 .addValue(
63 "app:style/Foo", test::parseConfigOrDie("v4"),
64 ResourceId(0x7f020000),
65 test::StyleBuilder()
66 .addItem("android:attr/onClick", ResourceId(0x0101026f),
67 util::make_unique<Id>())
68 .addItem("android:attr/paddingStart", ResourceId(0x010103b3),
69 util::make_unique<Id>())
70 .addItem("android:attr/requiresSmallestWidthDp",
71 ResourceId(0x01010364), util::make_unique<Id>())
72 .addItem("android:attr/colorAccent", ResourceId(0x01010435),
73 util::make_unique<Id>())
74 .build())
75 .addValue(
76 "app:style/Foo", test::parseConfigOrDie("v21"),
77 ResourceId(0x7f020000),
78 test::StyleBuilder()
79 .addItem("android:attr/paddingEnd", ResourceId(0x010103b4),
80 util::make_unique<Id>())
81 .build())
82 .build();
Adam Lesinski1ab598f2015-08-14 14:26:04 -070083
Adam Lesinskicacb28f2016-10-19 12:18:14 -070084 std::unique_ptr<IAaptContext> context = test::ContextBuilder()
85 .setCompilationPackage("app")
86 .setPackageId(0x7f)
87 .build();
Adam Lesinski1ab598f2015-08-14 14:26:04 -070088
Adam Lesinskicacb28f2016-10-19 12:18:14 -070089 AutoVersioner versioner;
90 ASSERT_TRUE(versioner.consume(context.get(), table.get()));
Adam Lesinski1ab598f2015-08-14 14:26:04 -070091
Adam Lesinskicacb28f2016-10-19 12:18:14 -070092 Style* style = test::getValueForConfig<Style>(table.get(), "app:style/Foo",
93 test::parseConfigOrDie("v4"));
94 ASSERT_NE(style, nullptr);
95 ASSERT_EQ(style->entries.size(), 1u);
96 AAPT_ASSERT_TRUE(style->entries.front().key.name);
97 EXPECT_EQ(style->entries.front().key.name.value(),
98 test::parseNameOrDie("android:attr/onClick"));
Adam Lesinski1ab598f2015-08-14 14:26:04 -070099
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700100 style = test::getValueForConfig<Style>(table.get(), "app:style/Foo",
101 test::parseConfigOrDie("v13"));
102 ASSERT_NE(style, nullptr);
103 ASSERT_EQ(style->entries.size(), 2u);
104 AAPT_ASSERT_TRUE(style->entries[0].key.name);
105 EXPECT_EQ(style->entries[0].key.name.value(),
106 test::parseNameOrDie("android:attr/onClick"));
107 AAPT_ASSERT_TRUE(style->entries[1].key.name);
108 EXPECT_EQ(style->entries[1].key.name.value(),
109 test::parseNameOrDie("android:attr/requiresSmallestWidthDp"));
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700110
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700111 style = test::getValueForConfig<Style>(table.get(), "app:style/Foo",
112 test::parseConfigOrDie("v17"));
113 ASSERT_NE(style, nullptr);
114 ASSERT_EQ(style->entries.size(), 3u);
115 AAPT_ASSERT_TRUE(style->entries[0].key.name);
116 EXPECT_EQ(style->entries[0].key.name.value(),
117 test::parseNameOrDie("android:attr/onClick"));
118 AAPT_ASSERT_TRUE(style->entries[1].key.name);
119 EXPECT_EQ(style->entries[1].key.name.value(),
120 test::parseNameOrDie("android:attr/requiresSmallestWidthDp"));
121 AAPT_ASSERT_TRUE(style->entries[2].key.name);
122 EXPECT_EQ(style->entries[2].key.name.value(),
123 test::parseNameOrDie("android:attr/paddingStart"));
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700124
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700125 style = test::getValueForConfig<Style>(table.get(), "app:style/Foo",
126 test::parseConfigOrDie("v21"));
127 ASSERT_NE(style, nullptr);
128 ASSERT_EQ(style->entries.size(), 1u);
129 AAPT_ASSERT_TRUE(style->entries.front().key.name);
130 EXPECT_EQ(style->entries.front().key.name.value(),
131 test::parseNameOrDie("android:attr/paddingEnd"));
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700132}
133
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700134} // namespace aapt