FileDocCategorySizeDatePackage
IntentFilterTest.javaAPI DocAndroid 1.5 API29039Wed May 06 22:42:02 BST 2009com.android.unit_tests.content

IntentFilterTest

public class IntentFilterTest extends TestCase

Fields Summary
Constructors Summary
Methods Summary
public static voidcheckMatches(android.content.IntentFilter filter, com.android.unit_tests.content.IntentFilterTest$MatchCondition[] results)

        for (int i = 0; i < results.length; i++) {
            MatchCondition mc = results[i];
            HashSet<String> categories = null;
            if (mc.categories != null) {
                for (int j = 0; j < mc.categories.length; j++) {
                    if (categories == null) {
                        categories = new HashSet<String>();
                    }
                    categories.add(mc.categories[j]);
                }
            }
            int result = filter.match(mc.action, mc.mimeType,
                    mc.data != null ? mc.data.getScheme() : null, mc.data,
                    categories, "test");
            if ( (result & IntentFilter.MATCH_CATEGORY_MASK)
                    != (mc.result & IntentFilter.MATCH_CATEGORY_MASK) ) {
                StringBuilder msg = new StringBuilder();
                msg.append("Error matching against IntentFilter:\n");
                filter.dump(new StringBuilderPrinter(msg), "    ");
                msg.append("Match action: ");
                msg.append(mc.action);
                msg.append("\nMatch mimeType: ");
                msg.append(mc.mimeType);
                msg.append("\nMatch data: ");
                msg.append(mc.data);
                msg.append("\nMatch categories: ");
                if (mc.categories != null) {
                    for (int j = 0; j < mc.categories.length; j++) {
                        if (j > 0) msg.append(", ");
                        msg.append(mc.categories[j]);
                    }
                }
                msg.append("\nExpected result: 0x");
                msg.append(Integer.toHexString(mc.result));
                msg.append(", got result: 0x");
                msg.append(Integer.toHexString(result));
                throw new RuntimeException(msg.toString());
            }
        }
    
public voidtestActions()

        IntentFilter filter = new Match(
                new String[]{"action1"}, null, null, null, null, null);
        checkMatches(filter, new MatchCondition[]{
                new MatchCondition(IntentFilter.MATCH_CATEGORY_EMPTY, null,
                        null, null, null),
                new MatchCondition(IntentFilter.MATCH_CATEGORY_EMPTY, "action1",
                        null, null, null),
                new MatchCondition(IntentFilter.NO_MATCH_ACTION, "action2",
                        null, null, null),
        });

        filter = new Match(
                new String[]{"action1", "action2"},
                null, null, null, null, null);
        checkMatches(filter, new MatchCondition[]{
                new MatchCondition(IntentFilter.MATCH_CATEGORY_EMPTY, null,
                        null, null, null),
                new MatchCondition(IntentFilter.MATCH_CATEGORY_EMPTY, "action1",
                        null, null, null),
                new MatchCondition(IntentFilter.MATCH_CATEGORY_EMPTY, "action2",
                        null, null, null),
                new MatchCondition(IntentFilter.NO_MATCH_ACTION, "action3",
                        null, null, null),
        });
    
