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 "link/Linkers.h"
18
19 #include "androidfw/ConfigDescription.h"
20
21 #include "test/Test.h"
22
23 using ::android::ConfigDescription;
24 using ::testing::NotNull;
25
26 namespace aapt {
27
TEST(AutoVersionerTest,GenerateVersionedResources)28 TEST(AutoVersionerTest, GenerateVersionedResources) {
29 const ConfigDescription land_config = test::ParseConfigOrDie("land");
30 const ConfigDescription sw600dp_land_config = test::ParseConfigOrDie("sw600dp-land");
31
32 ResourceEntry entry("foo");
33 entry.values.push_back(util::make_unique<ResourceConfigValue>(ConfigDescription::DefaultConfig(), ""));
34 entry.values.push_back(util::make_unique<ResourceConfigValue>(land_config, ""));
35 entry.values.push_back(util::make_unique<ResourceConfigValue>(sw600dp_land_config, ""));
36
37 EXPECT_TRUE(ShouldGenerateVersionedResource(&entry, ConfigDescription::DefaultConfig(), 17));
38 EXPECT_TRUE(ShouldGenerateVersionedResource(&entry, land_config, 17));
39 }
40
TEST(AutoVersionerTest,GenerateVersionedResourceWhenHigherVersionExists)41 TEST(AutoVersionerTest, GenerateVersionedResourceWhenHigherVersionExists) {
42 const ConfigDescription sw600dp_v13_config = test::ParseConfigOrDie("sw600dp-v13");
43 const ConfigDescription v21_config = test::ParseConfigOrDie("v21");
44
45 ResourceEntry entry("foo");
46 entry.values.push_back(util::make_unique<ResourceConfigValue>(ConfigDescription::DefaultConfig(), ""));
47 entry.values.push_back(util::make_unique<ResourceConfigValue>(sw600dp_v13_config, ""));
48 entry.values.push_back(util::make_unique<ResourceConfigValue>(v21_config, ""));
49
50 EXPECT_TRUE(ShouldGenerateVersionedResource(&entry, ConfigDescription::DefaultConfig(), 17));
51 EXPECT_FALSE(ShouldGenerateVersionedResource(&entry, ConfigDescription::DefaultConfig(), 22));
52 }
53
TEST(AutoVersionerTest,VersionStylesForTable)54 TEST(AutoVersionerTest, VersionStylesForTable) {
55 std::unique_ptr<ResourceTable> table =
56 test::ResourceTableBuilder()
57 .AddValue(
58 "app:style/Foo", test::ParseConfigOrDie("v4"),
59 ResourceId(0x7f020000),
60 test::StyleBuilder()
61 .AddItem("android:attr/onClick", ResourceId(0x0101026f),
62 util::make_unique<Id>())
63 .AddItem("android:attr/paddingStart", ResourceId(0x010103b3),
64 util::make_unique<Id>())
65 .AddItem("android:attr/requiresSmallestWidthDp",
66 ResourceId(0x01010364), util::make_unique<Id>())
67 .AddItem("android:attr/colorAccent", ResourceId(0x01010435),
68 util::make_unique<Id>())
69 .Build())
70 .AddValue(
71 "app:style/Foo", test::ParseConfigOrDie("v21"),
72 ResourceId(0x7f020000),
73 test::StyleBuilder()
74 .AddItem("android:attr/paddingEnd", ResourceId(0x010103b4),
75 util::make_unique<Id>())
76 .Build())
77 .Build();
78
79 std::unique_ptr<IAaptContext> context = test::ContextBuilder()
80 .SetCompilationPackage("app")
81 .SetPackageId(0x7f)
82 .Build();
83
84 AutoVersioner versioner;
85 ASSERT_TRUE(versioner.Consume(context.get(), table.get()));
86
87 Style* style = test::GetValueForConfig<Style>(table.get(), "app:style/Foo", test::ParseConfigOrDie("v4"));
88 ASSERT_THAT(style, NotNull());
89 ASSERT_EQ(style->entries.size(), 1u);
90 EXPECT_EQ(test::ParseNameOrDie("android:attr/onClick"), style->entries.front().key.name);
91
92 style = test::GetValueForConfig<Style>(table.get(), "app:style/Foo", test::ParseConfigOrDie("v13"));
93 ASSERT_THAT(style, NotNull());
94 ASSERT_EQ(style->entries.size(), 2u);
95 EXPECT_EQ(test::ParseNameOrDie("android:attr/onClick"), style->entries[0].key.name);
96 EXPECT_EQ(test::ParseNameOrDie("android:attr/requiresSmallestWidthDp"),
97 style->entries[1].key.name);
98
99 style = test::GetValueForConfig<Style>(table.get(), "app:style/Foo", test::ParseConfigOrDie("v17"));
100 ASSERT_THAT(style, NotNull());
101 ASSERT_EQ(style->entries.size(), 3u);
102 EXPECT_EQ(test::ParseNameOrDie("android:attr/onClick"), style->entries[0].key.name);
103 EXPECT_EQ(test::ParseNameOrDie("android:attr/requiresSmallestWidthDp"),
104 style->entries[1].key.name);
105 EXPECT_EQ(test::ParseNameOrDie("android:attr/paddingStart"), style->entries[2].key.name);
106
107 style = test::GetValueForConfig<Style>(table.get(), "app:style/Foo", test::ParseConfigOrDie("v21"));
108 ASSERT_THAT(style, NotNull());
109 ASSERT_EQ(1u, style->entries.size());
110 EXPECT_EQ(test::ParseNameOrDie("android:attr/paddingEnd"), style->entries.front().key.name);
111 }
112
113 } // namespace aapt
114