Debian Codegraph @

build-system/manifest-merger/src/test/java/com/android/manifmerger/ManifestMerger2SmallTest.java

raw
1 /*
2 * Copyright (C) 2014 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 package com.android.manifmerger;
18
19 import static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertNotNull;
21 import static org.junit.Assert.assertNull;
22 import static org.junit.Assert.assertTrue;
23 import static org.junit.Assert.fail;
24
25 import com.android.SdkConstants;
26 import com.android.annotations.NonNull;
27 import com.android.manifmerger.MergingReport.MergedManifestKind;
28 import com.android.sdklib.mock.MockLog;
29 import com.android.utils.XmlUtils;
30 import com.google.common.base.Charsets;
31 import com.google.common.base.Optional;
32 import com.google.common.collect.ImmutableMap;
33 import com.google.common.io.Files;
34
35 import org.junit.Test;
36 import org.junit.runner.RunWith;
37 import org.mockito.Mock;
38 import org.mockito.runners.MockitoJUnitRunner;
39 import org.w3c.dom.Attr;
40 import org.w3c.dom.Document;
41 import org.w3c.dom.Element;
42 import org.w3c.dom.Node;
43 import org.w3c.dom.NodeList;
44 import org.xml.sax.SAXException;
45
46 import java.io.ByteArrayInputStream;
47 import java.io.File;
48 import java.io.FileNotFoundException;
49 import java.io.IOException;
50 import java.io.InputStream;
51 import java.util.Map;
52 import java.util.logging.Logger;
53
54 import javax.xml.parsers.ParserConfigurationException;
55
56 /**
57 * Tests for the {@link ManifestMergerTestUtil} class
58 */
59 @RunWith(MockitoJUnitRunner.class)
60 public class ManifestMerger2SmallTest {
61
62 @Mock
63 private ActionRecorder mActionRecorder;
64
65 @Test
66 public void testValidationFailure() throws Exception {
67
68 MockLog mockLog = new MockLog();
69 String input = ""
70 + "<manifest\n"
71 + " xmlns:android=\"http://schemas.android.com/apk/res/android\"\n"
72 + " xmlns:tools=\"http://schemas.android.com/tools\"\n"
73 + " package=\"com.example.lib3\">\n"
74 + "\n"
75 + " <application android:label=\"@string/lib_name\" />\n"
76 + "\n"
77 + " <activity android:name=\"activityOne\" "
78 + " tools:replace=\"exported\"/>\n"
79 + "\n"
80 + "</manifest>";
81
82 File tmpFile = inputAsFile("ManifestMerger2Test_testValidationFailure", input);
83 assertTrue(tmpFile.exists());
84
85 try {
86 MergingReport mergingReport = ManifestMerger2.newMerger(tmpFile, mockLog,
87 ManifestMerger2.MergeType.APPLICATION).merge();
88 assertEquals(MergingReport.Result.ERROR, mergingReport.getResult());
89 // check the log complains about the incorrect "tools:replace"
90 assertStringPresenceInLogRecords(mergingReport, "tools:replace");
91 assertNull(mergingReport.getMergedDocument(MergedManifestKind.MERGED));
92 } finally {
93 assertTrue(tmpFile.delete());
94 }
95 }
96
97 @Test
98 public void testToolsAnnotationRemoval() throws Exception {
99
100 MockLog mockLog = new MockLog();
101 String input = ""
102 + "<manifest\n"
103 + " xmlns:android=\"http://schemas.android.com/apk/res/android\"\n"
104 + " xmlns:tools=\"http://schemas.android.com/tools\"\n"
105 + " package=\"com.example.lib3\">\n"
106 + "\n"
107 + " <application android:label=\"@string/lib_name\" "
108 + " tools:replace=\"label\"/>\n"
109 + "\n"
110 + "</manifest>";
111
112 File tmpFile = inputAsFile("testToolsAnnotationRemoval", input);
113 assertTrue(tmpFile.exists());
114
115 try {
116 MergingReport mergingReport = ManifestMerger2.newMerger(tmpFile, mockLog,
117 ManifestMerger2.MergeType.APPLICATION)
118 .withFeatures(ManifestMerger2.Invoker.Feature.REMOVE_TOOLS_DECLARATIONS)
119 .merge();
120 assertEquals(MergingReport.Result.WARNING, mergingReport.getResult());
121 // ensure tools annotation removal.
122 Document xmlDocument = parse(mergingReport.getMergedDocument(MergedManifestKind.MERGED));
123 NodeList applications = xmlDocument.getElementsByTagName(SdkConstants.TAG_APPLICATION);
124 assertTrue(applications.getLength() == 1);
125 Node replace = applications.item(0).getAttributes()
126 .getNamedItemNS(SdkConstants.TOOLS_URI, "replace");
127 assertNull(replace);
128 } finally {
129 assertTrue(tmpFile.delete());
130 }
131 }
132
133 @Test
134 public void testToolsAnnotationRemovalForLibraries() throws Exception {
135 MockLog mockLog = new MockLog();
136 String overlay = ""
137 + "<manifest\n"
138 + " xmlns:android=\"http://schemas.android.com/apk/res/android\"\n"
139 + " xmlns:tools=\"http://schemas.android.com/tools\"\n"
140 + " package=\"com.example.app1\">\n"
141 + "\n"
142 + " <application android:label=\"@string/lib_name\">\n"
143 + " <activity tools:node=\"removeAll\">\n"
144 + " </activity>\n"
145 + " </application>"
146 + "\n"
147 + "</manifest>";
148
149 File overlayFile = inputAsFile("testToolsAnnotationRemoval", overlay);
150 assertTrue(overlayFile.exists());
151
152 String libraryInput = ""
153 + "<manifest\n"
154 + "xmlns:android=\"http://schemas.android.com/apk/res/android\"\n"
155 + "package=\"com.example.app1\">\n"
156 + "\n"
157 + "<application android:name=\"TheApp\" >\n"
158 + " <!-- Activity to configure widget -->\n"
159 + " <activity\n"
160 + " android:icon=\"@drawable/widget_icon\"\n"
161 + " android:label=\"Configure Widget\"\n"
162 + " android:name=\"com.example.lib1.WidgetConfigurationUI\"\n"
163 + " android:theme=\"@style/Theme.WidgetConfigurationUI\" >\n"
164 + " <intent-filter >\n"
165 + " <action android:name=\"android.appwidget.action.APPWIDGET_CONFIGURE\" />\n"
166 + " </intent-filter>\n"
167 + " </activity>\n"
168 + "</application>\n"
169 + "\n"
170 + "</manifest>";
171 File libFile = inputAsFile("testToolsAnnotationRemoval", libraryInput);
172
173
174 try {
175 MergingReport mergingReport =
176 ManifestMerger2.newMerger(libFile, mockLog, ManifestMerger2.MergeType.LIBRARY)
177 .withFeatures(ManifestMerger2.Invoker.Feature.REMOVE_TOOLS_DECLARATIONS)
178 .addFlavorAndBuildTypeManifest(overlayFile)
179 .merge();
180 assertEquals(MergingReport.Result.SUCCESS, mergingReport.getResult());
181 // ensure tools annotation removal.
182 Document xmlDocument = parse(mergingReport.getMergedDocument(MergedManifestKind.MERGED));
183 NodeList applications = xmlDocument.getElementsByTagName(SdkConstants.TAG_ACTIVITY);
184 assertTrue(applications.getLength() == 0);
185 } finally {
186 assertTrue(overlayFile.delete());
187 assertTrue(libFile.delete());
188 }
189 }
190
191 @Test
192 public void testToolsAnnotationPresence() throws Exception {
193
194 MockLog mockLog = new MockLog();
195 String input = ""
196 + "<manifest\n"
197 + " xmlns:android=\"http://schemas.android.com/apk/res/android\"\n"
198 + " xmlns:tools=\"http://schemas.android.com/tools\"\n"
199 + " package=\"com.example.lib3\">\n"
200 + "\n"
201 + " <application android:label=\"@string/lib_name\" "
202 + " tools:replace=\"label\"/>\n"
203 + "\n"
204 + "</manifest>";
205
206 File tmpFile = inputAsFile("testToolsAnnotationRemoval", input);
207 assertTrue(tmpFile.exists());
208
209 try {
210 MergingReport mergingReport = ManifestMerger2.newMerger(tmpFile, mockLog,
211 ManifestMerger2.MergeType.LIBRARY)
212 .merge();
213 assertEquals(MergingReport.Result.WARNING, mergingReport.getResult());
214 // ensure tools annotation removal.
215 Document xmlDocument = parse(mergingReport.getMergedDocument(MergedManifestKind.MERGED));
216 NodeList applications = xmlDocument.getElementsByTagName(SdkConstants.TAG_APPLICATION);
217 assertTrue(applications.getLength() == 1);
218 Node replace = applications.item(0).getAttributes()
219 .getNamedItemNS(SdkConstants.TOOLS_URI, "replace");
220 assertNotNull(replace);
221 assertEquals("tools:replace value not correct", "label", replace.getNodeValue());
222 } finally {
223 assertTrue(tmpFile.delete());
224 }
225 }
226
227
228 @Test
229 public void testPackageOverride() throws Exception {
230 String xml = ""
231 + "<manifest\n"
232 + " xmlns:android=\"http://schemas.android.com/apk/res/android\""
233 + " package=\"com.foo.old\" >\n"
234 + " <activity android:name=\"activityOne\"/>\n"
235 + "</manifest>";
236
237 XmlDocument refDocument = TestUtils.xmlDocumentFromString(
238 TestUtils.sourceFile(getClass(), "testPackageOverride#xml"), xml);
239
240 ManifestSystemProperty.PACKAGE.addTo(mActionRecorder, refDocument, "com.bar.new");
241 // verify the package value was overridden.
242 assertEquals("com.bar.new", refDocument.getRootNode().getXml().getAttribute("package"));
243 }
244
245 @Test
246 public void testMissingPackageOverride() throws Exception {
247 String xml = ""
248 + "<manifest\n"
249 + " xmlns:android=\"http://schemas.android.com/apk/res/android\">\n"
250 + " <activity android:name=\"activityOne\"/>\n"
251 + "</manifest>";
252
253 XmlDocument refDocument = TestUtils.xmlDocumentFromString(
254 TestUtils.sourceFile(getClass(), "testMissingPackageOverride#xml"), xml);
255
256 ManifestSystemProperty.PACKAGE.addTo(mActionRecorder, refDocument, "com.bar.new");
257 // verify the package value was added.
258 assertEquals("com.bar.new", refDocument.getRootNode().getXml().getAttribute("package"));
259 }
260
261 @Test
262 public void testAddingSystemProperties() throws Exception {
263 String xml = ""
264 + "<manifest\n"
265 + " xmlns:android=\"http://schemas.android.com/apk/res/android\">\n"
266 + " <activity android:name=\"activityOne\"/>\n"
267 + "</manifest>";
268
269 XmlDocument document = TestUtils.xmlDocumentFromString(
270 TestUtils.sourceFile(getClass(),
271 "testAddingSystemProperties#xml"), xml);
272
273 ManifestSystemProperty.VERSION_CODE.addTo(mActionRecorder, document, "101");
274 assertEquals("101",
275 document.getXml().getDocumentElement().getAttribute("android:versionCode"));
276
277 ManifestSystemProperty.VERSION_NAME.addTo(mActionRecorder, document, "1.0.1");
278 assertEquals("1.0.1",
279 document.getXml().getDocumentElement().getAttribute("android:versionName"));
280
281 ManifestSystemProperty.MIN_SDK_VERSION.addTo(mActionRecorder, document, "10");
282 Element usesSdk = (Element) document.getXml().getElementsByTagName("uses-sdk").item(0);
283 assertNotNull(usesSdk);
284 assertEquals("10", usesSdk.getAttribute("android:minSdkVersion"));
285
286 ManifestSystemProperty.TARGET_SDK_VERSION.addTo(mActionRecorder, document, "14");
287 usesSdk = (Element) document.getXml().getElementsByTagName("uses-sdk").item(0);
288 assertNotNull(usesSdk);
289 assertEquals("14", usesSdk.getAttribute("android:targetSdkVersion"));
290
291 ManifestSystemProperty.MAX_SDK_VERSION.addTo(mActionRecorder, document, "16");
292 usesSdk = (Element) document.getXml().getElementsByTagName("uses-sdk").item(0);
293 assertNotNull(usesSdk);
294 assertEquals("16", usesSdk.getAttribute("android:maxSdkVersion"));
295 }
296
297 @Test
298 public void testAddingSystemProperties_withDifferentPrefix() throws Exception {
299 String xml = ""
300 + "<manifest\n"
301 + " xmlns:t=\"http://schemas.android.com/apk/res/android\">\n"
302 + " <activity t:name=\"activityOne\"/>\n"
303 + "</manifest>";
304
305 XmlDocument document = TestUtils.xmlDocumentFromString(
306 TestUtils.sourceFile(getClass(),
307 "testAddingSystemProperties#xml"), xml
308 );
309
310 ManifestSystemProperty.VERSION_CODE.addTo(mActionRecorder, document, "101");
311 // using the non namespace aware API to make sure the prefix is the expected one.
312 assertEquals("101",
313 document.getXml().getDocumentElement().getAttribute("t:versionCode"));
314 }
315
316 @Test
317 public void testOverridingSystemProperties() throws Exception {
318 String xml = ""
319 + "<manifest versionCode=\"34\" versionName=\"3.4\"\n"
320 + " xmlns:android=\"http://schemas.android.com/apk/res/android\">\n"
321 + " <uses-sdk minSdkVersion=\"9\" targetSdkVersion=\".9\"/>\n"
322 + " <activity android:name=\"activityOne\"/>\n"
323 + "</manifest>";
324
325 XmlDocument document = TestUtils.xmlDocumentFromString(
326 TestUtils.sourceFile(getClass(),
327 "testAddingSystemProperties#xml"), xml);
328 // check initial state.
329 assertEquals("34", document.getXml().getDocumentElement().getAttribute("versionCode"));
330 assertEquals("3.4", document.getXml().getDocumentElement().getAttribute("versionName"));
331 Element usesSdk = (Element) document.getXml().getElementsByTagName("uses-sdk").item(0);
332 assertNotNull(usesSdk);
333 assertEquals("9", usesSdk.getAttribute("minSdkVersion"));
334 assertEquals(".9", usesSdk.getAttribute("targetSdkVersion"));
335
336
337 ManifestSystemProperty.VERSION_CODE.addTo(mActionRecorder, document, "101");
338 assertEquals("101",
339 document.getXml().getDocumentElement().getAttribute("android:versionCode"));
340
341 ManifestSystemProperty.VERSION_NAME.addTo(mActionRecorder, document, "1.0.1");
342 assertEquals("1.0.1",
343 document.getXml().getDocumentElement().getAttribute("android:versionName"));
344
345 ManifestSystemProperty.MIN_SDK_VERSION.addTo(mActionRecorder, document, "10");
346 usesSdk = (Element) document.getXml().getElementsByTagName("uses-sdk").item(0);
347 assertNotNull(usesSdk);
348 assertEquals("10", usesSdk.getAttribute("android:minSdkVersion"));
349
350 ManifestSystemProperty.TARGET_SDK_VERSION.addTo(mActionRecorder, document, "14");
351 usesSdk = (Element) document.getXml().getElementsByTagName("uses-sdk").item(0);
352 assertNotNull(usesSdk);
353 assertEquals("14", usesSdk.getAttribute("android:targetSdkVersion"));
354 }
355
356 @Test
357 public void testPlaceholderSubstitution() throws Exception {
358 String xml = ""
359 + "<manifest package=\"foo\" versionCode=\"34\" versionName=\"3.4\"\n"
360 + " xmlns:android=\"http://schemas.android.com/apk/res/android\">\n"
361 + " <activity android:name=\".activityOne\" android:label=\"${labelName}\"/>\n"
362 + "</manifest>";
363
364 Map<String, String> placeholders = ImmutableMap.of("labelName", "injectedLabelName");
365 MockLog mockLog = new MockLog();
366 File inputFile = inputAsFile("testPlaceholderSubstitution", xml);
367 try {
368 MergingReport mergingReport = ManifestMerger2
369 .newMerger(inputFile, mockLog, ManifestMerger2.MergeType.APPLICATION)
370 .setPlaceHolderValues(placeholders)
371 .merge();
372
373 assertTrue(mergingReport.getResult().isSuccess());
374 Document document = parse(mergingReport.getMergedDocument(MergedManifestKind.MERGED));
375 assertNotNull(document);
376 Optional<Element> activityOne = getElementByTypeAndKey(
377 document, "activity", "foo.activityOne");
378 assertTrue(activityOne.isPresent());
379 Attr label = activityOne.get().getAttributeNodeNS(SdkConstants.ANDROID_URI, "label");
380 assertNotNull(label);
381 assertEquals("injectedLabelName", label.getValue());
382 } finally {
383 //noinspection ResultOfMethodCallIgnored
384 inputFile.delete();
385 }
386 }
387
388 @Test
389 public void testApplicationIdSubstitution() throws Exception {
390 String xml = ""
391 + "<manifest package=\"foo\" versionCode=\"34\" versionName=\"3.4\"\n"
392 + " xmlns:android=\"http://schemas.android.com/apk/res/android\">\n"
393 + " <activity android:name=\"${applicationId}.activityOne\"/>\n"
394 + "</manifest>";
395
396 MockLog mockLog = new MockLog();
397 File inputFile = inputAsFile("testPlaceholderSubstitution", xml);
398 try {
399 MergingReport mergingReport = ManifestMerger2
400 .newMerger(inputFile, mockLog, ManifestMerger2.MergeType.APPLICATION)
401 .setOverride(ManifestSystemProperty.PACKAGE, "bar")
402 .merge();
403
404 assertTrue(mergingReport.getResult().isSuccess());
405 Document document = parse(mergingReport.getMergedDocument(MergedManifestKind.MERGED));
406 assertEquals("bar", document.getElementsByTagName("manifest")
407 .item(0).getAttributes().getNamedItem("package").getNodeValue());
408 Optional<Element> activityOne = getElementByTypeAndKey(document, "activity",
409 "bar.activityOne");
410 assertTrue(activityOne.isPresent());
411 } finally {
412 //noinspection ResultOfMethodCallIgnored
413 inputFile.delete();
414 }
415 }
416
417 @Test
418 public void testNoApplicationIdValueProvided() throws Exception {
419 String xml = ""
420 + "<manifest package=\"foo\" versionCode=\"34\" versionName=\"3.4\"\n"
421 + " xmlns:android=\"http://schemas.android.com/apk/res/android\">\n"
422 + " <activity android:name=\"${applicationId}.activityOne\"/>\n"
423 + "</manifest>";
424
425 MockLog mockLog = new MockLog();
426 File inputFile = inputAsFile("testPlaceholderSubstitution", xml);
427 try {
428 MergingReport mergingReport = ManifestMerger2
429 .newMerger(inputFile, mockLog, ManifestMerger2.MergeType.APPLICATION)
430 .merge();
431
432 assertTrue(mergingReport.getResult().isSuccess());
433 assertNotNull(mergingReport.getMergedDocument(MergedManifestKind.MERGED));
434 Document document = parse(mergingReport.getMergedDocument(MergedManifestKind.MERGED));
435 assertEquals("foo", document.getElementsByTagName("manifest")
436 .item(0).getAttributes().getNamedItem("package").getNodeValue());
437 Optional<Element> activityOne = getElementByTypeAndKey(document, "activity",
438 "foo.activityOne");
439 assertTrue(activityOne.isPresent());
440 } finally {
441 //noinspection ResultOfMethodCallIgnored
442 inputFile.delete();
443 }
444 }
445
446 @Test
447 public void testNoFqcnsExtraction() throws Exception {
448 String xml = ""
449 + "<manifest\n"
450 + " package=\"com.foo.example\""
451 + " xmlns:t=\"http://schemas.android.com/apk/res/android\">\n"
452 + " <activity t:name=\"activityOne\"/>\n"
453 + " <activity t:name=\"com.foo.bar.example.activityTwo\"/>\n"
454 + " <activity t:name=\"com.foo.example.activityThree\"/>\n"
455 + " <application t:name=\".applicationOne\" "
456 + " t:backupAgent=\"com.foo.example.myBackupAgent\"/>\n"
457 + "</manifest>";
458
459 File inputFile = inputAsFile("testFcqnsExtraction", xml);
460
461 MockLog mockLog = new MockLog();
462 MergingReport mergingReport = ManifestMerger2
463 .newMerger(inputFile, mockLog, ManifestMerger2.MergeType.APPLICATION)
464 .merge();
465
466 assertTrue(mergingReport.getResult().isSuccess());
467 Document xmlDocument = parse(mergingReport.getMergedDocument(MergedManifestKind.MERGED));
468 assertEquals("com.foo.example.activityOne",
469 xmlDocument.getElementsByTagName("activity").item(0).getAttributes()
470 .item(0).getNodeValue());
471 assertEquals("com.foo.bar.example.activityTwo",
472 xmlDocument.getElementsByTagName("activity").item(1).getAttributes()
473 .item(0).getNodeValue());
474 assertEquals("com.foo.example.activityThree",
475 xmlDocument.getElementsByTagName("activity").item(2).getAttributes()
476 .item(0).getNodeValue());
477 assertEquals("com.foo.example.applicationOne",
478 xmlDocument.getElementsByTagName("application").item(0).getAttributes()
479 .getNamedItemNS("http://schemas.android.com/apk/res/android", "name")
480 .getNodeValue());
481 assertEquals("com.foo.example.myBackupAgent",
482 xmlDocument.getElementsByTagName("application").item(0).getAttributes()
483 .getNamedItemNS("http://schemas.android.com/apk/res/android", "backupAgent")
484 .getNodeValue());
485 }
486
487 @Test
488 public void testFqcnsExtraction() throws Exception {
489 String xml = ""
490 + "<manifest\n"
491 + " package=\"com.foo.example\""
492 + " xmlns:t=\"http://schemas.android.com/apk/res/android\">\n"
493 + " <activity t:name=\"activityOne\"/>\n"
494 + " <activity t:name=\"com.foo.bar.example.activityTwo\"/>\n"
495 + " <activity t:name=\"com.foo.example.activityThree\"/>\n"
496 + " <application t:name=\".applicationOne\" "
497 + " t:backupAgent=\"com.foo.example.myBackupAgent\"/>\n"
498 + "</manifest>";
499
500 File inputFile = inputAsFile("testFcqnsExtraction", xml);
501
502 MockLog mockLog = new MockLog();
503 MergingReport mergingReport = ManifestMerger2
504 .newMerger(inputFile, mockLog, ManifestMerger2.MergeType.APPLICATION)
505 .withFeatures(ManifestMerger2.Invoker.Feature.EXTRACT_FQCNS)
506 .merge();
507
508 assertTrue(mergingReport.getResult().isSuccess());
509 Document xmlDocument = parse(mergingReport.getMergedDocument(MergedManifestKind.MERGED));
510 assertEquals(".activityOne",
511 xmlDocument.getElementsByTagName("activity").item(0).getAttributes()
512 .item(0).getNodeValue());
513 assertEquals("com.foo.bar.example.activityTwo",
514 xmlDocument.getElementsByTagName("activity").item(1).getAttributes()
515 .item(0).getNodeValue());
516 assertEquals(".activityThree",
517 xmlDocument.getElementsByTagName("activity").item(2).getAttributes()
518 .item(0).getNodeValue());
519 assertEquals(".applicationOne",
520 xmlDocument.getElementsByTagName("application").item(0).getAttributes()
521 .getNamedItemNS("http://schemas.android.com/apk/res/android", "name")
522 .getNodeValue());
523 assertEquals(".myBackupAgent",
524 xmlDocument.getElementsByTagName("application").item(0).getAttributes()
525 .getNamedItemNS("http://schemas.android.com/apk/res/android", "backupAgent")
526 .getNodeValue());
527 }
528
529 @Test
530 public void testNoPlaceholderReplacement() throws Exception {
531 String xml = ""
532 + "<manifest\n"
533 + " package=\"${applicationId}\""
534 + " xmlns:t=\"http://schemas.android.com/apk/res/android\">\n"
535 + " <activity t:name=\"activityOne\"/>\n"
536 + " <application t:name=\".applicationOne\" "
537 + " t:backupAgent=\"com.foo.example.myBackupAgent\"/>\n"
538 + "</manifest>";
539
540 File inputFile = inputAsFile("testNoPlaceHolderReplacement", xml);
541
542 MockLog mockLog = new MockLog();
543 MergingReport mergingReport = ManifestMerger2
544 .newMerger(inputFile, mockLog, ManifestMerger2.MergeType.APPLICATION)
545 .withFeatures(ManifestMerger2.Invoker.Feature.NO_PLACEHOLDER_REPLACEMENT)
546 .merge();
547
548 assertTrue(mergingReport.getResult().isSuccess());
549 Document xmlDocument = parse(mergingReport.getMergedDocument(MergedManifestKind.MERGED));
550 assertEquals("${applicationId}",
551 xmlDocument.getElementsByTagName("manifest")
552 .item(0).getAttributes().getNamedItem("package").getNodeValue());
553 }
554
555 @Test
556 public void testReplaceInputStream() throws Exception {
557 // This test is identical to testNoPlaceholderReplacement but instead
558 // of reading from a string, we test the ManifestMerger's ability to
559 // supply a custom input stream
560 final String xml = ""
561 + "<manifest\n"
562 + " package=\"${applicationId}\""
563 + " xmlns:t=\"http://schemas.android.com/apk/res/android\">\n"
564 + " <activity t:name=\"activityOne\"/>\n"
565 + " <application t:name=\".applicationOne\" "
566 + " t:backupAgent=\"com.foo.example.myBackupAgent\"/>\n"
567 + "</manifest>";
568 String staleContent = "<manifest />";
569
570 // Note: disk content is wrong/stale; make sure we read the live content instead
571 File inputFile = inputAsFile("testNoPlaceHolderReplacement", staleContent);
572
573 MockLog mockLog = new MockLog();
574 MergingReport mergingReport = ManifestMerger2
575 .newMerger(inputFile, mockLog, ManifestMerger2.MergeType.APPLICATION)
576 .withFeatures(ManifestMerger2.Invoker.Feature.NO_PLACEHOLDER_REPLACEMENT)
577 .withFileStreamProvider(new ManifestMerger2.FileStreamProvider() {
578 @Override
579 protected InputStream getInputStream(@NonNull File file)
580 throws FileNotFoundException {
581 return new ByteArrayInputStream(xml.getBytes(Charsets.UTF_8));
582 }
583 })
584 .merge();
585
586 assertTrue(mergingReport.getResult().isSuccess());
587 Document xmlDocument = parse(mergingReport.getMergedDocument(MergedManifestKind.MERGED));
588 assertEquals("${applicationId}",
589 xmlDocument.getElementsByTagName("manifest")
590 .item(0).getAttributes().getNamedItem("package").getNodeValue());
591 }
592
593 @Test
594 public void testInstantRunReplacement() throws Exception {
595 String xml = ""
596 + "<manifest\n"
597 + " package=\"com.foo.bar\""
598 + " xmlns:t=\"http://schemas.android.com/apk/res/android\">\n"
599 + " <activity t:name=\"activityOne\"/>\n"
600 + " <application t:name=\".applicationOne\" "
601 + " t:backupAgent=\"com.foo.example.myBackupAgent\"/>\n"
602 + "</manifest>";
603
604 File inputFile = inputAsFile("testNoPlaceHolderReplacement", xml);
605
606 MockLog mockLog = new MockLog();
607 MergingReport mergingReport = ManifestMerger2
608 .newMerger(inputFile, mockLog, ManifestMerger2.MergeType.APPLICATION)
609 .withFeatures(ManifestMerger2.Invoker.Feature.INSTANT_RUN_REPLACEMENT)
610 .merge();
611
612 assertTrue(mergingReport.getResult().isSuccess());
613 Document xmlDocument = parse(mergingReport.getMergedDocument(MergedManifestKind.INSTANT_RUN));
614 assertEquals("com.foo.bar.applicationOne",
615 xmlDocument.getElementsByTagName(SdkConstants.TAG_APPLICATION)
616 .item(0).getAttributes().getNamedItem(
617 SdkConstants.ATTR_NAME).getNodeValue());
618 assertEquals(ManifestMerger2.BOOTSTRAP_APPLICATION,
619 xmlDocument.getElementsByTagName(SdkConstants.TAG_APPLICATION)
620 .item(0).getAttributes().getNamedItemNS(SdkConstants.ANDROID_URI,
621 SdkConstants.ATTR_NAME).getNodeValue());
622 }
623
624 @Test
625 public void testInstantRunReplacementWithNoAppName() throws Exception {
626 String xml = ""
627 + "<manifest\n"
628 + " package=\"com.foo.bar\""
629 + " xmlns:android=\"http://schemas.android.com/apk/res/android\">\n"
630 + " <activity android:name=\"activityOne\"/>\n"
631 + " <application android:backupAgent=\"com.foo.example.myBackupAgent\"/>\n"
632 + "</manifest>";
633
634 File inputFile = inputAsFile("testNoPlaceHolderReplacement", xml);
635
636 MockLog mockLog = new MockLog();
637 MergingReport mergingReport = ManifestMerger2
638 .newMerger(inputFile, mockLog, ManifestMerger2.MergeType.APPLICATION)
639 .withFeatures(ManifestMerger2.Invoker.Feature.INSTANT_RUN_REPLACEMENT)
640 .merge();
641
642 assertTrue(mergingReport.getResult().isSuccess());
643 Document xmlDocument = parse(mergingReport.getMergedDocument(MergedManifestKind.INSTANT_RUN));
644 assertEquals(ManifestMerger2.BOOTSTRAP_APPLICATION,
645 xmlDocument.getElementsByTagName(SdkConstants.TAG_APPLICATION)
646 .item(0).getAttributes().getNamedItemNS(SdkConstants.ANDROID_URI,
647 SdkConstants.ATTR_NAME).getNodeValue());
648 }
649
650 public static Optional<Element> getElementByTypeAndKey(Document xmlDocument, String nodeType, String key) {
651 NodeList elementsByTagName = xmlDocument.getElementsByTagName(nodeType);
652 for (int i = 0; i < elementsByTagName.getLength(); i++) {
653 Node item = elementsByTagName.item(i);
654 Node name = item.getAttributes().getNamedItemNS(SdkConstants.ANDROID_URI, "name");
655 if ((name == null && key == null) || (name != null && key.equals(name.getNodeValue()))) {
656 return Optional.of((Element) item);
657 }
658 }
659 return Optional.absent();
660 }
661
662 /**
663 * Utility method to save a {@link String} XML into a file.
664 */
665 private static File inputAsFile(String testName, String input) throws IOException {
666 File tmpFile = File.createTempFile(testName, ".xml");
667 tmpFile.deleteOnExit();
668 Files.write(input, tmpFile, Charsets.UTF_8);
669 return tmpFile;
670 }
671
672 private static void assertStringPresenceInLogRecords(MergingReport mergingReport, String s) {
673 for (MergingReport.Record record : mergingReport.getLoggingRecords()) {
674 if (record.toString().contains(s)) {
675 return;
676 }
677 }
678 // failed, dump the records
679 for (MergingReport.Record record : mergingReport.getLoggingRecords()) {
680 Logger.getAnonymousLogger().info(record.toString());
681 }
682 fail("could not find " + s + " in logging records");
683 }
684
685 private static Document parse(String xml)
686 throws IOException, SAXException, ParserConfigurationException {
687 return XmlUtils.parseDocument(xml, true /* namespaceAware */);
688 }
689 }