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.content; 18 19import java.util.ArrayList; 20 21import android.content.ContentService.ObserverCall; 22import android.content.ContentService.ObserverNode; 23import android.database.ContentObserver; 24import android.net.Uri; 25import android.os.Handler; 26import android.os.UserHandle; 27import android.test.AndroidTestCase; 28 29public class ObserverNodeTest extends AndroidTestCase { 30 static class TestObserver extends ContentObserver { 31 public TestObserver() { 32 super(new Handler()); 33 } 34 } 35 36 public void testUri() { 37 final int myUserHandle = UserHandle.myUserId(); 38 39 ObserverNode root = new ObserverNode(""); 40 Uri[] uris = new Uri[] { 41 Uri.parse("content://c/a/"), 42 Uri.parse("content://c/"), 43 Uri.parse("content://x/"), 44 Uri.parse("content://c/b/"), 45 Uri.parse("content://c/a/a1/1/"), 46 Uri.parse("content://c/a/a1/2/"), 47 Uri.parse("content://c/b/1/"), 48 Uri.parse("content://c/b/2/"), 49 }; 50 51 int[] nums = new int[] {4, 7, 1, 4, 2, 2, 3, 3}; 52 53 // special case 54 root.addObserverLocked(uris[0], new TestObserver().getContentObserver(), false, root, 55 0, 0, myUserHandle); 56 for(int i = 1; i < uris.length; i++) { 57 root.addObserverLocked(uris[i], new TestObserver().getContentObserver(), true, root, 58 0, 0, myUserHandle); 59 } 60 61 ArrayList<ObserverCall> calls = new ArrayList<ObserverCall>(); 62 63 for (int i = nums.length - 1; i >=0; --i) { 64 root.collectObserversLocked(uris[i], 0, null, false, myUserHandle, calls); 65 assertEquals(nums[i], calls.size()); 66 calls.clear(); 67 } 68 } 69 70 public void testUriNotNotify() { 71 final int myUserHandle = UserHandle.myUserId(); 72 73 ObserverNode root = new ObserverNode(""); 74 Uri[] uris = new Uri[] { 75 Uri.parse("content://c/"), 76 Uri.parse("content://x/"), 77 Uri.parse("content://c/a/"), 78 Uri.parse("content://c/b/"), 79 Uri.parse("content://c/a/1/"), 80 Uri.parse("content://c/a/2/"), 81 Uri.parse("content://c/b/1/"), 82 Uri.parse("content://c/b/2/"), 83 }; 84 int[] nums = new int[] {7, 1, 3, 3, 1, 1, 1, 1}; 85 86 for(int i = 0; i < uris.length; i++) { 87 root.addObserverLocked(uris[i], new TestObserver().getContentObserver(), false, root, 88 0, 0, myUserHandle); 89 } 90 91 ArrayList<ObserverCall> calls = new ArrayList<ObserverCall>(); 92 93 for (int i = uris.length - 1; i >=0; --i) { 94 root.collectObserversLocked(uris[i], 0, null, false, myUserHandle, calls); 95 assertEquals(nums[i], calls.size()); 96 calls.clear(); 97 } 98 } 99} 100