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 "ConfigDescription.h"
18#include "link/Linkers.h"
19#include "test/Builders.h"
20#include "test/Context.h"
21
22#include <gtest/gtest.h>
23
24namespace aapt {
25
26TEST(AutoVersionerTest, GenerateVersionedResources) {
27    const ConfigDescription defaultConfig = {};
28    const ConfigDescription landConfig = test::parseConfigOrDie("land");
29    const ConfigDescription sw600dpLandConfig = test::parseConfigOrDie("sw600dp-land");
30
31    ResourceEntry entry(u"foo");
32    entry.values.push_back(util::make_unique<ResourceConfigValue>(defaultConfig, ""));
33    entry.values.push_back(util::make_unique<ResourceConfigValue>(landConfig, ""));
34    entry.values.push_back(util::make_unique<ResourceConfigValue>(sw600dpLandConfig, ""));
35
36    EXPECT_TRUE(shouldGenerateVersionedResource(&entry, defaultConfig, 17));
37    EXPECT_TRUE(shouldGenerateVersionedResource(&entry, landConfig, 17));
38}
39
40TEST(AutoVersionerTest, GenerateVersionedResourceWhenHigherVersionExists) {
41    const ConfigDescription defaultConfig = {};
42    const ConfigDescription sw600dpV13Config = test::parseConfigOrDie("sw600dp-v13");
43    const ConfigDescription v21Config = test::parseConfigOrDie("v21");
44
45    ResourceEntry entry(u"foo");
46    entry.values.push_back(util::make_unique<ResourceConfigValue>(defaultConfig, ""));
47    entry.values.push_back(util::make_unique<ResourceConfigValue>(sw600dpV13Config, ""));
48    entry.values.push_back(util::make_unique<ResourceConfigValue>(v21Config, ""));
49
50    EXPECT_TRUE(shouldGenerateVersionedResource(&entry, defaultConfig, 17));
51    EXPECT_FALSE(shouldGenerateVersionedResource(&entry, defaultConfig, 22));
52}
53
54TEST(AutoVersionerTest, VersionStylesForTable) {
55    std::unique_ptr<ResourceTable> table = test::ResourceTableBuilder()
56            .setPackageId(u"app", 0x7f)
57            .addValue(u"@app:style/Foo", ResourceId(0x7f020000), test::parseConfigOrDie("v4"),
58                      test::StyleBuilder()
59                            .addItem(u"@android:attr/onClick", ResourceId(0x0101026f),
60                                     util::make_unique<Id>())
61                            .addItem(u"@android:attr/paddingStart", ResourceId(0x010103b3),
62                                     util::make_unique<Id>())
63                            .addItem(u"@android:attr/requiresSmallestWidthDp",
64                                     ResourceId(0x01010364), util::make_unique<Id>())
65                            .addItem(u"@android:attr/colorAccent", ResourceId(0x01010435),
66                                     util::make_unique<Id>())
67                            .build())
68            .addValue(u"@app:style/Foo", ResourceId(0x7f020000), test::parseConfigOrDie("v21"),
69                      test::StyleBuilder()
70                            .addItem(u"@android:attr/paddingEnd", ResourceId(0x010103b4),
71                                     util::make_unique<Id>())
72                            .build())
73            .build();
74
75    std::unique_ptr<IAaptContext> context = test::ContextBuilder()
76            .setCompilationPackage(u"app")
77            .setPackageId(0x7f)
78            .build();
79
80    AutoVersioner versioner;
81    ASSERT_TRUE(versioner.consume(context.get(), table.get()));
82
83    Style* style = test::getValueForConfig<Style>(table.get(), u"@app:style/Foo",
84                                                  test::parseConfigOrDie("v4"));
85    ASSERT_NE(style, nullptr);
86    ASSERT_EQ(style->entries.size(), 1u);
87    AAPT_ASSERT_TRUE(style->entries.front().key.name);
88    EXPECT_EQ(style->entries.front().key.name.value(),
89              test::parseNameOrDie(u"@android:attr/onClick"));
90
91    style = test::getValueForConfig<Style>(table.get(), u"@app:style/Foo",
92                                           test::parseConfigOrDie("v13"));
93    ASSERT_NE(style, nullptr);
94    ASSERT_EQ(style->entries.size(), 2u);
95    AAPT_ASSERT_TRUE(style->entries[0].key.name);
96    EXPECT_EQ(style->entries[0].key.name.value(),
97              test::parseNameOrDie(u"@android:attr/onClick"));
98    AAPT_ASSERT_TRUE(style->entries[1].key.name);
99    EXPECT_EQ(style->entries[1].key.name.value(),
100                  test::parseNameOrDie(u"@android:attr/requiresSmallestWidthDp"));
101
102    style = test::getValueForConfig<Style>(table.get(), u"@app:style/Foo",
103                                           test::parseConfigOrDie("v17"));
104    ASSERT_NE(style, nullptr);
105    ASSERT_EQ(style->entries.size(), 3u);
106    AAPT_ASSERT_TRUE(style->entries[0].key.name);
107    EXPECT_EQ(style->entries[0].key.name.value(),
108                  test::parseNameOrDie(u"@android:attr/onClick"));
109    AAPT_ASSERT_TRUE(style->entries[1].key.name);
110    EXPECT_EQ(style->entries[1].key.name.value(),
111                  test::parseNameOrDie(u"@android:attr/requiresSmallestWidthDp"));
112    AAPT_ASSERT_TRUE(style->entries[2].key.name);
113    EXPECT_EQ(style->entries[2].key.name.value(),
114                  test::parseNameOrDie(u"@android:attr/paddingStart"));
115
116    style = test::getValueForConfig<Style>(table.get(), u"@app:style/Foo",
117                                           test::parseConfigOrDie("v21"));
118    ASSERT_NE(style, nullptr);
119    ASSERT_EQ(style->entries.size(), 1u);
120    AAPT_ASSERT_TRUE(style->entries.front().key.name);
121    EXPECT_EQ(style->entries.front().key.name.value(),
122              test::parseNameOrDie(u"@android:attr/paddingEnd"));
123}
124
125} // namespace aapt
126