blob: dd46fa38d50ed0c0468decb3d7ea135ba8c92403 [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
Hidehiko Tsuchiya5acd41d2014-03-25 18:27:27 +090085 @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
Chiao Chengef23bf12013-03-20 13:12:41 -0700106 private void checkAll(UriMatcher matcher) {
107 check("content://asdf", UriMatcher.NO_MATCH, matcher);
108 check("content://people", PEOPLE, matcher);
Hidehiko Tsuchiya5acd41d2014-03-25 18:27:27 +0900109 check("content://people/", PEOPLE, matcher);
Chiao Chengef23bf12013-03-20 13:12:41 -0700110 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);
Hidehiko Tsuchiya5acd41d2014-03-25 18:27:27 +0900122 check("content://calls/", CALLS, matcher);
Chiao Chengef23bf12013-03-20 13:12:41 -0700123 check("content://calls/1", CALLS_ID, matcher);
124 check("content://calls/asdf", UriMatcher.NO_MATCH, matcher);
125 check("content://caller-id", CALLERID, matcher);
Hidehiko Tsuchiya5acd41d2014-03-25 18:27:27 +0900126 check("content://caller-id/", CALLERID, matcher);
Chiao Chengef23bf12013-03-20 13:12:41 -0700127 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));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800135 if (result != expected) {
136 String msg = "failed on " + uri;
137 msg += " expected " + expected + " got " + result;
138 throw new RuntimeException(msg);
139 }
140 }
141}
142