Debian Codegraph @

build-system/manifest-merger/src/test/java/com/android/manifmerger/XmlDocumentTest.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 com.android.manifmerger.MergingReport.Record.Severity.ERROR;
20
21 import com.android.SdkConstants;
22 import com.android.sdklib.SdkVersionInfo;
23 import com.android.sdklib.mock.MockLog;
24 import com.android.utils.ILogger;
25 import com.google.common.base.Optional;
26 import com.google.common.collect.ImmutableList;
27
28 import junit.framework.TestCase;
29
30 import org.mockito.Mock;
31 import org.mockito.MockitoAnnotations;
32 import org.w3c.dom.Document;
33 import org.w3c.dom.Element;
34 import org.w3c.dom.Node;
35 import org.w3c.dom.NodeList;
36 import org.xml.sax.ErrorHandler;
37 import org.xml.sax.InputSource;
38 import org.xml.sax.SAXException;
39 import org.xml.sax.SAXParseException;
40
41 import java.io.IOException;
42 import java.io.StringReader;
43 import java.util.List;
44 import java.util.logging.Logger;
45
46 import javax.xml.parsers.DocumentBuilder;
47 import javax.xml.parsers.DocumentBuilderFactory;
48 import javax.xml.parsers.ParserConfigurationException;
49
50 /**
51 * Tests for {@link com.android.manifmerger.XmlDocument}
52 */
53 public class XmlDocumentTest extends TestCase {
54
55 @Mock ILogger mLogger;
56
57 @Override
58 protected void setUp() throws Exception {
59 super.setUp();
60 MockitoAnnotations.initMocks(this);
61 }
62
63 public void testMergeableElementsIdentification()
64 throws ParserConfigurationException, SAXException, IOException {
65 String input = ""
66 + "<manifest\n"
67 + " xmlns:android=\"http://schemas.android.com/apk/res/android\"\n"
68 + " package=\"com.example.lib3\">\n"
69 + "\n"
70 + " <application android:label=\"@string/lib_name\" />\n"
71 + "\n"
72 + " <activity android:name=\"activityOne\" />\n"
73 + "\n"
74 + "</manifest>";
75
76 XmlDocument xmlDocument = TestUtils.xmlDocumentFromString(
77 TestUtils.sourceFile(getClass(), "testMergeableElementsIdentification()"), input);
78 ImmutableList<XmlElement> mergeableElements = xmlDocument.getRootNode().getMergeableElements();
79 assertEquals(2, mergeableElements.size());
80 }
81
82 public void testNamespaceEnabledElements()
83 throws ParserConfigurationException, SAXException, IOException {
84 String input = ""
85 + "<manifest\n"
86 + " xmlns:android=\"http://schemas.android.com/apk/res/android\"\n"
87 + " package=\"com.example.lib3\">\n"
88 + "\n"
89 + " <android:application android:label=\"@string/lib_name\" />\n"
90 + "\n"
91 + " <android:activity android:name=\"activityOne\" />\n"
92 + "\n"
93 + "</manifest>";
94
95 XmlDocument xmlDocument = TestUtils.xmlDocumentFromString(
96 TestUtils.sourceFile(getClass(), "testMergeableElementsIdentification()"), input);
97 ImmutableList<XmlElement> mergeableElements = xmlDocument.getRootNode().getMergeableElements();
98 assertEquals(2, mergeableElements.size());
99 assertEquals(ManifestModel.NodeTypes.APPLICATION, mergeableElements.get(0).getType());
100 assertEquals(ManifestModel.NodeTypes.ACTIVITY, mergeableElements.get(1).getType());
101 }
102
103 public void testMultipleNamespaceEnabledElements()
104 throws ParserConfigurationException, SAXException, IOException {
105 String input = ""
106 + "<android:manifest\n"
107 + " xmlns:android=\"http://schemas.android.com/apk/res/android\"\n"
108 + " xmlns:tools=\"http://schemas.android.com/tools\"\n"
109 + " xmlns:acme=\"http://acme.org/schemas\"\n"
110 + " package=\"com.example.lib3\">\n"
111 + "\n"
112 + " <android:application android:label=\"@string/lib_name\" \n"
113 + " tools:node=\"replace\" />\n"
114 + " <acme:custom-tag android:label=\"@string/lib_name\" />\n"
115 + " <acme:application acme:label=\"@string/lib_name\" />\n"
116 + "\n"
117 + "</android:manifest>";
118
119 XmlDocument xmlDocument = TestUtils.xmlDocumentFromString(
120 TestUtils.sourceFile(getClass(), "testMergeableElementsIdentification()"), input);
121 ImmutableList<XmlElement> mergeableElements = xmlDocument.getRootNode().getMergeableElements();
122 assertEquals(3, mergeableElements.size());
123 assertEquals(ManifestModel.NodeTypes.APPLICATION, mergeableElements.get(0).getType());
124 assertEquals(ManifestModel.NodeTypes.CUSTOM, mergeableElements.get(1).getType());
125 assertEquals(ManifestModel.NodeTypes.CUSTOM, mergeableElements.get(2).getType());
126
127 }
128
129 public void testGetXmlNodeByTypeAndKey()
130 throws ParserConfigurationException, SAXException, IOException {
131 String input = ""
132 + "<manifest\n"
133 + " xmlns:android=\"http://schemas.android.com/apk/res/android\"\n"
134 + " package=\"com.example.lib3\">\n"
135 + "\n"
136 + " <application android:label=\"@string/lib_name\" />\n"
137 + "\n"
138 + " <activity android:name=\"activityOne\" />\n"
139 + "\n"
140 + "</manifest>";
141
142 XmlDocument xmlDocument = TestUtils.xmlDocumentFromString(
143 TestUtils.sourceFile(getClass(), "testGetXmlNodeByTypeAndKey()"), input);
144 assertTrue(xmlDocument.getRootNode().getNodeByTypeAndKey(
145 ManifestModel.NodeTypes.ACTIVITY, "com.example.lib3.activityOne").isPresent());
146 assertFalse(xmlDocument.getRootNode().getNodeByTypeAndKey(
147 ManifestModel.NodeTypes.ACTIVITY, "noName").isPresent());
148 }
149
150 public void testSimpleMerge()
151 throws ParserConfigurationException, SAXException, IOException {
152 String main = ""
153 + "<manifest\n"
154 + " xmlns:android=\"http://schemas.android.com/apk/res/android\"\n"
155 + " package=\"com.example.lib3\">\n"
156 + "\n"
157 + " <application android:label=\"@string/lib_name\" />\n"
158 + "\n"
159 + "</manifest>";
160 String library = ""
161 + "<manifest\n"
162 + " xmlns:android=\"http://schemas.android.com/apk/res/android\"\n"
163 + " package=\"com.example.lib3\">\n"
164 + "\n"
165 + " <activity android:name=\"activityOne\" />\n"
166 + "\n"
167 + "</manifest>";
168
169 XmlDocument mainDocument = TestUtils.xmlDocumentFromString(
170 TestUtils.sourceFile(getClass(), "testSimpleMerge()"), main);
171 XmlDocument libraryDocument = TestUtils.xmlLibraryFromString(
172 TestUtils.sourceFile(getClass(), "testSimpleMerge()"), library);
173 MergingReport.Builder mergingReportBuilder = new MergingReport.Builder(mLogger);
174 Optional<XmlDocument> mergedDocument =
175 mainDocument.merge(libraryDocument, mergingReportBuilder);
176
177 assertTrue(mergedDocument.isPresent());
178 Logger.getAnonymousLogger().info(mergedDocument.get().prettyPrint());
179 assertTrue(mergedDocument.get().getRootNode().getNodeByTypeAndKey(
180 ManifestModel.NodeTypes.APPLICATION, null).isPresent());
181 Optional<XmlElement> activityOne = mergedDocument.get()
182 .getRootNode().getNodeByTypeAndKey(ManifestModel.NodeTypes.ACTIVITY,
183 "com.example.lib3.activityOne");
184 assertTrue(activityOne.isPresent());
185 }
186
187 public void testDiff1()
188 throws Exception {
189 String main = ""
190 + "<manifest\n"
191 + " xmlns:android=\"http://schemas.android.com/apk/res/android\"\n"
192 + " package=\"com.example.lib3\">\n"
193 + "\n"
194 + " <application android:label=\"@string/lib_name\" />\n"
195 + "\n"
196 + "</manifest>";
197 String library = ""
198 + "<manifest\n"
199 + " xmlns:android=\"http://schemas.android.com/apk/res/android\"\n"
200 + " package=\"com.example.lib3\">\n"
201 + "\n"
202 + " <activity android:name=\"activityOne\" />\n"
203 + "\n"
204 + "</manifest>";
205
206 XmlDocument mainDocument = TestUtils.xmlDocumentFromString(
207 TestUtils.sourceFile(getClass(), "testDiff1()"), main);
208 XmlDocument libraryDocument = TestUtils.xmlLibraryFromString(
209 TestUtils.sourceFile(getClass(), "testDiff1()"), library);
210 assertTrue(mainDocument.compareTo(libraryDocument).isPresent());
211 }
212
213 public void testDiff2()
214 throws Exception {
215 String main = ""
216 + "<manifest\n"
217 + " xmlns:android=\"http://schemas.android.com/apk/res/android\"\n"
218 + " package=\"com.example.lib3\">\n"
219 + "\n"
220 + " <application android:label=\"@string/lib_name\" />\n"
221 + " <activity android:name=\"activityOne\" />\n"
222 + "\n"
223 + "\n"
224 + "</manifest>";
225 String library = ""
226 + "<manifest\n"
227 + " xmlns:android=\"http://schemas.android.com/apk/res/android\"\n"
228 + " package=\"com.example.lib3\">\n"
229 + "\n"
230 + " <activity android:name=\"activityOne\" />\n"
231 + " <application android:label=\"@string/lib_name\" />\n"
232 + "\n"
233 + "</manifest>";
234
235 XmlDocument mainDocument = TestUtils.xmlDocumentFromString(
236 TestUtils.sourceFile(getClass(), "testDiff2()"), main);
237 XmlDocument libraryDocument = TestUtils.xmlLibraryFromString(
238 TestUtils.sourceFile(getClass(), "testDiff2()"), library);
239 assertFalse(mainDocument.compareTo(libraryDocument).isPresent());
240 }
241
242 public void testDiff3()
243 throws Exception {
244 String main = ""
245 + "<manifest\n"
246 + " xmlns:android=\"http://schemas.android.com/apk/res/android\"\n"
247 + " package=\"com.example.lib3\">\n"
248 + "\n"
249 + " <application android:label=\"@string/lib_name\" />\n"
250 + " <!-- some comment that should be ignored -->\n"
251 + " <activity android:name=\"activityOne\" />\n"
252 + "\n"
253 + "\n"
254 + "</manifest>";
255 String library = ""
256 + "<manifest\n"
257 + " xmlns:android=\"http://schemas.android.com/apk/res/android\"\n"
258 + " package=\"com.example.lib3\">\n"
259 + "\n"
260 + " <!-- some comment that should also be ignored -->\n"
261 + " <activity android:name=\"activityOne\" />\n"
262 + " <application android:label=\"@string/lib_name\" />\n"
263 + "\n"
264 + "</manifest>";
265
266 XmlDocument mainDocument = TestUtils.xmlDocumentFromString(
267 TestUtils.sourceFile(getClass(), "testDiff3()"), main);
268 XmlDocument libraryDocument = TestUtils.xmlLibraryFromString(
269 TestUtils.sourceFile(getClass(), "testDiff3()"), library);
270 assertFalse(mainDocument.compareTo(libraryDocument).isPresent());
271 }
272
273 public void testWriting() throws ParserConfigurationException, SAXException, IOException {
274 String input = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
275 + "<manifest xmlns:x=\"http://schemas.android.com/apk/res/android\"\n"
276 + " xmlns:y=\"http://schemas.android.com/apk/res/android/tools\"\n"
277 + " package=\"com.example.lib3\" >\n"
278 + "\n"
279 + " <application\n"
280 + " x:label=\"@string/lib_name\"\n"
281 + " y:node=\"replace\" />\n"
282 + "\n"
283 + "</manifest>";
284
285 XmlDocument xmlDocument = TestUtils.xmlDocumentFromString(
286 TestUtils.sourceFile(getClass(), "testWriting()"), input);
287 assertEquals(input, xmlDocument.prettyPrint());
288 }
289
290 public void testCustomElements()
291 throws ParserConfigurationException, SAXException, IOException {
292 String main = ""
293 + "<manifest\n"
294 + " xmlns:android=\"http://schemas.android.com/apk/res/android\"\n"
295 + " package=\"com.example.lib3\">\n"
296 + "\n"
297 + " <fantasy android:name=\"fantasyOne\" \n"
298 + " no-ns-attribute=\"no-ns\" >\n"
299 + " </fantasy>\n"
300 + " <application android:label=\"@string/lib_name\" />\n"
301 + "\n"
302 + "</manifest>";
303 String library = ""
304 + "<manifest\n"
305 + " xmlns:android=\"http://schemas.android.com/apk/res/android\"\n"
306 + " xmlns:acme=\"http://acme.org/schemas\"\n"
307 + " package=\"com.example.lib3\">\n"
308 + "\n"
309 + " <activity android:name=\"activityOne\" />\n"
310 + " <fantasy android:name=\"fantasyTwo\" \n"
311 + " no-ns-attribute=\"no-ns\" >\n"
312 + " </fantasy>\n"
313 + " <acme:another acme:name=\"anotherOne\" \n"
314 + " acme:ns-attribute=\"ns-value\" >\n"
315 + " <some-child acme:child-attr=\"foo\" /> \n"
316 + " </acme:another>\n"
317 + "\n"
318 + "</manifest>";
319
320 XmlDocument mainDocument = TestUtils.xmlDocumentFromString(
321 TestUtils.sourceFile(getClass(), "main"), main);
322 XmlDocument libraryDocument = TestUtils.xmlLibraryFromString(
323 TestUtils.sourceFile(getClass(), "library"), library);
324 MergingReport.Builder mergingReportBuilder = new MergingReport.Builder(mLogger);
325 Optional<XmlDocument> mergedDocument =
326 mainDocument.merge(libraryDocument, mergingReportBuilder);
327
328 assertTrue(mergedDocument.isPresent());
329 XmlElement rootNode = mergedDocument.get().getRootNode();
330 assertTrue(rootNode.getNodeByTypeAndKey(
331 ManifestModel.NodeTypes.APPLICATION, null).isPresent());
332 Optional<XmlElement> activityOne = rootNode
333 .getNodeByTypeAndKey(ManifestModel.NodeTypes.ACTIVITY,
334 "com.example.lib3.activityOne");
335 assertTrue(activityOne.isPresent());
336
337 boolean foundFantasyOne = false;
338 boolean foundFantasyTwo = false;
339 boolean foundAnother = false;
340 NodeList childNodes = rootNode.getXml().getChildNodes();
341 for (int i =0; i < childNodes.getLength(); i++) {
342 Node item = childNodes.item(i);
343 if (item.getNodeName().equals("fantasy")) {
344 String name = ((Element) item).getAttributeNS(SdkConstants.ANDROID_URI, "name");
345 if (name.equals("fantasyOne"))
346 foundFantasyOne = true;
347 if (name.equals("fantasyTwo"))
348 foundFantasyTwo = true;
349 }
350 if (item.getNodeName().equals("acme:another")) {
351 foundAnother = true;
352 }
353 }
354 assertTrue(foundAnother);
355 assertTrue(foundFantasyOne);
356 assertTrue(foundFantasyTwo);
357
358 Element validated = validate(mergedDocument.get().prettyPrint());
359
360 // make sure acme: namespace is defined.
361 Node namedItem = validated.getAttributes().getNamedItem("xmlns:acme");
362 assertEquals(namedItem.getNodeValue(), "http://acme.org/schemas");
363 }
364
365 public void testMultipleCustomElements()
366 throws ParserConfigurationException, SAXException, IOException {
367 String main = ""
368 + "<manifest\n"
369 + " xmlns:android=\"http://schemas.android.com/apk/res/android\"\n"
370 + " package=\"com.example.lib3\">\n"
371 + "\n"
372 + " <application android:label=\"@string/lib_name\" />\n"
373 + "\n"
374 + "</manifest>";
375 String library = ""
376 + "<manifest\n"
377 + " xmlns:android=\"http://schemas.android.com/apk/res/android\"\n"
378 + " xmlns:acme=\"http://acme.org/schemas\"\n"
379 + " xmlns:acme2=\"http://acme2.org/schemas\"\n"
380 + " package=\"com.example.lib3\">\n"
381 + "\n"
382 + " <activity android:name=\"activityOne\" />\n"
383 + " <acme:another acme:name=\"anotherOne\" \n"
384 + " acme:ns-attribute=\"ns-value\" >\n"
385 + " <acme:some-child acme:child-attr=\"foo\" /> \n"
386 + " </acme:another>\n"
387 + " <acme:another2 acme:name=\"anotherOne\" \n"
388 + " acme:ns-attribute=\"ns-value\" >\n"
389 + " <acme:some-child acme:child-attr=\"foo\" /> \n"
390 + " </acme:another2>\n"
391 + " <acme2:another acme2:name=\"anotherOne\" \n"
392 + " acme2:ns-attribute=\"ns-value\" >\n"
393 + " <acme2:some-child acme2:child-attr=\"foo\" /> \n"
394 + " </acme2:another>\n"
395 + " <acme2:another2 acme2:name=\"anotherOne\" \n"
396 + " acme2:ns-attribute=\"ns-value\" >\n"
397 + " <acme2:some-child acme2:child-attr=\"foo\" /> \n"
398 + " </acme2:another2>\n"
399 + "\n"
400 + "</manifest>";
401
402 XmlDocument mainDocument = TestUtils.xmlDocumentFromString(
403 TestUtils.sourceFile(getClass(), "main"), main);
404 XmlDocument libraryDocument = TestUtils.xmlLibraryFromString(
405 TestUtils.sourceFile(getClass(), "library"), library);
406 MergingReport.Builder mergingReportBuilder = new MergingReport.Builder(mLogger);
407 Optional<XmlDocument> mergedDocument =
408 mainDocument.merge(libraryDocument, mergingReportBuilder);
409
410 assertTrue(mergedDocument.isPresent());
411 XmlElement rootNode = mergedDocument.get().getRootNode();
412 assertTrue(rootNode.getNodeByTypeAndKey(
413 ManifestModel.NodeTypes.APPLICATION, null).isPresent());
414 Optional<XmlElement> activityOne = rootNode
415 .getNodeByTypeAndKey(ManifestModel.NodeTypes.ACTIVITY,
416 "com.example.lib3.activityOne");
417 assertTrue(activityOne.isPresent());
418
419 Element validated = validate(mergedDocument.get().prettyPrint());
420
421 // make sure acme: and acme2: namespaces are defined.
422 Node namedItem = validated.getAttributes().getNamedItem("xmlns:acme");
423 assertEquals(namedItem.getNodeValue(), "http://acme.org/schemas");
424 namedItem = validated.getAttributes().getNamedItem("xmlns:acme2");
425 assertEquals(namedItem.getNodeValue(), "http://acme2.org/schemas");
426
427 // check we have all the children we expected.
428 int elementsNumber = 0;
429 for (int i = 0; i < validated.getChildNodes().getLength(); i++) {
430 Node item = validated.getChildNodes().item(i);
431 if (item instanceof Element) {
432 elementsNumber++;
433 }
434 }
435 assertEquals(6, elementsNumber);
436 }
437
438 public void testIllegalLibraryVersionMerge()
439 throws ParserConfigurationException, SAXException, IOException {
440 String main = ""
441 + "<manifest\n"
442 + " xmlns:android=\"http://schemas.android.com/apk/res/android\"\n"
443 + " package=\"com.example.lib3\">\n"
444 + "\n"
445 + " <application android:label=\"@string/lib_name\" />\n"
446 + " <uses-sdk android:minSdkVersion=\"4\"/>\n"
447 + "\n"
448 + "</manifest>";
449 String library = ""
450 + "<manifest\n"
451 + " xmlns:android=\"http://schemas.android.com/apk/res/android\"\n"
452 + " xmlns:acme=\"http://acme.org/schemas\"\n"
453 + " package=\"com.example.lib3\">\n"
454 + "\n"
455 + " <activity android:name=\"activityOne\" />\n"
456 + " <uses-sdk android:minSdkVersion=\"19\"/>\n"
457 + "\n"
458 + "</manifest>";
459
460 XmlDocument mainDocument = TestUtils.xmlDocumentFromString(
461 TestUtils.sourceFile(getClass(), "main"), main);
462 XmlDocument libraryDocument = TestUtils.xmlLibraryFromString(
463 TestUtils.sourceFile(getClass(), "library"), library);
464 MergingReport.Builder mergingReportBuilder = new MergingReport.Builder(mLogger);
465 Optional<XmlDocument> mergedDocument =
466 mainDocument.merge(libraryDocument, mergingReportBuilder);
467
468 assertFalse(mergedDocument.isPresent());
469 MergingReport mergingReport = mergingReportBuilder.build();
470 assertEquals(1, mergingReport.getLoggingRecords().size());
471 assertTrue(mergingReport.getLoggingRecords().get(0).getSeverity() == ERROR);
472 assertTrue(mergingReport.getLoggingRecords().get(0).toString().contains(
473 "uses-sdk:minSdkVersion 4"));
474 }
475
476 public void testNoMergingNecessary()
477 throws ParserConfigurationException, SAXException, IOException {
478 String main = ""
479 + "<manifest\n"
480 + " xmlns:android=\"http://schemas.android.com/apk/res/android\"\n"
481 + " package=\"com.example.lib3\">\n"
482 + "\n"
483 + " <application android:label=\"@string/lib_name\" />\n"
484 + " <uses-sdk android:minSdkVersion=\"19\"/>\n"
485 + "\n"
486 + "</manifest>";
487 String library = ""
488 + "<manifest\n"
489 + " xmlns:android=\"http://schemas.android.com/apk/res/android\"\n"
490 + " xmlns:acme=\"http://acme.org/schemas\"\n"
491 + " package=\"com.example.lib3\">\n"
492 + "\n"
493 + " <activity android:name=\"activityOne\" />\n"
494 + " <uses-sdk android:minSdkVersion=\"19\"/>\n"
495 + "\n"
496 + "</manifest>";
497
498 XmlDocument mainDocument = TestUtils.xmlDocumentFromString(
499 TestUtils.sourceFile(getClass(), "main"), main);
500 XmlDocument libraryDocument = TestUtils.xmlLibraryFromString(
501 TestUtils.sourceFile(getClass(), "library"), library);
502 MergingReport.Builder mergingReportBuilder = new MergingReport.Builder(mLogger);
503 Optional<XmlDocument> mergedDocument =
504 mainDocument.merge(libraryDocument, mergingReportBuilder);
505
506 assertTrue(mergedDocument.isPresent());
507 XmlDocument xmlDocument = mergedDocument.get();
508 assertFalse(xmlDocument.getByTypeAndKey(ManifestModel.NodeTypes.USES_PERMISSION,
509 "android.permission.WRITE_EXTERNAL_STORAGE").isPresent());
510 assertFalse(xmlDocument.getByTypeAndKey(ManifestModel.NodeTypes.USES_PERMISSION,
511 "android.permission.READ_EXTERNAL_STORAGE").isPresent());
512 assertFalse(xmlDocument.getByTypeAndKey(ManifestModel.NodeTypes.USES_PERMISSION,
513 "android.permission.READ_PHONE_STATE").isPresent());
514 assertFalse(xmlDocument.getByTypeAndKey(ManifestModel.NodeTypes.USES_PERMISSION,
515 "android.permission.READ_CALL_LOG").isPresent());
516 assertFalse(xmlDocument.getByTypeAndKey(ManifestModel.NodeTypes.USES_PERMISSION,
517 "android.permission.WRITE_CALL_LOG").isPresent());
518 }
519
520 public void testNoUsesSdkPresence()
521 throws ParserConfigurationException, SAXException, IOException {
522 String main = ""
523 + "<manifest\n"
524 + " xmlns:android=\"http://schemas.android.com/apk/res/android\"\n"
525 + " package=\"com.example.lib3\">\n"
526 + "\n"
527 + " <application android:label=\"@string/lib_name\" />\n"
528 + "\n"
529 + "</manifest>";
530 String library = ""
531 + "<manifest\n"
532 + " xmlns:android=\"http://schemas.android.com/apk/res/android\"\n"
533 + " xmlns:acme=\"http://acme.org/schemas\"\n"
534 + " package=\"com.example.lib3\">\n"
535 + "\n"
536 + " <activity android:name=\"activityOne\" />\n"
537 + "\n"
538 + "</manifest>";
539
540 XmlDocument mainDocument = TestUtils.xmlDocumentFromString(
541 TestUtils.sourceFile(getClass(), "main"), main);
542 XmlDocument libraryDocument = TestUtils.xmlLibraryFromString(
543 TestUtils.sourceFile(getClass(), "library"), library);
544 MergingReport.Builder mergingReportBuilder = new MergingReport.Builder(mLogger);
545 Optional<XmlDocument> mergedDocument =
546 mainDocument.merge(libraryDocument, mergingReportBuilder);
547
548 assertTrue(mergedDocument.isPresent());
549 XmlDocument xmlDocument = mergedDocument.get();
550 assertFalse(xmlDocument.getByTypeAndKey(ManifestModel.NodeTypes.USES_PERMISSION,
551 "android.permission.WRITE_EXTERNAL_STORAGE").isPresent());
552 assertFalse(xmlDocument.getByTypeAndKey(ManifestModel.NodeTypes.USES_PERMISSION,
553 "android.permission.READ_EXTERNAL_STORAGE").isPresent());
554 assertFalse(xmlDocument.getByTypeAndKey(ManifestModel.NodeTypes.USES_PERMISSION,
555 "android.permission.READ_PHONE_STATE").isPresent());
556 assertFalse(xmlDocument.getByTypeAndKey(ManifestModel.NodeTypes.USES_PERMISSION,
557 "android.permission.READ_CALL_LOG").isPresent());
558 assertFalse(xmlDocument.getByTypeAndKey(ManifestModel.NodeTypes.USES_PERMISSION,
559 "android.permission.WRITE_CALL_LOG").isPresent());
560 }
561
562 public void testGlEsVersionFromFlavor()
563 throws ParserConfigurationException, SAXException, IOException {
564 String flavor = ""
565 + "<manifest\n"
566 + " xmlns:android=\"http://schemas.android.com/apk/res/android\"\n"
567 + " package=\"com.example.lib3\">\n"
568 + "\n"
569 + " <uses-feature\n"
570 + " android:name=\"android.hardware.camera\"\n"
571 + " android:glEsVersion=\"0x00020000\"\n"
572 + " android:required=\"true\" />"
573 + "\n"
574 + " <application android:label=\"@string/lib_name\" />\n"
575 + "\n"
576 + "</manifest>";
577 String main = ""
578 + "<manifest\n"
579 + " xmlns:android=\"http://schemas.android.com/apk/res/android\"\n"
580 + " xmlns:acme=\"http://acme.org/schemas\"\n"
581 + " package=\"com.example.lib3\">\n"
582 + "\n"
583 + " <activity android:name=\"activityOne\" />\n"
584 + "\n"
585 + "</manifest>";
586
587 XmlDocument flavorDocument = TestUtils.xmlDocumentFromString(
588 TestUtils.sourceFile(getClass(), "flavor"), flavor);
589 XmlDocument mainDocument = TestUtils.xmlLibraryFromString(
590 TestUtils.sourceFile(getClass(), "main"), main);
591 MergingReport.Builder mergingReportBuilder = new MergingReport.Builder(mLogger);
592 Optional<XmlDocument> mergedDocument =
593 flavorDocument.merge(mainDocument, mergingReportBuilder);
594
595 assertTrue(mergedDocument.isPresent());
596 System.out.println(mergedDocument.get().prettyPrint());
597 XmlDocument xmlDocument = mergedDocument.get();
598 Optional<XmlElement> usesFeature = xmlDocument
599 .getByTypeAndKey(ManifestModel.NodeTypes.USES_FEATURE,
600 "android.hardware.camera");
601 assertTrue(usesFeature.isPresent());
602 Optional<XmlAttribute> glEsVersion = usesFeature.get()
603 .getAttribute(XmlNode.fromXmlName("android:glEsVersion"));
604 assertTrue(glEsVersion.isPresent());
605 assertEquals("0x00020000", glEsVersion.get().getValue());
606 }
607
608
609 public void testNoUsesSdkPresenceInMain()
610 throws ParserConfigurationException, SAXException, IOException {
611 String main = ""
612 + "<manifest\n"
613 + " xmlns:android=\"http://schemas.android.com/apk/res/android\"\n"
614 + " package=\"com.example.lib3\">\n"
615 + "\n"
616 + " <application android:label=\"@string/lib_name\" />\n"
617 + "\n"
618 + "</manifest>";
619 String library = ""
620 + "<manifest\n"
621 + " xmlns:android=\"http://schemas.android.com/apk/res/android\"\n"
622 + " xmlns:acme=\"http://acme.org/schemas\"\n"
623 + " package=\"com.example.lib3\">\n"
624 + "\n"
625 + " <activity android:name=\"activityOne\" />\n"
626 + " <uses-sdk android:minSdkVersion=\"3\"/>\n"
627 + "\n"
628 + "</manifest>";
629
630 XmlDocument mainDocument = TestUtils.xmlDocumentFromString(
631 TestUtils.sourceFile(getClass(), "main"), main);
632 XmlDocument libraryDocument = TestUtils.xmlLibraryFromString(
633 TestUtils.sourceFile(getClass(), "library"), library);
634 MergingReport.Builder mergingReportBuilder = new MergingReport.Builder(mLogger);
635 Optional<XmlDocument> mergedDocument =
636 mainDocument.merge(libraryDocument, mergingReportBuilder);
637
638 assertFalse(mergedDocument.isPresent());
639 }
640
641 public void testNoUsesSdkPresenceInLibrary()
642 throws ParserConfigurationException, SAXException, IOException {
643 String main = ""
644 + "<manifest\n"
645 + " xmlns:android=\"http://schemas.android.com/apk/res/android\"\n"
646 + " package=\"com.example.lib3\">\n"
647 + "\n"
648 + " <application android:label=\"@string/lib_name\" />\n"
649 + " <uses-sdk android:minSdkVersion=\"3\"/>\n"
650 + "\n"
651 + "</manifest>";
652 String library = ""
653 + "<manifest\n"
654 + " xmlns:android=\"http://schemas.android.com/apk/res/android\"\n"
655 + " xmlns:acme=\"http://acme.org/schemas\"\n"
656 + " package=\"com.example.lib3\">\n"
657 + "\n"
658 + " <activity android:name=\"activityOne\" />\n"
659 + "\n"
660 + "</manifest>";
661
662 XmlDocument mainDocument = TestUtils.xmlDocumentFromString(
663 TestUtils.sourceFile(getClass(), "main"), main);
664 XmlDocument libraryDocument = TestUtils.xmlLibraryFromString(
665 TestUtils.sourceFile(getClass(), "library"), library);
666 MergingReport.Builder mergingReportBuilder = new MergingReport.Builder(mLogger);
667 Optional<XmlDocument> mergedDocument =
668 mainDocument.merge(libraryDocument, mergingReportBuilder);
669
670 assertTrue(mergedDocument.isPresent());
671 XmlDocument xmlDocument = mergedDocument.get();
672 assertFalse(xmlDocument.getByTypeAndKey(ManifestModel.NodeTypes.USES_PERMISSION,
673 "android.permission.WRITE_EXTERNAL_STORAGE").isPresent());
674 assertFalse(xmlDocument.getByTypeAndKey(ManifestModel.NodeTypes.USES_PERMISSION,
675 "android.permission.READ_EXTERNAL_STORAGE").isPresent());
676 assertFalse(xmlDocument.getByTypeAndKey(ManifestModel.NodeTypes.USES_PERMISSION,
677 "android.permission.READ_PHONE_STATE").isPresent());
678 assertFalse(xmlDocument.getByTypeAndKey(ManifestModel.NodeTypes.USES_PERMISSION,
679 "android.permission.READ_CALL_LOG").isPresent());
680 assertFalse(xmlDocument.getByTypeAndKey(ManifestModel.NodeTypes.USES_PERMISSION,
681 "android.permission.WRITE_CALL_LOG").isPresent());
682 }
683
684 public void testLibraryVersion3Merge()
685 throws ParserConfigurationException, SAXException, IOException {
686 String main = ""
687 + "<manifest\n"
688 + " xmlns:android=\"http://schemas.android.com/apk/res/android\"\n"
689 + " package=\"com.example.lib3\">\n"
690 + "\n"
691 + " <application android:label=\"@string/lib_name\" />\n"
692 + " <uses-sdk android:minSdkVersion=\"19\"/>\n"
693 + "\n"
694 + "</manifest>";
695 String library = ""
696 + "<manifest\n"
697 + " xmlns:android=\"http://schemas.android.com/apk/res/android\"\n"
698 + " xmlns:acme=\"http://acme.org/schemas\"\n"
699 + " package=\"com.example.lib3\">\n"
700 + "\n"
701 + " <activity android:name=\"activityOne\" />\n"
702 + " <uses-sdk android:minSdkVersion=\"3\"/>\n"
703 + "\n"
704 + "</manifest>";
705
706 XmlDocument mainDocument = TestUtils.xmlDocumentFromString(
707 TestUtils.sourceFile(getClass(), "main"), main);
708 XmlDocument libraryDocument = TestUtils.xmlLibraryFromString(
709 TestUtils.sourceFile(getClass(), "library"), library);
710 MergingReport.Builder mergingReportBuilder = new MergingReport.Builder(mLogger);
711 Optional<XmlDocument> mergedDocument =
712 mainDocument.merge(libraryDocument, mergingReportBuilder);
713
714 assertTrue(mergedDocument.isPresent());
715 XmlDocument xmlDocument = mergedDocument.get();
716 assertTrue(xmlDocument.getByTypeAndKey(ManifestModel.NodeTypes.USES_PERMISSION,
717 "android.permission.WRITE_EXTERNAL_STORAGE").isPresent());
718 assertTrue(xmlDocument.getByTypeAndKey(ManifestModel.NodeTypes.USES_PERMISSION,
719 "android.permission.READ_EXTERNAL_STORAGE").isPresent());
720 assertTrue(xmlDocument.getByTypeAndKey(ManifestModel.NodeTypes.USES_PERMISSION,
721 "android.permission.READ_PHONE_STATE").isPresent());
722 assertFalse(xmlDocument.getByTypeAndKey(ManifestModel.NodeTypes.USES_PERMISSION,
723 "android.permission.READ_CALL_LOG").isPresent());
724 assertFalse(xmlDocument.getByTypeAndKey(ManifestModel.NodeTypes.USES_PERMISSION,
725 "android.permission.WRITE_CALL_LOG").isPresent());
726
727 // check records.
728 Actions actions = mergingReportBuilder.getActionRecorder().build();
729 XmlElement xmlElement = xmlDocument.getByTypeAndKey(ManifestModel.NodeTypes.USES_PERMISSION,
730 "android.permission.WRITE_EXTERNAL_STORAGE").get();
731 ImmutableList<Actions.NodeRecord> nodeRecords = actions
732 .getNodeRecords(XmlNode.NodeKey.fromXml(xmlElement.getXml()));
733 assertEquals(1, nodeRecords.size());
734 assertEquals(nodeRecords.iterator().next().mReason,
735 "com.example.lib3 has a targetSdkVersion < 4");
736 }
737
738 public void testLibraryVersion10Merge()
739 throws ParserConfigurationException, SAXException, IOException {
740 String main = ""
741 + "<manifest\n"
742 + " xmlns:android=\"http://schemas.android.com/apk/res/android\"\n"
743 + " package=\"com.example.lib3\">\n"
744 + "\n"
745 + " <application android:label=\"@string/lib_name\" />\n"
746 + " <uses-sdk android:minSdkVersion=\"19\"/>\n"
747 + "\n"
748 + "</manifest>";
749 String library = ""
750 + "<manifest\n"
751 + " xmlns:android=\"http://schemas.android.com/apk/res/android\"\n"
752 + " xmlns:acme=\"http://acme.org/schemas\"\n"
753 + " package=\"com.example.lib3\">\n"
754 + "\n"
755 + " <activity android:name=\"activityOne\" />\n"
756 + " <uses-sdk android:minSdkVersion=\"10\"/>\n"
757 + "\n"
758 + "</manifest>";
759
760 XmlDocument mainDocument = TestUtils.xmlDocumentFromString(
761 TestUtils.sourceFile(getClass(), "main"), main);
762 XmlDocument libraryDocument = TestUtils.xmlLibraryFromString(
763 TestUtils.sourceFile(getClass(), "library"), library);
764 MergingReport.Builder mergingReportBuilder = new MergingReport.Builder(mLogger);
765 Optional<XmlDocument> mergedDocument =
766 mainDocument.merge(libraryDocument, mergingReportBuilder);
767
768 assertTrue(mergedDocument.isPresent());
769 XmlDocument xmlDocument = mergedDocument.get();
770 assertFalse(xmlDocument.getByTypeAndKey(ManifestModel.NodeTypes.USES_PERMISSION,
771 "android.permission.WRITE_EXTERNAL_STORAGE").isPresent());
772 assertFalse(xmlDocument.getByTypeAndKey(ManifestModel.NodeTypes.USES_PERMISSION,
773 "android.permission.READ_EXTERNAL_STORAGE").isPresent());
774 assertFalse(xmlDocument.getByTypeAndKey(ManifestModel.NodeTypes.USES_PERMISSION,
775 "android.permission.READ_PHONE_STATE").isPresent());
776 assertFalse(xmlDocument.getByTypeAndKey(ManifestModel.NodeTypes.USES_PERMISSION,
777 "android.permission.READ_CALL_LOG").isPresent());
778 assertFalse(xmlDocument.getByTypeAndKey(ManifestModel.NodeTypes.USES_PERMISSION,
779 "android.permission.WRITE_CALL_LOG").isPresent());
780 }
781
782 public void testLibraryVersion3MergeWithContacts()
783 throws ParserConfigurationException, SAXException, IOException {
784 String main = ""
785 + "<manifest\n"
786 + " xmlns:android=\"http://schemas.android.com/apk/res/android\"\n"
787 + " package=\"com.example.lib3\">\n"
788 + "\n"
789 + " <application android:label=\"@string/lib_name\" />\n"
790 + " <uses-sdk android:minSdkVersion=\"19\"/>\n"
791
792 + "\n"
793 + "</manifest>";
794 String library = ""
795 + "<manifest\n"
796 + " xmlns:android=\"http://schemas.android.com/apk/res/android\"\n"
797 + " xmlns:acme=\"http://acme.org/schemas\"\n"
798 + " package=\"com.example.lib3\">\n"
799 + "\n"
800 + " <activity android:name=\"activityOne\" />\n"
801 + " <uses-sdk android:minSdkVersion=\"3\"/>\n"
802 + " <uses-permission android:name=\"android.permission.READ_CONTACTS\"/>\n"
803 + " <uses-permission android:name=\"android.permission.WRITE_CONTACTS\"/>\n"
804 + "\n"
805 + "</manifest>";
806
807 XmlDocument mainDocument = TestUtils.xmlDocumentFromString(
808 TestUtils.sourceFile(getClass(), "main"), main);
809 XmlDocument libraryDocument = TestUtils.xmlLibraryFromString(
810 TestUtils.sourceFile(getClass(), "library"), library);
811 MergingReport.Builder mergingReportBuilder = new MergingReport.Builder(mLogger);
812 Optional<XmlDocument> mergedDocument =
813 mainDocument.merge(libraryDocument, mergingReportBuilder);
814
815 assertTrue(mergedDocument.isPresent());
816 XmlDocument xmlDocument = mergedDocument.get();
817 assertTrue(xmlDocument.getByTypeAndKey(ManifestModel.NodeTypes.USES_PERMISSION,
818 "android.permission.WRITE_EXTERNAL_STORAGE").isPresent());
819 assertTrue(xmlDocument.getByTypeAndKey(ManifestModel.NodeTypes.USES_PERMISSION,
820 "android.permission.READ_EXTERNAL_STORAGE").isPresent());
821 assertTrue(xmlDocument.getByTypeAndKey(ManifestModel.NodeTypes.USES_PERMISSION,
822 "android.permission.READ_PHONE_STATE").isPresent());
823 assertTrue(xmlDocument.getByTypeAndKey(ManifestModel.NodeTypes.USES_PERMISSION,
824 "android.permission.READ_CALL_LOG").isPresent());
825 assertTrue(xmlDocument.getByTypeAndKey(ManifestModel.NodeTypes.USES_PERMISSION,
826 "android.permission.WRITE_CALL_LOG").isPresent());
827
828 // check records.
829 Actions actions = mergingReportBuilder.getActionRecorder().build();
830 XmlElement xmlElement = xmlDocument.getByTypeAndKey(ManifestModel.NodeTypes.USES_PERMISSION,
831 "android.permission.WRITE_EXTERNAL_STORAGE").get();
832 ImmutableList<Actions.NodeRecord> nodeRecords = actions
833 .getNodeRecords(XmlNode.NodeKey.fromXml(xmlElement.getXml()));
834 assertEquals(1, nodeRecords.size());
835 assertEquals(nodeRecords.iterator().next().mReason,
836 "com.example.lib3 has a targetSdkVersion < 4");
837
838 xmlElement = xmlDocument.getByTypeAndKey(ManifestModel.NodeTypes.USES_PERMISSION,
839 "android.permission.READ_CALL_LOG").get();
840 nodeRecords = actions.getNodeRecords(XmlNode.NodeKey.fromXml(xmlElement.getXml()));
841 assertEquals(1, nodeRecords.size());
842 assertEquals(nodeRecords.iterator().next().mReason,
843 "com.example.lib3 has targetSdkVersion < 16 and requested READ_CONTACTS");
844 }
845
846 public void testLibraryVersion3MergeWithoutContacts()
847 throws ParserConfigurationException, SAXException, IOException {
848 String main = ""
849 + "<manifest\n"
850 + " xmlns:android=\"http://schemas.android.com/apk/res/android\"\n"
851 + " package=\"com.example.lib3\">\n"
852 + "\n"
853 + " <application android:label=\"@string/lib_name\" />\n"
854 + " <uses-sdk android:minSdkVersion=\"19\"/>\n"
855 + " <uses-permission android:name=\"android.permission.READ_CONTACTS\"/>\n"
856 + " <uses-permission android:name=\"android.permission.WRITE_CONTACTS\"/>\n"
857 + "\n"
858 + "</manifest>";
859 String library = ""
860 + "<manifest\n"
861 + " xmlns:android=\"http://schemas.android.com/apk/res/android\"\n"
862 + " xmlns:acme=\"http://acme.org/schemas\"\n"
863 + " package=\"com.example.lib3\">\n"
864 + "\n"
865 + " <activity android:name=\"activityOne\" />\n"
866 + " <uses-sdk android:minSdkVersion=\"3\"/>\n"
867 + "\n"
868 + "</manifest>";
869
870 XmlDocument mainDocument = TestUtils.xmlDocumentFromString(
871 TestUtils.sourceFile(getClass(), "main"), main);
872 XmlDocument libraryDocument = TestUtils.xmlLibraryFromString(
873 TestUtils.sourceFile(getClass(), "library"), library);
874 MergingReport.Builder mergingReportBuilder = new MergingReport.Builder(mLogger);
875 Optional<XmlDocument> mergedDocument =
876 mainDocument.merge(libraryDocument, mergingReportBuilder);
877
878 assertTrue(mergedDocument.isPresent());
879 XmlDocument xmlDocument = mergedDocument.get();
880 assertTrue(xmlDocument.getByTypeAndKey(ManifestModel.NodeTypes.USES_PERMISSION,
881 "android.permission.WRITE_EXTERNAL_STORAGE").isPresent());
882 assertTrue(xmlDocument.getByTypeAndKey(ManifestModel.NodeTypes.USES_PERMISSION,
883 "android.permission.READ_EXTERNAL_STORAGE").isPresent());
884 assertTrue(xmlDocument.getByTypeAndKey(ManifestModel.NodeTypes.USES_PERMISSION,
885 "android.permission.READ_PHONE_STATE").isPresent());
886 assertFalse(xmlDocument.getByTypeAndKey(ManifestModel.NodeTypes.USES_PERMISSION,
887 "android.permission.READ_CALL_LOG").isPresent());
888 assertFalse(xmlDocument.getByTypeAndKey(ManifestModel.NodeTypes.USES_PERMISSION,
889 "android.permission.WRITE_CALL_LOG").isPresent());
890 }
891
892 public void testLibraryVersion10MergeWithContacts()
893 throws ParserConfigurationException, SAXException, IOException {
894 String main = ""
895 + "<manifest\n"
896 + " xmlns:android=\"http://schemas.android.com/apk/res/android\"\n"
897 + " package=\"com.example.lib3\">\n"
898 + "\n"
899 + " <application android:label=\"@string/lib_name\" />\n"
900 + " <uses-sdk android:minSdkVersion=\"19\"/>\n"
901 + "\n"
902 + "</manifest>";
903 String library = ""
904 + "<manifest\n"
905 + " xmlns:android=\"http://schemas.android.com/apk/res/android\"\n"
906 + " xmlns:acme=\"http://acme.org/schemas\"\n"
907 + " package=\"com.example.lib3\">\n"
908 + "\n"
909 + " <activity android:name=\"activityOne\" />\n"
910 + " <uses-sdk android:minSdkVersion=\"10\"/>\n"
911 + " <uses-permission android:name=\"android.permission.READ_CONTACTS\"/>\n"
912 + " <uses-permission android:name=\"android.permission.WRITE_CONTACTS\"/>\n"
913 + "\n"
914 + "</manifest>";
915
916 XmlDocument mainDocument = TestUtils.xmlDocumentFromString(
917 TestUtils.sourceFile(getClass(), "main"), main);
918 XmlDocument libraryDocument = TestUtils.xmlLibraryFromString(
919 TestUtils.sourceFile(getClass(), "library"), library);
920 MergingReport.Builder mergingReportBuilder = new MergingReport.Builder(mLogger);
921 Optional<XmlDocument> mergedDocument =
922 mainDocument.merge(libraryDocument, mergingReportBuilder);
923
924 assertTrue(mergedDocument.isPresent());
925 XmlDocument xmlDocument = mergedDocument.get();
926 assertFalse(xmlDocument.getByTypeAndKey(ManifestModel.NodeTypes.USES_PERMISSION,
927 "android.permission.WRITE_EXTERNAL_STORAGE").isPresent());
928 assertFalse(xmlDocument.getByTypeAndKey(ManifestModel.NodeTypes.USES_PERMISSION,
929 "android.permission.READ_EXTERNAL_STORAGE").isPresent());
930 assertFalse(xmlDocument.getByTypeAndKey(ManifestModel.NodeTypes.USES_PERMISSION,
931 "android.permission.READ_PHONE_STATE").isPresent());
932 assertTrue(xmlDocument.getByTypeAndKey(ManifestModel.NodeTypes.USES_PERMISSION,
933 "android.permission.READ_CALL_LOG").isPresent());
934 assertTrue(xmlDocument.getByTypeAndKey(ManifestModel.NodeTypes.USES_PERMISSION,
935 "android.permission.WRITE_CALL_LOG").isPresent());
936 }
937
938 public void testLibraryAtVersion10MergeWithContactsInMain()
939 throws ParserConfigurationException, SAXException, IOException {
940 String main = ""
941 + "<manifest\n"
942 + " xmlns:android=\"http://schemas.android.com/apk/res/android\"\n"
943 + " package=\"com.example.lib3\">\n"
944 + "\n"
945 + " <application android:label=\"@string/lib_name\" />\n"
946 + " <uses-sdk android:minSdkVersion=\"19\"/>\n"
947 + " <uses-permission android:name=\"android.permission.READ_CONTACTS\"/>\n"
948 + " <uses-permission android:name=\"android.permission.WRITE_CONTACTS\"/>\n"
949 + "\n"
950 + "</manifest>";
951 String library = ""
952 + "<manifest\n"
953 + " xmlns:android=\"http://schemas.android.com/apk/res/android\"\n"
954 + " xmlns:acme=\"http://acme.org/schemas\"\n"
955 + " package=\"com.example.lib3\">\n"
956 + "\n"
957 + " <activity android:name=\"activityOne\" />\n"
958 + " <uses-sdk android:minSdkVersion=\"10\"/>\n"
959 + "\n"
960 + "</manifest>";
961
962 XmlDocument mainDocument = TestUtils.xmlDocumentFromString(
963 TestUtils.sourceFile(getClass(), "main"), main);
964 XmlDocument libraryDocument = TestUtils.xmlLibraryFromString(
965 TestUtils.sourceFile(getClass(), "library"), library);
966 MergingReport.Builder mergingReportBuilder = new MergingReport.Builder(mLogger);
967 Optional<XmlDocument> mergedDocument =
968 mainDocument.merge(libraryDocument, mergingReportBuilder);
969
970 assertTrue(mergedDocument.isPresent());
971 XmlDocument xmlDocument = mergedDocument.get();
972 // no permissions should be added since the library did not add the permissions itself.
973 assertFalse(xmlDocument.getByTypeAndKey(ManifestModel.NodeTypes.USES_PERMISSION,
974 "android.permission.WRITE_EXTERNAL_STORAGE").isPresent());
975 assertFalse(xmlDocument.getByTypeAndKey(ManifestModel.NodeTypes.USES_PERMISSION,
976 "android.permission.READ_EXTERNAL_STORAGE").isPresent());
977 assertFalse(xmlDocument.getByTypeAndKey(ManifestModel.NodeTypes.USES_PERMISSION,
978 "android.permission.READ_PHONE_STATE").isPresent());
979 assertFalse(xmlDocument.getByTypeAndKey(ManifestModel.NodeTypes.USES_PERMISSION,
980 "android.permission.READ_CALL_LOG").isPresent());
981 assertFalse(xmlDocument.getByTypeAndKey(ManifestModel.NodeTypes.USES_PERMISSION,
982 "android.permission.WRITE_CALL_LOG").isPresent());
983 }
984
985
986 public void testUsesSdkAbsenceInOverlay()
987 throws ParserConfigurationException, SAXException, IOException {
988 String overlay = ""
989 + "<manifest\n"
990 + " xmlns:android=\"http://schemas.android.com/apk/res/android\"\n"
991 + " package=\"com.example.lib3\">\n"
992 + "\n"
993 + " <uses-permission android:name=\"android.permission.READ_CONTACTS\"/>\n"
994 + " <uses-permission android:name=\"android.permission.WRITE_CONTACTS\"/>\n"
995 + "\n"
996 + "</manifest>";
997 String main = ""
998 + "<manifest\n"
999 + " xmlns:android=\"http://schemas.android.com/apk/res/android\"\n"
1000 + " xmlns:acme=\"http://acme.org/schemas\"\n"
1001 + " package=\"com.example.lib3\">\n"
1002 + "\n"
1003 + " <uses-sdk android:minSdkVersion=\"15\"/>\n"
1004 + " <application android:label=\"@string/lib_name\" />\n"
1005 + " <activity android:name=\"activityOne\" />\n"
1006 + "\n"
1007 + "</manifest>";
1008
1009 XmlDocument mainDocument = TestUtils.xmlDocumentFromString(
1010 TestUtils.sourceFile(getClass(), "overlay"),
1011 overlay,
1012 XmlDocument.Type.OVERLAY,
1013 Optional.of("com.example.lib3"));
1014 XmlDocument libraryDocument = TestUtils.xmlLibraryFromString(
1015 TestUtils.sourceFile(getClass(), "main"), main);
1016 MergingReport.Builder mergingReportBuilder = new MergingReport.Builder(mLogger);
1017 Optional<XmlDocument> mergedDocument =
1018 mainDocument.merge(libraryDocument, mergingReportBuilder);
1019
1020 MockLog mockLog = new MockLog();
1021 mergingReportBuilder.build().log(mockLog);
1022 System.out.println(mockLog.toString());
1023 assertTrue(mergedDocument.isPresent());
1024 }
1025
1026
1027 /**
1028 * test illegal importation of a preview library (using the minSdk attribute) in a released
1029 * application.
1030 */
1031 public void testLibraryAtPreviewInOldApp_usingMinSdk()
1032 throws ParserConfigurationException, SAXException, IOException {
1033 String main = ""
1034 + "<manifest\n"
1035 + " xmlns:android=\"http://schemas.android.com/apk/res/android\"\n"
1036 + " package=\"com.example.lib3\">\n"
1037 + "\n"
1038 + " <application android:label=\"@string/lib_name\" />\n"
1039 + " <uses-sdk android:minSdkVersion=\"19\"/>\n"
1040 + "\n"
1041 + "</manifest>";
1042 String library = ""
1043 + "<manifest\n"
1044 + " xmlns:android=\"http://schemas.android.com/apk/res/android\"\n"
1045 + " xmlns:acme=\"http://acme.org/schemas\"\n"
1046 + " package=\"com.example.lib3\">\n"
1047 + "\n"
1048 + " <activity android:name=\"activityOne\" />\n"
1049 + " <uses-sdk android:minSdkVersion=\"XYZ\"/>\n"
1050 + "\n"
1051 + "</manifest>";
1052
1053 XmlDocument mainDocument = TestUtils.xmlDocumentFromString(
1054 TestUtils.sourceFile(getClass(), "main"), main);
1055 XmlDocument libraryDocument = TestUtils.xmlLibraryFromString(
1056 TestUtils.sourceFile(getClass(), "library"), library);
1057 MergingReport.Builder mergingReportBuilder = new MergingReport.Builder(mLogger);
1058 Optional<XmlDocument> mergedDocument =
1059 mainDocument.merge(libraryDocument, mergingReportBuilder);
1060
1061 assertFalse(mergedDocument.isPresent());
1062 MergingReport mergingReport = mergingReportBuilder.build();
1063 ImmutableList<MergingReport.Record> loggingRecords = mergingReport.getLoggingRecords();
1064 assertTrue(mergingReport.getResult().isError());
1065 assertEquals(1, loggingRecords.size());
1066 assertTrue(loggingRecords.get(0).getMessage().contains("XYZ"));
1067 }
1068
1069 /**
1070 * test illegal importation of a preview library (using the minSdk attribute) in a released
1071 * application.
1072 */
1073 public void testLibraryAtPreviewInNewerApp_usingMinSdk()
1074 throws ParserConfigurationException, SAXException, IOException {
1075 String main = ""
1076 + "<manifest\n"
1077 + " xmlns:android=\"http://schemas.android.com/apk/res/android\"\n"
1078 + " package=\"com.example.lib3\">\n"
1079 + "\n"
1080 + " <application android:label=\"@string/lib_name\" />\n"
1081 + " <uses-sdk android:minSdkVersion=\""
1082 + (SdkVersionInfo.HIGHEST_KNOWN_API + 2) // fantasy version in the far future.
1083 + "\"/>\n"
1084 + "\n"
1085 + "</manifest>";
1086 String library = ""
1087 + "<manifest\n"
1088 + " xmlns:android=\"http://schemas.android.com/apk/res/android\"\n"
1089 + " xmlns:acme=\"http://acme.org/schemas\"\n"
1090 + " package=\"com.example.lib3\">\n"
1091 + "\n"
1092 + " <activity android:name=\"activityOne\" />\n"
1093 + " <uses-sdk android:minSdkVersion=\"XYZ\"/>\n"
1094 + "\n"
1095 + "</manifest>";
1096
1097 XmlDocument mainDocument = TestUtils.xmlDocumentFromString(
1098 TestUtils.sourceFile(getClass(), "main"), main);
1099 XmlDocument libraryDocument = TestUtils.xmlLibraryFromString(
1100 TestUtils.sourceFile(getClass(), "library"), library);
1101 MergingReport.Builder mergingReportBuilder = new MergingReport.Builder(mLogger);
1102 Optional<XmlDocument> mergedDocument =
1103 mainDocument.merge(libraryDocument, mergingReportBuilder);
1104
1105 assertFalse(mergedDocument.isPresent());
1106 MergingReport mergingReport = mergingReportBuilder.build();
1107 ImmutableList<MergingReport.Record> loggingRecords = mergingReport.getLoggingRecords();
1108 assertTrue(mergingReport.getResult().isError());
1109 assertEquals(1, loggingRecords.size());
1110 assertTrue(loggingRecords.get(0).getMessage().contains("XYZ"));
1111 }
1112
1113
1114 /**
1115 * test illegal importation of a preview library (using the targetSdk attribute) in a released
1116 * application.
1117 */
1118 public void testLibraryAtPreviewInOldApp_usingTargetSdk()
1119 throws ParserConfigurationException, SAXException, IOException {
1120 String main = ""
1121 + "<manifest\n"
1122 + " xmlns:android=\"http://schemas.android.com/apk/res/android\"\n"
1123 + " package=\"com.example.lib3\">\n"
1124 + "\n"
1125 + " <application android:label=\"@string/lib_name\" />\n"
1126 + " <uses-sdk android:minSdkVersion=\"14\" android:targetSdkVersion=\"19\"/>\n"
1127 + "\n"
1128 + "</manifest>";
1129 String library = ""
1130 + "<manifest\n"
1131 + " xmlns:android=\"http://schemas.android.com/apk/res/android\"\n"
1132 + " xmlns:acme=\"http://acme.org/schemas\"\n"
1133 + " package=\"com.example.lib3\">\n"
1134 + "\n"
1135 + " <activity android:name=\"activityOne\" />\n"
1136 + " <uses-sdk android:minSdkVersion=\"14\" android:targetSdkVersion=\"XYZ\"/>\n"
1137 + "\n"
1138 + "</manifest>";
1139
1140 XmlDocument mainDocument = TestUtils.xmlDocumentFromString(
1141 TestUtils.sourceFile(getClass(), "main"), main);
1142 XmlDocument libraryDocument = TestUtils.xmlLibraryFromString(
1143 TestUtils.sourceFile(getClass(), "library"), library);
1144 MergingReport.Builder mergingReportBuilder = new MergingReport.Builder(mLogger);
1145 Optional<XmlDocument> mergedDocument =
1146 mainDocument.merge(libraryDocument, mergingReportBuilder);
1147
1148 assertFalse(mergedDocument.isPresent());
1149 MergingReport mergingReport = mergingReportBuilder.build();
1150 ImmutableList<MergingReport.Record> loggingRecords = mergingReport.getLoggingRecords();
1151 assertTrue(mergingReport.getResult().isError());
1152 assertEquals(1, loggingRecords.size());
1153 assertTrue(loggingRecords.get(0).getMessage().contains("XYZ"));
1154 }
1155
1156 /**
1157 * test legal importation of a released library (using the minSdk attribute) into a preview
1158 * application.
1159 */
1160 public void testLibraryAtReleaseAgainstAppInPreview_usingMinSdk()
1161 throws ParserConfigurationException, SAXException, IOException {
1162 String main = ""
1163 + "<manifest\n"
1164 + " xmlns:android=\"http://schemas.android.com/apk/res/android\"\n"
1165 + " package=\"com.example.lib3\">\n"
1166 + "\n"
1167 + " <application android:label=\"@string/lib_name\" />\n"
1168 + " <uses-sdk android:minSdkVersion=\"XYZ\"/>\n"
1169 + "\n"
1170 + "</manifest>";
1171 String library = ""
1172 + "<manifest\n"
1173 + " xmlns:android=\"http://schemas.android.com/apk/res/android\"\n"
1174 + " xmlns:acme=\"http://acme.org/schemas\"\n"
1175 + " package=\"com.example.lib3\">\n"
1176 + "\n"
1177 + " <activity android:name=\"activityOne\" />\n"
1178 + " <uses-sdk android:minSdkVersion=\"19\"/>\n"
1179 + "\n"
1180 + "</manifest>";
1181
1182 XmlDocument mainDocument = TestUtils.xmlDocumentFromString(
1183 TestUtils.sourceFile(getClass(), "main"), main);
1184 XmlDocument libraryDocument = TestUtils.xmlLibraryFromString(
1185 TestUtils.sourceFile(getClass(), "library"), library);
1186 MergingReport.Builder mergingReportBuilder = new MergingReport.Builder(mLogger);
1187 Optional<XmlDocument> mergedDocument =
1188 mainDocument.merge(libraryDocument, mergingReportBuilder);
1189
1190 assertTrue(mergedDocument.isPresent());
1191 // make sure the resulting min version is "XYZ".
1192 Optional<XmlElement> usesSdk = mergedDocument.get()
1193 .getByTypeAndKey(ManifestModel.NodeTypes.USES_SDK, null);
1194 Optional<XmlAttribute> attribute = usesSdk.get()
1195 .getAttribute(XmlNode.fromXmlName("android:minSdkVersion"));
1196 assertTrue(attribute.isPresent());
1197 assertEquals("XYZ", attribute.get().getValue());
1198 }
1199
1200 /**
1201 * test legal importation of a released library (using the minSdk attribute) into a preview
1202 * application.
1203 */
1204 public void testLibraryAtReleaseAgainstAppInPreview_usingTargetSdk()
1205 throws ParserConfigurationException, SAXException, IOException {
1206 String main = ""
1207 + "<manifest\n"
1208 + " xmlns:android=\"http://schemas.android.com/apk/res/android\"\n"
1209 + " package=\"com.example.lib3\">\n"
1210 + "\n"
1211 + " <application android:label=\"@string/lib_name\" />\n"
1212 + " <uses-sdk android:minSdkVersion=\"14\" android:targetSdkVersion=\"XYZ\"/>\n"
1213 + "\n"
1214 + "</manifest>";
1215 String library = ""
1216 + "<manifest\n"
1217 + " xmlns:android=\"http://schemas.android.com/apk/res/android\"\n"
1218 + " xmlns:acme=\"http://acme.org/schemas\"\n"
1219 + " package=\"com.example.lib3\">\n"
1220 + "\n"
1221 + " <activity android:name=\"activityOne\" />\n"
1222 + " <uses-sdk android:minSdkVersion=\"14\" android:targetSdkVersion=\"14\"/>\n"
1223 + "\n"
1224 + "</manifest>";
1225
1226 XmlDocument mainDocument = TestUtils.xmlDocumentFromString(
1227 TestUtils.sourceFile(getClass(), "main"), main);
1228 XmlDocument libraryDocument = TestUtils.xmlLibraryFromString(
1229 TestUtils.sourceFile(getClass(), "library"), library);
1230 MergingReport.Builder mergingReportBuilder = new MergingReport.Builder(mLogger);
1231 Optional<XmlDocument> mergedDocument =
1232 mainDocument.merge(libraryDocument, mergingReportBuilder);
1233
1234 assertTrue(mergedDocument.isPresent());
1235 // make sure the resulting target version is "XYZ".
1236 Optional<XmlElement> usesSdk = mergedDocument.get()
1237 .getByTypeAndKey(ManifestModel.NodeTypes.USES_SDK, null);
1238 Optional<XmlAttribute> attribute = usesSdk.get()
1239 .getAttribute(XmlNode.fromXmlName("android:targetSdkVersion"));
1240 assertTrue(attribute.isPresent());
1241 assertEquals("XYZ", attribute.get().getValue());
1242 }
1243
1244 /**
1245 * test illegal importation of a more recent released library (using the minSdk attribute) into
1246 * an older preview application.
1247 */
1248 public void testLibraryMoreRecentThanCodeName()
1249 throws ParserConfigurationException, SAXException, IOException {
1250 String main = ""
1251 + "<manifest\n"
1252 + " xmlns:android=\"http://schemas.android.com/apk/res/android\"\n"
1253 + " package=\"com.example.lib3\">\n"
1254 + "\n"
1255 + " <application android:label=\"@string/lib_name\" />\n"
1256 + " <uses-sdk android:minSdkVersion=\"XYZ\"/>\n"
1257 + "\n"
1258 + "</manifest>";
1259 String library = ""
1260 + "<manifest\n"
1261 + " xmlns:android=\"http://schemas.android.com/apk/res/android\"\n"
1262 + " xmlns:acme=\"http://acme.org/schemas\"\n"
1263 + " package=\"com.example.lib3\">\n"
1264 + "\n"
1265 + " <activity android:name=\"activityOne\" />\n"
1266 + " <uses-sdk android:minSdkVersion=\""
1267 + (SdkVersionInfo.HIGHEST_KNOWN_API + 2) // fantasy version in the far future.
1268 + "\"/>\n"
1269 + "\n"
1270 + "</manifest>";
1271
1272 XmlDocument mainDocument = TestUtils.xmlDocumentFromString(
1273 TestUtils.sourceFile(getClass(), "main"), main);
1274 XmlDocument libraryDocument = TestUtils.xmlLibraryFromString(
1275 TestUtils.sourceFile(getClass(), "library"), library);
1276 MergingReport.Builder mergingReportBuilder = new MergingReport.Builder(mLogger);
1277 Optional<XmlDocument> mergedDocument =
1278 mainDocument.merge(libraryDocument, mergingReportBuilder);
1279
1280 assertFalse(mergedDocument.isPresent());
1281 }
1282
1283 /**
1284 * Test that implicit elements are added correctly when importing an old library into a preview
1285 * application.
1286 */
1287 public void testLibraryVersion3MergeInPreviewApp()
1288 throws ParserConfigurationException, SAXException, IOException {
1289 String main = ""
1290 + "<manifest\n"
1291 + " xmlns:android=\"http://schemas.android.com/apk/res/android\"\n"
1292 + " package=\"com.example.lib3\">\n"
1293 + "\n"
1294 + " <application android:label=\"@string/lib_name\" />\n"
1295 + " <uses-sdk android:targetSdkVersion=\"XYZ\"/>\n"
1296 + "\n"
1297 + "</manifest>";
1298 String library = ""
1299 + "<manifest\n"
1300 + " xmlns:android=\"http://schemas.android.com/apk/res/android\"\n"
1301 + " xmlns:acme=\"http://acme.org/schemas\"\n"
1302 + " package=\"com.example.lib3\">\n"
1303 + "\n"
1304 + " <activity android:name=\"activityOne\" />\n"
1305 + " <uses-sdk android:targetSdkVersion=\"3\"/>\n"
1306 + "\n"
1307 + "</manifest>";
1308
1309 XmlDocument mainDocument = TestUtils.xmlDocumentFromString(
1310 TestUtils.sourceFile(getClass(), "main"), main);
1311 XmlDocument libraryDocument = TestUtils.xmlLibraryFromString(
1312 TestUtils.sourceFile(getClass(), "library"), library);
1313 MergingReport.Builder mergingReportBuilder = new MergingReport.Builder(mLogger);
1314 Optional<XmlDocument> mergedDocument =
1315 mainDocument.merge(libraryDocument, mergingReportBuilder);
1316
1317 assertTrue(mergedDocument.isPresent());
1318 XmlDocument xmlDocument = mergedDocument.get();
1319 assertTrue(xmlDocument.getByTypeAndKey(ManifestModel.NodeTypes.USES_PERMISSION,
1320 "android.permission.WRITE_EXTERNAL_STORAGE").isPresent());
1321 assertTrue(xmlDocument.getByTypeAndKey(ManifestModel.NodeTypes.USES_PERMISSION,
1322 "android.permission.READ_EXTERNAL_STORAGE").isPresent());
1323 assertTrue(xmlDocument.getByTypeAndKey(ManifestModel.NodeTypes.USES_PERMISSION,
1324 "android.permission.READ_PHONE_STATE").isPresent());
1325 assertFalse(xmlDocument.getByTypeAndKey(ManifestModel.NodeTypes.USES_PERMISSION,
1326 "android.permission.READ_CALL_LOG").isPresent());
1327 assertFalse(xmlDocument.getByTypeAndKey(ManifestModel.NodeTypes.USES_PERMISSION,
1328 "android.permission.WRITE_CALL_LOG").isPresent());
1329
1330 // check records.
1331 Actions actions = mergingReportBuilder.getActionRecorder().build();
1332 XmlElement xmlElement = xmlDocument.getByTypeAndKey(ManifestModel.NodeTypes.USES_PERMISSION,
1333 "android.permission.WRITE_EXTERNAL_STORAGE").get();
1334 ImmutableList<Actions.NodeRecord> nodeRecords = actions
1335 .getNodeRecords(XmlNode.NodeKey.fromXml(xmlElement.getXml()));
1336 assertEquals(1, nodeRecords.size());
1337 assertEquals(nodeRecords.iterator().next().mReason,
1338 "com.example.lib3 has a targetSdkVersion < 4");
1339 }
1340
1341 /**
1342 * Test that multiple intent-filters with the same key and no override are not merged or
1343 * discarded.
1344 */
1345 public void testMultipleIntentFilter_sameKey_noLibraryDeclaration()
1346 throws ParserConfigurationException, SAXException, IOException {
1347 String main = ""
1348 + "<manifest\n"
1349 + " xmlns:android=\"http://schemas.android.com/apk/res/android\"\n"
1350 + " package=\"com.example.lib3\">\n"
1351 + "\n"
1352 + " <application>\n"
1353 + " <activity android:name=\"activityOne\">\n"
1354 + " <intent-filter>\n"
1355 + " <action android:name=\"android.intent.action.VIEW\"/>\n"
1356 + " <category android:name=\"android.intent.category.DEFAULT\"/>\n"
1357 + " <category android:name=\"android.intent.category.BROWSABLE\"/>\n"
1358 + " <data android:scheme=\"myspecialdeeplinkscheme\"/>\n"
1359 + " <data android:host=\"home\"/>\n"
1360 + " </intent-filter>\n"
1361 + " <intent-filter>\n"
1362 + " <action android:name=\"android.intent.action.VIEW\"/>\n"
1363 + " <category android:name=\"android.intent.category.DEFAULT\"/>\n"
1364 + " <category android:name=\"android.intent.category.BROWSABLE\"/>\n"
1365 + " <data android:scheme=\"https\"/>\n"
1366 + " <data android:host=\"www.foo.com\"/>\n"
1367 + " </intent-filter>\n"
1368 + " </activity>\n"
1369 + " </application>"
1370 + "\n"
1371 + "</manifest>";
1372 String library = ""
1373 + "<manifest\n"
1374 + " xmlns:android=\"http://schemas.android.com/apk/res/android\"\n"
1375 + " xmlns:acme=\"http://acme.org/schemas\"\n"
1376 + " package=\"com.example.lib3\">\n"
1377 + "\n"
1378 + " <activity android:name=\"activityOne\" />\n"
1379 + " <uses-sdk android:targetSdkVersion=\"3\"/>\n"
1380 + "\n"
1381 + "</manifest>";
1382
1383 XmlDocument mainDocument = TestUtils.xmlDocumentFromString(
1384 TestUtils.sourceFile(getClass(), "main"), main);
1385 XmlDocument libraryDocument = TestUtils.xmlLibraryFromString(
1386 TestUtils.sourceFile(getClass(), "library"), library);
1387 MergingReport.Builder mergingReportBuilder = new MergingReport.Builder(mLogger);
1388 Optional<XmlDocument> mergedDocument =
1389 mainDocument.merge(libraryDocument, mergingReportBuilder);
1390
1391 assertTrue(mergedDocument.isPresent());
1392 XmlDocument xmlDocument = mergedDocument.get();
1393 List<XmlElement> allIntentFilters = getAllElementsOfType(xmlDocument,
1394 ManifestModel.NodeTypes.INTENT_FILTER);
1395 assertEquals(2, allIntentFilters.size());
1396 assertEquals(allIntentFilters.get(0).getId(), allIntentFilters.get(1).getId());
1397 }
1398
1399 /**
1400 * Test that multiple intent-filters with the same key and no override are not merged or
1401 * discarded.
1402 */
1403 public void testMultipleIntentFilter_sameKey_noOverride()
1404 throws ParserConfigurationException, SAXException, IOException {
1405 String main = ""
1406 + "<manifest\n"
1407 + " xmlns:android=\"http://schemas.android.com/apk/res/android\"\n"
1408 + " package=\"com.example.lib3\">\n"
1409 + "\n"
1410 + " <application>\n"
1411 + " <activity android:name=\"activityOne\">\n"
1412 + " <intent-filter>\n"
1413 + " <action android:name=\"android.intent.action.VIEW\"/>\n"
1414 + " <category android:name=\"android.intent.category.DEFAULT\"/>\n"
1415 + " <category android:name=\"android.intent.category.BROWSABLE\"/>\n"
1416 + " <data android:scheme=\"myspecialdeeplinkscheme\"/>\n"
1417 + " <data android:host=\"home\"/>\n"
1418 + " </intent-filter>\n"
1419 + " <intent-filter>\n"
1420 + " <action android:name=\"android.intent.action.VIEW\"/>\n"
1421 + " <category android:name=\"android.intent.category.DEFAULT\"/>\n"
1422 + " <category android:name=\"android.intent.category.BROWSABLE\"/>\n"
1423 + " <data android:scheme=\"https\"/>\n"
1424 + " <data android:host=\"www.foo.com\"/>\n"
1425 + " </intent-filter>\n"
1426 + " </activity>\n"
1427 + " </application>"
1428 + "\n"
1429 + "</manifest>";
1430 String library = ""
1431 + "<manifest\n"
1432 + " xmlns:android=\"http://schemas.android.com/apk/res/android\"\n"
1433 + " xmlns:acme=\"http://acme.org/schemas\"\n"
1434 + " package=\"com.example.lib3\">\n"
1435 + "\n"
1436 + " <application>\n"
1437 + " <activity android:name=\"activityOne\">\n"
1438 + " <intent-filter>\n"
1439 + " <action android:name=\"android.intent.action.SEARCH\" />\n"
1440 + " </intent-filter>\n"
1441 + " </activity>"
1442 + " </application>"
1443 + " <uses-sdk android:targetSdkVersion=\"3\"/>\n"
1444 + "\n"
1445 + "</manifest>";
1446
1447 XmlDocument mainDocument = TestUtils.xmlDocumentFromString(
1448 TestUtils.sourceFile(getClass(), "main"), main);
1449 XmlDocument libraryDocument = TestUtils.xmlLibraryFromString(
1450 TestUtils.sourceFile(getClass(), "library"), library);
1451 MergingReport.Builder mergingReportBuilder = new MergingReport.Builder(mLogger);
1452 Optional<XmlDocument> mergedDocument =
1453 mainDocument.merge(libraryDocument, mergingReportBuilder);
1454
1455 assertTrue(mergedDocument.isPresent());
1456 XmlDocument xmlDocument = mergedDocument.get();
1457 List<XmlElement> allIntentFilters = getAllElementsOfType(xmlDocument,
1458 ManifestModel.NodeTypes.INTENT_FILTER);
1459 assertEquals(3, allIntentFilters.size());
1460 }
1461
1462 /**
1463 * Test that multiple intent-filters with the same key and no override are not merged or
1464 * discarded.
1465 */
1466 public void testMultipleIntentFilter_sameKey_sameOverride()
1467 throws ParserConfigurationException, SAXException, IOException {
1468 String main = ""
1469 + "<manifest\n"
1470 + " xmlns:android=\"http://schemas.android.com/apk/res/android\"\n"
1471 + " package=\"com.example.lib3\">\n"
1472 + "\n"
1473 + " <application>\n"
1474 + " <activity android:name=\"activityOne\">\n"
1475 + " <intent-filter>\n"
1476 + " <action android:name=\"android.intent.action.VIEW\"/>\n"
1477 + " <category android:name=\"android.intent.category.DEFAULT\"/>\n"
1478 + " <category android:name=\"android.intent.category.BROWSABLE\"/>\n"
1479 + " <data android:scheme=\"myspecialdeeplinkscheme\"/>\n"
1480 + " <data android:host=\"home\"/>\n"
1481 + " </intent-filter>\n"
1482 + " <intent-filter>\n"
1483 + " <action android:name=\"android.intent.action.VIEW\"/>\n"
1484 + " <category android:name=\"android.intent.category.DEFAULT\"/>\n"
1485 + " <category android:name=\"android.intent.category.BROWSABLE\"/>\n"
1486 + " <data android:scheme=\"https\"/>\n"
1487 + " <data android:host=\"www.foo.com\"/>\n"
1488 + " </intent-filter>\n"
1489 + " </activity>\n"
1490 + " </application>"
1491 + "\n"
1492 + "</manifest>";
1493 String library = ""
1494 + "<manifest\n"
1495 + " xmlns:android=\"http://schemas.android.com/apk/res/android\"\n"
1496 + " xmlns:acme=\"http://acme.org/schemas\"\n"
1497 + " package=\"com.example.lib3\">\n"
1498 + "\n"
1499 + " <application>\n"
1500 + " <activity android:name=\"activityOne\">\n"
1501 + " <intent-filter>\n"
1502 + " <action android:name=\"android.intent.action.SEARCH\" />\n"
1503 + " </intent-filter>\n"
1504 + " <intent-filter>\n"
1505 + " <action android:name=\"android.intent.action.VIEW\"/>\n"
1506 + " <category android:name=\"android.intent.category.DEFAULT\"/>\n"
1507 + " <category android:name=\"android.intent.category.BROWSABLE\"/>\n"
1508 + " <data android:scheme=\"https\"/>\n"
1509 + " <data android:host=\"www.foo.com\"/>\n"
1510 + " </intent-filter>\n"
1511 + " </activity>"
1512 + " </application>"
1513 + " <uses-sdk android:targetSdkVersion=\"3\"/>\n"
1514 + "\n"
1515 + "</manifest>";
1516
1517 XmlDocument mainDocument = TestUtils.xmlDocumentFromString(
1518 TestUtils.sourceFile(getClass(), "main"), main);
1519 XmlDocument libraryDocument = TestUtils.xmlLibraryFromString(
1520 TestUtils.sourceFile(getClass(), "library"), library);
1521 MergingReport.Builder mergingReportBuilder = new MergingReport.Builder(mLogger);
1522 Optional<XmlDocument> mergedDocument =
1523 mainDocument.merge(libraryDocument, mergingReportBuilder);
1524
1525 assertTrue(mergedDocument.isPresent());
1526 XmlDocument xmlDocument = mergedDocument.get();
1527 List<XmlElement> allIntentFilters = getAllElementsOfType(xmlDocument,
1528 ManifestModel.NodeTypes.INTENT_FILTER);
1529
1530 // the second intent-filter of the library should not have been merged in.
1531 assertEquals(3, allIntentFilters.size());
1532 }
1533
1534 /**
1535 * Test that multiple intent-filters with the same key and no override are not merged or
1536 * discarded.
1537 */
1538 public void testMultipleIntentFilter_sameKey_differentOverride()
1539 throws ParserConfigurationException, SAXException, IOException {
1540 String main = ""
1541 + "<manifest\n"
1542 + " xmlns:android=\"http://schemas.android.com/apk/res/android\"\n"
1543 + " package=\"com.example.lib3\">\n"
1544 + "\n"
1545 + " <application>\n"
1546 + " <activity android:name=\"activityOne\">\n"
1547 + " <intent-filter>\n"
1548 + " <action android:name=\"android.intent.action.VIEW\"/>\n"
1549 + " <category android:name=\"android.intent.category.DEFAULT\"/>\n"
1550 + " <category android:name=\"android.intent.category.BROWSABLE\"/>\n"
1551 + " <data android:scheme=\"myspecialdeeplinkscheme\"/>\n"
1552 + " <data android:host=\"home\"/>\n"
1553 + " </intent-filter>\n"
1554 + " <intent-filter>\n"
1555 + " <action android:name=\"android.intent.action.VIEW\"/>\n"
1556 + " <category android:name=\"android.intent.category.DEFAULT\"/>\n"
1557 + " <category android:name=\"android.intent.category.BROWSABLE\"/>\n"
1558 + " <data android:scheme=\"https\"/>\n"
1559 + " <data android:host=\"www.foo.com\"/>\n"
1560 + " </intent-filter>\n"
1561 + " </activity>\n"
1562 + " </application>"
1563 + "\n"
1564 + "</manifest>";
1565 String library = ""
1566 + "<manifest\n"
1567 + " xmlns:android=\"http://schemas.android.com/apk/res/android\"\n"
1568 + " xmlns:acme=\"http://acme.org/schemas\"\n"
1569 + " package=\"com.example.lib3\">\n"
1570 + "\n"
1571 + " <application>\n"
1572 + " <activity android:name=\"activityOne\">\n"
1573 + " <intent-filter>\n"
1574 + " <action android:name=\"android.intent.action.SEARCH\" />\n"
1575 + " </intent-filter>\n"
1576 + " <intent-filter>\n"
1577 + " <action android:name=\"android.intent.action.VIEW\"/>\n"
1578 + " <category android:name=\"android.intent.category.DEFAULT\"/>\n"
1579 + " <category android:name=\"android.intent.category.BROWSABLE\"/>\n"
1580 + " <data android:scheme=\"https\"/>\n"
1581 + " <data android:host=\"www.bar.com\"/>\n"
1582 + " </intent-filter>\n"
1583 + " </activity>"
1584 + " </application>"
1585 + " <uses-sdk android:targetSdkVersion=\"3\"/>\n"
1586 + "\n"
1587 + "</manifest>";
1588
1589 XmlDocument mainDocument = TestUtils.xmlDocumentFromString(
1590 TestUtils.sourceFile(getClass(), "main"), main);
1591 XmlDocument libraryDocument = TestUtils.xmlLibraryFromString(
1592 TestUtils.sourceFile(getClass(), "library"), library);
1593 MergingReport.Builder mergingReportBuilder = new MergingReport.Builder(mLogger);
1594 Optional<XmlDocument> mergedDocument =
1595 mainDocument.merge(libraryDocument, mergingReportBuilder);
1596
1597 assertTrue(mergedDocument.isPresent());
1598 XmlDocument xmlDocument = mergedDocument.get();
1599 List<XmlElement> allIntentFilters = getAllElementsOfType(xmlDocument,
1600 ManifestModel.NodeTypes.INTENT_FILTER);
1601 // all intent-filters should have been merged.
1602 assertEquals(4, allIntentFilters.size());
1603 }
1604
1605 /**
1606 * Test that multiple intent-filters with the same key and no override are not merged or
1607 * discarded.
1608 */
1609 public void testMultipleIntentFilter_sameKey_removal()
1610 throws ParserConfigurationException, SAXException, IOException {
1611 String main = ""
1612 + "<manifest\n"
1613 + " xmlns:android=\"http://schemas.android.com/apk/res/android\"\n"
1614 + " xmlns:tools=\"http://schemas.android.com/tools\"\n"
1615 + " package=\"com.example.lib3\">\n"
1616 + "\n"
1617 + " <application>\n"
1618 + " <activity android:name=\"activityOne\">\n"
1619 + " <intent-filter tools:node=\"remove\">\n"
1620 + " <action android:name=\"android.intent.action.VIEW\"/>\n"
1621 + " <category android:name=\"android.intent.category.DEFAULT\"/>\n"
1622 + " <category android:name=\"android.intent.category.BROWSABLE\"/>\n"
1623 + " </intent-filter>\n"
1624 + " </activity>\n"
1625 + " </application>"
1626 + "\n"
1627 + "</manifest>";
1628 String library = ""
1629 + "<manifest\n"
1630 + " xmlns:android=\"http://schemas.android.com/apk/res/android\"\n"
1631 + " xmlns:acme=\"http://acme.org/schemas\"\n"
1632 + " package=\"com.example.lib3\">\n"
1633 + "\n"
1634 + " <application>\n"
1635 + " <activity android:name=\"activityOne\">\n"
1636 + " <intent-filter>\n"
1637 + " <action android:name=\"android.intent.action.SEARCH\" />\n"
1638 + " </intent-filter>\n"
1639 + " <intent-filter>\n"
1640 + " <action android:name=\"android.intent.action.VIEW\"/>\n"
1641 + " <category android:name=\"android.intent.category.DEFAULT\"/>\n"
1642 + " <category android:name=\"android.intent.category.BROWSABLE\"/>\n"
1643 + " <data android:scheme=\"https\"/>\n"
1644 + " <data android:host=\"www.bar.com\"/>\n"
1645 + " </intent-filter>\n"
1646 + " <intent-filter>\n"
1647 + " <action android:name=\"android.intent.action.VIEW\"/>\n"
1648 + " <category android:name=\"android.intent.category.DEFAULT\"/>\n"
1649 + " <category android:name=\"android.intent.category.BROWSABLE\"/>\n"
1650 + " <data android:scheme=\"https\"/>\n"
1651 + " <data android:host=\"www.foo.com\"/>\n"
1652 + " </intent-filter>\n"
1653 + " </activity>"
1654 + " </application>"
1655 + " <uses-sdk android:targetSdkVersion=\"3\"/>\n"
1656 + "\n"
1657 + "</manifest>";
1658
1659 XmlDocument mainDocument = TestUtils.xmlDocumentFromString(
1660 TestUtils.sourceFile(getClass(), "main"), main);
1661 XmlDocument libraryDocument = TestUtils.xmlLibraryFromString(
1662 TestUtils.sourceFile(getClass(), "library"), library);
1663 MergingReport.Builder mergingReportBuilder = new MergingReport.Builder(mLogger);
1664 Optional<XmlDocument> mergedDocument =
1665 mainDocument.merge(libraryDocument, mergingReportBuilder);
1666
1667 assertTrue(mergedDocument.isPresent());
1668 XmlDocument xmlDocument = mergedDocument.get();
1669 List<XmlElement> allIntentFilters = getAllElementsOfType(xmlDocument,
1670 ManifestModel.NodeTypes.INTENT_FILTER);
1671 // the cleaner is not removed the "remove" node so we should have 2.
1672 assertEquals(2, allIntentFilters.size());
1673 }
1674
1675 public void testMultipleIntentFilter_sameKey_removalAll()
1676 throws ParserConfigurationException, SAXException, IOException {
1677 String main = ""
1678 + "<manifest\n"
1679 + " xmlns:android=\"http://schemas.android.com/apk/res/android\"\n"
1680 + " xmlns:tools=\"http://schemas.android.com/tools\"\n"
1681 + " package=\"com.example.lib3\">\n"
1682 + "\n"
1683 + " <application>\n"
1684 + " <activity android:name=\"activityOne\">\n"
1685 + " <intent-filter tools:node=\"removeAll\"/>\n"
1686 + " </activity>\n"
1687 + " </application>"
1688 + "\n"
1689 + "</manifest>";
1690 String library = ""
1691 + "<manifest\n"
1692 + " xmlns:android=\"http://schemas.android.com/apk/res/android\"\n"
1693 + " xmlns:acme=\"http://acme.org/schemas\"\n"
1694 + " package=\"com.example.lib3\">\n"
1695 + "\n"
1696 + " <application>\n"
1697 + " <activity android:name=\"activityOne\">\n"
1698 + " <intent-filter>\n"
1699 + " <action android:name=\"android.intent.action.SEARCH\" />\n"
1700 + " </intent-filter>\n"
1701 + " <intent-filter>\n"
1702 + " <action android:name=\"android.intent.action.VIEW\"/>\n"
1703 + " <category android:name=\"android.intent.category.DEFAULT\"/>\n"
1704 + " <category android:name=\"android.intent.category.BROWSABLE\"/>\n"
1705 + " <data android:scheme=\"https\"/>\n"
1706 + " <data android:host=\"www.bar.com\"/>\n"
1707 + " </intent-filter>\n"
1708 + " <intent-filter>\n"
1709 + " <action android:name=\"android.intent.action.VIEW\"/>\n"
1710 + " <category android:name=\"android.intent.category.DEFAULT\"/>\n"
1711 + " <category android:name=\"android.intent.category.BROWSABLE\"/>\n"
1712 + " <data android:scheme=\"https\"/>\n"
1713 + " <data android:host=\"www.foo.com\"/>\n"
1714 + " </intent-filter>\n"
1715 + " </activity>"
1716 + " </application>"
1717 + " <uses-sdk android:targetSdkVersion=\"3\"/>\n"
1718 + "\n"
1719 + "</manifest>";
1720
1721 XmlDocument mainDocument = TestUtils.xmlDocumentFromString(
1722 TestUtils.sourceFile(getClass(), "main"), main);
1723 XmlDocument libraryDocument = TestUtils.xmlLibraryFromString(
1724 TestUtils.sourceFile(getClass(), "library"), library);
1725 MergingReport.Builder mergingReportBuilder = new MergingReport.Builder(mLogger);
1726 Optional<XmlDocument> mergedDocument =
1727 mainDocument.merge(libraryDocument, mergingReportBuilder);
1728
1729 assertTrue(mergedDocument.isPresent());
1730 XmlDocument xmlDocument = mergedDocument.get();
1731 List<XmlElement> allIntentFilters = getAllElementsOfType(xmlDocument,
1732 ManifestModel.NodeTypes.INTENT_FILTER);
1733 // since the cleaner has not run, there is one intent-filter with the removeAll annotation.
1734 assertEquals(1, allIntentFilters.size());
1735 }
1736
1737 private static List<XmlElement> getAllElementsOfType(
1738 XmlDocument xmlDocument,
1739 ManifestModel.NodeTypes nodeType) {
1740 ImmutableList.Builder<XmlElement> listBuilder = ImmutableList.builder();
1741 getAllElementsOfType(xmlDocument.getRootNode(), nodeType, listBuilder);
1742 return listBuilder.build();
1743 }
1744
1745 private static void getAllElementsOfType(XmlElement element,
1746 ManifestModel.NodeTypes nodeType,
1747 ImmutableList.Builder<XmlElement> allElementsBuilder) {
1748
1749 for (XmlElement xmlElement : element.getMergeableElements()) {
1750 if (xmlElement.isA(nodeType)) {
1751 allElementsBuilder.add(xmlElement);
1752 } else {
1753 getAllElementsOfType(xmlElement, nodeType, allElementsBuilder);
1754 }
1755 }
1756 }
1757
1758 private static Element validate(String xml)
1759 throws ParserConfigurationException, IOException, SAXException {
1760
1761 DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
1762 dbFactory.setNamespaceAware(true);
1763 DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
1764 dBuilder.setErrorHandler(new ErrorHandler() {
1765 @Override
1766 public void warning(SAXParseException e) throws SAXException {
1767
1768 }
1769
1770 @Override
1771 public void error(SAXParseException e) throws SAXException {
1772 fail(e.getMessage());
1773 }
1774
1775 @Override
1776 public void fatalError(SAXParseException e) throws SAXException {
1777 fail(e.getMessage());
1778 }
1779 });
1780 Document validated = dBuilder.parse(
1781 new InputSource(new StringReader(xml)));
1782 return (Element) validated.getChildNodes().item(0);
1783 }
1784 }