1/*
2 * Copyright (C) 2007 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
17package android.net;
18
19import android.content.UriMatcher;
20import android.net.Uri;
21import android.test.suitebuilder.annotation.SmallTest;
22
23import junit.framework.TestCase;
24
25public class UriMatcherTest extends TestCase {
26
27    static final int ROOT = 0;
28    static final int PEOPLE = 1;
29    static final int PEOPLE_ID = 2;
30    static final int PEOPLE_PHONES = 3;
31    static final int PEOPLE_PHONES_ID = 4;
32    static final int PEOPLE_ADDRESSES = 5;
33    static final int PEOPLE_ADDRESSES_ID = 6;
34    static final int PEOPLE_CONTACTMETH = 7;
35    static final int PEOPLE_CONTACTMETH_ID = 8;
36    static final int CALLS = 9;
37    static final int CALLS_ID = 10;
38    static final int CALLERID = 11;
39    static final int CALLERID_TEXT = 12;
40    static final int FILTERRECENT = 13;
41    static final int ANOTHER_PATH_SEGMENT = 13;
42
43    @SmallTest
44    public void testContentUris() {
45        UriMatcher matcher = new UriMatcher(ROOT);
46        matcher.addURI("people", null, PEOPLE);
47        matcher.addURI("people", "#", PEOPLE_ID);
48        matcher.addURI("people", "#/phones", PEOPLE_PHONES);
49        matcher.addURI("people", "#/phones/blah", PEOPLE_PHONES_ID);
50        matcher.addURI("people", "#/phones/#", PEOPLE_PHONES_ID);
51        matcher.addURI("people", "#/addresses", PEOPLE_ADDRESSES);
52        matcher.addURI("people", "#/addresses/#", PEOPLE_ADDRESSES_ID);
53        matcher.addURI("people", "#/contact-methods", PEOPLE_CONTACTMETH);
54        matcher.addURI("people", "#/contact-methods/#", PEOPLE_CONTACTMETH_ID);
55        matcher.addURI("calls", null, CALLS);
56        matcher.addURI("calls", "#", CALLS_ID);
57        matcher.addURI("caller-id", null, CALLERID);
58        matcher.addURI("caller-id", "*", CALLERID_TEXT);
59        matcher.addURI("filter-recent", null, FILTERRECENT);
60        matcher.addURI("auth", "another/path/segment", ANOTHER_PATH_SEGMENT);
61        checkAll(matcher);
62    }
63
64    @SmallTest
65    public void testContentUrisWithLeadingSlash() {
66        UriMatcher matcher = new UriMatcher(ROOT);
67        matcher.addURI("people", null, PEOPLE);
68        matcher.addURI("people", "/#", PEOPLE_ID);
69        matcher.addURI("people", "/#/phones", PEOPLE_PHONES);
70        matcher.addURI("people", "/#/phones/blah", PEOPLE_PHONES_ID);
71        matcher.addURI("people", "/#/phones/#", PEOPLE_PHONES_ID);
72        matcher.addURI("people", "/#/addresses", PEOPLE_ADDRESSES);
73        matcher.addURI("people", "/#/addresses/#", PEOPLE_ADDRESSES_ID);
74        matcher.addURI("people", "/#/contact-methods", PEOPLE_CONTACTMETH);
75        matcher.addURI("people", "/#/contact-methods/#", PEOPLE_CONTACTMETH_ID);
76        matcher.addURI("calls", null, CALLS);
77        matcher.addURI("calls", "/#", CALLS_ID);
78        matcher.addURI("caller-id", null, CALLERID);
79        matcher.addURI("caller-id", "/*", CALLERID_TEXT);
80        matcher.addURI("filter-recent", null, FILTERRECENT);
81        matcher.addURI("auth", "/another/path/segment", ANOTHER_PATH_SEGMENT);
82        checkAll(matcher);
83    }
84
85    @SmallTest
86    public void testContentUrisWithLeadingSlashAndOnlySlash() {
87        UriMatcher matcher = new UriMatcher(ROOT);
88        matcher.addURI("people", "/", PEOPLE);
89        matcher.addURI("people", "/#", PEOPLE_ID);
90        matcher.addURI("people", "/#/phones", PEOPLE_PHONES);
91        matcher.addURI("people", "/#/phones/blah", PEOPLE_PHONES_ID);
92        matcher.addURI("people", "/#/phones/#", PEOPLE_PHONES_ID);
93        matcher.addURI("people", "/#/addresses", PEOPLE_ADDRESSES);
94        matcher.addURI("people", "/#/addresses/#", PEOPLE_ADDRESSES_ID);
95        matcher.addURI("people", "/#/contact-methods", PEOPLE_CONTACTMETH);
96        matcher.addURI("people", "/#/contact-methods/#", PEOPLE_CONTACTMETH_ID);
97        matcher.addURI("calls", "/", CALLS);
98        matcher.addURI("calls", "/#", CALLS_ID);
99        matcher.addURI("caller-id", "/", CALLERID);
100        matcher.addURI("caller-id", "/*", CALLERID_TEXT);
101        matcher.addURI("filter-recent", null, FILTERRECENT);
102        matcher.addURI("auth", "/another/path/segment", ANOTHER_PATH_SEGMENT);
103        checkAll(matcher);
104    }
105
106    private void checkAll(UriMatcher matcher) {
107        check("content://asdf", UriMatcher.NO_MATCH, matcher);
108        check("content://people", PEOPLE, matcher);
109        check("content://people/", PEOPLE, matcher);
110        check("content://people/1", PEOPLE_ID, matcher);
111        check("content://people/asdf", UriMatcher.NO_MATCH, matcher);
112        check("content://people/2/phones", PEOPLE_PHONES, matcher);
113        check("content://people/2/phones/3", PEOPLE_PHONES_ID, matcher);
114        check("content://people/2/phones/asdf", UriMatcher.NO_MATCH, matcher);
115        check("content://people/2/addresses", PEOPLE_ADDRESSES, matcher);
116        check("content://people/2/addresses/3", PEOPLE_ADDRESSES_ID, matcher);
117        check("content://people/2/addresses/asdf", UriMatcher.NO_MATCH, matcher);
118        check("content://people/2/contact-methods", PEOPLE_CONTACTMETH, matcher);
119        check("content://people/2/contact-methods/3", PEOPLE_CONTACTMETH_ID, matcher);
120        check("content://people/2/contact-methods/asdf", UriMatcher.NO_MATCH, matcher);
121        check("content://calls", CALLS, matcher);
122        check("content://calls/", CALLS, matcher);
123        check("content://calls/1", CALLS_ID, matcher);
124        check("content://calls/asdf", UriMatcher.NO_MATCH, matcher);
125        check("content://caller-id", CALLERID, matcher);
126        check("content://caller-id/", CALLERID, matcher);
127        check("content://caller-id/asdf", CALLERID_TEXT, matcher);
128        check("content://caller-id/1", CALLERID_TEXT, matcher);
129        check("content://filter-recent", FILTERRECENT, matcher);
130        check("content://auth/another/path/segment", ANOTHER_PATH_SEGMENT, matcher);
131    }
132
133    private void check(String uri, int expected, UriMatcher matcher) {
134        int result = matcher.match(Uri.parse(uri));
135        if (result != expected) {
136            String msg = "failed on " + uri;
137            msg += " expected " + expected + " got " + result;
138            throw new RuntimeException(msg);
139        }
140    }
141}
142
143