File: AutoVersioner_test.cpp

package info (click to toggle)
android-platform-frameworks-base 1%3A7.0.0%2Br33-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 137,116 kB
  • sloc: java: 704,370; cpp: 206,670; xml: 185,778; python: 2,626; ansic: 486; sh: 291; makefile: 48; sed: 19
file content (125 lines) | stat: -rw-r--r-- 5,941 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*
 * Copyright (C) 2015 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "ConfigDescription.h"
#include "link/Linkers.h"
#include "test/Builders.h"
#include "test/Context.h"

#include <gtest/gtest.h>

namespace aapt {

TEST(AutoVersionerTest, GenerateVersionedResources) {
    const ConfigDescription defaultConfig = {};
    const ConfigDescription landConfig = test::parseConfigOrDie("land");
    const ConfigDescription sw600dpLandConfig = test::parseConfigOrDie("sw600dp-land");

    ResourceEntry entry(u"foo");
    entry.values.push_back(util::make_unique<ResourceConfigValue>(defaultConfig, ""));
    entry.values.push_back(util::make_unique<ResourceConfigValue>(landConfig, ""));
    entry.values.push_back(util::make_unique<ResourceConfigValue>(sw600dpLandConfig, ""));

    EXPECT_TRUE(shouldGenerateVersionedResource(&entry, defaultConfig, 17));
    EXPECT_TRUE(shouldGenerateVersionedResource(&entry, landConfig, 17));
}

TEST(AutoVersionerTest, GenerateVersionedResourceWhenHigherVersionExists) {
    const ConfigDescription defaultConfig = {};
    const ConfigDescription sw600dpV13Config = test::parseConfigOrDie("sw600dp-v13");
    const ConfigDescription v21Config = test::parseConfigOrDie("v21");

    ResourceEntry entry(u"foo");
    entry.values.push_back(util::make_unique<ResourceConfigValue>(defaultConfig, ""));
    entry.values.push_back(util::make_unique<ResourceConfigValue>(sw600dpV13Config, ""));
    entry.values.push_back(util::make_unique<ResourceConfigValue>(v21Config, ""));

    EXPECT_TRUE(shouldGenerateVersionedResource(&entry, defaultConfig, 17));
    EXPECT_FALSE(shouldGenerateVersionedResource(&entry, defaultConfig, 22));
}

TEST(AutoVersionerTest, VersionStylesForTable) {
    std::unique_ptr<ResourceTable> table = test::ResourceTableBuilder()
            .setPackageId(u"app", 0x7f)
            .addValue(u"@app:style/Foo", ResourceId(0x7f020000), test::parseConfigOrDie("v4"),
                      test::StyleBuilder()
                            .addItem(u"@android:attr/onClick", ResourceId(0x0101026f),
                                     util::make_unique<Id>())
                            .addItem(u"@android:attr/paddingStart", ResourceId(0x010103b3),
                                     util::make_unique<Id>())
                            .addItem(u"@android:attr/requiresSmallestWidthDp",
                                     ResourceId(0x01010364), util::make_unique<Id>())
                            .addItem(u"@android:attr/colorAccent", ResourceId(0x01010435),
                                     util::make_unique<Id>())
                            .build())
            .addValue(u"@app:style/Foo", ResourceId(0x7f020000), test::parseConfigOrDie("v21"),
                      test::StyleBuilder()
                            .addItem(u"@android:attr/paddingEnd", ResourceId(0x010103b4),
                                     util::make_unique<Id>())
                            .build())
            .build();

    std::unique_ptr<IAaptContext> context = test::ContextBuilder()
            .setCompilationPackage(u"app")
            .setPackageId(0x7f)
            .build();

    AutoVersioner versioner;
    ASSERT_TRUE(versioner.consume(context.get(), table.get()));

    Style* style = test::getValueForConfig<Style>(table.get(), u"@app:style/Foo",
                                                  test::parseConfigOrDie("v4"));
    ASSERT_NE(style, nullptr);
    ASSERT_EQ(style->entries.size(), 1u);
    AAPT_ASSERT_TRUE(style->entries.front().key.name);
    EXPECT_EQ(style->entries.front().key.name.value(),
              test::parseNameOrDie(u"@android:attr/onClick"));

    style = test::getValueForConfig<Style>(table.get(), u"@app:style/Foo",
                                           test::parseConfigOrDie("v13"));
    ASSERT_NE(style, nullptr);
    ASSERT_EQ(style->entries.size(), 2u);
    AAPT_ASSERT_TRUE(style->entries[0].key.name);
    EXPECT_EQ(style->entries[0].key.name.value(),
              test::parseNameOrDie(u"@android:attr/onClick"));
    AAPT_ASSERT_TRUE(style->entries[1].key.name);
    EXPECT_EQ(style->entries[1].key.name.value(),
                  test::parseNameOrDie(u"@android:attr/requiresSmallestWidthDp"));

    style = test::getValueForConfig<Style>(table.get(), u"@app:style/Foo",
                                           test::parseConfigOrDie("v17"));
    ASSERT_NE(style, nullptr);
    ASSERT_EQ(style->entries.size(), 3u);
    AAPT_ASSERT_TRUE(style->entries[0].key.name);
    EXPECT_EQ(style->entries[0].key.name.value(),
                  test::parseNameOrDie(u"@android:attr/onClick"));
    AAPT_ASSERT_TRUE(style->entries[1].key.name);
    EXPECT_EQ(style->entries[1].key.name.value(),
                  test::parseNameOrDie(u"@android:attr/requiresSmallestWidthDp"));
    AAPT_ASSERT_TRUE(style->entries[2].key.name);
    EXPECT_EQ(style->entries[2].key.name.value(),
                  test::parseNameOrDie(u"@android:attr/paddingStart"));

    style = test::getValueForConfig<Style>(table.get(), u"@app:style/Foo",
                                           test::parseConfigOrDie("v21"));
    ASSERT_NE(style, nullptr);
    ASSERT_EQ(style->entries.size(), 1u);
    AAPT_ASSERT_TRUE(style->entries.front().key.name);
    EXPECT_EQ(style->entries.front().key.name.value(),
              test::parseNameOrDie(u"@android:attr/paddingEnd"));
}

} // namespace aapt