public voidtestAuthorities()

        IntentFilter filter = new Match(null, null, null,
                new String[]{"scheme1"},
                new String[]{"authority1"}, new String[]{null});
        checkMatches(filter, new MatchCondition[]{
                new MatchCondition(IntentFilter.NO_MATCH_DATA, null,
                        null, null, null),
                new MatchCondition(IntentFilter.NO_MATCH_DATA, null,
                        null, null, "scheme1:foo"),
                new MatchCondition(IntentFilter.MATCH_CATEGORY_HOST, null,
                        null, null, "scheme1://authority1/"),
                new MatchCondition(IntentFilter.NO_MATCH_DATA, null,
                        null, null, "scheme1://authority2/"),
                new MatchCondition(IntentFilter.MATCH_CATEGORY_HOST, null,
                        null, null, "scheme1://authority1:100/"),
        });

        filter = new Match(null, null, null, new String[]{"scheme1"},
                new String[]{"authority1"}, new String[]{"100"});
        checkMatches(filter, new MatchCondition[]{
                new MatchCondition(IntentFilter.NO_MATCH_DATA, null,
                        null, null, null),
                new MatchCondition(IntentFilter.NO_MATCH_DATA, null,
                        null, null, "scheme1:foo"),
                new MatchCondition(IntentFilter.NO_MATCH_DATA, null,
                        null, null, "scheme1://authority1/"),
                new MatchCondition(IntentFilter.NO_MATCH_DATA, null,
                        null, null, "scheme1://authority2/"),
                new MatchCondition(IntentFilter.MATCH_CATEGORY_PORT, null,
                        null, null, "scheme1://authority1:100/"),
                new MatchCondition(IntentFilter.NO_MATCH_DATA, null,
                        null, null, "scheme1://authority1:200/"),
        });

        filter = new Match(null, null, null, new String[]{"scheme1"},
                new String[]{"authority1", "authority2"},
                new String[]{"100", null});
        checkMatches(filter, new MatchCondition[]{
                new MatchCondition(IntentFilter.NO_MATCH_DATA, null,
                        null, null, null),
                new MatchCondition(IntentFilter.NO_MATCH_DATA, null,
                        null, null, "scheme1:foo"),
                new MatchCondition(IntentFilter.NO_MATCH_DATA, null,
                        null, null, "scheme1://authority1/"),
                new MatchCondition(IntentFilter.MATCH_CATEGORY_HOST, null,
                        null, null, "scheme1://authority2/"),
                new MatchCondition(IntentFilter.MATCH_CATEGORY_PORT, null,
                        null, null, "scheme1://authority1:100/"),
                new MatchCondition(IntentFilter.NO_MATCH_DATA, null,
                        null, null, "scheme1://authority1:200/"),
        });
    
public voidtestCategories()

        IntentFilter filter = new Match(
                null, new String[]{"category1"}, null, null, null, null);
        checkMatches(filter, new MatchCondition[]{
                new MatchCondition(IntentFilter.MATCH_CATEGORY_EMPTY, null,
                        null, null, null),
                new MatchCondition(IntentFilter.MATCH_CATEGORY_EMPTY, null,
                        new String[]{"category1"}, null, null),
                new MatchCondition(IntentFilter.NO_MATCH_CATEGORY, null,
                        new String[]{"category2"}, null, null),
                new MatchCondition(IntentFilter.NO_MATCH_CATEGORY, null,
                        new String[]{"category1", "category2"}, null, null),
        });

        filter = new Match(
                null, new String[]{"category1", "category2"}, null, null,
                null, null);
        checkMatches(filter, new MatchCondition[]{
                new MatchCondition(IntentFilter.MATCH_CATEGORY_EMPTY, null,
                        null, null, null),
                new MatchCondition(IntentFilter.MATCH_CATEGORY_EMPTY, null,
                        new String[]{"category1"}, null, null),
                new MatchCondition(IntentFilter.MATCH_CATEGORY_EMPTY, null,
                        new String[]{"category2"}, null, null),
                new MatchCondition(IntentFilter.MATCH_CATEGORY_EMPTY, null,
                        new String[]{"category1", "category2"}, null, null),
                new MatchCondition(IntentFilter.NO_MATCH_CATEGORY, null,
                        new String[]{"category3"}, null, null),
                new MatchCondition(IntentFilter.NO_MATCH_CATEGORY, null,
                        new String[]{"category1", "category2", "category3"},
                        null, null),
        });
    
