Home | History | Annotate | Download | only in tests
      1 /*
      2  * Copyright (C) 2017 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 
     17 package com.android.server.telecom.tests;
     18 
     19 import android.app.NotificationManager;
     20 import android.content.Context;
     21 import android.content.pm.ApplicationInfo;
     22 import android.os.Build;
     23 import android.telecom.VideoProfile;
     24 import android.test.suitebuilder.annotation.SmallTest;
     25 
     26 import com.android.server.telecom.Call;
     27 import com.android.server.telecom.CallState;
     28 import com.android.server.telecom.HandoverState;
     29 import com.android.server.telecom.ui.IncomingCallNotifier;
     30 
     31 import org.junit.Before;
     32 import org.junit.Test;
     33 import org.junit.runner.RunWith;
     34 import org.junit.runners.JUnit4;
     35 import org.mockito.Mock;
     36 
     37 import static org.mockito.Matchers.any;
     38 import static org.mockito.Matchers.eq;
     39 import static org.mockito.Matchers.isNull;
     40 import static org.mockito.Mockito.doReturn;
     41 import static org.mockito.Mockito.never;
     42 import static org.mockito.Mockito.verify;
     43 import static org.mockito.Mockito.when;
     44 
     45 /**
     46  * Tests for the {@link com.android.server.telecom.ui.IncomingCallNotifier} class.
     47  */
     48 @RunWith(JUnit4.class)
     49 public class IncomingCallNotifierTest extends TelecomTestCase {
     50 
     51     @Mock private IncomingCallNotifier.CallsManagerProxy mCallsManagerProxy;
     52     @Mock private Call mAudioCall;
     53     @Mock private Call mVideoCall;
     54     @Mock private Call mRingingCall;
     55     private IncomingCallNotifier mIncomingCallNotifier;
     56     private NotificationManager mNotificationManager;
     57 
     58     @Override
     59     @Before
     60     public void setUp() throws Exception {
     61         super.setUp();
     62         mContext = mComponentContextFixture.getTestDouble().getApplicationContext();
     63         ApplicationInfo info = new ApplicationInfo();
     64         info.targetSdkVersion = Build.VERSION_CODES.N_MR1;
     65         doReturn(info).when(mContext).getApplicationInfo();
     66         doReturn(null).when(mContext).getTheme();
     67         mNotificationManager = (NotificationManager) mContext.getSystemService(
     68                 Context.NOTIFICATION_SERVICE);
     69         mIncomingCallNotifier = new IncomingCallNotifier(mContext);
     70         mIncomingCallNotifier.setCallsManagerProxy(mCallsManagerProxy);
     71 
     72         when(mAudioCall.getVideoState()).thenReturn(VideoProfile.STATE_AUDIO_ONLY);
     73         when(mAudioCall.getTargetPhoneAccountLabel()).thenReturn("Bar");
     74         when(mVideoCall.getVideoState()).thenReturn(VideoProfile.STATE_BIDIRECTIONAL);
     75         when(mVideoCall.getTargetPhoneAccountLabel()).thenReturn("Bar");
     76         when(mRingingCall.isSelfManaged()).thenReturn(true);
     77         when(mRingingCall.isIncoming()).thenReturn(true);
     78         when(mRingingCall.getState()).thenReturn(CallState.RINGING);
     79         when(mRingingCall.getVideoState()).thenReturn(VideoProfile.STATE_AUDIO_ONLY);
     80         when(mRingingCall.getTargetPhoneAccountLabel()).thenReturn("Foo");
     81         when(mRingingCall.getHandoverState()).thenReturn(HandoverState.HANDOVER_NONE);
     82     }
     83 
     84     /**
     85      * Add a call that isn't ringing.
     86      */
     87     @SmallTest
     88     @Test
     89     public void testSingleCall() {
     90         mIncomingCallNotifier.onCallAdded(mAudioCall);
     91         verify(mNotificationManager, never()).notify(eq(IncomingCallNotifier.NOTIFICATION_TAG),
     92                 eq(IncomingCallNotifier.NOTIFICATION_INCOMING_CALL), any());
     93     }
     94 
     95     /**
     96      * Add a ringing call when there is no other ongoing call.
     97      */
     98     @SmallTest
     99     @Test
    100     public void testIncomingDuringOngoingCall() {
    101         when(mCallsManagerProxy.hasCallsForOtherPhoneAccount(any())).thenReturn(false);
    102         mIncomingCallNotifier.onCallAdded(mRingingCall);
    103         verify(mNotificationManager, never()).notify(eq(IncomingCallNotifier.NOTIFICATION_TAG),
    104                 eq(IncomingCallNotifier.NOTIFICATION_INCOMING_CALL), any());
    105     }
    106 
    107     /**
    108      * Add a ringing call with another call ongoing, not from a different phone account.
    109      */
    110     @SmallTest
    111     @Test
    112     public void testIncomingDuringOngoingCall2() {
    113         when(mCallsManagerProxy.hasCallsForOtherPhoneAccount(any())).thenReturn(false);
    114         when(mCallsManagerProxy.getNumCallsForOtherPhoneAccount(any())).thenReturn(0);
    115         when(mCallsManagerProxy.getActiveCall()).thenReturn(mAudioCall);
    116 
    117         mIncomingCallNotifier.onCallAdded(mAudioCall);
    118         mIncomingCallNotifier.onCallAdded(mRingingCall);
    119         verify(mNotificationManager, never()).notify(eq(IncomingCallNotifier.NOTIFICATION_TAG),
    120                 eq(IncomingCallNotifier.NOTIFICATION_INCOMING_CALL), any());;
    121     }
    122 
    123     /**
    124      * Remove ringing call with another call ongoing.
    125      */
    126     @SmallTest
    127     @Test
    128     public void testCallRemoved() {
    129         when(mCallsManagerProxy.hasCallsForOtherPhoneAccount(any())).thenReturn(true);
    130         when(mCallsManagerProxy.getNumCallsForOtherPhoneAccount(any())).thenReturn(1);
    131         when(mCallsManagerProxy.getActiveCall()).thenReturn(mAudioCall);
    132 
    133         mIncomingCallNotifier.onCallAdded(mAudioCall);
    134         mIncomingCallNotifier.onCallAdded(mRingingCall);
    135         verify(mNotificationManager).notify(eq(IncomingCallNotifier.NOTIFICATION_TAG),
    136                 eq(IncomingCallNotifier.NOTIFICATION_INCOMING_CALL), any());
    137         mIncomingCallNotifier.onCallRemoved(mRingingCall);
    138         verify(mNotificationManager).cancel(eq(IncomingCallNotifier.NOTIFICATION_TAG),
    139                 eq(IncomingCallNotifier.NOTIFICATION_INCOMING_CALL));
    140     }
    141 
    142     /**
    143      * Ensure notification doesn't show during handover.
    144      */
    145     @SmallTest
    146     @Test
    147     public void testDontShowDuringHandover1() {
    148         when(mCallsManagerProxy.hasCallsForOtherPhoneAccount(any())).thenReturn(true);
    149         when(mCallsManagerProxy.getNumCallsForOtherPhoneAccount(any())).thenReturn(1);
    150         when(mCallsManagerProxy.getActiveCall()).thenReturn(mAudioCall);
    151         when(mRingingCall.getHandoverState()).thenReturn(HandoverState.HANDOVER_FROM_STARTED);
    152 
    153         mIncomingCallNotifier.onCallAdded(mAudioCall);
    154         mIncomingCallNotifier.onCallAdded(mRingingCall);
    155 
    156         // Incoming call is in the middle of a handover, don't expect to be notified.
    157         verify(mNotificationManager, never()).notify(eq(IncomingCallNotifier.NOTIFICATION_TAG),
    158                 eq(IncomingCallNotifier.NOTIFICATION_INCOMING_CALL), any());;
    159     }
    160 
    161     /**
    162      * Ensure notification doesn't show during handover.
    163      */
    164     @SmallTest
    165     @Test
    166     public void testDontShowDuringHandover2() {
    167         when(mCallsManagerProxy.hasCallsForOtherPhoneAccount(any())).thenReturn(true);
    168         when(mCallsManagerProxy.getNumCallsForOtherPhoneAccount(any())).thenReturn(1);
    169         when(mCallsManagerProxy.getActiveCall()).thenReturn(mAudioCall);
    170         when(mRingingCall.getHandoverState()).thenReturn(HandoverState.HANDOVER_COMPLETE);
    171 
    172         mIncomingCallNotifier.onCallAdded(mAudioCall);
    173         mIncomingCallNotifier.onCallAdded(mRingingCall);
    174 
    175         // Incoming call is done a handover, don't expect to be notified.
    176         verify(mNotificationManager, never()).notify(eq(IncomingCallNotifier.NOTIFICATION_TAG),
    177                 eq(IncomingCallNotifier.NOTIFICATION_INCOMING_CALL), any());;
    178     }
    179 }
    180