blob: a728d4fd58137418b0db089c5820f44614bad61a [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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
Neal Nguyen1a44d5d2010-01-13 10:42:43 -080017package android.net;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018
19import android.content.UriMatcher;
20import android.net.Uri;
21import android.test.suitebuilder.annotation.SmallTest;
Chiao Chengef23bf12013-03-20 13:12:41 -070022
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080023import junit.framework.TestCase;
24
Chiao Chengef23bf12013-03-20 13:12:41 -070025public class UriMatcherTest extends TestCase {
26
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080027 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;
Chiao Chengef23bf12013-03-20 13:12:41 -070041 static final int ANOTHER_PATH_SEGMENT = 13;
42
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043 @SmallTest
44 public void testContentUris() {
Chiao Chengef23bf12013-03-20 13:12:41 -070045 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);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080062 }
63
Chiao Chengef23bf12013-03-20 13:12:41 -070064 @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);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080083 }
84
Chiao Chengef23bf12013-03-20 13:12:41 -070085 private void checkAll(UriMatcher matcher) {
86 check("content://asdf", UriMatcher.NO_MATCH, matcher);
87 check("content://people", PEOPLE, matcher);
88 check("content://people/1", PEOPLE_ID, matcher);
89 check("content://people/asdf", UriMatcher.NO_MATCH, matcher);
90 check("content://people/2/phones", PEOPLE_PHONES, matcher);
91 check("content://people/2/phones/3", PEOPLE_PHONES_ID, matcher);
92 check("content://people/2/phones/asdf", UriMatcher.NO_MATCH, matcher);
93 check("content://people/2/addresses", PEOPLE_ADDRESSES, matcher);
94 check("content://people/2/addresses/3", PEOPLE_ADDRESSES_ID, matcher);
95 check("content://people/2/addresses/asdf", UriMatcher.NO_MATCH, matcher);
96 check("content://people/2/contact-methods", PEOPLE_CONTACTMETH, matcher);
97 check("content://people/2/contact-methods/3", PEOPLE_CONTACTMETH_ID, matcher);
98 check("content://people/2/contact-methods/asdf", UriMatcher.NO_MATCH, matcher);
99 check("content://calls", CALLS, matcher);
100 check("content://calls/1", CALLS_ID, matcher);
101 check("content://calls/asdf", UriMatcher.NO_MATCH, matcher);
102 check("content://caller-id", CALLERID, matcher);
103 check("content://caller-id/asdf", CALLERID_TEXT, matcher);
104 check("content://caller-id/1", CALLERID_TEXT, matcher);
105 check("content://filter-recent", FILTERRECENT, matcher);
106 check("content://auth/another/path/segment", ANOTHER_PATH_SEGMENT, matcher);
107 }
108
109 private void check(String uri, int expected, UriMatcher matcher) {
110 int result = matcher.match(Uri.parse(uri));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800111 if (result != expected) {
112 String msg = "failed on " + uri;
113 msg += " expected " + expected + " got " + result;
114 throw new RuntimeException(msg);
115 }
116 }
117}
118