public voidtestMimeTypes()

        IntentFilter filter = new Match(
                null, null, new String[]{"which1/what1"}, null, null, null);
        checkMatches(filter, new MatchCondition[]{
                new MatchCondition(IntentFilter.NO_MATCH_TYPE, null,
                        null, null, null),
                new MatchCondition(IntentFilter.MATCH_CATEGORY_TYPE, null, null,
                        "which1/what1", null),
                new MatchCondition(IntentFilter.MATCH_CATEGORY_TYPE, null, null,
                        "which1/*", null),
                new MatchCondition(IntentFilter.MATCH_CATEGORY_TYPE, null, null,
                        "*/*", null),
                new MatchCondition(IntentFilter.NO_MATCH_TYPE, null, null,
                        "which2/what2", null),
                new MatchCondition(IntentFilter.NO_MATCH_TYPE, null, null,
                        "which2/*", null),
                new MatchCondition(IntentFilter.NO_MATCH_TYPE, null, null,
                        "which1/what2", null),
        });

        filter = new Match(null, null,
                new String[]{"which1/what1", "which2/what2"}, null, null,
                null);
        checkMatches(filter, new MatchCondition[]{
                new MatchCondition(IntentFilter.NO_MATCH_TYPE, null,
                        null, null, null),
                new MatchCondition(IntentFilter.MATCH_CATEGORY_TYPE, null, null,
                        "which1/what1", null),
                new MatchCondition(IntentFilter.MATCH_CATEGORY_TYPE, null, null,
                        "which1/*", null),
                new MatchCondition(IntentFilter.MATCH_CATEGORY_TYPE, null, null,
                        "*/*", null),
                new MatchCondition(IntentFilter.MATCH_CATEGORY_TYPE, null, null,
                        "which2/what2", null),
                new MatchCondition(IntentFilter.MATCH_CATEGORY_TYPE, null, null,
                        "which2/*", null),
                new MatchCondition(IntentFilter.NO_MATCH_TYPE, null, null,
                        "which1/what2", null),
                new MatchCondition(IntentFilter.NO_MATCH_TYPE, null, null,
                        "which3/what3", null),
        });

        filter = new Match(null, null,
                new String[]{"which1/*"}, null, null, null);
        checkMatches(filter, new MatchCondition[]{
                new MatchCondition(IntentFilter.NO_MATCH_TYPE, null,
                        null, null, null),
                new MatchCondition(IntentFilter.MATCH_CATEGORY_TYPE, null, null,
                        "which1/what1", null),
                new MatchCondition(IntentFilter.MATCH_CATEGORY_TYPE, null, null,
                        "which1/*", null),
                new MatchCondition(IntentFilter.MATCH_CATEGORY_TYPE, null, null,
                        "*/*", null),
                new MatchCondition(IntentFilter.NO_MATCH_TYPE, null, null,
                        "which2/what2", null),
                new MatchCondition(IntentFilter.NO_MATCH_TYPE, null, null,
                        "which2/*", null),
                new MatchCondition(IntentFilter.MATCH_CATEGORY_TYPE, null, null,
                        "which1/what2", null),
                new MatchCondition(IntentFilter.NO_MATCH_TYPE, null, null,
                        "which3/what3", null),
        });

        filter = new Match(null, null,
                new String[]{"*/*"}, null, null, null);
        checkMatches(filter, new MatchCondition[]{
                new MatchCondition(IntentFilter.NO_MATCH_TYPE, null,
                        null, null, null),
                new MatchCondition(IntentFilter.MATCH_CATEGORY_TYPE, null, null,
                        "which1/what1", null),
                new MatchCondition(IntentFilter.MATCH_CATEGORY_TYPE, null, null,
                        "which1/*", null),
                new MatchCondition(IntentFilter.MATCH_CATEGORY_TYPE, null, null,
                        "*/*", null),
                new MatchCondition(IntentFilter.MATCH_CATEGORY_TYPE, null, null,
                        "which2/what2", null),
                new MatchCondition(IntentFilter.MATCH_CATEGORY_TYPE, null, null,
                        "which2/*", null),
                new MatchCondition(IntentFilter.MATCH_CATEGORY_TYPE, null, null,
                        "which1/what2", null),
                new MatchCondition(IntentFilter.MATCH_CATEGORY_TYPE, null, null,
                        "which3/what3", null),
        });
    
