blob: 62b0ca805c1b6710c8f36242732fb1c86b3ae41b [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
Amith Yamasani03cb98a2012-12-11 11:14:58 -080017package com.android.server.content;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018
19import java.util.ArrayList;
20
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080021import android.database.ContentObserver;
22import android.net.Uri;
23import android.os.Handler;
Makoto Onuki15e7a252017-06-08 17:12:05 -070024import android.os.Looper;
Christopher Tate16aa9732012-09-17 16:23:44 -070025import android.os.UserHandle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026import android.test.AndroidTestCase;
Makoto Onuki15e7a252017-06-08 17:12:05 -070027import android.test.suitebuilder.annotation.SmallTest;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028
Amith Yamasani03cb98a2012-12-11 11:14:58 -080029import com.android.server.content.ContentService.ObserverCall;
30import com.android.server.content.ContentService.ObserverNode;
31
Makoto Onuki15e7a252017-06-08 17:12:05 -070032/**
33 * bit FrameworksServicesTests:com.android.server.content.ObserverNodeTest
34 */
35@SmallTest
Fred Quintana002ffad52010-03-02 11:18:16 -080036public class ObserverNodeTest extends AndroidTestCase {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037 static class TestObserver extends ContentObserver {
38 public TestObserver() {
Makoto Onuki15e7a252017-06-08 17:12:05 -070039 super(new Handler(Looper.getMainLooper()));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040 }
41 }
Fred Quintana002ffad52010-03-02 11:18:16 -080042
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043 public void testUri() {
Christopher Tate16aa9732012-09-17 16:23:44 -070044 final int myUserHandle = UserHandle.myUserId();
45
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046 ObserverNode root = new ObserverNode("");
47 Uri[] uris = new Uri[] {
48 Uri.parse("content://c/a/"),
49 Uri.parse("content://c/"),
Fred Quintana002ffad52010-03-02 11:18:16 -080050 Uri.parse("content://x/"),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051 Uri.parse("content://c/b/"),
52 Uri.parse("content://c/a/a1/1/"),
53 Uri.parse("content://c/a/a1/2/"),
54 Uri.parse("content://c/b/1/"),
55 Uri.parse("content://c/b/2/"),
56 };
Fred Quintana002ffad52010-03-02 11:18:16 -080057
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080058 int[] nums = new int[] {4, 7, 1, 4, 2, 2, 3, 3};
Fred Quintana002ffad52010-03-02 11:18:16 -080059
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060 // special case
Christopher Tate16aa9732012-09-17 16:23:44 -070061 root.addObserverLocked(uris[0], new TestObserver().getContentObserver(), false, root,
62 0, 0, myUserHandle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063 for(int i = 1; i < uris.length; i++) {
Christopher Tate16aa9732012-09-17 16:23:44 -070064 root.addObserverLocked(uris[i], new TestObserver().getContentObserver(), true, root,
65 0, 0, myUserHandle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080066 }
Fred Quintana002ffad52010-03-02 11:18:16 -080067
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068 ArrayList<ObserverCall> calls = new ArrayList<ObserverCall>();
Fred Quintana002ffad52010-03-02 11:18:16 -080069
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080070 for (int i = nums.length - 1; i >=0; --i) {
Dianne Hackborn141f11c2016-04-05 15:46:12 -070071 root.collectObserversLocked(uris[i], 0, null, false, 0, myUserHandle, calls);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080072 assertEquals(nums[i], calls.size());
73 calls.clear();
74 }
75 }
Fred Quintana002ffad52010-03-02 11:18:16 -080076
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077 public void testUriNotNotify() {
Christopher Tate16aa9732012-09-17 16:23:44 -070078 final int myUserHandle = UserHandle.myUserId();
79
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080080 ObserverNode root = new ObserverNode("");
81 Uri[] uris = new Uri[] {
82 Uri.parse("content://c/"),
Fred Quintana002ffad52010-03-02 11:18:16 -080083 Uri.parse("content://x/"),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084 Uri.parse("content://c/a/"),
85 Uri.parse("content://c/b/"),
86 Uri.parse("content://c/a/1/"),
87 Uri.parse("content://c/a/2/"),
88 Uri.parse("content://c/b/1/"),
89 Uri.parse("content://c/b/2/"),
90 };
91 int[] nums = new int[] {7, 1, 3, 3, 1, 1, 1, 1};
Fred Quintana002ffad52010-03-02 11:18:16 -080092
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080093 for(int i = 0; i < uris.length; i++) {
Christopher Tate16aa9732012-09-17 16:23:44 -070094 root.addObserverLocked(uris[i], new TestObserver().getContentObserver(), false, root,
95 0, 0, myUserHandle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080096 }
Fred Quintana002ffad52010-03-02 11:18:16 -080097
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080098 ArrayList<ObserverCall> calls = new ArrayList<ObserverCall>();
Fred Quintana002ffad52010-03-02 11:18:16 -080099
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800100 for (int i = uris.length - 1; i >=0; --i) {
Dianne Hackborn141f11c2016-04-05 15:46:12 -0700101 root.collectObserversLocked(uris[i], 0, null, false, 0, myUserHandle, calls);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800102 assertEquals(nums[i], calls.size());
103 calls.clear();
104 }
105 }
106}