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