Debian Codegraph @

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