summaryrefslogtreecommitdiff
path: root/tests/src/com/android/server/telecom/tests/IncomingCallNotifierTest.java
blob: a871b734fce1b78ff8a3a97812b1a1acf9e4ac3f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License
 */

package com.android.server.telecom.tests;

import android.app.NotificationManager;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.os.Build;
import android.telecom.VideoProfile;
import android.test.suitebuilder.annotation.SmallTest;

import com.android.server.telecom.Call;
import com.android.server.telecom.CallState;
import com.android.server.telecom.HandoverState;
import com.android.server.telecom.ui.IncomingCallNotifier;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.mockito.Mock;

import static org.mockito.Matchers.any;
import static org.mockito.Matchers.eq;
import static org.mockito.Matchers.isNull;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

/**
 * Tests for the {@link com.android.server.telecom.ui.IncomingCallNotifier} class.
 */
@RunWith(JUnit4.class)
public class IncomingCallNotifierTest extends TelecomTestCase {

    @Mock private IncomingCallNotifier.CallsManagerProxy mCallsManagerProxy;
    @Mock private Call mAudioCall;
    @Mock private Call mVideoCall;
    @Mock private Call mRingingCall;
    private IncomingCallNotifier mIncomingCallNotifier;
    private NotificationManager mNotificationManager;

    @Override
    @Before
    public void setUp() throws Exception {
        super.setUp();
        mContext = mComponentContextFixture.getTestDouble().getApplicationContext();
        ApplicationInfo info = new ApplicationInfo();
        info.targetSdkVersion = Build.VERSION_CODES.N_MR1;
        doReturn(info).when(mContext).getApplicationInfo();
        doReturn(null).when(mContext).getTheme();
        mNotificationManager = (NotificationManager) mContext.getSystemService(
                Context.NOTIFICATION_SERVICE);
        mIncomingCallNotifier = new IncomingCallNotifier(mContext);
        mIncomingCallNotifier.setCallsManagerProxy(mCallsManagerProxy);

        when(mAudioCall.getVideoState()).thenReturn(VideoProfile.STATE_AUDIO_ONLY);
        when(mAudioCall.getTargetPhoneAccountLabel()).thenReturn("Bar");
        when(mVideoCall.getVideoState()).thenReturn(VideoProfile.STATE_BIDIRECTIONAL);
        when(mVideoCall.getTargetPhoneAccountLabel()).thenReturn("Bar");
        when(mRingingCall.isSelfManaged()).thenReturn(true);
        when(mRingingCall.isIncoming()).thenReturn(true);
        when(mRingingCall.getState()).thenReturn(CallState.RINGING);
        when(mRingingCall.getVideoState()).thenReturn(VideoProfile.STATE_AUDIO_ONLY);
        when(mRingingCall.getTargetPhoneAccountLabel()).thenReturn("Foo");
        when(mRingingCall.getHandoverState()).thenReturn(HandoverState.HANDOVER_NONE);
    }

    @Override
    @After
    public void tearDown() throws Exception {
        super.tearDown();
    }

    /**
     * Add a call that isn't ringing.
     */
    @SmallTest
    @Test
    public void testSingleCall() {
        mIncomingCallNotifier.onCallAdded(mAudioCall);
        verify(mNotificationManager, never()).notify(eq(IncomingCallNotifier.NOTIFICATION_TAG),
                eq(IncomingCallNotifier.NOTIFICATION_INCOMING_CALL), any());
    }

    /**
     * Add a ringing call when there is no other ongoing call.
     */
    @SmallTest
    @Test
    public void testIncomingDuringOngoingCall() {
        when(mCallsManagerProxy.hasUnholdableCallsForOtherConnectionService(any())).thenReturn(false);
        mIncomingCallNotifier.onCallAdded(mRingingCall);
        verify(mNotificationManager, never()).notify(eq(IncomingCallNotifier.NOTIFICATION_TAG),
                eq(IncomingCallNotifier.NOTIFICATION_INCOMING_CALL), any());
    }