public voidtestPaths()

        IntentFilter filter = new Match(null, null, null,
                new String[]{"scheme"}, new String[]{"authority"}, null,
                new String[]{"/literal1", "/2literal"},
                new int[]{PATTERN_LITERAL, PATTERN_LITERAL});
        checkMatches(filter, new MatchCondition[]{
                new MatchCondition(IntentFilter.NO_MATCH_DATA, null,
                        null, null, null),
                new MatchCondition(IntentFilter.MATCH_CATEGORY_PATH, null,
                        null, null, "scheme://authority/literal1"),
                new MatchCondition(IntentFilter.MATCH_CATEGORY_PATH, null,
                        null, null, "scheme://authority/2literal"),
                new MatchCondition(IntentFilter.NO_MATCH_DATA, null,
                        null, null, "scheme://authority/literal"),
                new MatchCondition(IntentFilter.NO_MATCH_DATA, null,
                        null, null, "scheme://authority/literal12"),
        });
        filter = new Match(null, null, null,
                new String[]{"scheme"}, new String[]{"authority"}, null,
                new String[]{"/literal1", "/2literal"},
                new int[]{PATTERN_PREFIX, PATTERN_PREFIX});
        checkMatches(filter, new MatchCondition[]{
                new MatchCondition(IntentFilter.NO_MATCH_DATA, null,
                        null, null, null),
                new MatchCondition(IntentFilter.MATCH_CATEGORY_PATH, null,
                        null, null, "scheme://authority/literal1"),
                new MatchCondition(IntentFilter.MATCH_CATEGORY_PATH, null,
                        null, null, "scheme://authority/2literal"),
                new MatchCondition(IntentFilter.NO_MATCH_DATA, null,
                        null, null, "scheme://authority/literal"),
                new MatchCondition(IntentFilter.MATCH_CATEGORY_PATH, null,
                        null, null, "scheme://authority/literal12"),
        });
        filter = new Match(null, null, null,
                new String[]{"scheme"}, new String[]{"authority"}, null,
                new String[]{"/.*"},
                new int[]{PATTERN_SIMPLE_GLOB});
        checkMatches(filter, new MatchCondition[]{
                new MatchCondition(IntentFilter.NO_MATCH_DATA, null,
                        null, null, null),
                new MatchCondition(IntentFilter.MATCH_CATEGORY_PATH, null,
                        null, null, "scheme://authority/literal1"),
                new MatchCondition(IntentFilter.MATCH_CATEGORY_PATH, null,
                        null, null, "scheme://authority/"),
                new MatchCondition(IntentFilter.NO_MATCH_DATA, null,
                        null, null, "scheme://authority"),
        });
        filter = new Match(null, null, null,
                new String[]{"scheme"}, new String[]{"authority"}, null,
                new String[]{".*"},
                new int[]{PATTERN_SIMPLE_GLOB});
        checkMatches(filter, new MatchCondition[]{
                new MatchCondition(IntentFilter.NO_MATCH_DATA, null,
                        null, null, null),
                new MatchCondition(IntentFilter.MATCH_CATEGORY_PATH, null,
                        null, null, "scheme://authority/literal1"),
                new MatchCondition(IntentFilter.MATCH_CATEGORY_PATH, null,
                        null, null, "scheme://authority/"),
                new MatchCondition(IntentFilter.MATCH_CATEGORY_PATH, null,
                        null, null, "scheme://authority"),
        });
        filter = new Match(null, null, null,
                new String[]{"scheme"}, new String[]{"authority"}, null,
                new String[]{"/a1*b"},
                new int[]{PATTERN_SIMPLE_GLOB});
        checkMatches(filter, new MatchCondition[]{
                new MatchCondition(IntentFilter.NO_MATCH_DATA, null,
                        null, null, null),
                new MatchCondition(IntentFilter.MATCH_CATEGORY_PATH, null,
                        null, null, "scheme://authority/ab"),
                new MatchCondition(IntentFilter.MATCH_CATEGORY_PATH, null,
                        null, null, "scheme://authority/a1b"),
                new MatchCondition(IntentFilter.MATCH_CATEGORY_PATH, null,
                        null, null, "scheme://authority/a11b"),
                new MatchCondition(IntentFilter.NO_MATCH_DATA, null,
                        null, null, "scheme://authority/a2b"),
                new MatchCondition(IntentFilter.NO_MATCH_DATA, null,
                        null, null, "scheme://authority/a1bc"),
                new MatchCondition(IntentFilter.NO_MATCH_DATA, null,
                        null, null, "scheme://authority/"),
        });
        filter = new Match(null, null, null,
                new String[]{"scheme"}, new String[]{"authority"}, null,
                new String[]{"/a1*"},
                new int[]{PATTERN_SIMPLE_GLOB});
        checkMatches(filter, new MatchCondition[]{
                new MatchCondition(IntentFilter.NO_MATCH_DATA, null,
                        null, null, null),
                new MatchCondition(IntentFilter.MATCH_CATEGORY_PATH, null,
                        null, null, "scheme://authority/a1"),
                new MatchCondition(IntentFilter.NO_MATCH_DATA, null,
                        null, null, "scheme://authority/ab"),
                new MatchCondition(IntentFilter.MATCH_CATEGORY_PATH, null,
                        null, null, "scheme://authority/a11"),
                new MatchCondition(IntentFilter.NO_MATCH_DATA, null,
                        null, null, "scheme://authority/a1b"),
                new MatchCondition(IntentFilter.MATCH_CATEGORY_PATH, null,
                        null, null, "scheme://authority/a11"),
                new MatchCondition(IntentFilter.NO_MATCH_DATA, null,
                        null, null, "scheme://authority/a2"),
        });
        filter = new Match(null, null, null,
                new String[]{"scheme"}, new String[]{"authority"}, null,
                new String[]{"/a\\.*b"},
                new int[]{PATTERN_SIMPLE_GLOB});
        checkMatches(filter, new MatchCondition[]{
                new MatchCondition(IntentFilter.NO_MATCH_DATA, null,
                        null, null, null),
                new MatchCondition(IntentFilter.MATCH_CATEGORY_PATH, null,
                        null, null, "scheme://authority/ab"),
                new MatchCondition(IntentFilter.MATCH_CATEGORY_PATH, null,
                        null, null, "scheme://authority/a.b"),
                new MatchCondition(IntentFilter.MATCH_CATEGORY_PATH, null,
                        null, null, "scheme://authority/a..b"),
                new MatchCondition(IntentFilter.NO_MATCH_DATA, null,
                        null, null, "scheme://authority/a2b"),
                new MatchCondition(IntentFilter.NO_MATCH_DATA, null,
                        null, null, "scheme://authority/a.bc"),
                new MatchCondition(IntentFilter.NO_MATCH_DATA, null,
                        null, null, "scheme://authority/"),
        });
        filter = new Match(null, null, null,
                new String[]{"scheme"}, new String[]{"authority"}, null,
                new String[]{"/a.*b"},
                new int[]{PATTERN_SIMPLE_GLOB});
        checkMatches(filter, new MatchCondition[]{
                new MatchCondition(IntentFilter.NO_MATCH_DATA, null,
                        null, null, null),
                new MatchCondition(IntentFilter.MATCH_CATEGORY_PATH, null,
                        null, null, "scheme://authority/ab"),
                new MatchCondition(IntentFilter.MATCH_CATEGORY_PATH, null,
                        null, null, "scheme://authority/a.b"),
                new MatchCondition(IntentFilter.MATCH_CATEGORY_PATH, null,
                        null, null, "scheme://authority/a.1b"),
                new MatchCondition(IntentFilter.MATCH_CATEGORY_PATH, null,
                        null, null, "scheme://authority/a2b"),
                new MatchCondition(IntentFilter.NO_MATCH_DATA, null,
                        null, null, "scheme://authority/a.bc"),
                new MatchCondition(IntentFilter.NO_MATCH_DATA, null,
                        null, null, "scheme://authority/"),
        });
        filter = new Match(null, null, null,
                new String[]{"scheme"}, new String[]{"authority"}, null,
                new String[]{"/a.*"},
                new int[]{PATTERN_SIMPLE_GLOB});
        checkMatches(filter, new MatchCondition[]{
                new MatchCondition(IntentFilter.NO_MATCH_DATA, null,
                        null, null, null),
                new MatchCondition(IntentFilter.MATCH_CATEGORY_PATH, null,
                        null, null, "scheme://authority/ab"),
                new MatchCondition(IntentFilter.MATCH_CATEGORY_PATH, null,
                        null, null, "scheme://authority/a.b"),
                new MatchCondition(IntentFilter.MATCH_CATEGORY_PATH, null,
                        null, null, "scheme://authority/a.1b"),
                new MatchCondition(IntentFilter.MATCH_CATEGORY_PATH, null,
                        null, null, "scheme://authority/a2b"),
                new MatchCondition(IntentFilter.MATCH_CATEGORY_PATH, null,
                        null, null, "scheme://authority/a.bc"),
                new MatchCondition(IntentFilter.NO_MATCH_DATA, null,
                        null, null, "scheme://authority/"),
        });
        filter = new Match(null, null, null,
                new String[]{"scheme"}, new String[]{"authority"}, null,
                new String[]{"/a.\\*b"},
                new int[]{PATTERN_SIMPLE_GLOB});
        checkMatches(filter, new MatchCondition[]{
                new MatchCondition(IntentFilter.NO_MATCH_DATA, null,
                        null, null, null),
                new MatchCondition(IntentFilter.NO_MATCH_DATA, null,
                        null, null, "scheme://authority/ab"),
                new MatchCondition(IntentFilter.MATCH_CATEGORY_PATH, null,
                        null, null, "scheme://authority/a.*b"),
                new MatchCondition(IntentFilter.MATCH_CATEGORY_PATH, null,
                        null, null, "scheme://authority/a1*b"),
                new MatchCondition(IntentFilter.NO_MATCH_DATA, null,
                        null, null, "scheme://authority/a2b"),
                new MatchCondition(IntentFilter.NO_MATCH_DATA, null,
                        null, null, "scheme://authority/a.bc"),
                new MatchCondition(IntentFilter.NO_MATCH_DATA, null,
                        null, null, "scheme://authority/"),
        });
        filter = new Match(null, null, null,
                new String[]{"scheme"}, new String[]{"authority"}, null,
                new String[]{"/a.\\*"},
                new int[]{PATTERN_SIMPLE_GLOB});
        checkMatches(filter, new MatchCondition[]{
                new MatchCondition(IntentFilter.NO_MATCH_DATA, null,
                        null, null, null),
                new MatchCondition(IntentFilter.NO_MATCH_DATA, null,
                        null, null, "scheme://authority/ab"),
                new MatchCondition(IntentFilter.MATCH_CATEGORY_PATH, null,
                        null, null, "scheme://authority/a.*"),
                new MatchCondition(IntentFilter.MATCH_CATEGORY_PATH, null,
                        null, null, "scheme://authority/a1*"),
                new MatchCondition(IntentFilter.NO_MATCH_DATA, null,
                        null, null, "scheme://authority/a1b"),
        });
    
public voidtestSchemes()

        IntentFilter filter = new Match(null, null, null,
                new String[]{"scheme1"}, null, null);
        checkMatches(filter, new MatchCondition[]{
                new MatchCondition(IntentFilter.NO_MATCH_DATA, null,
                        null, null, null),
                new MatchCondition(IntentFilter.MATCH_CATEGORY_SCHEME, null,
                        null, null, "scheme1:foo"),
                new MatchCondition(IntentFilter.NO_MATCH_DATA, null,
                        null, null, "scheme2:foo"),
        });

        filter = new Match(null, null, null,
                new String[]{"scheme1", "scheme2"}, null, null);
        checkMatches(filter, new MatchCondition[]{
                new MatchCondition(IntentFilter.NO_MATCH_DATA, null,
                        null, null, null),
                new MatchCondition(IntentFilter.MATCH_CATEGORY_SCHEME, null,
                        null, null, "scheme1:foo"),
                new MatchCondition(IntentFilter.MATCH_CATEGORY_SCHEME, null,
                        null, null, "scheme2:foo"),
                new MatchCondition(IntentFilter.NO_MATCH_DATA, null,
                        null, null, "scheme3:foo"),
        });