Home | History | Annotate | Download | only in notification
      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 
     28 import android.content.ComponentName;
     29 import android.content.Context;
     30 import android.content.pm.IPackageManager;
     31 import android.content.pm.PackageManager;
     32 import android.content.pm.ResolveInfo;
     33 import android.content.pm.ServiceInfo;
     34 import android.content.pm.UserInfo;
     35 import android.os.UserManager;
     36 import android.util.Xml;
     37 
     38 import com.android.internal.util.FastXmlSerializer;
     39 import com.android.server.UiServiceTestCase;
     40 import com.android.server.notification.NotificationManagerService.NotificationAssistants;
     41 
     42 import org.junit.Before;
     43 import org.junit.Test;
     44 import org.mockito.Mock;
     45 import org.mockito.Mockito;
     46 import org.mockito.MockitoAnnotations;
     47 import org.xmlpull.v1.XmlPullParser;
     48 import org.xmlpull.v1.XmlSerializer;
     49 
     50 import java.io.BufferedInputStream;
     51 import java.io.BufferedOutputStream;
     52 import java.io.ByteArrayInputStream;
     53 import java.io.ByteArrayOutputStream;
     54 import java.util.ArrayList;
     55 import java.util.List;
     56 
     57 public class NotificationAssistantsTest extends UiServiceTestCase {
     58 
     59     @Mock
     60     private PackageManager mPm;
     61     @Mock
     62     private IPackageManager miPm;
     63     @Mock
     64     private UserManager mUm;
     65     @Mock
     66     NotificationManagerService mNm;
     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));
     85 
     86         List<ResolveInfo> approved = new ArrayList<>();
     87         ResolveInfo resolve = new ResolveInfo();
     88         approved.add(resolve);
     89         ServiceInfo info = new ServiceInfo();
     90         info.packageName = "a";
     91         info.name="a";
     92         resolve.serviceInfo = info;
     93         when(mPm.queryIntentServicesAsUser(any(), anyInt(), anyInt()))
     94                 .thenReturn(approved);
     95 
     96         List<UserInfo> users = new ArrayList<>();
     97         users.add(mZero);
     98         users.add(mTen);
     99         users.add(new UserInfo(11, "11", 0));
    100         users.add(new UserInfo(12, "12", 0));
    101         for (UserInfo user : users) {
    102             when(mUm.getUserInfo(eq(user.id))).thenReturn(user);
    103         }
    104         when(mUm.getUsers()).thenReturn(users);
    105         when(mUm.getUsers(anyBoolean())).thenReturn(users);
    106         when(mUserProfiles.getCurrentProfileIds()).thenReturn(new int[] {0, 10, 11, 12});
    107     }
    108 
    109     @Test
    110     public void testXmlUpgrade() {
    111         mAssistants.ensureAssistant();
    112 
    113         //once per user
    114         verify(mNm, times(mUm.getUsers().size())).readDefaultAssistant(anyInt());
    115     }
    116 
    117     @Test
    118     public void testXmlUpgradeExistingApprovedComponents() throws Exception {
    119         String xml = "<enabled_assistants>"
    120                 + "<service_listing approved=\"b/b\" user=\"10\" primary=\"true\" />"
    121                 + "</enabled_assistants>";
    122 
    123         XmlPullParser parser = Xml.newPullParser();
    124         parser.setInput(new BufferedInputStream(
    125                 new ByteArrayInputStream(xml.toString().getBytes())), null);
    126         parser.nextTag();
    127         mAssistants.readXml(parser, null);
    128 
    129         verify(mNm, never()).readDefaultAssistant(anyInt());
    130         verify(mAssistants, times(1)).addApprovedList(
    131                 new ComponentName("b", "b").flattenToString(),10, true);
    132     }
    133 }
    134