    /**
     * Add a ringing call with another call ongoing, not from a different phone account.
     */
    @SmallTest
    @Test
    public void testIncomingDuringOngoingCall2() {
        when(mCallsManagerProxy.hasUnholdableCallsForOtherConnectionService(any())).thenReturn(false);
        when(mCallsManagerProxy.getNumUnholdableCallsForOtherConnectionService(any())).thenReturn(0);
        when(mCallsManagerProxy.getActiveCall()).thenReturn(mAudioCall);

        mIncomingCallNotifier.onCallAdded(mAudioCall);
        mIncomingCallNotifier.onCallAdded(mRingingCall);
        verify(mNotificationManager, never()).notify(eq(IncomingCallNotifier.NOTIFICATION_TAG),
                eq(IncomingCallNotifier.NOTIFICATION_INCOMING_CALL), any());;
    }

    /**
     * Remove ringing call with another call ongoing.
     */
    @SmallTest
    @Test
    public void testCallRemoved() {
        when(mCallsManagerProxy.hasUnholdableCallsForOtherConnectionService(any())).thenReturn(true);
        when(mCallsManagerProxy.getNumUnholdableCallsForOtherConnectionService(any())).thenReturn(1);
        when(mCallsManagerProxy.getActiveCall()).thenReturn(mAudioCall);

        mIncomingCallNotifier.onCallAdded(mAudioCall);
        mIncomingCallNotifier.onCallAdded(mRingingCall);
        verify(mNotificationManager).notify(eq(IncomingCallNotifier.NOTIFICATION_TAG),
                eq(IncomingCallNotifier.NOTIFICATION_INCOMING_CALL), any());
        mIncomingCallNotifier.onCallRemoved(mRingingCall);
        verify(mNotificationManager).cancel(eq(IncomingCallNotifier.NOTIFICATION_TAG),
                eq(IncomingCallNotifier.NOTIFICATION_INCOMING_CALL));
    }

    /**
     * Ensure notification doesn't show during handover.
     */
    @SmallTest
    @Test
    public void testDontShowDuringHandover1() {
        when(mCallsManagerProxy.hasUnholdableCallsForOtherConnectionService(any())).thenReturn(true);
        when(mCallsManagerProxy.getNumUnholdableCallsForOtherConnectionService(any())).thenReturn(1);
        when(mCallsManagerProxy.getActiveCall()).thenReturn(mAudioCall);
        when(mRingingCall.getHandoverState()).thenReturn(HandoverState.HANDOVER_FROM_STARTED);

        mIncomingCallNotifier.onCallAdded(mAudioCall);
        mIncomingCallNotifier.onCallAdded(mRingingCall);

        // Incoming call is in the middle of a handover, don't expect to be notified.
        verify(mNotificationManager, never()).notify(eq(IncomingCallNotifier.NOTIFICATION_TAG),
                eq(IncomingCallNotifier.NOTIFICATION_INCOMING_CALL), any());;
    }

    /**
     * Ensure notification doesn't show during handover.
     */
    @SmallTest
    @Test
    public void testDontShowDuringHandover2() {
        when(mCallsManagerProxy.hasUnholdableCallsForOtherConnectionService(any())).thenReturn(true);
        when(mCallsManagerProxy.getNumUnholdableCallsForOtherConnectionService(any())).thenReturn(1);
        when(mCallsManagerProxy.getActiveCall()).thenReturn(mAudioCall);
        when(mRingingCall.getHandoverState()).thenReturn(HandoverState.HANDOVER_COMPLETE);

        mIncomingCallNotifier.onCallAdded(mAudioCall);
        mIncomingCallNotifier.onCallAdded(mRingingCall);

        // Incoming call is done a handover, don't expect to be notified.
        verify(mNotificationManager, never()).notify(eq(IncomingCallNotifier.NOTIFICATION_TAG),
                eq(IncomingCallNotifier.NOTIFICATION_INCOMING_CALL), any());;
    }
}