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