blob: 88186cdb3873368b3d224b31482ea62df13a1ea6 [file] [log] [blame]
Julia Reynolds7380d872018-01-12 10:28:26 -05001/*
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 */
16package com.android.server.notification;
17
18import static org.mockito.ArgumentMatchers.anyBoolean;
19import static org.mockito.ArgumentMatchers.eq;
20import static org.mockito.Matchers.any;
21import static org.mockito.Matchers.anyInt;
22import static org.mockito.Mockito.never;
23import static org.mockito.Mockito.spy;
24import static org.mockito.Mockito.times;
25import static org.mockito.Mockito.verify;
26import static org.mockito.Mockito.when;
27
Fabian Kozynskid9425662019-01-29 13:08:30 -050028import android.app.INotificationManager;
Julia Reynolds7380d872018-01-12 10:28:26 -050029import android.content.ComponentName;
30import android.content.Context;
31import android.content.pm.IPackageManager;
32import android.content.pm.PackageManager;
33import android.content.pm.ResolveInfo;
34import android.content.pm.ServiceInfo;
35import android.content.pm.UserInfo;
Annie Meng8b646fd2019-02-01 18:46:42 +000036import android.os.UserHandle;
Julia Reynolds7380d872018-01-12 10:28:26 -050037import android.os.UserManager;
Julia Reynoldsca8e5352018-09-18 13:39:26 -040038import android.util.IntArray;
Julia Reynolds7380d872018-01-12 10:28:26 -050039import android.util.Xml;
40
Julia Reynolds7380d872018-01-12 10:28:26 -050041import com.android.server.UiServiceTestCase;
42import com.android.server.notification.NotificationManagerService.NotificationAssistants;
43
44import org.junit.Before;
45import org.junit.Test;
46import org.mockito.Mock;
Julia Reynolds7380d872018-01-12 10:28:26 -050047import org.mockito.MockitoAnnotations;
48import org.xmlpull.v1.XmlPullParser;
Julia Reynolds7380d872018-01-12 10:28:26 -050049
50import java.io.BufferedInputStream;
Julia Reynolds7380d872018-01-12 10:28:26 -050051import java.io.ByteArrayInputStream;
Julia Reynolds7380d872018-01-12 10:28:26 -050052import java.util.ArrayList;
53import java.util.List;
54
55public 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 Kozynskid9425662019-01-29 13:08:30 -050065 @Mock
66 private INotificationManager mINm;
Julia Reynolds7380d872018-01-12 10:28:26 -050067
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 Kozynskid9425662019-01-29 13:08:30 -050085 when(mNm.getBinderService()).thenReturn(mINm);
Julia Reynolds7380d872018-01-12 10:28:26 -050086
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 Reynoldsca8e5352018-09-18 13:39:26 -0400107 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 Reynolds7380d872018-01-12 10:28:26 -0500113 }
114
115 @Test
Julia Reynoldsd6d5a592018-04-02 11:03:32 -0400116 public void testXmlUpgrade() {
Tony Mak9a3c1f12019-03-04 16:04:42 +0000117 mAssistants.resetDefaultAssistantsIfNecessary();
Julia Reynolds7380d872018-01-12 10:28:26 -0500118
119 //once per user
Tony Mak9a3c1f12019-03-04 16:04:42 +0000120 verify(mNm, times(mUm.getUsers().size())).setDefaultAssistantForUser(anyInt());
Julia Reynolds7380d872018-01-12 10:28:26 -0500121 }
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 Meng8b646fd2019-02-01 18:46:42 +0000133 mAssistants.readXml(parser, null, false, UserHandle.USER_ALL);
Julia Reynolds7380d872018-01-12 10:28:26 -0500134
Tony Mak9a3c1f12019-03-04 16:04:42 +0000135 verify(mNm, never()).setDefaultAssistantForUser(anyInt());
Julia Reynolds7380d872018-01-12 10:28:26 -0500136 verify(mAssistants, times(1)).addApprovedList(
137 new ComponentName("b", "b").flattenToString(),10, true);
138 }
Fabian Kozynskid9425662019-01-29 13:08:30 -0500139
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 Mak9a3c1f12019-03-04 16:04:42 +0000146 verify(mNm, never()).setNotificationAssistantAccessGrantedForUserInternal(
147 any(ComponentName.class), eq(mZero.id), anyBoolean());
Fabian Kozynskid9425662019-01-29 13:08:30 -0500148
149 mAssistants.setPackageOrComponentEnabled(component2.flattenToString(), mZero.id, true,
150 true);
Tony Mak9a3c1f12019-03-04 16:04:42 +0000151 verify(mNm, times(1)).setNotificationAssistantAccessGrantedForUserInternal(
152 component1, mZero.id, false);
Fabian Kozynskid9425662019-01-29 13:08:30 -0500153 }
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 Mak9a3c1f12019-03-04 16:04:42 +0000162 verify(mNm, never()).setNotificationAssistantAccessGrantedForUserInternal(
163 any(ComponentName.class), eq(mZero.id), anyBoolean());
Fabian Kozynskid9425662019-01-29 13:08:30 -0500164 }
Julia Reynolds7380d872018-01-12 10:28:26 -0500165}