blob: 4842f62e53b58cc2b4c0983369a26452aaf868ab [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
328TEST_F(ManifestFixerTest, UseDefaultVersionNameAndCode) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700329 ManifestFixerOptions options;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700330 options.version_name_default = std::string("Beta");
331 options.version_code_default = std::string("0x10000000");
Ryan Mitchell704090e2018-07-31 14:59:25 -0700332 options.version_code_major_default = std::string("0x20000000");
Adam Lesinski52364f72016-01-11 13:10:24 -0800333
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700334 std::unique_ptr<xml::XmlResource> doc = VerifyWithOptions(R"EOF(
Adam Lesinski52364f72016-01-11 13:10:24 -0800335 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700336 package="android" />)EOF",
337 options);
Adam Lesinskic6284372017-12-04 13:46:23 -0800338 ASSERT_THAT(doc, NotNull());
Adam Lesinski52364f72016-01-11 13:10:24 -0800339
Adam Lesinski6b372992017-08-09 10:54:23 -0700340 xml::Element* manifest_el = doc->root.get();
Adam Lesinskic6284372017-12-04 13:46:23 -0800341 ASSERT_THAT(manifest_el, NotNull());
Adam Lesinski52364f72016-01-11 13:10:24 -0800342
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700343 xml::Attribute* attr =
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700344 manifest_el->FindAttribute(xml::kSchemaAndroid, "versionName");
Adam Lesinskic6284372017-12-04 13:46:23 -0800345 ASSERT_THAT(attr, NotNull());
346 EXPECT_THAT(attr->value, StrEq("Beta"));
Adam Lesinski52364f72016-01-11 13:10:24 -0800347
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700348 attr = manifest_el->FindAttribute(xml::kSchemaAndroid, "versionCode");
Adam Lesinskic6284372017-12-04 13:46:23 -0800349 ASSERT_THAT(attr, NotNull());
350 EXPECT_THAT(attr->value, StrEq("0x10000000"));
Ryan Mitchell704090e2018-07-31 14:59:25 -0700351
352 attr = manifest_el->FindAttribute(xml::kSchemaAndroid, "versionCodeMajor");
353 ASSERT_THAT(attr, NotNull());
354 EXPECT_THAT(attr->value, StrEq("0x20000000"));
Adam Lesinski52364f72016-01-11 13:10:24 -0800355}
356
Colin Crossdcd58c42018-05-25 22:46:35 -0700357TEST_F(ManifestFixerTest, DontUseDefaultVersionNameAndCode) {
Ryan Mitchell704090e2018-07-31 14:59:25 -0700358 ManifestFixerOptions options;
359 options.version_name_default = std::string("Beta");
360 options.version_code_default = std::string("0x10000000");
361 options.version_code_major_default = std::string("0x20000000");
Colin Crossdcd58c42018-05-25 22:46:35 -0700362
Ryan Mitchell704090e2018-07-31 14:59:25 -0700363 std::unique_ptr<xml::XmlResource> doc = VerifyWithOptions(R"EOF(
364 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
365 package="android"
366 android:versionCode="0x00000001"
367 android:versionCodeMajor="0x00000002"
368 android:versionName="Alpha" />)EOF",
369 options);
370 ASSERT_THAT(doc, NotNull());
Colin Crossdcd58c42018-05-25 22:46:35 -0700371
Ryan Mitchell704090e2018-07-31 14:59:25 -0700372 xml::Element* manifest_el = doc->root.get();
373 ASSERT_THAT(manifest_el, NotNull());
Colin Crossdcd58c42018-05-25 22:46:35 -0700374
Ryan Mitchell704090e2018-07-31 14:59:25 -0700375 xml::Attribute* attr =
376 manifest_el->FindAttribute(xml::kSchemaAndroid, "versionName");
377 ASSERT_THAT(attr, NotNull());
378 EXPECT_THAT(attr->value, StrEq("Alpha"));
Colin Crossdcd58c42018-05-25 22:46:35 -0700379
Ryan Mitchell704090e2018-07-31 14:59:25 -0700380 attr = manifest_el->FindAttribute(xml::kSchemaAndroid, "versionCode");
381 ASSERT_THAT(attr, NotNull());
382 EXPECT_THAT(attr->value, StrEq("0x00000001"));
383
384 attr = manifest_el->FindAttribute(xml::kSchemaAndroid, "versionCodeMajor");
385 ASSERT_THAT(attr, NotNull());
386 EXPECT_THAT(attr->value, StrEq("0x00000002"));
Colin Crossdcd58c42018-05-25 22:46:35 -0700387}
388
389TEST_F(ManifestFixerTest, ReplaceVersionNameAndCode) {
Ryan Mitchell704090e2018-07-31 14:59:25 -0700390 ManifestFixerOptions options;
391 options.replace_version = true;
392 options.version_name_default = std::string("Beta");
393 options.version_code_default = std::string("0x10000000");
394 options.version_code_major_default = std::string("0x20000000");
Colin Crossdcd58c42018-05-25 22:46:35 -0700395
Ryan Mitchell704090e2018-07-31 14:59:25 -0700396 std::unique_ptr<xml::XmlResource> doc = VerifyWithOptions(R"EOF(
397 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
398 package="android"
399 android:versionCode="0x00000001"
400 android:versionCodeMajor="0x00000002"
401 android:versionName="Alpha" />)EOF",
402 options);
403 ASSERT_THAT(doc, NotNull());
Colin Crossdcd58c42018-05-25 22:46:35 -0700404
Ryan Mitchell704090e2018-07-31 14:59:25 -0700405 xml::Element* manifest_el = doc->root.get();
406 ASSERT_THAT(manifest_el, NotNull());
Colin Crossdcd58c42018-05-25 22:46:35 -0700407
Ryan Mitchell704090e2018-07-31 14:59:25 -0700408 xml::Attribute* attr =
409 manifest_el->FindAttribute(xml::kSchemaAndroid, "versionName");
410 ASSERT_THAT(attr, NotNull());
411 EXPECT_THAT(attr->value, StrEq("Beta"));
Colin Crossdcd58c42018-05-25 22:46:35 -0700412
Ryan Mitchell704090e2018-07-31 14:59:25 -0700413 attr = manifest_el->FindAttribute(xml::kSchemaAndroid, "versionCode");
414 ASSERT_THAT(attr, NotNull());
415 EXPECT_THAT(attr->value, StrEq("0x10000000"));
416
417 attr = manifest_el->FindAttribute(xml::kSchemaAndroid, "versionCodeMajor");
418 ASSERT_THAT(attr, NotNull());
419 EXPECT_THAT(attr->value, StrEq("0x20000000"));
Colin Crossdcd58c42018-05-25 22:46:35 -0700420}
421
422TEST_F(ManifestFixerTest, ReplaceVersionName) {
Ryan Mitchell704090e2018-07-31 14:59:25 -0700423 ManifestFixerOptions options;
424 options.replace_version = true;
425 options.version_name_default = std::string("Beta");
Colin Crossdcd58c42018-05-25 22:46:35 -0700426
Colin Crossdcd58c42018-05-25 22:46:35 -0700427
Ryan Mitchell704090e2018-07-31 14:59:25 -0700428 std::unique_ptr<xml::XmlResource> doc = VerifyWithOptions(R"EOF(
429 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
430 package="android"
431 android:versionCode="0x00000001"
432 android:versionCodeMajor="0x00000002"
433 android:versionName="Alpha" />)EOF",
434 options);
435 ASSERT_THAT(doc, NotNull());
Colin Crossdcd58c42018-05-25 22:46:35 -0700436
Ryan Mitchell704090e2018-07-31 14:59:25 -0700437 xml::Element* manifest_el = doc->root.get();
438 ASSERT_THAT(manifest_el, NotNull());
Colin Crossdcd58c42018-05-25 22:46:35 -0700439
Ryan Mitchell704090e2018-07-31 14:59:25 -0700440 xml::Attribute* attr =
441 manifest_el->FindAttribute(xml::kSchemaAndroid, "versionName");
442 ASSERT_THAT(attr, NotNull());
443 EXPECT_THAT(attr->value, StrEq("Beta"));
444
445 attr = manifest_el->FindAttribute(xml::kSchemaAndroid, "versionCode");
446 ASSERT_THAT(attr, NotNull());
447 EXPECT_THAT(attr->value, StrEq("0x00000001"));
448
449 attr = manifest_el->FindAttribute(xml::kSchemaAndroid, "versionCodeMajor");
450 ASSERT_THAT(attr, NotNull());
451 EXPECT_THAT(attr->value, StrEq("0x00000002"));
Colin Crossdcd58c42018-05-25 22:46:35 -0700452}
453
454TEST_F(ManifestFixerTest, ReplaceVersionCode) {
Ryan Mitchell704090e2018-07-31 14:59:25 -0700455 ManifestFixerOptions options;
456 options.replace_version = true;
457 options.version_code_default = std::string("0x10000000");
Colin Crossdcd58c42018-05-25 22:46:35 -0700458
Ryan Mitchell704090e2018-07-31 14:59:25 -0700459 std::unique_ptr<xml::XmlResource> doc = VerifyWithOptions(R"EOF(
460 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
461 package="android"
462 android:versionCode="0x00000001"
463 android:versionCodeMajor="0x00000002"
464 android:versionName="Alpha" />)EOF",
465 options);
466 ASSERT_THAT(doc, NotNull());
467
468 xml::Element* manifest_el = doc->root.get();
469 ASSERT_THAT(manifest_el, NotNull());
470
471 xml::Attribute* attr =
472 manifest_el->FindAttribute(xml::kSchemaAndroid, "versionName");
473 ASSERT_THAT(attr, NotNull());
474 EXPECT_THAT(attr->value, StrEq("Alpha"));
475
476 attr = manifest_el->FindAttribute(xml::kSchemaAndroid, "versionCode");
477 ASSERT_THAT(attr, NotNull());
478 EXPECT_THAT(attr->value, StrEq("0x10000000"));
479
480 attr = manifest_el->FindAttribute(xml::kSchemaAndroid, "versionCodeMajor");
481 ASSERT_THAT(attr, NotNull());
482 EXPECT_THAT(attr->value, StrEq("0x00000002"));
483}
484
485TEST_F(ManifestFixerTest, ReplaceVersionCodeMajor) {
486 ManifestFixerOptions options;
487 options.replace_version = true;
488 options.version_code_major_default = std::string("0x20000000");
489
490 std::unique_ptr<xml::XmlResource> doc = VerifyWithOptions(R"EOF(
Colin Crossdcd58c42018-05-25 22:46:35 -0700491 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
Ryan Mitchell704090e2018-07-31 14:59:25 -0700492 package="android"
493 android:versionCode="0x00000001"
494 android:versionCodeMajor="0x00000002"
495 android:versionName="Alpha" />)EOF",
496 options);
497 ASSERT_THAT(doc, NotNull());
Colin Crossdcd58c42018-05-25 22:46:35 -0700498
Ryan Mitchell704090e2018-07-31 14:59:25 -0700499 xml::Element* manifest_el = doc->root.get();
500 ASSERT_THAT(manifest_el, NotNull());
Colin Crossdcd58c42018-05-25 22:46:35 -0700501
Ryan Mitchell704090e2018-07-31 14:59:25 -0700502 xml::Attribute* attr =
503 manifest_el->FindAttribute(xml::kSchemaAndroid, "versionName");
504 ASSERT_THAT(attr, NotNull());
505 EXPECT_THAT(attr->value, StrEq("Alpha"));
Colin Crossdcd58c42018-05-25 22:46:35 -0700506
Ryan Mitchell704090e2018-07-31 14:59:25 -0700507 attr = manifest_el->FindAttribute(xml::kSchemaAndroid, "versionCode");
508 ASSERT_THAT(attr, NotNull());
509 EXPECT_THAT(attr->value, StrEq("0x00000001"));
510
511 attr = manifest_el->FindAttribute(xml::kSchemaAndroid, "versionCodeMajor");
512 ASSERT_THAT(attr, NotNull());
513 EXPECT_THAT(attr->value, StrEq("0x20000000"));
Colin Crossdcd58c42018-05-25 22:46:35 -0700514}
515
516TEST_F(ManifestFixerTest, DontReplaceVersionNameOrCode) {
Ryan Mitchell704090e2018-07-31 14:59:25 -0700517 ManifestFixerOptions options;
518 options.replace_version = true;
Colin Crossdcd58c42018-05-25 22:46:35 -0700519
Ryan Mitchell704090e2018-07-31 14:59:25 -0700520 std::unique_ptr<xml::XmlResource> doc = VerifyWithOptions(R"EOF(
521 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
522 package="android"
523 android:versionCode="0x00000001"
524 android:versionCodeMajor="0x00000002"
525 android:versionName="Alpha" />)EOF",
526 options);
527 ASSERT_THAT(doc, NotNull());
Colin Crossdcd58c42018-05-25 22:46:35 -0700528
Ryan Mitchell704090e2018-07-31 14:59:25 -0700529 xml::Element* manifest_el = doc->root.get();
530 ASSERT_THAT(manifest_el, NotNull());
Colin Crossdcd58c42018-05-25 22:46:35 -0700531
Ryan Mitchell704090e2018-07-31 14:59:25 -0700532 xml::Attribute* attr =
533 manifest_el->FindAttribute(xml::kSchemaAndroid, "versionName");
534 ASSERT_THAT(attr, NotNull());
535 EXPECT_THAT(attr->value, StrEq("Alpha"));
Colin Crossdcd58c42018-05-25 22:46:35 -0700536
Ryan Mitchell704090e2018-07-31 14:59:25 -0700537 attr = manifest_el->FindAttribute(xml::kSchemaAndroid, "versionCode");
538 ASSERT_THAT(attr, NotNull());
539 EXPECT_THAT(attr->value, StrEq("0x00000001"));
540
541 attr = manifest_el->FindAttribute(xml::kSchemaAndroid, "versionCodeMajor");
542 ASSERT_THAT(attr, NotNull());
543 EXPECT_THAT(attr->value, StrEq("0x00000002"));
Colin Crossdcd58c42018-05-25 22:46:35 -0700544}
545
Adam Lesinski6b17d2c2016-08-10 11:37:06 -0700546TEST_F(ManifestFixerTest, EnsureManifestAttributesAreTyped) {
Adam Lesinskic6284372017-12-04 13:46:23 -0800547 EXPECT_THAT(Verify("<manifest package=\"android\" coreApp=\"hello\" />"), IsNull());
548 EXPECT_THAT(Verify("<manifest package=\"android\" coreApp=\"1dp\" />"), IsNull());
Adam Lesinski6b17d2c2016-08-10 11:37:06 -0700549
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700550 std::unique_ptr<xml::XmlResource> doc =
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700551 Verify("<manifest package=\"android\" coreApp=\"true\" />");
Adam Lesinskic6284372017-12-04 13:46:23 -0800552 ASSERT_THAT(doc, NotNull());
Adam Lesinski6b17d2c2016-08-10 11:37:06 -0700553
Adam Lesinski6b372992017-08-09 10:54:23 -0700554 xml::Element* el = doc->root.get();
Adam Lesinskic6284372017-12-04 13:46:23 -0800555 ASSERT_THAT(el, NotNull());
Adam Lesinski6b17d2c2016-08-10 11:37:06 -0700556
Adam Lesinskic6284372017-12-04 13:46:23 -0800557 EXPECT_THAT(el->name, StrEq("manifest"));
Adam Lesinski6b17d2c2016-08-10 11:37:06 -0700558
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700559 xml::Attribute* attr = el->FindAttribute("", "coreApp");
Adam Lesinskic6284372017-12-04 13:46:23 -0800560 ASSERT_THAT(attr, NotNull());
Adam Lesinski6b17d2c2016-08-10 11:37:06 -0700561
Adam Lesinskic6284372017-12-04 13:46:23 -0800562 EXPECT_THAT(attr->compiled_value, NotNull());
563 EXPECT_THAT(ValueCast<BinaryPrimitive>(attr->compiled_value.get()), NotNull());
Adam Lesinski6b17d2c2016-08-10 11:37:06 -0700564}
565
Adam Lesinski86d67df2017-01-31 13:47:27 -0800566TEST_F(ManifestFixerTest, UsesFeatureMustHaveNameOrGlEsVersion) {
567 std::string input = R"EOF(
568 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
569 package="android">
570 <uses-feature android:name="feature" />
571 <uses-feature android:glEsVersion="1" />
572 <feature-group />
573 <feature-group>
574 <uses-feature android:name="feature_in_group" />
575 <uses-feature android:glEsVersion="2" />
576 </feature-group>
577 </manifest>)EOF";
Adam Lesinskic6284372017-12-04 13:46:23 -0800578 EXPECT_THAT(Verify(input), NotNull());
Adam Lesinski86d67df2017-01-31 13:47:27 -0800579
580 input = R"EOF(
581 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
582 package="android">
583 <uses-feature android:name="feature" android:glEsVersion="1" />
584 </manifest>)EOF";
Adam Lesinskic6284372017-12-04 13:46:23 -0800585 EXPECT_THAT(Verify(input), IsNull());
Adam Lesinski86d67df2017-01-31 13:47:27 -0800586
587 input = R"EOF(
588 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
589 package="android">
590 <uses-feature />
591 </manifest>)EOF";
Adam Lesinskic6284372017-12-04 13:46:23 -0800592 EXPECT_THAT(Verify(input), IsNull());
Adam Lesinski86d67df2017-01-31 13:47:27 -0800593
594 input = R"EOF(
595 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
596 package="android">
597 <feature-group>
598 <uses-feature android:name="feature" android:glEsVersion="1" />
599 </feature-group>
600 </manifest>)EOF";
Adam Lesinskic6284372017-12-04 13:46:23 -0800601 EXPECT_THAT(Verify(input), IsNull());
Adam Lesinski86d67df2017-01-31 13:47:27 -0800602
603 input = R"EOF(
604 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
605 package="android">
606 <feature-group>
607 <uses-feature />
608 </feature-group>
609 </manifest>)EOF";
Adam Lesinskic6284372017-12-04 13:46:23 -0800610 EXPECT_THAT(Verify(input), IsNull());
Adam Lesinski86d67df2017-01-31 13:47:27 -0800611}
612
Ryan Mitchelle5b38a62018-03-23 13:35:00 -0700613TEST_F(ManifestFixerTest, ApplicationInjectDebuggable) {
614 ManifestFixerOptions options;
615 options.debug_mode = true;
616
617 std::string no_d = R"(
618 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
619 package="android">
620 <application>
621 </application>
622 </manifest>)";
623
624 std::string false_d = R"(
625 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
626 package="android">
627 <application android:debuggable="false">
628 </application>
629 </manifest>)";
630
631 std::string true_d = R"(
632 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
633 package="android">
634 <application android:debuggable="true">
635 </application>
636 </manifest>)";
637
638 // Inject the debuggable attribute when the attribute is not present and the
639 // flag is present
640 std::unique_ptr<xml::XmlResource> manifest = VerifyWithOptions(no_d, options);
641 EXPECT_THAT(manifest->root.get()->FindChildWithAttribute(
642 {}, "application", xml::kSchemaAndroid, "debuggable", "true"), NotNull());
643
644 // Set the debuggable flag to true if the attribute is false and the flag is
645 // present
646 manifest = VerifyWithOptions(false_d, options);
647 EXPECT_THAT(manifest->root.get()->FindChildWithAttribute(
648 {}, "application", xml::kSchemaAndroid, "debuggable", "true"), NotNull());
649
650 // Keep debuggable flag true if the attribute is true and the flag is present
651 manifest = VerifyWithOptions(true_d, options);
652 EXPECT_THAT(manifest->root.get()->FindChildWithAttribute(
653 {}, "application", xml::kSchemaAndroid, "debuggable", "true"), NotNull());
654
655 // Do not inject the debuggable attribute when the attribute is not present
656 // and the flag is not present
657 manifest = Verify(no_d);
658 EXPECT_THAT(manifest->root.get()->FindChildWithAttribute(
659 {}, "application", xml::kSchemaAndroid, "debuggable", "true"), IsNull());
660
661 // Do not set the debuggable flag to true if the attribute is false and the
662 // flag is not present
663 manifest = Verify(false_d);
664 EXPECT_THAT(manifest->root.get()->FindChildWithAttribute(
665 {}, "application", xml::kSchemaAndroid, "debuggable", "true"), IsNull());
666
667 // Keep debuggable flag true if the attribute is true and the flag is not
668 // present
669 manifest = Verify(true_d);
670 EXPECT_THAT(manifest->root.get()->FindChildWithAttribute(
671 {}, "application", xml::kSchemaAndroid, "debuggable", "true"), NotNull());
672}
673
674
Adam Lesinski63699b12017-05-08 18:36:33 -0700675TEST_F(ManifestFixerTest, IgnoreNamespacedElements) {
676 std::string input = R"EOF(
677 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
678 package="android">
679 <special:tag whoo="true" xmlns:special="http://google.com" />
680 </manifest>)EOF";
Adam Lesinskic6284372017-12-04 13:46:23 -0800681 EXPECT_THAT(Verify(input), NotNull());
Adam Lesinski63699b12017-05-08 18:36:33 -0700682}
683
684TEST_F(ManifestFixerTest, DoNotIgnoreNonNamespacedElements) {
685 std::string input = R"EOF(
686 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
687 package="android">
688 <tag whoo="true" />
689 </manifest>)EOF";
Adam Lesinskic6284372017-12-04 13:46:23 -0800690 EXPECT_THAT(Verify(input), IsNull());
Adam Lesinski63699b12017-05-08 18:36:33 -0700691}
692
Adam Lesinski87f1e0f2017-06-27 16:21:58 -0700693TEST_F(ManifestFixerTest, SupportKeySets) {
694 std::string input = R"(
695 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
696 package="android">
697 <key-sets>
698 <key-set android:name="old-set">
699 <public-key android:name="old-key" android:value="some+old+key" />
700 </key-set>
701 <key-set android:name="new-set">
702 <public-key android:name="new-key" android:value="some+new+key" />
703 </key-set>
704 <upgrade-key-set android:name="old-set" />
705 <upgrade-key-set android:name="new-set" />
706 </key-sets>
707 </manifest>)";
708 EXPECT_THAT(Verify(input), NotNull());
709}
710
Adam Lesinskic6284372017-12-04 13:46:23 -0800711TEST_F(ManifestFixerTest, InsertCompileSdkVersions) {
712 std::string input = R"(
713 <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="android" />)";
714 ManifestFixerOptions options;
715 options.compile_sdk_version = {"28"};
716 options.compile_sdk_version_codename = {"P"};
717
718 std::unique_ptr<xml::XmlResource> manifest = VerifyWithOptions(input, options);
719 ASSERT_THAT(manifest, NotNull());
720
721 xml::Attribute* attr = manifest->root->FindAttribute(xml::kSchemaAndroid, "compileSdkVersion");
722 ASSERT_THAT(attr, NotNull());
723 EXPECT_THAT(attr->value, StrEq("28"));
724
725 attr = manifest->root->FindAttribute(xml::kSchemaAndroid, "compileSdkVersionCodename");
726 ASSERT_THAT(attr, NotNull());
727 EXPECT_THAT(attr->value, StrEq("P"));
Ryan Mitchellaada89c2019-02-12 08:06:26 -0800728
729 attr = manifest->root->FindAttribute("", "platformBuildVersionCode");
730 ASSERT_THAT(attr, NotNull());
731 EXPECT_THAT(attr->value, StrEq("28"));
732
733 attr = manifest->root->FindAttribute("", "platformBuildVersionName");
734 ASSERT_THAT(attr, NotNull());
735 EXPECT_THAT(attr->value, StrEq("P"));
736}
737
738TEST_F(ManifestFixerTest, OverrideCompileSdkVersions) {
739 std::string input = R"(
740 <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="android"
741 compileSdkVersion="27" compileSdkVersionCodename="O"
742 platformBuildVersionCode="27" platformBuildVersionName="O"/>)";
743 ManifestFixerOptions options;
744 options.compile_sdk_version = {"28"};
745 options.compile_sdk_version_codename = {"P"};
746
747 std::unique_ptr<xml::XmlResource> manifest = VerifyWithOptions(input, options);
748 ASSERT_THAT(manifest, NotNull());
749
750 xml::Attribute* attr = manifest->root->FindAttribute(xml::kSchemaAndroid, "compileSdkVersion");
751 ASSERT_THAT(attr, NotNull());
752 EXPECT_THAT(attr->value, StrEq("28"));
753
754 attr = manifest->root->FindAttribute(xml::kSchemaAndroid, "compileSdkVersionCodename");
755 ASSERT_THAT(attr, NotNull());
756 EXPECT_THAT(attr->value, StrEq("P"));
757
758 attr = manifest->root->FindAttribute("", "platformBuildVersionCode");
759 ASSERT_THAT(attr, NotNull());
760 EXPECT_THAT(attr->value, StrEq("28"));
761
762 attr = manifest->root->FindAttribute("", "platformBuildVersionName");
763 ASSERT_THAT(attr, NotNull());
764 EXPECT_THAT(attr->value, StrEq("P"));
Adam Lesinskic6284372017-12-04 13:46:23 -0800765}
766
Izabela Orlowskaad9e1322017-12-19 16:22:42 +0000767TEST_F(ManifestFixerTest, UnexpectedElementsInManifest) {
768 std::string input = R"(
769 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
770 package="android">
771 <beep/>
772 </manifest>)";
773 ManifestFixerOptions options;
774 options.warn_validation = true;
775
776 // Unexpected element should result in a warning if the flag is set to 'true'.
777 std::unique_ptr<xml::XmlResource> manifest = VerifyWithOptions(input, options);
778 ASSERT_THAT(manifest, NotNull());
779
780 // Unexpected element should result in an error if the flag is set to 'false'.
781 options.warn_validation = false;
782 manifest = VerifyWithOptions(input, options);
783 ASSERT_THAT(manifest, IsNull());
784
785 // By default the flag should be set to 'false'.
786 manifest = Verify(input);
787 ASSERT_THAT(manifest, IsNull());
788}
789
Adam Lesinskifca5e422017-12-20 15:03:36 -0800790TEST_F(ManifestFixerTest, UsesLibraryMustHaveNonEmptyName) {
791 std::string input = R"(
792 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
793 package="android">
794 <application>
795 <uses-library android:name="" />
796 </application>
797 </manifest>)";
798 EXPECT_THAT(Verify(input), IsNull());
799
800 input = R"(
801 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
802 package="android">
803 <application>
804 <uses-library />
805 </application>
806 </manifest>)";
807 EXPECT_THAT(Verify(input), IsNull());
808
809 input = R"(
810 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
811 package="android">
812 <application>
813 <uses-library android:name="blahhh" />
814 </application>
815 </manifest>)";
816 EXPECT_THAT(Verify(input), NotNull());
817}
818
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700819} // namespace aapt