blob: 432f10bdab97212f7993b45320401a648f5fcced [file] [log] [blame]
Adam Lesinski2ae4a872015-11-02 16:10:55 -08001/*
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"
Adam Lesinski2ae4a872015-11-02 16:10:55 -080018
Adam Lesinskice5e56e2016-10-21 17:56:45 -070019#include "test/Test.h"
Adam Lesinski2ae4a872015-11-02 16:10:55 -080020
Adam Lesinski87f1e0f2017-06-27 16:21:58 -070021using ::android::StringPiece;
Adam Lesinskic6284372017-12-04 13:46:23 -080022using ::testing::Eq;
23using ::testing::Gt;
24using ::testing::IsNull;
25using ::testing::Ne;
Adam Lesinski87f1e0f2017-06-27 16:21:58 -070026using ::testing::NotNull;
Adam Lesinskic6284372017-12-04 13:46:23 -080027using ::testing::StrEq;
Adam Lesinskid5083f62017-01-16 15:07:21 -080028
Adam Lesinski2ae4a872015-11-02 16:10:55 -080029namespace aapt {
30
31struct ManifestFixerTest : public ::testing::Test {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070032 std::unique_ptr<IAaptContext> mContext;
Adam Lesinski2ae4a872015-11-02 16:10:55 -080033
Adam Lesinskicacb28f2016-10-19 12:18:14 -070034 void SetUp() override {
35 mContext =
36 test::ContextBuilder()
Adam Lesinskice5e56e2016-10-21 17:56:45 -070037 .SetCompilationPackage("android")
38 .SetPackageId(0x01)
39 .SetNameManglerPolicy(NameManglerPolicy{"android"})
40 .AddSymbolSource(
Adam Lesinskicacb28f2016-10-19 12:18:14 -070041 test::StaticSymbolSourceBuilder()
Adam Lesinskice5e56e2016-10-21 17:56:45 -070042 .AddSymbol(
Adam Lesinskicacb28f2016-10-19 12:18:14 -070043 "android:attr/package", ResourceId(0x01010000),
44 test::AttributeBuilder()
Adam Lesinskice5e56e2016-10-21 17:56:45 -070045 .SetTypeMask(android::ResTable_map::TYPE_STRING)
46 .Build())
47 .AddSymbol(
Adam Lesinskicacb28f2016-10-19 12:18:14 -070048 "android:attr/minSdkVersion", ResourceId(0x01010001),
49 test::AttributeBuilder()
Adam Lesinskice5e56e2016-10-21 17:56:45 -070050 .SetTypeMask(android::ResTable_map::TYPE_STRING |
Adam Lesinskicacb28f2016-10-19 12:18:14 -070051 android::ResTable_map::TYPE_INTEGER)
Adam Lesinskice5e56e2016-10-21 17:56:45 -070052 .Build())
53 .AddSymbol(
Adam Lesinskicacb28f2016-10-19 12:18:14 -070054 "android:attr/targetSdkVersion", ResourceId(0x01010002),
55 test::AttributeBuilder()
Adam Lesinskice5e56e2016-10-21 17:56:45 -070056 .SetTypeMask(android::ResTable_map::TYPE_STRING |
Adam Lesinskicacb28f2016-10-19 12:18:14 -070057 android::ResTable_map::TYPE_INTEGER)
Adam Lesinskice5e56e2016-10-21 17:56:45 -070058 .Build())
59 .AddSymbol("android:string/str", ResourceId(0x01060000))
60 .Build())
61 .Build();
Adam Lesinskicacb28f2016-10-19 12:18:14 -070062 }
Adam Lesinski2ae4a872015-11-02 16:10:55 -080063
Adam Lesinskice5e56e2016-10-21 17:56:45 -070064 std::unique_ptr<xml::XmlResource> Verify(const StringPiece& str) {
65 return VerifyWithOptions(str, {});
Adam Lesinskicacb28f2016-10-19 12:18:14 -070066 }
Adam Lesinski2ae4a872015-11-02 16:10:55 -080067
Adam Lesinskice5e56e2016-10-21 17:56:45 -070068 std::unique_ptr<xml::XmlResource> VerifyWithOptions(
Adam Lesinskicacb28f2016-10-19 12:18:14 -070069 const StringPiece& str, const ManifestFixerOptions& options) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070070 std::unique_ptr<xml::XmlResource> doc = test::BuildXmlDom(str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070071 ManifestFixer fixer(options);
Adam Lesinskice5e56e2016-10-21 17:56:45 -070072 if (fixer.Consume(mContext.get(), doc.get())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070073 return doc;
Adam Lesinski2ae4a872015-11-02 16:10:55 -080074 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -070075 return {};
76 }
Adam Lesinski2ae4a872015-11-02 16:10:55 -080077};
78
79TEST_F(ManifestFixerTest, EnsureManifestIsRootTag) {
Adam Lesinskic6284372017-12-04 13:46:23 -080080 EXPECT_THAT(Verify("<other-tag />"), IsNull());
81 EXPECT_THAT(Verify("<ns:manifest xmlns:ns=\"com\" />"), IsNull());
82 EXPECT_THAT(Verify("<manifest package=\"android\"></manifest>"), NotNull());
Adam Lesinski2ae4a872015-11-02 16:10:55 -080083}
84
85TEST_F(ManifestFixerTest, EnsureManifestHasPackage) {
Adam Lesinskic6284372017-12-04 13:46:23 -080086 EXPECT_THAT(Verify("<manifest package=\"android\" />"), NotNull());
87 EXPECT_THAT(Verify("<manifest package=\"com.android\" />"), NotNull());
88 EXPECT_THAT(Verify("<manifest package=\"com.android.google\" />"), NotNull());
89 EXPECT_THAT(Verify("<manifest package=\"com.android.google.Class$1\" />"), IsNull());
90 EXPECT_THAT(Verify("<manifest xmlns:android=\"http://schemas.android.com/apk/res/android\" "
91 "android:package=\"com.android\" />"),
92 IsNull());
93 EXPECT_THAT(Verify("<manifest package=\"@string/str\" />"), IsNull());
Adam Lesinski2ae4a872015-11-02 16:10:55 -080094}
95
Adam Lesinski5119e512016-12-05 19:48:20 -080096TEST_F(ManifestFixerTest, AllowMetaData) {
Adam Lesinski86d67df2017-01-31 13:47:27 -080097 auto doc = Verify(R"EOF(
Adam Lesinski5119e512016-12-05 19:48:20 -080098 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
99 package="android">
100 <meta-data />
101 <application>
102 <meta-data />
103 <activity android:name=".Hi"><meta-data /></activity>
104 <activity-alias android:name=".Ho"><meta-data /></activity-alias>
Adam Lesinski86d67df2017-01-31 13:47:27 -0800105 <receiver android:name=".OffTo"><meta-data /></receiver>
106 <provider android:name=".Work"><meta-data /></provider>
107 <service android:name=".We"><meta-data /></service>
Adam Lesinski5119e512016-12-05 19:48:20 -0800108 </application>
Adam Lesinski86d67df2017-01-31 13:47:27 -0800109 <instrumentation android:name=".Go"><meta-data /></instrumentation>
Adam Lesinski5119e512016-12-05 19:48:20 -0800110 </manifest>)EOF");
Adam Lesinskic6284372017-12-04 13:46:23 -0800111 ASSERT_THAT(doc, NotNull());
Adam Lesinski5119e512016-12-05 19:48:20 -0800112}
113
Adam Lesinski2ae4a872015-11-02 16:10:55 -0800114TEST_F(ManifestFixerTest, UseDefaultSdkVersionsIfNonePresent) {
Izabela Orlowskaad9e1322017-12-19 16:22:42 +0000115 ManifestFixerOptions options;
116 options.min_sdk_version_default = std::string("8");
117 options.target_sdk_version_default = std::string("22");
Adam Lesinski2ae4a872015-11-02 16:10:55 -0800118
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700119 std::unique_ptr<xml::XmlResource> doc = VerifyWithOptions(R"EOF(
Adam Lesinski2ae4a872015-11-02 16:10:55 -0800120 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
121 package="android">
122 <uses-sdk android:minSdkVersion="7" android:targetSdkVersion="21" />
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700123 </manifest>)EOF",
124 options);
Adam Lesinskic6284372017-12-04 13:46:23 -0800125 ASSERT_THAT(doc, NotNull());
Adam Lesinski2ae4a872015-11-02 16:10:55 -0800126
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700127 xml::Element* el;
128 xml::Attribute* attr;
Adam Lesinski2ae4a872015-11-02 16:10:55 -0800129
Adam Lesinski6b372992017-08-09 10:54:23 -0700130 el = doc->root.get();
Adam Lesinskic6284372017-12-04 13:46:23 -0800131 ASSERT_THAT(el, NotNull());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700132 el = el->FindChild({}, "uses-sdk");
Adam Lesinskic6284372017-12-04 13:46:23 -0800133 ASSERT_THAT(el, NotNull());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700134 attr = el->FindAttribute(xml::kSchemaAndroid, "minSdkVersion");
Adam Lesinskic6284372017-12-04 13:46:23 -0800135 ASSERT_THAT(attr, NotNull());
136 EXPECT_THAT(attr->value, StrEq("7"));
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700137 attr = el->FindAttribute(xml::kSchemaAndroid, "targetSdkVersion");
Adam Lesinskic6284372017-12-04 13:46:23 -0800138 ASSERT_THAT(attr, NotNull());
139 EXPECT_THAT(attr->value, StrEq("21"));
Adam Lesinski2ae4a872015-11-02 16:10:55 -0800140
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700141 doc = VerifyWithOptions(R"EOF(
Adam Lesinski2ae4a872015-11-02 16:10:55 -0800142 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
143 package="android">
144 <uses-sdk android:targetSdkVersion="21" />
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700145 </manifest>)EOF",
146 options);
Adam Lesinskic6284372017-12-04 13:46:23 -0800147 ASSERT_THAT(doc, NotNull());
Adam Lesinski2ae4a872015-11-02 16:10:55 -0800148
Adam Lesinski6b372992017-08-09 10:54:23 -0700149 el = doc->root.get();
Adam Lesinskic6284372017-12-04 13:46:23 -0800150 ASSERT_THAT(el, NotNull());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700151 el = el->FindChild({}, "uses-sdk");
Adam Lesinskic6284372017-12-04 13:46:23 -0800152 ASSERT_THAT(el, NotNull());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700153 attr = el->FindAttribute(xml::kSchemaAndroid, "minSdkVersion");
Adam Lesinskic6284372017-12-04 13:46:23 -0800154 ASSERT_THAT(attr, NotNull());
155 EXPECT_THAT(attr->value, StrEq("8"));
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700156 attr = el->FindAttribute(xml::kSchemaAndroid, "targetSdkVersion");
Adam Lesinskic6284372017-12-04 13:46:23 -0800157 ASSERT_THAT(attr, NotNull());
158 EXPECT_THAT(attr->value, StrEq("21"));
Adam Lesinski2ae4a872015-11-02 16:10:55 -0800159
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700160 doc = VerifyWithOptions(R"EOF(
Adam Lesinski2ae4a872015-11-02 16:10:55 -0800161 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
162 package="android">
163 <uses-sdk />
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700164 </manifest>)EOF",
165 options);
Adam Lesinskic6284372017-12-04 13:46:23 -0800166 ASSERT_THAT(doc, NotNull());
Adam Lesinski2ae4a872015-11-02 16:10:55 -0800167
Adam Lesinski6b372992017-08-09 10:54:23 -0700168 el = doc->root.get();
Adam Lesinskic6284372017-12-04 13:46:23 -0800169 ASSERT_THAT(el, NotNull());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700170 el = el->FindChild({}, "uses-sdk");
Adam Lesinskic6284372017-12-04 13:46:23 -0800171 ASSERT_THAT(el, NotNull());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700172 attr = el->FindAttribute(xml::kSchemaAndroid, "minSdkVersion");
Adam Lesinskic6284372017-12-04 13:46:23 -0800173 ASSERT_THAT(attr, NotNull());
174 EXPECT_THAT(attr->value, StrEq("8"));
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700175 attr = el->FindAttribute(xml::kSchemaAndroid, "targetSdkVersion");
Adam Lesinskic6284372017-12-04 13:46:23 -0800176 ASSERT_THAT(attr, NotNull());
177 EXPECT_THAT(attr->value, StrEq("22"));
Adam Lesinski2ae4a872015-11-02 16:10:55 -0800178
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700179 doc = VerifyWithOptions(R"EOF(
Adam Lesinski2ae4a872015-11-02 16:10:55 -0800180 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700181 package="android" />)EOF",
182 options);
Adam Lesinskic6284372017-12-04 13:46:23 -0800183 ASSERT_THAT(doc, NotNull());
Adam Lesinski2ae4a872015-11-02 16:10:55 -0800184
Adam Lesinski6b372992017-08-09 10:54:23 -0700185 el = doc->root.get();
Adam Lesinskic6284372017-12-04 13:46:23 -0800186 ASSERT_THAT(el, NotNull());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700187 el = el->FindChild({}, "uses-sdk");
Adam Lesinskic6284372017-12-04 13:46:23 -0800188 ASSERT_THAT(el, NotNull());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700189 attr = el->FindAttribute(xml::kSchemaAndroid, "minSdkVersion");
Adam Lesinskic6284372017-12-04 13:46:23 -0800190 ASSERT_THAT(attr, NotNull());
191 EXPECT_THAT(attr->value, StrEq("8"));
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700192 attr = el->FindAttribute(xml::kSchemaAndroid, "targetSdkVersion");
Adam Lesinskic6284372017-12-04 13:46:23 -0800193 ASSERT_THAT(attr, NotNull());
194 EXPECT_THAT(attr->value, StrEq("22"));
Adam Lesinski2ae4a872015-11-02 16:10:55 -0800195}
196
Adam Lesinskie343eb12016-10-27 16:31:58 -0700197TEST_F(ManifestFixerTest, UsesSdkMustComeBeforeApplication) {
Izabela Orlowskaad9e1322017-12-19 16:22:42 +0000198 ManifestFixerOptions options;
199 options.min_sdk_version_default = std::string("8");
200 options.target_sdk_version_default = std::string("22");
Adam Lesinskie343eb12016-10-27 16:31:58 -0700201 std::unique_ptr<xml::XmlResource> doc = VerifyWithOptions(R"EOF(
202 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
203 package="android">
204 <application android:name=".MainApplication" />
205 </manifest>)EOF",
206 options);
Adam Lesinskic6284372017-12-04 13:46:23 -0800207 ASSERT_THAT(doc, NotNull());
Adam Lesinskie343eb12016-10-27 16:31:58 -0700208
Adam Lesinski6b372992017-08-09 10:54:23 -0700209 xml::Element* manifest_el = doc->root.get();
Adam Lesinskic6284372017-12-04 13:46:23 -0800210 ASSERT_THAT(manifest_el, NotNull());
Adam Lesinskie343eb12016-10-27 16:31:58 -0700211 ASSERT_EQ("manifest", manifest_el->name);
212
213 xml::Element* application_el = manifest_el->FindChild("", "application");
Adam Lesinskic6284372017-12-04 13:46:23 -0800214 ASSERT_THAT(application_el, NotNull());
Adam Lesinskie343eb12016-10-27 16:31:58 -0700215
216 xml::Element* uses_sdk_el = manifest_el->FindChild("", "uses-sdk");
Adam Lesinskic6284372017-12-04 13:46:23 -0800217 ASSERT_THAT(uses_sdk_el, NotNull());
Adam Lesinskie343eb12016-10-27 16:31:58 -0700218
219 // Check that the uses_sdk_el comes before application_el in the children
220 // vector.
221 // Since there are no namespaces here, these children are direct descendants
222 // of manifest.
223 auto uses_sdk_iter =
224 std::find_if(manifest_el->children.begin(), manifest_el->children.end(),
225 [&](const std::unique_ptr<xml::Node>& child) {
226 return child.get() == uses_sdk_el;
227 });
228
229 auto application_iter =
230 std::find_if(manifest_el->children.begin(), manifest_el->children.end(),
231 [&](const std::unique_ptr<xml::Node>& child) {
232 return child.get() == application_el;
233 });
234
Adam Lesinskic6284372017-12-04 13:46:23 -0800235 ASSERT_THAT(uses_sdk_iter, Ne(manifest_el->children.end()));
236 ASSERT_THAT(application_iter, Ne(manifest_el->children.end()));
Adam Lesinskie343eb12016-10-27 16:31:58 -0700237
238 // The distance should be positive, meaning uses_sdk_iter comes before
239 // application_iter.
Adam Lesinskic6284372017-12-04 13:46:23 -0800240 EXPECT_THAT(std::distance(uses_sdk_iter, application_iter), Gt(0));
Adam Lesinskie343eb12016-10-27 16:31:58 -0700241}
242
Adam Lesinski52364f72016-01-11 13:10:24 -0800243TEST_F(ManifestFixerTest, RenameManifestPackageAndFullyQualifyClasses) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700244 ManifestFixerOptions options;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700245 options.rename_manifest_package = std::string("com.android");
Adam Lesinski52364f72016-01-11 13:10:24 -0800246
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700247 std::unique_ptr<xml::XmlResource> doc = VerifyWithOptions(R"EOF(
Adam Lesinski52364f72016-01-11 13:10:24 -0800248 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
249 package="android">
Adam Lesinski23034b92017-11-29 16:27:44 -0800250 <uses-split android:name="feature_a" />
Adam Lesinskicc5609d2016-04-05 12:41:07 -0700251 <application android:name=".MainApplication" text="hello">
252 <activity android:name=".activity.Start" />
253 <receiver android:name="com.google.android.Receiver" />
Adam Lesinski52364f72016-01-11 13:10:24 -0800254 </application>
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700255 </manifest>)EOF",
256 options);
Adam Lesinskic6284372017-12-04 13:46:23 -0800257 ASSERT_THAT(doc, NotNull());
Adam Lesinski52364f72016-01-11 13:10:24 -0800258
Adam Lesinski23034b92017-11-29 16:27:44 -0800259 xml::Element* manifest_el = doc->root.get();
Adam Lesinskic6284372017-12-04 13:46:23 -0800260 ASSERT_THAT(manifest_el, NotNull());
Adam Lesinski52364f72016-01-11 13:10:24 -0800261
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700262 xml::Attribute* attr = nullptr;
Adam Lesinski52364f72016-01-11 13:10:24 -0800263
Adam Lesinski23034b92017-11-29 16:27:44 -0800264 attr = manifest_el->FindAttribute({}, "package");
Adam Lesinskic6284372017-12-04 13:46:23 -0800265 ASSERT_THAT(attr, NotNull());
266 EXPECT_THAT(attr->value, StrEq("com.android"));
Adam Lesinski52364f72016-01-11 13:10:24 -0800267
Adam Lesinski23034b92017-11-29 16:27:44 -0800268 xml::Element* uses_split_el = manifest_el->FindChild({}, "uses-split");
Adam Lesinskic6284372017-12-04 13:46:23 -0800269 ASSERT_THAT(uses_split_el, NotNull());
Adam Lesinski23034b92017-11-29 16:27:44 -0800270 attr = uses_split_el->FindAttribute(xml::kSchemaAndroid, "name");
Adam Lesinskic6284372017-12-04 13:46:23 -0800271 ASSERT_THAT(attr, NotNull());
272 // This should NOT have been affected.
273 EXPECT_THAT(attr->value, StrEq("feature_a"));
Adam Lesinski52364f72016-01-11 13:10:24 -0800274
Adam Lesinski23034b92017-11-29 16:27:44 -0800275 xml::Element* application_el = manifest_el->FindChild({}, "application");
Adam Lesinskic6284372017-12-04 13:46:23 -0800276 ASSERT_THAT(application_el, NotNull());
Adam Lesinski23034b92017-11-29 16:27:44 -0800277
278 attr = application_el->FindAttribute(xml::kSchemaAndroid, "name");
Adam Lesinskic6284372017-12-04 13:46:23 -0800279 ASSERT_THAT(attr, NotNull());
280 EXPECT_THAT(attr->value, StrEq("android.MainApplication"));
Adam Lesinski52364f72016-01-11 13:10:24 -0800281
Adam Lesinski23034b92017-11-29 16:27:44 -0800282 attr = application_el->FindAttribute({}, "text");
Adam Lesinskic6284372017-12-04 13:46:23 -0800283 ASSERT_THAT(attr, NotNull());
284 EXPECT_THAT(attr->value, StrEq("hello"));
Adam Lesinski52364f72016-01-11 13:10:24 -0800285
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700286 xml::Element* el;
Adam Lesinski23034b92017-11-29 16:27:44 -0800287 el = application_el->FindChild({}, "activity");
Adam Lesinskic6284372017-12-04 13:46:23 -0800288 ASSERT_THAT(el, NotNull());
Adam Lesinski52364f72016-01-11 13:10:24 -0800289
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700290 attr = el->FindAttribute(xml::kSchemaAndroid, "name");
Adam Lesinskic6284372017-12-04 13:46:23 -0800291 ASSERT_THAT(el, NotNull());
292 EXPECT_THAT(attr->value, StrEq("android.activity.Start"));
Adam Lesinski52364f72016-01-11 13:10:24 -0800293
Adam Lesinski23034b92017-11-29 16:27:44 -0800294 el = application_el->FindChild({}, "receiver");
Adam Lesinskic6284372017-12-04 13:46:23 -0800295 ASSERT_THAT(el, NotNull());
Adam Lesinski52364f72016-01-11 13:10:24 -0800296
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700297 attr = el->FindAttribute(xml::kSchemaAndroid, "name");
Adam Lesinskic6284372017-12-04 13:46:23 -0800298 ASSERT_THAT(el, NotNull());
299 EXPECT_THAT(attr->value, StrEq("com.google.android.Receiver"));
Adam Lesinski52364f72016-01-11 13:10:24 -0800300}
301
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700302TEST_F(ManifestFixerTest,
303 RenameManifestInstrumentationPackageAndFullyQualifyTarget) {
304 ManifestFixerOptions options;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700305 options.rename_instrumentation_target_package = std::string("com.android");
Adam Lesinski52364f72016-01-11 13:10:24 -0800306
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700307 std::unique_ptr<xml::XmlResource> doc = VerifyWithOptions(R"EOF(
Adam Lesinski52364f72016-01-11 13:10:24 -0800308 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
309 package="android">
Adam Lesinski86d67df2017-01-31 13:47:27 -0800310 <instrumentation android:name=".TestRunner" android:targetPackage="android" />
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700311 </manifest>)EOF",
312 options);
Adam Lesinskic6284372017-12-04 13:46:23 -0800313 ASSERT_THAT(doc, NotNull());
Adam Lesinski52364f72016-01-11 13:10:24 -0800314
Adam Lesinski6b372992017-08-09 10:54:23 -0700315 xml::Element* manifest_el = doc->root.get();
Adam Lesinskic6284372017-12-04 13:46:23 -0800316 ASSERT_THAT(manifest_el, NotNull());
Adam Lesinski52364f72016-01-11 13:10:24 -0800317
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700318 xml::Element* instrumentation_el =
319 manifest_el->FindChild({}, "instrumentation");
Adam Lesinskic6284372017-12-04 13:46:23 -0800320 ASSERT_THAT(instrumentation_el, NotNull());
Adam Lesinski52364f72016-01-11 13:10:24 -0800321
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700322 xml::Attribute* attr =
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700323 instrumentation_el->FindAttribute(xml::kSchemaAndroid, "targetPackage");
Adam Lesinskic6284372017-12-04 13:46:23 -0800324 ASSERT_THAT(attr, NotNull());
325 EXPECT_THAT(attr->value, StrEq("com.android"));
Adam Lesinski52364f72016-01-11 13:10:24 -0800326}
327
Roshan Piusae12ce42020-04-25 16:08:55 -0700328TEST_F(ManifestFixerTest,
329 RenameManifestOverlayPackageAndFullyQualifyTarget) {
330 ManifestFixerOptions options;
331 options.rename_overlay_target_package = std::string("com.android");
332
333 std::unique_ptr<xml::XmlResource> doc = VerifyWithOptions(R"EOF(
334 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
335 package="android">
336 <overlay android:targetName="Customization" android:targetPackage="android" />
337 </manifest>)EOF",
338 options);
339 ASSERT_THAT(doc, NotNull());
340
341 xml::Element* manifest_el = doc->root.get();
342 ASSERT_THAT(manifest_el, NotNull());
343
344 xml::Element* overlay_el =
345 manifest_el->FindChild({}, "overlay");
346 ASSERT_THAT(overlay_el, NotNull());
347
348 xml::Attribute* attr =
349 overlay_el->FindAttribute(xml::kSchemaAndroid, "targetPackage");
350 ASSERT_THAT(attr, NotNull());
351 EXPECT_THAT(attr->value, StrEq("com.android"));
352}
353
Adam Lesinski52364f72016-01-11 13:10:24 -0800354TEST_F(ManifestFixerTest, UseDefaultVersionNameAndCode) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700355 ManifestFixerOptions options;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700356 options.version_name_default = std::string("Beta");
357 options.version_code_default = std::string("0x10000000");
Ryan Mitchell704090e2018-07-31 14:59:25 -0700358 options.version_code_major_default = std::string("0x20000000");
Adam Lesinski52364f72016-01-11 13:10:24 -0800359
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700360 std::unique_ptr<xml::XmlResource> doc = VerifyWithOptions(R"EOF(
Adam Lesinski52364f72016-01-11 13:10:24 -0800361 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700362 package="android" />)EOF",
363 options);
Adam Lesinskic6284372017-12-04 13:46:23 -0800364 ASSERT_THAT(doc, NotNull());
Adam Lesinski52364f72016-01-11 13:10:24 -0800365
Adam Lesinski6b372992017-08-09 10:54:23 -0700366 xml::Element* manifest_el = doc->root.get();
Adam Lesinskic6284372017-12-04 13:46:23 -0800367 ASSERT_THAT(manifest_el, NotNull());
Adam Lesinski52364f72016-01-11 13:10:24 -0800368
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700369 xml::Attribute* attr =
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700370 manifest_el->FindAttribute(xml::kSchemaAndroid, "versionName");
Adam Lesinskic6284372017-12-04 13:46:23 -0800371 ASSERT_THAT(attr, NotNull());
372 EXPECT_THAT(attr->value, StrEq("Beta"));
Adam Lesinski52364f72016-01-11 13:10:24 -0800373
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700374 attr = manifest_el->FindAttribute(xml::kSchemaAndroid, "versionCode");
Adam Lesinskic6284372017-12-04 13:46:23 -0800375 ASSERT_THAT(attr, NotNull());
376 EXPECT_THAT(attr->value, StrEq("0x10000000"));
Ryan Mitchell704090e2018-07-31 14:59:25 -0700377
378 attr = manifest_el->FindAttribute(xml::kSchemaAndroid, "versionCodeMajor");
379 ASSERT_THAT(attr, NotNull());
380 EXPECT_THAT(attr->value, StrEq("0x20000000"));
Adam Lesinski52364f72016-01-11 13:10:24 -0800381}
382
Colin Crossdcd58c42018-05-25 22:46:35 -0700383TEST_F(ManifestFixerTest, DontUseDefaultVersionNameAndCode) {
Ryan Mitchell704090e2018-07-31 14:59:25 -0700384 ManifestFixerOptions options;
385 options.version_name_default = std::string("Beta");
386 options.version_code_default = std::string("0x10000000");
387 options.version_code_major_default = std::string("0x20000000");
Colin Crossdcd58c42018-05-25 22:46:35 -0700388
Ryan Mitchell704090e2018-07-31 14:59:25 -0700389 std::unique_ptr<xml::XmlResource> doc = VerifyWithOptions(R"EOF(
390 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
391 package="android"
392 android:versionCode="0x00000001"
393 android:versionCodeMajor="0x00000002"
394 android:versionName="Alpha" />)EOF",
395 options);
396 ASSERT_THAT(doc, NotNull());
Colin Crossdcd58c42018-05-25 22:46:35 -0700397
Ryan Mitchell704090e2018-07-31 14:59:25 -0700398 xml::Element* manifest_el = doc->root.get();
399 ASSERT_THAT(manifest_el, NotNull());
Colin Crossdcd58c42018-05-25 22:46:35 -0700400
Ryan Mitchell704090e2018-07-31 14:59:25 -0700401 xml::Attribute* attr =
402 manifest_el->FindAttribute(xml::kSchemaAndroid, "versionName");
403 ASSERT_THAT(attr, NotNull());
404 EXPECT_THAT(attr->value, StrEq("Alpha"));
Colin Crossdcd58c42018-05-25 22:46:35 -0700405
Ryan Mitchell704090e2018-07-31 14:59:25 -0700406 attr = manifest_el->FindAttribute(xml::kSchemaAndroid, "versionCode");
407 ASSERT_THAT(attr, NotNull());
408 EXPECT_THAT(attr->value, StrEq("0x00000001"));
409
410 attr = manifest_el->FindAttribute(xml::kSchemaAndroid, "versionCodeMajor");
411 ASSERT_THAT(attr, NotNull());
412 EXPECT_THAT(attr->value, StrEq("0x00000002"));
Colin Crossdcd58c42018-05-25 22:46:35 -0700413}
414
415TEST_F(ManifestFixerTest, ReplaceVersionNameAndCode) {
Ryan Mitchell704090e2018-07-31 14:59:25 -0700416 ManifestFixerOptions options;
417 options.replace_version = true;
418 options.version_name_default = std::string("Beta");
419 options.version_code_default = std::string("0x10000000");
420 options.version_code_major_default = std::string("0x20000000");
Colin Crossdcd58c42018-05-25 22:46:35 -0700421
Ryan Mitchell704090e2018-07-31 14:59:25 -0700422 std::unique_ptr<xml::XmlResource> doc = VerifyWithOptions(R"EOF(
423 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
424 package="android"
425 android:versionCode="0x00000001"
426 android:versionCodeMajor="0x00000002"
427 android:versionName="Alpha" />)EOF",
428 options);
429 ASSERT_THAT(doc, NotNull());
Colin Crossdcd58c42018-05-25 22:46:35 -0700430
Ryan Mitchell704090e2018-07-31 14:59:25 -0700431 xml::Element* manifest_el = doc->root.get();
432 ASSERT_THAT(manifest_el, NotNull());
Colin Crossdcd58c42018-05-25 22:46:35 -0700433
Ryan Mitchell704090e2018-07-31 14:59:25 -0700434 xml::Attribute* attr =
435 manifest_el->FindAttribute(xml::kSchemaAndroid, "versionName");
436 ASSERT_THAT(attr, NotNull());
437 EXPECT_THAT(attr->value, StrEq("Beta"));
Colin Crossdcd58c42018-05-25 22:46:35 -0700438
Ryan Mitchell704090e2018-07-31 14:59:25 -0700439 attr = manifest_el->FindAttribute(xml::kSchemaAndroid, "versionCode");
440 ASSERT_THAT(attr, NotNull());
441 EXPECT_THAT(attr->value, StrEq("0x10000000"));
442
443 attr = manifest_el->FindAttribute(xml::kSchemaAndroid, "versionCodeMajor");
444 ASSERT_THAT(attr, NotNull());
445 EXPECT_THAT(attr->value, StrEq("0x20000000"));
Colin Crossdcd58c42018-05-25 22:46:35 -0700446}
447
Rhed Jaob9ccb8a42020-11-30 21:42:16 +0800448TEST_F(ManifestFixerTest, UseDefaultRevisionCode) {
449 ManifestFixerOptions options;
450 options.revision_code_default = std::string("0x10000000");
451
452 std::unique_ptr<xml::XmlResource> doc = VerifyWithOptions(R"EOF(
453 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
454 package="android"
455 android:versionCode="0x00000001" />)EOF",
456 options);
457 ASSERT_THAT(doc, NotNull());
458
459 xml::Element* manifest_el = doc->root.get();
460 ASSERT_THAT(manifest_el, NotNull());
461
462 xml::Attribute* attr = manifest_el->FindAttribute(xml::kSchemaAndroid, "revisionCode");
463 ASSERT_THAT(attr, NotNull());
464 EXPECT_THAT(attr->value, StrEq("0x10000000"));
465}
466
467TEST_F(ManifestFixerTest, DontUseDefaultRevisionCode) {
468 ManifestFixerOptions options;
469 options.revision_code_default = std::string("0x10000000");
470
471 std::unique_ptr<xml::XmlResource> doc = VerifyWithOptions(R"EOF(
472 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
473 package="android"
474 android:versionCode="0x00000001"
475 android:revisionCode="0x00000002" />)EOF",
476 options);
477 ASSERT_THAT(doc, NotNull());
478
479 xml::Element* manifest_el = doc->root.get();
480 ASSERT_THAT(manifest_el, NotNull());
481
482 xml::Attribute* attr = manifest_el->FindAttribute(xml::kSchemaAndroid, "revisionCode");
483 ASSERT_THAT(attr, NotNull());
484 EXPECT_THAT(attr->value, StrEq("0x00000002"));
485}
486
487TEST_F(ManifestFixerTest, ReplaceRevisionCode) {
488 ManifestFixerOptions options;
489 options.replace_version = true;
490 options.revision_code_default = std::string("0x10000000");
491
492 std::unique_ptr<xml::XmlResource> doc = VerifyWithOptions(R"EOF(
493 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
494 package="android"
495 android:versionCode="0x00000001"
496 android:revisionCode="0x00000002" />)EOF",
497 options);
498 ASSERT_THAT(doc, NotNull());
499
500 xml::Element* manifest_el = doc->root.get();
501 ASSERT_THAT(manifest_el, NotNull());
502
503 xml::Attribute* attr = manifest_el->FindAttribute(xml::kSchemaAndroid, "revisionCode");
504 ASSERT_THAT(attr, NotNull());
505 EXPECT_THAT(attr->value, StrEq("0x10000000"));
506}
507
Colin Crossdcd58c42018-05-25 22:46:35 -0700508TEST_F(ManifestFixerTest, ReplaceVersionName) {
Ryan Mitchell704090e2018-07-31 14:59:25 -0700509 ManifestFixerOptions options;
510 options.replace_version = true;
511 options.version_name_default = std::string("Beta");
Colin Crossdcd58c42018-05-25 22:46:35 -0700512
Colin Crossdcd58c42018-05-25 22:46:35 -0700513
Ryan Mitchell704090e2018-07-31 14:59:25 -0700514 std::unique_ptr<xml::XmlResource> doc = VerifyWithOptions(R"EOF(
515 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
516 package="android"
517 android:versionCode="0x00000001"
518 android:versionCodeMajor="0x00000002"
519 android:versionName="Alpha" />)EOF",
520 options);
521 ASSERT_THAT(doc, NotNull());
Colin Crossdcd58c42018-05-25 22:46:35 -0700522
Ryan Mitchell704090e2018-07-31 14:59:25 -0700523 xml::Element* manifest_el = doc->root.get();
524 ASSERT_THAT(manifest_el, NotNull());
Colin Crossdcd58c42018-05-25 22:46:35 -0700525
Ryan Mitchell704090e2018-07-31 14:59:25 -0700526 xml::Attribute* attr =
527 manifest_el->FindAttribute(xml::kSchemaAndroid, "versionName");
528 ASSERT_THAT(attr, NotNull());
529 EXPECT_THAT(attr->value, StrEq("Beta"));
530
531 attr = manifest_el->FindAttribute(xml::kSchemaAndroid, "versionCode");
532 ASSERT_THAT(attr, NotNull());
533 EXPECT_THAT(attr->value, StrEq("0x00000001"));
534
535 attr = manifest_el->FindAttribute(xml::kSchemaAndroid, "versionCodeMajor");
536 ASSERT_THAT(attr, NotNull());
537 EXPECT_THAT(attr->value, StrEq("0x00000002"));
Colin Crossdcd58c42018-05-25 22:46:35 -0700538}
539
540TEST_F(ManifestFixerTest, ReplaceVersionCode) {
Ryan Mitchell704090e2018-07-31 14:59:25 -0700541 ManifestFixerOptions options;
542 options.replace_version = true;
543 options.version_code_default = std::string("0x10000000");
Colin Crossdcd58c42018-05-25 22:46:35 -0700544
Ryan Mitchell704090e2018-07-31 14:59:25 -0700545 std::unique_ptr<xml::XmlResource> doc = VerifyWithOptions(R"EOF(
546 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
547 package="android"
548 android:versionCode="0x00000001"
549 android:versionCodeMajor="0x00000002"
550 android:versionName="Alpha" />)EOF",
551 options);
552 ASSERT_THAT(doc, NotNull());
553
554 xml::Element* manifest_el = doc->root.get();
555 ASSERT_THAT(manifest_el, NotNull());
556
557 xml::Attribute* attr =
558 manifest_el->FindAttribute(xml::kSchemaAndroid, "versionName");
559 ASSERT_THAT(attr, NotNull());
560 EXPECT_THAT(attr->value, StrEq("Alpha"));
561
562 attr = manifest_el->FindAttribute(xml::kSchemaAndroid, "versionCode");
563 ASSERT_THAT(attr, NotNull());
564 EXPECT_THAT(attr->value, StrEq("0x10000000"));
565
566 attr = manifest_el->FindAttribute(xml::kSchemaAndroid, "versionCodeMajor");
567 ASSERT_THAT(attr, NotNull());
568 EXPECT_THAT(attr->value, StrEq("0x00000002"));
569}
570
571TEST_F(ManifestFixerTest, ReplaceVersionCodeMajor) {
572 ManifestFixerOptions options;
573 options.replace_version = true;
574 options.version_code_major_default = std::string("0x20000000");
575
576 std::unique_ptr<xml::XmlResource> doc = VerifyWithOptions(R"EOF(
Colin Crossdcd58c42018-05-25 22:46:35 -0700577 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
Ryan Mitchell704090e2018-07-31 14:59:25 -0700578 package="android"
579 android:versionCode="0x00000001"
580 android:versionCodeMajor="0x00000002"
581 android:versionName="Alpha" />)EOF",
582 options);
583 ASSERT_THAT(doc, NotNull());
Colin Crossdcd58c42018-05-25 22:46:35 -0700584
Ryan Mitchell704090e2018-07-31 14:59:25 -0700585 xml::Element* manifest_el = doc->root.get();
586 ASSERT_THAT(manifest_el, NotNull());
Colin Crossdcd58c42018-05-25 22:46:35 -0700587
Ryan Mitchell704090e2018-07-31 14:59:25 -0700588 xml::Attribute* attr =
589 manifest_el->FindAttribute(xml::kSchemaAndroid, "versionName");
590 ASSERT_THAT(attr, NotNull());
591 EXPECT_THAT(attr->value, StrEq("Alpha"));
Colin Crossdcd58c42018-05-25 22:46:35 -0700592
Ryan Mitchell704090e2018-07-31 14:59:25 -0700593 attr = manifest_el->FindAttribute(xml::kSchemaAndroid, "versionCode");
594 ASSERT_THAT(attr, NotNull());
595 EXPECT_THAT(attr->value, StrEq("0x00000001"));
596
597 attr = manifest_el->FindAttribute(xml::kSchemaAndroid, "versionCodeMajor");
598 ASSERT_THAT(attr, NotNull());
599 EXPECT_THAT(attr->value, StrEq("0x20000000"));
Colin Crossdcd58c42018-05-25 22:46:35 -0700600}
601
602TEST_F(ManifestFixerTest, DontReplaceVersionNameOrCode) {
Ryan Mitchell704090e2018-07-31 14:59:25 -0700603 ManifestFixerOptions options;
604 options.replace_version = true;
Colin Crossdcd58c42018-05-25 22:46:35 -0700605
Ryan Mitchell704090e2018-07-31 14:59:25 -0700606 std::unique_ptr<xml::XmlResource> doc = VerifyWithOptions(R"EOF(
607 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
608 package="android"
609 android:versionCode="0x00000001"
610 android:versionCodeMajor="0x00000002"
611 android:versionName="Alpha" />)EOF",
612 options);
613 ASSERT_THAT(doc, NotNull());
Colin Crossdcd58c42018-05-25 22:46:35 -0700614
Ryan Mitchell704090e2018-07-31 14:59:25 -0700615 xml::Element* manifest_el = doc->root.get();
616 ASSERT_THAT(manifest_el, NotNull());
Colin Crossdcd58c42018-05-25 22:46:35 -0700617
Ryan Mitchell704090e2018-07-31 14:59:25 -0700618 xml::Attribute* attr =
619 manifest_el->FindAttribute(xml::kSchemaAndroid, "versionName");
620 ASSERT_THAT(attr, NotNull());
621 EXPECT_THAT(attr->value, StrEq("Alpha"));
Colin Crossdcd58c42018-05-25 22:46:35 -0700622
Ryan Mitchell704090e2018-07-31 14:59:25 -0700623 attr = manifest_el->FindAttribute(xml::kSchemaAndroid, "versionCode");
624 ASSERT_THAT(attr, NotNull());
625 EXPECT_THAT(attr->value, StrEq("0x00000001"));
626
627 attr = manifest_el->FindAttribute(xml::kSchemaAndroid, "versionCodeMajor");
628 ASSERT_THAT(attr, NotNull());
629 EXPECT_THAT(attr->value, StrEq("0x00000002"));
Colin Crossdcd58c42018-05-25 22:46:35 -0700630}
631
Adam Lesinski6b17d2c2016-08-10 11:37:06 -0700632TEST_F(ManifestFixerTest, EnsureManifestAttributesAreTyped) {
Adam Lesinskic6284372017-12-04 13:46:23 -0800633 EXPECT_THAT(Verify("<manifest package=\"android\" coreApp=\"hello\" />"), IsNull());
634 EXPECT_THAT(Verify("<manifest package=\"android\" coreApp=\"1dp\" />"), IsNull());
Adam Lesinski6b17d2c2016-08-10 11:37:06 -0700635
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700636 std::unique_ptr<xml::XmlResource> doc =
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700637 Verify("<manifest package=\"android\" coreApp=\"true\" />");
Adam Lesinskic6284372017-12-04 13:46:23 -0800638 ASSERT_THAT(doc, NotNull());
Adam Lesinski6b17d2c2016-08-10 11:37:06 -0700639
Adam Lesinski6b372992017-08-09 10:54:23 -0700640 xml::Element* el = doc->root.get();
Adam Lesinskic6284372017-12-04 13:46:23 -0800641 ASSERT_THAT(el, NotNull());
Adam Lesinski6b17d2c2016-08-10 11:37:06 -0700642
Adam Lesinskic6284372017-12-04 13:46:23 -0800643 EXPECT_THAT(el->name, StrEq("manifest"));
Adam Lesinski6b17d2c2016-08-10 11:37:06 -0700644
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700645 xml::Attribute* attr = el->FindAttribute("", "coreApp");
Adam Lesinskic6284372017-12-04 13:46:23 -0800646 ASSERT_THAT(attr, NotNull());
Adam Lesinski6b17d2c2016-08-10 11:37:06 -0700647
Adam Lesinskic6284372017-12-04 13:46:23 -0800648 EXPECT_THAT(attr->compiled_value, NotNull());
649 EXPECT_THAT(ValueCast<BinaryPrimitive>(attr->compiled_value.get()), NotNull());
Adam Lesinski6b17d2c2016-08-10 11:37:06 -0700650}
651
Adam Lesinski86d67df2017-01-31 13:47:27 -0800652TEST_F(ManifestFixerTest, UsesFeatureMustHaveNameOrGlEsVersion) {
653 std::string input = R"EOF(
654 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
655 package="android">
656 <uses-feature android:name="feature" />
657 <uses-feature android:glEsVersion="1" />
658 <feature-group />
659 <feature-group>
660 <uses-feature android:name="feature_in_group" />
661 <uses-feature android:glEsVersion="2" />
662 </feature-group>
663 </manifest>)EOF";
Adam Lesinskic6284372017-12-04 13:46:23 -0800664 EXPECT_THAT(Verify(input), NotNull());
Adam Lesinski86d67df2017-01-31 13:47:27 -0800665
666 input = R"EOF(
667 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
668 package="android">
669 <uses-feature android:name="feature" android:glEsVersion="1" />
670 </manifest>)EOF";
Adam Lesinskic6284372017-12-04 13:46:23 -0800671 EXPECT_THAT(Verify(input), IsNull());
Adam Lesinski86d67df2017-01-31 13:47:27 -0800672
673 input = R"EOF(
674 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
675 package="android">
676 <uses-feature />
677 </manifest>)EOF";
Adam Lesinskic6284372017-12-04 13:46:23 -0800678 EXPECT_THAT(Verify(input), IsNull());
Adam Lesinski86d67df2017-01-31 13:47:27 -0800679
680 input = R"EOF(
681 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
682 package="android">
683 <feature-group>
684 <uses-feature android:name="feature" android:glEsVersion="1" />
685 </feature-group>
686 </manifest>)EOF";
Adam Lesinskic6284372017-12-04 13:46:23 -0800687 EXPECT_THAT(Verify(input), IsNull());
Adam Lesinski86d67df2017-01-31 13:47:27 -0800688
689 input = R"EOF(
690 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
691 package="android">
692 <feature-group>
693 <uses-feature />
694 </feature-group>
695 </manifest>)EOF";
Adam Lesinskic6284372017-12-04 13:46:23 -0800696 EXPECT_THAT(Verify(input), IsNull());
Adam Lesinski86d67df2017-01-31 13:47:27 -0800697}
698
Ryan Mitchelle5b38a62018-03-23 13:35:00 -0700699TEST_F(ManifestFixerTest, ApplicationInjectDebuggable) {
700 ManifestFixerOptions options;
701 options.debug_mode = true;
702
703 std::string no_d = R"(
704 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
705 package="android">
706 <application>
707 </application>
708 </manifest>)";
709
710 std::string false_d = R"(
711 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
712 package="android">
713 <application android:debuggable="false">
714 </application>
715 </manifest>)";
716
717 std::string true_d = R"(
718 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
719 package="android">
720 <application android:debuggable="true">
721 </application>
722 </manifest>)";
723
724 // Inject the debuggable attribute when the attribute is not present and the
725 // flag is present
726 std::unique_ptr<xml::XmlResource> manifest = VerifyWithOptions(no_d, options);
727 EXPECT_THAT(manifest->root.get()->FindChildWithAttribute(
728 {}, "application", xml::kSchemaAndroid, "debuggable", "true"), NotNull());
729
730 // Set the debuggable flag to true if the attribute is false and the flag is
731 // present
732 manifest = VerifyWithOptions(false_d, options);
733 EXPECT_THAT(manifest->root.get()->FindChildWithAttribute(
734 {}, "application", xml::kSchemaAndroid, "debuggable", "true"), NotNull());
735
736 // Keep debuggable flag true if the attribute is true and the flag is present
737 manifest = VerifyWithOptions(true_d, options);
738 EXPECT_THAT(manifest->root.get()->FindChildWithAttribute(
739 {}, "application", xml::kSchemaAndroid, "debuggable", "true"), NotNull());
740
741 // Do not inject the debuggable attribute when the attribute is not present
742 // and the flag is not present
743 manifest = Verify(no_d);
744 EXPECT_THAT(manifest->root.get()->FindChildWithAttribute(
745 {}, "application", xml::kSchemaAndroid, "debuggable", "true"), IsNull());
746
747 // Do not set the debuggable flag to true if the attribute is false and the
748 // flag is not present
749 manifest = Verify(false_d);
750 EXPECT_THAT(manifest->root.get()->FindChildWithAttribute(
751 {}, "application", xml::kSchemaAndroid, "debuggable", "true"), IsNull());
752
753 // Keep debuggable flag true if the attribute is true and the flag is not
754 // present
755 manifest = Verify(true_d);
756 EXPECT_THAT(manifest->root.get()->FindChildWithAttribute(
757 {}, "application", xml::kSchemaAndroid, "debuggable", "true"), NotNull());
758}
759
Chris Craik335b5652019-04-04 12:46:47 -0700760TEST_F(ManifestFixerTest, ApplicationProfileable) {
761 std::string shell = R"(
762 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
763 package="android">
764 <application>
765 <profileable android:shell="true"/>
766 </application>
767 </manifest>)";
768 EXPECT_THAT(Verify(shell), NotNull());
769 std::string noshell = R"(
770 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
771 package="android">
772 <application>
773 <profileable/>
774 </application>
775 </manifest>)";
776 EXPECT_THAT(Verify(noshell), NotNull());
777}
Ryan Mitchelle5b38a62018-03-23 13:35:00 -0700778
Adam Lesinski63699b12017-05-08 18:36:33 -0700779TEST_F(ManifestFixerTest, IgnoreNamespacedElements) {
780 std::string input = R"EOF(
781 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
782 package="android">
783 <special:tag whoo="true" xmlns:special="http://google.com" />
784 </manifest>)EOF";
Adam Lesinskic6284372017-12-04 13:46:23 -0800785 EXPECT_THAT(Verify(input), NotNull());
Adam Lesinski63699b12017-05-08 18:36:33 -0700786}
787
788TEST_F(ManifestFixerTest, DoNotIgnoreNonNamespacedElements) {
789 std::string input = R"EOF(
790 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
791 package="android">
792 <tag whoo="true" />
793 </manifest>)EOF";
Adam Lesinskic6284372017-12-04 13:46:23 -0800794 EXPECT_THAT(Verify(input), IsNull());
Adam Lesinski63699b12017-05-08 18:36:33 -0700795}
796
Adam Lesinski87f1e0f2017-06-27 16:21:58 -0700797TEST_F(ManifestFixerTest, SupportKeySets) {
798 std::string input = R"(
799 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
800 package="android">
801 <key-sets>
802 <key-set android:name="old-set">
803 <public-key android:name="old-key" android:value="some+old+key" />
804 </key-set>
805 <key-set android:name="new-set">
806 <public-key android:name="new-key" android:value="some+new+key" />
807 </key-set>
808 <upgrade-key-set android:name="old-set" />
809 <upgrade-key-set android:name="new-set" />
810 </key-sets>
811 </manifest>)";
812 EXPECT_THAT(Verify(input), NotNull());
813}
814
Adam Lesinskic6284372017-12-04 13:46:23 -0800815TEST_F(ManifestFixerTest, InsertCompileSdkVersions) {
Donald Chai6e497352019-05-19 21:07:50 -0700816 std::string input = R"(<manifest package="com.pkg" />)";
Adam Lesinskic6284372017-12-04 13:46:23 -0800817 ManifestFixerOptions options;
818 options.compile_sdk_version = {"28"};
819 options.compile_sdk_version_codename = {"P"};
820
821 std::unique_ptr<xml::XmlResource> manifest = VerifyWithOptions(input, options);
822 ASSERT_THAT(manifest, NotNull());
823
Donald Chai6e497352019-05-19 21:07:50 -0700824 // There should be a declaration of kSchemaAndroid, even when the input
825 // didn't have one.
826 EXPECT_EQ(manifest->root->namespace_decls.size(), 1);
827 EXPECT_EQ(manifest->root->namespace_decls[0].prefix, "android");
828 EXPECT_EQ(manifest->root->namespace_decls[0].uri, xml::kSchemaAndroid);
829
Adam Lesinskic6284372017-12-04 13:46:23 -0800830 xml::Attribute* attr = manifest->root->FindAttribute(xml::kSchemaAndroid, "compileSdkVersion");
831 ASSERT_THAT(attr, NotNull());
832 EXPECT_THAT(attr->value, StrEq("28"));
833
834 attr = manifest->root->FindAttribute(xml::kSchemaAndroid, "compileSdkVersionCodename");
835 ASSERT_THAT(attr, NotNull());
836 EXPECT_THAT(attr->value, StrEq("P"));
Ryan Mitchellaada89c2019-02-12 08:06:26 -0800837
838 attr = manifest->root->FindAttribute("", "platformBuildVersionCode");
839 ASSERT_THAT(attr, NotNull());
840 EXPECT_THAT(attr->value, StrEq("28"));
841
842 attr = manifest->root->FindAttribute("", "platformBuildVersionName");
843 ASSERT_THAT(attr, NotNull());
844 EXPECT_THAT(attr->value, StrEq("P"));
845}
846
847TEST_F(ManifestFixerTest, OverrideCompileSdkVersions) {
848 std::string input = R"(
849 <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="android"
850 compileSdkVersion="27" compileSdkVersionCodename="O"
851 platformBuildVersionCode="27" platformBuildVersionName="O"/>)";
852 ManifestFixerOptions options;
853 options.compile_sdk_version = {"28"};
854 options.compile_sdk_version_codename = {"P"};
855
856 std::unique_ptr<xml::XmlResource> manifest = VerifyWithOptions(input, options);
857 ASSERT_THAT(manifest, NotNull());
858
859 xml::Attribute* attr = manifest->root->FindAttribute(xml::kSchemaAndroid, "compileSdkVersion");
860 ASSERT_THAT(attr, NotNull());
861 EXPECT_THAT(attr->value, StrEq("28"));
862
863 attr = manifest->root->FindAttribute(xml::kSchemaAndroid, "compileSdkVersionCodename");
864 ASSERT_THAT(attr, NotNull());
865 EXPECT_THAT(attr->value, StrEq("P"));
866
867 attr = manifest->root->FindAttribute("", "platformBuildVersionCode");
868 ASSERT_THAT(attr, NotNull());
869 EXPECT_THAT(attr->value, StrEq("28"));
870
871 attr = manifest->root->FindAttribute("", "platformBuildVersionName");
872 ASSERT_THAT(attr, NotNull());
873 EXPECT_THAT(attr->value, StrEq("P"));
Adam Lesinskic6284372017-12-04 13:46:23 -0800874}
875
Donald Chai6e497352019-05-19 21:07:50 -0700876TEST_F(ManifestFixerTest, AndroidPrefixAlreadyUsed) {
877 std::string input =
878 R"(<manifest package="com.pkg"
879 xmlns:android="http://schemas.android.com/apk/prv/res/android"
880 android:private_attr="foo" />)";
881 ManifestFixerOptions options;
882 options.compile_sdk_version = {"28"};
883 options.compile_sdk_version_codename = {"P"};
884
885 std::unique_ptr<xml::XmlResource> manifest = VerifyWithOptions(input, options);
886 ASSERT_THAT(manifest, NotNull());
887
888 // Make sure that we don't redefine "android".
889 EXPECT_EQ(manifest->root->namespace_decls.size(), 2);
890 EXPECT_EQ(manifest->root->namespace_decls[0].prefix, "android");
891 EXPECT_EQ(manifest->root->namespace_decls[0].uri,
892 "http://schemas.android.com/apk/prv/res/android");
893 EXPECT_EQ(manifest->root->namespace_decls[1].prefix, "android0");
894 EXPECT_EQ(manifest->root->namespace_decls[1].uri, xml::kSchemaAndroid);
895}
896
Izabela Orlowskaad9e1322017-12-19 16:22:42 +0000897TEST_F(ManifestFixerTest, UnexpectedElementsInManifest) {
898 std::string input = R"(
899 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
900 package="android">
901 <beep/>
902 </manifest>)";
903 ManifestFixerOptions options;
904 options.warn_validation = true;
905
906 // Unexpected element should result in a warning if the flag is set to 'true'.
907 std::unique_ptr<xml::XmlResource> manifest = VerifyWithOptions(input, options);
908 ASSERT_THAT(manifest, NotNull());
909
910 // Unexpected element should result in an error if the flag is set to 'false'.
911 options.warn_validation = false;
912 manifest = VerifyWithOptions(input, options);
913 ASSERT_THAT(manifest, IsNull());
914
915 // By default the flag should be set to 'false'.
916 manifest = Verify(input);
917 ASSERT_THAT(manifest, IsNull());
918}
919
Adam Lesinskifca5e422017-12-20 15:03:36 -0800920TEST_F(ManifestFixerTest, UsesLibraryMustHaveNonEmptyName) {
921 std::string input = R"(
922 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
923 package="android">
924 <application>
925 <uses-library android:name="" />
926 </application>
927 </manifest>)";
928 EXPECT_THAT(Verify(input), IsNull());
929
930 input = R"(
931 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
932 package="android">
933 <application>
934 <uses-library />
935 </application>
936 </manifest>)";
937 EXPECT_THAT(Verify(input), IsNull());
938
939 input = R"(
940 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
941 package="android">
942 <application>
943 <uses-library android:name="blahhh" />
944 </application>
945 </manifest>)";
946 EXPECT_THAT(Verify(input), NotNull());
947}
948
Todd Kennedyce3e1292020-10-29 17:14:24 -0700949TEST_F(ManifestFixerTest, ApplicationPropertyAttributeRequired) {
950 std::string input = R"(
951 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
952 package="android">
953 <application>
954 <property android:name="" />
955 </application>
956 </manifest>)";
957 EXPECT_THAT(Verify(input), IsNull());
958}
959
960TEST_F(ManifestFixerTest, ApplicationPropertyOnlyOneAttributeDefined) {
961 std::string input = R"(
962 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
963 package="android">
964 <application>
965 <property android:name="" android:value="" android:resource="" />
966 </application>
967 </manifest>)";
968 EXPECT_THAT(Verify(input), IsNull());
969
970 input = R"(
971 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
972 package="android">
973 <application>
974 <property android:name="" android:resource="" />
975 </application>
976 </manifest>)";
977 EXPECT_THAT(Verify(input), NotNull());
978
979 input = R"(
980 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
981 package="android">
982 <application>
983 <property android:name="" android:value="" />
984 </application>
985 </manifest>)";
986 EXPECT_THAT(Verify(input), NotNull());
987}
988
989TEST_F(ManifestFixerTest, ComponentPropertyOnlyOneAttributeDefined) {
990 std::string input = R"(
991 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
992 package="android">
993 <application>
994 <activity android:name=".MyActivity">
995 <property android:name="" android:value="" android:resource="" />
996 </activity>
997 </application>
998 </manifest>)";
999 EXPECT_THAT(Verify(input), IsNull());
1000
1001 input = R"(
1002 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
1003 package="android">
1004 <application>
1005 <activity android:name=".MyActivity">
1006 <property android:name="" android:value="" />
1007 </activity>
1008 </application>
1009 </manifest>)";
1010 EXPECT_THAT(Verify(input), NotNull());
1011
1012 input = R"(
1013 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
1014 package="android">
1015 <application>
1016 <activity android:name=".MyActivity">
1017 <property android:name="" android:resource="" />
1018 </activity>
1019 </application>
1020 </manifest>)";
1021 EXPECT_THAT(Verify(input), NotNull());
1022}
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001023} // namespace aapt