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;
22import junit.framework.TestCase;
23
24public class UriMatcherTest extends TestCase
25{
26    static final int ROOT = 0;
27    static final int PEOPLE = 1;
28    static final int PEOPLE_ID = 2;
29    static final int PEOPLE_PHONES = 3;
30    static final int PEOPLE_PHONES_ID = 4;
31    static final int PEOPLE_ADDRESSES = 5;
32    static final int PEOPLE_ADDRESSES_ID = 6;
33    static final int PEOPLE_CONTACTMETH = 7;
34    static final int PEOPLE_CONTACTMETH_ID = 8;
35    static final int CALLS = 9;
36    static final int CALLS_ID = 10;
37    static final int CALLERID = 11;
38    static final int CALLERID_TEXT = 12;
39    static final int FILTERRECENT = 13;
40
41    @SmallTest
42    public void testContentUris() {
43        check("content://asdf", UriMatcher.NO_MATCH);
44        check("content://people", PEOPLE);
45        check("content://people/1", PEOPLE_ID);
46        check("content://people/asdf", UriMatcher.NO_MATCH);
47        check("content://people/2/phones", PEOPLE_PHONES);
48        check("content://people/2/phones/3", PEOPLE_PHONES_ID);
49        check("content://people/2/phones/asdf", UriMatcher.NO_MATCH);
50        check("content://people/2/addresses", PEOPLE_ADDRESSES);
51        check("content://people/2/addresses/3", PEOPLE_ADDRESSES_ID);
52        check("content://people/2/addresses/asdf", UriMatcher.NO_MATCH);
53        check("content://people/2/contact-methods", PEOPLE_CONTACTMETH);
54        check("content://people/2/contact-methods/3", PEOPLE_CONTACTMETH_ID);
55        check("content://people/2/contact-methods/asdf", UriMatcher.NO_MATCH);
56        check("content://calls", CALLS);
57        check("content://calls/1", CALLS_ID);
58        check("content://calls/asdf", UriMatcher.NO_MATCH);
59        check("content://caller-id", CALLERID);
60        check("content://caller-id/asdf", CALLERID_TEXT);
61        check("content://caller-id/1", CALLERID_TEXT);
62        check("content://filter-recent", FILTERRECENT);
63    }
64
65    private static final UriMatcher mURLMatcher = new UriMatcher(ROOT);
66
67    static
68    {
69        mURLMatcher.addURI("people", null, PEOPLE);
70        mURLMatcher.addURI("people", "#", PEOPLE_ID);
71        mURLMatcher.addURI("people", "#/phones", PEOPLE_PHONES);
72        mURLMatcher.addURI("people", "#/phones/blah", PEOPLE_PHONES_ID);
73        mURLMatcher.addURI("people", "#/phones/#", PEOPLE_PHONES_ID);
74        mURLMatcher.addURI("people", "#/addresses", PEOPLE_ADDRESSES);
75        mURLMatcher.addURI("people", "#/addresses/#", PEOPLE_ADDRESSES_ID);
76        mURLMatcher.addURI("people", "#/contact-methods", PEOPLE_CONTACTMETH);
77        mURLMatcher.addURI("people", "#/contact-methods/#", PEOPLE_CONTACTMETH_ID);
78        mURLMatcher.addURI("calls", null, CALLS);
79        mURLMatcher.addURI("calls", "#", CALLS_ID);
80        mURLMatcher.addURI("caller-id", null, CALLERID);
81        mURLMatcher.addURI("caller-id", "*", CALLERID_TEXT);
82        mURLMatcher.addURI("filter-recent", null, FILTERRECENT);
83    }
84
85    void check(String uri, int expected)
86    {
87        int result = mURLMatcher.match(Uri.parse(uri));
88        if (result != expected) {
89            String msg = "failed on " + uri;
90            msg += " expected " + expected + " got " + result;
91            throw new RuntimeException(msg);
92        }
93    }
94}
95
96