| 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 |
|
| 21 |
import junit.framework.TestCase; |
| 22 |
|
| 23 |
import org.xml.sax.SAXException; |
| 24 |
|
| 25 |
import java.io.IOException; |
| 26 |
import java.util.logging.Logger; |
| 27 |
|
| 28 |
import javax.xml.parsers.ParserConfigurationException; |
| 29 |
|
| 30 |
/** |
| 31 |
* Tests for the {@link com.android.manifmerger.PreValidator} class. |
| 32 |
*/ |
| 33 |
public class PreValidatorTest extends TestCase { |
| 34 |
|
| 35 |
public void testCorrectInstructions() |
| 36 |
throws ParserConfigurationException, SAXException, IOException { |
| 37 |
|
| 38 |
MockLog mockLog = new MockLog(); |
| 39 |
String input = "" |
| 40 |
+ "<manifest\n" |
| 41 |
+ " xmlns:android=\"http://schemas.android.com/apk/res/android\"\n" |
| 42 |
+ " xmlns:tools=\"http://schemas.android.com/tools\"\n" |
| 43 |
+ " package=\"com.example.lib3\">\n" |
| 44 |
+ "\n" |
| 45 |
+ " <application android:label=\"@string/lib_name\" />\n" |
| 46 |
+ "\n" |
| 47 |
+ " <activity android:name=\"activityOne\" " |
| 48 |
+ " android:exported=\"false\"" |
| 49 |
+ " tools:replace=\"exported\"/>\n" |
| 50 |
+ "\n" |
| 51 |
+ "</manifest>"; |
| 52 |
|
| 53 |
XmlDocument xmlDocument = TestUtils.xmlDocumentFromString( |
| 54 |
TestUtils.sourceFile(getClass(), "testIncorrectRemove"), input); |
| 55 |
|
| 56 |
MergingReport.Builder mergingReport = new MergingReport.Builder(mockLog); |
| 57 |
MergingReport.Result validated = PreValidator.validate(mergingReport, xmlDocument); |
| 58 |
assertEquals(MergingReport.Result.SUCCESS, validated); |
| 59 |
assertTrue(mockLog.toString().isEmpty()); |
| 60 |
} |
| 61 |
|
| 62 |
public void testIncorrectReplace() |
| 63 |
throws ParserConfigurationException, SAXException, IOException { |
| 64 |
|
| 65 |
MockLog mockLog = new MockLog(); |
| 66 |
String input = "" |
| 67 |
+ "<manifest\n" |
| 68 |
+ " xmlns:android=\"http://schemas.android.com/apk/res/android\"\n" |
| 69 |
+ " xmlns:tools=\"http://schemas.android.com/tools\"\n" |
| 70 |
+ " package=\"com.example.lib3\">\n" |
| 71 |
+ "\n" |
| 72 |
+ " <application android:label=\"@string/lib_name\" />\n" |
| 73 |
+ "\n" |
| 74 |
+ " <activity android:name=\"activityOne\" " |
| 75 |
+ " tools:replace=\"exported\"/>\n" |
| 76 |
+ "\n" |
| 77 |
+ "</manifest>"; |
| 78 |
|
| 79 |
XmlDocument xmlDocument = TestUtils.xmlDocumentFromString( |
| 80 |
TestUtils.sourceFile(getClass(), "testIncorrectRemove"), input); |
| 81 |
|
| 82 |
MergingReport.Builder mergingReport = new MergingReport.Builder(mockLog); |
| 83 |
MergingReport.Result validated = PreValidator.validate(mergingReport, xmlDocument); |
| 84 |
assertEquals(MergingReport.Result.ERROR, validated); |
| 85 |
// assert the error message complains about the bad instruction usage. |
| 86 |
assertStringPresenceInLogRecords(mergingReport, "tools:replace"); |
| 87 |
} |
| 88 |
|
| 89 |
public void testIncorrectRemove() |
| 90 |
throws ParserConfigurationException, SAXException, IOException { |
| 91 |
|
| 92 |
MockLog mockLog = new MockLog(); |
| 93 |
String input = "" |
| 94 |
+ "<manifest\n" |
| 95 |
+ " xmlns:android=\"http://schemas.android.com/apk/res/android\"\n" |
| 96 |
+ " xmlns:tools=\"http://schemas.android.com/tools\"\n" |
| 97 |
+ " package=\"com.example.lib3\">\n" |
| 98 |
+ "\n" |
| 99 |
+ " <application android:label=\"@string/lib_name\" />\n" |
| 100 |
+ "\n" |
| 101 |
+ " <activity android:name=\"activityOne\" " |
| 102 |
+ " android:exported=\"true\"" |
| 103 |
+ " tools:remove=\"exported\"/>\n" |
| 104 |
+ "\n" |
| 105 |
+ "</manifest>"; |
| 106 |
|
| 107 |
XmlDocument xmlDocument = TestUtils.xmlDocumentFromString( |
| 108 |
TestUtils.sourceFile(getClass(), "testIncorrectRemove"), input); |
| 109 |
|
| 110 |
MergingReport.Builder mergingReport = new MergingReport.Builder(mockLog); |
| 111 |
MergingReport.Result validated = PreValidator.validate(mergingReport, xmlDocument); |
| 112 |
assertEquals(MergingReport.Result.ERROR, validated); |
| 113 |
// assert the error message complains about the bad instruction usage. |
| 114 |
assertStringPresenceInLogRecords(mergingReport, "tools:remove"); |
| 115 |
} |
| 116 |
|
| 117 |
public void testIncorrectRemoveAll() |
| 118 |
throws ParserConfigurationException, SAXException, IOException { |
| 119 |
|
| 120 |
MockLog mockLog = new MockLog(); |
| 121 |
String input = "" |
| 122 |
+ "<manifest\n" |
| 123 |
+ " xmlns:android=\"http://schemas.android.com/apk/res/android\"\n" |
| 124 |
+ " xmlns:tools=\"http://schemas.android.com/tools\"\n" |
| 125 |
+ " package=\"com.example.lib3\">\n" |
| 126 |
+ "\n" |
| 127 |
+ " <permission " |
| 128 |
+ " android:label=\"@string/lib_name\"" |
| 129 |
+ " android:name=\"permissionOne\"" |
| 130 |
+ " tools:node=\"removeAll\">\n" |
| 131 |
+ " </permission>\n" |
| 132 |
+ "\n" |
| 133 |
+ "</manifest>"; |
| 134 |
|
| 135 |
XmlDocument xmlDocument = TestUtils.xmlDocumentFromString( |
| 136 |
TestUtils.sourceFile(getClass(), "testIncorrectRemove"), input); |
| 137 |
|
| 138 |
MergingReport.Builder mergingReport = new MergingReport.Builder(mockLog); |
| 139 |
MergingReport.Result validated = PreValidator.validate(mergingReport, xmlDocument); |
| 140 |
assertEquals(MergingReport.Result.ERROR, validated); |
| 141 |
// assert the error message complains about the bad instruction usage. |
| 142 |
assertStringPresenceInLogRecords(mergingReport, "tools:node=\"removeAll\""); |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
public void testIncorrectSelector() |
| 147 |
throws ParserConfigurationException, SAXException, IOException { |
| 148 |
|
| 149 |
MockLog mockLog = new MockLog(); |
| 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 |
+ " <permission " |
| 157 |
+ " android:label=\"@string/lib_name\"" |
| 158 |
+ " android:name=\"permissionOne\"" |
| 159 |
+ " tools:node=\"replace\" tools:selector=\"foo\">\n" |
| 160 |
+ " </permission>\n" |
| 161 |
+ "\n" |
| 162 |
+ "</manifest>"; |
| 163 |
|
| 164 |
XmlDocument xmlDocument = TestUtils.xmlDocumentFromString( |
| 165 |
TestUtils.sourceFile(getClass(), "testIncorrectRemove"), input); |
| 166 |
|
| 167 |
MergingReport.Builder mergingReport = new MergingReport.Builder(mockLog); |
| 168 |
MergingReport.Result validated = PreValidator.validate(mergingReport, xmlDocument); |
| 169 |
assertEquals(MergingReport.Result.ERROR, validated); |
| 170 |
// assert the error message complains about the bad instruction usage. |
| 171 |
assertStringPresenceInLogRecords(mergingReport, "tools:selector=\"foo\""); |
| 172 |
} |
| 173 |
|
| 174 |
public void testNoKeyElement() |
| 175 |
throws ParserConfigurationException, SAXException, IOException { |
| 176 |
|
| 177 |
MockLog mockLog = new MockLog(); |
| 178 |
String input = "" |
| 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 |
+ " <compatible-screens>\n" |
| 185 |
+ " <!-- all small size screens -->\n" |
| 186 |
+ " <screen android:screenSize=\"small\" android:screenDensity=\"ldpi\" />\n" |
| 187 |
+ " <screen android:screenSize=\"small\" android:screenDensity=\"mdpi\" />\n" |
| 188 |
+ " <screen android:screenSize=\"small\" android:screenDensity=\"xhdpi\" />\n" |
| 189 |
+ " <!-- all normal size screens -->\n" |
| 190 |
+ " <screen android:screenSize=\"normal\" android:screenDensity=\"ldpi\" />\n" |
| 191 |
+ " <screen android:screenSize=\"normal\" android:screenDensity=\"hdpi\" />\n" |
| 192 |
+ " <screen android:screenSize=\"normal\" android:screenDensity=\"xhdpi\" />\n" |
| 193 |
+ " </compatible-screens>" |
| 194 |
+ "\n" |
| 195 |
+ "</manifest>"; |
| 196 |
|
| 197 |
XmlDocument xmlDocument = TestUtils.xmlDocumentFromString( |
| 198 |
TestUtils.sourceFile(getClass(), "testScreenMerging"), input); |
| 199 |
|
| 200 |
MergingReport.Builder mergingReport = new MergingReport.Builder(mockLog); |
| 201 |
MergingReport.Result validated = PreValidator.validate(mergingReport, xmlDocument); |
| 202 |
assertEquals(MergingReport.Result.SUCCESS, validated); |
| 203 |
} |
| 204 |
|
| 205 |
public void testMultipleIntentFilterWithSameKeyValue() |
| 206 |
throws ParserConfigurationException, SAXException, IOException { |
| 207 |
MockLog mockLog = new MockLog(); |
| 208 |
String input = "" |
| 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>\n" |
| 215 |
+ " <activity android:name=\"com.foo.bar.DeepLinkRouterActivity\" android:theme=\"@android:style/Theme.NoDisplay\">\n" |
| 216 |
+ " <intent-filter>\n" |
| 217 |
+ " <action android:name=\"android.intent.action.VIEW\"/>\n" |
| 218 |
+ " <category android:name=\"android.intent.category.DEFAULT\"/>\n" |
| 219 |
+ " <category android:name=\"android.intent.category.BROWSABLE\"/>\n" |
| 220 |
+ " <data android:scheme=\"myspecialdeeplinkscheme\"/>\n" |
| 221 |
+ " <data android:host=\"home\"/>\n" |
| 222 |
+ " </intent-filter>\n" |
| 223 |
+ " <intent-filter>\n" |
| 224 |
+ " <action android:name=\"android.intent.action.VIEW\"/>\n" |
| 225 |
+ " <category android:name=\"android.intent.category.DEFAULT\"/>\n" |
| 226 |
+ " <category android:name=\"android.intent.category.BROWSABLE\"/>\n" |
| 227 |
+ " <data android:scheme=\"https\"/>\n" |
| 228 |
+ " <data android:host=\"www.foo.com\"/>\n" |
| 229 |
+ " </intent-filter>\n" |
| 230 |
+ " </activity>\n" |
| 231 |
+ " </application>" |
| 232 |
+ "\n" |
| 233 |
+ "</manifest>"; |
| 234 |
|
| 235 |
XmlDocument xmlDocument = TestUtils.xmlDocumentFromString( |
| 236 |
TestUtils.sourceFile(getClass(), "testMultipleIntentFilterWithSameKeyValue"), input); |
| 237 |
|
| 238 |
MergingReport.Builder mergingReport = new MergingReport.Builder(mockLog); |
| 239 |
MergingReport.Result validated = PreValidator.validate(mergingReport, xmlDocument); |
| 240 |
assertEquals(MergingReport.Result.SUCCESS, validated); |
| 241 |
} |
| 242 |
|
| 243 |
private static void assertStringPresenceInLogRecords(MergingReport.Builder mergingReport, String s) { |
| 244 |
for (MergingReport.Record record : mergingReport.build().getLoggingRecords()) { |
| 245 |
if (record.toString().contains(s)) { |
| 246 |
return; |
| 247 |
} |
| 248 |
} |
| 249 |
// failed, dump the records |
| 250 |
for (MergingReport.Record record : mergingReport.build().getLoggingRecords()) { |
| 251 |
Logger.getAnonymousLogger().info(record.toString()); |
| 252 |
} |
| 253 |
fail("could not find " + s + " in logging records"); |
| 254 |
} |
| 255 |
} |