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           .SetPackageId("app", 0x7f)
58           .AddValue(
59               "app:style/Foo", test::ParseConfigOrDie("v4"),
60               ResourceId(0x7f020000),
61               test::StyleBuilder()
62                   .AddItem("android:attr/onClick", ResourceId(0x0101026f),
63                            util::make_unique<Id>())
64                   .AddItem("android:attr/paddingStart", ResourceId(0x010103b3),
65                            util::make_unique<Id>())
66                   .AddItem("android:attr/requiresSmallestWidthDp",
67                            ResourceId(0x01010364), util::make_unique<Id>())
68                   .AddItem("android:attr/colorAccent", ResourceId(0x01010435),
69                            util::make_unique<Id>())
70                   .Build())
71           .AddValue(
72               "app:style/Foo", test::ParseConfigOrDie("v21"),
73               ResourceId(0x7f020000),
74               test::StyleBuilder()
75                   .AddItem("android:attr/paddingEnd", ResourceId(0x010103b4),
76                            util::make_unique<Id>())
77                   .Build())
78           .Build();
79 
80   std::unique_ptr<IAaptContext> context = test::ContextBuilder()
81                                               .SetCompilationPackage("app")
82                                               .SetPackageId(0x7f)
83                                               .Build();
84 
85   AutoVersioner versioner;
86   ASSERT_TRUE(versioner.Consume(context.get(), table.get()));
87 
88   Style* style = test::GetValueForConfig<Style>(table.get(), "app:style/Foo", test::ParseConfigOrDie("v4"));
89   ASSERT_THAT(style, NotNull());
90   ASSERT_EQ(style->entries.size(), 1u);
91   EXPECT_EQ(make_value(test::ParseNameOrDie("android:attr/onClick")), style->entries.front().key.name);
92 
93   style = test::GetValueForConfig<Style>(table.get(), "app:style/Foo", test::ParseConfigOrDie("v13"));
94   ASSERT_THAT(style, NotNull());
95   ASSERT_EQ(style->entries.size(), 2u);
96   EXPECT_EQ(make_value(test::ParseNameOrDie("android:attr/onClick")),style->entries[0].key.name);
97   EXPECT_EQ(make_value(test::ParseNameOrDie("android:attr/requiresSmallestWidthDp")), 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(make_value(test::ParseNameOrDie("android:attr/onClick")), style->entries[0].key.name);
103   EXPECT_EQ(make_value(test::ParseNameOrDie("android:attr/requiresSmallestWidthDp")), style->entries[1].key.name);
104   EXPECT_EQ(make_value(test::ParseNameOrDie("android:attr/paddingStart")), style->entries[2].key.name);
105 
106   style = test::GetValueForConfig<Style>(table.get(), "app:style/Foo", test::ParseConfigOrDie("v21"));
107   ASSERT_THAT(style, NotNull());
108   ASSERT_EQ(1u, style->entries.size());
109   EXPECT_EQ(make_value(test::ParseNameOrDie("android:attr/paddingEnd")), style->entries.front().key.name);
110 }
111 
112 }  // namespace aapt
113