| 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.sdklib.mock.MockLog; |
| 20 |
import com.google.common.base.Strings; |
| 21 |
|
| 22 |
import junit.framework.TestCase; |
| 23 |
|
| 24 |
import org.w3c.dom.Element; |
| 25 |
import org.w3c.dom.Node; |
| 26 |
import org.w3c.dom.NodeList; |
| 27 |
import org.xml.sax.SAXException; |
| 28 |
|
| 29 |
import java.io.IOException; |
| 30 |
import java.util.Optional; |
| 31 |
|
| 32 |
import javax.xml.parsers.ParserConfigurationException; |
| 33 |
|
| 34 |
/** |
| 35 |
* Tests for the {@link com.android.manifmerger.ToolsInstructionsCleaner} class. |
| 36 |
*/ |
| 37 |
public class ToolsInstructionsCleanerTest extends TestCase { |
| 38 |
|
| 39 |
public void testNodeRemoveOperation() |
| 40 |
throws ParserConfigurationException, SAXException, IOException { |
| 41 |
MockLog mockLog = new MockLog(); |
| 42 |
String main = "" |
| 43 |
+ "<manifest\n" |
| 44 |
+ " xmlns:android=\"http://schemas.android.com/apk/res/android\"\n" |
| 45 |
+ " xmlns:tools=\"http://schemas.android.com/tools\"\n" |
| 46 |
+ " package=\"com.example.lib3\">\n" |
| 47 |
+ "\n" |
| 48 |
+ " <application android:label=\"@string/lib_name\">\n" |
| 49 |
+ "\n" |
| 50 |
+ " <activity android:name=\"activityOne\" tools:node=\"remove\"/>\n" |
| 51 |
+ " </application>" |
| 52 |
+ "\n" |
| 53 |
+ "</manifest>"; |
| 54 |
|
| 55 |
XmlDocument mainDocument = TestUtils.xmlDocumentFromString( |
| 56 |
TestUtils.sourceFile(getClass(), "testNodeRemoveOperation"), main); |
| 57 |
|
| 58 |
Element rootElement = mainDocument.getRootNode().getXml(); |
| 59 |
ToolsInstructionsCleaner.cleanToolsReferences( |
| 60 |
ManifestMerger2.MergeType.APPLICATION, mainDocument, mockLog); |
| 61 |
|
| 62 |
Optional<Element> application = getChildElementByName(rootElement, "application"); |
| 63 |
assertTrue(application.isPresent()); |
| 64 |
|
| 65 |
Optional<Element> activity = getChildElementByName(application.get(), "activity"); |
| 66 |
// ensure the activity did get deleted. |
| 67 |
assertFalse(activity.isPresent()); |
| 68 |
} |
| 69 |
|
| 70 |
public void testNodeWithChildrenRemoveOperation() |
| 71 |
throws ParserConfigurationException, SAXException, IOException { |
| 72 |
MockLog mockLog = new MockLog(); |
| 73 |
String main = "<manifest xmlns:android=\"http://schemas.android.com/apk/res/android\"\n" |
| 74 |
+ " xmlns:tools=\"http://schemas.android.com/tools\"\n" |
| 75 |
+ " package=\"com.example.lib3\" >\n" |
| 76 |
+ "\n" |
| 77 |
+ " <application>\n" |
| 78 |
+ " <activity android:name=\"com.example.lib3.activityOne\" >\n" |
| 79 |
+ " <intent-filter tools:node=\"remove\" >\n" |
| 80 |
+ " <action android:name=\"android.intent.action.VIEW\" />\n" |
| 81 |
+ " <category android:name=\"android.intent.category.DEFAULT\" />\n" |
| 82 |
+ " <category android:name=\"android.intent.category.BROWSABLE\" />\n" |
| 83 |
+ " </intent-filter>\n" |
| 84 |
+ " </activity>\n" |
| 85 |
+ " </application>\n" |
| 86 |
+ "\n" |
| 87 |
+ "</manifest>"; |
| 88 |
|
| 89 |
XmlDocument mainDocument = TestUtils.xmlDocumentFromString( |
| 90 |
TestUtils.sourceFile(getClass(), "testNodeRemoveWithChildrenOperation"), main); |
| 91 |
|
| 92 |
Element rootElement = mainDocument.getRootNode().getXml(); |
| 93 |
ToolsInstructionsCleaner.cleanToolsReferences( |
| 94 |
ManifestMerger2.MergeType.APPLICATION, mainDocument, mockLog); |
| 95 |
|
| 96 |
Optional<Element> application = getChildElementByName(rootElement, "application"); |
| 97 |
assertTrue(application.isPresent()); |
| 98 |
|
| 99 |
Optional<Element> activity = getChildElementByName(application.get(), "activity"); |
| 100 |
assertTrue(activity.isPresent()); |
| 101 |
Optional<Element> intentFilter = getChildElementByName(application.get(), "intent-filter"); |
| 102 |
|
| 103 |
// ensure the intent-filter did get deleted. |
| 104 |
assertFalse(intentFilter.isPresent()); |
| 105 |
} |
| 106 |
|
| 107 |
public void testInvalidToolsRemoveOperation() |
| 108 |
throws ParserConfigurationException, SAXException, IOException { |
| 109 |
MockLog mockLog = new MockLog(); |
| 110 |
String main = "" |
| 111 |
+ "<manifest\n" |
| 112 |
+ " xmlns:android=\"http://schemas.android.com/apk/res/android\"\n" |
| 113 |
+ " xmlns:tools=\"http://schemas.android.com/tools\"\n" |
| 114 |
+ " package=\"com.example.lib3\"" |
| 115 |
+ " tools:node=\"remove\">\n" |
| 116 |
+ "\n" |
| 117 |
+ "</manifest>"; |
| 118 |
|
| 119 |
XmlDocument mainDocument = TestUtils.xmlDocumentFromString( |
| 120 |
TestUtils.sourceFile(getClass(), "testNodeRemoveOperation"), main); |
| 121 |
|
| 122 |
assertNull(ToolsInstructionsCleaner.cleanToolsReferences( |
| 123 |
ManifestMerger2.MergeType.APPLICATION, mainDocument, mockLog)); |
| 124 |
} |
| 125 |
|
| 126 |
public void testInvalidToolsRemoveAllOperation() |
| 127 |
throws ParserConfigurationException, SAXException, IOException { |
| 128 |
MockLog mockLog = new MockLog(); |
| 129 |
String main = "" |
| 130 |
+ "<manifest\n" |
| 131 |
+ " xmlns:android=\"http://schemas.android.com/apk/res/android\"\n" |
| 132 |
+ " xmlns:tools=\"http://schemas.android.com/tools\"\n" |
| 133 |
+ " package=\"com.example.lib3\"" |
| 134 |
+ " tools:node=\"removeAll\">\n" |
| 135 |
+ "\n" |
| 136 |
+ "</manifest>"; |
| 137 |
|
| 138 |
XmlDocument mainDocument = TestUtils.xmlDocumentFromString( |
| 139 |
TestUtils.sourceFile(getClass(), "testNodeRemoveOperation"), main); |
| 140 |
|
| 141 |
assertNull(ToolsInstructionsCleaner.cleanToolsReferences( |
| 142 |
ManifestMerger2.MergeType.APPLICATION, mainDocument, mockLog)); |
| 143 |
} |
| 144 |
|
| 145 |
public void testNodeReplaceOperation() |
| 146 |
throws ParserConfigurationException, SAXException, IOException { |
| 147 |
MockLog mockLog = new MockLog(); |
| 148 |
String main = "" |
| 149 |
+ "<manifest\n" |
| 150 |
+ " xmlns:android=\"http://schemas.android.com/apk/res/android\"\n" |
| 151 |
+ " xmlns:tools=\"http://schemas.android.com/tools\"\n" |
| 152 |
+ " package=\"com.example.lib3\">\n" |
| 153 |
+ "\n" |
| 154 |
+ " <application android:label=\"@string/lib_name\">\n" |
| 155 |
+ " <activity android:name=\"activityOne\" tools:node=\"replace\"/>\n" |
| 156 |
+ " </application>\n" |
| 157 |
+ "\n" |
| 158 |
+ "</manifest>"; |
| 159 |
|
| 160 |
XmlDocument mainDocument = TestUtils.xmlDocumentFromString( |
| 161 |
TestUtils.sourceFile(getClass(), "testNodeReplaceOperation"), main); |
| 162 |
|
| 163 |
Element rootElement = mainDocument.getRootNode().getXml(); |
| 164 |
ToolsInstructionsCleaner.cleanToolsReferences( |
| 165 |
ManifestMerger2.MergeType.APPLICATION, mainDocument, mockLog); |
| 166 |
|
| 167 |
Optional<Element> application = getChildElementByName(rootElement, "application"); |
| 168 |
assertTrue(application.isPresent()); |
| 169 |
|
| 170 |
Optional<Element> activity = getChildElementByName(application.get(), "activity"); |
| 171 |
// ensure the activity did not get deleted. |
| 172 |
assertTrue(activity.isPresent()); |
| 173 |
} |
| 174 |
|
| 175 |
public void testAttributeRemoveOperation() |
| 176 |
throws ParserConfigurationException, SAXException, IOException { |
| 177 |
MockLog mockLog = new MockLog(); |
| 178 |
String main = "" |
| 179 |
+ "<manifest\n" |
| 180 |
+ " xmlns:android=\"http://schemas.android.com/apk/res/android\"\n" |
| 181 |
+ " xmlns:tools=\"http://schemas.android.com/tools\"\n" |
| 182 |
+ " package=\"com.example.lib3\">\n" |
| 183 |
+ "\n" |
| 184 |
+ " <application android:label=\"@string/lib_name\">\n" |
| 185 |
+ " <activity android:name=\"activityOne\" tools:remove=\"exported\"/>\n" |
| 186 |
+ " </application>\n" |
| 187 |
+ "</manifest>"; |
| 188 |
|
| 189 |
XmlDocument mainDocument = TestUtils.xmlDocumentFromString( |
| 190 |
TestUtils.sourceFile(getClass(), "testAttributeRemoveOperation"), main); |
| 191 |
|
| 192 |
Element rootElement = mainDocument.getRootNode().getXml(); |
| 193 |
ToolsInstructionsCleaner.cleanToolsReferences(ManifestMerger2.MergeType.APPLICATION, mainDocument, mockLog); |
| 194 |
|
| 195 |
Optional<Element> application = getChildElementByName(rootElement, "application"); |
| 196 |
assertTrue(application.isPresent()); |
| 197 |
|
| 198 |
Optional<Element> activity = getChildElementByName(application.get(), "activity"); |
| 199 |
// ensure the activity did not get deleted. |
| 200 |
assertTrue(activity.isPresent()); |
| 201 |
assertEquals(1, activity.get().getAttributes().getLength()); |
| 202 |
assertNotNull(activity.get().getAttribute("android:name")); |
| 203 |
} |
| 204 |
|
| 205 |
public void testSelectorRemoval() |
| 206 |
throws ParserConfigurationException, SAXException, IOException { |
| 207 |
MockLog mockLog = new MockLog(); |
| 208 |
String main = "" |
| 209 |
+ "<manifest\n" |
| 210 |
+ " xmlns:android=\"http://schemas.android.com/apk/res/android\"\n" |
| 211 |
+ " xmlns:tools=\"http://schemas.android.com/tools\"\n" |
| 212 |
+ " package=\"com.example.lib3\">\n" |
| 213 |
+ "\n" |
| 214 |
+ " <application android:label=\"@string/lib_name\">\n" |
| 215 |
+ " <activity android:name=\"activityOne\" " |
| 216 |
+ " tools:node=\"remove\" tools:selector=\"foo\"/>\n" |
| 217 |
+ " </application>\n" |
| 218 |
+ "\n" |
| 219 |
+ "</manifest>"; |
| 220 |
|
| 221 |
XmlDocument mainDocument = TestUtils.xmlDocumentFromString( |
| 222 |
TestUtils.sourceFile(getClass(), "testSelectorRemoval"), main); |
| 223 |
|
| 224 |
Element rootElement = mainDocument.getRootNode().getXml(); |
| 225 |
ToolsInstructionsCleaner.cleanToolsReferences( |
| 226 |
ManifestMerger2.MergeType.APPLICATION, mainDocument, mockLog); |
| 227 |
|
| 228 |
Optional<Element> application = getChildElementByName(rootElement, "application"); |
| 229 |
assertTrue(application.isPresent()); |
| 230 |
|
| 231 |
Optional<Element> activity = getChildElementByName(application.get(), "activity"); |
| 232 |
// ensure the activity did not get deleted since it has a selector |
| 233 |
assertTrue(activity.isPresent()); |
| 234 |
assertTrue(Strings.isNullOrEmpty(activity.get().getAttribute("tools:selector"))); |
| 235 |
} |
| 236 |
|
| 237 |
public void testOtherToolInstructionRemoval() |
| 238 |
throws ParserConfigurationException, SAXException, IOException { |
| 239 |
MockLog mockLog = new MockLog(); |
| 240 |
String main = "" |
| 241 |
+ "<manifest\n" |
| 242 |
+ " xmlns:android=\"http://schemas.android.com/apk/res/android\"\n" |
| 243 |
+ " xmlns:tools=\"http://schemas.android.com/tools\"\n" |
| 244 |
+ " package=\"com.example.lib3\">\n" |
| 245 |
+ "\n" |
| 246 |
+ " <application android:label=\"@string/lib_name\">\n" |
| 247 |
+ " <activity android:name=\"activityOne\" tools:ignore=\"value\"/>\n" |
| 248 |
+ " </application>\n" |
| 249 |
+ "\n" |
| 250 |
+ "</manifest>"; |
| 251 |
|
| 252 |
XmlDocument mainDocument = TestUtils.xmlDocumentFromString( |
| 253 |
TestUtils.sourceFile(getClass(), "testNodeReplaceOperation"), main); |
| 254 |
|
| 255 |
Element rootElement = mainDocument.getRootNode().getXml(); |
| 256 |
ToolsInstructionsCleaner.cleanToolsReferences( |
| 257 |
ManifestMerger2.MergeType.APPLICATION, mainDocument, mockLog); |
| 258 |
|
| 259 |
Optional<Element> application = getChildElementByName(rootElement, "application"); |
| 260 |
assertTrue(application.isPresent()); |
| 261 |
|
| 262 |
Optional<Element> activity = getChildElementByName(application.get(), "activity"); |
| 263 |
// ensure the activity did not get deleted. |
| 264 |
assertTrue(activity.isPresent()); |
| 265 |
// ensure tools:ignore got deleted. |
| 266 |
assertNull(activity.get().getAttributeNodeNS("http://schemas.android.com/tools", "ignore")); |
| 267 |
} |
| 268 |
|
| 269 |
private static Optional<Element> getChildElementByName(Element parent, String name) { |
| 270 |
NodeList childNodes = parent.getChildNodes(); |
| 271 |
for (int i = 0; i < childNodes.getLength(); i++) { |
| 272 |
Node item = childNodes.item(i); |
| 273 |
if (item.getNodeType() == Node.ELEMENT_NODE |
| 274 |
&& item.getNodeName().equals(name)) { |
| 275 |
return Optional.of((Element) item); |
| 276 |
} |
| 277 |
} |
| 278 |
return Optional.empty(); |
| 279 |
} |
| 280 |
} |