blob: 8d1a647b494d8e00fed60425fcc594969a8f3a2b [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
Jeremy Meyer6b05b7d2022-09-30 22:22:24 +0000354TEST_F(ManifestFixerTest, AddOverlayCategory) {
355 ManifestFixerOptions options;
356 options.rename_overlay_category = std::string("category");
357
358 std::unique_ptr<xml::XmlResource> doc = VerifyWithOptions(R"EOF(
359 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
360 package="android">
361 <overlay android:targetName="Customization" android:targetPackage="android" />
362 </manifest>)EOF",
363 options);
364 ASSERT_THAT(doc, NotNull());
365
366 xml::Element* manifest_el = doc->root.get();
367 ASSERT_THAT(manifest_el, NotNull());
368
369 xml::Element* overlay_el = manifest_el->FindChild({}, "overlay");
370 ASSERT_THAT(overlay_el, NotNull());
371
372 xml::Attribute* attr = overlay_el->FindAttribute(xml::kSchemaAndroid, "category");
373 ASSERT_THAT(attr, NotNull());
374 EXPECT_THAT(attr->value, StrEq("category"));
375}
376
377TEST_F(ManifestFixerTest, OverrideOverlayCategory) {
378 ManifestFixerOptions options;
379 options.rename_overlay_category = std::string("category");
380
381 std::unique_ptr<xml::XmlResource> doc = VerifyWithOptions(R"EOF(
382 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
383 package="android">
384 <overlay android:targetName="Customization"
385 android:targetPackage="android"
386 android:category="yrogetac"/>
387 </manifest>)EOF",
388 options);
389 ASSERT_THAT(doc, NotNull());
390
391 xml::Element* manifest_el = doc->root.get();
392 ASSERT_THAT(manifest_el, NotNull());
393
394 xml::Element* overlay_el = manifest_el->FindChild({}, "overlay");
395 ASSERT_THAT(overlay_el, NotNull());
396
397 xml::Attribute* attr = overlay_el->FindAttribute(xml::kSchemaAndroid, "category");
398 ASSERT_THAT(attr, NotNull());
399 EXPECT_THAT(attr->value, StrEq("category"));
400}
401
Adam Lesinski52364f72016-01-11 13:10:24 -0800402TEST_F(ManifestFixerTest, UseDefaultVersionNameAndCode) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700403 ManifestFixerOptions options;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700404 options.version_name_default = std::string("Beta");
405 options.version_code_default = std::string("0x10000000");
Ryan Mitchell704090e2018-07-31 14:59:25 -0700406 options.version_code_major_default = std::string("0x20000000");
Adam Lesinski52364f72016-01-11 13:10:24 -0800407
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700408 std::unique_ptr<xml::XmlResource> doc = VerifyWithOptions(R"EOF(
Adam Lesinski52364f72016-01-11 13:10:24 -0800409 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700410 package="android" />)EOF",
411 options);
Adam Lesinskic6284372017-12-04 13:46:23 -0800412 ASSERT_THAT(doc, NotNull());
Adam Lesinski52364f72016-01-11 13:10:24 -0800413
Adam Lesinski6b372992017-08-09 10:54:23 -0700414 xml::Element* manifest_el = doc->root.get();
Adam Lesinskic6284372017-12-04 13:46:23 -0800415 ASSERT_THAT(manifest_el, NotNull());
Adam Lesinski52364f72016-01-11 13:10:24 -0800416
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700417 xml::Attribute* attr =
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700418 manifest_el->FindAttribute(xml::kSchemaAndroid, "versionName");
Adam Lesinskic6284372017-12-04 13:46:23 -0800419 ASSERT_THAT(attr, NotNull());
420 EXPECT_THAT(attr->value, StrEq("Beta"));
Adam Lesinski52364f72016-01-11 13:10:24 -0800421
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700422 attr = manifest_el->FindAttribute(xml::kSchemaAndroid, "versionCode");
Adam Lesinskic6284372017-12-04 13:46:23 -0800423 ASSERT_THAT(attr, NotNull());
424 EXPECT_THAT(attr->value, StrEq("0x10000000"));
Ryan Mitchell704090e2018-07-31 14:59:25 -0700425
426 attr = manifest_el->FindAttribute(xml::kSchemaAndroid, "versionCodeMajor");
427 ASSERT_THAT(attr, NotNull());
428 EXPECT_THAT(attr->value, StrEq("0x20000000"));
Adam Lesinski52364f72016-01-11 13:10:24 -0800429}
430
Colin Crossdcd58c42018-05-25 22:46:35 -0700431TEST_F(ManifestFixerTest, DontUseDefaultVersionNameAndCode) {
Ryan Mitchell704090e2018-07-31 14:59:25 -0700432 ManifestFixerOptions options;
433 options.version_name_default = std::string("Beta");
434 options.version_code_default = std::string("0x10000000");
435 options.version_code_major_default = std::string("0x20000000");
Colin Crossdcd58c42018-05-25 22:46:35 -0700436
Ryan Mitchell704090e2018-07-31 14:59:25 -0700437 std::unique_ptr<xml::XmlResource> doc = VerifyWithOptions(R"EOF(
438 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
439 package="android"
440 android:versionCode="0x00000001"
441 android:versionCodeMajor="0x00000002"
442 android:versionName="Alpha" />)EOF",
443 options);
444 ASSERT_THAT(doc, NotNull());
Colin Crossdcd58c42018-05-25 22:46:35 -0700445
Ryan Mitchell704090e2018-07-31 14:59:25 -0700446 xml::Element* manifest_el = doc->root.get();
447 ASSERT_THAT(manifest_el, NotNull());
Colin Crossdcd58c42018-05-25 22:46:35 -0700448
Ryan Mitchell704090e2018-07-31 14:59:25 -0700449 xml::Attribute* attr =
450 manifest_el->FindAttribute(xml::kSchemaAndroid, "versionName");
451 ASSERT_THAT(attr, NotNull());
452 EXPECT_THAT(attr->value, StrEq("Alpha"));
Colin Crossdcd58c42018-05-25 22:46:35 -0700453
Ryan Mitchell704090e2018-07-31 14:59:25 -0700454 attr = manifest_el->FindAttribute(xml::kSchemaAndroid, "versionCode");
455 ASSERT_THAT(attr, NotNull());
456 EXPECT_THAT(attr->value, StrEq("0x00000001"));
457
458 attr = manifest_el->FindAttribute(xml::kSchemaAndroid, "versionCodeMajor");
459 ASSERT_THAT(attr, NotNull());
460 EXPECT_THAT(attr->value, StrEq("0x00000002"));
Colin Crossdcd58c42018-05-25 22:46:35 -0700461}
462
463TEST_F(ManifestFixerTest, ReplaceVersionNameAndCode) {
Ryan Mitchell704090e2018-07-31 14:59:25 -0700464 ManifestFixerOptions options;
465 options.replace_version = true;
466 options.version_name_default = std::string("Beta");
467 options.version_code_default = std::string("0x10000000");
468 options.version_code_major_default = std::string("0x20000000");
Colin Crossdcd58c42018-05-25 22:46:35 -0700469
Ryan Mitchell704090e2018-07-31 14:59:25 -0700470 std::unique_ptr<xml::XmlResource> doc = VerifyWithOptions(R"EOF(
471 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
472 package="android"
473 android:versionCode="0x00000001"
474 android:versionCodeMajor="0x00000002"
475 android:versionName="Alpha" />)EOF",
476 options);
477 ASSERT_THAT(doc, NotNull());
Colin Crossdcd58c42018-05-25 22:46:35 -0700478
Ryan Mitchell704090e2018-07-31 14:59:25 -0700479 xml::Element* manifest_el = doc->root.get();
480 ASSERT_THAT(manifest_el, NotNull());
Colin Crossdcd58c42018-05-25 22:46:35 -0700481
Ryan Mitchell704090e2018-07-31 14:59:25 -0700482 xml::Attribute* attr =
483 manifest_el->FindAttribute(xml::kSchemaAndroid, "versionName");
484 ASSERT_THAT(attr, NotNull());
485 EXPECT_THAT(attr->value, StrEq("Beta"));
Colin Crossdcd58c42018-05-25 22:46:35 -0700486
Ryan Mitchell704090e2018-07-31 14:59:25 -0700487 attr = manifest_el->FindAttribute(xml::kSchemaAndroid, "versionCode");
488 ASSERT_THAT(attr, NotNull());
489 EXPECT_THAT(attr->value, StrEq("0x10000000"));
490
491 attr = manifest_el->FindAttribute(xml::kSchemaAndroid, "versionCodeMajor");
492 ASSERT_THAT(attr, NotNull());
493 EXPECT_THAT(attr->value, StrEq("0x20000000"));
Colin Crossdcd58c42018-05-25 22:46:35 -0700494}
495
Rhed Jaob9ccb8a42020-11-30 21:42:16 +0800496TEST_F(ManifestFixerTest, UseDefaultRevisionCode) {
497 ManifestFixerOptions options;
498 options.revision_code_default = std::string("0x10000000");
499
500 std::unique_ptr<xml::XmlResource> doc = VerifyWithOptions(R"EOF(
501 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
502 package="android"
503 android:versionCode="0x00000001" />)EOF",
504 options);
505 ASSERT_THAT(doc, NotNull());
506
507 xml::Element* manifest_el = doc->root.get();
508 ASSERT_THAT(manifest_el, NotNull());
509
510 xml::Attribute* attr = manifest_el->FindAttribute(xml::kSchemaAndroid, "revisionCode");
511 ASSERT_THAT(attr, NotNull());
512 EXPECT_THAT(attr->value, StrEq("0x10000000"));
513}
514
515TEST_F(ManifestFixerTest, DontUseDefaultRevisionCode) {
516 ManifestFixerOptions options;
517 options.revision_code_default = std::string("0x10000000");
518
519 std::unique_ptr<xml::XmlResource> doc = VerifyWithOptions(R"EOF(
520 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
521 package="android"
522 android:versionCode="0x00000001"
523 android:revisionCode="0x00000002" />)EOF",
524 options);
525 ASSERT_THAT(doc, NotNull());
526
527 xml::Element* manifest_el = doc->root.get();
528 ASSERT_THAT(manifest_el, NotNull());
529
530 xml::Attribute* attr = manifest_el->FindAttribute(xml::kSchemaAndroid, "revisionCode");
531 ASSERT_THAT(attr, NotNull());
532 EXPECT_THAT(attr->value, StrEq("0x00000002"));
533}
534
535TEST_F(ManifestFixerTest, ReplaceRevisionCode) {
536 ManifestFixerOptions options;
537 options.replace_version = true;
538 options.revision_code_default = std::string("0x10000000");
539
540 std::unique_ptr<xml::XmlResource> doc = VerifyWithOptions(R"EOF(
541 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
542 package="android"
543 android:versionCode="0x00000001"
544 android:revisionCode="0x00000002" />)EOF",
545 options);
546 ASSERT_THAT(doc, NotNull());
547
548 xml::Element* manifest_el = doc->root.get();
549 ASSERT_THAT(manifest_el, NotNull());
550
551 xml::Attribute* attr = manifest_el->FindAttribute(xml::kSchemaAndroid, "revisionCode");
552 ASSERT_THAT(attr, NotNull());
553 EXPECT_THAT(attr->value, StrEq("0x10000000"));
554}
555
Colin Crossdcd58c42018-05-25 22:46:35 -0700556TEST_F(ManifestFixerTest, ReplaceVersionName) {
Ryan Mitchell704090e2018-07-31 14:59:25 -0700557 ManifestFixerOptions options;
558 options.replace_version = true;
559 options.version_name_default = std::string("Beta");
Colin Crossdcd58c42018-05-25 22:46:35 -0700560
Colin Crossdcd58c42018-05-25 22:46:35 -0700561
Ryan Mitchell704090e2018-07-31 14:59:25 -0700562 std::unique_ptr<xml::XmlResource> doc = VerifyWithOptions(R"EOF(
563 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
564 package="android"
565 android:versionCode="0x00000001"
566 android:versionCodeMajor="0x00000002"
567 android:versionName="Alpha" />)EOF",
568 options);
569 ASSERT_THAT(doc, NotNull());
Colin Crossdcd58c42018-05-25 22:46:35 -0700570
Ryan Mitchell704090e2018-07-31 14:59:25 -0700571 xml::Element* manifest_el = doc->root.get();
572 ASSERT_THAT(manifest_el, NotNull());
Colin Crossdcd58c42018-05-25 22:46:35 -0700573
Ryan Mitchell704090e2018-07-31 14:59:25 -0700574 xml::Attribute* attr =
575 manifest_el->FindAttribute(xml::kSchemaAndroid, "versionName");
576 ASSERT_THAT(attr, NotNull());
577 EXPECT_THAT(attr->value, StrEq("Beta"));
578
579 attr = manifest_el->FindAttribute(xml::kSchemaAndroid, "versionCode");
580 ASSERT_THAT(attr, NotNull());
581 EXPECT_THAT(attr->value, StrEq("0x00000001"));
582
583 attr = manifest_el->FindAttribute(xml::kSchemaAndroid, "versionCodeMajor");
584 ASSERT_THAT(attr, NotNull());
585 EXPECT_THAT(attr->value, StrEq("0x00000002"));
Colin Crossdcd58c42018-05-25 22:46:35 -0700586}
587
588TEST_F(ManifestFixerTest, ReplaceVersionCode) {
Ryan Mitchell704090e2018-07-31 14:59:25 -0700589 ManifestFixerOptions options;
590 options.replace_version = true;
591 options.version_code_default = std::string("0x10000000");
Colin Crossdcd58c42018-05-25 22:46:35 -0700592
Ryan Mitchell704090e2018-07-31 14:59:25 -0700593 std::unique_ptr<xml::XmlResource> doc = VerifyWithOptions(R"EOF(
594 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
595 package="android"
596 android:versionCode="0x00000001"
597 android:versionCodeMajor="0x00000002"
598 android:versionName="Alpha" />)EOF",
599 options);
600 ASSERT_THAT(doc, NotNull());
601
602 xml::Element* manifest_el = doc->root.get();
603 ASSERT_THAT(manifest_el, NotNull());
604
605 xml::Attribute* attr =
606 manifest_el->FindAttribute(xml::kSchemaAndroid, "versionName");
607 ASSERT_THAT(attr, NotNull());
608 EXPECT_THAT(attr->value, StrEq("Alpha"));
609
610 attr = manifest_el->FindAttribute(xml::kSchemaAndroid, "versionCode");
611 ASSERT_THAT(attr, NotNull());
612 EXPECT_THAT(attr->value, StrEq("0x10000000"));
613
614 attr = manifest_el->FindAttribute(xml::kSchemaAndroid, "versionCodeMajor");
615 ASSERT_THAT(attr, NotNull());
616 EXPECT_THAT(attr->value, StrEq("0x00000002"));
617}
618
619TEST_F(ManifestFixerTest, ReplaceVersionCodeMajor) {
620 ManifestFixerOptions options;
621 options.replace_version = true;
622 options.version_code_major_default = std::string("0x20000000");
623
624 std::unique_ptr<xml::XmlResource> doc = VerifyWithOptions(R"EOF(
Colin Crossdcd58c42018-05-25 22:46:35 -0700625 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
Ryan Mitchell704090e2018-07-31 14:59:25 -0700626 package="android"
627 android:versionCode="0x00000001"
628 android:versionCodeMajor="0x00000002"
629 android:versionName="Alpha" />)EOF",
630 options);
631 ASSERT_THAT(doc, NotNull());
Colin Crossdcd58c42018-05-25 22:46:35 -0700632
Ryan Mitchell704090e2018-07-31 14:59:25 -0700633 xml::Element* manifest_el = doc->root.get();
634 ASSERT_THAT(manifest_el, NotNull());
Colin Crossdcd58c42018-05-25 22:46:35 -0700635
Ryan Mitchell704090e2018-07-31 14:59:25 -0700636 xml::Attribute* attr =
637 manifest_el->FindAttribute(xml::kSchemaAndroid, "versionName");
638 ASSERT_THAT(attr, NotNull());
639 EXPECT_THAT(attr->value, StrEq("Alpha"));
Colin Crossdcd58c42018-05-25 22:46:35 -0700640
Ryan Mitchell704090e2018-07-31 14:59:25 -0700641 attr = manifest_el->FindAttribute(xml::kSchemaAndroid, "versionCode");
642 ASSERT_THAT(attr, NotNull());
643 EXPECT_THAT(attr->value, StrEq("0x00000001"));
644
645 attr = manifest_el->FindAttribute(xml::kSchemaAndroid, "versionCodeMajor");
646 ASSERT_THAT(attr, NotNull());
647 EXPECT_THAT(attr->value, StrEq("0x20000000"));
Colin Crossdcd58c42018-05-25 22:46:35 -0700648}
649
650TEST_F(ManifestFixerTest, DontReplaceVersionNameOrCode) {
Ryan Mitchell704090e2018-07-31 14:59:25 -0700651 ManifestFixerOptions options;
652 options.replace_version = true;
Colin Crossdcd58c42018-05-25 22:46:35 -0700653
Ryan Mitchell704090e2018-07-31 14:59:25 -0700654 std::unique_ptr<xml::XmlResource> doc = VerifyWithOptions(R"EOF(
655 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
656 package="android"
657 android:versionCode="0x00000001"
658 android:versionCodeMajor="0x00000002"
659 android:versionName="Alpha" />)EOF",
660 options);
661 ASSERT_THAT(doc, NotNull());
Colin Crossdcd58c42018-05-25 22:46:35 -0700662
Ryan Mitchell704090e2018-07-31 14:59:25 -0700663 xml::Element* manifest_el = doc->root.get();
664 ASSERT_THAT(manifest_el, NotNull());
Colin Crossdcd58c42018-05-25 22:46:35 -0700665
Ryan Mitchell704090e2018-07-31 14:59:25 -0700666 xml::Attribute* attr =
667 manifest_el->FindAttribute(xml::kSchemaAndroid, "versionName");
668 ASSERT_THAT(attr, NotNull());
669 EXPECT_THAT(attr->value, StrEq("Alpha"));
Colin Crossdcd58c42018-05-25 22:46:35 -0700670
Ryan Mitchell704090e2018-07-31 14:59:25 -0700671 attr = manifest_el->FindAttribute(xml::kSchemaAndroid, "versionCode");
672 ASSERT_THAT(attr, NotNull());
673 EXPECT_THAT(attr->value, StrEq("0x00000001"));
674
675 attr = manifest_el->FindAttribute(xml::kSchemaAndroid, "versionCodeMajor");
676 ASSERT_THAT(attr, NotNull());
677 EXPECT_THAT(attr->value, StrEq("0x00000002"));
Colin Crossdcd58c42018-05-25 22:46:35 -0700678}
679
Adam Lesinski6b17d2c2016-08-10 11:37:06 -0700680TEST_F(ManifestFixerTest, EnsureManifestAttributesAreTyped) {
Adam Lesinskic6284372017-12-04 13:46:23 -0800681 EXPECT_THAT(Verify("<manifest package=\"android\" coreApp=\"hello\" />"), IsNull());
682 EXPECT_THAT(Verify("<manifest package=\"android\" coreApp=\"1dp\" />"), IsNull());
Adam Lesinski6b17d2c2016-08-10 11:37:06 -0700683
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700684 std::unique_ptr<xml::XmlResource> doc =
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700685 Verify("<manifest package=\"android\" coreApp=\"true\" />");
Adam Lesinskic6284372017-12-04 13:46:23 -0800686 ASSERT_THAT(doc, NotNull());
Adam Lesinski6b17d2c2016-08-10 11:37:06 -0700687
Adam Lesinski6b372992017-08-09 10:54:23 -0700688 xml::Element* el = doc->root.get();
Adam Lesinskic6284372017-12-04 13:46:23 -0800689 ASSERT_THAT(el, NotNull());
Adam Lesinski6b17d2c2016-08-10 11:37:06 -0700690
Adam Lesinskic6284372017-12-04 13:46:23 -0800691 EXPECT_THAT(el->name, StrEq("manifest"));
Adam Lesinski6b17d2c2016-08-10 11:37:06 -0700692
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700693 xml::Attribute* attr = el->FindAttribute("", "coreApp");
Adam Lesinskic6284372017-12-04 13:46:23 -0800694 ASSERT_THAT(attr, NotNull());
Adam Lesinski6b17d2c2016-08-10 11:37:06 -0700695
Adam Lesinskic6284372017-12-04 13:46:23 -0800696 EXPECT_THAT(attr->compiled_value, NotNull());
697 EXPECT_THAT(ValueCast<BinaryPrimitive>(attr->compiled_value.get()), NotNull());
Adam Lesinski6b17d2c2016-08-10 11:37:06 -0700698}
699
Adam Lesinski86d67df2017-01-31 13:47:27 -0800700TEST_F(ManifestFixerTest, UsesFeatureMustHaveNameOrGlEsVersion) {
701 std::string input = R"EOF(
702 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
703 package="android">
704 <uses-feature android:name="feature" />
705 <uses-feature android:glEsVersion="1" />
706 <feature-group />
707 <feature-group>
708 <uses-feature android:name="feature_in_group" />
709 <uses-feature android:glEsVersion="2" />
710 </feature-group>
711 </manifest>)EOF";
Adam Lesinskic6284372017-12-04 13:46:23 -0800712 EXPECT_THAT(Verify(input), NotNull());
Adam Lesinski86d67df2017-01-31 13:47:27 -0800713
714 input = R"EOF(
715 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
716 package="android">
717 <uses-feature android:name="feature" android:glEsVersion="1" />
718 </manifest>)EOF";
Adam Lesinskic6284372017-12-04 13:46:23 -0800719 EXPECT_THAT(Verify(input), IsNull());
Adam Lesinski86d67df2017-01-31 13:47:27 -0800720
721 input = R"EOF(
722 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
723 package="android">
724 <uses-feature />
725 </manifest>)EOF";
Adam Lesinskic6284372017-12-04 13:46:23 -0800726 EXPECT_THAT(Verify(input), IsNull());
Adam Lesinski86d67df2017-01-31 13:47:27 -0800727
728 input = R"EOF(
729 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
730 package="android">
731 <feature-group>
732 <uses-feature android:name="feature" android:glEsVersion="1" />
733 </feature-group>
734 </manifest>)EOF";
Adam Lesinskic6284372017-12-04 13:46:23 -0800735 EXPECT_THAT(Verify(input), IsNull());
Adam Lesinski86d67df2017-01-31 13:47:27 -0800736
737 input = R"EOF(
738 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
739 package="android">
740 <feature-group>
741 <uses-feature />
742 </feature-group>
743 </manifest>)EOF";
Adam Lesinskic6284372017-12-04 13:46:23 -0800744 EXPECT_THAT(Verify(input), IsNull());
Adam Lesinski86d67df2017-01-31 13:47:27 -0800745}
746
Ryan Mitchelle5b38a62018-03-23 13:35:00 -0700747TEST_F(ManifestFixerTest, ApplicationInjectDebuggable) {
748 ManifestFixerOptions options;
749 options.debug_mode = true;
750
751 std::string no_d = R"(
752 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
753 package="android">
754 <application>
755 </application>
756 </manifest>)";
757
758 std::string false_d = R"(
759 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
760 package="android">
761 <application android:debuggable="false">
762 </application>
763 </manifest>)";
764
765 std::string true_d = R"(
766 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
767 package="android">
768 <application android:debuggable="true">
769 </application>
770 </manifest>)";
771
772 // Inject the debuggable attribute when the attribute is not present and the
773 // flag is present
774 std::unique_ptr<xml::XmlResource> manifest = VerifyWithOptions(no_d, options);
775 EXPECT_THAT(manifest->root.get()->FindChildWithAttribute(
776 {}, "application", xml::kSchemaAndroid, "debuggable", "true"), NotNull());
777
778 // Set the debuggable flag to true if the attribute is false and the flag is
779 // present
780 manifest = VerifyWithOptions(false_d, options);
781 EXPECT_THAT(manifest->root.get()->FindChildWithAttribute(
782 {}, "application", xml::kSchemaAndroid, "debuggable", "true"), NotNull());
783
784 // Keep debuggable flag true if the attribute is true and the flag is present
785 manifest = VerifyWithOptions(true_d, options);
786 EXPECT_THAT(manifest->root.get()->FindChildWithAttribute(
787 {}, "application", xml::kSchemaAndroid, "debuggable", "true"), NotNull());
788
789 // Do not inject the debuggable attribute when the attribute is not present
790 // and the flag is not present
791 manifest = Verify(no_d);
792 EXPECT_THAT(manifest->root.get()->FindChildWithAttribute(
793 {}, "application", xml::kSchemaAndroid, "debuggable", "true"), IsNull());
794
795 // Do not set the debuggable flag to true if the attribute is false and the
796 // flag is not present
797 manifest = Verify(false_d);
798 EXPECT_THAT(manifest->root.get()->FindChildWithAttribute(
799 {}, "application", xml::kSchemaAndroid, "debuggable", "true"), IsNull());
800
801 // Keep debuggable flag true if the attribute is true and the flag is not
802 // present
803 manifest = Verify(true_d);
804 EXPECT_THAT(manifest->root.get()->FindChildWithAttribute(
805 {}, "application", xml::kSchemaAndroid, "debuggable", "true"), NotNull());
806}
807
Chris Craik335b5652019-04-04 12:46:47 -0700808TEST_F(ManifestFixerTest, ApplicationProfileable) {
809 std::string shell = R"(
810 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
811 package="android">
812 <application>
813 <profileable android:shell="true"/>
814 </application>
815 </manifest>)";
816 EXPECT_THAT(Verify(shell), NotNull());
817 std::string noshell = R"(
818 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
819 package="android">
820 <application>
821 <profileable/>
822 </application>
823 </manifest>)";
824 EXPECT_THAT(Verify(noshell), NotNull());
825}
Ryan Mitchelle5b38a62018-03-23 13:35:00 -0700826
Adam Lesinski63699b12017-05-08 18:36:33 -0700827TEST_F(ManifestFixerTest, IgnoreNamespacedElements) {
828 std::string input = R"EOF(
829 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
830 package="android">
831 <special:tag whoo="true" xmlns:special="http://google.com" />
832 </manifest>)EOF";
Adam Lesinskic6284372017-12-04 13:46:23 -0800833 EXPECT_THAT(Verify(input), NotNull());
Adam Lesinski63699b12017-05-08 18:36:33 -0700834}
835
836TEST_F(ManifestFixerTest, DoNotIgnoreNonNamespacedElements) {
837 std::string input = R"EOF(
838 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
839 package="android">
840 <tag whoo="true" />
841 </manifest>)EOF";
Adam Lesinskic6284372017-12-04 13:46:23 -0800842 EXPECT_THAT(Verify(input), IsNull());
Adam Lesinski63699b12017-05-08 18:36:33 -0700843}
844
Adam Lesinski87f1e0f2017-06-27 16:21:58 -0700845TEST_F(ManifestFixerTest, SupportKeySets) {
846 std::string input = R"(
847 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
848 package="android">
849 <key-sets>
850 <key-set android:name="old-set">
851 <public-key android:name="old-key" android:value="some+old+key" />
852 </key-set>
853 <key-set android:name="new-set">
854 <public-key android:name="new-key" android:value="some+new+key" />
855 </key-set>
856 <upgrade-key-set android:name="old-set" />
857 <upgrade-key-set android:name="new-set" />
858 </key-sets>
859 </manifest>)";
860 EXPECT_THAT(Verify(input), NotNull());
861}
862
Adam Lesinskic6284372017-12-04 13:46:23 -0800863TEST_F(ManifestFixerTest, InsertCompileSdkVersions) {
Donald Chai6e497352019-05-19 21:07:50 -0700864 std::string input = R"(<manifest package="com.pkg" />)";
Adam Lesinskic6284372017-12-04 13:46:23 -0800865 ManifestFixerOptions options;
866 options.compile_sdk_version = {"28"};
867 options.compile_sdk_version_codename = {"P"};
868
869 std::unique_ptr<xml::XmlResource> manifest = VerifyWithOptions(input, options);
870 ASSERT_THAT(manifest, NotNull());
871
Donald Chai6e497352019-05-19 21:07:50 -0700872 // There should be a declaration of kSchemaAndroid, even when the input
873 // didn't have one.
874 EXPECT_EQ(manifest->root->namespace_decls.size(), 1);
875 EXPECT_EQ(manifest->root->namespace_decls[0].prefix, "android");
876 EXPECT_EQ(manifest->root->namespace_decls[0].uri, xml::kSchemaAndroid);
877
Adam Lesinskic6284372017-12-04 13:46:23 -0800878 xml::Attribute* attr = manifest->root->FindAttribute(xml::kSchemaAndroid, "compileSdkVersion");
879 ASSERT_THAT(attr, NotNull());
880 EXPECT_THAT(attr->value, StrEq("28"));
881
882 attr = manifest->root->FindAttribute(xml::kSchemaAndroid, "compileSdkVersionCodename");
883 ASSERT_THAT(attr, NotNull());
884 EXPECT_THAT(attr->value, StrEq("P"));
Ryan Mitchellaada89c2019-02-12 08:06:26 -0800885
886 attr = manifest->root->FindAttribute("", "platformBuildVersionCode");
887 ASSERT_THAT(attr, NotNull());
888 EXPECT_THAT(attr->value, StrEq("28"));
889
890 attr = manifest->root->FindAttribute("", "platformBuildVersionName");
891 ASSERT_THAT(attr, NotNull());
892 EXPECT_THAT(attr->value, StrEq("P"));
893}
894
895TEST_F(ManifestFixerTest, OverrideCompileSdkVersions) {
896 std::string input = R"(
897 <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="android"
898 compileSdkVersion="27" compileSdkVersionCodename="O"
899 platformBuildVersionCode="27" platformBuildVersionName="O"/>)";
900 ManifestFixerOptions options;
901 options.compile_sdk_version = {"28"};
902 options.compile_sdk_version_codename = {"P"};
903
904 std::unique_ptr<xml::XmlResource> manifest = VerifyWithOptions(input, options);
905 ASSERT_THAT(manifest, NotNull());
906
907 xml::Attribute* attr = manifest->root->FindAttribute(xml::kSchemaAndroid, "compileSdkVersion");
908 ASSERT_THAT(attr, NotNull());
909 EXPECT_THAT(attr->value, StrEq("28"));
910
911 attr = manifest->root->FindAttribute(xml::kSchemaAndroid, "compileSdkVersionCodename");
912 ASSERT_THAT(attr, NotNull());
913 EXPECT_THAT(attr->value, StrEq("P"));
914
915 attr = manifest->root->FindAttribute("", "platformBuildVersionCode");
916 ASSERT_THAT(attr, NotNull());
917 EXPECT_THAT(attr->value, StrEq("28"));
918
919 attr = manifest->root->FindAttribute("", "platformBuildVersionName");
920 ASSERT_THAT(attr, NotNull());
921 EXPECT_THAT(attr->value, StrEq("P"));
Adam Lesinskic6284372017-12-04 13:46:23 -0800922}
923
Donald Chai6e497352019-05-19 21:07:50 -0700924TEST_F(ManifestFixerTest, AndroidPrefixAlreadyUsed) {
925 std::string input =
926 R"(<manifest package="com.pkg"
927 xmlns:android="http://schemas.android.com/apk/prv/res/android"
928 android:private_attr="foo" />)";
929 ManifestFixerOptions options;
930 options.compile_sdk_version = {"28"};
931 options.compile_sdk_version_codename = {"P"};
932
933 std::unique_ptr<xml::XmlResource> manifest = VerifyWithOptions(input, options);
934 ASSERT_THAT(manifest, NotNull());
935
936 // Make sure that we don't redefine "android".
937 EXPECT_EQ(manifest->root->namespace_decls.size(), 2);
938 EXPECT_EQ(manifest->root->namespace_decls[0].prefix, "android");
939 EXPECT_EQ(manifest->root->namespace_decls[0].uri,
940 "http://schemas.android.com/apk/prv/res/android");
941 EXPECT_EQ(manifest->root->namespace_decls[1].prefix, "android0");
942 EXPECT_EQ(manifest->root->namespace_decls[1].uri, xml::kSchemaAndroid);
943}
944
Izabela Orlowskaad9e1322017-12-19 16:22:42 +0000945TEST_F(ManifestFixerTest, UnexpectedElementsInManifest) {
946 std::string input = R"(
947 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
948 package="android">
949 <beep/>
950 </manifest>)";
951 ManifestFixerOptions options;
952 options.warn_validation = true;
953
954 // Unexpected element should result in a warning if the flag is set to 'true'.
955 std::unique_ptr<xml::XmlResource> manifest = VerifyWithOptions(input, options);
956 ASSERT_THAT(manifest, NotNull());
957
958 // Unexpected element should result in an error if the flag is set to 'false'.
959 options.warn_validation = false;
960 manifest = VerifyWithOptions(input, options);
961 ASSERT_THAT(manifest, IsNull());
962
963 // By default the flag should be set to 'false'.
964 manifest = Verify(input);
965 ASSERT_THAT(manifest, IsNull());
966}
967
Adam Lesinskifca5e422017-12-20 15:03:36 -0800968TEST_F(ManifestFixerTest, UsesLibraryMustHaveNonEmptyName) {
969 std::string input = R"(
970 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
971 package="android">
972 <application>
973 <uses-library android:name="" />
974 </application>
975 </manifest>)";
976 EXPECT_THAT(Verify(input), IsNull());
977
978 input = R"(
979 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
980 package="android">
981 <application>
982 <uses-library />
983 </application>
984 </manifest>)";
985 EXPECT_THAT(Verify(input), IsNull());
986
987 input = R"(
988 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
989 package="android">
990 <application>
991 <uses-library android:name="blahhh" />
992 </application>
993 </manifest>)";
994 EXPECT_THAT(Verify(input), NotNull());
995}
996
Todd Kennedyce3e1292020-10-29 17:14:24 -0700997TEST_F(ManifestFixerTest, ApplicationPropertyAttributeRequired) {
998 std::string input = R"(
999 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
1000 package="android">
1001 <application>
1002 <property android:name="" />
1003 </application>
1004 </manifest>)";
1005 EXPECT_THAT(Verify(input), IsNull());
1006}
1007
1008TEST_F(ManifestFixerTest, ApplicationPropertyOnlyOneAttributeDefined) {
1009 std::string input = R"(
1010 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
1011 package="android">
1012 <application>
1013 <property android:name="" android:value="" android:resource="" />
1014 </application>
1015 </manifest>)";
1016 EXPECT_THAT(Verify(input), IsNull());
1017
1018 input = R"(
1019 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
1020 package="android">
1021 <application>
1022 <property android:name="" android:resource="" />
1023 </application>
1024 </manifest>)";
1025 EXPECT_THAT(Verify(input), NotNull());
1026
1027 input = R"(
1028 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
1029 package="android">
1030 <application>
1031 <property android:name="" android:value="" />
1032 </application>
1033 </manifest>)";
1034 EXPECT_THAT(Verify(input), NotNull());
1035}
1036
1037TEST_F(ManifestFixerTest, ComponentPropertyOnlyOneAttributeDefined) {
1038 std::string input = R"(
1039 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
1040 package="android">
1041 <application>
1042 <activity android:name=".MyActivity">
1043 <property android:name="" android:value="" android:resource="" />
1044 </activity>
1045 </application>
1046 </manifest>)";
1047 EXPECT_THAT(Verify(input), IsNull());
1048
1049 input = R"(
1050 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
1051 package="android">
1052 <application>
1053 <activity android:name=".MyActivity">
1054 <property android:name="" android:value="" />
1055 </activity>
1056 </application>
1057 </manifest>)";
1058 EXPECT_THAT(Verify(input), NotNull());
1059
1060 input = R"(
1061 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
1062 package="android">
1063 <application>
1064 <activity android:name=".MyActivity">
1065 <property android:name="" android:resource="" />
1066 </activity>
1067 </application>
1068 </manifest>)";
1069 EXPECT_THAT(Verify(input), NotNull());
1070}
Brandon Liu3cff2552022-10-19 18:51:07 +00001071
1072TEST_F(ManifestFixerTest, IntentFilterActionMustHaveNonEmptyName) {
1073 std::string input = R"(
1074 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
1075 package="android">
1076 <application>
1077 <activity android:name=".MainActivity">
1078 <intent-filter>
1079 <action android:name="" />
1080 </intent-filter>
1081 </activity>
1082 </application>
1083 </manifest>)";
1084 EXPECT_THAT(Verify(input), IsNull());
1085
1086 input = R"(
1087 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
1088 package="android">
1089 <application>
1090 <activity android:name=".MainActivity">
1091 <intent-filter>
1092 <action />
1093 </intent-filter>
1094 </activity>
1095 </application>
1096 </manifest>)";
1097 EXPECT_THAT(Verify(input), IsNull());
1098
1099 input = R"(
1100 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
1101 package="android">
1102 <application>
1103 <activity android:name=".MainActivity">
1104 <intent-filter>
1105 <action android:name="android.intent.action.MAIN" />
1106 </intent-filter>
1107 </activity>
1108 </application>
1109 </manifest>)";
1110 EXPECT_THAT(Verify(input), NotNull());
1111}
1112
1113TEST_F(ManifestFixerTest, IntentFilterCategoryMustHaveNonEmptyName) {
1114 std::string input = R"(
1115 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
1116 package="android">
1117 <application>
1118 <activity android:name=".MainActivity">
1119 <intent-filter>
1120 <category android:name="" />
1121 </intent-filter>
1122 </activity>
1123 </application>
1124 </manifest>)";
1125 EXPECT_THAT(Verify(input), IsNull());
1126
1127 input = R"(
1128 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
1129 package="android">
1130 <application>
1131 <activity android:name=".MainActivity">
1132 <intent-filter>
1133 <category />
1134 </intent-filter>
1135 </activity>
1136 </application>
1137 </manifest>)";
1138 EXPECT_THAT(Verify(input), IsNull());
1139
1140 input = R"(
1141 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
1142 package="android">
1143 <application>
1144 <activity android:name=".MainActivity">
1145 <intent-filter>
1146 <category android:name="android.intent.category.LAUNCHER" />
1147 </intent-filter>
1148 </activity>
1149 </application>
1150 </manifest>)";
1151 EXPECT_THAT(Verify(input), NotNull());
1152}
1153
1154TEST_F(ManifestFixerTest, IntentFilterPathMustStartWithLeadingSlashOnDeepLinks) {
1155 // No DeepLink.
1156 std::string input = R"(
1157 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
1158 package="android">
1159 <application>
1160 <activity android:name=".MainActivity">
1161 <intent-filter>
1162 <data />
1163 </intent-filter>
1164 </activity>
1165 </application>
1166 </manifest>)";
1167 EXPECT_THAT(Verify(input), NotNull());
1168
1169 // No DeepLink, missing ACTION_VIEW.
1170 input = R"(
1171 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
1172 package="android">
1173 <application>
1174 <activity android:name=".MainActivity">
1175 <intent-filter>
1176 <category android:name="android.intent.category.DEFAULT" />
1177 <category android:name="android.intent.category.BROWSABLE" />
1178 <data android:scheme="http"
1179 android:host="www.example.com"
1180 android:pathPrefix="pathPattern" />
1181 </intent-filter>
1182 </activity>
1183 </application>
1184 </manifest>)";
1185 EXPECT_THAT(Verify(input), NotNull());
1186
1187 // DeepLink, missing DEFAULT category while DEFAULT is recommended but not required.
1188 input = R"(
1189 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
1190 package="android">
1191 <application>
1192 <activity android:name=".MainActivity">
1193 <intent-filter>
1194 <action android:name="android.intent.action.VIEW" />
1195 <category android:name="android.intent.category.BROWSABLE" />
1196 <data android:scheme="http"
1197 android:host="www.example.com"
1198 android:pathPrefix="pathPattern" />
1199 </intent-filter>
1200 </activity>
1201 </application>
1202 </manifest>)";
1203 EXPECT_THAT(Verify(input), IsNull());
1204
1205 // No DeepLink, missing BROWSABLE category.
1206 input = R"(
1207 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
1208 package="android">
1209 <application>
1210 <activity android:name=".MainActivity">
1211 <intent-filter>
1212 <action android:name="android.intent.action.VIEW" />
1213 <category android:name="android.intent.category.DEFAULT" />
1214 <data android:scheme="http"
1215 android:host="www.example.com"
1216 android:pathPrefix="pathPattern" />
1217 </intent-filter>
1218 </activity>
1219 </application>
1220 </manifest>)";
1221 EXPECT_THAT(Verify(input), NotNull());
1222
1223 // No DeepLink, missing 'android:scheme' in <data> tag.
1224 input = R"(
1225 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
1226 package="android">
1227 <application>
1228 <activity android:name=".MainActivity">
1229 <intent-filter>
1230 <action android:name="android.intent.action.VIEW" />
1231 <category android:name="android.intent.category.DEFAULT" />
1232 <category android:name="android.intent.category.BROWSABLE" />
1233 <data android:host="www.example.com"
1234 android:pathPrefix="pathPattern" />
1235 </intent-filter>
1236 </activity>
1237 </application>
1238 </manifest>)";
1239 EXPECT_THAT(Verify(input), NotNull());
1240
1241 // No DeepLink, <action> is ACTION_MAIN not ACTION_VIEW.
1242 input = R"(
1243 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
1244 package="android">
1245 <application>
1246 <activity android:name=".MainActivity">
1247 <intent-filter>
1248 <action android:name="android.intent.action.MAIN" />
1249 <category android:name="android.intent.category.DEFAULT" />
1250 <category android:name="android.intent.category.BROWSABLE" />
1251 <data android:scheme="http"
1252 android:host="www.example.com"
1253 android:pathPrefix="pathPattern" />
1254 </intent-filter>
1255 </activity>
1256 </application>
1257 </manifest>)";
1258 EXPECT_THAT(Verify(input), NotNull());
1259
1260 // DeepLink with no leading slash in android:path.
1261 input = R"(
1262 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
1263 package="android">
1264 <application>
1265 <activity android:name=".MainActivity">
1266 <intent-filter>
1267 <action android:name="android.intent.action.VIEW" />
1268 <category android:name="android.intent.category.DEFAULT" />
1269 <category android:name="android.intent.category.BROWSABLE" />
1270 <data android:scheme="http"
1271 android:host="www.example.com"
1272 android:path="path" />
1273 </intent-filter>
1274 </activity>
1275 </application>
1276 </manifest>)";
1277 EXPECT_THAT(Verify(input), IsNull());
1278
1279 // DeepLink with leading slash in android:path.
1280 input = R"(
1281 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
1282 package="android">
1283 <application>
1284 <activity android:name=".MainActivity">
1285 <intent-filter>
1286 <action android:name="android.intent.action.VIEW" />
1287 <category android:name="android.intent.category.DEFAULT" />
1288 <category android:name="android.intent.category.BROWSABLE" />
1289 <data android:scheme="http"
1290 android:host="www.example.com"
1291 android:path="/path" />
1292 </intent-filter>
1293 </activity>
1294 </application>
1295 </manifest>)";
1296 EXPECT_THAT(Verify(input), NotNull());
1297
1298 // DeepLink with no leading slash in android:pathPrefix.
1299 input = R"(
1300 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
1301 package="android">
1302 <application>
1303 <activity android:name=".MainActivity">
1304 <intent-filter>
1305 <action android:name="android.intent.action.VIEW" />
1306 <category android:name="android.intent.category.DEFAULT" />
1307 <category android:name="android.intent.category.BROWSABLE" />
1308 <data android:scheme="http"
1309 android:host="www.example.com"
1310 android:pathPrefix="pathPrefix" />
1311 </intent-filter>
1312 </activity>
1313 </application>
1314 </manifest>)";
1315 EXPECT_THAT(Verify(input), IsNull());
1316
1317 // DeepLink with leading slash in android:pathPrefix.
1318 input = R"(
1319 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
1320 package="android">
1321 <application>
1322 <activity android:name=".MainActivity">
1323 <intent-filter>
1324 <action android:name="android.intent.action.VIEW" />
1325 <category android:name="android.intent.category.DEFAULT" />
1326 <category android:name="android.intent.category.BROWSABLE" />
1327 <data android:scheme="http"
1328 android:host="www.example.com"
1329 android:pathPrefix="/pathPrefix" />
1330 </intent-filter>
1331 </activity>
1332 </application>
1333 </manifest>)";
1334 EXPECT_THAT(Verify(input), NotNull());
1335
1336 // DeepLink with no leading slash in android:pathPattern.
1337 input = R"(
1338 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
1339 package="android">
1340 <application>
1341 <activity android:name=".MainActivity">
1342 <intent-filter>
1343 <action android:name="android.intent.action.VIEW" />
1344 <category android:name="android.intent.category.DEFAULT" />
1345 <category android:name="android.intent.category.BROWSABLE" />
1346 <data android:scheme="http"
1347 android:host="www.example.com"
1348 android:pathPattern="pathPattern" />
1349 </intent-filter>
1350 </activity>
1351 </application>
1352 </manifest>)";
1353 EXPECT_THAT(Verify(input), IsNull());
1354
1355 // DeepLink with leading slash in android:pathPattern.
1356 input = R"(
1357 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
1358 package="android">
1359 <application>
1360 <activity android:name=".MainActivity">
1361 <intent-filter>
1362 <action android:name="android.intent.action.VIEW" />
1363 <category android:name="android.intent.category.DEFAULT" />
1364 <category android:name="android.intent.category.BROWSABLE" />
1365 <data android:scheme="http"
1366 android:host="www.example.com"
1367 android:pathPattern="/pathPattern" />
1368 </intent-filter>
1369 </activity>
1370 </application>
1371 </manifest>)";
1372 EXPECT_THAT(Verify(input), NotNull());
1373
1374 // DeepLink with '.' start in pathPattern.
1375 input = R"(
1376 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
1377 package="android">
1378 <application>
1379 <activity android:name=".MainActivity">
1380 <intent-filter>
1381 <action android:name="android.intent.action.VIEW" />
1382 <category android:name="android.intent.category.DEFAULT" />
1383 <category android:name="android.intent.category.BROWSABLE" />
1384 <data android:scheme="http"
1385 android:host="www.example.com"
1386 android:pathPattern=".*\\.pathPattern" />
1387 </intent-filter>
1388 </activity>
1389 </application>
1390 </manifest>)";
1391 EXPECT_THAT(Verify(input), NotNull());
1392
1393 // DeepLink with '*' start in pathPattern.
1394 input = R"(
1395 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
1396 package="android">
1397 <application>
1398 <activity android:name=".MainActivity">
1399 <intent-filter>
1400 <action android:name="android.intent.action.VIEW" />
1401 <category android:name="android.intent.category.DEFAULT" />
1402 <category android:name="android.intent.category.BROWSABLE" />
1403 <data android:scheme="http"
1404 android:host="www.example.com"
1405 android:pathPattern="*" />
1406 </intent-filter>
1407 </activity>
1408 </application>
1409 </manifest>)";
1410 EXPECT_THAT(Verify(input), NotNull());
Brandon Liua35bf782022-11-03 19:04:31 +00001411
1412 // DeepLink with string reference as a path.
1413 input = R"(
1414 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
1415 package="android">
1416 <application>
1417 <activity android:name=".MainActivity">
1418 <intent-filter>
1419 <action android:name="android.intent.action.VIEW" />
1420 <category android:name="android.intent.category.DEFAULT" />
1421 <category android:name="android.intent.category.BROWSABLE" />
1422 <data android:scheme="http"
1423 android:host="www.example.com"
1424 android:path="@string/startup_uri" />
1425 </intent-filter>
1426 </activity>
1427 </application>
1428 </manifest>)";
1429 EXPECT_THAT(Verify(input), NotNull());
Brandon Liu3cff2552022-10-19 18:51:07 +00001430}
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001431} // namespace aapt