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