Debian Codegraph @

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

raw
1 /*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package com.android.manifmerger;
18
19 import static org.mockito.Mockito.when;
20
21 import com.android.SdkConstants;
22 import com.android.utils.StdLogger;
23 import com.google.common.base.Optional;
24
25 import junit.framework.TestCase;
26
27 import org.mockito.Mock;
28 import org.mockito.Mockito;
29 import org.mockito.MockitoAnnotations;
30 import org.w3c.dom.Attr;
31 import org.xml.sax.SAXException;
32
33 import java.io.IOException;
34 import java.util.List;
35
36 import javax.xml.parsers.ParserConfigurationException;
37
38 /**
39 * Tests for {@link com.android.manifmerger.XmlAttribute} class
40 */
41 public class XmlAttributeTest extends TestCase {
42
43 @Mock
44 XmlDocument mXmlDocument;
45
46 @Mock
47 XmlElement mXmlElement;
48
49 @Mock
50 Attr mAttr;
51
52 @Override
53 protected void setUp() throws Exception {
54 super.setUp();
55 MockitoAnnotations.initMocks(this);
56
57 when(mAttr.getNamespaceURI()).thenReturn(SdkConstants.ANDROID_URI);
58 when(mAttr.getPrefix()).thenReturn("android");
59 when(mAttr.getLocalName()).thenReturn("name");
60
61 when(mXmlElement.getType()).thenReturn(ManifestModel.NodeTypes.ACTIVITY);
62 when(mXmlElement.getDocument()).thenReturn(mXmlDocument);
63 when(mXmlDocument.getPackageNameForAttributeExpansion()).thenReturn("com.foo.bar");
64 }
65
66 public void testPackageSubstitution_noDot() {
67
68 when(mAttr.getValue()).thenReturn("ActivityOne");
69 // this will reset the package.
70 assertNotNull(new XmlAttribute(mXmlElement, mAttr,
71 AttributeModel.newModel("ActivityOne").setIsPackageDependent().build()));
72 Mockito.verify(mAttr).setValue("com.foo.bar.ActivityOne");
73 }
74
75 public void testPackageSubstitution_withDot() {
76
77 when(mAttr.getValue()).thenReturn(".ActivityOne");
78 // this will reset the package.
79 assertNotNull(new XmlAttribute(mXmlElement, mAttr,
80 AttributeModel.newModel("ActivityOne").setIsPackageDependent().build()));
81 Mockito.verify(mAttr).setValue("com.foo.bar.ActivityOne");
82 }
83
84 public void testNoPackageSubstitution() {
85
86 when(mAttr.getValue()).thenReturn("com.foo.foo2.ActivityOne");
87 // this will NOT reset the package.
88 assertNotNull(new XmlAttribute(mXmlElement, mAttr,
89 AttributeModel.newModel("ActivityOne").setIsPackageDependent().build()));
90 Mockito.verify(mAttr).getValue();
91 Mockito.verifyNoMoreInteractions(mAttr);
92 }
93
94 public void testAttributeRemoval()
95 throws ParserConfigurationException, SAXException, IOException {
96 String higherPriority = ""
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\"\n"
103 + " tools:remove=\"theme\"/>\n"
104 + "\n"
105 + "</manifest>";
106
107 String lowerPriority = ""
108 + "<manifest\n"
109 + " xmlns:android=\"http://schemas.android.com/apk/res/android\"\n"
110 + " xmlns:tools=\"http://schemas.android.com/tools\"\n"
111 + " package=\"com.example.lib3\">\n"
112 + "\n"
113 + " <activity android:name=\"activityOne\" \n"
114 + " android:theme=\"@oldtheme\"/>\n"
115 + "\n"
116 + "</manifest>";
117
118 XmlDocument refDocument = TestUtils.xmlDocumentFromString(
119 TestUtils.sourceFile(getClass(), "higherPriority"), higherPriority);
120 XmlDocument otherDocument = TestUtils.xmlLibraryFromString(
121 TestUtils.sourceFile(getClass(), "lowerPriority"), lowerPriority);
122
123 MergingReport.Builder mergingReportBuilder = new MergingReport.Builder(
124 new StdLogger(StdLogger.Level.VERBOSE));
125 Optional<XmlDocument> result = refDocument.merge(otherDocument, mergingReportBuilder);
126 assertTrue(result.isPresent());
127 XmlDocument resultDocument = result.get();
128
129 Optional<XmlElement> activityOne = resultDocument.getRootNode()
130 .getNodeByTypeAndKey(ManifestModel.NodeTypes.ACTIVITY,
131 "com.example.lib3.activityOne");
132 assertTrue(activityOne.isPresent());
133
134 // verify that only android:name and tools:remove remains in the result.
135 List<XmlAttribute> attributes = activityOne.get().getAttributes();
136 assertEquals(2, attributes.size());
137 assertTrue(activityOne.get().getAttribute(
138 XmlNode.fromXmlName("android:name")).isPresent());
139 assertTrue(activityOne.get().getAttribute(
140 XmlNode.fromNSName(SdkConstants.TOOLS_URI, "tools", "remove")).isPresent());
141
142 Actions actions = mergingReportBuilder.getActionRecorder().build();
143 // check the recorded actions.
144 List<Actions.AttributeRecord> attributeRecords =
145 actions.getAttributeRecords(activityOne.get().getId(),
146 XmlNode.fromXmlName("android:theme"));
147 assertNotNull(attributeRecords);
148 assertEquals(1, attributeRecords.size());
149 Actions.AttributeRecord attributeRecord = attributeRecords.get(0);
150 assertEquals(Actions.ActionType.REJECTED, attributeRecord.getActionType());
151 assertEquals(AttributeOperationType.REMOVE, attributeRecord.getOperationType());
152 assertEquals(6, attributeRecord.getActionLocation().getPosition().getStartLine());
153 }
154
155 public void testNamespaceAwareAttributeRemoval()
156 throws ParserConfigurationException, SAXException, IOException {
157 String higherPriority = ""
158 + "<manifest\n"
159 + " xmlns:android=\"http://schemas.android.com/apk/res/android\"\n"
160 + " xmlns:tools=\"http://schemas.android.com/tools\"\n"
161 + " package=\"com.example.lib3\">\n"
162 + "\n"
163 + " <activity android:name=\"activityOne\"\n"
164 + " tools:remove=\"android:theme\"/>\n"
165 + "\n"
166 + "</manifest>";
167
168 String lowerPriority = ""
169 + "<manifest\n"
170 + " xmlns:android=\"http://schemas.android.com/apk/res/android\"\n"
171 + " xmlns:tools=\"http://schemas.android.com/tools\"\n"
172 + " package=\"com.example.lib3\">\n"
173 + "\n"
174 + " <activity android:name=\"activityOne\" \n"
175 + " android:theme=\"@oldtheme\"/>\n"
176 + "\n"
177 + "</manifest>";
178
179 XmlDocument refDocument = TestUtils.xmlDocumentFromString(
180 TestUtils.sourceFile(getClass(), "higherPriority"), higherPriority);
181 XmlDocument otherDocument = TestUtils.xmlLibraryFromString(
182 TestUtils.sourceFile(getClass(), "lowerPriority"), lowerPriority);
183
184 MergingReport.Builder mergingReportBuilder = new MergingReport.Builder(
185 new StdLogger(StdLogger.Level.VERBOSE));
186 Optional<XmlDocument> result = refDocument.merge(otherDocument, mergingReportBuilder);
187 assertTrue(result.isPresent());
188 XmlDocument resultDocument = result.get();
189
190 Optional<XmlElement> activityOne = resultDocument.getRootNode()
191 .getNodeByTypeAndKey(ManifestModel.NodeTypes.ACTIVITY,
192 "com.example.lib3.activityOne");
193 assertTrue(activityOne.isPresent());
194
195 List<XmlAttribute> attributes = activityOne.get().getAttributes();
196 assertEquals(2, attributes.size());
197 assertTrue(activityOne.get().getAttribute(
198 XmlNode.fromXmlName("android:name")).isPresent());
199 assertTrue(activityOne.get().getAttribute(
200 XmlNode.fromNSName(SdkConstants.TOOLS_URI, "tools", "remove")).isPresent());
201 }
202
203 public void testMultipleAttributesRemoval()
204 throws ParserConfigurationException, SAXException, IOException {
205 String higherPriority = ""
206 + "<manifest\n"
207 + " xmlns:android=\"http://schemas.android.com/apk/res/android\"\n"
208 + " xmlns:tools=\"http://schemas.android.com/tools\"\n"
209 + " package=\"com.example.lib3\">\n"
210 + "\n"
211 + " <activity android:name=\"activityOne\"\n"
212 + " tools:remove=\"theme, exported\"/>\n"
213 + "\n"
214 + "</manifest>";
215
216 String lowerPriority = ""
217 + "<manifest\n"
218 + " xmlns:android=\"http://schemas.android.com/apk/res/android\"\n"
219 + " xmlns:tools=\"http://schemas.android.com/tools\"\n"
220 + " package=\"com.example.lib3\">\n"
221 + "\n"
222 + " <activity android:name=\"activityOne\" \n"
223 + " android:exported=\"true\"\n"
224 + " android:theme=\"@oldtheme\"/>\n"
225 + "\n"
226 + "</manifest>";
227
228 XmlDocument refDocument = TestUtils.xmlDocumentFromString(
229 TestUtils.sourceFile(getClass(), "higherPriority"), higherPriority);
230 XmlDocument otherDocument = TestUtils.xmlLibraryFromString(
231 TestUtils.sourceFile(getClass(), "lowerPriority"), lowerPriority);
232
233 MergingReport.Builder mergingReportBuilder = new MergingReport.Builder(
234 new StdLogger(StdLogger.Level.VERBOSE));
235 Optional<XmlDocument> result = refDocument.merge(otherDocument, mergingReportBuilder);
236 assertTrue(result.isPresent());
237 XmlDocument resultDocument = result.get();
238
239 Optional<XmlElement> activityOne = resultDocument.getRootNode()
240 .getNodeByTypeAndKey(ManifestModel.NodeTypes.ACTIVITY,
241 "com.example.lib3.activityOne");
242 assertTrue(activityOne.isPresent());
243
244 List<XmlAttribute> attributes = activityOne.get().getAttributes();
245 assertEquals(2, attributes.size());
246 assertTrue(activityOne.get().getAttribute(
247 XmlNode.fromXmlName("android:name")).isPresent());
248 assertTrue(activityOne.get().getAttribute(
249 XmlNode.fromNSName(SdkConstants.TOOLS_URI, "tools", "remove")).isPresent());
250 }
251
252 public void testDeepAttributeRemoval()
253 throws ParserConfigurationException, SAXException, IOException {
254 String higherPriority = ""
255 + "<manifest\n"
256 + " xmlns:android=\"http://schemas.android.com/apk/res/android\"\n"
257 + " xmlns:tools=\"http://schemas.android.com/tools\"\n"
258 + " package=\"com.example.lib3\">\n"
259 + "\n"
260 + " <activity android:name=\"activityOne\"\n"
261 + " tools:remove=\"theme, exported\"/>\n"
262 + "\n"
263 + "</manifest>";
264
265 String lowerPriority = ""
266 + "<manifest\n"
267 + " xmlns:android=\"http://schemas.android.com/apk/res/android\"\n"
268 + " xmlns:tools=\"http://schemas.android.com/tools\"\n"
269 + " package=\"com.example.lib3\">\n"
270 + "\n"
271 + " <activity android:name=\"activityOne\" \n"
272 + " android:screenOrientation=\"landscape\"\n"
273 + " android:theme=\"@oldtheme\"/>\n"
274 + "\n"
275 + "</manifest>";
276
277 String evenLowerPriority = ""
278 + "<manifest\n"
279 + " xmlns:android=\"http://schemas.android.com/apk/res/android\"\n"
280 + " xmlns:tools=\"http://schemas.android.com/tools\"\n"
281 + " package=\"com.example.lib3\">\n"
282 + "\n"
283 + " <activity android:name=\"activityOne\" \n"
284 + " android:theme=\"@oldtheme\"\n"
285 + " android:exported=\"true\"/>\n"
286 + "\n"
287 + "</manifest>";
288
289 XmlDocument highPriority = TestUtils.xmlDocumentFromString(
290 TestUtils.sourceFile(getClass(), "highPriority"), higherPriority);
291 XmlDocument lowPriority = TestUtils.xmlLibraryFromString(
292 TestUtils.sourceFile(getClass(), "lowPriority"), lowerPriority);
293 XmlDocument lowestPriority = TestUtils.xmlLibraryFromString(
294 TestUtils.sourceFile(getClass(), "lowestPriority"), evenLowerPriority);
295
296 MergingReport.Builder mergingReportBuilder = new MergingReport.Builder(
297 new StdLogger(StdLogger.Level.VERBOSE));
298 Optional<XmlDocument> result = highPriority.merge(lowPriority, mergingReportBuilder);
299 assertTrue(result.isPresent());
300 XmlDocument resultDocument = result.get();
301 result = resultDocument.merge(lowestPriority, mergingReportBuilder);
302 assertTrue(result.isPresent());
303 resultDocument = result.get();
304
305 Optional<XmlElement> activityOne = resultDocument.getRootNode()
306 .getNodeByTypeAndKey(ManifestModel.NodeTypes.ACTIVITY,
307 "com.example.lib3.activityOne");
308 assertTrue(activityOne.isPresent());
309
310 List<XmlAttribute> attributes = activityOne.get().getAttributes();
311 assertEquals(3, attributes.size());
312 assertTrue(activityOne.get().getAttribute(
313 XmlNode.fromXmlName("android:name")).isPresent());
314 assertTrue(activityOne.get().getAttribute(
315 XmlNode.fromXmlName("android:screenOrientation")).isPresent());
316 assertTrue(activityOne.get().getAttribute(
317 XmlNode.fromNSName(SdkConstants.TOOLS_URI, "tools", "remove")).isPresent());
318
319 Actions actions = mergingReportBuilder.getActionRecorder().build();
320 // check the recorded actions.
321 List<Actions.AttributeRecord> attributeRecords =
322 actions.getAttributeRecords(activityOne.get().getId(),
323 XmlNode.fromXmlName("android:theme"));
324 assertNotNull(attributeRecords);
325
326 // theme was removed twice...
327 assertEquals(2, attributeRecords.size());
328 Actions.AttributeRecord attributeRecord = attributeRecords.get(0);
329 assertEquals(Actions.ActionType.REJECTED, attributeRecord.getActionType());
330 assertEquals(AttributeOperationType.REMOVE, attributeRecord.getOperationType());
331 assertEquals("XmlAttributeTest#lowPriority",
332 attributeRecord.getActionLocation().getFile().print(true));
333 assertEquals(7, attributeRecord.getActionLocation().getPosition().getStartLine());
334
335 attributeRecord = attributeRecords.get(1);
336 assertEquals(Actions.ActionType.REJECTED, attributeRecord.getActionType());
337 assertEquals(AttributeOperationType.REMOVE, attributeRecord.getOperationType());
338 assertEquals("XmlAttributeTest#lowestPriority",
339 attributeRecord.getActionLocation().getFile().print(true));
340 assertEquals(6, attributeRecord.getActionLocation().getPosition().getStartLine());
341 }
342
343 public void testDefaultValueIllegalOverriding()
344 throws ParserConfigurationException, SAXException, IOException {
345 String higherPriority = ""
346 + "<manifest\n"
347 + " xmlns:android=\"http://schemas.android.com/apk/res/android\"\n"
348 + " xmlns:tools=\"http://schemas.android.com/tools\"\n"
349 + " package=\"com.example.lib3\">\n"
350 + "\n"
351 // implicit required=true attribute present.
352 + " <uses-library android:name=\"libraryOne\"/>\n"
353 + " <permission android:name=\"permissionOne\"/>\n"
354 + "\n"
355 + "</manifest>";
356
357 String lowerPriority = ""
358 + "<manifest\n"
359 + " xmlns:android=\"http://schemas.android.com/apk/res/android\"\n"
360 + " xmlns:tools=\"http://schemas.android.com/tools\"\n"
361 + " package=\"com.example.lib3\">\n"
362 + "\n"
363 + " <uses-library android:name=\"libraryOne\" android:required=\"false\"/>\n"
364 + " <permission android:name=\"permissionOne\" "
365 + " android:protectionLevel=\"dangerous\"/>\n"
366 + "\n"
367 + "</manifest>";
368
369 XmlDocument refDocument = TestUtils.xmlDocumentFromString(
370 TestUtils.sourceFile(getClass(), "higherPriority"), higherPriority);
371 XmlDocument otherDocument = TestUtils.xmlLibraryFromString(
372 TestUtils.sourceFile(getClass(), "lowerPriority"), lowerPriority);
373
374 MergingReport.Builder mergingReportBuilder = new MergingReport.Builder(
375 new StdLogger(StdLogger.Level.VERBOSE));
376 Optional<XmlDocument> result = refDocument.merge(otherDocument, mergingReportBuilder);
377 assertTrue(result.isPresent());
378
379 }
380
381 public void testToolsAttributeMerging()
382 throws ParserConfigurationException, SAXException, IOException {
383 String higherPriority = ""
384 + "<manifest\n"
385 + " xmlns:android=\"http://schemas.android.com/apk/res/android\"\n"
386 + " xmlns:tools=\"http://schemas.android.com/tools\"\n"
387 + " package=\"com.example.lib3\">\n"
388 + "\n"
389 + " <activity android:name=\"activityOne\"\n"
390 + " tools:remove=\"theme,exported\"/>\n"
391 + "\n"
392 + "</manifest>";
393
394 String lowerPriority = ""
395 + "<manifest\n"
396 + " xmlns:android=\"http://schemas.android.com/apk/res/android\"\n"
397 + " xmlns:tools=\"http://schemas.android.com/tools\"\n"
398 + " package=\"com.example.lib3\">\n"
399 + "\n"
400 + "\n"
401 + " <activity android:name=\"activityOne\" \n"
402 +" tools:remove=\"bar\"/>\n"
403 + "\n"
404 + "</manifest>";
405
406 XmlDocument refDocument = TestUtils.xmlDocumentFromString(
407 TestUtils.sourceFile(getClass(), "higherPriority"), higherPriority);
408 XmlDocument otherDocument = TestUtils.xmlDocumentFromString(
409 TestUtils.sourceFile(getClass(), "lowerPriority"), lowerPriority);
410
411 MergingReport.Builder mergingReportBuilder = new MergingReport.Builder(
412 new StdLogger(StdLogger.Level.VERBOSE));
413 Optional<XmlDocument> result = refDocument.merge(otherDocument, mergingReportBuilder);
414 assertTrue(result.isPresent());
415
416 Optional<XmlElement> activityOne = result.get().getRootNode()
417 .getNodeByTypeAndKey(ManifestModel.NodeTypes.ACTIVITY,
418 "com.example.lib3.activityOne");
419 assertTrue(activityOne.isPresent());
420
421 // verify that only android:name and tools:remove remains in the result.
422 List<XmlAttribute> attributes = activityOne.get().getAttributes();
423 assertEquals(2, attributes.size());
424 assertTrue(activityOne.get().getAttribute(
425 XmlNode.fromXmlName("android:name")).isPresent());
426 Optional<XmlAttribute> toolsRemove = activityOne.get().getAttribute(
427 XmlNode.fromNSName(SdkConstants.TOOLS_URI, "tools", "remove"));
428 assertTrue(toolsRemove.isPresent());
429 assertEquals("theme,exported,bar", toolsRemove.get().getValue());
430
431 }
432
433 public void testToolsNodeAttributeNotMerging()
434 throws ParserConfigurationException, SAXException, IOException {
435 String higherPriority = ""
436 + "<manifest\n"
437 + " xmlns:android=\"http://schemas.android.com/apk/res/android\"\n"
438 + " xmlns:tools=\"http://schemas.android.com/tools\"\n"
439 + " package=\"com.example.lib3\">\n"
440 + "\n"
441 + " <activity android:name=\"activityOne\"\n"
442 + " tools:node=\"replace\"/>\n"
443 + "\n"
444 + "</manifest>";
445
446 String lowerPriority = ""
447 + "<manifest\n"
448 + " xmlns:android=\"http://schemas.android.com/apk/res/android\"\n"
449 + " xmlns:tools=\"http://schemas.android.com/tools\"\n"
450 + " package=\"com.example.lib3\">\n"
451 + "\n"
452 + "\n"
453 + " <activity android:name=\"activityOne\" \n"
454 +" tools:node=\"remove\"/>\n"
455 + "\n"
456 + "</manifest>";
457
458 XmlDocument refDocument = TestUtils.xmlDocumentFromString(
459 TestUtils.sourceFile(getClass(), "higherPriority"), higherPriority);
460 XmlDocument otherDocument = TestUtils.xmlDocumentFromString(
461 TestUtils.sourceFile(getClass(), "lowerPriority"), lowerPriority);
462
463 MergingReport.Builder mergingReportBuilder = new MergingReport.Builder(
464 new StdLogger(StdLogger.Level.VERBOSE));
465 Optional<XmlDocument> result = refDocument.merge(otherDocument, mergingReportBuilder);
466 assertTrue(result.isPresent());
467
468 Optional<XmlElement> activityOne = result.get().getRootNode()
469 .getNodeByTypeAndKey(ManifestModel.NodeTypes.ACTIVITY,
470 "com.example.lib3.activityOne");
471 assertTrue(activityOne.isPresent());
472
473 // verify that only android:name and tools:remove remains in the result.
474 List<XmlAttribute> attributes = activityOne.get().getAttributes();
475 assertEquals(2, attributes.size());
476 assertTrue(activityOne.get().getAttribute(
477 XmlNode.fromXmlName("android:name")).isPresent());
478 Optional<XmlAttribute> toolsRemove = activityOne.get().getAttribute(
479 XmlNode.fromNSName(SdkConstants.TOOLS_URI, "tools", "node"));
480 assertTrue(toolsRemove.isPresent());
481 assertEquals("replace", toolsRemove.get().getValue());
482
483 }
484 }