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