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 17 package android.net; 18 19 import android.content.UriMatcher; 20 import android.net.Uri; 21 import android.test.suitebuilder.annotation.SmallTest; 22 23 import junit.framework.TestCase; 24 25 public 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 testContentUris()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 testContentUrisWithLeadingSlash()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 checkAll(UriMatcher matcher)85 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 check(String uri, int expected, UriMatcher matcher)109 private void check(String uri, int expected, UriMatcher matcher) { 110 int result = matcher.match(Uri.parse(uri)); 111 if (result != expected) { 112 String msg = "failed on " + uri; 113 msg += " expected " + expected + " got " + result; 114 throw new RuntimeException(msg); 115 } 116 } 117 } 118 119