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