The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 | |
Amith Yamasani | 03cb98a | 2012-12-11 11:14:58 -0800 | [diff] [blame] | 17 | package com.android.server.content; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 18 | |
| 19 | import java.util.ArrayList; |
| 20 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 21 | import android.database.ContentObserver; |
| 22 | import android.net.Uri; |
| 23 | import android.os.Handler; |
Makoto Onuki | 15e7a25 | 2017-06-08 17:12:05 -0700 | [diff] [blame] | 24 | import android.os.Looper; |
Christopher Tate | 16aa973 | 2012-09-17 16:23:44 -0700 | [diff] [blame] | 25 | import android.os.UserHandle; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 26 | import android.test.AndroidTestCase; |
Makoto Onuki | 15e7a25 | 2017-06-08 17:12:05 -0700 | [diff] [blame] | 27 | import android.test.suitebuilder.annotation.SmallTest; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 28 | |
Amith Yamasani | 03cb98a | 2012-12-11 11:14:58 -0800 | [diff] [blame] | 29 | import com.android.server.content.ContentService.ObserverCall; |
| 30 | import com.android.server.content.ContentService.ObserverNode; |
| 31 | |
Makoto Onuki | 15e7a25 | 2017-06-08 17:12:05 -0700 | [diff] [blame] | 32 | /** |
| 33 | * bit FrameworksServicesTests:com.android.server.content.ObserverNodeTest |
| 34 | */ |
| 35 | @SmallTest |
Fred Quintana | 002ffad5 | 2010-03-02 11:18:16 -0800 | [diff] [blame] | 36 | public class ObserverNodeTest extends AndroidTestCase { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 37 | static class TestObserver extends ContentObserver { |
| 38 | public TestObserver() { |
Makoto Onuki | 15e7a25 | 2017-06-08 17:12:05 -0700 | [diff] [blame] | 39 | super(new Handler(Looper.getMainLooper())); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 40 | } |
| 41 | } |
Fred Quintana | 002ffad5 | 2010-03-02 11:18:16 -0800 | [diff] [blame] | 42 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 43 | public void testUri() { |
Christopher Tate | 16aa973 | 2012-09-17 16:23:44 -0700 | [diff] [blame] | 44 | final int myUserHandle = UserHandle.myUserId(); |
| 45 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 46 | ObserverNode root = new ObserverNode(""); |
| 47 | Uri[] uris = new Uri[] { |
| 48 | Uri.parse("content://c/a/"), |
| 49 | Uri.parse("content://c/"), |
Fred Quintana | 002ffad5 | 2010-03-02 11:18:16 -0800 | [diff] [blame] | 50 | Uri.parse("content://x/"), |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 51 | 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 Quintana | 002ffad5 | 2010-03-02 11:18:16 -0800 | [diff] [blame] | 57 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 58 | int[] nums = new int[] {4, 7, 1, 4, 2, 2, 3, 3}; |
Fred Quintana | 002ffad5 | 2010-03-02 11:18:16 -0800 | [diff] [blame] | 59 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 60 | // special case |
Christopher Tate | 16aa973 | 2012-09-17 16:23:44 -0700 | [diff] [blame] | 61 | root.addObserverLocked(uris[0], new TestObserver().getContentObserver(), false, root, |
| 62 | 0, 0, myUserHandle); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 63 | for(int i = 1; i < uris.length; i++) { |
Christopher Tate | 16aa973 | 2012-09-17 16:23:44 -0700 | [diff] [blame] | 64 | root.addObserverLocked(uris[i], new TestObserver().getContentObserver(), true, root, |
| 65 | 0, 0, myUserHandle); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 66 | } |
Fred Quintana | 002ffad5 | 2010-03-02 11:18:16 -0800 | [diff] [blame] | 67 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 68 | ArrayList<ObserverCall> calls = new ArrayList<ObserverCall>(); |
Fred Quintana | 002ffad5 | 2010-03-02 11:18:16 -0800 | [diff] [blame] | 69 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 70 | for (int i = nums.length - 1; i >=0; --i) { |
Dianne Hackborn | 141f11c | 2016-04-05 15:46:12 -0700 | [diff] [blame] | 71 | root.collectObserversLocked(uris[i], 0, null, false, 0, myUserHandle, calls); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 72 | assertEquals(nums[i], calls.size()); |
| 73 | calls.clear(); |
| 74 | } |
| 75 | } |
Fred Quintana | 002ffad5 | 2010-03-02 11:18:16 -0800 | [diff] [blame] | 76 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 77 | public void testUriNotNotify() { |
Christopher Tate | 16aa973 | 2012-09-17 16:23:44 -0700 | [diff] [blame] | 78 | final int myUserHandle = UserHandle.myUserId(); |
| 79 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 80 | ObserverNode root = new ObserverNode(""); |
| 81 | Uri[] uris = new Uri[] { |
| 82 | Uri.parse("content://c/"), |
Fred Quintana | 002ffad5 | 2010-03-02 11:18:16 -0800 | [diff] [blame] | 83 | Uri.parse("content://x/"), |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 84 | 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 Quintana | 002ffad5 | 2010-03-02 11:18:16 -0800 | [diff] [blame] | 92 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 93 | for(int i = 0; i < uris.length; i++) { |
Christopher Tate | 16aa973 | 2012-09-17 16:23:44 -0700 | [diff] [blame] | 94 | root.addObserverLocked(uris[i], new TestObserver().getContentObserver(), false, root, |
| 95 | 0, 0, myUserHandle); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 96 | } |
Fred Quintana | 002ffad5 | 2010-03-02 11:18:16 -0800 | [diff] [blame] | 97 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 98 | ArrayList<ObserverCall> calls = new ArrayList<ObserverCall>(); |
Fred Quintana | 002ffad5 | 2010-03-02 11:18:16 -0800 | [diff] [blame] | 99 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 100 | for (int i = uris.length - 1; i >=0; --i) { |
Dianne Hackborn | 141f11c | 2016-04-05 15:46:12 -0700 | [diff] [blame] | 101 | root.collectObserversLocked(uris[i], 0, null, false, 0, myUserHandle, calls); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 102 | assertEquals(nums[i], calls.size()); |
| 103 | calls.clear(); |
| 104 | } |
| 105 | } |
| 106 | } |