ManifestFixer_test.cpp revision 467f171315f9c2037fcd3eb5edcfabc40671bf7b
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/ManifestFixer.h"
18#include "test/Builders.h"
19#include "test/Context.h"
20
21#include <gtest/gtest.h>
22
23namespace aapt {
24
25struct ManifestFixerTest : public ::testing::Test {
26    std::unique_ptr<IAaptContext> mContext;
27
28    void SetUp() override {
29        mContext = test::ContextBuilder()
30                .setCompilationPackage(u"android")
31                .setPackageId(0x01)
32                .setNameManglerPolicy(NameManglerPolicy{ u"android" })
33                .setSymbolTable(test::StaticSymbolTableBuilder()
34                        .addSymbol(u"@android:attr/package", ResourceId(0x01010000),
35                                   test::AttributeBuilder()
36                                        .setTypeMask(android::ResTable_map::TYPE_STRING)
37                                        .build())
38                        .addSymbol(u"@android:attr/minSdkVersion", ResourceId(0x01010001),
39                                   test::AttributeBuilder()
40                                        .setTypeMask(android::ResTable_map::TYPE_STRING |
41                                                     android::ResTable_map::TYPE_INTEGER)
42                                        .build())
43                        .addSymbol(u"@android:attr/targetSdkVersion", ResourceId(0x01010002),
44                                   test::AttributeBuilder()
45                                        .setTypeMask(android::ResTable_map::TYPE_STRING |
46                                                     android::ResTable_map::TYPE_INTEGER)
47                                        .build())
48                        .addSymbol(u"@android:string/str", ResourceId(0x01060000))
49                        .build())
50                .build();
51    }
52
53    std::unique_ptr<xml::XmlResource> verify(const StringPiece& str) {
54        return verifyWithOptions(str, {});
55    }
56
57    std::unique_ptr<xml::XmlResource> verifyWithOptions(const StringPiece& str,
58                                                        const ManifestFixerOptions& options) {
59        std::unique_ptr<xml::XmlResource> doc = test::buildXmlDom(str);
60        ManifestFixer fixer(options);
61        if (fixer.consume(mContext.get(), doc.get())) {
62            return doc;
63        }
64        return {};
65    }
66};
67
68TEST_F(ManifestFixerTest, EnsureManifestIsRootTag) {
69    EXPECT_EQ(nullptr, verify("<other-tag />"));
70    EXPECT_EQ(nullptr, verify("<ns:manifest xmlns:ns=\"com\" />"));
71    EXPECT_NE(nullptr, verify("<manifest package=\"android\"></manifest>"));
72}
73
74TEST_F(ManifestFixerTest, EnsureManifestHasPackage) {
75    EXPECT_NE(nullptr, verify("<manifest package=\"android\" />"));
76    EXPECT_NE(nullptr, verify("<manifest package=\"com.android\" />"));
77    EXPECT_NE(nullptr, verify("<manifest package=\"com.android.google\" />"));
78    EXPECT_EQ(nullptr, verify("<manifest package=\"com.android.google.Class$1\" />"));
79    EXPECT_EQ(nullptr,
80              verify("<manifest xmlns:android=\"http://schemas.android.com/apk/res/android\" "
81                     "android:package=\"com.android\" />"));
82    EXPECT_EQ(nullptr, verify("<manifest package=\"@string/str\" />"));
83}
84
85
86
87TEST_F(ManifestFixerTest, UseDefaultSdkVersionsIfNonePresent) {
88    ManifestFixerOptions options = { std::u16string(u"8"), std::u16string(u"22") };
89
90    std::unique_ptr<xml::XmlResource> doc = verifyWithOptions(R"EOF(
91      <manifest xmlns:android="http://schemas.android.com/apk/res/android"
92                package="android">
93        <uses-sdk android:minSdkVersion="7" android:targetSdkVersion="21" />
94      </manifest>)EOF", options);
95    ASSERT_NE(nullptr, doc);
96
97    xml::Element* el;
98    xml::Attribute* attr;
99
100    el = xml::findRootElement(doc->root.get());
101    ASSERT_NE(nullptr, el);
102    el = el->findChild({}, u"uses-sdk");
103    ASSERT_NE(nullptr, el);
104    attr = el->findAttribute(xml::kSchemaAndroid, u"minSdkVersion");
105    ASSERT_NE(nullptr, attr);
106    EXPECT_EQ(u"7", attr->value);
107    attr = el->findAttribute(xml::kSchemaAndroid, u"targetSdkVersion");
108    ASSERT_NE(nullptr, attr);
109    EXPECT_EQ(u"21", attr->value);
110
111    doc = verifyWithOptions(R"EOF(
112      <manifest xmlns:android="http://schemas.android.com/apk/res/android"
113                package="android">
114        <uses-sdk android:targetSdkVersion="21" />
115      </manifest>)EOF", options);
116    ASSERT_NE(nullptr, doc);
117
118    el = xml::findRootElement(doc->root.get());
119    ASSERT_NE(nullptr, el);
120    el = el->findChild({}, u"uses-sdk");
121    ASSERT_NE(nullptr, el);
122    attr = el->findAttribute(xml::kSchemaAndroid, u"minSdkVersion");
123    ASSERT_NE(nullptr, attr);
124    EXPECT_EQ(u"8", attr->value);
125    attr = el->findAttribute(xml::kSchemaAndroid, u"targetSdkVersion");
126    ASSERT_NE(nullptr, attr);
127    EXPECT_EQ(u"21", attr->value);
128
129    doc = verifyWithOptions(R"EOF(
130      <manifest xmlns:android="http://schemas.android.com/apk/res/android"
131                package="android">
132        <uses-sdk />
133      </manifest>)EOF", options);
134    ASSERT_NE(nullptr, doc);
135
136    el = xml::findRootElement(doc->root.get());
137    ASSERT_NE(nullptr, el);
138    el = el->findChild({}, u"uses-sdk");
139    ASSERT_NE(nullptr, el);
140    attr = el->findAttribute(xml::kSchemaAndroid, u"minSdkVersion");
141    ASSERT_NE(nullptr, attr);
142    EXPECT_EQ(u"8", attr->value);
143    attr = el->findAttribute(xml::kSchemaAndroid, u"targetSdkVersion");
144    ASSERT_NE(nullptr, attr);
145    EXPECT_EQ(u"22", attr->value);
146
147    doc = verifyWithOptions(R"EOF(
148      <manifest xmlns:android="http://schemas.android.com/apk/res/android"
149                package="android" />)EOF", options);
150    ASSERT_NE(nullptr, doc);
151
152    el = xml::findRootElement(doc->root.get());
153    ASSERT_NE(nullptr, el);
154    el = el->findChild({}, u"uses-sdk");
155    ASSERT_NE(nullptr, el);
156    attr = el->findAttribute(xml::kSchemaAndroid, u"minSdkVersion");
157    ASSERT_NE(nullptr, attr);
158    EXPECT_EQ(u"8", attr->value);
159    attr = el->findAttribute(xml::kSchemaAndroid, u"targetSdkVersion");
160    ASSERT_NE(nullptr, attr);
161    EXPECT_EQ(u"22", attr->value);
162}
163
164} // namespace aapt
165