| 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.XmlNode.NodeKey; |
| 20 |
import static org.mockito.Matchers.eq; |
| 21 |
import static org.mockito.Mockito.verifyNoMoreInteractions; |
| 22 |
import static org.mockito.Mockito.when; |
| 23 |
|
| 24 |
import com.android.xml.AndroidManifest; |
| 25 |
import com.google.common.collect.ImmutableList; |
| 26 |
|
| 27 |
import junit.framework.TestCase; |
| 28 |
|
| 29 |
import org.mockito.ArgumentCaptor; |
| 30 |
import org.mockito.Mockito; |
| 31 |
import org.xml.sax.SAXException; |
| 32 |
|
| 33 |
import java.io.IOException; |
| 34 |
|
| 35 |
import javax.xml.parsers.ParserConfigurationException; |
| 36 |
|
| 37 |
/** |
| 38 |
* Tests for the {@link com.android.manifmerger.ManifestModel} class. |
| 39 |
*/ |
| 40 |
public class ManifestModelTest extends TestCase { |
| 41 |
|
| 42 |
public void testNameResolution() |
| 43 |
throws ParserConfigurationException, SAXException, IOException { |
| 44 |
String input = "" |
| 45 |
+ "<manifest\n" |
| 46 |
+ " xmlns:android=\"http://schemas.android.com/apk/res/android\"\n" |
| 47 |
+ " xmlns:tools=\"http://schemas.android.com/tools\"\n" |
| 48 |
+ " package=\"com.example.lib3\">\n" |
| 49 |
+ "\n" |
| 50 |
+ " <uses-feature android:name=\"camera\"/>\n" |
| 51 |
+ "\n" |
| 52 |
+ "</manifest>"; |
| 53 |
|
| 54 |
XmlDocument xmlDocument = TestUtils.xmlDocumentFromString( |
| 55 |
TestUtils.sourceFile(getClass(), "testNoUseFeaturesDeclaration"), input); |
| 56 |
|
| 57 |
XmlElement xmlElement = xmlDocument.getRootNode().getMergeableElements().get(0); |
| 58 |
assertEquals("uses-feature",xmlElement.getXml().getNodeName()); |
| 59 |
assertEquals("camera", xmlElement.getKey()); |
| 60 |
} |
| 61 |
|
| 62 |
public void testGlEsKeyResolution() |
| 63 |
throws ParserConfigurationException, SAXException, IOException { |
| 64 |
String input = "" |
| 65 |
+ "<manifest\n" |
| 66 |
+ " xmlns:android=\"http://schemas.android.com/apk/res/android\"\n" |
| 67 |
+ " xmlns:tools=\"http://schemas.android.com/tools\"\n" |
| 68 |
+ " package=\"com.example.lib3\">\n" |
| 69 |
+ "\n" |
| 70 |
+ " <uses-feature android:glEsVersion=\"0x00030000\"/>\n" |
| 71 |
+ "\n" |
| 72 |
+ "</manifest>"; |
| 73 |
|
| 74 |
XmlDocument xmlDocument = TestUtils.xmlDocumentFromString( |
| 75 |
TestUtils.sourceFile(getClass(), "testNoUseFeaturesDeclaration"), input); |
| 76 |
|
| 77 |
XmlElement xmlElement = xmlDocument.getRootNode().getMergeableElements().get(0); |
| 78 |
assertEquals("uses-feature",xmlElement.getXml().getNodeName()); |
| 79 |
assertEquals("0x00030000", xmlElement.getKey()); |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
public void testInvalidGlEsVersion() |
| 84 |
throws ParserConfigurationException, SAXException, IOException { |
| 85 |
|
| 86 |
AttributeModel.Hexadecimal32Bits validator = |
| 87 |
new AttributeModel.Hexadecimal32Bits(); |
| 88 |
XmlAttribute xmlAttribute = Mockito.mock(XmlAttribute.class); |
| 89 |
MergingReport.Builder mergingReport = Mockito.mock(MergingReport.Builder.class); |
| 90 |
when(xmlAttribute.getId()).thenReturn(new NodeKey(AndroidManifest.ATTRIBUTE_GLESVERSION)); |
| 91 |
|
| 92 |
ArgumentCaptor<String> argumentCaptor = ArgumentCaptor.forClass(String.class); |
| 93 |
Mockito.doNothing().when(xmlAttribute).addMessage( |
| 94 |
Mockito.any(MergingReport.Builder.class), |
| 95 |
eq(MergingReport.Record.Severity.ERROR), |
| 96 |
argumentCaptor.capture()); |
| 97 |
when(xmlAttribute.printPosition()).thenReturn("unknown"); |
| 98 |
assertFalse(validator.validates(mergingReport, xmlAttribute, "0xFFFFFFFFFFFF")); |
| 99 |
assertEquals("Attribute glEsVersion at unknown is not a valid hexadecimal " |
| 100 |
+ "32 bit value, found 0xFFFFFFFFFFFF", |
| 101 |
argumentCaptor.getValue()); |
| 102 |
} |
| 103 |
|
| 104 |
public void testTooLowGlEsVersion() |
| 105 |
throws ParserConfigurationException, SAXException, IOException { |
| 106 |
|
| 107 |
AttributeModel.Hexadecimal32BitsWithMinimumValue validator = |
| 108 |
new AttributeModel.Hexadecimal32BitsWithMinimumValue(0x00010000); |
| 109 |
XmlAttribute xmlAttribute = Mockito.mock(XmlAttribute.class); |
| 110 |
MergingReport.Builder mergingReport = Mockito.mock(MergingReport.Builder.class); |
| 111 |
|
| 112 |
ArgumentCaptor<String> argumentCaptor = ArgumentCaptor.forClass(String.class); |
| 113 |
Mockito.doNothing().when(xmlAttribute).addMessage( |
| 114 |
Mockito.any(MergingReport.Builder.class), |
| 115 |
eq(MergingReport.Record.Severity.ERROR), |
| 116 |
argumentCaptor.capture()); |
| 117 |
|
| 118 |
when(xmlAttribute.getId()).thenReturn(new NodeKey(AndroidManifest.ATTRIBUTE_GLESVERSION)); |
| 119 |
when(xmlAttribute.printPosition()).thenReturn("unknown"); |
| 120 |
assertFalse(validator.validates(mergingReport, xmlAttribute, "0xFFF")); |
| 121 |
assertEquals("Attribute glEsVersion at unknown is not a valid hexadecimal value, " |
| 122 |
+ "minimum is 0x00010000, maximum is 0x7FFFFFFF, found 0xFFF", |
| 123 |
argumentCaptor.getValue()); |
| 124 |
} |
| 125 |
|
| 126 |
public void testOkGlEsVersion() |
| 127 |
throws ParserConfigurationException, SAXException, IOException { |
| 128 |
|
| 129 |
AttributeModel.Hexadecimal32BitsWithMinimumValue validator = |
| 130 |
new AttributeModel.Hexadecimal32BitsWithMinimumValue(0x00010000); |
| 131 |
XmlAttribute xmlAttribute = Mockito.mock(XmlAttribute.class); |
| 132 |
MergingReport.Builder mergingReport = Mockito.mock(MergingReport.Builder.class); |
| 133 |
|
| 134 |
when(xmlAttribute.getId()).thenReturn(new NodeKey(AndroidManifest.ATTRIBUTE_GLESVERSION)); |
| 135 |
when(xmlAttribute.printPosition()).thenReturn("unknown"); |
| 136 |
assertTrue(validator.validates(mergingReport, xmlAttribute, "0x00020001")); |
| 137 |
verifyNoMoreInteractions(xmlAttribute); |
| 138 |
} |
| 139 |
|
| 140 |
public void testTooBigGlEsVersion() |
| 141 |
throws ParserConfigurationException, SAXException, IOException { |
| 142 |
|
| 143 |
AttributeModel.Hexadecimal32BitsWithMinimumValue validator = |
| 144 |
new AttributeModel.Hexadecimal32BitsWithMinimumValue(0x00010000); |
| 145 |
XmlAttribute xmlAttribute = Mockito.mock(XmlAttribute.class); |
| 146 |
MergingReport.Builder mergingReport = Mockito.mock(MergingReport.Builder.class); |
| 147 |
|
| 148 |
ArgumentCaptor<String> argumentCaptor = ArgumentCaptor.forClass(String.class); |
| 149 |
Mockito.doNothing().when(xmlAttribute).addMessage( |
| 150 |
Mockito.any(MergingReport.Builder.class), |
| 151 |
eq(MergingReport.Record.Severity.ERROR), |
| 152 |
argumentCaptor.capture()); |
| 153 |
|
| 154 |
when(xmlAttribute.getId()).thenReturn(new NodeKey(AndroidManifest.ATTRIBUTE_GLESVERSION)); |
| 155 |
when(xmlAttribute.printPosition()).thenReturn("unknown"); |
| 156 |
assertFalse(validator.validates(mergingReport, xmlAttribute, "0xFFFFFFFF")); |
| 157 |
assertEquals("Attribute glEsVersion at unknown is not a valid hexadecimal value," |
| 158 |
+ " minimum is 0x00010000, maximum is 0x7FFFFFFF, found 0xFFFFFFFF", |
| 159 |
argumentCaptor.getValue()); |
| 160 |
} |
| 161 |
|
| 162 |
public void testNoKeyResolution() |
| 163 |
throws ParserConfigurationException, SAXException, IOException { |
| 164 |
String input = "" |
| 165 |
+ "<manifest\n" |
| 166 |
+ " xmlns:android=\"http://schemas.android.com/apk/res/android\"\n" |
| 167 |
+ " xmlns:tools=\"http://schemas.android.com/tools\"\n" |
| 168 |
+ " package=\"com.example.lib3\">\n" |
| 169 |
+ "\n" |
| 170 |
+ " <uses-feature android:required=\"false\"/>\n" |
| 171 |
+ "\n" |
| 172 |
+ "</manifest>"; |
| 173 |
|
| 174 |
XmlDocument xmlDocument = TestUtils.xmlDocumentFromString( |
| 175 |
TestUtils.sourceFile(getClass(), "testNoUseFeaturesDeclaration"), input); |
| 176 |
|
| 177 |
XmlElement xmlElement = xmlDocument.getRootNode().getMergeableElements().get(0); |
| 178 |
assertEquals("uses-feature",xmlElement.getXml().getNodeName()); |
| 179 |
assertNull(xmlElement.getKey()); |
| 180 |
} |
| 181 |
|
| 182 |
public void testTwoAttributesKeyResolution() |
| 183 |
throws ParserConfigurationException, SAXException, IOException { |
| 184 |
|
| 185 |
String input = "" |
| 186 |
+ "<manifest\n" |
| 187 |
+ " xmlns:android=\"http://schemas.android.com/apk/res/android\"\n" |
| 188 |
+ " xmlns:tools=\"http://schemas.android.com/tools\"\n" |
| 189 |
+ " package=\"com.example.lib3\">\n" |
| 190 |
+ "\n" |
| 191 |
+ " <compatible-screens>\n" |
| 192 |
+ " <screen/>\n" |
| 193 |
+ " <screen android:screenDensity=\"mdpi\"/>\n" |
| 194 |
+ " <screen android:screenSize=\"normal\"/>\n" |
| 195 |
+ " <screen android:screenSize=\"normal\" android:screenDensity=\"mdpi\"/>\n" |
| 196 |
+ " </compatible-screens>\n" |
| 197 |
+ "\n" |
| 198 |
+ "</manifest>"; |
| 199 |
|
| 200 |
XmlDocument xmlDocument = TestUtils.xmlDocumentFromString( |
| 201 |
TestUtils.sourceFile(getClass(), "testNoUseFeaturesDeclaration"), input); |
| 202 |
|
| 203 |
XmlElement xmlElement = xmlDocument.getRootNode().getMergeableElements().get(0); |
| 204 |
ImmutableList<XmlElement> screenDefinitions = xmlElement.getMergeableElements(); |
| 205 |
assertNull(screenDefinitions.get(0).getKey()); |
| 206 |
assertEquals("mdpi", screenDefinitions.get(1).getKey()); |
| 207 |
assertEquals("normal", screenDefinitions.get(2).getKey()); |
| 208 |
assertEquals("normal+mdpi", screenDefinitions.get(3).getKey()); |
| 209 |
} |
| 210 |
} |