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.content.IntentFilter.MATCH_ADJUSTMENT_NORMAL; 20 import static android.content.IntentFilter.MATCH_CATEGORY_HOST; 21 import static android.content.IntentFilter.MATCH_CATEGORY_PATH; 22 import static android.content.IntentFilter.MATCH_CATEGORY_SCHEME_SPECIFIC_PART; 23 import static android.content.IntentFilter.MATCH_CATEGORY_TYPE; 24 import static android.content.IntentFilter.NO_MATCH_DATA; 25 import static android.content.UriRelativeFilter.PATH; 26 import static android.content.UriRelativeFilter.QUERY; 27 import static android.content.UriRelativeFilter.FRAGMENT; 28 import static android.content.UriRelativeFilterGroup.ACTION_ALLOW; 29 import static android.content.UriRelativeFilterGroup.ACTION_BLOCK; 30 import static android.os.PatternMatcher.PATTERN_ADVANCED_GLOB; 31 import static android.os.PatternMatcher.PATTERN_LITERAL; 32 import static android.os.PatternMatcher.PATTERN_PREFIX; 33 import static android.os.PatternMatcher.PATTERN_SIMPLE_GLOB; 34 import static android.os.PatternMatcher.PATTERN_SUFFIX; 35 36 import static org.junit.Assert.assertEquals; 37 import static org.junit.Assert.assertFalse; 38 import static org.junit.Assert.assertNotNull; 39 import static org.junit.Assert.assertNull; 40 import static org.junit.Assert.assertTrue; 41 import static org.junit.Assert.fail; 42 43 import android.content.ComponentName; 44 import android.content.ContentResolver; 45 import android.content.Context; 46 import android.content.Intent; 47 import android.content.IntentFilter; 48 import android.content.IntentFilter.AuthorityEntry; 49 import android.content.IntentFilter.MalformedMimeTypeException; 50 import android.content.UriRelativeFilter; 51 import android.content.UriRelativeFilterGroup; 52 import android.content.pm.ActivityInfo; 53 import android.content.pm.Flags; 54 import android.content.pm.PackageManager; 55 import android.content.pm.PackageManager.NameNotFoundException; 56 import android.net.Uri; 57 import android.os.Parcel; 58 import android.os.PatternMatcher; 59 import android.platform.test.annotations.AppModeSdkSandbox; 60 import android.platform.test.annotations.IgnoreUnderRavenwood; 61 import android.platform.test.annotations.PlatinumTest; 62 import android.platform.test.annotations.RequiresFlagsEnabled; 63 import android.platform.test.flag.junit.CheckFlagsRule; 64 import android.platform.test.flag.junit.DeviceFlagsValueProvider; 65 import android.platform.test.flag.junit.RavenwoodFlagsValueProvider; 66 import android.platform.test.ravenwood.RavenwoodRule; 67 import android.test.mock.MockContext; 68 import android.util.Printer; 69 import android.util.StringBuilderPrinter; 70 import android.util.Xml; 71 72 import androidx.test.InstrumentationRegistry; 73 import androidx.test.runner.AndroidJUnit4; 74 75 import org.junit.Before; 76 import org.junit.Rule; 77 import org.junit.Test; 78 import org.junit.runner.RunWith; 79 import org.xmlpull.v1.XmlPullParser; 80 import org.xmlpull.v1.XmlPullParserException; 81 import org.xmlpull.v1.XmlSerializer; 82 83 import java.io.ByteArrayInputStream; 84 import java.io.ByteArrayOutputStream; 85 import java.io.IOException; 86 import java.io.InputStream; 87 import java.util.Arrays; 88 import java.util.Collection; 89 import java.util.HashSet; 90 import java.util.Iterator; 91 import java.util.Set; 92 import java.util.function.Predicate; 93 94 @RunWith(AndroidJUnit4.class) 95 @AppModeSdkSandbox(reason = "Allow test in the SDK sandbox (does not prevent other modes).") 96 @PlatinumTest(focusArea = "pm") 97 public class IntentFilterTest { 98 @Rule 99 public final RavenwoodRule mRavenwood = new RavenwoodRule(); 100 101 @Rule 102 public final CheckFlagsRule mCheckFlagsRule = RavenwoodRule.isOnRavenwood() 103 ? RavenwoodFlagsValueProvider.createAllOnCheckFlagsRule() 104 : DeviceFlagsValueProvider.createCheckFlagsRule(); 105 106 private Context mContext; 107 private IntentFilter mIntentFilter; 108 private static final String ACTION = "testAction"; 109 private static final String CATEGORY = "testCategory"; 110 private static final String DATA_DYNAMIC_TYPE = "type/dynamic"; 111 private static final String DATA_STATIC_TYPE = "vnd.android.cursor.dir/person"; 112 private static final String DATA_SCHEME = "testDataSchemes."; 113 private static final String MIME_GROUP = "mime_group"; 114 private static final String SSP = "testSsp"; 115 private static final String HOST = "testHost"; 116 private static final int PORT = 80; 117 private static final String DATA_PATH = "testDataPath"; 118 private static final Uri URI = Uri.parse("content://com.example/people"); 119 120 @Before setUp()121 public void setUp() throws Exception { 122 if (mRavenwood.isUnderRavenwood()) { 123 // TODO: replace with mockito when better supported 124 mContext = new MockContext() { 125 @Override 126 public String getPackageName() { 127 return "android.content.cts"; 128 } 129 }; 130 } else { 131 mContext = InstrumentationRegistry.getTargetContext(); 132 } 133 134 mIntentFilter = new IntentFilter(); 135 } 136 137 @Test testConstructor()138 public void testConstructor() throws MalformedMimeTypeException { 139 140 IntentFilter filter = new IntentFilter(); 141 verifyContent(filter, null, null); 142 143 filter = new IntentFilter(ACTION); 144 verifyContent(filter, ACTION, null); 145 146 final IntentFilter actionTypeFilter = new IntentFilter(ACTION, DATA_STATIC_TYPE); 147 verifyContent(actionTypeFilter, ACTION, DATA_STATIC_TYPE); 148 149 filter = new IntentFilter(actionTypeFilter); 150 verifyContent(filter, ACTION, DATA_STATIC_TYPE); 151 152 final String dataType = "testdataType"; 153 try { 154 new IntentFilter(ACTION, dataType); 155 fail("Should throw MalformedMimeTypeException "); 156 } catch (MalformedMimeTypeException e) { 157 // expected 158 } 159 } 160 161 /** 162 * Assert that the given filter contains the given action and dataType. If 163 * action or dataType are null, assert that the filter has no actions or 164 * dataTypes registered. 165 */ verifyContent(IntentFilter filter, String action, String dataType)166 private void verifyContent(IntentFilter filter, String action, String dataType) { 167 if (action != null) { 168 assertEquals(1, filter.countActions()); 169 assertEquals(action, filter.getAction(0)); 170 } else { 171 assertEquals(0, filter.countActions()); 172 } 173 if (dataType != null) { 174 assertEquals(1, filter.countDataTypes()); 175 assertEquals(dataType, filter.getDataType(0)); 176 assertEquals(1, filter.countStaticDataTypes()); 177 } else { 178 assertEquals(0, filter.countDataTypes()); 179 assertEquals(0, filter.countStaticDataTypes()); 180 } 181 } 182 183 @Test testCategories()184 public void testCategories() { 185 for (int i = 0; i < 10; i++) { 186 mIntentFilter.addCategory(CATEGORY + i); 187 } 188 assertEquals(10, mIntentFilter.countCategories()); 189 Iterator<String> iter = mIntentFilter.categoriesIterator(); 190 String actual = null; 191 int i = 0; 192 while (iter.hasNext()) { 193 actual = iter.next(); 194 assertEquals(CATEGORY + i, actual); 195 assertEquals(CATEGORY + i, mIntentFilter.getCategory(i)); 196 assertTrue(mIntentFilter.hasCategory(CATEGORY + i)); 197 assertFalse(mIntentFilter.hasCategory(CATEGORY + i + 10)); 198 i++; 199 } 200 IntentFilter filter = new Match(null, new String[]{"category1"}, null, null, null, null); 201 checkMatches(filter, 202 new MatchCondition(IntentFilter.MATCH_CATEGORY_EMPTY, null, null, null, null), 203 new MatchCondition(IntentFilter.MATCH_CATEGORY_EMPTY, null, 204 new String[]{"category1"}, null, null), 205 new MatchCondition(IntentFilter.NO_MATCH_CATEGORY, null, 206 new String[]{"category2"}, null, null), 207 new MatchCondition(IntentFilter.NO_MATCH_CATEGORY, null, new String[]{ 208 "category1", "category2"}, null, null)); 209 210 filter = new Match(null, new String[]{"category1", "category2"}, null, null, null, null); 211 checkMatches(filter, 212 new MatchCondition(IntentFilter.MATCH_CATEGORY_EMPTY, null, null, null, null), 213 new MatchCondition(IntentFilter.MATCH_CATEGORY_EMPTY, null, 214 new String[]{"category1"}, null, null), 215 new MatchCondition(IntentFilter.MATCH_CATEGORY_EMPTY, null, 216 new String[]{"category2"}, null, null), 217 new MatchCondition(IntentFilter.MATCH_CATEGORY_EMPTY, null, new String[]{ 218 "category1", "category2"}, null, null), 219 new MatchCondition(IntentFilter.NO_MATCH_CATEGORY, null, 220 new String[]{"category3"}, null, null), 221 new MatchCondition(IntentFilter.NO_MATCH_CATEGORY, null, new String[]{ 222 "category1", "category2", "category3"}, null, null)); 223 } 224 225 @Test testMimeTypes()226 public void testMimeTypes() throws Exception { 227 IntentFilter filter = new Match(null, null, new String[]{"which1/what1"}, null, null, 228 null); 229 checkMatches(filter, 230 new MatchCondition(IntentFilter.NO_MATCH_TYPE, null, null, null, null), 231 new MatchCondition(IntentFilter.MATCH_CATEGORY_TYPE, null, null, "which1/what1", 232 null), 233 new MatchCondition(IntentFilter.MATCH_CATEGORY_TYPE, null, null, "which1/*", null), 234 new MatchCondition(IntentFilter.MATCH_CATEGORY_TYPE, null, null, "*/*", null), 235 new MatchCondition(IntentFilter.NO_MATCH_TYPE, null, null, "which2/what2", null), 236 new MatchCondition(IntentFilter.NO_MATCH_TYPE, null, null, "which2/*", null), 237 new MatchCondition(IntentFilter.NO_MATCH_TYPE, null, null, "which1/what2", null)); 238 239 filter = new Match(null, null, new String[]{"which1/what1", "which2/what2"}, null, null, 240 null); 241 checkMatches(filter, 242 new MatchCondition(IntentFilter.NO_MATCH_TYPE, null, null, null, null), 243 new MatchCondition(IntentFilter.MATCH_CATEGORY_TYPE, null, null, "which1/what1", 244 null), 245 new MatchCondition(IntentFilter.MATCH_CATEGORY_TYPE, null, null, "which1/*", null), 246 new MatchCondition(IntentFilter.MATCH_CATEGORY_TYPE, null, null, "*/*", null), 247 new MatchCondition(IntentFilter.MATCH_CATEGORY_TYPE, null, null, "which2/what2", 248 null), 249 new MatchCondition(IntentFilter.MATCH_CATEGORY_TYPE, null, null, "which2/*", null), 250 new MatchCondition(IntentFilter.NO_MATCH_TYPE, null, null, "which1/what2", null), 251 new MatchCondition(IntentFilter.NO_MATCH_TYPE, null, null, "which3/what3", null)); 252 253 filter = new Match(null, null, new String[]{"which1/*"}, null, null, null); 254 checkMatches(filter, 255 new MatchCondition(IntentFilter.NO_MATCH_TYPE, null, null, null, null), 256 new MatchCondition(IntentFilter.MATCH_CATEGORY_TYPE, null, null, "which1/what1", 257 null), 258 new MatchCondition(IntentFilter.MATCH_CATEGORY_TYPE, null, null, "which1/*", null), 259 new MatchCondition(IntentFilter.MATCH_CATEGORY_TYPE, null, null, "*/*", null), 260 new MatchCondition(IntentFilter.NO_MATCH_TYPE, null, null, "which2/what2", null), 261 new MatchCondition(IntentFilter.NO_MATCH_TYPE, null, null, "which2/*", null), 262 new MatchCondition(IntentFilter.MATCH_CATEGORY_TYPE, null, null, "which1/what2", 263 null), 264 new MatchCondition(IntentFilter.NO_MATCH_TYPE, null, null, "which3/what3", null)); 265 266 filter = new Match(null, null, new String[]{"*/*"}, null, null, null); 267 checkMatches(filter, 268 new MatchCondition(IntentFilter.NO_MATCH_TYPE, null, null, null, null), 269 new MatchCondition(IntentFilter.MATCH_CATEGORY_TYPE, null, null, "which1/what1", 270 null), 271 new MatchCondition(IntentFilter.MATCH_CATEGORY_TYPE, null, null, "which1/*", null), 272 new MatchCondition(IntentFilter.MATCH_CATEGORY_TYPE, null, null, "*/*", null), 273 new MatchCondition(IntentFilter.MATCH_CATEGORY_TYPE, null, null, "which2/what2", 274 null), 275 new MatchCondition(IntentFilter.MATCH_CATEGORY_TYPE, null, null, "which2/*", null), 276 new MatchCondition(IntentFilter.MATCH_CATEGORY_TYPE, null, null, "which1/what2", 277 null), 278 new MatchCondition(IntentFilter.MATCH_CATEGORY_TYPE, null, null, "which3/what3", 279 null)); 280 } 281 282 @Test testDynamicMimeTypes()283 public void testDynamicMimeTypes() { 284 IntentFilter filter = new Match() 285 .addDynamicMimeTypes(new String[] { "which1/what1" }); 286 checkMatches(filter, 287 new MatchCondition(IntentFilter.NO_MATCH_TYPE, null, null, null, null), 288 new MatchCondition(IntentFilter.MATCH_CATEGORY_TYPE, null, null, "which1/what1", 289 null), 290 new MatchCondition(IntentFilter.MATCH_CATEGORY_TYPE, null, null, "which1/*", null), 291 new MatchCondition(IntentFilter.MATCH_CATEGORY_TYPE, null, null, "*/*", null), 292 new MatchCondition(IntentFilter.NO_MATCH_TYPE, null, null, "which2/what2", null), 293 new MatchCondition(IntentFilter.NO_MATCH_TYPE, null, null, "which2/*", null), 294 new MatchCondition(IntentFilter.NO_MATCH_TYPE, null, null, "which1/what2", null)); 295 296 filter = new Match() 297 .addDynamicMimeTypes(new String[] { "which1/what1", "which2/what2" }); 298 checkMatches(filter, 299 new MatchCondition(IntentFilter.NO_MATCH_TYPE, null, null, null, null), 300 new MatchCondition(IntentFilter.MATCH_CATEGORY_TYPE, null, null, "which1/what1", 301 null), 302 new MatchCondition(IntentFilter.MATCH_CATEGORY_TYPE, null, null, "which1/*", null), 303 new MatchCondition(IntentFilter.MATCH_CATEGORY_TYPE, null, null, "*/*", null), 304 new MatchCondition(IntentFilter.MATCH_CATEGORY_TYPE, null, null, "which2/what2", 305 null), 306 new MatchCondition(IntentFilter.MATCH_CATEGORY_TYPE, null, null, "which2/*", null), 307 new MatchCondition(IntentFilter.NO_MATCH_TYPE, null, null, "which1/what2", null), 308 new MatchCondition(IntentFilter.NO_MATCH_TYPE, null, null, "which3/what3", null)); 309 310 filter = new Match() 311 .addDynamicMimeTypes(new String[] { "which1/*" }); 312 checkMatches(filter, 313 new MatchCondition(IntentFilter.NO_MATCH_TYPE, null, null, null, null), 314 new MatchCondition(IntentFilter.MATCH_CATEGORY_TYPE, null, null, "which1/what1", 315 null), 316 new MatchCondition(IntentFilter.MATCH_CATEGORY_TYPE, null, null, "which1/*", null), 317 new MatchCondition(IntentFilter.MATCH_CATEGORY_TYPE, null, null, "*/*", null), 318 new MatchCondition(IntentFilter.NO_MATCH_TYPE, null, null, "which2/what2", null), 319 new MatchCondition(IntentFilter.NO_MATCH_TYPE, null, null, "which2/*", null), 320 new MatchCondition(IntentFilter.MATCH_CATEGORY_TYPE, null, null, "which1/what2", 321 null), 322 new MatchCondition(IntentFilter.NO_MATCH_TYPE, null, null, "which3/what3", null)); 323 324 filter = new Match() 325 .addDynamicMimeTypes(new String[] { "*/*" }); 326 checkMatches(filter, 327 new MatchCondition(IntentFilter.NO_MATCH_TYPE, null, null, null, null), 328 new MatchCondition(IntentFilter.MATCH_CATEGORY_TYPE, null, null, "which1/what1", 329 null), 330 new MatchCondition(IntentFilter.MATCH_CATEGORY_TYPE, null, null, "which1/*", null), 331 new MatchCondition(IntentFilter.MATCH_CATEGORY_TYPE, null, null, "*/*", null), 332 new MatchCondition(IntentFilter.MATCH_CATEGORY_TYPE, null, null, "which2/what2", 333 null), 334 new MatchCondition(IntentFilter.MATCH_CATEGORY_TYPE, null, null, "which2/*", null), 335 new MatchCondition(IntentFilter.MATCH_CATEGORY_TYPE, null, null, "which1/what2", 336 null), 337 new MatchCondition(IntentFilter.MATCH_CATEGORY_TYPE, null, null, "which3/what3", 338 null)); 339 } 340 341 @Test testClearDynamicMimeTypesWithStaticType()342 public void testClearDynamicMimeTypesWithStaticType() { 343 IntentFilter filter = new Match() 344 .addMimeTypes(new String[] {"which1/what1"}) 345 .addDynamicMimeTypes(new String[] { "which2/what2" }); 346 347 checkMatches(filter, new MatchCondition(IntentFilter.NO_MATCH_TYPE, null, null, null, null), 348 new MatchCondition(IntentFilter.MATCH_CATEGORY_TYPE, null, null, "which1/what1", 349 null), 350 new MatchCondition(IntentFilter.MATCH_CATEGORY_TYPE, null, null, "which1/*", null), 351 new MatchCondition(IntentFilter.MATCH_CATEGORY_TYPE, null, null, "*/*", null), 352 new MatchCondition(IntentFilter.MATCH_CATEGORY_TYPE, null, null, "which2/what2", 353 null), 354 new MatchCondition(IntentFilter.MATCH_CATEGORY_TYPE, null, null, "which2/*", null), 355 new MatchCondition(IntentFilter.NO_MATCH_TYPE, null, null, "which1/what2", null), 356 new MatchCondition(IntentFilter.NO_MATCH_TYPE, null, null, "which3/what3", null)); 357 358 filter.clearDynamicDataTypes(); 359 360 checkMatches(filter, 361 new MatchCondition(IntentFilter.NO_MATCH_TYPE, null, null, null, null), 362 new MatchCondition(IntentFilter.MATCH_CATEGORY_TYPE, null, null, "which1/what1", 363 null), 364 new MatchCondition(IntentFilter.MATCH_CATEGORY_TYPE, null, null, "which1/*", null), 365 new MatchCondition(IntentFilter.MATCH_CATEGORY_TYPE, null, null, "*/*", null), 366 new MatchCondition(IntentFilter.NO_MATCH_TYPE, null, null, "which2/what2", null), 367 new MatchCondition(IntentFilter.NO_MATCH_TYPE, null, null, "which2/*", null), 368 new MatchCondition(IntentFilter.NO_MATCH_TYPE, null, null, "which1/what2", null)); 369 } 370 371 @Test testClearDynamicMimeTypesWithAction()372 public void testClearDynamicMimeTypesWithAction() { 373 IntentFilter filter = new Match() 374 .addActions(new String[] {"action1"}) 375 .addDynamicMimeTypes(new String[] { "which1/what1" }); 376 377 checkMatches(filter, 378 new MatchCondition(IntentFilter.NO_MATCH_TYPE, null, null, null, null), 379 new MatchCondition(IntentFilter.MATCH_CATEGORY_TYPE, null, null, "which1/what1", 380 null), 381 new MatchCondition(IntentFilter.MATCH_CATEGORY_TYPE, null, null, "which1/*", null), 382 new MatchCondition(IntentFilter.MATCH_CATEGORY_TYPE, null, null, "*/*", null), 383 new MatchCondition(IntentFilter.NO_MATCH_TYPE, null, null, "which2/what2", null), 384 new MatchCondition(IntentFilter.NO_MATCH_TYPE, null, null, "which2/*", null), 385 new MatchCondition(IntentFilter.NO_MATCH_TYPE, null, null, "which1/what2", null)); 386 387 filter.clearDynamicDataTypes(); 388 389 checkMatches(filter, 390 new MatchCondition(IntentFilter.MATCH_CATEGORY_EMPTY, null, null, null, null), 391 new MatchCondition(IntentFilter.MATCH_CATEGORY_EMPTY, "action1", null, null, null), 392 new MatchCondition(IntentFilter.NO_MATCH_TYPE, "action1", null, "which2/what2", 393 null)); 394 } 395 396 @Test testAccessPriority()397 public void testAccessPriority() { 398 final int expected = 1; 399 mIntentFilter.setPriority(expected); 400 assertEquals(expected, mIntentFilter.getPriority()); 401 } 402 403 @Test testDataSchemes()404 public void testDataSchemes() { 405 for (int i = 0; i < 10; i++) { 406 mIntentFilter.addDataScheme(DATA_SCHEME + i); 407 } 408 assertEquals(10, mIntentFilter.countDataSchemes()); 409 final Iterator<String> iter = mIntentFilter.schemesIterator(); 410 String actual = null; 411 int i = 0; 412 while (iter.hasNext()) { 413 actual = iter.next(); 414 assertEquals(DATA_SCHEME + i, actual); 415 assertEquals(DATA_SCHEME + i, mIntentFilter.getDataScheme(i)); 416 assertTrue(mIntentFilter.hasDataScheme(DATA_SCHEME + i)); 417 assertFalse(mIntentFilter.hasDataScheme(DATA_SCHEME + i + 10)); 418 i++; 419 } 420 IntentFilter filter = new Match(null, null, null, new String[]{"scheme1"}, null, null); 421 checkMatches(filter, 422 MatchCondition.data(IntentFilter.NO_MATCH_DATA, null), 423 MatchCondition.data(IntentFilter.MATCH_CATEGORY_SCHEME, "scheme1:foo"), 424 MatchCondition.data(IntentFilter.NO_MATCH_DATA, "scheme2:foo")); 425 426 filter = new Match(null, null, null, new String[]{"scheme1", "scheme2"}, null, null); 427 checkMatches(filter, 428 MatchCondition.data(IntentFilter.NO_MATCH_DATA, null), 429 MatchCondition.data(IntentFilter.MATCH_CATEGORY_SCHEME, "scheme1:foo"), 430 MatchCondition.data(IntentFilter.MATCH_CATEGORY_SCHEME, "scheme2:foo"), 431 MatchCondition.data(IntentFilter.NO_MATCH_DATA, "scheme3:foo")); 432 } 433 434 @Test testCreate()435 public void testCreate() { 436 IntentFilter filter = IntentFilter.create(ACTION, DATA_STATIC_TYPE); 437 assertNotNull(filter); 438 verifyContent(filter, ACTION, DATA_STATIC_TYPE); 439 } 440 441 @Test testSchemeSpecificParts()442 public void testSchemeSpecificParts() throws Exception { 443 IntentFilter filter = new Match(null, null, null, new String[]{"scheme"}, 444 null, null, null, null, new String[]{"ssp1", "2ssp"}, 445 new int[]{PATTERN_LITERAL, PATTERN_LITERAL}); 446 checkMatches(filter, 447 MatchCondition.data(IntentFilter.NO_MATCH_DATA, null), 448 MatchCondition.data(MATCH_CATEGORY_SCHEME_SPECIFIC_PART, "scheme:ssp1"), 449 MatchCondition.data(MATCH_CATEGORY_SCHEME_SPECIFIC_PART, "scheme:2ssp"), 450 MatchCondition.data(IntentFilter.NO_MATCH_DATA, "scheme:ssp"), 451 MatchCondition.data(IntentFilter.NO_MATCH_DATA, "scheme:ssp12")); 452 filter = new Match(null, null, null, new String[]{"scheme"}, 453 null, null, null, null, new String[]{"ssp1", "2ssp"}, 454 new int[]{PATTERN_PREFIX, PATTERN_PREFIX}); 455 checkMatches(filter, 456 MatchCondition.data(IntentFilter.NO_MATCH_DATA, null), 457 MatchCondition.data(MATCH_CATEGORY_SCHEME_SPECIFIC_PART, "scheme:ssp1"), 458 MatchCondition.data(MATCH_CATEGORY_SCHEME_SPECIFIC_PART, "scheme:2ssp"), 459 MatchCondition.data(IntentFilter.NO_MATCH_DATA, "scheme:ssp"), 460 MatchCondition.data(MATCH_CATEGORY_SCHEME_SPECIFIC_PART, "scheme:ssp12")); 461 filter = new Match(null, null, null, new String[]{"scheme"}, 462 null, null, null, null, new String[]{"p1", "sp", ".file"}, 463 new int[]{PATTERN_SUFFIX, PATTERN_SUFFIX, PATTERN_SUFFIX}); 464 checkMatches(filter, 465 MatchCondition.data(IntentFilter.NO_MATCH_DATA, null), 466 MatchCondition.data(MATCH_CATEGORY_SCHEME_SPECIFIC_PART, "scheme:ssp1"), 467 MatchCondition.data(MATCH_CATEGORY_SCHEME_SPECIFIC_PART, "scheme:2ssp"), 468 MatchCondition.data(MATCH_CATEGORY_SCHEME_SPECIFIC_PART, "scheme:something.file"), 469 MatchCondition.data(MATCH_CATEGORY_SCHEME_SPECIFIC_PART, "scheme:ssp"), 470 MatchCondition.data(NO_MATCH_DATA, "scheme:ssp12")); 471 checkMatches(new IntentFilter[]{ 472 filterForSchemeAndSchemeSpecificPart("scheme", "ssp.*", 473 PATTERN_ADVANCED_GLOB), 474 filterForSchemeAndSchemeSpecificPart("scheme", "ssp.*", 475 PATTERN_SIMPLE_GLOB)}, 476 MatchCondition.data(IntentFilter.NO_MATCH_DATA, null), 477 MatchCondition.data(MATCH_CATEGORY_SCHEME_SPECIFIC_PART, "scheme:ssp1"), 478 MatchCondition.data(MATCH_CATEGORY_SCHEME_SPECIFIC_PART, "scheme:ssp"), 479 MatchCondition.data(IntentFilter.NO_MATCH_DATA, "scheme:ss")); 480 checkMatches(new IntentFilter[]{ 481 filterForSchemeAndSchemeSpecificPart("scheme", ".*", 482 PATTERN_ADVANCED_GLOB), 483 filterForSchemeAndSchemeSpecificPart("scheme", ".*", 484 PATTERN_SIMPLE_GLOB)}, 485 MatchCondition.data(IntentFilter.NO_MATCH_DATA, null), 486 MatchCondition.data(MATCH_CATEGORY_SCHEME_SPECIFIC_PART, "scheme:ssp1"), 487 MatchCondition.data(MATCH_CATEGORY_SCHEME_SPECIFIC_PART, "scheme:ssp"), 488 MatchCondition.data(MATCH_CATEGORY_SCHEME_SPECIFIC_PART, "scheme:")); 489 checkMatches(new IntentFilter[]{ 490 filterForSchemeAndSchemeSpecificPart("scheme", "a1*b", 491 PATTERN_ADVANCED_GLOB), 492 filterForSchemeAndSchemeSpecificPart("scheme", "a1*b", 493 PATTERN_SIMPLE_GLOB)}, 494 MatchCondition.data(IntentFilter.NO_MATCH_DATA, null), 495 MatchCondition.data(MATCH_CATEGORY_SCHEME_SPECIFIC_PART, "scheme:ab"), 496 MatchCondition.data(MATCH_CATEGORY_SCHEME_SPECIFIC_PART, "scheme:a1b"), 497 MatchCondition.data(MATCH_CATEGORY_SCHEME_SPECIFIC_PART, "scheme:a11b"), 498 MatchCondition.data(IntentFilter.NO_MATCH_DATA, "scheme:a2b"), 499 MatchCondition.data(IntentFilter.NO_MATCH_DATA, "scheme:a1bc"), 500 MatchCondition.data(IntentFilter.NO_MATCH_DATA, "scheme:a")); 501 checkMatches(new IntentFilter[]{ 502 filterForSchemeAndSchemeSpecificPart("scheme", "a1*", 503 PATTERN_ADVANCED_GLOB), 504 filterForSchemeAndSchemeSpecificPart("scheme", "a1*", 505 PATTERN_SIMPLE_GLOB)}, 506 MatchCondition.data(IntentFilter.NO_MATCH_DATA, null), 507 MatchCondition.data(MATCH_CATEGORY_SCHEME_SPECIFIC_PART, "scheme:a1"), 508 MatchCondition.data(IntentFilter.NO_MATCH_DATA, "scheme:ab"), 509 MatchCondition.data(MATCH_CATEGORY_SCHEME_SPECIFIC_PART, "scheme:a11"), 510 MatchCondition.data(IntentFilter.NO_MATCH_DATA, "scheme:a1b"), 511 MatchCondition.data(MATCH_CATEGORY_SCHEME_SPECIFIC_PART, "scheme:a11"), 512 MatchCondition.data(IntentFilter.NO_MATCH_DATA, "scheme:a2")); 513 checkMatches(new IntentFilter[]{ 514 filterForSchemeAndSchemeSpecificPart("scheme", "a\\.*b", 515 PATTERN_ADVANCED_GLOB), 516 filterForSchemeAndSchemeSpecificPart("scheme", "a\\.*b", 517 PATTERN_SIMPLE_GLOB)}, 518 MatchCondition.data(IntentFilter.NO_MATCH_DATA, null), 519 MatchCondition.data(MATCH_CATEGORY_SCHEME_SPECIFIC_PART, "scheme:ab"), 520 MatchCondition.data(MATCH_CATEGORY_SCHEME_SPECIFIC_PART, "scheme:a.b"), 521 MatchCondition.data(MATCH_CATEGORY_SCHEME_SPECIFIC_PART, "scheme:a..b"), 522 MatchCondition.data(IntentFilter.NO_MATCH_DATA, "scheme:a2b"), 523 MatchCondition.data(IntentFilter.NO_MATCH_DATA, "scheme:a.bc"), 524 MatchCondition.data(IntentFilter.NO_MATCH_DATA, "scheme:")); 525 checkMatches(new IntentFilter[]{ 526 filterForSchemeAndSchemeSpecificPart("scheme", "a[.1-2]*b", 527 PATTERN_ADVANCED_GLOB), 528 filterForSchemeAndSchemeSpecificPart("scheme", "a.*b", 529 PATTERN_SIMPLE_GLOB)}, 530 MatchCondition.data(IntentFilter.NO_MATCH_DATA, null), 531 MatchCondition.data(MATCH_CATEGORY_SCHEME_SPECIFIC_PART, "scheme:ab"), 532 MatchCondition.data(MATCH_CATEGORY_SCHEME_SPECIFIC_PART, "scheme:a.b"), 533 MatchCondition.data(MATCH_CATEGORY_SCHEME_SPECIFIC_PART, "scheme:a.1b"), 534 MatchCondition.data(MATCH_CATEGORY_SCHEME_SPECIFIC_PART, "scheme:a2b"), 535 MatchCondition.data(IntentFilter.NO_MATCH_DATA, "scheme:a.bc"), 536 MatchCondition.data(IntentFilter.NO_MATCH_DATA, "scheme:")); 537 checkMatches(new IntentFilter[]{ 538 filterForSchemeAndSchemeSpecificPart("scheme", "a.*", 539 PATTERN_ADVANCED_GLOB), 540 filterForSchemeAndSchemeSpecificPart("scheme", "a.*", 541 PATTERN_SIMPLE_GLOB)}, 542 MatchCondition.data(IntentFilter.NO_MATCH_DATA, null), 543 MatchCondition.data(MATCH_CATEGORY_SCHEME_SPECIFIC_PART, "scheme:ab"), 544 MatchCondition.data(MATCH_CATEGORY_SCHEME_SPECIFIC_PART, "scheme:a.b"), 545 MatchCondition.data(MATCH_CATEGORY_SCHEME_SPECIFIC_PART, "scheme:a.1b"), 546 MatchCondition.data(MATCH_CATEGORY_SCHEME_SPECIFIC_PART, "scheme:a2b"), 547 MatchCondition.data(MATCH_CATEGORY_SCHEME_SPECIFIC_PART, "scheme:a.bc"), 548 MatchCondition.data(IntentFilter.NO_MATCH_DATA, "scheme:")); 549 checkMatches(new IntentFilter[]{ 550 filterForSchemeAndSchemeSpecificPart("scheme", "a.\\*b", 551 PATTERN_ADVANCED_GLOB), 552 filterForSchemeAndSchemeSpecificPart("scheme", "a.\\*b", 553 PATTERN_SIMPLE_GLOB)}, 554 MatchCondition.data(IntentFilter.NO_MATCH_DATA, null), 555 MatchCondition.data(IntentFilter.NO_MATCH_DATA, "scheme:ab"), 556 MatchCondition.data(MATCH_CATEGORY_SCHEME_SPECIFIC_PART, "scheme:a.*b"), 557 MatchCondition.data(MATCH_CATEGORY_SCHEME_SPECIFIC_PART, "scheme:a1*b"), 558 MatchCondition.data(IntentFilter.NO_MATCH_DATA, "scheme:a2b"), 559 MatchCondition.data(IntentFilter.NO_MATCH_DATA, "scheme:a.bc"), 560 MatchCondition.data(IntentFilter.NO_MATCH_DATA, "scheme:")); 561 checkMatches(new IntentFilter[]{ 562 filterForSchemeAndSchemeSpecificPart("scheme", "a.\\*", 563 PATTERN_ADVANCED_GLOB), 564 filterForSchemeAndSchemeSpecificPart("scheme", "a.\\*", 565 PATTERN_SIMPLE_GLOB)}, 566 MatchCondition.data(IntentFilter.NO_MATCH_DATA, null), 567 MatchCondition.data(IntentFilter.NO_MATCH_DATA, "scheme:ab"), 568 MatchCondition.data(MATCH_CATEGORY_SCHEME_SPECIFIC_PART, "scheme:a.*"), 569 MatchCondition.data(MATCH_CATEGORY_SCHEME_SPECIFIC_PART, "scheme:a1*"), 570 MatchCondition.data(IntentFilter.NO_MATCH_DATA, "scheme:a1b")); 571 } 572 filterForSchemeAndSchemeSpecificPart(String scheme, String ssp, int matchType)573 private Match filterForSchemeAndSchemeSpecificPart(String scheme, String ssp, int matchType) { 574 return new Match(null, null, null, new String[]{scheme}, 575 null, null, null, null, new String[]{ssp}, 576 new int[]{matchType}); 577 } 578 579 @Test testSchemeSpecificPartsWithWildCards()580 public void testSchemeSpecificPartsWithWildCards() throws Exception { 581 IntentFilter filter = new Match(null, null, null, new String[]{"scheme"}, 582 null, null, null, null, new String[]{"ssp1"}, 583 new int[]{PATTERN_LITERAL, PATTERN_LITERAL}); 584 checkMatches(filter, 585 MatchCondition.data(IntentFilter.NO_MATCH_DATA, null, true), 586 MatchCondition.data(MATCH_CATEGORY_SCHEME_SPECIFIC_PART, "scheme:ssp1", true), 587 MatchCondition.data(MATCH_CATEGORY_SCHEME_SPECIFIC_PART, "*:ssp1", true), 588 MatchCondition.data(MATCH_CATEGORY_SCHEME_SPECIFIC_PART, "scheme:*", true), 589 MatchCondition.data(MATCH_CATEGORY_SCHEME_SPECIFIC_PART, "*:*", true), 590 MatchCondition.data(IntentFilter.NO_MATCH_DATA, "scheme:ssp12", true)); 591 592 checkMatches(filter, 593 MatchCondition.data(IntentFilter.NO_MATCH_DATA, "*:ssp1", false), 594 MatchCondition.data(IntentFilter.NO_MATCH_DATA, "scheme:*", false), 595 MatchCondition.data(IntentFilter.NO_MATCH_DATA, "*:*", false)); 596 } 597 598 @Test testAuthorities()599 public void testAuthorities() { 600 for (int i = 0; i < 10; i++) { 601 mIntentFilter.addDataAuthority(HOST + i, String.valueOf(PORT + i)); 602 } 603 assertEquals(10, mIntentFilter.countDataAuthorities()); 604 605 final Iterator<AuthorityEntry> iter = mIntentFilter.authoritiesIterator(); 606 AuthorityEntry actual = null; 607 int i = 0; 608 while (iter.hasNext()) { 609 actual = iter.next(); 610 assertEquals(HOST + i, actual.getHost()); 611 assertEquals(PORT + i, actual.getPort()); 612 AuthorityEntry ae = new AuthorityEntry(HOST + i, String.valueOf(PORT + i)); 613 assertEquals(ae.getHost(), mIntentFilter.getDataAuthority(i).getHost()); 614 assertEquals(ae.getPort(), mIntentFilter.getDataAuthority(i).getPort()); 615 Uri uri = Uri.parse("http://" + HOST + i + ":" + String.valueOf(PORT + i)); 616 assertTrue(mIntentFilter.hasDataAuthority(uri)); 617 Uri uri2 = Uri.parse("http://" + HOST + i + 10 + ":" + PORT + i + 10); 618 assertFalse(mIntentFilter.hasDataAuthority(uri2)); 619 i++; 620 } 621 IntentFilter filter = new Match(null, null, null, new String[]{"scheme1"}, 622 new String[]{"authority1"}, new String[]{null}); 623 checkMatches(filter, 624 MatchCondition.data(IntentFilter.NO_MATCH_DATA, null), 625 MatchCondition.data(IntentFilter.NO_MATCH_DATA, "scheme1:foo"), 626 MatchCondition.data(IntentFilter.MATCH_CATEGORY_HOST, "scheme1://authority1/"), 627 MatchCondition.data(IntentFilter.NO_MATCH_DATA, "scheme1://authority2/"), 628 MatchCondition.data(IntentFilter.MATCH_CATEGORY_HOST, "scheme1://authority1:100/")); 629 630 filter = new Match(null, null, null, new String[]{"scheme1"}, 631 new String[]{"authority1"}, new String[]{"100"}); 632 checkMatches(filter, 633 MatchCondition.data(IntentFilter.NO_MATCH_DATA, null), 634 MatchCondition.data(IntentFilter.NO_MATCH_DATA, "scheme1:foo"), 635 MatchCondition.data(IntentFilter.NO_MATCH_DATA, "scheme1://authority1/"), 636 MatchCondition.data(IntentFilter.NO_MATCH_DATA, "scheme1://authority2/"), 637 MatchCondition.data(IntentFilter.MATCH_CATEGORY_PORT, "scheme1://authority1:100/"), 638 MatchCondition.data(IntentFilter.NO_MATCH_DATA, "scheme1://authority1:200/")); 639 640 filter = new Match(null, null, null, new String[]{"scheme1"}, 641 new String[]{"authority1", "authority2"}, new String[]{"100", null}); 642 checkMatches(filter, 643 MatchCondition.data(IntentFilter.NO_MATCH_DATA, null), 644 MatchCondition.data(IntentFilter.NO_MATCH_DATA, "scheme1:foo"), 645 MatchCondition.data(IntentFilter.NO_MATCH_DATA, "scheme1://authority1/"), 646 MatchCondition.data(IntentFilter.MATCH_CATEGORY_HOST, "scheme1://authority2/"), 647 MatchCondition.data(IntentFilter.MATCH_CATEGORY_PORT, "scheme1://authority1:100/"), 648 MatchCondition.data(IntentFilter.NO_MATCH_DATA, "scheme1://authority1:200/")); 649 } 650 651 @Test testAuthoritiesWithWildcards()652 public void testAuthoritiesWithWildcards() throws Exception { 653 IntentFilter filter = new Match(null, null, null, new String[]{"scheme1"}, 654 new String[]{"authority1"}, new String[]{null}); 655 checkMatches(filter, 656 MatchCondition.data(IntentFilter.NO_MATCH_DATA, null, true), 657 MatchCondition.data(IntentFilter.NO_MATCH_DATA, "scheme1:*", true), 658 MatchCondition.data(IntentFilter.MATCH_CATEGORY_HOST, "scheme1://*/", true), 659 MatchCondition.data(IntentFilter.MATCH_CATEGORY_HOST, "scheme1://*:100/", true), 660 MatchCondition.data(IntentFilter.NO_MATCH_DATA, "scheme1://*/", false), 661 MatchCondition.data(IntentFilter.NO_MATCH_DATA, "scheme1://*:100/", false)); 662 663 filter = new Match(null, null, null, new String[]{"scheme1"}, 664 new String[]{"authority1"}, new String[]{"100"}); 665 checkMatches(filter, 666 MatchCondition.data(IntentFilter.NO_MATCH_DATA, null, true), 667 MatchCondition.data(IntentFilter.NO_MATCH_DATA, "scheme1:*", true), 668 MatchCondition.data(IntentFilter.MATCH_CATEGORY_HOST, "scheme1://*/", true), 669 MatchCondition.data(IntentFilter.MATCH_CATEGORY_HOST, "scheme1://*:100/", true), 670 MatchCondition.data(IntentFilter.MATCH_CATEGORY_HOST, "scheme1://*:200/", true), 671 MatchCondition.data(IntentFilter.NO_MATCH_DATA, "*:foo", true), 672 MatchCondition.data(IntentFilter.MATCH_CATEGORY_HOST, "*://authority1/", true), 673 MatchCondition.data(IntentFilter.MATCH_CATEGORY_HOST, "*://authority1:100/", true), 674 MatchCondition.data(IntentFilter.MATCH_CATEGORY_HOST, "*://authority1:200/", true), 675 MatchCondition.data(IntentFilter.NO_MATCH_DATA, "*:*", true), 676 MatchCondition.data(IntentFilter.MATCH_CATEGORY_HOST, "*://*/", true), 677 MatchCondition.data(IntentFilter.MATCH_CATEGORY_HOST, "*://*:100/", true), 678 MatchCondition.data(IntentFilter.MATCH_CATEGORY_HOST, "*://*:200/", true)); 679 680 checkMatches(filter, 681 MatchCondition.data(IntentFilter.NO_MATCH_DATA, "scheme1://*/", false), 682 MatchCondition.data(IntentFilter.NO_MATCH_DATA, "scheme1://*:100/", false), 683 MatchCondition.data(IntentFilter.NO_MATCH_DATA, "*://authority1:100/", false), 684 MatchCondition.data(IntentFilter.NO_MATCH_DATA, "*://*/", false), 685 MatchCondition.data(IntentFilter.NO_MATCH_DATA, "*://*:100/", false)); 686 687 filter = new Match(null, null, null, new String[]{"scheme1"}, 688 new String[]{"*"}, null); 689 checkMatches(filter, 690 MatchCondition.data(IntentFilter.MATCH_CATEGORY_HOST, "scheme1://", true), 691 MatchCondition.data(IntentFilter.MATCH_CATEGORY_HOST, "scheme1://", false), 692 MatchCondition.data(IntentFilter.MATCH_CATEGORY_HOST, "scheme1://*", true)); 693 } 694 695 @Test testDataTypes()696 public void testDataTypes() throws MalformedMimeTypeException { 697 for (int i = 0; i < 10; i++) { 698 mIntentFilter.addDataType(DATA_STATIC_TYPE + i); 699 } 700 assertEquals(10, mIntentFilter.countDataTypes()); 701 assertEquals(10, mIntentFilter.countStaticDataTypes()); 702 final Iterator<String> iter = mIntentFilter.typesIterator(); 703 String actual; 704 int i = 0; 705 while (iter.hasNext()) { 706 actual = iter.next(); 707 assertEquals(DATA_STATIC_TYPE + i, actual); 708 assertEquals(DATA_STATIC_TYPE + i, mIntentFilter.getDataType(i)); 709 assertTrue(mIntentFilter.hasDataType(DATA_STATIC_TYPE + i)); 710 assertFalse(mIntentFilter.hasDataType(DATA_STATIC_TYPE + i + 10)); 711 i++; 712 } 713 } 714 715 @Test testDynamicDataTypes()716 public void testDynamicDataTypes() throws MalformedMimeTypeException { 717 for (int i = 0; i < 10; i++) { 718 mIntentFilter.addDynamicDataType(DATA_DYNAMIC_TYPE + i); 719 } 720 assertEquals(10, mIntentFilter.countDataTypes()); 721 assertEquals(0, mIntentFilter.countStaticDataTypes()); 722 723 final Iterator<String> iter = mIntentFilter.typesIterator(); 724 String actual; 725 int i = 0; 726 while (iter.hasNext()) { 727 actual = iter.next(); 728 assertEquals(DATA_DYNAMIC_TYPE + i, actual); 729 assertEquals(DATA_DYNAMIC_TYPE + i, mIntentFilter.getDataType(i)); 730 assertTrue(mIntentFilter.hasDataType(DATA_DYNAMIC_TYPE + i)); 731 assertFalse(mIntentFilter.hasDataType(DATA_DYNAMIC_TYPE + i + 10)); 732 i++; 733 } 734 } 735 736 @Test testClearDynamicDataTypes()737 public void testClearDynamicDataTypes() throws MalformedMimeTypeException { 738 for (int i = 0; i < 10; i++) { 739 mIntentFilter.addDataType(DATA_STATIC_TYPE + i); 740 mIntentFilter.addDynamicDataType(DATA_DYNAMIC_TYPE + i); 741 } 742 assertEquals(20, mIntentFilter.countDataTypes()); 743 assertEquals(10, mIntentFilter.countStaticDataTypes()); 744 745 mIntentFilter.clearDynamicDataTypes(); 746 747 assertEquals(10, mIntentFilter.countDataTypes()); 748 assertEquals(10, mIntentFilter.countStaticDataTypes()); 749 750 final Iterator<String> iter = mIntentFilter.typesIterator(); 751 String actual; 752 int i = 0; 753 while (iter.hasNext()) { 754 actual = iter.next(); 755 assertEquals(DATA_STATIC_TYPE + i, actual); 756 assertEquals(DATA_STATIC_TYPE + i, mIntentFilter.getDataType(i)); 757 assertTrue(mIntentFilter.hasDataType(DATA_STATIC_TYPE + i)); 758 assertFalse(mIntentFilter.hasDataType(DATA_DYNAMIC_TYPE + i)); 759 i++; 760 } 761 } 762 763 @Test testMimeGroups()764 public void testMimeGroups() { 765 for (int i = 0; i < 10; i++) { 766 mIntentFilter.addMimeGroup(MIME_GROUP + i); 767 } 768 assertEquals(10, mIntentFilter.countMimeGroups()); 769 final Iterator<String> iter = mIntentFilter.mimeGroupsIterator(); 770 String actual = null; 771 int i = 0; 772 while (iter.hasNext()) { 773 actual = iter.next(); 774 assertEquals(MIME_GROUP + i, actual); 775 assertEquals(MIME_GROUP + i, mIntentFilter.getMimeGroup(i)); 776 assertTrue(mIntentFilter.hasMimeGroup(MIME_GROUP + i)); 777 assertFalse(mIntentFilter.hasMimeGroup(MIME_GROUP + i + 10)); 778 i++; 779 } 780 } 781 782 @Test testAppEnumerationMatchesMimeGroups()783 public void testAppEnumerationMatchesMimeGroups() { 784 IntentFilter filter = new Match(new String[]{ACTION}, null, null, new String[]{"scheme1"}, 785 new String[]{"authority1"}, null).addMimeGroups(new String[]{"test"}); 786 787 // assume any mime type or no mime type matches a filter with a mimegroup defined. 788 checkMatches(filter, 789 new MatchCondition(IntentFilter.MATCH_CATEGORY_TYPE, 790 ACTION, null, "img/jpeg", "scheme1://authority1", true), 791 new MatchCondition(IntentFilter.MATCH_CATEGORY_TYPE, 792 ACTION, null, null, "scheme1://authority1", true), 793 new MatchCondition(IntentFilter.MATCH_CATEGORY_TYPE, 794 ACTION, null, "*/*", "scheme1://authority1", true)); 795 } 796 797 @Test testMatchData()798 public void testMatchData() throws MalformedMimeTypeException { 799 int expected = IntentFilter.MATCH_CATEGORY_EMPTY + IntentFilter.MATCH_ADJUSTMENT_NORMAL; 800 assertEquals(expected, mIntentFilter.matchData(null, null, null)); 801 assertEquals(expected, mIntentFilter.matchData(null, DATA_SCHEME, null)); 802 803 assertEquals(IntentFilter.NO_MATCH_DATA, mIntentFilter.matchData(null, DATA_SCHEME, URI)); 804 assertEquals(IntentFilter.NO_MATCH_DATA, mIntentFilter.matchData(DATA_STATIC_TYPE, 805 DATA_SCHEME, URI)); 806 807 mIntentFilter.addDataScheme(DATA_SCHEME); 808 assertEquals(IntentFilter.NO_MATCH_DATA, mIntentFilter.matchData(DATA_STATIC_TYPE, 809 "mDataSchemestest", URI)); 810 assertEquals(IntentFilter.NO_MATCH_DATA, mIntentFilter.matchData(DATA_STATIC_TYPE, "", 811 URI)); 812 813 expected = IntentFilter.MATCH_CATEGORY_SCHEME + IntentFilter.MATCH_ADJUSTMENT_NORMAL; 814 assertEquals(expected, mIntentFilter.matchData(null, DATA_SCHEME, URI)); 815 assertEquals(IntentFilter.NO_MATCH_TYPE, mIntentFilter.matchData(DATA_STATIC_TYPE, 816 DATA_SCHEME, URI)); 817 818 mIntentFilter.addDataType(DATA_STATIC_TYPE); 819 assertEquals(IntentFilter.MATCH_CATEGORY_TYPE + IntentFilter.MATCH_ADJUSTMENT_NORMAL, 820 mIntentFilter.matchData(DATA_STATIC_TYPE, DATA_SCHEME, URI)); 821 822 mIntentFilter.addDataAuthority(HOST, String.valueOf(PORT)); 823 assertEquals(IntentFilter.NO_MATCH_DATA, mIntentFilter.matchData(null, DATA_SCHEME, URI)); 824 825 final Uri uri = Uri.parse("http://" + HOST + ":" + PORT); 826 mIntentFilter.addDataPath(DATA_PATH, PatternMatcher.PATTERN_LITERAL); 827 assertEquals(IntentFilter.NO_MATCH_DATA, mIntentFilter.matchData(null, DATA_SCHEME, uri)); 828 } 829 830 @Test 831 @IgnoreUnderRavenwood 832 @RequiresFlagsEnabled(Flags.FLAG_RELATIVE_REFERENCE_INTENT_FILTERS) testMatchDataWithRelRefGroups()833 public void testMatchDataWithRelRefGroups() { 834 Uri uri = Uri.parse("https://" + HOST + "/path?query=string&cat=gizmo#fragment"); 835 Uri uri2 = Uri.parse("https://" + HOST + "/path?query=string;cat=gizmo#fragment"); 836 mIntentFilter.addDataScheme(DATA_SCHEME); 837 mIntentFilter.addDataAuthority(HOST, null); 838 assertEquals(MATCH_CATEGORY_HOST + MATCH_ADJUSTMENT_NORMAL, 839 mIntentFilter.matchData(null, DATA_SCHEME, uri)); 840 841 // PATH matches so group match 842 UriRelativeFilterGroup matchGroup = new UriRelativeFilterGroup(ACTION_ALLOW); 843 matchGroup.addUriRelativeFilter(new UriRelativeFilter(PATH, PATTERN_LITERAL, "/path")); 844 mIntentFilter.addUriRelativeFilterGroup(matchGroup); 845 assertEquals(MATCH_CATEGORY_PATH + MATCH_ADJUSTMENT_NORMAL, 846 mIntentFilter.matchData(null, DATA_SCHEME, uri)); 847 mIntentFilter.clearUriRelativeFilterGroups(); 848 849 // PATH does not match so group does not match 850 UriRelativeFilterGroup noMatchGroup = new UriRelativeFilterGroup(ACTION_ALLOW); 851 noMatchGroup.addUriRelativeFilter(new UriRelativeFilter(PATH, PATTERN_LITERAL, "/trail")); 852 mIntentFilter.addUriRelativeFilterGroup(noMatchGroup); 853 assertEquals(NO_MATCH_DATA, mIntentFilter.matchData(null, DATA_SCHEME, uri)); 854 mIntentFilter.clearUriRelativeFilterGroups(); 855 856 // PATH and QUERY match so group match 857 UriRelativeFilterGroup matchGroup1 = new UriRelativeFilterGroup(ACTION_ALLOW); 858 matchGroup1.addUriRelativeFilter(new UriRelativeFilter(PATH, PATTERN_LITERAL, "/path")); 859 matchGroup1.addUriRelativeFilter(new UriRelativeFilter(QUERY, PATTERN_SIMPLE_GLOB, ".*")); 860 mIntentFilter.addUriRelativeFilterGroup(matchGroup1); 861 assertEquals(MATCH_CATEGORY_PATH + MATCH_ADJUSTMENT_NORMAL, 862 mIntentFilter.matchData(null, DATA_SCHEME, uri)); 863 assertEquals(MATCH_CATEGORY_PATH + MATCH_ADJUSTMENT_NORMAL, 864 mIntentFilter.matchData(null, DATA_SCHEME, uri2)); 865 mIntentFilter.clearUriRelativeFilterGroups(); 866 867 // PATH match but QUERY does not match so group does not match 868 UriRelativeFilterGroup noMatchGroup1 = new UriRelativeFilterGroup(ACTION_ALLOW); 869 noMatchGroup1.addUriRelativeFilter(new UriRelativeFilter(PATH, PATTERN_LITERAL, "/path")); 870 noMatchGroup1.addUriRelativeFilter(new UriRelativeFilter(QUERY, PATTERN_PREFIX, "query")); 871 noMatchGroup1.addUriRelativeFilter(new UriRelativeFilter(QUERY, PATTERN_SUFFIX, "widget")); 872 mIntentFilter.addUriRelativeFilterGroup(noMatchGroup1); 873 assertEquals(NO_MATCH_DATA, mIntentFilter.matchData(null, DATA_SCHEME, uri)); 874 assertEquals(NO_MATCH_DATA, mIntentFilter.matchData(null, DATA_SCHEME, uri2)); 875 mIntentFilter.clearUriRelativeFilterGroups(); 876 877 // PATH, QUERY, and FRAGMENT all match so group matches 878 UriRelativeFilterGroup matchGroup2 = new UriRelativeFilterGroup(ACTION_ALLOW); 879 matchGroup2.addUriRelativeFilter(new UriRelativeFilter(PATH, PATTERN_LITERAL, "/path")); 880 matchGroup2.addUriRelativeFilter(new UriRelativeFilter(QUERY, PATTERN_PREFIX, "query")); 881 matchGroup2.addUriRelativeFilter(new UriRelativeFilter(FRAGMENT, PATTERN_SIMPLE_GLOB, 882 "fr.*")); 883 mIntentFilter.addUriRelativeFilterGroup(matchGroup2); 884 assertEquals(MATCH_CATEGORY_PATH + MATCH_ADJUSTMENT_NORMAL, 885 mIntentFilter.matchData(null, DATA_SCHEME, uri)); 886 assertEquals(MATCH_CATEGORY_PATH + MATCH_ADJUSTMENT_NORMAL, 887 mIntentFilter.matchData(null, DATA_SCHEME, uri2)); 888 mIntentFilter.clearUriRelativeFilterGroups(); 889 890 // PATH, QUERY, and FRAGMENT all match but group is disallow 891 // and intent filter will not match 892 UriRelativeFilterGroup disallowGroup = new UriRelativeFilterGroup(ACTION_BLOCK); 893 disallowGroup.addUriRelativeFilter(new UriRelativeFilter(PATH, PATTERN_LITERAL, "/path")); 894 disallowGroup.addUriRelativeFilter(new UriRelativeFilter(QUERY, PATTERN_PREFIX, "query")); 895 disallowGroup.addUriRelativeFilter(new UriRelativeFilter(QUERY, PATTERN_SUFFIX, "gizmo")); 896 disallowGroup.addUriRelativeFilter(new UriRelativeFilter(FRAGMENT, PATTERN_SIMPLE_GLOB, 897 "fr.*")); 898 mIntentFilter.addUriRelativeFilterGroup(disallowGroup); 899 assertEquals(NO_MATCH_DATA, mIntentFilter.matchData(null, DATA_SCHEME, uri)); 900 assertEquals(NO_MATCH_DATA, mIntentFilter.matchData(null, DATA_SCHEME, uri2)); 901 mIntentFilter.clearUriRelativeFilterGroups(); 902 903 // Since disallow group is defined first it will match but prevent the 904 // intent filter from matching even though though a matching group is 905 // defined after the disallow group. 906 mIntentFilter.addUriRelativeFilterGroup(disallowGroup); 907 mIntentFilter.addUriRelativeFilterGroup(matchGroup); 908 assertEquals(NO_MATCH_DATA, mIntentFilter.matchData(null, DATA_SCHEME, uri)); 909 assertEquals(NO_MATCH_DATA, mIntentFilter.matchData(null, DATA_SCHEME, uri2)); 910 mIntentFilter.clearUriRelativeFilterGroups(); 911 912 // First group will not match so check the next group. The second group will 913 // match so the disallowed group will not be checked and the intent filter 914 // will match. 915 mIntentFilter.addUriRelativeFilterGroup(noMatchGroup); 916 mIntentFilter.addUriRelativeFilterGroup(matchGroup); 917 mIntentFilter.addUriRelativeFilterGroup(disallowGroup); 918 assertEquals(MATCH_CATEGORY_PATH + MATCH_ADJUSTMENT_NORMAL, 919 mIntentFilter.matchData(null, DATA_SCHEME, uri)); 920 assertEquals(MATCH_CATEGORY_PATH + MATCH_ADJUSTMENT_NORMAL, 921 mIntentFilter.matchData(null, DATA_SCHEME, uri2)); 922 } 923 924 @Test testActions()925 public void testActions() { 926 for (int i = 0; i < 10; i++) { 927 mIntentFilter.addAction(ACTION + i); 928 } 929 assertEquals(10, mIntentFilter.countActions()); 930 final Iterator<String> iter = mIntentFilter.actionsIterator(); 931 String actual = null; 932 int i = 0; 933 while (iter.hasNext()) { 934 actual = iter.next(); 935 assertEquals(ACTION + i, actual); 936 assertEquals(ACTION + i, mIntentFilter.getAction(i)); 937 assertTrue(mIntentFilter.hasAction(ACTION + i)); 938 assertFalse(mIntentFilter.hasAction(ACTION + i + 10)); 939 assertTrue(mIntentFilter.matchAction(ACTION + i)); 940 assertFalse(mIntentFilter.matchAction(ACTION + i + 10)); 941 i++; 942 } 943 IntentFilter filter = new Match(new String[]{"action1"}, null, null, null, null, null); 944 checkMatches(filter, 945 new MatchCondition(IntentFilter.MATCH_CATEGORY_EMPTY, null, null, null, null), 946 new MatchCondition(IntentFilter.MATCH_CATEGORY_EMPTY, "action1", null, null, null), 947 new MatchCondition(IntentFilter.NO_MATCH_ACTION, "action2", null, null, null)); 948 949 filter = new Match(new String[]{"action1", "action2"}, null, null, null, null, null); 950 checkMatches(filter, 951 new MatchCondition(IntentFilter.MATCH_CATEGORY_EMPTY, null, null, null, null), 952 new MatchCondition(IntentFilter.MATCH_CATEGORY_EMPTY, "action1", null, null, null), 953 new MatchCondition(IntentFilter.MATCH_CATEGORY_EMPTY, "action2", null, null, null), 954 new MatchCondition(IntentFilter.NO_MATCH_ACTION, "action3", null, null, null), 955 new MatchCondition(IntentFilter.NO_MATCH_ACTION, "action1", null, null, null, false, 956 Arrays.asList("action1", "action2")), 957 new MatchCondition(IntentFilter.NO_MATCH_ACTION, "action2", null, null, null, false, 958 Arrays.asList("action1", "action2")), 959 new MatchCondition(IntentFilter.MATCH_CATEGORY_EMPTY, "action1", null, null, null, 960 false, Arrays.asList("action2"))); 961 } 962 963 @Test testActionWildCards()964 public void testActionWildCards() throws Exception { 965 IntentFilter filter = 966 new Match(new String[]{"action1", "action2"}, null, null, null, null, null); 967 checkMatches(filter, 968 new MatchCondition(IntentFilter.MATCH_CATEGORY_EMPTY, null, null, null, null, true), 969 new MatchCondition(IntentFilter.MATCH_CATEGORY_EMPTY, "*", null, null, null, true), 970 new MatchCondition(IntentFilter.MATCH_CATEGORY_EMPTY, "*", null, null, null, true, 971 Arrays.asList("action1")), 972 new MatchCondition(IntentFilter.NO_MATCH_ACTION, "*", null, null, null, true, 973 Arrays.asList("action1", "action2")), 974 new MatchCondition( 975 IntentFilter.NO_MATCH_ACTION, "action3", null, null, null, true)); 976 977 checkMatches(filter, 978 new MatchCondition(IntentFilter.NO_MATCH_ACTION, "*", null, null, null, false)); 979 980 } 981 982 @Test testAppEnumerationContactProviders()983 public void testAppEnumerationContactProviders() throws Exception { 984 // sample contact source 985 IntentFilter filter = new Match(new String[]{Intent.ACTION_VIEW}, 986 new String[]{Intent.CATEGORY_DEFAULT}, 987 new String[]{"vnd.android.cursor.item/vnd.com.someapp.profile"}, 988 new String[]{"content"}, 989 new String[]{"com.android.contacts"}, 990 null /*ports*/); 991 992 // app that would like to match all contact sources 993 checkMatches(filter, 994 new MatchCondition(MATCH_CATEGORY_TYPE, 995 Intent.ACTION_VIEW, 996 null /*categories*/, 997 "vnd.android.cursor.item/*", 998 "content://com.android.contacts", 999 true)); 1000 } 1001 1002 @Test testAppEnumerationDocumentEditor()1003 public void testAppEnumerationDocumentEditor() throws Exception { 1004 // sample document editor 1005 IntentFilter filter = new Match( 1006 new String[]{ 1007 Intent.ACTION_VIEW, 1008 Intent.ACTION_EDIT, 1009 "com.app.android.intent.action.APP_EDIT", 1010 "com.app.android.intent.action.APP_VIEW"}, 1011 new String[]{Intent.CATEGORY_DEFAULT}, 1012 new String[]{ 1013 "application/msword", 1014 "application/vnd.oasis.opendocument.text", 1015 "application/rtf", 1016 "text/rtf", 1017 "text/plain", 1018 "application/pdf", 1019 "application/x-pdf", 1020 "application/docm"}, 1021 null /*schemes*/, 1022 null /*authorities*/, 1023 null /*ports*/); 1024 1025 // app that would like to match all doc editors 1026 checkMatches(filter, 1027 new MatchCondition(MATCH_CATEGORY_TYPE, 1028 Intent.ACTION_VIEW, 1029 new String[]{Intent.CATEGORY_DEFAULT}, 1030 "*/*", 1031 "content://com.example.fileprovider", 1032 true)); 1033 1034 } 1035 1036 @Test testAppEnumerationDeepLinks()1037 public void testAppEnumerationDeepLinks() throws Exception { 1038 // Sample app that supports deep-links 1039 IntentFilter filter = new Match( 1040 new String[]{Intent.ACTION_VIEW}, 1041 new String[]{ 1042 Intent.CATEGORY_DEFAULT, 1043 Intent.CATEGORY_BROWSABLE}, 1044 null /*types*/, 1045 new String[]{"http", "https"}, 1046 new String[]{"arbitrary-site.com"}, 1047 null /*ports*/); 1048 1049 // Browser that would like to see all deep-linkable http/s app, but not all apps 1050 checkMatches(filter, 1051 new MatchCondition(MATCH_CATEGORY_HOST, 1052 Intent.ACTION_VIEW, 1053 new String[]{Intent.CATEGORY_BROWSABLE}, 1054 null, 1055 "https://*", 1056 true)); 1057 checkMatches(filter, 1058 new MatchCondition(MATCH_CATEGORY_HOST, 1059 Intent.ACTION_VIEW, 1060 new String[]{Intent.CATEGORY_BROWSABLE}, 1061 null, 1062 "http://*", 1063 true)); 1064 } 1065 1066 @Test testAppEnumerationCustomShareSheet()1067 public void testAppEnumerationCustomShareSheet() throws Exception { 1068 // Sample share target 1069 IntentFilter filter = new Match( 1070 new String[]{Intent.ACTION_SEND}, 1071 new String[]{Intent.CATEGORY_DEFAULT}, 1072 new String[]{"*/*"}, 1073 null /*schemes*/, 1074 null /*authorities*/, 1075 null /*ports*/); 1076 1077 // App with custom share sheet that would like to see all jpeg targets 1078 checkMatches(filter, 1079 new MatchCondition(MATCH_CATEGORY_TYPE, 1080 Intent.ACTION_SEND, 1081 null /*categories*/, 1082 "image/jpeg", 1083 "content://com.example.fileprovider", 1084 true)); 1085 // App with custom share sheet that would like to see all jpeg targets that don't specify 1086 // a host 1087 checkMatches(filter, 1088 new MatchCondition(MATCH_CATEGORY_TYPE, 1089 Intent.ACTION_SEND, 1090 null /*categories*/, 1091 "image/jpeg", 1092 "content:", 1093 true)); 1094 } 1095 1096 @Test testAppEnumerationNoHostMatchesWildcardHost()1097 public void testAppEnumerationNoHostMatchesWildcardHost() throws Exception { 1098 IntentFilter filter = new Match( 1099 new String[]{Intent.ACTION_VIEW}, 1100 new String[]{Intent.CATEGORY_BROWSABLE}, 1101 null, 1102 new String[]{"http", "https"}, 1103 new String[]{"*"}, 1104 null /*ports*/); 1105 checkMatches(filter, 1106 new MatchCondition(MATCH_CATEGORY_HOST, 1107 Intent.ACTION_VIEW, 1108 new String[]{Intent.CATEGORY_BROWSABLE}, 1109 null, 1110 "https://*", 1111 true)); 1112 1113 checkMatches(filter, 1114 new MatchCondition(MATCH_CATEGORY_HOST, 1115 Intent.ACTION_VIEW, 1116 new String[]{Intent.CATEGORY_BROWSABLE}, 1117 null, 1118 "https://", 1119 true)); 1120 } 1121 1122 @Test testAppEnumerationNoPortMatchesPortFilter()1123 public void testAppEnumerationNoPortMatchesPortFilter() throws Exception { 1124 IntentFilter filter = new Match( 1125 new String[]{Intent.ACTION_VIEW}, 1126 new String[]{Intent.CATEGORY_BROWSABLE}, 1127 null, 1128 new String[]{"http", "https"}, 1129 new String[]{"*"}, 1130 new String[]{"81"}); 1131 checkMatches(filter, 1132 new MatchCondition(MATCH_CATEGORY_HOST, 1133 Intent.ACTION_VIEW, 1134 new String[]{Intent.CATEGORY_BROWSABLE}, 1135 null, 1136 "https://something", 1137 true)); 1138 } 1139 1140 @Test testAppEnumerationBrowser()1141 public void testAppEnumerationBrowser() throws Exception { 1142 IntentFilter appWithWebLink = new Match( 1143 new String[]{Intent.ACTION_VIEW}, 1144 new String[]{Intent.CATEGORY_BROWSABLE}, 1145 null, 1146 new String[]{"http", "https"}, 1147 new String[]{"some.app.domain"}, 1148 null); 1149 1150 IntentFilter appWithWildcardWebLink = new Match( 1151 new String[]{Intent.ACTION_VIEW}, 1152 new String[]{Intent.CATEGORY_BROWSABLE}, 1153 null, 1154 new String[]{"http", "https"}, 1155 new String[]{"*.app.domain"}, 1156 null); 1157 1158 IntentFilter browserFilterWithWildcard = new Match( 1159 new String[]{Intent.ACTION_VIEW}, 1160 new String[]{Intent.CATEGORY_BROWSABLE}, 1161 null, 1162 new String[]{"http", "https"}, 1163 new String[]{"*"}, 1164 null); 1165 1166 IntentFilter browserFilterWithoutWildcard = new Match( 1167 new String[]{Intent.ACTION_VIEW}, 1168 new String[]{Intent.CATEGORY_BROWSABLE}, 1169 null, 1170 new String[]{"http", "https"}, 1171 null, 1172 null); 1173 1174 checkMatches(browserFilterWithWildcard, 1175 new MatchCondition(MATCH_CATEGORY_HOST, 1176 Intent.ACTION_VIEW, 1177 new String[]{Intent.CATEGORY_BROWSABLE}, 1178 null, 1179 "https://", 1180 true)); 1181 checkMatches(browserFilterWithoutWildcard, 1182 new MatchCondition(IntentFilter.MATCH_CATEGORY_SCHEME | MATCH_ADJUSTMENT_NORMAL, 1183 Intent.ACTION_VIEW, 1184 new String[]{Intent.CATEGORY_BROWSABLE}, 1185 null, 1186 "https://", 1187 true)); 1188 checkMatches(appWithWebLink, 1189 new MatchCondition(NO_MATCH_DATA, 1190 Intent.ACTION_VIEW, 1191 new String[]{Intent.CATEGORY_BROWSABLE}, 1192 null, 1193 "https://", 1194 true)); 1195 checkMatches(appWithWildcardWebLink, 1196 new MatchCondition(NO_MATCH_DATA, 1197 Intent.ACTION_VIEW, 1198 new String[]{Intent.CATEGORY_BROWSABLE}, 1199 null, 1200 "https://", 1201 true)); 1202 } 1203 1204 @Test testWriteToXml()1205 public void testWriteToXml() throws IllegalArgumentException, IllegalStateException, 1206 IOException, MalformedMimeTypeException, XmlPullParserException { 1207 XmlSerializer xml; 1208 ByteArrayOutputStream out; 1209 1210 xml = Xml.newSerializer(); 1211 out = new ByteArrayOutputStream(); 1212 xml.setOutput(out, "utf-8"); 1213 mIntentFilter.addAction(ACTION); 1214 mIntentFilter.addCategory(CATEGORY); 1215 mIntentFilter.addDataAuthority(HOST, String.valueOf(PORT)); 1216 mIntentFilter.addDataPath(DATA_PATH, 1); 1217 mIntentFilter.addDataScheme(DATA_SCHEME); 1218 mIntentFilter.addDataType(DATA_STATIC_TYPE); 1219 mIntentFilter.addDynamicDataType(DATA_DYNAMIC_TYPE); 1220 mIntentFilter.addMimeGroup(MIME_GROUP); 1221 mIntentFilter.writeToXml(xml); 1222 xml.flush(); 1223 final XmlPullParser parser = Xml.newPullParser(); 1224 final InputStream in = new ByteArrayInputStream(out.toByteArray()); 1225 parser.setInput(in, "utf-8"); 1226 final IntentFilter intentFilter = new IntentFilter(); 1227 intentFilter.readFromXml(parser); 1228 assertEquals(ACTION, intentFilter.getAction(0)); 1229 assertEquals(CATEGORY, intentFilter.getCategory(0)); 1230 assertTrue(intentFilter.hasExactStaticDataType(DATA_STATIC_TYPE)); 1231 assertTrue(intentFilter.hasExactDynamicDataType(DATA_DYNAMIC_TYPE)); 1232 assertEquals(MIME_GROUP, intentFilter.getMimeGroup(0)); 1233 assertEquals(DATA_SCHEME, intentFilter.getDataScheme(0)); 1234 assertEquals(DATA_PATH, intentFilter.getDataPath(0).getPath()); 1235 assertEquals(HOST, intentFilter.getDataAuthority(0).getHost()); 1236 assertEquals(PORT, intentFilter.getDataAuthority(0).getPort()); 1237 out.close(); 1238 } 1239 1240 @Test 1241 @IgnoreUnderRavenwood 1242 @RequiresFlagsEnabled(Flags.FLAG_RELATIVE_REFERENCE_INTENT_FILTERS) testWriteToXmlWithRelRefGroup()1243 public void testWriteToXmlWithRelRefGroup() throws IllegalArgumentException, 1244 IllegalStateException, IOException, MalformedMimeTypeException, XmlPullParserException { 1245 XmlSerializer xml; 1246 ByteArrayOutputStream out; 1247 UriRelativeFilterGroup relRefGroup = new UriRelativeFilterGroup(ACTION_ALLOW); 1248 relRefGroup.addUriRelativeFilter(new UriRelativeFilter(PATH, PATTERN_LITERAL, "/path")); 1249 relRefGroup.addUriRelativeFilter(new UriRelativeFilter(QUERY, PATTERN_SIMPLE_GLOB, "q*")); 1250 1251 xml = Xml.newSerializer(); 1252 out = new ByteArrayOutputStream(); 1253 xml.setOutput(out, "utf-8"); 1254 mIntentFilter.addAction(ACTION); 1255 mIntentFilter.addCategory(CATEGORY); 1256 mIntentFilter.addDataAuthority(HOST, String.valueOf(PORT)); 1257 mIntentFilter.addDataPath(DATA_PATH, 1); 1258 mIntentFilter.addDataScheme(DATA_SCHEME); 1259 mIntentFilter.addDataType(DATA_STATIC_TYPE); 1260 mIntentFilter.addDynamicDataType(DATA_DYNAMIC_TYPE); 1261 mIntentFilter.addMimeGroup(MIME_GROUP); 1262 mIntentFilter.addUriRelativeFilterGroup(relRefGroup); 1263 mIntentFilter.writeToXml(xml); 1264 xml.flush(); 1265 final XmlPullParser parser = Xml.newPullParser(); 1266 final InputStream in = new ByteArrayInputStream(out.toByteArray()); 1267 parser.setInput(in, "utf-8"); 1268 final IntentFilter intentFilter = new IntentFilter(); 1269 intentFilter.readFromXml(parser); 1270 assertEquals(ACTION, intentFilter.getAction(0)); 1271 assertEquals(CATEGORY, intentFilter.getCategory(0)); 1272 assertTrue(intentFilter.hasExactStaticDataType(DATA_STATIC_TYPE)); 1273 assertTrue(intentFilter.hasExactDynamicDataType(DATA_DYNAMIC_TYPE)); 1274 assertEquals(MIME_GROUP, intentFilter.getMimeGroup(0)); 1275 assertEquals(DATA_SCHEME, intentFilter.getDataScheme(0)); 1276 assertEquals(DATA_PATH, intentFilter.getDataPath(0).getPath()); 1277 assertEquals(HOST, intentFilter.getDataAuthority(0).getHost()); 1278 assertEquals(PORT, intentFilter.getDataAuthority(0).getPort()); 1279 assertEquals(mIntentFilter.countUriRelativeFilterGroups(), 1280 intentFilter.countUriRelativeFilterGroups()); 1281 assertEquals(mIntentFilter.getUriRelativeFilterGroup(0).getUriRelativeFilters().size(), 1282 intentFilter.getUriRelativeFilterGroup(0).getUriRelativeFilters().size()); 1283 Iterator<UriRelativeFilter> it = 1284 intentFilter.getUriRelativeFilterGroup(0).getUriRelativeFilters().iterator(); 1285 assertTrue(relRefGroup.getUriRelativeFilters().contains(it.next())); 1286 assertTrue(relRefGroup.getUriRelativeFilters().contains(it.next())); 1287 assertFalse(it.hasNext()); 1288 out.close(); 1289 } 1290 1291 @Test testMatchCategories()1292 public void testMatchCategories() { 1293 assertNull(mIntentFilter.matchCategories(null)); 1294 Set<String> cat = new HashSet<String>(); 1295 assertNull(mIntentFilter.matchCategories(cat)); 1296 1297 final String expected = "mytest"; 1298 cat.add(expected); 1299 assertEquals(expected, mIntentFilter.matchCategories(cat)); 1300 1301 cat = new HashSet<String>(); 1302 cat.add(CATEGORY); 1303 mIntentFilter.addCategory(CATEGORY); 1304 assertNull(mIntentFilter.matchCategories(cat)); 1305 cat.add(expected); 1306 assertEquals(expected, mIntentFilter.matchCategories(cat)); 1307 } 1308 1309 @Test testMatchDataAuthority()1310 public void testMatchDataAuthority() { 1311 assertEquals(IntentFilter.NO_MATCH_DATA, mIntentFilter.matchDataAuthority(null)); 1312 mIntentFilter.addDataAuthority(HOST, String.valueOf(PORT)); 1313 final Uri uri = Uri.parse("http://" + HOST + ":" + PORT); 1314 assertEquals(IntentFilter.MATCH_CATEGORY_PORT, mIntentFilter.matchDataAuthority(uri)); 1315 } 1316 1317 @Test testDescribeContents()1318 public void testDescribeContents() { 1319 assertEquals(0, mIntentFilter.describeContents()); 1320 } 1321 1322 @Test 1323 @IgnoreUnderRavenwood(blockedBy = PackageManager.class) testReadFromXml()1324 public void testReadFromXml() 1325 throws NameNotFoundException, XmlPullParserException, IOException { 1326 XmlPullParser parser = null; 1327 ActivityInfo ai = null; 1328 1329 final ComponentName mComponentName = new ComponentName( 1330 "android.content.cts", "android.content.cts.MockActivity"); 1331 final PackageManager pm = mContext.getPackageManager(); 1332 ai = pm.getActivityInfo(mComponentName, PackageManager.GET_META_DATA); 1333 1334 parser = ai.loadXmlMetaData(pm, "android.app.intent.filter"); 1335 1336 int type; 1337 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT 1338 && type != XmlPullParser.START_TAG) { 1339 } 1340 1341 final String nodeName = parser.getName(); 1342 1343 if (!"intent-filter".equals(nodeName)) { 1344 throw new RuntimeException(); 1345 } 1346 1347 mIntentFilter.readFromXml(parser); 1348 1349 assertEquals("testAction", mIntentFilter.getAction(0)); 1350 assertEquals("testCategory", mIntentFilter.getCategory(0)); 1351 1352 assertTrue(mIntentFilter.hasExactDynamicDataType("vnd.android.cursor.dir/person")); 1353 assertTrue(mIntentFilter.hasExactStaticDataType("text/plain")); 1354 1355 assertEquals("testMimeGroup", mIntentFilter.getMimeGroup(0)); 1356 assertEquals("testScheme", mIntentFilter.getDataScheme(0)); 1357 assertEquals("testHost", mIntentFilter.getDataAuthority(0).getHost()); 1358 assertEquals(80, mIntentFilter.getDataAuthority(0).getPort()); 1359 1360 assertEquals("test", mIntentFilter.getDataPath(0).getPath()); 1361 assertEquals("test", mIntentFilter.getDataPath(1).getPath()); 1362 assertEquals("test", mIntentFilter.getDataPath(2).getPath()); 1363 } 1364 1365 @Test testDataPaths()1366 public void testDataPaths() { 1367 for (int i = 0; i < 10; i++) { 1368 mIntentFilter.addDataPath(DATA_PATH + i, PatternMatcher.PATTERN_PREFIX); 1369 } 1370 assertEquals(10, mIntentFilter.countDataPaths()); 1371 Iterator<PatternMatcher> iter = mIntentFilter.pathsIterator(); 1372 PatternMatcher actual = null; 1373 int i = 0; 1374 while (iter.hasNext()) { 1375 actual = iter.next(); 1376 assertEquals(DATA_PATH + i, actual.getPath()); 1377 assertEquals(PatternMatcher.PATTERN_PREFIX, actual.getType()); 1378 PatternMatcher p = new PatternMatcher(DATA_PATH + i, PatternMatcher.PATTERN_PREFIX); 1379 assertEquals(p.getPath(), mIntentFilter.getDataPath(i).getPath()); 1380 assertEquals(p.getType(), mIntentFilter.getDataPath(i).getType()); 1381 assertTrue(mIntentFilter.hasDataPath(DATA_PATH + i)); 1382 assertTrue(mIntentFilter.hasDataPath(DATA_PATH + i + 10)); 1383 i++; 1384 } 1385 1386 mIntentFilter = new IntentFilter(); 1387 for (i = 0; i < 10; i++) { 1388 mIntentFilter.addDataPath(DATA_PATH + i, PatternMatcher.PATTERN_SUFFIX); 1389 } 1390 assertEquals(10, mIntentFilter.countDataPaths()); 1391 iter = mIntentFilter.pathsIterator(); 1392 i = 0; 1393 while (iter.hasNext()) { 1394 actual = iter.next(); 1395 assertEquals(DATA_PATH + i, actual.getPath()); 1396 assertEquals(PatternMatcher.PATTERN_SUFFIX, actual.getType()); 1397 PatternMatcher p = new PatternMatcher(DATA_PATH + i, PatternMatcher.PATTERN_SUFFIX); 1398 assertEquals(p.getPath(), mIntentFilter.getDataPath(i).getPath()); 1399 assertEquals(p.getType(), mIntentFilter.getDataPath(i).getType()); 1400 assertTrue(mIntentFilter.hasDataPath(DATA_PATH + i)); 1401 assertTrue(mIntentFilter.hasDataPath("a" + DATA_PATH + i)); 1402 i++; 1403 } 1404 1405 mIntentFilter = new IntentFilter(); 1406 i = 0; 1407 for (i = 0; i < 10; i++) { 1408 mIntentFilter.addDataPath(DATA_PATH + i, PatternMatcher.PATTERN_LITERAL); 1409 } 1410 assertEquals(10, mIntentFilter.countDataPaths()); 1411 iter = mIntentFilter.pathsIterator(); 1412 i = 0; 1413 while (iter.hasNext()) { 1414 actual = iter.next(); 1415 assertEquals(DATA_PATH + i, actual.getPath()); 1416 assertEquals(PatternMatcher.PATTERN_LITERAL, actual.getType()); 1417 PatternMatcher p = new PatternMatcher(DATA_PATH + i, PatternMatcher.PATTERN_LITERAL); 1418 assertEquals(p.getPath(), mIntentFilter.getDataPath(i).getPath()); 1419 assertEquals(p.getType(), mIntentFilter.getDataPath(i).getType()); 1420 assertTrue(mIntentFilter.hasDataPath(DATA_PATH + i)); 1421 assertFalse(mIntentFilter.hasDataPath(DATA_PATH + i + 10)); 1422 i++; 1423 } 1424 mIntentFilter = new IntentFilter(); 1425 i = 0; 1426 for (i = 0; i < 10; i++) { 1427 mIntentFilter.addDataPath(DATA_PATH + i, PatternMatcher.PATTERN_SIMPLE_GLOB); 1428 } 1429 assertEquals(10, mIntentFilter.countDataPaths()); 1430 iter = mIntentFilter.pathsIterator(); 1431 i = 0; 1432 while (iter.hasNext()) { 1433 actual = iter.next(); 1434 assertEquals(DATA_PATH + i, actual.getPath()); 1435 assertEquals(PatternMatcher.PATTERN_SIMPLE_GLOB, actual.getType()); 1436 PatternMatcher p = new PatternMatcher(DATA_PATH + i, 1437 PatternMatcher.PATTERN_SIMPLE_GLOB); 1438 assertEquals(p.getPath(), mIntentFilter.getDataPath(i).getPath()); 1439 assertEquals(p.getType(), mIntentFilter.getDataPath(i).getType()); 1440 assertTrue(mIntentFilter.hasDataPath(DATA_PATH + i)); 1441 assertFalse(mIntentFilter.hasDataPath(DATA_PATH + i + 10)); 1442 i++; 1443 } 1444 1445 mIntentFilter = new IntentFilter(); 1446 for (i = 0; i < 10; i++) { 1447 mIntentFilter.addDataPath(DATA_PATH + i, PatternMatcher.PATTERN_ADVANCED_GLOB); 1448 } 1449 assertEquals(10, mIntentFilter.countDataPaths()); 1450 iter = mIntentFilter.pathsIterator(); 1451 i = 0; 1452 while (iter.hasNext()) { 1453 actual = iter.next(); 1454 assertEquals(DATA_PATH + i, actual.getPath()); 1455 assertEquals(PatternMatcher.PATTERN_ADVANCED_GLOB, actual.getType()); 1456 PatternMatcher p = new PatternMatcher(DATA_PATH + i, 1457 PatternMatcher.PATTERN_ADVANCED_GLOB); 1458 assertEquals(p.getPath(), mIntentFilter.getDataPath(i).getPath()); 1459 assertEquals(p.getType(), mIntentFilter.getDataPath(i).getType()); 1460 assertTrue(mIntentFilter.hasDataPath(DATA_PATH + i)); 1461 assertFalse(mIntentFilter.hasDataPath(DATA_PATH + i + 10)); 1462 i++; 1463 } 1464 1465 IntentFilter filter = new Match(null, null, null, new String[]{"scheme1"}, 1466 new String[]{"authority1"}, new String[]{null}); 1467 checkMatches(filter, 1468 MatchCondition.data(IntentFilter.NO_MATCH_DATA, null), 1469 MatchCondition.data(IntentFilter.NO_MATCH_DATA, "scheme1:foo"), 1470 MatchCondition.data(IntentFilter.MATCH_CATEGORY_HOST, "scheme1://authority1/"), 1471 MatchCondition.data(IntentFilter.NO_MATCH_DATA, "scheme1://authority2/"), 1472 MatchCondition.data(IntentFilter.MATCH_CATEGORY_HOST, "scheme1://authority1:100/")); 1473 1474 filter = new Match(null, null, null, new String[]{"scheme1"}, 1475 new String[]{"authority1"}, new String[]{"100"}); 1476 checkMatches(filter, 1477 MatchCondition.data(IntentFilter.NO_MATCH_DATA, null), 1478 MatchCondition.data(IntentFilter.NO_MATCH_DATA, "scheme1:foo"), 1479 MatchCondition.data(IntentFilter.NO_MATCH_DATA, "scheme1://authority1/"), 1480 MatchCondition.data(IntentFilter.NO_MATCH_DATA, "scheme1://authority2/"), 1481 MatchCondition.data(IntentFilter.MATCH_CATEGORY_PORT, "scheme1://authority1:100/"), 1482 MatchCondition.data(IntentFilter.NO_MATCH_DATA, "scheme1://authority1:200/")); 1483 1484 filter = new Match(null, null, null, new String[]{"scheme1"}, 1485 new String[]{"authority1", "authority2"}, new String[]{"100", null}); 1486 checkMatches(filter, 1487 MatchCondition.data(IntentFilter.NO_MATCH_DATA, null), 1488 MatchCondition.data(IntentFilter.NO_MATCH_DATA, "scheme1:foo"), 1489 MatchCondition.data(IntentFilter.NO_MATCH_DATA, "scheme1://authority1/"), 1490 MatchCondition.data(IntentFilter.MATCH_CATEGORY_HOST, "scheme1://authority2/"), 1491 MatchCondition.data(IntentFilter.MATCH_CATEGORY_PORT, "scheme1://authority1:100/"), 1492 MatchCondition.data(IntentFilter.NO_MATCH_DATA, "scheme1://authority1:200/")); 1493 } 1494 1495 @Test 1496 @IgnoreUnderRavenwood(blockedBy = ContentResolver.class) testMatchWithIntent()1497 public void testMatchWithIntent() throws MalformedMimeTypeException { 1498 final ContentResolver resolver = mContext.getContentResolver(); 1499 1500 Intent intent = new Intent(ACTION); 1501 assertEquals(IntentFilter.NO_MATCH_ACTION, mIntentFilter.match(resolver, intent, true, 1502 null)); 1503 mIntentFilter.addAction(ACTION); 1504 assertEquals(IntentFilter.MATCH_CATEGORY_EMPTY + IntentFilter.MATCH_ADJUSTMENT_NORMAL, 1505 mIntentFilter.match(resolver, intent, true, null)); 1506 1507 final Uri uri = Uri.parse(DATA_SCHEME + "://" + HOST + ":" + PORT); 1508 intent.setData(uri); 1509 assertEquals(IntentFilter.NO_MATCH_DATA, mIntentFilter.match(resolver, intent, true, null)); 1510 mIntentFilter.addDataAuthority(HOST, String.valueOf(PORT)); 1511 assertEquals(IntentFilter.NO_MATCH_DATA, mIntentFilter.match(resolver, intent, true, null)); 1512 intent.setType(DATA_STATIC_TYPE); 1513 assertEquals(IntentFilter.NO_MATCH_DATA, mIntentFilter.match(resolver, intent, true, null)); 1514 1515 mIntentFilter.addDataType(DATA_STATIC_TYPE); 1516 1517 assertEquals(IntentFilter.MATCH_CATEGORY_TYPE + IntentFilter.MATCH_ADJUSTMENT_NORMAL, 1518 mIntentFilter.match(resolver, intent, true, null)); 1519 assertEquals(IntentFilter.MATCH_CATEGORY_TYPE + IntentFilter.MATCH_ADJUSTMENT_NORMAL, 1520 mIntentFilter.match(resolver, intent, false, null)); 1521 intent.addCategory(CATEGORY); 1522 assertEquals(IntentFilter.NO_MATCH_CATEGORY, mIntentFilter.match(resolver, intent, true, 1523 null)); 1524 mIntentFilter.addCategory(CATEGORY); 1525 assertEquals(IntentFilter.MATCH_CATEGORY_TYPE + IntentFilter.MATCH_ADJUSTMENT_NORMAL, 1526 mIntentFilter.match(resolver, intent, true, null)); 1527 1528 intent.setDataAndType(uri, DATA_STATIC_TYPE); 1529 assertEquals(IntentFilter.NO_MATCH_DATA, mIntentFilter.match(resolver, intent, true, null)); 1530 1531 } 1532 1533 @Test testMatchWithIntentData()1534 public void testMatchWithIntentData() throws MalformedMimeTypeException { 1535 Set<String> cat = new HashSet<String>(); 1536 assertEquals(IntentFilter.NO_MATCH_ACTION, mIntentFilter.match(ACTION, null, null, null, 1537 null, null)); 1538 mIntentFilter.addAction(ACTION); 1539 1540 assertEquals(IntentFilter.MATCH_CATEGORY_EMPTY + IntentFilter.MATCH_ADJUSTMENT_NORMAL, 1541 mIntentFilter.match(ACTION, null, null, null, null, null)); 1542 assertEquals(IntentFilter.MATCH_CATEGORY_EMPTY + IntentFilter.MATCH_ADJUSTMENT_NORMAL, 1543 mIntentFilter.match(ACTION, null, DATA_SCHEME, null, null, null)); 1544 1545 assertEquals(IntentFilter.NO_MATCH_DATA, mIntentFilter.matchData(null, DATA_SCHEME, URI)); 1546 1547 assertEquals(IntentFilter.NO_MATCH_DATA, mIntentFilter.match(ACTION, DATA_STATIC_TYPE, 1548 DATA_SCHEME, URI, null, null)); 1549 1550 mIntentFilter.addDataScheme(DATA_SCHEME); 1551 assertEquals(IntentFilter.NO_MATCH_TYPE, mIntentFilter.match(ACTION, DATA_STATIC_TYPE, 1552 DATA_SCHEME, URI, null, null)); 1553 assertEquals(IntentFilter.NO_MATCH_DATA, mIntentFilter.match(ACTION, DATA_STATIC_TYPE, "", 1554 URI, null, null)); 1555 mIntentFilter.addDataType(DATA_STATIC_TYPE); 1556 1557 assertEquals(IntentFilter.MATCH_CATEGORY_TYPE + IntentFilter.MATCH_ADJUSTMENT_NORMAL, 1558 mIntentFilter.match(ACTION, DATA_STATIC_TYPE, DATA_SCHEME, URI, null, null)); 1559 1560 assertEquals(IntentFilter.NO_MATCH_TYPE, mIntentFilter.match(ACTION, null, DATA_SCHEME, 1561 URI, null, null)); 1562 1563 assertEquals(IntentFilter.NO_MATCH_TYPE, mIntentFilter.match(ACTION, null, DATA_SCHEME, 1564 URI, cat, null)); 1565 1566 cat.add(CATEGORY); 1567 assertEquals(IntentFilter.NO_MATCH_CATEGORY, mIntentFilter.match(ACTION, DATA_STATIC_TYPE, 1568 DATA_SCHEME, URI, cat, null)); 1569 cat = new HashSet<String>(); 1570 mIntentFilter.addDataAuthority(HOST, String.valueOf(PORT)); 1571 assertEquals(IntentFilter.NO_MATCH_DATA, mIntentFilter.match(ACTION, null, DATA_SCHEME, 1572 URI, null, null)); 1573 assertEquals(IntentFilter.NO_MATCH_DATA, mIntentFilter.match(ACTION, DATA_STATIC_TYPE, 1574 DATA_SCHEME, URI, null, null)); 1575 1576 final Uri uri = Uri.parse(DATA_SCHEME + "://" + HOST + ":" + PORT); 1577 mIntentFilter.addDataPath(DATA_PATH, PatternMatcher.PATTERN_LITERAL); 1578 assertEquals(IntentFilter.NO_MATCH_DATA, mIntentFilter.match(ACTION, DATA_STATIC_TYPE, 1579 DATA_SCHEME, uri, null, null)); 1580 assertEquals(IntentFilter.NO_MATCH_DATA, mIntentFilter.match(ACTION, DATA_STATIC_TYPE, 1581 DATA_SCHEME, URI, null, null)); 1582 1583 assertEquals(IntentFilter.NO_MATCH_DATA, mIntentFilter.match(ACTION, DATA_STATIC_TYPE, 1584 DATA_SCHEME, URI, cat, null)); 1585 cat.add(CATEGORY); 1586 assertEquals(IntentFilter.NO_MATCH_DATA, mIntentFilter.match(ACTION, DATA_STATIC_TYPE, 1587 DATA_SCHEME, URI, cat, null)); 1588 mIntentFilter.addCategory(CATEGORY); 1589 assertEquals(IntentFilter.NO_MATCH_DATA, mIntentFilter.match(ACTION, DATA_STATIC_TYPE, 1590 DATA_SCHEME, URI, cat, null)); 1591 } 1592 1593 @Test testWriteToParcel()1594 public void testWriteToParcel() throws MalformedMimeTypeException { 1595 mIntentFilter.addAction(ACTION); 1596 mIntentFilter.addCategory(CATEGORY); 1597 mIntentFilter.addDataAuthority(HOST, String.valueOf(PORT)); 1598 mIntentFilter.addDataPath(DATA_PATH, 1); 1599 mIntentFilter.addDataScheme(DATA_SCHEME); 1600 mIntentFilter.addDataType(DATA_STATIC_TYPE); 1601 mIntentFilter.addDynamicDataType(DATA_DYNAMIC_TYPE); 1602 mIntentFilter.addMimeGroup(MIME_GROUP); 1603 Parcel parcel = Parcel.obtain(); 1604 mIntentFilter.writeToParcel(parcel, 1); 1605 parcel.setDataPosition(0); 1606 IntentFilter target = IntentFilter.CREATOR.createFromParcel(parcel); 1607 assertEquals(mIntentFilter.getAction(0), target.getAction(0)); 1608 assertEquals(mIntentFilter.getCategory(0), target.getCategory(0)); 1609 assertEquals(mIntentFilter.getDataAuthority(0).getHost(), 1610 target.getDataAuthority(0).getHost()); 1611 assertEquals(mIntentFilter.getDataAuthority(0).getPort(), 1612 target.getDataAuthority(0).getPort()); 1613 assertEquals(mIntentFilter.getDataPath(0).getPath(), target.getDataPath(0).getPath()); 1614 assertEquals(mIntentFilter.getDataScheme(0), target.getDataScheme(0)); 1615 assertEquals(mIntentFilter.getDataType(0), target.getDataType(0)); 1616 assertEquals(mIntentFilter.getDataType(1), target.getDataType(1)); 1617 assertEquals(mIntentFilter.countStaticDataTypes(), target.countStaticDataTypes()); 1618 assertEquals(mIntentFilter.countDataTypes(), target.countDataTypes()); 1619 assertEquals(mIntentFilter.getMimeGroup(0), target.getMimeGroup(0)); 1620 } 1621 1622 @Test 1623 @IgnoreUnderRavenwood 1624 @RequiresFlagsEnabled(Flags.FLAG_RELATIVE_REFERENCE_INTENT_FILTERS) testWriteToParcelWithRelRefGroup()1625 public void testWriteToParcelWithRelRefGroup() throws MalformedMimeTypeException { 1626 UriRelativeFilterGroup relRefGroup = new UriRelativeFilterGroup(ACTION_ALLOW); 1627 relRefGroup.addUriRelativeFilter(new UriRelativeFilter(PATH, PATTERN_LITERAL, "/path")); 1628 relRefGroup.addUriRelativeFilter(new UriRelativeFilter(QUERY, PATTERN_SIMPLE_GLOB, "q*")); 1629 1630 mIntentFilter.addAction(ACTION); 1631 mIntentFilter.addCategory(CATEGORY); 1632 mIntentFilter.addDataAuthority(HOST, String.valueOf(PORT)); 1633 mIntentFilter.addDataPath(DATA_PATH, 1); 1634 mIntentFilter.addDataScheme(DATA_SCHEME); 1635 mIntentFilter.addDataType(DATA_STATIC_TYPE); 1636 mIntentFilter.addDynamicDataType(DATA_DYNAMIC_TYPE); 1637 mIntentFilter.addMimeGroup(MIME_GROUP); 1638 mIntentFilter.addUriRelativeFilterGroup(relRefGroup); 1639 Parcel parcel = Parcel.obtain(); 1640 mIntentFilter.writeToParcel(parcel, 1); 1641 parcel.setDataPosition(0); 1642 IntentFilter target = IntentFilter.CREATOR.createFromParcel(parcel); 1643 assertEquals(mIntentFilter.getAction(0), target.getAction(0)); 1644 assertEquals(mIntentFilter.getCategory(0), target.getCategory(0)); 1645 assertEquals(mIntentFilter.getDataAuthority(0).getHost(), 1646 target.getDataAuthority(0).getHost()); 1647 assertEquals(mIntentFilter.getDataAuthority(0).getPort(), 1648 target.getDataAuthority(0).getPort()); 1649 assertEquals(mIntentFilter.getDataPath(0).getPath(), target.getDataPath(0).getPath()); 1650 assertEquals(mIntentFilter.getDataScheme(0), target.getDataScheme(0)); 1651 assertEquals(mIntentFilter.getDataType(0), target.getDataType(0)); 1652 assertEquals(mIntentFilter.getDataType(1), target.getDataType(1)); 1653 assertEquals(mIntentFilter.countStaticDataTypes(), target.countStaticDataTypes()); 1654 assertEquals(mIntentFilter.countDataTypes(), target.countDataTypes()); 1655 assertEquals(mIntentFilter.getMimeGroup(0), target.getMimeGroup(0)); 1656 assertEquals(mIntentFilter.countUriRelativeFilterGroups(), 1657 target.countUriRelativeFilterGroups()); 1658 assertEquals(mIntentFilter.getUriRelativeFilterGroup(0).getUriRelativeFilters().size(), 1659 target.getUriRelativeFilterGroup(0).getUriRelativeFilters().size()); 1660 Iterator<UriRelativeFilter> it = 1661 target.getUriRelativeFilterGroup(0).getUriRelativeFilters().iterator(); 1662 assertTrue(relRefGroup.getUriRelativeFilters().contains(it.next())); 1663 assertTrue(relRefGroup.getUriRelativeFilters().contains(it.next())); 1664 assertFalse(it.hasNext()); 1665 } 1666 1667 @Test testAddDataType()1668 public void testAddDataType() throws MalformedMimeTypeException { 1669 try { 1670 mIntentFilter.addDataType("test"); 1671 fail("should throw MalformedMimeTypeException"); 1672 } catch (MalformedMimeTypeException e) { 1673 // expected 1674 } 1675 1676 mIntentFilter.addDataType(DATA_STATIC_TYPE); 1677 assertEquals(DATA_STATIC_TYPE, mIntentFilter.getDataType(0)); 1678 } 1679 1680 private static class Match extends IntentFilter { Match()1681 Match() { 1682 } 1683 Match(String[] actions, String[] categories, String[] mimeTypes, String[] schemes, String[] authorities, String[] ports)1684 Match(String[] actions, String[] categories, String[] mimeTypes, String[] schemes, 1685 String[] authorities, String[] ports) { 1686 if (actions != null) { 1687 addActions(actions); 1688 } 1689 if (categories != null) { 1690 for (int i = 0; i < categories.length; i++) { 1691 addCategory(categories[i]); 1692 } 1693 } 1694 if (mimeTypes != null) { 1695 for (int i = 0; i < mimeTypes.length; i++) { 1696 try { 1697 addDataType(mimeTypes[i]); 1698 } catch (IntentFilter.MalformedMimeTypeException e) { 1699 throw new RuntimeException("Bad mime type", e); 1700 } 1701 } 1702 } 1703 if (schemes != null) { 1704 for (int i = 0; i < schemes.length; i++) { 1705 addDataScheme(schemes[i]); 1706 } 1707 } 1708 if (authorities != null) { 1709 for (int i = 0; i < authorities.length; i++) { 1710 addDataAuthority(authorities[i], ports != null ? ports[i] : null); 1711 } 1712 } 1713 } 1714 Match(String[] actions, String[] categories, String[] mimeTypes, String[] schemes, String[] authorities, String[] ports, String[] paths, int[] pathTypes)1715 Match(String[] actions, String[] categories, String[] mimeTypes, String[] schemes, 1716 String[] authorities, String[] ports, String[] paths, int[] pathTypes) { 1717 this(actions, categories, mimeTypes, schemes, authorities, ports); 1718 if (paths != null) { 1719 for (int i = 0; i < paths.length; i++) { 1720 addDataPath(paths[i], pathTypes[i]); 1721 } 1722 } 1723 } 1724 Match(String[] actions, String[] categories, String[] mimeTypes, String[] schemes, String[] authorities, String[] ports, String[] paths, int[] pathTypes, String[] ssps, int[] sspTypes)1725 Match(String[] actions, String[] categories, String[] mimeTypes, String[] schemes, 1726 String[] authorities, String[] ports, String[] paths, int[] pathTypes, 1727 String[] ssps, int[] sspTypes) { 1728 this(actions, categories, mimeTypes, schemes, authorities, ports, paths, pathTypes); 1729 if (ssps != null) { 1730 for (int i = 0; i < ssps.length; i++) { 1731 addDataSchemeSpecificPart(ssps[i], sspTypes[i]); 1732 } 1733 } 1734 } 1735 addDynamicMimeTypes(String[] dynamicMimeTypes)1736 Match addDynamicMimeTypes(String[] dynamicMimeTypes) { 1737 for (int i = 0; i < dynamicMimeTypes.length; i++) { 1738 try { 1739 addDynamicDataType(dynamicMimeTypes[i]); 1740 } catch (IntentFilter.MalformedMimeTypeException e) { 1741 throw new RuntimeException("Bad mime type", e); 1742 } 1743 } 1744 return this; 1745 } 1746 addMimeGroups(String[] mimeGroups)1747 Match addMimeGroups(String[] mimeGroups) { 1748 for (int i = 0; i < mimeGroups.length; i++) { 1749 addMimeGroup(mimeGroups[i]); 1750 } 1751 return this; 1752 } 1753 addMimeTypes(String[] mimeTypes)1754 Match addMimeTypes(String[] mimeTypes) { 1755 for (int i = 0; i < mimeTypes.length; i++) { 1756 try { 1757 addDataType(mimeTypes[i]); 1758 } catch (IntentFilter.MalformedMimeTypeException e) { 1759 throw new RuntimeException("Bad mime type", e); 1760 } 1761 } 1762 return this; 1763 } 1764 addActions(String[] actions)1765 Match addActions(String[] actions) { 1766 for (int i = 0; i < actions.length; i++) { 1767 addAction(actions[i]); 1768 } 1769 return this; 1770 } 1771 } 1772 1773 private static class MatchCondition { 1774 public final int result; 1775 public final String action; 1776 public final String mimeType; 1777 public final Uri data; 1778 public final String[] categories; 1779 public final boolean wildcardSupported; 1780 public final Collection<String> ignoredActions; 1781 data(int result, String data)1782 public static MatchCondition data(int result, String data) { 1783 return new MatchCondition(result, null, null, null, data); 1784 } data(int result, String data, boolean wildcardSupported)1785 public static MatchCondition data(int result, String data, boolean wildcardSupported) { 1786 return new MatchCondition(result, null, null, null, data, wildcardSupported, null); 1787 } data(int result, String data, boolean wildcardSupported, Collection<String> ignoredActions)1788 public static MatchCondition data(int result, String data, boolean wildcardSupported, 1789 Collection<String> ignoredActions) { 1790 return new MatchCondition(result, null, null, null, data, wildcardSupported, 1791 ignoredActions); 1792 } MatchCondition(int result, String action, String[] categories, String mimeType, String data)1793 MatchCondition(int result, String action, String[] categories, String mimeType, 1794 String data) { 1795 this(result, action, categories, mimeType, data, false, null); 1796 } MatchCondition(int result, String action, String[] categories, String mimeType, String data, boolean wildcardSupported)1797 MatchCondition(int result, String action, String[] categories, String mimeType, 1798 String data, boolean wildcardSupported) { 1799 this(result, action, categories, mimeType, data, wildcardSupported, null); 1800 } MatchCondition(int result, String action, String[] categories, String mimeType, String data, boolean wildcardSupported, Collection<String> ignoredActions)1801 MatchCondition(int result, String action, String[] categories, String mimeType, 1802 String data, boolean wildcardSupported, Collection<String> ignoredActions) { 1803 this.result = result; 1804 this.action = action; 1805 this.mimeType = mimeType; 1806 this.data = data != null ? Uri.parse(data) : null; 1807 this.categories = categories; 1808 this.wildcardSupported = wildcardSupported; 1809 this.ignoredActions = ignoredActions; 1810 } 1811 } 1812 checkMatches(IntentFilter[] filters, MatchCondition... results)1813 private static void checkMatches(IntentFilter[] filters, MatchCondition... results) { 1814 for (IntentFilter filter : filters) { 1815 checkMatches(filter, results); 1816 } 1817 } 1818 checkMatches(IntentFilter filter, MatchCondition... results)1819 private static void checkMatches(IntentFilter filter, MatchCondition... results) { 1820 for (int i = 0; i < results.length; i++) { 1821 MatchCondition mc = results[i]; 1822 HashSet<String> categories = null; 1823 if (mc.categories != null) { 1824 for (int j = 0; j < mc.categories.length; j++) { 1825 if (categories == null) { 1826 categories = new HashSet<String>(); 1827 } 1828 categories.add(mc.categories[j]); 1829 } 1830 } 1831 int result = filter.match(mc.action, mc.mimeType, mc.data != null ? mc.data.getScheme() 1832 : null, mc.data, categories, "test", mc.wildcardSupported, mc.ignoredActions); 1833 if ((result & IntentFilter.MATCH_CATEGORY_MASK) != 1834 (mc.result & IntentFilter.MATCH_CATEGORY_MASK)) { 1835 StringBuilder msg = new StringBuilder(); 1836 msg.append("Error matching against IntentFilter:\n"); 1837 filter.dump(new StringBuilderPrinter(msg), " "); 1838 msg.append("Match action: "); 1839 msg.append(mc.action); 1840 msg.append("\nMatch mimeType: "); 1841 msg.append(mc.mimeType); 1842 msg.append("\nMatch data: "); 1843 msg.append(mc.data); 1844 msg.append("\nMatch categories: "); 1845 if (mc.categories != null) { 1846 for (int j = 0; j < mc.categories.length; j++) { 1847 if (j > 0) { 1848 msg.append(", "); 1849 } 1850 msg.append(mc.categories[j]); 1851 } 1852 } 1853 msg.append("\nExpected result: 0x"); 1854 msg.append(Integer.toHexString(mc.result)); 1855 msg.append(", got result: 0x"); 1856 msg.append(Integer.toHexString(result)); 1857 throw new RuntimeException(msg.toString()); 1858 } 1859 } 1860 } 1861 1862 @Test testPaths()1863 public void testPaths() throws Exception { 1864 IntentFilter filter = new Match(null, null, null, 1865 new String[]{"scheme"}, new String[]{"authority"}, null, 1866 new String[]{"/literal1", "/2literal"}, 1867 new int[]{PATTERN_LITERAL, PATTERN_LITERAL}); 1868 checkMatches(filter, 1869 MatchCondition.data( 1870 IntentFilter.NO_MATCH_DATA, null), 1871 MatchCondition.data( 1872 IntentFilter.MATCH_CATEGORY_PATH, "scheme://authority/literal1"), 1873 MatchCondition.data( 1874 IntentFilter.MATCH_CATEGORY_PATH, "scheme://authority/2literal"), 1875 MatchCondition.data( 1876 IntentFilter.NO_MATCH_DATA, "scheme://authority/literal"), 1877 MatchCondition.data( 1878 IntentFilter.NO_MATCH_DATA, "scheme://authority/literal12")); 1879 filter = new Match(null, null, null, 1880 new String[]{"scheme"}, new String[]{"authority"}, null, 1881 new String[]{"/literal1", "/2literal"}, new int[]{PATTERN_PREFIX, PATTERN_PREFIX}); 1882 checkMatches(filter, 1883 MatchCondition.data( 1884 IntentFilter.NO_MATCH_DATA, null), 1885 MatchCondition.data( 1886 IntentFilter.MATCH_CATEGORY_PATH, "scheme://authority/literal1"), 1887 MatchCondition.data( 1888 IntentFilter.MATCH_CATEGORY_PATH, "scheme://authority/2literal"), 1889 MatchCondition.data( 1890 IntentFilter.NO_MATCH_DATA, "scheme://authority/literal"), 1891 MatchCondition.data( 1892 IntentFilter.MATCH_CATEGORY_PATH, "scheme://authority/literal12")); 1893 filter = new Match(null, null, null, 1894 new String[]{"scheme"}, new String[]{"authority"}, null, 1895 new String[]{"literal1", "2literal"}, new int[]{PATTERN_SUFFIX, PATTERN_SUFFIX}); 1896 checkMatches(filter, 1897 MatchCondition.data( 1898 IntentFilter.NO_MATCH_DATA, null), 1899 MatchCondition.data( 1900 IntentFilter.MATCH_CATEGORY_PATH, "scheme://authority/aliteral1"), 1901 MatchCondition.data( 1902 IntentFilter.MATCH_CATEGORY_PATH, "scheme://authority/2literal"), 1903 MatchCondition.data( 1904 IntentFilter.NO_MATCH_DATA, "scheme://authority/literal"), 1905 MatchCondition.data( 1906 IntentFilter.MATCH_CATEGORY_PATH, "scheme://authority/literal1"), 1907 MatchCondition.data( 1908 IntentFilter.MATCH_CATEGORY_PATH, "scheme://authority/2literal1"), 1909 MatchCondition.data( 1910 IntentFilter.NO_MATCH_DATA, "scheme://authority/literal1a")); 1911 filter = new Match(null, null, null, 1912 new String[]{"scheme"}, new String[]{"authority"}, null, new String[]{"/.*"}, 1913 new int[]{PATTERN_SIMPLE_GLOB}); 1914 checkMatches(filter, 1915 MatchCondition.data( 1916 IntentFilter.NO_MATCH_DATA, null), 1917 MatchCondition.data( 1918 IntentFilter.MATCH_CATEGORY_PATH, "scheme://authority/literal1"), 1919 MatchCondition.data( 1920 IntentFilter.MATCH_CATEGORY_PATH, "scheme://authority/"), 1921 MatchCondition.data( 1922 IntentFilter.NO_MATCH_DATA, "scheme://authority")); 1923 filter = new Match(null, null, null, 1924 new String[]{"scheme"}, new String[]{"authority"}, null, new String[]{".*"}, 1925 new int[]{PATTERN_SIMPLE_GLOB}); 1926 checkMatches(filter, 1927 MatchCondition.data( 1928 IntentFilter.NO_MATCH_DATA, null), 1929 MatchCondition.data( 1930 IntentFilter.MATCH_CATEGORY_PATH, "scheme://authority/literal1"), 1931 MatchCondition.data( 1932 IntentFilter.MATCH_CATEGORY_PATH, "scheme://authority/"), 1933 MatchCondition.data( 1934 IntentFilter.MATCH_CATEGORY_PATH, "scheme://authority")); 1935 filter = new Match(null, null, null, 1936 new String[]{"scheme"}, new String[]{"authority"}, null, new String[]{"/a1*b"}, 1937 new int[]{PATTERN_SIMPLE_GLOB}); 1938 checkMatches(filter, 1939 MatchCondition.data( 1940 IntentFilter.NO_MATCH_DATA, null), 1941 MatchCondition.data( 1942 IntentFilter.MATCH_CATEGORY_PATH, "scheme://authority/ab"), 1943 MatchCondition.data( 1944 IntentFilter.MATCH_CATEGORY_PATH, "scheme://authority/a1b"), 1945 MatchCondition.data( 1946 IntentFilter.MATCH_CATEGORY_PATH, "scheme://authority/a11b"), 1947 MatchCondition.data( 1948 IntentFilter.NO_MATCH_DATA, "scheme://authority/a2b"), 1949 MatchCondition.data( 1950 IntentFilter.NO_MATCH_DATA, "scheme://authority/a1bc"), 1951 MatchCondition.data( 1952 IntentFilter.NO_MATCH_DATA, "scheme://authority/")); 1953 filter = new Match(null, null, null, 1954 new String[]{"scheme"}, new String[]{"authority"}, null, new String[]{"/a1*"}, 1955 new int[]{PATTERN_SIMPLE_GLOB}); 1956 checkMatches(filter, 1957 MatchCondition.data( 1958 IntentFilter.NO_MATCH_DATA, null), 1959 MatchCondition.data( 1960 IntentFilter.MATCH_CATEGORY_PATH, "scheme://authority/a1"), 1961 MatchCondition.data( 1962 IntentFilter.NO_MATCH_DATA, "scheme://authority/ab"), 1963 MatchCondition.data( 1964 IntentFilter.MATCH_CATEGORY_PATH, "scheme://authority/a11"), 1965 MatchCondition.data( 1966 IntentFilter.NO_MATCH_DATA, "scheme://authority/a1b"), 1967 MatchCondition.data( 1968 IntentFilter.MATCH_CATEGORY_PATH, "scheme://authority/a11"), 1969 MatchCondition.data( 1970 IntentFilter.NO_MATCH_DATA, "scheme://authority/a2")); 1971 filter = new Match(null, null, null, 1972 new String[]{"scheme"}, new String[]{"authority"}, null, new String[]{"/a\\.*b"}, 1973 new int[]{PATTERN_SIMPLE_GLOB}); 1974 checkMatches(filter, 1975 MatchCondition.data( 1976 IntentFilter.NO_MATCH_DATA, null), 1977 MatchCondition.data( 1978 IntentFilter.MATCH_CATEGORY_PATH, "scheme://authority/ab"), 1979 MatchCondition.data( 1980 IntentFilter.MATCH_CATEGORY_PATH, "scheme://authority/a.b"), 1981 MatchCondition.data( 1982 IntentFilter.MATCH_CATEGORY_PATH, "scheme://authority/a..b"), 1983 MatchCondition.data( 1984 IntentFilter.NO_MATCH_DATA, "scheme://authority/a2b"), 1985 MatchCondition.data( 1986 IntentFilter.NO_MATCH_DATA, "scheme://authority/a.bc"), 1987 MatchCondition.data( 1988 IntentFilter.NO_MATCH_DATA, "scheme://authority/")); 1989 filter = new Match(null, null, null, 1990 new String[]{"scheme"}, new String[]{"authority"}, null, new String[]{"/a.*b"}, 1991 new int[]{PATTERN_SIMPLE_GLOB}); 1992 checkMatches(filter, 1993 MatchCondition.data( 1994 IntentFilter.NO_MATCH_DATA, null), 1995 MatchCondition.data( 1996 IntentFilter.MATCH_CATEGORY_PATH, "scheme://authority/ab"), 1997 MatchCondition.data( 1998 IntentFilter.MATCH_CATEGORY_PATH, "scheme://authority/a.b"), 1999 MatchCondition.data( 2000 IntentFilter.MATCH_CATEGORY_PATH, "scheme://authority/a.1b"), 2001 MatchCondition.data( 2002 IntentFilter.MATCH_CATEGORY_PATH, "scheme://authority/a2b"), 2003 MatchCondition.data( 2004 IntentFilter.NO_MATCH_DATA, "scheme://authority/a.bc"), 2005 MatchCondition.data( 2006 IntentFilter.NO_MATCH_DATA, "scheme://authority/")); 2007 filter = new Match(null, null, null, 2008 new String[]{"scheme"}, new String[]{"authority"}, null, new String[]{"/a.*"}, 2009 new int[]{PATTERN_SIMPLE_GLOB}); 2010 checkMatches(filter, 2011 MatchCondition.data( 2012 IntentFilter.NO_MATCH_DATA, null), 2013 MatchCondition.data( 2014 IntentFilter.MATCH_CATEGORY_PATH, "scheme://authority/ab"), 2015 MatchCondition.data( 2016 IntentFilter.MATCH_CATEGORY_PATH, "scheme://authority/a.b"), 2017 MatchCondition.data( 2018 IntentFilter.MATCH_CATEGORY_PATH, "scheme://authority/a.1b"), 2019 MatchCondition.data( 2020 IntentFilter.MATCH_CATEGORY_PATH, "scheme://authority/a2b"), 2021 MatchCondition.data( 2022 IntentFilter.MATCH_CATEGORY_PATH, "scheme://authority/a.bc"), 2023 MatchCondition.data( 2024 IntentFilter.NO_MATCH_DATA, "scheme://authority/")); 2025 filter = new Match(null, null, null, 2026 new String[]{"scheme"}, new String[]{"authority"}, null, new String[]{"/a.\\*b"}, 2027 new int[]{PATTERN_SIMPLE_GLOB}); 2028 checkMatches(filter, 2029 MatchCondition.data( 2030 IntentFilter.NO_MATCH_DATA, null), 2031 MatchCondition.data( 2032 IntentFilter.NO_MATCH_DATA, "scheme://authority/ab"), 2033 MatchCondition.data( 2034 IntentFilter.MATCH_CATEGORY_PATH, "scheme://authority/a.*b"), 2035 MatchCondition.data( 2036 IntentFilter.MATCH_CATEGORY_PATH, "scheme://authority/a1*b"), 2037 MatchCondition.data( 2038 IntentFilter.NO_MATCH_DATA, "scheme://authority/a2b"), 2039 MatchCondition.data( 2040 IntentFilter.NO_MATCH_DATA, "scheme://authority/a.bc"), 2041 MatchCondition.data( 2042 IntentFilter.NO_MATCH_DATA, "scheme://authority/")); 2043 filter = new Match(null, null, null, 2044 new String[]{"scheme"}, new String[]{"authority"}, null, new String[]{"/a.\\*"}, 2045 new int[]{PATTERN_SIMPLE_GLOB}); 2046 checkMatches(filter, 2047 MatchCondition.data( 2048 IntentFilter.NO_MATCH_DATA, null), 2049 MatchCondition.data( 2050 IntentFilter.NO_MATCH_DATA, "scheme://authority/ab"), 2051 MatchCondition.data( 2052 IntentFilter.MATCH_CATEGORY_PATH, "scheme://authority/a.*"), 2053 MatchCondition.data( 2054 IntentFilter.MATCH_CATEGORY_PATH, "scheme://authority/a1*"), 2055 MatchCondition.data( 2056 IntentFilter.NO_MATCH_DATA, "scheme://authority/a1b")); 2057 } 2058 2059 @Test testDump()2060 public void testDump() throws MalformedMimeTypeException { 2061 TestPrinter printer = new TestPrinter(); 2062 String prefix = "TestIntentFilter"; 2063 IntentFilter filter = new IntentFilter(ACTION, DATA_STATIC_TYPE); 2064 filter.dump(printer, prefix); 2065 assertTrue(printer.isPrintlnCalled); 2066 } 2067 2068 private class TestPrinter implements Printer { 2069 public boolean isPrintlnCalled; 2070 println(String x)2071 public void println(String x) { 2072 isPrintlnCalled = true; 2073 } 2074 } 2075 2076 @Test testAsPredicate()2077 public void testAsPredicate() throws Exception { 2078 final Predicate<Intent> pred = new IntentFilter(ACTION).asPredicate(); 2079 2080 assertTrue(pred.test(new Intent(ACTION))); 2081 assertFalse(pred.test(new Intent(CATEGORY))); 2082 } 2083 2084 @Test 2085 @IgnoreUnderRavenwood(blockedBy = ContentResolver.class) testAsPredicateWithTypeResolution()2086 public void testAsPredicateWithTypeResolution() throws Exception { 2087 final ContentResolver resolver = mContext.getContentResolver(); 2088 final Predicate<Intent> pred = new IntentFilter(ACTION, DATA_STATIC_TYPE) 2089 .asPredicateWithTypeResolution(resolver); 2090 2091 assertTrue(pred.test(new Intent(ACTION).setDataAndType(URI, DATA_STATIC_TYPE))); 2092 assertFalse(pred.test(new Intent(ACTION).setDataAndType(URI, DATA_DYNAMIC_TYPE))); 2093 } 2094 } 2095