| 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 com.android.SdkConstants; |
| 20 |
import com.android.ide.common.blame.SourcePosition; |
| 21 |
import com.android.ide.common.xml.XmlFormatPreferences; |
| 22 |
import com.android.ide.common.xml.XmlFormatStyle; |
| 23 |
import com.android.ide.common.xml.XmlPrettyPrinter; |
| 24 |
import com.google.common.base.Optional; |
| 25 |
|
| 26 |
import junit.framework.TestCase; |
| 27 |
|
| 28 |
import org.w3c.dom.Attr; |
| 29 |
import org.w3c.dom.Element; |
| 30 |
import org.w3c.dom.Node; |
| 31 |
import org.xml.sax.SAXException; |
| 32 |
|
| 33 |
import java.io.IOException; |
| 34 |
|
| 35 |
import javax.xml.parsers.ParserConfigurationException; |
| 36 |
|
| 37 |
/** |
| 38 |
* Tests for {@link XmlLoader} |
| 39 |
*/ |
| 40 |
public class XmlLoaderTest extends TestCase { |
| 41 |
|
| 42 |
public void testAndroidPrefix() throws IOException, SAXException, ParserConfigurationException { |
| 43 |
|
| 44 |
String input = "" |
| 45 |
+ "<manifest\n" |
| 46 |
+ " xmlns:android=\"http://schemas.android.com/apk/res/android\"\n" |
| 47 |
+ " package=\"com.example.lib3\">\n" |
| 48 |
+ "\n" |
| 49 |
+ " <application android:label=\"@string/lib_name\" />\n" |
| 50 |
+ "\n" |
| 51 |
+ "</manifest>"; |
| 52 |
|
| 53 |
XmlDocument xmlDocument = TestUtils.xmlDocumentFromString( |
| 54 |
TestUtils.sourceFile(getClass(), "testToolsPrefix()"), input); |
| 55 |
Optional<XmlElement> applicationOptional = xmlDocument.getRootNode() |
| 56 |
.getNodeByTypeAndKey(ManifestModel.NodeTypes.APPLICATION, null); |
| 57 |
assertTrue(applicationOptional.isPresent()); |
| 58 |
Node label = applicationOptional.get().getXml().getAttributes().item(0); |
| 59 |
assertEquals("label", label.getLocalName()); |
| 60 |
assertEquals(SdkConstants.ANDROID_URI, label.getNamespaceURI()); |
| 61 |
assertEquals("android:label", label.getNodeName()); |
| 62 |
} |
| 63 |
|
| 64 |
public void testPrettyPrint() throws IOException, SAXException, ParserConfigurationException { |
| 65 |
|
| 66 |
String input = "" |
| 67 |
+ "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" |
| 68 |
+ "<manifest xmlns:android=\"http://schemas.android.com/apk/res/android\"\n" |
| 69 |
+ " package=\"com.example.lib3\" >\n" |
| 70 |
+ "\n" |
| 71 |
+ " <!-- this is a comment -->\n" |
| 72 |
+ " <application android:label=\"@string/lib_name\" >\n" |
| 73 |
+ "\n" |
| 74 |
+ " <!-- The activity name will be expanded to its full FQCN by default. -->\n" |
| 75 |
+ " <activity\n" |
| 76 |
+ " android:name=\"com.example.lib3.MainActivity\"\n" |
| 77 |
+ " android:label=\"@string/app_name\" >\n" |
| 78 |
+ " <intent-filter>\n\n" |
| 79 |
+ " <!-- some other comment -->\n" |
| 80 |
+ " <action android:name=\"android.intent.action.MAIN\" />\n" |
| 81 |
+ "\n" |
| 82 |
+ " <category android:name=\"android.intent.category.LAUNCHER\" />\n" |
| 83 |
+ " </intent-filter>\n" |
| 84 |
+ " </activity>\n" |
| 85 |
+ " </application>\n" |
| 86 |
+ "\n" |
| 87 |
+ "</manifest>"; |
| 88 |
|
| 89 |
XmlDocument xmlDocument = TestUtils.xmlDocumentFromString( |
| 90 |
TestUtils.sourceFile(getClass(), "testPrettyPrint()"), input); |
| 91 |
Optional<XmlElement> applicationOptional = xmlDocument.getRootNode() |
| 92 |
.getNodeByTypeAndKey(ManifestModel.NodeTypes.APPLICATION, null); |
| 93 |
assertTrue(applicationOptional.isPresent()); |
| 94 |
String prettyPrinted = XmlPrettyPrinter |
| 95 |
.prettyPrint(xmlDocument.getXml(), XmlFormatPreferences.defaults(), |
| 96 |
XmlFormatStyle.get(xmlDocument.getRootNode().getXml()), null, false); |
| 97 |
assertEquals(input, prettyPrinted); |
| 98 |
} |
| 99 |
|
| 100 |
public void testToolsPrefix() throws IOException, SAXException, ParserConfigurationException { |
| 101 |
|
| 102 |
String input = "" |
| 103 |
+ "<manifest\n" |
| 104 |
+ " xmlns:android=\"http://schemas.android.com/apk/res/android\"\n" |
| 105 |
+ " xmlns:tools=\"http://schemas.android.com/tools\"\n" |
| 106 |
+ " package=\"com.example.lib3\">\n" |
| 107 |
+ "\n" |
| 108 |
+ " <application android:label=\"@string/lib_name\" tools:node=\"replace\"/>\n" |
| 109 |
+ "\n" |
| 110 |
+ "</manifest>"; |
| 111 |
|
| 112 |
XmlDocument xmlDocument = TestUtils.xmlDocumentFromString( |
| 113 |
TestUtils.sourceFile(getClass(), "testToolsPrefix()"),input); |
| 114 |
Optional<XmlElement> applicationOptional = xmlDocument.getRootNode() |
| 115 |
.getNodeByTypeAndKey(ManifestModel.NodeTypes.APPLICATION, null); |
| 116 |
assertTrue(applicationOptional.isPresent()); |
| 117 |
Element application = applicationOptional.get().getXml(); |
| 118 |
assertEquals(2, application.getAttributes().getLength()); |
| 119 |
Attr label = application.getAttributeNodeNS(SdkConstants.ANDROID_URI, "label"); |
| 120 |
assertEquals("android:label", label.getNodeName()); |
| 121 |
Attr tools = application.getAttributeNodeNS(SdkConstants.TOOLS_URI, |
| 122 |
NodeOperationType.NODE_LOCAL_NAME); |
| 123 |
assertEquals("replace", tools.getNodeValue()); |
| 124 |
|
| 125 |
// check positions. |
| 126 |
SourcePosition applicationPosition = applicationOptional.get().getPosition(); |
| 127 |
assertNotNull(applicationPosition); |
| 128 |
assertEquals(6, applicationPosition.getStartLine() + 1); |
| 129 |
assertEquals(5, applicationPosition.getStartColumn() + 1); |
| 130 |
|
| 131 |
XmlAttribute xmlAttribute = |
| 132 |
new XmlAttribute(applicationOptional.get(), tools, null /* AttributeModel */); |
| 133 |
SourcePosition toolsPosition = xmlAttribute.getPosition(); |
| 134 |
assertNotNull(toolsPosition); |
| 135 |
assertEquals(6, toolsPosition.getStartLine() + 1); |
| 136 |
assertEquals(51, toolsPosition.getStartColumn() + 1); |
| 137 |
} |
| 138 |
|
| 139 |
public void testUnusualPrefixes() |
| 140 |
throws IOException, SAXException, ParserConfigurationException { |
| 141 |
|
| 142 |
String input = "" |
| 143 |
+ "<manifest\n" |
| 144 |
+ " xmlns:x=\"http://schemas.android.com/apk/res/android\"\n" |
| 145 |
+ " xmlns:y=\"http://schemas.android.com/tools\"\n" |
| 146 |
+ " package=\"com.example.lib3\">\n" |
| 147 |
+ "\n" |
| 148 |
+ " <application x:label=\"@string/lib_name\" y:node=\"replace\"/>\n" |
| 149 |
+ "\n" |
| 150 |
+ "</manifest>"; |
| 151 |
|
| 152 |
XmlDocument xmlDocument = TestUtils.xmlDocumentFromString( |
| 153 |
TestUtils.sourceFile(getClass(), "testUnusualPrefixes()"), input); |
| 154 |
Optional<XmlElement> applicationOptional = xmlDocument.getRootNode() |
| 155 |
.getNodeByTypeAndKey(ManifestModel.NodeTypes.APPLICATION, null); |
| 156 |
assertTrue(applicationOptional.isPresent()); |
| 157 |
Element application = applicationOptional.get().getXml(); |
| 158 |
assertEquals(2, application.getAttributes().getLength()); |
| 159 |
Node label = application.getAttributeNodeNS(SdkConstants.ANDROID_URI, "label"); |
| 160 |
assertEquals("x:label", label.getNodeName()); |
| 161 |
Node tools = application.getAttributeNodeNS(SdkConstants.TOOLS_URI, |
| 162 |
NodeOperationType.NODE_LOCAL_NAME); |
| 163 |
assertEquals("y:node", tools.getNodeName()); |
| 164 |
assertEquals("replace", tools.getNodeValue()); |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
} |