| 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.Matchers.any; |
| 20 |
import static org.mockito.Matchers.eq; |
| 21 |
import static org.mockito.Mockito.verify; |
| 22 |
import static org.mockito.Mockito.when; |
| 23 |
|
| 24 |
import com.android.SdkConstants; |
| 25 |
import com.android.utils.ILogger; |
| 26 |
import com.android.xml.AndroidManifest; |
| 27 |
import com.google.common.collect.ImmutableList; |
| 28 |
|
| 29 |
import junit.framework.TestCase; |
| 30 |
|
| 31 |
import org.mockito.Mock; |
| 32 |
import org.mockito.Mockito; |
| 33 |
import org.mockito.MockitoAnnotations; |
| 34 |
import org.w3c.dom.Attr; |
| 35 |
import org.w3c.dom.Element; |
| 36 |
import org.w3c.dom.NamedNodeMap; |
| 37 |
import org.w3c.dom.NodeList; |
| 38 |
import org.xml.sax.SAXException; |
| 39 |
|
| 40 |
import java.io.IOException; |
| 41 |
import java.util.List; |
| 42 |
|
| 43 |
import javax.xml.parsers.ParserConfigurationException; |
| 44 |
|
| 45 |
/** |
| 46 |
* Tests for the {@link ElementsTrimmer} class |
| 47 |
*/ |
| 48 |
public class ElementsTrimmerTest extends TestCase { |
| 49 |
|
| 50 |
@Mock |
| 51 |
ILogger mILogger; |
| 52 |
|
| 53 |
@Override |
| 54 |
protected void setUp() throws Exception { |
| 55 |
super.setUp(); |
| 56 |
MockitoAnnotations.initMocks(this); |
| 57 |
} |
| 58 |
|
| 59 |
public void testNoUseFeaturesDeclaration() |
| 60 |
throws ParserConfigurationException, SAXException, IOException { |
| 61 |
String input = "" |
| 62 |
+ "<manifest\n" |
| 63 |
+ " xmlns:android=\"http://schemas.android.com/apk/res/android\"\n" |
| 64 |
+ " xmlns:tools=\"http://schemas.android.com/tools\"\n" |
| 65 |
+ " package=\"com.example.lib3\">\n" |
| 66 |
+ "\n" |
| 67 |
+ " <permission android:name=\"permissionOne\" " |
| 68 |
+ " permissionGroup=\"permissionGroupOne\"/>\n" |
| 69 |
+ "\n" |
| 70 |
+ "</manifest>"; |
| 71 |
|
| 72 |
XmlDocument xmlDocument = TestUtils.xmlDocumentFromString( |
| 73 |
TestUtils.sourceFile(getClass(), "testNoUseFeaturesDeclaration"), input); |
| 74 |
|
| 75 |
MergingReport.Builder mergingReport = new MergingReport.Builder(mILogger); |
| 76 |
ElementsTrimmer.trim(xmlDocument, mergingReport); |
| 77 |
assertFalse(mergingReport.hasErrors()); |
| 78 |
Mockito.verifyZeroInteractions(mILogger); |
| 79 |
assertEquals(0, mergingReport.getActionRecorder().build().getNodeKeys().size()); |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
public void testNothingToTrim() |
| 84 |
throws ParserConfigurationException, SAXException, IOException { |
| 85 |
|
| 86 |
String input = "" |
| 87 |
+ "<manifest\n" |
| 88 |
+ " xmlns:android=\"http://schemas.android.com/apk/res/android\"\n" |
| 89 |
+ " xmlns:tools=\"http://schemas.android.com/tools\"\n" |
| 90 |
+ " package=\"com.example.lib3\">\n" |
| 91 |
+ "\n" |
| 92 |
+ " <uses-feature" |
| 93 |
+ " android:required=\"false\"" |
| 94 |
+ " android:glEsVersion=\"0x0002000\"/>\n" |
| 95 |
+ "\n" |
| 96 |
+ "</manifest>"; |
| 97 |
|
| 98 |
XmlDocument xmlDocument = TestUtils.xmlDocumentFromString( |
| 99 |
TestUtils.sourceFile(getClass(), "testNothingToTrim"), input); |
| 100 |
|
| 101 |
MergingReport.Builder mergingReport = new MergingReport.Builder(mILogger); |
| 102 |
ElementsTrimmer.trim(xmlDocument, mergingReport); |
| 103 |
assertFalse(mergingReport.hasErrors()); |
| 104 |
Mockito.verifyZeroInteractions(mILogger); |
| 105 |
assertEquals(0, mergingReport.getActionRecorder().build().getNodeKeys().size()); |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
public void testMultipleAboveTwoResults() |
| 110 |
throws ParserConfigurationException, SAXException, IOException { |
| 111 |
|
| 112 |
String input = "" |
| 113 |
+ "<manifest\n" |
| 114 |
+ " xmlns:android=\"http://schemas.android.com/apk/res/android\"\n" |
| 115 |
+ " xmlns:tools=\"http://schemas.android.com/tools\"\n" |
| 116 |
+ " package=\"com.example.lib3\">\n" |
| 117 |
+ "\n" |
| 118 |
+ " <uses-feature" |
| 119 |
+ " android:required=\"true\"" |
| 120 |
+ " android:glEsVersion=\"0x00020000\"/>\n" |
| 121 |
+ " <uses-feature" |
| 122 |
+ " android:required=\"false\"" |
| 123 |
+ " android:glEsVersion=\"0x00021000\"/>\n" |
| 124 |
+ " <uses-feature" |
| 125 |
+ " android:glEsVersion=\"0x00022000\"/>\n" |
| 126 |
+ " <uses-feature" |
| 127 |
+ " android:required=\"false\"" |
| 128 |
+ " android:glEsVersion=\"0x00030000\"/>\n" |
| 129 |
+ "\n" |
| 130 |
+ "</manifest>"; |
| 131 |
|
| 132 |
XmlDocument xmlDocument = TestUtils.xmlDocumentFromString( |
| 133 |
TestUtils.sourceFile(getClass(), "testMultipleAboveTwoResults"), input); |
| 134 |
|
| 135 |
MergingReport.Builder mergingReport = new MergingReport.Builder(mILogger); |
| 136 |
ElementsTrimmer.trim(xmlDocument, mergingReport); |
| 137 |
assertFalse(mergingReport.hasErrors()); |
| 138 |
Mockito.verifyZeroInteractions(mILogger); |
| 139 |
|
| 140 |
// check action recording. |
| 141 |
checkActionsRecording(mergingReport, 2); |
| 142 |
|
| 143 |
// check results. |
| 144 |
checkResults(xmlDocument, ImmutableList.of("0x00030000", "0x00022000")); |
| 145 |
} |
| 146 |
|
| 147 |
public void testSingleAboveTwoResults() |
| 148 |
throws ParserConfigurationException, SAXException, IOException { |
| 149 |
|
| 150 |
String input = "" |
| 151 |
+ "<manifest\n" |
| 152 |
+ " xmlns:android=\"http://schemas.android.com/apk/res/android\"\n" |
| 153 |
+ " xmlns:tools=\"http://schemas.android.com/tools\"\n" |
| 154 |
+ " package=\"com.example.lib3\">\n" |
| 155 |
+ "\n" |
| 156 |
+ " <uses-feature android:glEsVersion=\"0x00020000\"/>\n" |
| 157 |
+ " <uses-feature android:glEsVersion=\"0x00021000\"/>\n" |
| 158 |
+ " <uses-feature android:glEsVersion=\"0x00030000\"/>\n" |
| 159 |
+ "\n" |
| 160 |
+ "</manifest>"; |
| 161 |
|
| 162 |
XmlDocument xmlDocument = TestUtils.xmlDocumentFromString( |
| 163 |
TestUtils.sourceFile(getClass(), "testSingleAboveTwoResults"), input); |
| 164 |
|
| 165 |
MergingReport.Builder mergingReport = new MergingReport.Builder(mILogger); |
| 166 |
ElementsTrimmer.trim(xmlDocument, mergingReport); |
| 167 |
assertFalse(mergingReport.hasErrors()); |
| 168 |
Mockito.verifyZeroInteractions(mILogger); |
| 169 |
|
| 170 |
// check action recording. |
| 171 |
checkActionsRecording(mergingReport, 2); |
| 172 |
|
| 173 |
// check results. |
| 174 |
checkResults(xmlDocument, ImmutableList.of("0x00030000")); |
| 175 |
} |
| 176 |
|
| 177 |
public void testMultipleBelowTwoResults() |
| 178 |
throws ParserConfigurationException, SAXException, IOException { |
| 179 |
|
| 180 |
String input = "" |
| 181 |
+ "<manifest\n" |
| 182 |
+ " xmlns:android=\"http://schemas.android.com/apk/res/android\"\n" |
| 183 |
+ " xmlns:tools=\"http://schemas.android.com/tools\"\n" |
| 184 |
+ " package=\"com.example.lib3\">\n" |
| 185 |
+ "\n" |
| 186 |
+ " <uses-feature" |
| 187 |
+ " android:required=\"true\"" |
| 188 |
+ " android:glEsVersion=\"0x00010000\"/>\n" |
| 189 |
+ " <uses-feature" |
| 190 |
+ " android:glEsVersion=\"0x00011000\"/>\n" |
| 191 |
+ " <uses-feature" |
| 192 |
+ " android:required=\"false\"" |
| 193 |
+ " android:glEsVersion=\"0x00012000\"/>\n" |
| 194 |
+ "\n" |
| 195 |
+ "</manifest>"; |
| 196 |
|
| 197 |
XmlDocument xmlDocument = TestUtils.xmlDocumentFromString( |
| 198 |
TestUtils.sourceFile(getClass(), "testMultipleBelowTwoResults"), input); |
| 199 |
|
| 200 |
MergingReport.Builder mergingReport = new MergingReport.Builder(mILogger); |
| 201 |
ElementsTrimmer.trim(xmlDocument, mergingReport); |
| 202 |
assertFalse(mergingReport.hasErrors()); |
| 203 |
Mockito.verifyZeroInteractions(mILogger); |
| 204 |
|
| 205 |
// check action recording. |
| 206 |
checkActionsRecording(mergingReport, 1); |
| 207 |
|
| 208 |
// check results. |
| 209 |
checkResults(xmlDocument, ImmutableList.of("0x00011000", "0x00012000")); |
| 210 |
} |
| 211 |
|
| 212 |
public void testSingleBelowTwoResults() |
| 213 |
throws ParserConfigurationException, SAXException, IOException { |
| 214 |
|
| 215 |
String input = "" |
| 216 |
+ "<manifest\n" |
| 217 |
+ " xmlns:android=\"http://schemas.android.com/apk/res/android\"\n" |
| 218 |
+ " xmlns:tools=\"http://schemas.android.com/tools\"\n" |
| 219 |
+ " package=\"com.example.lib3\">\n" |
| 220 |
+ "\n" |
| 221 |
+ " <uses-feature android:glEsVersion=\"0x00010000\"/>\n" |
| 222 |
+ " <uses-feature android:glEsVersion=\"0x00011000\"/>\n" |
| 223 |
+ " <uses-feature android:glEsVersion=\"0x00012000\"/>\n" |
| 224 |
+ "\n" |
| 225 |
+ "</manifest>"; |
| 226 |
|
| 227 |
XmlDocument xmlDocument = TestUtils.xmlDocumentFromString( |
| 228 |
TestUtils.sourceFile(getClass(), "testSingleBelowTwoResults"), input); |
| 229 |
|
| 230 |
MergingReport.Builder mergingReport = new MergingReport.Builder(mILogger); |
| 231 |
ElementsTrimmer.trim(xmlDocument, mergingReport); |
| 232 |
assertFalse(mergingReport.hasErrors()); |
| 233 |
Mockito.verifyZeroInteractions(mILogger); |
| 234 |
|
| 235 |
// check action recording. |
| 236 |
checkActionsRecording(mergingReport, 2); |
| 237 |
|
| 238 |
// check results. |
| 239 |
checkResults(xmlDocument, ImmutableList.of("0x00012000")); |
| 240 |
} |
| 241 |
|
| 242 |
public void testMultipleAboveAndBelowTwoResults() |
| 243 |
throws ParserConfigurationException, SAXException, IOException { |
| 244 |
|
| 245 |
String input = "" |
| 246 |
+ "<manifest\n" |
| 247 |
+ " xmlns:android=\"http://schemas.android.com/apk/res/android\"\n" |
| 248 |
+ " xmlns:tools=\"http://schemas.android.com/tools\"\n" |
| 249 |
+ " package=\"com.example.lib3\">\n" |
| 250 |
+ "\n" |
| 251 |
+ " <uses-feature" |
| 252 |
+ " android:required=\"true\"" |
| 253 |
+ " android:glEsVersion=\"0x00010000\"/>\n" |
| 254 |
+ " <uses-feature" |
| 255 |
+ " android:glEsVersion=\"0x00011000\"/>\n" |
| 256 |
+ " <uses-feature" |
| 257 |
+ " android:required=\"false\"" |
| 258 |
+ " android:glEsVersion=\"0x00012000\"/>\n" |
| 259 |
+ " <uses-feature" |
| 260 |
+ " android:required=\"true\"" |
| 261 |
+ " android:glEsVersion=\"0x00020000\"/>\n" |
| 262 |
+ " <uses-feature" |
| 263 |
+ " android:required=\"false\"" |
| 264 |
+ " android:glEsVersion=\"0x00021000\"/>\n" |
| 265 |
+ " <uses-feature" |
| 266 |
+ " android:glEsVersion=\"0x00022000\"/>\n" |
| 267 |
+ " <uses-feature" |
| 268 |
+ " android:required=\"false\"" |
| 269 |
+ " android:glEsVersion=\"0x00030000\"/>\n" |
| 270 |
+ "\n" |
| 271 |
+ "</manifest>"; |
| 272 |
|
| 273 |
XmlDocument xmlDocument = TestUtils.xmlDocumentFromString( |
| 274 |
TestUtils.sourceFile(getClass(), "testMultipleAboveAndBelowTwoResults"), input); |
| 275 |
|
| 276 |
MergingReport.Builder mergingReport = new MergingReport.Builder(mILogger); |
| 277 |
ElementsTrimmer.trim(xmlDocument, mergingReport); |
| 278 |
assertFalse(mergingReport.hasErrors()); |
| 279 |
Mockito.verifyZeroInteractions(mILogger); |
| 280 |
|
| 281 |
// check action recording. |
| 282 |
checkActionsRecording(mergingReport, 3); |
| 283 |
|
| 284 |
// check results. |
| 285 |
checkResults(xmlDocument, |
| 286 |
ImmutableList.of("0x00011000", "0x00012000", "0x00022000", "0x00030000")); |
| 287 |
} |
| 288 |
|
| 289 |
public void testUsesFeatureSplit_attributeDeleted() |
| 290 |
throws ParserConfigurationException, SAXException, IOException { |
| 291 |
|
| 292 |
String input = "" |
| 293 |
+ "<manifest\n" |
| 294 |
+ " xmlns:android=\"http://schemas.android.com/apk/res/android\"\n" |
| 295 |
+ " xmlns:tools=\"http://schemas.android.com/tools\"\n" |
| 296 |
+ " package=\"com.example.lib3\">\n" |
| 297 |
+ "\n" |
| 298 |
+ " <uses-feature android:name=\"@string/lib_name\"" |
| 299 |
+ " android:required=\"false\"" |
| 300 |
+ " android:glEsVersion=\"0x00020000\"/>\n" |
| 301 |
+ " <uses-feature" |
| 302 |
+ " android:required=\"false\"" |
| 303 |
+ " android:glEsVersion=\"0x00030000\"/>\n" |
| 304 |
+ "\n" |
| 305 |
+ "</manifest>"; |
| 306 |
|
| 307 |
XmlDocument xmlDocument = TestUtils.xmlDocumentFromString( |
| 308 |
TestUtils.sourceFile(getClass(), "testUsesFeatureSplit"), input); |
| 309 |
|
| 310 |
ActionRecorder mockActionRecorder = Mockito.mock(ActionRecorder.class); |
| 311 |
MergingReport.Builder mockReport = Mockito.mock(MergingReport.Builder.class); |
| 312 |
when(mockReport.getActionRecorder()).thenReturn(mockActionRecorder); |
| 313 |
ElementsTrimmer.trim(xmlDocument, mockReport); |
| 314 |
|
| 315 |
// check that we have now 2 uses-feature with separated keys. |
| 316 |
NodeList elementsByTagName = xmlDocument.getRootNode().getXml() |
| 317 |
.getElementsByTagName("uses-feature"); |
| 318 |
assertEquals(2, elementsByTagName.getLength()); |
| 319 |
|
| 320 |
// verify the action was recorded. |
| 321 |
verify(mockActionRecorder).recordAttributeAction(any(XmlAttribute.class), |
| 322 |
eq(Actions.ActionType.REJECTED), (AttributeOperationType) eq(null)); |
| 323 |
|
| 324 |
for (int i = 0; i < elementsByTagName.getLength(); i++) { |
| 325 |
NamedNodeMap attributes = elementsByTagName.item(i).getAttributes(); |
| 326 |
assertEquals(2, attributes.getLength()); |
| 327 |
ensureOnlyOneKey(attributes, ManifestModel.NodeTypes.USES_FEATURE); |
| 328 |
} |
| 329 |
} |
| 330 |
|
| 331 |
public void testUsesFeatureSplit_elementDeleted() |
| 332 |
throws ParserConfigurationException, SAXException, IOException { |
| 333 |
|
| 334 |
String input = "" |
| 335 |
+ "<manifest\n" |
| 336 |
+ " xmlns:android=\"http://schemas.android.com/apk/res/android\"\n" |
| 337 |
+ " xmlns:tools=\"http://schemas.android.com/tools\"\n" |
| 338 |
+ " package=\"com.example.lib3\">\n" |
| 339 |
+ "\n" |
| 340 |
+ " <uses-feature android:required=\"false\"" |
| 341 |
+ " android:glEsVersion=\"0x00020000\"/>\n" |
| 342 |
+ " <uses-feature" |
| 343 |
+ " android:required=\"false\"" |
| 344 |
+ " android:glEsVersion=\"0x00030000\"/>\n" |
| 345 |
+ "\n" |
| 346 |
+ "</manifest>"; |
| 347 |
|
| 348 |
XmlDocument xmlDocument = TestUtils.xmlDocumentFromString( |
| 349 |
TestUtils.sourceFile(getClass(), "testUsesFeatureSplit"), input); |
| 350 |
|
| 351 |
ActionRecorder mockActionRecorder = Mockito.mock(ActionRecorder.class); |
| 352 |
MergingReport.Builder mockReport = Mockito.mock(MergingReport.Builder.class); |
| 353 |
when(mockReport.getActionRecorder()).thenReturn(mockActionRecorder); |
| 354 |
ElementsTrimmer.trim(xmlDocument, mockReport); |
| 355 |
|
| 356 |
// check that we have now 2 uses-feature with separated keys. |
| 357 |
NodeList elementsByTagName = xmlDocument.getRootNode().getXml() |
| 358 |
.getElementsByTagName("uses-feature"); |
| 359 |
assertEquals(1, elementsByTagName.getLength()); |
| 360 |
|
| 361 |
// verify the action was recorded. |
| 362 |
verify(mockActionRecorder).recordNodeAction(any(XmlElement.class), |
| 363 |
eq(Actions.ActionType.REJECTED)); |
| 364 |
|
| 365 |
for (int i = 0; i < elementsByTagName.getLength(); i++) { |
| 366 |
NamedNodeMap attributes = elementsByTagName.item(i).getAttributes(); |
| 367 |
assertEquals(2, attributes.getLength()); |
| 368 |
ensureOnlyOneKey(attributes, ManifestModel.NodeTypes.USES_FEATURE); |
| 369 |
} |
| 370 |
} |
| 371 |
|
| 372 |
private void ensureOnlyOneKey(NamedNodeMap namedNodeMap, ManifestModel.NodeTypes nodeType) { |
| 373 |
String firstKey = null; |
| 374 |
ImmutableList<String> keyAttributesNames = |
| 375 |
nodeType.getNodeKeyResolver().getKeyAttributesNames(); |
| 376 |
for (String keyAttributesName : keyAttributesNames) { |
| 377 |
if (namedNodeMap.getNamedItemNS(SdkConstants. ANDROID_URI, keyAttributesName) != null) { |
| 378 |
if (firstKey != null) { |
| 379 |
fail("Found 2 keys : " + firstKey + " and " + keyAttributesName); |
| 380 |
} |
| 381 |
firstKey = keyAttributesName; |
| 382 |
} |
| 383 |
} |
| 384 |
} |
| 385 |
|
| 386 |
|
| 387 |
private static void checkActionsRecording( |
| 388 |
MergingReport.Builder mergingReport, |
| 389 |
int expectedActionsNumber) { |
| 390 |
|
| 391 |
Actions actions = mergingReport.build().getActions(); |
| 392 |
assertEquals(expectedActionsNumber, actions.getNodeKeys().size()); |
| 393 |
for (XmlNode.NodeKey nodeKey : actions.getNodeKeys()) { |
| 394 |
assertEquals(1, actions.getNodeRecords(nodeKey).size()); |
| 395 |
assertEquals(Actions.ActionType.REJECTED, |
| 396 |
actions.getNodeRecords(nodeKey).get(0).getActionType()); |
| 397 |
} |
| 398 |
} |
| 399 |
|
| 400 |
private static void checkResults(XmlDocument xmlDocument, List<String> expectedVersions) { |
| 401 |
NodeList elementsByTagName = xmlDocument.getRootNode().getXml() |
| 402 |
.getElementsByTagName("uses-feature"); |
| 403 |
assertEquals(expectedVersions.size(), elementsByTagName.getLength()); |
| 404 |
for (int i = 0; i < elementsByTagName.getLength(); i++) { |
| 405 |
Element item = (Element) elementsByTagName.item(i); |
| 406 |
Attr glEsVersion = item.getAttributeNodeNS(SdkConstants.ANDROID_URI, |
| 407 |
AndroidManifest.ATTRIBUTE_GLESVERSION); |
| 408 |
assertNotNull(glEsVersion); |
| 409 |
assertTrue(expectedVersions.contains(glEsVersion.getValue())); |
| 410 |
} |
| 411 |
} |
| 412 |
} |