1 /* 2 * Copyright (C) 2008 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 android.content.cts; 18 19 import static android.os.PatternMatcher.PATTERN_LITERAL; 20 import static android.os.PatternMatcher.PATTERN_PREFIX; 21 import static android.os.PatternMatcher.PATTERN_SIMPLE_GLOB; 22 23 import java.io.ByteArrayInputStream; 24 import java.io.ByteArrayOutputStream; 25 import java.io.IOException; 26 import java.io.InputStream; 27 import java.util.HashSet; 28 import java.util.Iterator; 29 import java.util.Set; 30 31 import org.xmlpull.v1.XmlPullParser; 32 import org.xmlpull.v1.XmlPullParserException; 33 import org.xmlpull.v1.XmlSerializer; 34 35 import android.content.ComponentName; 36 import android.content.ContentResolver; 37 import android.content.Intent; 38 import android.content.IntentFilter; 39 import android.content.IntentFilter.AuthorityEntry; 40 import android.content.IntentFilter.MalformedMimeTypeException; 41 import android.content.pm.ActivityInfo; 42 import android.content.pm.PackageManager; 43 import android.content.pm.PackageManager.NameNotFoundException; 44 import android.net.Uri; 45 import android.os.Parcel; 46 import android.os.PatternMatcher; 47 import android.provider.Contacts.People; 48 import android.test.AndroidTestCase; 49 import android.util.Printer; 50 import android.util.StringBuilderPrinter; 51 import android.util.Xml; 52 53 import com.android.internal.util.FastXmlSerializer; 54 55 56 public class IntentFilterTest extends AndroidTestCase { 57 58 private IntentFilter mIntentFilter; 59 private static final String ACTION = "testAction"; 60 private static final String CATEGORY = "testCategory"; 61 private static final String DATA_TYPE = "vnd.android.cursor.dir/person"; 62 private static final String DATA_SCHEME = "testDataSchemes."; 63 private static final String SSP = "testSsp"; 64 private static final String HOST = "testHost"; 65 private static final int PORT = 80; 66 private static final String DATA_PATH = "testDataPath"; 67 private static final Uri URI = People.CONTENT_URI; 68 69 @Override 70 protected void setUp() throws Exception { 71 super.setUp(); 72 mIntentFilter = new IntentFilter(); 73 } 74 75 public void testConstructor() throws MalformedMimeTypeException { 76 77 IntentFilter filter = new IntentFilter(); 78 verifyContent(filter, null, null); 79 80 filter = new IntentFilter(ACTION); 81 verifyContent(filter, ACTION, null); 82 83 final IntentFilter actionTypeFilter = new IntentFilter(ACTION, DATA_TYPE); 84 verifyContent(actionTypeFilter, ACTION, DATA_TYPE); 85 86 filter = new IntentFilter(actionTypeFilter); 87 verifyContent(filter, ACTION, DATA_TYPE); 88 89 final String dataType = "testdataType"; 90 try { 91 new IntentFilter(ACTION, dataType); 92 fail("Should throw MalformedMimeTypeException "); 93 } catch (MalformedMimeTypeException e) { 94 // expected 95 } 96 } 97 98 /** 99 * Assert that the given filter contains the given action and dataType. If 100 * action or dataType are null, assert that the filter has no actions or 101 * dataTypes registered. 102 */ 103 private void verifyContent(IntentFilter filter, String action, String dataType) { 104 if (action != null) { 105 assertEquals(1, filter.countActions()); 106 assertEquals(action, filter.getAction(0)); 107 } else { 108 assertEquals(0, filter.countActions()); 109 } 110 if (dataType != null) { 111 assertEquals(1, filter.countDataTypes()); 112 assertEquals(dataType, filter.getDataType(0)); 113 } else { 114 assertEquals(0, filter.countDataTypes()); 115 } 116 } 117 118 public void testCategories() { 119 for (int i = 0; i < 10; i++) { 120 mIntentFilter.addCategory(CATEGORY + i); 121 } 122 assertEquals(10, mIntentFilter.countCategories()); 123 Iterator<String> iter = mIntentFilter.categoriesIterator(); 124 String actual = null; 125 int i = 0; 126 while (iter.hasNext()) { 127 actual = iter.next(); 128 assertEquals(CATEGORY + i, actual); 129 assertEquals(CATEGORY + i, mIntentFilter.getCategory(i)); 130 assertTrue(mIntentFilter.hasCategory(CATEGORY + i)); 131 assertFalse(mIntentFilter.hasCategory(CATEGORY + i + 10)); 132 i++; 133 } 134 IntentFilter filter = new Match(null, new String[] { "category1" }, null, null, null, null); 135 checkMatches(filter, new MatchCondition[] { 136 new MatchCondition(IntentFilter.MATCH_CATEGORY_EMPTY, null, null, null, null), 137 new MatchCondition(IntentFilter.MATCH_CATEGORY_EMPTY, null, 138 new String[] { "category1" }, null, null), 139 new MatchCondition(IntentFilter.NO_MATCH_CATEGORY, null, 140 new String[] { "category2" }, null, null), 141 new MatchCondition(IntentFilter.NO_MATCH_CATEGORY, null, new String[] { 142 "category1", "category2" }, null, null), }); 143 144 filter = new Match(null, new String[] { "category1", "category2" }, null, null, null, null); 145 checkMatches(filter, new MatchCondition[] { 146 new MatchCondition(IntentFilter.MATCH_CATEGORY_EMPTY, null, null, null, null), 147 new MatchCondition(IntentFilter.MATCH_CATEGORY_EMPTY, null, 148 new String[] { "category1" }, null, null), 149 new MatchCondition(IntentFilter.MATCH_CATEGORY_EMPTY, null, 150 new String[] { "category2" }, null, null), 151 new MatchCondition(IntentFilter.MATCH_CATEGORY_EMPTY, null, new String[] { 152 "category1", "category2" }, null, null), 153 new MatchCondition(IntentFilter.NO_MATCH_CATEGORY, null, 154 new String[] { "category3" }, null, null), 155 new MatchCondition(IntentFilter.NO_MATCH_CATEGORY, null, new String[] { 156 "category1", "category2", "category3" }, null, null), }); 157 } 158 159 public void testMimeTypes() throws Exception { 160 IntentFilter filter = new Match(null, null, new String[] { "which1/what1" }, null, null, 161 null); 162 checkMatches(filter, new MatchCondition[] { 163 new MatchCondition(IntentFilter.NO_MATCH_TYPE, null, null, null, null), 164 new MatchCondition(IntentFilter.MATCH_CATEGORY_TYPE, null, null, "which1/what1", 165 null), 166 new MatchCondition(IntentFilter.MATCH_CATEGORY_TYPE, null, null, "which1/*", null), 167 new MatchCondition(IntentFilter.MATCH_CATEGORY_TYPE, null, null, "*/*", null), 168 new MatchCondition(IntentFilter.NO_MATCH_TYPE, null, null, "which2/what2", null), 169 new MatchCondition(IntentFilter.NO_MATCH_TYPE, null, null, "which2/*", null), 170 new MatchCondition(IntentFilter.NO_MATCH_TYPE, null, null, "which1/what2", null), 171 }); 172 173 filter = new Match(null, null, new String[] { "which1/what1", "which2/what2" }, null, null, 174 null); 175 checkMatches(filter, new MatchCondition[] { 176 new MatchCondition(IntentFilter.NO_MATCH_TYPE, null, null, null, null), 177 new MatchCondition(IntentFilter.MATCH_CATEGORY_TYPE, null, null, "which1/what1", 178 null), 179 new MatchCondition(IntentFilter.MATCH_CATEGORY_TYPE, null, null, "which1/*", null), 180 new MatchCondition(IntentFilter.MATCH_CATEGORY_TYPE, null, null, "*/*", null), 181 new MatchCondition(IntentFilter.MATCH_CATEGORY_TYPE, null, null, "which2/what2", 182 null), 183 new MatchCondition(IntentFilter.MATCH_CATEGORY_TYPE, null, null, "which2/*", null), 184 new MatchCondition(IntentFilter.NO_MATCH_TYPE, null, null, "which1/what2", null), 185 new MatchCondition(IntentFilter.NO_MATCH_TYPE, null, null, "which3/what3", null), 186 }); 187 188 filter = new Match(null, null, new String[] { "which1/*" }, null, null, null); 189 checkMatches(filter, new MatchCondition[] { 190 new MatchCondition(IntentFilter.NO_MATCH_TYPE, null, null, null, null), 191 new MatchCondition(IntentFilter.MATCH_CATEGORY_TYPE, null, null, "which1/what1", 192 null), 193 new MatchCondition(IntentFilter.MATCH_CATEGORY_TYPE, null, null, "which1/*", null), 194 new MatchCondition(IntentFilter.MATCH_CATEGORY_TYPE, null, null, "*/*", null), 195 new MatchCondition(IntentFilter.NO_MATCH_TYPE, null, null, "which2/what2", null), 196 new MatchCondition(IntentFilter.NO_MATCH_TYPE, null, null, "which2/*", null), 197 new MatchCondition(IntentFilter.MATCH_CATEGORY_TYPE, null, null, "which1/what2", 198 null), 199 new MatchCondition(IntentFilter.NO_MATCH_TYPE, null, null, "which3/what3", null), 200 }); 201 202 filter = new Match(null, null, new String[] { "*/*" }, null, null, null); 203 checkMatches(filter, new MatchCondition[] { 204 new MatchCondition(IntentFilter.NO_MATCH_TYPE, null, null, null, null), 205 new MatchCondition(IntentFilter.MATCH_CATEGORY_TYPE, null, null, "which1/what1", 206 null), 207 new MatchCondition(IntentFilter.MATCH_CATEGORY_TYPE, null, null, "which1/*", null), 208 new MatchCondition(IntentFilter.MATCH_CATEGORY_TYPE, null, null, "*/*", null), 209 new MatchCondition(IntentFilter.MATCH_CATEGORY_TYPE, null, null, "which2/what2", 210 null), 211 new MatchCondition(IntentFilter.MATCH_CATEGORY_TYPE, null, null, "which2/*", null), 212 new MatchCondition(IntentFilter.MATCH_CATEGORY_TYPE, null, null, "which1/what2", 213 null), 214 new MatchCondition(IntentFilter.MATCH_CATEGORY_TYPE, null, null, "which3/what3", 215 null), }); 216 } 217 218 public void testAccessPriority() { 219 final int expected = 1; 220 mIntentFilter.setPriority(expected); 221 assertEquals(expected, mIntentFilter.getPriority()); 222 } 223 224 public void testDataSchemes() { 225 for (int i = 0; i < 10; i++) { 226 mIntentFilter.addDataScheme(DATA_SCHEME + i); 227 } 228 assertEquals(10, mIntentFilter.countDataSchemes()); 229 final Iterator<String> iter = mIntentFilter.schemesIterator(); 230 String actual = null; 231 int i = 0; 232 while (iter.hasNext()) { 233 actual = iter.next(); 234 assertEquals(DATA_SCHEME + i, actual); 235 assertEquals(DATA_SCHEME + i, mIntentFilter.getDataScheme(i)); 236 assertTrue(mIntentFilter.hasDataScheme(DATA_SCHEME + i)); 237 assertFalse(mIntentFilter.hasDataScheme(DATA_SCHEME + i + 10)); 238 i++; 239 } 240 IntentFilter filter = new Match(null, null, null, new String[] { "scheme1" }, null, null); 241 checkMatches(filter, new MatchCondition[] { 242 new MatchCondition(IntentFilter.NO_MATCH_DATA, null, null, null, null), 243 new MatchCondition(IntentFilter.MATCH_CATEGORY_SCHEME, null, null, null, 244 "scheme1:foo"), 245 new MatchCondition(IntentFilter.NO_MATCH_DATA, null, null, null, "scheme2:foo"), }); 246 247 filter = new Match(null, null, null, new String[] { "scheme1", "scheme2" }, null, null); 248 checkMatches(filter, new MatchCondition[] { 249 new MatchCondition(IntentFilter.NO_MATCH_DATA, null, null, null, null), 250 new MatchCondition(IntentFilter.MATCH_CATEGORY_SCHEME, null, null, null, 251 "scheme1:foo"), 252 new MatchCondition(IntentFilter.MATCH_CATEGORY_SCHEME, null, null, null, 253 "scheme2:foo"), 254 new MatchCondition(IntentFilter.NO_MATCH_DATA, null, null, null, "scheme3:foo"), }); 255 } 256 257 public void testCreate() { 258 IntentFilter filter = IntentFilter.create(ACTION, DATA_TYPE); 259 assertNotNull(filter); 260 verifyContent(filter, ACTION, DATA_TYPE); 261 } 262 263 264 public void testSchemeSpecificParts() throws Exception { 265 IntentFilter filter = new Match(null, null, null, new String[]{"scheme"}, 266 null, null, null, null, new String[]{"ssp1", "2ssp"}, 267 new int[]{PATTERN_LITERAL, PATTERN_LITERAL}); 268 checkMatches(filter, new MatchCondition[] { 269 new MatchCondition(IntentFilter.NO_MATCH_DATA, null, null, null, null), 270 new MatchCondition(IntentFilter.MATCH_CATEGORY_SCHEME_SPECIFIC_PART, null, null, null, 271 "scheme:ssp1"), 272 new MatchCondition(IntentFilter.MATCH_CATEGORY_SCHEME_SPECIFIC_PART, null, null, null, 273 "scheme:2ssp"), 274 new MatchCondition(IntentFilter.NO_MATCH_DATA, null, null, null, 275 "scheme:ssp"), 276 new MatchCondition(IntentFilter.NO_MATCH_DATA, null, null, null, 277 "scheme:ssp12"), }); 278 filter = new Match(null, null, null, new String[]{"scheme"}, 279 null, null, null, null, new String[]{"ssp1", "2ssp"}, 280 new int[]{PATTERN_PREFIX, PATTERN_PREFIX}); 281 checkMatches(filter, new MatchCondition[] { 282 new MatchCondition(IntentFilter.NO_MATCH_DATA, null, null, null, null), 283 new MatchCondition(IntentFilter.MATCH_CATEGORY_SCHEME_SPECIFIC_PART, null, null, null, 284 "scheme:ssp1"), 285 new MatchCondition(IntentFilter.MATCH_CATEGORY_SCHEME_SPECIFIC_PART, null, null, null, 286 "scheme:2ssp"), 287 new MatchCondition(IntentFilter.NO_MATCH_DATA, null, null, null, 288 "scheme:ssp"), 289 new MatchCondition(IntentFilter.MATCH_CATEGORY_SCHEME_SPECIFIC_PART, null, null, null, 290 "scheme:ssp12"), }); 291 filter = new Match(null, null, null, new String[]{"scheme"}, 292 null, null, null, null, new String[]{"ssp.*"}, 293 new int[]{PATTERN_SIMPLE_GLOB}); 294 checkMatches(filter, new MatchCondition[] { 295 new MatchCondition(IntentFilter.NO_MATCH_DATA, null, null, null, null), 296 new MatchCondition(IntentFilter.MATCH_CATEGORY_SCHEME_SPECIFIC_PART, null, null, null, 297 "scheme:ssp1"), 298 new MatchCondition(IntentFilter.MATCH_CATEGORY_SCHEME_SPECIFIC_PART, null, null, null, 299 "scheme:ssp"), 300 new MatchCondition(IntentFilter.NO_MATCH_DATA, null, null, null, 301 "scheme:ss"), }); 302 filter = new Match(null, null, null, new String[]{"scheme"}, 303 null, null, null, null, new String[]{".*"}, 304 new int[]{PATTERN_SIMPLE_GLOB}); 305 checkMatches(filter, new MatchCondition[] { 306 new MatchCondition(IntentFilter.NO_MATCH_DATA, null, null, null, null), 307 new MatchCondition(IntentFilter.MATCH_CATEGORY_SCHEME_SPECIFIC_PART, null, null, null, 308 "scheme:ssp1"), 309 new MatchCondition(IntentFilter.MATCH_CATEGORY_SCHEME_SPECIFIC_PART, null, null, null, 310 "scheme:ssp"), 311 new MatchCondition(IntentFilter.MATCH_CATEGORY_SCHEME_SPECIFIC_PART, null, null, null, 312 "scheme:"), }); 313 filter = new Match(null, null, null, new String[]{"scheme"}, 314 null, null, null, null, new String[]{"a1*b"}, 315 new int[]{PATTERN_SIMPLE_GLOB}); 316 checkMatches(filter, new MatchCondition[] { 317 new MatchCondition(IntentFilter.NO_MATCH_DATA, null, null, null, null), 318 new MatchCondition(IntentFilter.MATCH_CATEGORY_SCHEME_SPECIFIC_PART, null, null, null, 319 "scheme:ab"), 320 new MatchCondition(IntentFilter.MATCH_CATEGORY_SCHEME_SPECIFIC_PART, null, null, null, 321 "scheme:a1b"), 322 new MatchCondition(IntentFilter.MATCH_CATEGORY_SCHEME_SPECIFIC_PART, null, null, null, 323 "scheme:a11b"), 324 new MatchCondition(IntentFilter.NO_MATCH_DATA, null, null, null, 325 "scheme:a2b"), 326 new MatchCondition(IntentFilter.NO_MATCH_DATA, null, null, null, 327 "scheme:a1bc"), 328 new MatchCondition(IntentFilter.NO_MATCH_DATA, null, null, null, 329 "scheme:a"), }); 330 filter = new Match(null, null, null, new String[]{"scheme"}, 331 null, null, null, null, new String[]{"a1*"}, 332 new int[]{PATTERN_SIMPLE_GLOB}); 333 checkMatches(filter, new MatchCondition[] { 334 new MatchCondition(IntentFilter.NO_MATCH_DATA, null, null, null, null), 335 new MatchCondition(IntentFilter.MATCH_CATEGORY_SCHEME_SPECIFIC_PART, null, null, null, 336 "scheme:a1"), 337 new MatchCondition(IntentFilter.NO_MATCH_DATA, null, null, null, 338 "scheme:ab"), 339 new MatchCondition(IntentFilter.MATCH_CATEGORY_SCHEME_SPECIFIC_PART, null, null, null, 340 "scheme:a11"), 341 new MatchCondition(IntentFilter.NO_MATCH_DATA, null, null, null, 342 "scheme:a1b"), 343 new MatchCondition(IntentFilter.MATCH_CATEGORY_SCHEME_SPECIFIC_PART, null, null, null, 344 "scheme:a11"), 345 new MatchCondition(IntentFilter.NO_MATCH_DATA, null, null, null, 346 "scheme:a2"), }); 347 filter = new Match(null, null, null, new String[]{"scheme"}, 348 null, null, null, null, new String[]{"a\\.*b"}, 349 new int[]{PATTERN_SIMPLE_GLOB}); 350 checkMatches(filter, new MatchCondition[] { 351 new MatchCondition(IntentFilter.NO_MATCH_DATA, null, null, null, null), 352 new MatchCondition(IntentFilter.MATCH_CATEGORY_SCHEME_SPECIFIC_PART, null, null, null, 353 "scheme:ab"), 354 new MatchCondition(IntentFilter.MATCH_CATEGORY_SCHEME_SPECIFIC_PART, null, null, null, 355 "scheme:a.b"), 356 new MatchCondition(IntentFilter.MATCH_CATEGORY_SCHEME_SPECIFIC_PART, null, null, null, 357 "scheme:a..b"), 358 new MatchCondition(IntentFilter.NO_MATCH_DATA, null, null, null, 359 "scheme:a2b"), 360 new MatchCondition(IntentFilter.NO_MATCH_DATA, null, null, null, 361 "scheme:a.bc"), 362 new MatchCondition(IntentFilter.NO_MATCH_DATA, null, null, null, 363 "scheme:"), }); 364 filter = new Match(null, null, null, new String[]{"scheme"}, 365 null, null, null, null, new String[]{"a.*b"}, 366 new int[]{PATTERN_SIMPLE_GLOB}); 367 checkMatches(filter, new MatchCondition[] { 368 new MatchCondition(IntentFilter.NO_MATCH_DATA, null, null, null, null), 369 new MatchCondition(IntentFilter.MATCH_CATEGORY_SCHEME_SPECIFIC_PART, null, null, null, 370 "scheme:ab"), 371 new MatchCondition(IntentFilter.MATCH_CATEGORY_SCHEME_SPECIFIC_PART, null, null, null, 372 "scheme:a.b"), 373 new MatchCondition(IntentFilter.MATCH_CATEGORY_SCHEME_SPECIFIC_PART, null, null, null, 374 "scheme:a.1b"), 375 new MatchCondition(IntentFilter.MATCH_CATEGORY_SCHEME_SPECIFIC_PART, null, null, null, 376 "scheme:a2b"), 377 new MatchCondition(IntentFilter.NO_MATCH_DATA, null, null, null, 378 "scheme:a.bc"), 379 new MatchCondition(IntentFilter.NO_MATCH_DATA, null, null, null, 380 "scheme:"), }); 381 filter = new Match(null, null, null, new String[]{"scheme"}, 382 null, null, null, null, new String[]{"a.*"}, 383 new int[]{PATTERN_SIMPLE_GLOB}); 384 checkMatches(filter, new MatchCondition[] { 385 new MatchCondition(IntentFilter.NO_MATCH_DATA, null, null, null, null), 386 new MatchCondition(IntentFilter.MATCH_CATEGORY_SCHEME_SPECIFIC_PART, null, null, null, 387 "scheme:ab"), 388 new MatchCondition(IntentFilter.MATCH_CATEGORY_SCHEME_SPECIFIC_PART, null, null, null, 389 "scheme:a.b"), 390 new MatchCondition(IntentFilter.MATCH_CATEGORY_SCHEME_SPECIFIC_PART, null, null, null, 391 "scheme:a.1b"), 392 new MatchCondition(IntentFilter.MATCH_CATEGORY_SCHEME_SPECIFIC_PART, null, null, null, 393 "scheme:a2b"), 394 new MatchCondition(IntentFilter.MATCH_CATEGORY_SCHEME_SPECIFIC_PART, null, null, null, 395 "scheme:a.bc"), 396 new MatchCondition(IntentFilter.NO_MATCH_DATA, null, null, null, 397 "scheme:"), }); 398 filter = new Match(null, null, null, new String[]{"scheme"}, 399 null, null, null, null, new String[]{"a.\\*b"}, 400 new int[]{PATTERN_SIMPLE_GLOB}); 401 checkMatches(filter, new MatchCondition[] { 402 new MatchCondition(IntentFilter.NO_MATCH_DATA, null, null, null, null), 403 new MatchCondition(IntentFilter.NO_MATCH_DATA, null, null, null, 404 "scheme:ab"), 405 new MatchCondition(IntentFilter.MATCH_CATEGORY_SCHEME_SPECIFIC_PART, null, null, null, 406 "scheme:a.*b"), 407 new MatchCondition(IntentFilter.MATCH_CATEGORY_SCHEME_SPECIFIC_PART, null, null, null, 408 "scheme:a1*b"), 409 new MatchCondition(IntentFilter.NO_MATCH_DATA, null, null, null, 410 "scheme:a2b"), 411 new MatchCondition(IntentFilter.NO_MATCH_DATA, null, null, null, 412 "scheme:a.bc"), 413 new MatchCondition(IntentFilter.NO_MATCH_DATA, null, null, null, 414 "scheme:"), }); 415 filter = new Match(null, null, null, new String[]{"scheme"}, 416 null, null, null, null, new String[]{"a.\\*"}, 417 new int[]{PATTERN_SIMPLE_GLOB}); 418 checkMatches(filter, new MatchCondition[] { 419 new MatchCondition(IntentFilter.NO_MATCH_DATA, null, null, null, null), 420 new MatchCondition(IntentFilter.NO_MATCH_DATA, null, null, null, 421 "scheme:ab"), 422 new MatchCondition(IntentFilter.MATCH_CATEGORY_SCHEME_SPECIFIC_PART, null, null, null, 423 "scheme:a.*"), 424 new MatchCondition(IntentFilter.MATCH_CATEGORY_SCHEME_SPECIFIC_PART, null, null, null, 425 "scheme:a1*"), 426 new MatchCondition(IntentFilter.NO_MATCH_DATA, null, null, null, 427 "scheme:a1b"), }); 428 } 429 430 public void testAuthorities() { 431 for (int i = 0; i < 10; i++) { 432 mIntentFilter.addDataAuthority(HOST + i, String.valueOf(PORT + i)); 433 } 434 assertEquals(10, mIntentFilter.countDataAuthorities()); 435 436 final Iterator<AuthorityEntry> iter = mIntentFilter.authoritiesIterator(); 437 AuthorityEntry actual = null; 438 int i = 0; 439 while (iter.hasNext()) { 440 actual = iter.next(); 441 assertEquals(HOST + i, actual.getHost()); 442 assertEquals(PORT + i, actual.getPort()); 443 AuthorityEntry ae = new AuthorityEntry(HOST + i, String.valueOf(PORT + i)); 444 assertEquals(ae.getHost(), mIntentFilter.getDataAuthority(i).getHost()); 445 assertEquals(ae.getPort(), mIntentFilter.getDataAuthority(i).getPort()); 446 Uri uri = Uri.parse("http://" + HOST + i + ":" + String.valueOf(PORT + i)); 447 assertTrue(mIntentFilter.hasDataAuthority(uri)); 448 Uri uri2 = Uri.parse("http://" + HOST + i + 10 + ":" + PORT + i + 10); 449 assertFalse(mIntentFilter.hasDataAuthority(uri2)); 450 i++; 451 } 452 IntentFilter filter = new Match(null, null, null, new String[] { "scheme1" }, 453 new String[] { "authority1" }, new String[] { null }); 454 checkMatches(filter, new MatchCondition[] { 455 new MatchCondition(IntentFilter.NO_MATCH_DATA, null, null, null, null), 456 new MatchCondition(IntentFilter.NO_MATCH_DATA, null, null, null, "scheme1:foo"), 457 new MatchCondition(IntentFilter.MATCH_CATEGORY_HOST, null, null, null, 458 "scheme1://authority1/"), 459 new MatchCondition(IntentFilter.NO_MATCH_DATA, null, null, null, 460 "scheme1://authority2/"), 461 new MatchCondition(IntentFilter.MATCH_CATEGORY_HOST, null, null, null, 462 "scheme1://authority1:100/"), }); 463 464 filter = new Match(null, null, null, new String[] { "scheme1" }, 465 new String[] { "authority1" }, new String[] { "100" }); 466 checkMatches(filter, new MatchCondition[] { 467 new MatchCondition(IntentFilter.NO_MATCH_DATA, null, null, null, null), 468 new MatchCondition(IntentFilter.NO_MATCH_DATA, null, null, null, "scheme1:foo"), 469 new MatchCondition(IntentFilter.NO_MATCH_DATA, null, null, null, 470 "scheme1://authority1/"), 471 new MatchCondition(IntentFilter.NO_MATCH_DATA, null, null, null, 472 "scheme1://authority2/"), 473 new MatchCondition(IntentFilter.MATCH_CATEGORY_PORT, null, null, null, 474 "scheme1://authority1:100/"), 475 new MatchCondition(IntentFilter.NO_MATCH_DATA, null, null, null, 476 "scheme1://authority1:200/"), }); 477 478 filter = new Match(null, null, null, new String[] { "scheme1" }, 479 new String[] { "authority1", "authority2" }, new String[] { "100", null }); 480 checkMatches(filter, new MatchCondition[] { 481 new MatchCondition(IntentFilter.NO_MATCH_DATA, null, null, null, null), 482 new MatchCondition(IntentFilter.NO_MATCH_DATA, null, null, null, "scheme1:foo"), 483 new MatchCondition(IntentFilter.NO_MATCH_DATA, null, null, null, 484 "scheme1://authority1/"), 485 new MatchCondition(IntentFilter.MATCH_CATEGORY_HOST, null, null, null, 486 "scheme1://authority2/"), 487 new MatchCondition(IntentFilter.MATCH_CATEGORY_PORT, null, null, null, 488 "scheme1://authority1:100/"), 489 new MatchCondition(IntentFilter.NO_MATCH_DATA, null, null, null, 490 "scheme1://authority1:200/"), }); 491 } 492 493 public void testDataTypes() throws MalformedMimeTypeException { 494 for (int i = 0; i < 10; i++) { 495 mIntentFilter.addDataType(DATA_TYPE + i); 496 } 497 assertEquals(10, mIntentFilter.countDataTypes()); 498 final Iterator<String> iter = mIntentFilter.typesIterator(); 499 String actual = null; 500 int i = 0; 501 while (iter.hasNext()) { 502 actual = iter.next(); 503 assertEquals(DATA_TYPE + i, actual); 504 assertEquals(DATA_TYPE + i, mIntentFilter.getDataType(i)); 505 assertTrue(mIntentFilter.hasDataType(DATA_TYPE + i)); 506 assertFalse(mIntentFilter.hasDataType(DATA_TYPE + i + 10)); 507 i++; 508 } 509 } 510 511 public void testMatchData() throws MalformedMimeTypeException { 512 int expected = IntentFilter.MATCH_CATEGORY_EMPTY + IntentFilter.MATCH_ADJUSTMENT_NORMAL; 513 assertEquals(expected, mIntentFilter.matchData(null, null, null)); 514 assertEquals(expected, mIntentFilter.matchData(null, DATA_SCHEME, null)); 515 516 assertEquals(IntentFilter.NO_MATCH_DATA, mIntentFilter.matchData(null, DATA_SCHEME, URI)); 517 assertEquals(IntentFilter.NO_MATCH_DATA, mIntentFilter.matchData(DATA_TYPE, DATA_SCHEME, 518 URI)); 519 520 mIntentFilter.addDataScheme(DATA_SCHEME); 521 assertEquals(IntentFilter.NO_MATCH_DATA, mIntentFilter.matchData(DATA_TYPE, 522 "mDataSchemestest", URI)); 523 assertEquals(IntentFilter.NO_MATCH_DATA, mIntentFilter.matchData(DATA_TYPE, "", URI)); 524 525 expected = IntentFilter.MATCH_CATEGORY_SCHEME + IntentFilter.MATCH_ADJUSTMENT_NORMAL; 526 assertEquals(expected, mIntentFilter.matchData(null, DATA_SCHEME, URI)); 527 assertEquals(IntentFilter.NO_MATCH_TYPE, mIntentFilter.matchData(DATA_TYPE, DATA_SCHEME, 528 URI)); 529 530 mIntentFilter.addDataType(DATA_TYPE); 531 assertEquals(IntentFilter.MATCH_CATEGORY_TYPE + IntentFilter.MATCH_ADJUSTMENT_NORMAL, 532 mIntentFilter.matchData(DATA_TYPE, DATA_SCHEME, URI)); 533 534 mIntentFilter.addDataAuthority(HOST, String.valueOf(PORT)); 535 assertEquals(IntentFilter.NO_MATCH_DATA, mIntentFilter.matchData(null, DATA_SCHEME, URI)); 536 537 final Uri uri = Uri.parse("http://" + HOST + ":" + PORT); 538 mIntentFilter.addDataPath(DATA_PATH, PatternMatcher.PATTERN_LITERAL); 539 assertEquals(IntentFilter.NO_MATCH_DATA, mIntentFilter.matchData(null, DATA_SCHEME, uri)); 540 } 541 542 public void testActions() { 543 for (int i = 0; i < 10; i++) { 544 mIntentFilter.addAction(ACTION + i); 545 } 546 assertEquals(10, mIntentFilter.countActions()); 547 final Iterator<String> iter = mIntentFilter.actionsIterator(); 548 String actual = null; 549 int i = 0; 550 while (iter.hasNext()) { 551 actual = iter.next(); 552 assertEquals(ACTION + i, actual); 553 assertEquals(ACTION + i, mIntentFilter.getAction(i)); 554 assertTrue(mIntentFilter.hasAction(ACTION + i)); 555 assertFalse(mIntentFilter.hasAction(ACTION + i + 10)); 556 assertTrue(mIntentFilter.matchAction(ACTION + i)); 557 assertFalse(mIntentFilter.matchAction(ACTION + i + 10)); 558 i++; 559 } 560 IntentFilter filter = new Match(new String[] { "action1" }, null, null, null, null, null); 561 checkMatches(filter, new MatchCondition[] { 562 new MatchCondition(IntentFilter.MATCH_CATEGORY_EMPTY, null, null, null, null), 563 new MatchCondition(IntentFilter.MATCH_CATEGORY_EMPTY, "action1", null, null, null), 564 new MatchCondition(IntentFilter.NO_MATCH_ACTION, "action2", null, null, null), }); 565 566 filter = new Match(new String[] { "action1", "action2" }, null, null, null, null, null); 567 checkMatches(filter, new MatchCondition[] { 568 new MatchCondition(IntentFilter.MATCH_CATEGORY_EMPTY, null, null, null, null), 569 new MatchCondition(IntentFilter.MATCH_CATEGORY_EMPTY, "action1", null, null, null), 570 new MatchCondition(IntentFilter.MATCH_CATEGORY_EMPTY, "action2", null, null, null), 571 new MatchCondition(IntentFilter.NO_MATCH_ACTION, "action3", null, null, null), }); 572 } 573 574 public void testWriteToXml() throws IllegalArgumentException, IllegalStateException, 575 IOException, MalformedMimeTypeException, XmlPullParserException { 576 XmlSerializer xml; 577 ByteArrayOutputStream out; 578 579 xml = new FastXmlSerializer(); 580 out = new ByteArrayOutputStream(); 581 xml.setOutput(out, "utf-8"); 582 mIntentFilter.addAction(ACTION); 583 mIntentFilter.addCategory(CATEGORY); 584 mIntentFilter.addDataAuthority(HOST, String.valueOf(PORT)); 585 mIntentFilter.addDataPath(DATA_PATH, 1); 586 mIntentFilter.addDataScheme(DATA_SCHEME); 587 mIntentFilter.addDataType(DATA_TYPE); 588 mIntentFilter.writeToXml(xml); 589 xml.flush(); 590 final XmlPullParser parser = Xml.newPullParser(); 591 final InputStream in = new ByteArrayInputStream(out.toByteArray()); 592 parser.setInput(in, "utf-8"); 593 final IntentFilter intentFilter = new IntentFilter(); 594 intentFilter.readFromXml(parser); 595 assertEquals(ACTION, intentFilter.getAction(0)); 596 assertEquals(CATEGORY, intentFilter.getCategory(0)); 597 assertEquals(DATA_TYPE, intentFilter.getDataType(0)); 598 assertEquals(DATA_SCHEME, intentFilter.getDataScheme(0)); 599 assertEquals(DATA_PATH, intentFilter.getDataPath(0).getPath()); 600 assertEquals(HOST, intentFilter.getDataAuthority(0).getHost()); 601 assertEquals(PORT, intentFilter.getDataAuthority(0).getPort()); 602 out.close(); 603 } 604 605 public void testMatchCategories() { 606 assertNull(mIntentFilter.matchCategories(null)); 607 Set<String> cat = new HashSet<String>(); 608 assertNull(mIntentFilter.matchCategories(cat)); 609 610 final String expected = "mytest"; 611 cat.add(expected); 612 assertEquals(expected, mIntentFilter.matchCategories(cat)); 613 614 cat = new HashSet<String>(); 615 cat.add(CATEGORY); 616 mIntentFilter.addCategory(CATEGORY); 617 assertNull(mIntentFilter.matchCategories(cat)); 618 cat.add(expected); 619 assertEquals(expected, mIntentFilter.matchCategories(cat)); 620 } 621 622 public void testMatchDataAuthority() { 623 assertEquals(IntentFilter.NO_MATCH_DATA, mIntentFilter.matchDataAuthority(null)); 624 mIntentFilter.addDataAuthority(HOST, String.valueOf(PORT)); 625 final Uri uri = Uri.parse("http://" + HOST + ":" + PORT); 626 assertEquals(IntentFilter.MATCH_CATEGORY_PORT, mIntentFilter.matchDataAuthority(uri)); 627 } 628 629 public void testDescribeContents() { 630 assertEquals(0, mIntentFilter.describeContents()); 631 } 632 633 public void testReadFromXml() throws NameNotFoundException, XmlPullParserException, IOException { 634 XmlPullParser parser = null; 635 ActivityInfo ai = null; 636 637 final ComponentName mComponentName = new ComponentName(mContext, MockActivity.class); 638 final PackageManager pm = mContext.getPackageManager(); 639 ai = pm.getActivityInfo(mComponentName, PackageManager.GET_META_DATA); 640 641 parser = ai.loadXmlMetaData(pm, "android.app.intent.filter"); 642 643 int type; 644 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT 645 && type != XmlPullParser.START_TAG) { 646 } 647 648 final String nodeName = parser.getName(); 649 650 if (!"intent-filter".equals(nodeName)) { 651 throw new RuntimeException(); 652 } 653 654 mIntentFilter.readFromXml(parser); 655 656 assertEquals("testAction", mIntentFilter.getAction(0)); 657 assertEquals("testCategory", mIntentFilter.getCategory(0)); 658 assertEquals("vnd.android.cursor.dir/person", mIntentFilter.getDataType(0)); 659 assertEquals("testScheme", mIntentFilter.getDataScheme(0)); 660 assertEquals("testHost", mIntentFilter.getDataAuthority(0).getHost()); 661 assertEquals(80, mIntentFilter.getDataAuthority(0).getPort()); 662 663 assertEquals("test", mIntentFilter.getDataPath(0).getPath()); 664 assertEquals("test", mIntentFilter.getDataPath(1).getPath()); 665 assertEquals("test", mIntentFilter.getDataPath(2).getPath()); 666 } 667 668 public void testDataPaths() { 669 for (int i = 0; i < 10; i++) { 670 mIntentFilter.addDataPath(DATA_PATH + i, PatternMatcher.PATTERN_PREFIX); 671 } 672 assertEquals(10, mIntentFilter.countDataPaths()); 673 Iterator<PatternMatcher> iter = mIntentFilter.pathsIterator(); 674 PatternMatcher actual = null; 675 int i = 0; 676 while (iter.hasNext()) { 677 actual = iter.next(); 678 assertEquals(DATA_PATH + i, actual.getPath()); 679 assertEquals(PatternMatcher.PATTERN_PREFIX, actual.getType()); 680 PatternMatcher p = new PatternMatcher(DATA_PATH + i, PatternMatcher.PATTERN_PREFIX); 681 assertEquals(p.getPath(), mIntentFilter.getDataPath(i).getPath()); 682 assertEquals(p.getType(), mIntentFilter.getDataPath(i).getType()); 683 assertTrue(mIntentFilter.hasDataPath(DATA_PATH + i)); 684 assertTrue(mIntentFilter.hasDataPath(DATA_PATH + i + 10)); 685 i++; 686 } 687 688 mIntentFilter = new IntentFilter(); 689 i = 0; 690 for (i = 0; i < 10; i++) { 691 mIntentFilter.addDataPath(DATA_PATH + i, PatternMatcher.PATTERN_LITERAL); 692 } 693 assertEquals(10, mIntentFilter.countDataPaths()); 694 iter = mIntentFilter.pathsIterator(); 695 i = 0; 696 while (iter.hasNext()) { 697 actual = iter.next(); 698 assertEquals(DATA_PATH + i, actual.getPath()); 699 assertEquals(PatternMatcher.PATTERN_LITERAL, actual.getType()); 700 PatternMatcher p = new PatternMatcher(DATA_PATH + i, PatternMatcher.PATTERN_LITERAL); 701 assertEquals(p.getPath(), mIntentFilter.getDataPath(i).getPath()); 702 assertEquals(p.getType(), mIntentFilter.getDataPath(i).getType()); 703 assertTrue(mIntentFilter.hasDataPath(DATA_PATH + i)); 704 assertFalse(mIntentFilter.hasDataPath(DATA_PATH + i + 10)); 705 i++; 706 } 707 mIntentFilter = new IntentFilter(); 708 i = 0; 709 for (i = 0; i < 10; i++) { 710 mIntentFilter.addDataPath(DATA_PATH + i, PatternMatcher.PATTERN_SIMPLE_GLOB); 711 } 712 assertEquals(10, mIntentFilter.countDataPaths()); 713 iter = mIntentFilter.pathsIterator(); 714 i = 0; 715 while (iter.hasNext()) { 716 actual = iter.next(); 717 assertEquals(DATA_PATH + i, actual.getPath()); 718 assertEquals(PatternMatcher.PATTERN_SIMPLE_GLOB, actual.getType()); 719 PatternMatcher p = new PatternMatcher(DATA_PATH + i, 720 PatternMatcher.PATTERN_SIMPLE_GLOB); 721 assertEquals(p.getPath(), mIntentFilter.getDataPath(i).getPath()); 722 assertEquals(p.getType(), mIntentFilter.getDataPath(i).getType()); 723 assertTrue(mIntentFilter.hasDataPath(DATA_PATH + i)); 724 assertFalse(mIntentFilter.hasDataPath(DATA_PATH + i + 10)); 725 i++; 726 } 727 728 IntentFilter filter = new Match(null, null, null, new String[] { "scheme1" }, 729 new String[] { "authority1" }, new String[] { null }); 730 checkMatches(filter, new MatchCondition[] { 731 new MatchCondition(IntentFilter.NO_MATCH_DATA, null, null, null, null), 732 new MatchCondition(IntentFilter.NO_MATCH_DATA, null, null, null, "scheme1:foo"), 733 new MatchCondition(IntentFilter.MATCH_CATEGORY_HOST, null, null, null, 734 "scheme1://authority1/"), 735 new MatchCondition(IntentFilter.NO_MATCH_DATA, null, null, null, 736 "scheme1://authority2/"), 737 new MatchCondition(IntentFilter.MATCH_CATEGORY_HOST, null, null, null, 738 "scheme1://authority1:100/"), }); 739 740 filter = new Match(null, null, null, new String[] { "scheme1" }, 741 new String[] { "authority1" }, new String[] { "100" }); 742 checkMatches(filter, new MatchCondition[] { 743 new MatchCondition(IntentFilter.NO_MATCH_DATA, null, null, null, null), 744 new MatchCondition(IntentFilter.NO_MATCH_DATA, null, null, null, "scheme1:foo"), 745 new MatchCondition(IntentFilter.NO_MATCH_DATA, null, null, null, 746 "scheme1://authority1/"), 747 new MatchCondition(IntentFilter.NO_MATCH_DATA, null, null, null, 748 "scheme1://authority2/"), 749 new MatchCondition(IntentFilter.MATCH_CATEGORY_PORT, null, null, null, 750 "scheme1://authority1:100/"), 751 new MatchCondition(IntentFilter.NO_MATCH_DATA, null, null, null, 752 "scheme1://authority1:200/"), }); 753 754 filter = new Match(null, null, null, new String[] { "scheme1" }, 755 new String[] { "authority1", "authority2" }, new String[] { "100", null }); 756 checkMatches(filter, new MatchCondition[] { 757 new MatchCondition(IntentFilter.NO_MATCH_DATA, null, null, null, null), 758 new MatchCondition(IntentFilter.NO_MATCH_DATA, null, null, null, "scheme1:foo"), 759 new MatchCondition(IntentFilter.NO_MATCH_DATA, null, null, null, 760 "scheme1://authority1/"), 761 new MatchCondition(IntentFilter.MATCH_CATEGORY_HOST, null, null, null, 762 "scheme1://authority2/"), 763 new MatchCondition(IntentFilter.MATCH_CATEGORY_PORT, null, null, null, 764 "scheme1://authority1:100/"), 765 new MatchCondition(IntentFilter.NO_MATCH_DATA, null, null, null, 766 "scheme1://authority1:200/"), }); 767 } 768 769 public void testMatchWithIntent() throws MalformedMimeTypeException { 770 final ContentResolver resolver = mContext.getContentResolver(); 771 772 Intent intent = new Intent(ACTION); 773 assertEquals(IntentFilter.NO_MATCH_ACTION, mIntentFilter.match(resolver, intent, true, 774 null)); 775 mIntentFilter.addAction(ACTION); 776 assertEquals(IntentFilter.MATCH_CATEGORY_EMPTY + IntentFilter.MATCH_ADJUSTMENT_NORMAL, 777 mIntentFilter.match(resolver, intent, true, null)); 778 779 final Uri uri = Uri.parse(DATA_SCHEME + "://" + HOST + ":" + PORT); 780 intent.setData(uri); 781 assertEquals(IntentFilter.NO_MATCH_DATA, mIntentFilter.match(resolver, intent, true, null)); 782 mIntentFilter.addDataAuthority(HOST, String.valueOf(PORT)); 783 assertEquals(IntentFilter.NO_MATCH_DATA, mIntentFilter.match(resolver, intent, true, null)); 784 intent.setType(DATA_TYPE); 785 assertEquals(IntentFilter.NO_MATCH_DATA, mIntentFilter.match(resolver, intent, true, null)); 786 787 mIntentFilter.addDataType(DATA_TYPE); 788 789 assertEquals(IntentFilter.MATCH_CATEGORY_TYPE + IntentFilter.MATCH_ADJUSTMENT_NORMAL, 790 mIntentFilter.match(resolver, intent, true, null)); 791 assertEquals(IntentFilter.MATCH_CATEGORY_TYPE + IntentFilter.MATCH_ADJUSTMENT_NORMAL, 792 mIntentFilter.match(resolver, intent, false, null)); 793 intent.addCategory(CATEGORY); 794 assertEquals(IntentFilter.NO_MATCH_CATEGORY, mIntentFilter.match(resolver, intent, true, 795 null)); 796 mIntentFilter.addCategory(CATEGORY); 797 assertEquals(IntentFilter.MATCH_CATEGORY_TYPE + IntentFilter.MATCH_ADJUSTMENT_NORMAL, 798 mIntentFilter.match(resolver, intent, true, null)); 799 800 intent.setDataAndType(uri, DATA_TYPE); 801 assertEquals(IntentFilter.NO_MATCH_DATA, mIntentFilter.match(resolver, intent, true, null)); 802 803 } 804 805 public void testMatchWithIntentData() throws MalformedMimeTypeException { 806 Set<String> cat = new HashSet<String>(); 807 assertEquals(IntentFilter.NO_MATCH_ACTION, mIntentFilter.match(ACTION, null, null, null, 808 null, null)); 809 mIntentFilter.addAction(ACTION); 810 811 assertEquals(IntentFilter.MATCH_CATEGORY_EMPTY + IntentFilter.MATCH_ADJUSTMENT_NORMAL, 812 mIntentFilter.match(ACTION, null, null, null, null, null)); 813 assertEquals(IntentFilter.MATCH_CATEGORY_EMPTY + IntentFilter.MATCH_ADJUSTMENT_NORMAL, 814 mIntentFilter.match(ACTION, null, DATA_SCHEME, null, null, null)); 815 816 assertEquals(IntentFilter.NO_MATCH_DATA, mIntentFilter.matchData(null, DATA_SCHEME, URI)); 817 818 assertEquals(IntentFilter.NO_MATCH_DATA, mIntentFilter.match(ACTION, DATA_TYPE, 819 DATA_SCHEME, URI, null, null)); 820 821 mIntentFilter.addDataScheme(DATA_SCHEME); 822 assertEquals(IntentFilter.NO_MATCH_TYPE, mIntentFilter.match(ACTION, DATA_TYPE, 823 DATA_SCHEME, URI, null, null)); 824 assertEquals(IntentFilter.NO_MATCH_DATA, mIntentFilter.match(ACTION, DATA_TYPE, "", URI, 825 null, null)); 826 mIntentFilter.addDataType(DATA_TYPE); 827 828 assertEquals(IntentFilter.MATCH_CATEGORY_TYPE + IntentFilter.MATCH_ADJUSTMENT_NORMAL, 829 mIntentFilter.match(ACTION, DATA_TYPE, DATA_SCHEME, URI, null, null)); 830 831 assertEquals(IntentFilter.NO_MATCH_TYPE, mIntentFilter.match(ACTION, null, DATA_SCHEME, 832 URI, null, null)); 833 834 assertEquals(IntentFilter.NO_MATCH_TYPE, mIntentFilter.match(ACTION, null, DATA_SCHEME, 835 URI, cat, null)); 836 837 cat.add(CATEGORY); 838 assertEquals(IntentFilter.NO_MATCH_CATEGORY, mIntentFilter.match(ACTION, DATA_TYPE, 839 DATA_SCHEME, URI, cat, null)); 840 cat = new HashSet<String>(); 841 mIntentFilter.addDataAuthority(HOST, String.valueOf(PORT)); 842 assertEquals(IntentFilter.NO_MATCH_DATA, mIntentFilter.match(ACTION, null, DATA_SCHEME, 843 URI, null, null)); 844 assertEquals(IntentFilter.NO_MATCH_DATA, mIntentFilter.match(ACTION, DATA_TYPE, 845 DATA_SCHEME, URI, null, null)); 846 847 final Uri uri = Uri.parse(DATA_SCHEME + "://" + HOST + ":" + PORT); 848 mIntentFilter.addDataPath(DATA_PATH, PatternMatcher.PATTERN_LITERAL); 849 assertEquals(IntentFilter.NO_MATCH_DATA, mIntentFilter.match(ACTION, DATA_TYPE, 850 DATA_SCHEME, uri, null, null)); 851 assertEquals(IntentFilter.NO_MATCH_DATA, mIntentFilter.match(ACTION, DATA_TYPE, 852 DATA_SCHEME, URI, null, null)); 853 854 assertEquals(IntentFilter.NO_MATCH_DATA, mIntentFilter.match(ACTION, DATA_TYPE, 855 DATA_SCHEME, URI, cat, null)); 856 cat.add(CATEGORY); 857 assertEquals(IntentFilter.NO_MATCH_DATA, mIntentFilter.match(ACTION, DATA_TYPE, 858 DATA_SCHEME, URI, cat, null)); 859 mIntentFilter.addCategory(CATEGORY); 860 assertEquals(IntentFilter.NO_MATCH_DATA, mIntentFilter.match(ACTION, DATA_TYPE, 861 DATA_SCHEME, URI, cat, null)); 862 } 863 864 public void testWriteToParcel() throws MalformedMimeTypeException { 865 mIntentFilter.addAction(ACTION); 866 mIntentFilter.addCategory(CATEGORY); 867 mIntentFilter.addDataAuthority(HOST, String.valueOf(PORT)); 868 mIntentFilter.addDataPath(DATA_PATH, 1); 869 mIntentFilter.addDataScheme(DATA_SCHEME); 870 mIntentFilter.addDataType(DATA_TYPE); 871 Parcel parcel = Parcel.obtain(); 872 mIntentFilter.writeToParcel(parcel, 1); 873 parcel.setDataPosition(0); 874 IntentFilter target = IntentFilter.CREATOR.createFromParcel(parcel); 875 assertEquals(mIntentFilter.getAction(0), target.getAction(0)); 876 assertEquals(mIntentFilter.getCategory(0), target.getCategory(0)); 877 assertEquals(mIntentFilter.getDataAuthority(0).getHost(), 878 target.getDataAuthority(0).getHost()); 879 assertEquals(mIntentFilter.getDataAuthority(0).getPort(), 880 target.getDataAuthority(0).getPort()); 881 assertEquals(mIntentFilter.getDataPath(0).getPath(), target.getDataPath(0).getPath()); 882 assertEquals(mIntentFilter.getDataScheme(0), target.getDataScheme(0)); 883 } 884 885 public void testAddDataType() throws MalformedMimeTypeException { 886 try { 887 mIntentFilter.addDataType("test"); 888 fail("should throw MalformedMimeTypeException"); 889 } catch (MalformedMimeTypeException e) { 890 // expected 891 } 892 893 mIntentFilter.addDataType(DATA_TYPE); 894 assertEquals(DATA_TYPE, mIntentFilter.getDataType(0)); 895 } 896 897 private static class Match extends IntentFilter { 898 Match(String[] actions, String[] categories, String[] mimeTypes, String[] schemes, 899 String[] authorities, String[] ports) { 900 if (actions != null) { 901 for (int i = 0; i < actions.length; i++) { 902 addAction(actions[i]); 903 } 904 } 905 if (categories != null) { 906 for (int i = 0; i < categories.length; i++) { 907 addCategory(categories[i]); 908 } 909 } 910 if (mimeTypes != null) { 911 for (int i = 0; i < mimeTypes.length; i++) { 912 try { 913 addDataType(mimeTypes[i]); 914 } catch (IntentFilter.MalformedMimeTypeException e) { 915 throw new RuntimeException("Bad mime type", e); 916 } 917 } 918 } 919 if (schemes != null) { 920 for (int i = 0; i < schemes.length; i++) { 921 addDataScheme(schemes[i]); 922 } 923 } 924 if (authorities != null) { 925 for (int i = 0; i < authorities.length; i++) { 926 addDataAuthority(authorities[i], ports != null ? ports[i] : null); 927 } 928 } 929 } 930 931 Match(String[] actions, String[] categories, String[] mimeTypes, String[] schemes, 932 String[] authorities, String[] ports, String[] paths, int[] pathTypes) { 933 this(actions, categories, mimeTypes, schemes, authorities, ports); 934 if (paths != null) { 935 for (int i = 0; i < paths.length; i++) { 936 addDataPath(paths[i], pathTypes[i]); 937 } 938 } 939 } 940 941 Match(String[] actions, String[] categories, String[] mimeTypes, String[] schemes, 942 String[] authorities, String[] ports, String[] paths, int[] pathTypes, 943 String[] ssps, int[] sspTypes) { 944 this(actions, categories, mimeTypes, schemes, authorities, ports, paths, pathTypes); 945 if (ssps != null) { 946 for (int i = 0; i < ssps.length; i++) { 947 addDataSchemeSpecificPart(ssps[i], sspTypes[i]); 948 } 949 } 950 } 951 } 952 953 private static class MatchCondition { 954 public final int result; 955 public final String action; 956 public final String mimeType; 957 public final Uri data; 958 public final String[] categories; 959 960 public MatchCondition(int _result, String _action, String[] _categories, String _mimeType, 961 String _data) { 962 result = _result; 963 action = _action; 964 mimeType = _mimeType; 965 data = _data != null ? Uri.parse(_data) : null; 966 categories = _categories; 967 } 968 } 969 970 private static void checkMatches(IntentFilter filter, MatchCondition[] results) { 971 for (int i = 0; i < results.length; i++) { 972 MatchCondition mc = results[i]; 973 HashSet<String> categories = null; 974 if (mc.categories != null) { 975 for (int j = 0; j < mc.categories.length; j++) { 976 if (categories == null) { 977 categories = new HashSet<String>(); 978 } 979 categories.add(mc.categories[j]); 980 } 981 } 982 int result = filter.match(mc.action, mc.mimeType, mc.data != null ? mc.data.getScheme() 983 : null, mc.data, categories, "test"); 984 if ((result & IntentFilter.MATCH_CATEGORY_MASK) != 985 (mc.result & IntentFilter.MATCH_CATEGORY_MASK)) { 986 StringBuilder msg = new StringBuilder(); 987 msg.append("Error matching against IntentFilter:\n"); 988 filter.dump(new StringBuilderPrinter(msg), " "); 989 msg.append("Match action: "); 990 msg.append(mc.action); 991 msg.append("\nMatch mimeType: "); 992 msg.append(mc.mimeType); 993 msg.append("\nMatch data: "); 994 msg.append(mc.data); 995 msg.append("\nMatch categories: "); 996 if (mc.categories != null) { 997 for (int j = 0; j < mc.categories.length; j++) { 998 if (j > 0) 999 msg.append(", "); 1000 msg.append(mc.categories[j]); 1001 } 1002 } 1003 msg.append("\nExpected result: 0x"); 1004 msg.append(Integer.toHexString(mc.result)); 1005 msg.append(", got result: 0x"); 1006 msg.append(Integer.toHexString(result)); 1007 throw new RuntimeException(msg.toString()); 1008 } 1009 } 1010 } 1011 1012 public void testPaths() throws Exception { 1013 IntentFilter filter = new Match(null, null, null, 1014 new String[]{"scheme"}, new String[]{"authority"}, null, 1015 new String[]{"/literal1", "/2literal"}, 1016 new int[]{PATTERN_LITERAL, PATTERN_LITERAL}); 1017 checkMatches(filter, new MatchCondition[] { 1018 new MatchCondition(IntentFilter.NO_MATCH_DATA, null, null, null, null), 1019 new MatchCondition(IntentFilter.MATCH_CATEGORY_PATH, null, null, null, 1020 "scheme://authority/literal1"), 1021 new MatchCondition(IntentFilter.MATCH_CATEGORY_PATH, null, null, null, 1022 "scheme://authority/2literal"), 1023 new MatchCondition(IntentFilter.NO_MATCH_DATA, null, null, null, 1024 "scheme://authority/literal"), 1025 new MatchCondition(IntentFilter.NO_MATCH_DATA, null, null, null, 1026 "scheme://authority/literal12"), }); 1027 filter = new Match(null, null, null, 1028 new String[]{"scheme"}, new String[]{"authority"}, null, 1029 new String[]{"/literal1", "/2literal"}, new int[]{PATTERN_PREFIX, PATTERN_PREFIX}); 1030 checkMatches(filter, new MatchCondition[] { 1031 new MatchCondition(IntentFilter.NO_MATCH_DATA, null, null, null, null), 1032 new MatchCondition(IntentFilter.MATCH_CATEGORY_PATH, null, null, null, 1033 "scheme://authority/literal1"), 1034 new MatchCondition(IntentFilter.MATCH_CATEGORY_PATH, null, null, null, 1035 "scheme://authority/2literal"), 1036 new MatchCondition(IntentFilter.NO_MATCH_DATA, null, null, null, 1037 "scheme://authority/literal"), 1038 new MatchCondition(IntentFilter.MATCH_CATEGORY_PATH, null, null, null, 1039 "scheme://authority/literal12"), }); 1040 filter = new Match(null, null, null, 1041 new String[]{"scheme"}, new String[]{"authority"}, null, new String[]{"/.*"}, 1042 new int[]{PATTERN_SIMPLE_GLOB}); 1043 checkMatches(filter, new MatchCondition[] { 1044 new MatchCondition(IntentFilter.NO_MATCH_DATA, null, null, null, null), 1045 new MatchCondition(IntentFilter.MATCH_CATEGORY_PATH, null, null, null, 1046 "scheme://authority/literal1"), 1047 new MatchCondition(IntentFilter.MATCH_CATEGORY_PATH, null, null, null, 1048 "scheme://authority/"), 1049 new MatchCondition(IntentFilter.NO_MATCH_DATA, null, null, null, 1050 "scheme://authority"), }); 1051 filter = new Match(null, null, null, 1052 new String[]{"scheme"}, new String[]{"authority"}, null, new String[]{".*"}, 1053 new int[]{PATTERN_SIMPLE_GLOB}); 1054 checkMatches(filter, new MatchCondition[] { 1055 new MatchCondition(IntentFilter.NO_MATCH_DATA, null, null, null, null), 1056 new MatchCondition(IntentFilter.MATCH_CATEGORY_PATH, null, null, null, 1057 "scheme://authority/literal1"), 1058 new MatchCondition(IntentFilter.MATCH_CATEGORY_PATH, null, null, null, 1059 "scheme://authority/"), 1060 new MatchCondition(IntentFilter.MATCH_CATEGORY_PATH, null, null, null, 1061 "scheme://authority"), }); 1062 filter = new Match(null, null, null, 1063 new String[]{"scheme"}, new String[]{"authority"}, null, new String[]{"/a1*b"}, 1064 new int[]{PATTERN_SIMPLE_GLOB}); 1065 checkMatches(filter, new MatchCondition[] { 1066 new MatchCondition(IntentFilter.NO_MATCH_DATA, null, null, null, null), 1067 new MatchCondition(IntentFilter.MATCH_CATEGORY_PATH, null, null, null, 1068 "scheme://authority/ab"), 1069 new MatchCondition(IntentFilter.MATCH_CATEGORY_PATH, null, null, null, 1070 "scheme://authority/a1b"), 1071 new MatchCondition(IntentFilter.MATCH_CATEGORY_PATH, null, null, null, 1072 "scheme://authority/a11b"), 1073 new MatchCondition(IntentFilter.NO_MATCH_DATA, null, null, null, 1074 "scheme://authority/a2b"), 1075 new MatchCondition(IntentFilter.NO_MATCH_DATA, null, null, null, 1076 "scheme://authority/a1bc"), 1077 new MatchCondition(IntentFilter.NO_MATCH_DATA, null, null, null, 1078 "scheme://authority/"), }); 1079 filter = new Match(null, null, null, 1080 new String[]{"scheme"}, new String[]{"authority"}, null, new String[]{"/a1*"}, 1081 new int[]{PATTERN_SIMPLE_GLOB}); 1082 checkMatches(filter, new MatchCondition[] { 1083 new MatchCondition(IntentFilter.NO_MATCH_DATA, null, null, null, null), 1084 new MatchCondition(IntentFilter.MATCH_CATEGORY_PATH, null, null, null, 1085 "scheme://authority/a1"), 1086 new MatchCondition(IntentFilter.NO_MATCH_DATA, null, null, null, 1087 "scheme://authority/ab"), 1088 new MatchCondition(IntentFilter.MATCH_CATEGORY_PATH, null, null, null, 1089 "scheme://authority/a11"), 1090 new MatchCondition(IntentFilter.NO_MATCH_DATA, null, null, null, 1091 "scheme://authority/a1b"), 1092 new MatchCondition(IntentFilter.MATCH_CATEGORY_PATH, null, null, null, 1093 "scheme://authority/a11"), 1094 new MatchCondition(IntentFilter.NO_MATCH_DATA, null, null, null, 1095 "scheme://authority/a2"), }); 1096 filter = new Match(null, null, null, 1097 new String[]{"scheme"}, new String[]{"authority"}, null, new String[]{"/a\\.*b"}, 1098 new int[]{PATTERN_SIMPLE_GLOB}); 1099 checkMatches(filter, new MatchCondition[] { 1100 new MatchCondition(IntentFilter.NO_MATCH_DATA, null, null, null, null), 1101 new MatchCondition(IntentFilter.MATCH_CATEGORY_PATH, null, null, null, 1102 "scheme://authority/ab"), 1103 new MatchCondition(IntentFilter.MATCH_CATEGORY_PATH, null, null, null, 1104 "scheme://authority/a.b"), 1105 new MatchCondition(IntentFilter.MATCH_CATEGORY_PATH, null, null, null, 1106 "scheme://authority/a..b"), 1107 new MatchCondition(IntentFilter.NO_MATCH_DATA, null, null, null, 1108 "scheme://authority/a2b"), 1109 new MatchCondition(IntentFilter.NO_MATCH_DATA, null, null, null, 1110 "scheme://authority/a.bc"), 1111 new MatchCondition(IntentFilter.NO_MATCH_DATA, null, null, null, 1112 "scheme://authority/"), }); 1113 filter = new Match(null, null, null, 1114 new String[]{"scheme"}, new String[]{"authority"}, null, new String[]{"/a.*b"}, 1115 new int[]{PATTERN_SIMPLE_GLOB}); 1116 checkMatches(filter, new MatchCondition[] { 1117 new MatchCondition(IntentFilter.NO_MATCH_DATA, null, null, null, null), 1118 new MatchCondition(IntentFilter.MATCH_CATEGORY_PATH, null, null, null, 1119 "scheme://authority/ab"), 1120 new MatchCondition(IntentFilter.MATCH_CATEGORY_PATH, null, null, null, 1121 "scheme://authority/a.b"), 1122 new MatchCondition(IntentFilter.MATCH_CATEGORY_PATH, null, null, null, 1123 "scheme://authority/a.1b"), 1124 new MatchCondition(IntentFilter.MATCH_CATEGORY_PATH, null, null, null, 1125 "scheme://authority/a2b"), 1126 new MatchCondition(IntentFilter.NO_MATCH_DATA, null, null, null, 1127 "scheme://authority/a.bc"), 1128 new MatchCondition(IntentFilter.NO_MATCH_DATA, null, null, null, 1129 "scheme://authority/"), }); 1130 filter = new Match(null, null, null, 1131 new String[]{"scheme"}, new String[]{"authority"}, null, new String[]{"/a.*"}, 1132 new int[]{PATTERN_SIMPLE_GLOB}); 1133 checkMatches(filter, new MatchCondition[] { 1134 new MatchCondition(IntentFilter.NO_MATCH_DATA, null, null, null, null), 1135 new MatchCondition(IntentFilter.MATCH_CATEGORY_PATH, null, null, null, 1136 "scheme://authority/ab"), 1137 new MatchCondition(IntentFilter.MATCH_CATEGORY_PATH, null, null, null, 1138 "scheme://authority/a.b"), 1139 new MatchCondition(IntentFilter.MATCH_CATEGORY_PATH, null, null, null, 1140 "scheme://authority/a.1b"), 1141 new MatchCondition(IntentFilter.MATCH_CATEGORY_PATH, null, null, null, 1142 "scheme://authority/a2b"), 1143 new MatchCondition(IntentFilter.MATCH_CATEGORY_PATH, null, null, null, 1144 "scheme://authority/a.bc"), 1145 new MatchCondition(IntentFilter.NO_MATCH_DATA, null, null, null, 1146 "scheme://authority/"), }); 1147 filter = new Match(null, null, null, 1148 new String[]{"scheme"}, new String[]{"authority"}, null, new String[]{"/a.\\*b"}, 1149 new int[]{PATTERN_SIMPLE_GLOB}); 1150 checkMatches(filter, new MatchCondition[] { 1151 new MatchCondition(IntentFilter.NO_MATCH_DATA, null, null, null, null), 1152 new MatchCondition(IntentFilter.NO_MATCH_DATA, null, null, null, 1153 "scheme://authority/ab"), 1154 new MatchCondition(IntentFilter.MATCH_CATEGORY_PATH, null, null, null, 1155 "scheme://authority/a.*b"), 1156 new MatchCondition(IntentFilter.MATCH_CATEGORY_PATH, null, null, null, 1157 "scheme://authority/a1*b"), 1158 new MatchCondition(IntentFilter.NO_MATCH_DATA, null, null, null, 1159 "scheme://authority/a2b"), 1160 new MatchCondition(IntentFilter.NO_MATCH_DATA, null, null, null, 1161 "scheme://authority/a.bc"), 1162 new MatchCondition(IntentFilter.NO_MATCH_DATA, null, null, null, 1163 "scheme://authority/"), }); 1164 filter = new Match(null, null, null, 1165 new String[]{"scheme"}, new String[]{"authority"}, null, new String[]{"/a.\\*"}, 1166 new int[]{PATTERN_SIMPLE_GLOB}); 1167 checkMatches(filter, new MatchCondition[] { 1168 new MatchCondition(IntentFilter.NO_MATCH_DATA, null, null, null, null), 1169 new MatchCondition(IntentFilter.NO_MATCH_DATA, null, null, null, 1170 "scheme://authority/ab"), 1171 new MatchCondition(IntentFilter.MATCH_CATEGORY_PATH, null, null, null, 1172 "scheme://authority/a.*"), 1173 new MatchCondition(IntentFilter.MATCH_CATEGORY_PATH, null, null, null, 1174 "scheme://authority/a1*"), 1175 new MatchCondition(IntentFilter.NO_MATCH_DATA, null, null, null, 1176 "scheme://authority/a1b"), }); 1177 } 1178 1179 public void testDump() throws MalformedMimeTypeException { 1180 TestPrinter printer = new TestPrinter(); 1181 String prefix = "TestIntentFilter"; 1182 IntentFilter filter = new IntentFilter(ACTION, DATA_TYPE); 1183 filter.dump(printer, prefix); 1184 assertTrue(printer.isPrintlnCalled); 1185 } 1186 1187 private class TestPrinter implements Printer { 1188 public boolean isPrintlnCalled; 1189 public void println(String x) { 1190 isPrintlnCalled = true; 1191 } 1192 } 1193 } 1194