Julia Reynolds | 7380d87 | 2018-01-12 10:28:26 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 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 | package com.android.server.notification; |
| 17 | |
| 18 | import static org.mockito.ArgumentMatchers.anyBoolean; |
| 19 | import static org.mockito.ArgumentMatchers.eq; |
| 20 | import static org.mockito.Matchers.any; |
| 21 | import static org.mockito.Matchers.anyInt; |
| 22 | import static org.mockito.Mockito.never; |
| 23 | import static org.mockito.Mockito.spy; |
| 24 | import static org.mockito.Mockito.times; |
| 25 | import static org.mockito.Mockito.verify; |
| 26 | import static org.mockito.Mockito.when; |
| 27 | |
Fabian Kozynski | d942566 | 2019-01-29 13:08:30 -0500 | [diff] [blame] | 28 | import android.app.INotificationManager; |
Julia Reynolds | 7380d87 | 2018-01-12 10:28:26 -0500 | [diff] [blame] | 29 | import android.content.ComponentName; |
| 30 | import android.content.Context; |
| 31 | import android.content.pm.IPackageManager; |
| 32 | import android.content.pm.PackageManager; |
| 33 | import android.content.pm.ResolveInfo; |
| 34 | import android.content.pm.ServiceInfo; |
| 35 | import android.content.pm.UserInfo; |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 36 | import android.os.UserHandle; |
Julia Reynolds | 7380d87 | 2018-01-12 10:28:26 -0500 | [diff] [blame] | 37 | import android.os.UserManager; |
Julia Reynolds | ca8e535 | 2018-09-18 13:39:26 -0400 | [diff] [blame] | 38 | import android.util.IntArray; |
Julia Reynolds | 7380d87 | 2018-01-12 10:28:26 -0500 | [diff] [blame] | 39 | import android.util.Xml; |
| 40 | |
Julia Reynolds | 7380d87 | 2018-01-12 10:28:26 -0500 | [diff] [blame] | 41 | import com.android.server.UiServiceTestCase; |
| 42 | import com.android.server.notification.NotificationManagerService.NotificationAssistants; |
| 43 | |
| 44 | import org.junit.Before; |
| 45 | import org.junit.Test; |
| 46 | import org.mockito.Mock; |
Julia Reynolds | 7380d87 | 2018-01-12 10:28:26 -0500 | [diff] [blame] | 47 | import org.mockito.MockitoAnnotations; |
| 48 | import org.xmlpull.v1.XmlPullParser; |
Julia Reynolds | 7380d87 | 2018-01-12 10:28:26 -0500 | [diff] [blame] | 49 | |
| 50 | import java.io.BufferedInputStream; |
Julia Reynolds | 7380d87 | 2018-01-12 10:28:26 -0500 | [diff] [blame] | 51 | import java.io.ByteArrayInputStream; |
Julia Reynolds | 7380d87 | 2018-01-12 10:28:26 -0500 | [diff] [blame] | 52 | import java.util.ArrayList; |
| 53 | import java.util.List; |
| 54 | |
| 55 | public class NotificationAssistantsTest extends UiServiceTestCase { |
| 56 | |
| 57 | @Mock |
| 58 | private PackageManager mPm; |
| 59 | @Mock |
| 60 | private IPackageManager miPm; |
| 61 | @Mock |
| 62 | private UserManager mUm; |
| 63 | @Mock |
| 64 | NotificationManagerService mNm; |
Fabian Kozynski | d942566 | 2019-01-29 13:08:30 -0500 | [diff] [blame] | 65 | @Mock |
| 66 | private INotificationManager mINm; |
Julia Reynolds | 7380d87 | 2018-01-12 10:28:26 -0500 | [diff] [blame] | 67 | |
| 68 | NotificationAssistants mAssistants; |
| 69 | |
| 70 | @Mock |
| 71 | private ManagedServices.UserProfiles mUserProfiles; |
| 72 | |
| 73 | Object mLock = new Object(); |
| 74 | |
| 75 | UserInfo mZero = new UserInfo(0, "zero", 0); |
| 76 | UserInfo mTen = new UserInfo(10, "ten", 0); |
| 77 | |
| 78 | @Before |
| 79 | public void setUp() throws Exception { |
| 80 | MockitoAnnotations.initMocks(this); |
| 81 | |
| 82 | getContext().setMockPackageManager(mPm); |
| 83 | getContext().addMockSystemService(Context.USER_SERVICE, mUm); |
| 84 | mAssistants = spy(mNm.new NotificationAssistants(getContext(), mLock, mUserProfiles, miPm)); |
Fabian Kozynski | d942566 | 2019-01-29 13:08:30 -0500 | [diff] [blame] | 85 | when(mNm.getBinderService()).thenReturn(mINm); |
Julia Reynolds | 7380d87 | 2018-01-12 10:28:26 -0500 | [diff] [blame] | 86 | |
| 87 | List<ResolveInfo> approved = new ArrayList<>(); |
| 88 | ResolveInfo resolve = new ResolveInfo(); |
| 89 | approved.add(resolve); |
| 90 | ServiceInfo info = new ServiceInfo(); |
| 91 | info.packageName = "a"; |
| 92 | info.name="a"; |
| 93 | resolve.serviceInfo = info; |
| 94 | when(mPm.queryIntentServicesAsUser(any(), anyInt(), anyInt())) |
| 95 | .thenReturn(approved); |
| 96 | |
| 97 | List<UserInfo> users = new ArrayList<>(); |
| 98 | users.add(mZero); |
| 99 | users.add(mTen); |
| 100 | users.add(new UserInfo(11, "11", 0)); |
| 101 | users.add(new UserInfo(12, "12", 0)); |
| 102 | for (UserInfo user : users) { |
| 103 | when(mUm.getUserInfo(eq(user.id))).thenReturn(user); |
| 104 | } |
| 105 | when(mUm.getUsers()).thenReturn(users); |
| 106 | when(mUm.getUsers(anyBoolean())).thenReturn(users); |
Julia Reynolds | ca8e535 | 2018-09-18 13:39:26 -0400 | [diff] [blame] | 107 | IntArray profileIds = new IntArray(); |
| 108 | profileIds.add(0); |
| 109 | profileIds.add(11); |
| 110 | profileIds.add(10); |
| 111 | profileIds.add(12); |
| 112 | when(mUserProfiles.getCurrentProfileIds()).thenReturn(profileIds); |
Julia Reynolds | 7380d87 | 2018-01-12 10:28:26 -0500 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | @Test |
Julia Reynolds | d6d5a59 | 2018-04-02 11:03:32 -0400 | [diff] [blame] | 116 | public void testXmlUpgrade() { |
Tony Mak | 9a3c1f1 | 2019-03-04 16:04:42 +0000 | [diff] [blame] | 117 | mAssistants.resetDefaultAssistantsIfNecessary(); |
Julia Reynolds | 7380d87 | 2018-01-12 10:28:26 -0500 | [diff] [blame] | 118 | |
| 119 | //once per user |
Tony Mak | 9a3c1f1 | 2019-03-04 16:04:42 +0000 | [diff] [blame] | 120 | verify(mNm, times(mUm.getUsers().size())).setDefaultAssistantForUser(anyInt()); |
Julia Reynolds | 7380d87 | 2018-01-12 10:28:26 -0500 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | @Test |
| 124 | public void testXmlUpgradeExistingApprovedComponents() throws Exception { |
| 125 | String xml = "<enabled_assistants>" |
| 126 | + "<service_listing approved=\"b/b\" user=\"10\" primary=\"true\" />" |
| 127 | + "</enabled_assistants>"; |
| 128 | |
| 129 | XmlPullParser parser = Xml.newPullParser(); |
| 130 | parser.setInput(new BufferedInputStream( |
| 131 | new ByteArrayInputStream(xml.toString().getBytes())), null); |
| 132 | parser.nextTag(); |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 133 | mAssistants.readXml(parser, null, false, UserHandle.USER_ALL); |
Julia Reynolds | 7380d87 | 2018-01-12 10:28:26 -0500 | [diff] [blame] | 134 | |
Tony Mak | 9a3c1f1 | 2019-03-04 16:04:42 +0000 | [diff] [blame] | 135 | verify(mNm, never()).setDefaultAssistantForUser(anyInt()); |
Julia Reynolds | 7380d87 | 2018-01-12 10:28:26 -0500 | [diff] [blame] | 136 | verify(mAssistants, times(1)).addApprovedList( |
| 137 | new ComponentName("b", "b").flattenToString(),10, true); |
| 138 | } |
Fabian Kozynski | d942566 | 2019-01-29 13:08:30 -0500 | [diff] [blame] | 139 | |
| 140 | @Test |
| 141 | public void testSetPackageOrComponentEnabled_onlyOnePackage() throws Exception { |
| 142 | ComponentName component1 = ComponentName.unflattenFromString("package/Component1"); |
| 143 | ComponentName component2 = ComponentName.unflattenFromString("package/Component2"); |
| 144 | mAssistants.setPackageOrComponentEnabled(component1.flattenToString(), mZero.id, true, |
| 145 | true); |
Tony Mak | 9a3c1f1 | 2019-03-04 16:04:42 +0000 | [diff] [blame] | 146 | verify(mNm, never()).setNotificationAssistantAccessGrantedForUserInternal( |
| 147 | any(ComponentName.class), eq(mZero.id), anyBoolean()); |
Fabian Kozynski | d942566 | 2019-01-29 13:08:30 -0500 | [diff] [blame] | 148 | |
| 149 | mAssistants.setPackageOrComponentEnabled(component2.flattenToString(), mZero.id, true, |
| 150 | true); |
Tony Mak | 9a3c1f1 | 2019-03-04 16:04:42 +0000 | [diff] [blame] | 151 | verify(mNm, times(1)).setNotificationAssistantAccessGrantedForUserInternal( |
| 152 | component1, mZero.id, false); |
Fabian Kozynski | d942566 | 2019-01-29 13:08:30 -0500 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | @Test |
| 156 | public void testSetPackageOrComponentEnabled_samePackage() throws Exception { |
| 157 | ComponentName component1 = ComponentName.unflattenFromString("package/Component1"); |
| 158 | mAssistants.setPackageOrComponentEnabled(component1.flattenToString(), mZero.id, true, |
| 159 | true); |
| 160 | mAssistants.setPackageOrComponentEnabled(component1.flattenToString(), mZero.id, true, |
| 161 | true); |
Tony Mak | 9a3c1f1 | 2019-03-04 16:04:42 +0000 | [diff] [blame] | 162 | verify(mNm, never()).setNotificationAssistantAccessGrantedForUserInternal( |
| 163 | any(ComponentName.class), eq(mZero.id), anyBoolean()); |
Fabian Kozynski | d942566 | 2019-01-29 13:08:30 -0500 | [diff] [blame] | 164 | } |
Julia Reynolds | 7380d87 | 2018-01-12 10:28:26 -0500 | [diff] [blame] | 165 | } |