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 
17 package com.android.server.telecom.tests;
18 
19 import android.media.AudioManager;
20 import android.os.HandlerThread;
21 import android.test.suitebuilder.annotation.SmallTest;
22 
23 import com.android.server.telecom.CallAudioManager;
24 import com.android.server.telecom.CallAudioModeStateMachine;
25 import com.android.server.telecom.CallAudioModeStateMachine.MessageArgs;
26 import com.android.server.telecom.SystemStateHelper;
27 
28 import org.junit.After;
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.junit.runner.RunWith;
32 import org.junit.runners.Parameterized;
33 import org.mockito.Mock;
34 
35 import java.util.ArrayList;
36 import java.util.Collection;
37 import java.util.List;
38 
39 import static org.junit.Assert.assertEquals;
40 import static org.mockito.ArgumentMatchers.anyInt;
41 import static org.mockito.ArgumentMatchers.eq;
42 import static org.mockito.ArgumentMatchers.nullable;
43 import static org.mockito.Mockito.never;
44 import static org.mockito.Mockito.reset;
45 import static org.mockito.Mockito.verify;
46 import static org.mockito.Mockito.when;
47 
48 @RunWith(Parameterized.class)
49 public class CallAudioModeTransitionTests extends TelecomTestCase {
50     private static class ModeTestParameters {
51         public String name;
52         public int initialAudioState; // One of the explicit switch focus constants in CAMSM
53         public int messageType; // Any of the commands from the state machine
54         public CallAudioModeStateMachine.MessageArgs externalState;
55         public String expectedFinalStateName;
56         public int expectedFocus; // one of the FOCUS_* constants below
57         public int expectedMode; // NO_CHANGE, or an AudioManager.MODE_* constant
58         public int expectedRingingInteraction; // NO_CHANGE, ON, or OFF
59         public int expectedCallWaitingInteraction; // NO_CHANGE, ON, or OFF
60 
ModeTestParameters(String name, int initialAudioState, int messageType, CallAudioModeStateMachine.MessageArgs externalState, String expectedFinalStateName, int expectedFocus, int expectedMode, int expectedRingingInteraction, int expectedCallWaitingInteraction)61         public ModeTestParameters(String name, int initialAudioState, int messageType,
62                 CallAudioModeStateMachine.MessageArgs externalState, String
63                 expectedFinalStateName, int expectedFocus, int expectedMode, int
64                 expectedRingingInteraction, int expectedCallWaitingInteraction) {
65             this.name = name;
66             this.initialAudioState = initialAudioState;
67             this.messageType = messageType;
68             this.externalState = externalState;
69             this.expectedFinalStateName = expectedFinalStateName;
70             this.expectedFocus = expectedFocus;
71             this.expectedMode = expectedMode;
72             this.expectedRingingInteraction = expectedRingingInteraction;
73             this.expectedCallWaitingInteraction = expectedCallWaitingInteraction;
74         }
75 
76         @Override
toString()77         public String toString() {
78             return "ModeTestParameters{" +
79                     "name='" + name + '\'' +
80                     ", initialAudioState=" + initialAudioState +
81                     ", messageType=" + messageType +
82                     ", externalState=" + externalState +
83                     ", expectedFinalStateName='" + expectedFinalStateName + '\'' +
84                     ", expectedFocus=" + expectedFocus +
85                     ", expectedMode=" + expectedMode +
86                     ", expectedRingingInteraction=" + expectedRingingInteraction +
87                     ", expectedCallWaitingInteraction=" + expectedCallWaitingInteraction +
88                     '}';
89         }
90     }
91 
92     private static final int FOCUS_NO_CHANGE = 0;
93     private static final int FOCUS_RING = 1;
94     private static final int FOCUS_VOICE = 2;
95     private static final int FOCUS_OFF = 3;
96 
97     private static final int NO_CHANGE = -1;
98     private static final int ON = 0;
99     private static final int OFF = 1;
100 
101     private static final int TEST_TIMEOUT = 1000;
102 
103     @Mock private SystemStateHelper mSystemStateHelper;
104     @Mock private AudioManager mAudioManager;
105     @Mock private CallAudioManager mCallAudioManager;
106     private final ModeTestParameters mParams;
107     private HandlerThread mTestThread;
108 
109     @Override
110     @Before
setUp()111     public void setUp() throws Exception {
112         mTestThread = new HandlerThread("CallAudioModeStateMachineTest");
113         mTestThread.start();
114         super.setUp();
115     }
116 
117     @Override
118     @After
tearDown()119     public void tearDown() throws Exception {
120         mTestThread.quit();
121         mTestThread.join();
122         super.tearDown();
123     }
124 
CallAudioModeTransitionTests(ModeTestParameters params)125     public CallAudioModeTransitionTests(ModeTestParameters params) {
126         mParams = params;
127     }
128 
129     @Test
130     @SmallTest
modeTransitionTest()131     public void modeTransitionTest() {
132         CallAudioModeStateMachine sm = new CallAudioModeStateMachine(mSystemStateHelper,
133                 mAudioManager, mTestThread.getLooper());
134         sm.setCallAudioManager(mCallAudioManager);
135         sm.sendMessage(mParams.initialAudioState);
136         waitForHandlerAction(sm.getHandler(), TEST_TIMEOUT);
137 
138         resetMocks();
139         when(mCallAudioManager.startRinging()).thenReturn(true);
140         if (mParams.initialAudioState
141                 == CallAudioModeStateMachine.ENTER_AUDIO_PROCESSING_FOCUS_FOR_TESTING) {
142             when(mAudioManager.getMode())
143                     .thenReturn(CallAudioModeStateMachine.NEW_AUDIO_MODE_FOR_AUDIO_PROCESSING);
144         }
145 
146         sm.sendMessage(mParams.messageType, mParams.externalState);
147         waitForHandlerAction(sm.getHandler(), TEST_TIMEOUT);
148         if (mParams.expectedFocus == FOCUS_OFF
149                 && mParams.messageType != CallAudioModeStateMachine.AUDIO_OPERATIONS_COMPLETE) {
150             // If we expect the focus to turn off, we need to signal operations complete first
151             sm.sendMessage(CallAudioModeStateMachine.AUDIO_OPERATIONS_COMPLETE);
152             waitForHandlerAction(sm.getHandler(), TEST_TIMEOUT);
153         }
154 
155         assertEquals(mParams.expectedFinalStateName, sm.getCurrentStateName());
156 
157         switch (mParams.expectedFocus) {
158             case FOCUS_NO_CHANGE:
159                 verify(mAudioManager, never()).requestAudioFocusForCall(anyInt(), anyInt());
160                 break;
161             case FOCUS_OFF:
162                 verify(mAudioManager).abandonAudioFocusForCall();
163                 break;
164             case FOCUS_RING:
165                 verify(mAudioManager).requestAudioFocusForCall(
166                         eq(AudioManager.STREAM_RING), anyInt());
167                 break;
168             case FOCUS_VOICE:
169                 verify(mAudioManager).requestAudioFocusForCall(
170                         eq(AudioManager.STREAM_VOICE_CALL), anyInt());
171                 break;
172         }
173 
174         if (mParams.expectedMode != NO_CHANGE) {
175             verify(mAudioManager).setMode(eq(mParams.expectedMode));
176         } else {
177             verify(mAudioManager, never()).setMode(anyInt());
178         }
179 
180         switch (mParams.expectedRingingInteraction) {
181             case NO_CHANGE:
182                 verify(mCallAudioManager, never()).startRinging();
183                 verify(mCallAudioManager, never()).stopRinging();
184                 break;
185             case ON:
186                 verify(mCallAudioManager).startRinging();
187                 break;
188             case OFF:
189                 verify(mCallAudioManager).stopRinging();
190                 break;
191         }
192 
193         switch (mParams.expectedCallWaitingInteraction) {
194             case NO_CHANGE:
195                 verify(mCallAudioManager, never()).startCallWaiting(nullable(String.class));
196                 verify(mCallAudioManager, never()).stopCallWaiting();
197                 break;
198             case ON:
199                 verify(mCallAudioManager).startCallWaiting(nullable(String.class));
200                 break;
201             case OFF:
202                 verify(mCallAudioManager).stopCallWaiting();
203                 break;
204         }
205     }
206 
207     @Parameterized.Parameters(name = "{0}")
generateTestCases()208     public static Collection<ModeTestParameters> generateTestCases() {
209         List<ModeTestParameters> result = new ArrayList<>();
210         result.add(new ModeTestParameters(
211                 "New active/dialing call with no other calls when unfocused",
212                 CallAudioModeStateMachine.ABANDON_FOCUS_FOR_TESTING, // initialAudioState
213                 CallAudioModeStateMachine.NEW_ACTIVE_OR_DIALING_CALL, // messageType
214                 new MessageArgs.Builder()
215                         .setHasActiveOrDialingCalls(true)
216                         .setHasRingingCalls(false)
217                         .setHasHoldingCalls(false)
218                         .setIsTonePlaying(false)
219                         .setForegroundCallIsVoip(false)
220                         .setSession(null)
221                         .build(),
222                 CallAudioModeStateMachine.CALL_STATE_NAME, // expectedFinalStateName
223                 FOCUS_VOICE, // expectedFocus
224                 AudioManager.MODE_IN_CALL, // expectedMode
225                 NO_CHANGE, // expectedRingingInteraction
226                 NO_CHANGE // expectedCallWaitingInteraction
227         ));
228 
229         result.add(new ModeTestParameters(
230                 "New active/dialing voip call with no other calls when unfocused",
231                 CallAudioModeStateMachine.ABANDON_FOCUS_FOR_TESTING, // initialAudioState
232                 CallAudioModeStateMachine.NEW_ACTIVE_OR_DIALING_CALL, // messageType
233                 new MessageArgs.Builder()
234                         .setHasActiveOrDialingCalls(true)
235                         .setHasRingingCalls(false)
236                         .setHasHoldingCalls(false)
237                         .setIsTonePlaying(false)
238                         .setForegroundCallIsVoip(true)
239                         .setSession(null)
240                         .build(),
241                 CallAudioModeStateMachine.COMMS_STATE_NAME, // expectedFinalStateName
242                 FOCUS_VOICE, // expectedFocus
243                 AudioManager.MODE_IN_COMMUNICATION, // expectedMode
244                 NO_CHANGE, // expectedRingingInteraction
245                 NO_CHANGE // expectedCallWaitingInteraction
246         ));
247 
248         result.add(new ModeTestParameters(
249                 "New ringing call with no other calls when unfocused",
250                 CallAudioModeStateMachine.ABANDON_FOCUS_FOR_TESTING, // initialAudioState
251                 CallAudioModeStateMachine.NEW_RINGING_CALL, // messageType
252                 new MessageArgs.Builder()
253                         .setHasActiveOrDialingCalls(false)
254                         .setHasRingingCalls(true)
255                         .setHasHoldingCalls(false)
256                         .setIsTonePlaying(false)
257                         .setForegroundCallIsVoip(false)
258                         .setSession(null)
259                         .build(),
260                 CallAudioModeStateMachine.RING_STATE_NAME, // expectedFinalStateName
261                 FOCUS_RING, // expectedFocus
262                 AudioManager.MODE_RINGTONE, // expectedMode
263                 ON, // expectedRingingInteraction
264                 OFF // expectedCallWaitingInteraction
265         ));
266 
267         result.add(new ModeTestParameters(
268                 "New ringing call coming in on top of active/dialing call",
269                 CallAudioModeStateMachine.ENTER_CALL_FOCUS_FOR_TESTING, // initialAudioState
270                 CallAudioModeStateMachine.NEW_RINGING_CALL, // messageType
271                 new MessageArgs.Builder()
272                         .setHasActiveOrDialingCalls(true)
273                         .setHasRingingCalls(true)
274                         .setHasHoldingCalls(false)
275                         .setIsTonePlaying(false)
276                         .setForegroundCallIsVoip(false)
277                         .setSession(null)
278                         .build(),
279                 CallAudioModeStateMachine.CALL_STATE_NAME, // expectedFinalStateName
280                 NO_CHANGE, // expectedFocus
281                 NO_CHANGE, // expectedMode
282                 NO_CHANGE, // expectedRingingInteraction
283                 ON // expectedCallWaitingInteraction
284         ));
285 
286         result.add(new ModeTestParameters(
287                 "Ringing call becomes active, part 1",
288                 CallAudioModeStateMachine.ENTER_RING_FOCUS_FOR_TESTING, // initialAudioState
289                 CallAudioModeStateMachine.NEW_ACTIVE_OR_DIALING_CALL, // messageType
290                 new MessageArgs.Builder()
291                         .setHasActiveOrDialingCalls(true)
292                         .setHasRingingCalls(false)
293                         .setHasHoldingCalls(false)
294                         .setIsTonePlaying(false)
295                         .setForegroundCallIsVoip(false)
296                         .setSession(null)
297                         .build(),
298                 CallAudioModeStateMachine.CALL_STATE_NAME, // expectedFinalStateName
299                 FOCUS_VOICE, // expectedFocus
300                 AudioManager.MODE_IN_CALL, // expectedMode
301                 OFF, // expectedRingingInteraction
302                 NO_CHANGE // expectedCallWaitingInteraction
303         ));
304 
305         result.add(new ModeTestParameters(
306                 "Ringing call becomes active, part 2",
307                 CallAudioModeStateMachine.ENTER_CALL_FOCUS_FOR_TESTING, // initialAudioState
308                 CallAudioModeStateMachine.NO_MORE_RINGING_CALLS, // messageType
309                 new MessageArgs.Builder()
310                         .setHasActiveOrDialingCalls(true)
311                         .setHasRingingCalls(false)
312                         .setHasHoldingCalls(false)
313                         .setIsTonePlaying(false)
314                         .setForegroundCallIsVoip(false)
315                         .setSession(null)
316                         .build(),
317                 CallAudioModeStateMachine.CALL_STATE_NAME, // expectedFinalStateName
318                 NO_CHANGE, // expectedFocus
319                 NO_CHANGE, // expectedMode
320                 NO_CHANGE, // expectedRingingInteraction
321                 NO_CHANGE // expectedCallWaitingInteraction
322         ));
323 
324         result.add(new ModeTestParameters(
325                 "Active call disconnects, but tone is playing",
326                 CallAudioModeStateMachine.ENTER_CALL_FOCUS_FOR_TESTING, // initialAudioState
327                 CallAudioModeStateMachine.NO_MORE_ACTIVE_OR_DIALING_CALLS, // messageType
328                 new MessageArgs.Builder()
329                         .setHasActiveOrDialingCalls(false)
330                         .setHasRingingCalls(false)
331                         .setHasHoldingCalls(false)
332                         .setIsTonePlaying(true)
333                         .setForegroundCallIsVoip(false)
334                         .setSession(null)
335                         .build(),
336                 CallAudioModeStateMachine.TONE_HOLD_STATE_NAME, // expectedFinalStateName
337                 FOCUS_VOICE, // expectedFocus
338                 AudioManager.MODE_IN_CALL, // expectedMode
339                 NO_CHANGE, // expectedRingingInteraction
340                 NO_CHANGE // expectedCallWaitingInteraction
341         ));
342 
343         result.add(new ModeTestParameters(
344                 "Tone stops playing, with no active calls",
345                 CallAudioModeStateMachine.ENTER_TONE_OR_HOLD_FOCUS_FOR_TESTING, // initialAudioState
346                 CallAudioModeStateMachine.TONE_STOPPED_PLAYING, // messageType
347                 new MessageArgs.Builder()
348                         .setHasActiveOrDialingCalls(false)
349                         .setHasRingingCalls(false)
350                         .setHasHoldingCalls(false)
351                         .setIsTonePlaying(false)
352                         .setForegroundCallIsVoip(false)
353                         .setSession(null)
354                         .build(),
355                 CallAudioModeStateMachine.UNFOCUSED_STATE_NAME, // expectedFinalStateName
356                 FOCUS_OFF, // expectedFocus
357                 AudioManager.MODE_NORMAL, // expectedMode
358                 NO_CHANGE, // expectedRingingInteraction
359                 NO_CHANGE // expectedCallWaitingInteraction
360         ));
361 
362         result.add(new ModeTestParameters(
363                 "Ringing call disconnects",
364                 CallAudioModeStateMachine.ENTER_RING_FOCUS_FOR_TESTING, // initialAudioState
365                 CallAudioModeStateMachine.NO_MORE_RINGING_CALLS, // messageType
366                 new MessageArgs.Builder()
367                         .setHasActiveOrDialingCalls(false)
368                         .setHasRingingCalls(false)
369                         .setHasHoldingCalls(false)
370                         .setIsTonePlaying(false)
371                         .setForegroundCallIsVoip(false)
372                         .setSession(null)
373                         .build(),
374                 CallAudioModeStateMachine.UNFOCUSED_STATE_NAME, // expectedFinalStateName
375                 FOCUS_OFF, // expectedFocus
376                 AudioManager.MODE_NORMAL, // expectedMode
377                 OFF, // expectedRingingInteraction
378                 NO_CHANGE // expectedCallWaitingInteraction
379         ));
380 
381         result.add(new ModeTestParameters(
382                 "Call-waiting call disconnects",
383                 CallAudioModeStateMachine.ENTER_CALL_FOCUS_FOR_TESTING, // initialAudioState
384                 CallAudioModeStateMachine.NO_MORE_RINGING_CALLS, // messageType
385                 new MessageArgs.Builder()
386                         .setHasActiveOrDialingCalls(true)
387                         .setHasRingingCalls(false)
388                         .setHasHoldingCalls(false)
389                         .setIsTonePlaying(true)
390                         .setForegroundCallIsVoip(false)
391                         .setSession(null)
392                         .build(),
393                 CallAudioModeStateMachine.CALL_STATE_NAME, // expectedFinalStateName
394                 FOCUS_NO_CHANGE, // expectedFocus
395                 NO_CHANGE, // expectedMode
396                 NO_CHANGE, // expectedRingingInteraction
397                 OFF // expectedCallWaitingInteraction
398         ));
399 
400         result.add(new ModeTestParameters(
401                 "Call is placed on hold - 1",
402                 CallAudioModeStateMachine.ENTER_CALL_FOCUS_FOR_TESTING, // initialAudioState
403                 CallAudioModeStateMachine.NO_MORE_ACTIVE_OR_DIALING_CALLS, // messageType
404                 new MessageArgs.Builder()
405                         .setHasActiveOrDialingCalls(false)
406                         .setHasRingingCalls(false)
407                         .setHasHoldingCalls(true)
408                         .setIsTonePlaying(false)
409                         .setForegroundCallIsVoip(false)
410                         .setSession(null)
411                         .build(),
412                 CallAudioModeStateMachine.TONE_HOLD_STATE_NAME, // expectedFinalStateName
413                 FOCUS_VOICE, // expectedFocus
414                 AudioManager.MODE_IN_CALL, // expectedMode
415                 NO_CHANGE, // expectedRingingInteraction
416                 NO_CHANGE // expectedCallWaitingInteraction
417         ));
418 
419         result.add(new ModeTestParameters(
420                 "Call is placed on hold - 2",
421                 CallAudioModeStateMachine.ENTER_TONE_OR_HOLD_FOCUS_FOR_TESTING, // initialAudioState
422                 CallAudioModeStateMachine.NEW_HOLDING_CALL, // messageType
423                 new MessageArgs.Builder()
424                         .setHasActiveOrDialingCalls(false)
425                         .setHasRingingCalls(false)
426                         .setHasHoldingCalls(true)
427                         .setIsTonePlaying(false)
428                         .setForegroundCallIsVoip(false)
429                         .setSession(null)
430                         .build(),
431                 CallAudioModeStateMachine.TONE_HOLD_STATE_NAME, // expectedFinalStateName
432                 FOCUS_NO_CHANGE, // expectedFocus
433                 NO_CHANGE, // expectedMode
434                 NO_CHANGE, // expectedRingingInteraction
435                 NO_CHANGE // expectedCallWaitingInteraction
436         ));
437 
438         result.add(new ModeTestParameters(
439                 "Swap between voip and sim calls - 1",
440                 CallAudioModeStateMachine.ENTER_CALL_FOCUS_FOR_TESTING, // initialAudioState
441                 CallAudioModeStateMachine.NEW_HOLDING_CALL, // messageType
442                 new MessageArgs.Builder()
443                         .setHasActiveOrDialingCalls(false)
444                         .setHasRingingCalls(false)
445                         .setHasHoldingCalls(true)
446                         .setIsTonePlaying(false)
447                         .setForegroundCallIsVoip(true)
448                         .setSession(null)
449                         .build(),
450                 CallAudioModeStateMachine.COMMS_STATE_NAME, // expectedFinalStateName
451                 FOCUS_VOICE, // expectedFocus
452                 AudioManager.MODE_IN_COMMUNICATION, // expectedMode
453                 NO_CHANGE, // expectedRingingInteraction
454                 NO_CHANGE // expectedCallWaitingInteraction
455         ));
456 
457         result.add(new ModeTestParameters(
458                 "Swap between voip and sim calls - 2",
459                 CallAudioModeStateMachine.ENTER_COMMS_FOCUS_FOR_TESTING, // initialAudioState
460                 CallAudioModeStateMachine.NEW_HOLDING_CALL, // messageType
461                 new MessageArgs.Builder()
462                         .setHasActiveOrDialingCalls(false)
463                         .setHasRingingCalls(false)
464                         .setHasHoldingCalls(true)
465                         .setIsTonePlaying(false)
466                         .setForegroundCallIsVoip(false)
467                         .setSession(null)
468                         .build(),
469                 CallAudioModeStateMachine.CALL_STATE_NAME, // expectedFinalStateName
470                 FOCUS_VOICE, // expectedFocus
471                 AudioManager.MODE_IN_CALL, // expectedMode
472                 NO_CHANGE, // expectedRingingInteraction
473                 NO_CHANGE // expectedCallWaitingInteraction
474         ));
475 
476         result.add(new ModeTestParameters(
477                 "Swap between voip and sim calls - 3",
478                 CallAudioModeStateMachine.ENTER_COMMS_FOCUS_FOR_TESTING, // initialAudioState
479                 CallAudioModeStateMachine.NEW_ACTIVE_OR_DIALING_CALL, // messageType
480                 new MessageArgs.Builder()
481                         .setHasActiveOrDialingCalls(false)
482                         .setHasRingingCalls(false)
483                         .setHasHoldingCalls(true)
484                         .setIsTonePlaying(false)
485                         .setForegroundCallIsVoip(false)
486                         .setSession(null)
487                         .build(),
488                 CallAudioModeStateMachine.CALL_STATE_NAME, // expectedFinalStateName
489                 FOCUS_VOICE, // expectedFocus
490                 AudioManager.MODE_IN_CALL, // expectedMode
491                 NO_CHANGE, // expectedRingingInteraction
492                 NO_CHANGE // expectedCallWaitingInteraction
493         ));
494 
495         result.add(new ModeTestParameters(
496                 "Swap between voip and sim calls - 4",
497                 CallAudioModeStateMachine.ENTER_CALL_FOCUS_FOR_TESTING, // initialAudioState
498                 CallAudioModeStateMachine.NEW_HOLDING_CALL, // messageType
499                 new MessageArgs.Builder()
500                         .setHasActiveOrDialingCalls(false)
501                         .setHasRingingCalls(false)
502                         .setHasHoldingCalls(true)
503                         .setIsTonePlaying(false)
504                         .setForegroundCallIsVoip(true)
505                         .setSession(null)
506                         .build(),
507                 CallAudioModeStateMachine.COMMS_STATE_NAME, // expectedFinalStateName
508                 FOCUS_VOICE, // expectedFocus
509                 AudioManager.MODE_IN_COMMUNICATION, // expectedMode
510                 NO_CHANGE, // expectedRingingInteraction
511                 NO_CHANGE // expectedCallWaitingInteraction
512         ));
513 
514         result.add(new ModeTestParameters(
515                 "Call is taken off hold - 1",
516                 CallAudioModeStateMachine.ENTER_TONE_OR_HOLD_FOCUS_FOR_TESTING, // initialAudioState
517                 CallAudioModeStateMachine.NO_MORE_HOLDING_CALLS, // messageType
518                 new MessageArgs.Builder()
519                         .setHasActiveOrDialingCalls(true)
520                         .setHasRingingCalls(false)
521                         .setHasHoldingCalls(false)
522                         .setIsTonePlaying(false)
523                         .setForegroundCallIsVoip(false)
524                         .setSession(null)
525                         .build(),
526                 CallAudioModeStateMachine.CALL_STATE_NAME, // expectedFinalStateName
527                 FOCUS_VOICE, // expectedFocus
528                 AudioManager.MODE_IN_CALL, // expectedMode
529                 NO_CHANGE, // expectedRingingInteraction
530                 NO_CHANGE // expectedCallWaitingInteraction
531         ));
532 
533         result.add(new ModeTestParameters(
534                 "Call is taken off hold - 2",
535                 CallAudioModeStateMachine.ENTER_CALL_FOCUS_FOR_TESTING, // initialAudioState
536                 CallAudioModeStateMachine.NEW_ACTIVE_OR_DIALING_CALL, // messageType
537                 new MessageArgs.Builder()
538                         .setHasActiveOrDialingCalls(true)
539                         .setHasRingingCalls(false)
540                         .setHasHoldingCalls(false)
541                         .setIsTonePlaying(false)
542                         .setForegroundCallIsVoip(false)
543                         .setSession(null)
544                         .build(),
545                 CallAudioModeStateMachine.CALL_STATE_NAME, // expectedFinalStateName
546                 FOCUS_NO_CHANGE, // expectedFocus
547                 NO_CHANGE, // expectedMode
548                 NO_CHANGE, // expectedRingingInteraction
549                 NO_CHANGE // expectedCallWaitingInteraction
550         ));
551 
552         result.add(new ModeTestParameters(
553                 "Active call disconnects while there's a call-waiting call",
554                 CallAudioModeStateMachine.ENTER_CALL_FOCUS_FOR_TESTING, // initialAudioState
555                 CallAudioModeStateMachine.NO_MORE_ACTIVE_OR_DIALING_CALLS, // messageType
556                 new MessageArgs.Builder()
557                         .setHasActiveOrDialingCalls(false)
558                         .setHasRingingCalls(true)
559                         .setHasHoldingCalls(false)
560                         .setIsTonePlaying(true)
561                         .setForegroundCallIsVoip(false)
562                         .setSession(null)
563                         .build(),
564                 CallAudioModeStateMachine.RING_STATE_NAME, // expectedFinalStateName
565                 FOCUS_RING, // expectedFocus
566                 AudioManager.MODE_RINGTONE, // expectedMode
567                 ON, // expectedRingingInteraction
568                 OFF // expectedCallWaitingInteraction
569         ));
570 
571         result.add(new ModeTestParameters(
572                 "New dialing call when there's a call on hold",
573                 CallAudioModeStateMachine.ENTER_TONE_OR_HOLD_FOCUS_FOR_TESTING, // initialAudioState
574                 CallAudioModeStateMachine.NEW_ACTIVE_OR_DIALING_CALL, // messageType
575                 new MessageArgs.Builder()
576                         .setHasActiveOrDialingCalls(true)
577                         .setHasRingingCalls(false)
578                         .setHasHoldingCalls(true)
579                         .setIsTonePlaying(false)
580                         .setForegroundCallIsVoip(false)
581                         .setSession(null)
582                         .build(),
583                 CallAudioModeStateMachine.CALL_STATE_NAME, // expectedFinalStateName
584                 FOCUS_VOICE, // expectedFocus
585                 AudioManager.MODE_IN_CALL, // expectedMode
586                 NO_CHANGE, // expectedRingingInteraction
587                 NO_CHANGE // expectedCallWaitingInteraction
588         ));
589 
590         result.add(new ModeTestParameters(
591                 "Ringing call disconnects with a holding call in the background",
592                 CallAudioModeStateMachine.ENTER_RING_FOCUS_FOR_TESTING, // initialAudioState
593                 CallAudioModeStateMachine.NO_MORE_RINGING_CALLS, // messageType
594                 new MessageArgs.Builder()
595                         .setHasActiveOrDialingCalls(false)
596                         .setHasRingingCalls(false)
597                         .setHasHoldingCalls(true)
598                         .setIsTonePlaying(false)
599                         .setForegroundCallIsVoip(false)
600                         .setSession(null)
601                         .build(),
602                 CallAudioModeStateMachine.TONE_HOLD_STATE_NAME, // expectedFinalStateName
603                 FOCUS_VOICE, // expectedFocus
604                 AudioManager.MODE_NORMAL, // expectedMode -- we're expecting this because
605                                           // mMostRecentMode hasn't been set properly.
606                 OFF, // expectedRingingInteraction
607                 NO_CHANGE // expectedCallWaitingInteraction
608         ));
609 
610         result.add(new ModeTestParameters(
611                 "Foreground call transitions from sim to voip",
612                 CallAudioModeStateMachine.ENTER_CALL_FOCUS_FOR_TESTING, // initialAudioState
613                 CallAudioModeStateMachine.FOREGROUND_VOIP_MODE_CHANGE, // messageType
614                 new MessageArgs.Builder()
615                         .setHasActiveOrDialingCalls(true)
616                         .setHasRingingCalls(false)
617                         .setHasHoldingCalls(false)
618                         .setIsTonePlaying(false)
619                         .setForegroundCallIsVoip(true)
620                         .setSession(null)
621                         .build(),
622                 CallAudioModeStateMachine.COMMS_STATE_NAME, // expectedFinalStateName
623                 FOCUS_VOICE, // expectedFocus
624                 AudioManager.MODE_IN_COMMUNICATION, // expectedMode
625                 NO_CHANGE, // expectedRingingInteraction
626                 NO_CHANGE // expectedCallWaitingInteraction
627         ));
628 
629         result.add(new ModeTestParameters(
630                 "Foreground call transitions from voip to sim",
631                 CallAudioModeStateMachine.ENTER_COMMS_FOCUS_FOR_TESTING, // initialAudioState
632                 CallAudioModeStateMachine.FOREGROUND_VOIP_MODE_CHANGE, // messageType
633                 new MessageArgs.Builder()
634                         .setHasActiveOrDialingCalls(true)
635                         .setHasRingingCalls(false)
636                         .setHasHoldingCalls(false)
637                         .setIsTonePlaying(false)
638                         .setForegroundCallIsVoip(false)
639                         .setSession(null)
640                         .build(),
641                 CallAudioModeStateMachine.CALL_STATE_NAME, // expectedFinalStateName
642                 FOCUS_VOICE, // expectedFocus
643                 AudioManager.MODE_IN_CALL, // expectedMode
644                 NO_CHANGE, // expectedRingingInteraction
645                 NO_CHANGE // expectedCallWaitingInteraction
646         ));
647 
648         result.add(new ModeTestParameters(
649                 "Call-waiting hangs up before being answered, with another sim call in " +
650                         "foreground",
651                 CallAudioModeStateMachine.ENTER_CALL_FOCUS_FOR_TESTING, // initialAudioState
652                 CallAudioModeStateMachine.NO_MORE_RINGING_CALLS, // messageType
653                 new MessageArgs.Builder()
654                         .setHasActiveOrDialingCalls(true)
655                         .setHasRingingCalls(false)
656                         .setHasHoldingCalls(false)
657                         .setIsTonePlaying(true)
658                         .setForegroundCallIsVoip(false)
659                         .setSession(null)
660                         .build(),
661                 CallAudioModeStateMachine.CALL_STATE_NAME, // expectedFinalStateName
662                 FOCUS_NO_CHANGE, // expectedFocus
663                 NO_CHANGE, // expectedMode
664                 NO_CHANGE, // expectedRingingInteraction
665                 OFF // expectedCallWaitingInteraction
666         ));
667 
668         result.add(new ModeTestParameters(
669                 "Call-waiting hangs up before being answered, with another voip call in " +
670                         "foreground",
671                 CallAudioModeStateMachine.ENTER_COMMS_FOCUS_FOR_TESTING, // initialAudioState
672                 CallAudioModeStateMachine.NO_MORE_RINGING_CALLS, // messageType
673                 new MessageArgs.Builder()
674                         .setHasActiveOrDialingCalls(true)
675                         .setHasRingingCalls(false)
676                         .setHasHoldingCalls(false)
677                         .setIsTonePlaying(true)
678                         .setForegroundCallIsVoip(true)
679                         .setSession(null)
680                         .build(),
681                 CallAudioModeStateMachine.COMMS_STATE_NAME, // expectedFinalStateName
682                 FOCUS_NO_CHANGE, // expectedFocus
683                 NO_CHANGE, // expectedMode
684                 NO_CHANGE, // expectedRingingInteraction
685                 OFF // expectedCallWaitingInteraction
686         ));
687 
688         result.add(new ModeTestParameters(
689                 "Call enters audio processing state from call screening service",
690                 CallAudioModeStateMachine.ABANDON_FOCUS_FOR_TESTING, // initialAudioState
691                 CallAudioModeStateMachine.NEW_AUDIO_PROCESSING_CALL, // messageType
692                 new MessageArgs.Builder()
693                         .setHasActiveOrDialingCalls(false)
694                         .setHasRingingCalls(false)
695                         .setHasHoldingCalls(false)
696                         .setHasAudioProcessingCalls(true)
697                         .setIsTonePlaying(false)
698                         .setForegroundCallIsVoip(false)
699                         .setSession(null)
700                         .build(),
701                 CallAudioModeStateMachine.AUDIO_PROCESSING_STATE_NAME, // expectedFinalStateName
702                 FOCUS_NO_CHANGE, // expectedFocus
703                 CallAudioModeStateMachine.NEW_AUDIO_MODE_FOR_AUDIO_PROCESSING, // expectedMode
704                 NO_CHANGE, // expectedRingingInteraction
705                 NO_CHANGE // expectedCallWaitingInteraction
706         ));
707 
708         result.add(new ModeTestParameters(
709                 "Call enters audio processing state by manual intervention from ringing state, 1",
710                 CallAudioModeStateMachine.ENTER_RING_FOCUS_FOR_TESTING, // initialAudioState
711                 CallAudioModeStateMachine.NO_MORE_RINGING_CALLS, // messageType
712                 new MessageArgs.Builder()
713                         .setHasActiveOrDialingCalls(false)
714                         .setHasRingingCalls(false)
715                         .setHasHoldingCalls(false)
716                         .setHasAudioProcessingCalls(true)
717                         .setIsTonePlaying(false)
718                         .setForegroundCallIsVoip(false)
719                         .setSession(null)
720                         .build(),
721                 CallAudioModeStateMachine.AUDIO_PROCESSING_STATE_NAME, // expectedFinalStateName
722                 FOCUS_OFF, // expectedFocus
723                 CallAudioModeStateMachine.NEW_AUDIO_MODE_FOR_AUDIO_PROCESSING, // expectedMode
724                 OFF, // expectedRingingInteraction
725                 NO_CHANGE // expectedCallWaitingInteraction
726         ));
727 
728         result.add(new ModeTestParameters(
729                 "Call enters audio processing state by manual intervention from ringing state, 2",
730                 CallAudioModeStateMachine.ENTER_RING_FOCUS_FOR_TESTING, // initialAudioState
731                 CallAudioModeStateMachine.NEW_AUDIO_PROCESSING_CALL, // messageType
732                 new MessageArgs.Builder()
733                         .setHasActiveOrDialingCalls(false)
734                         .setHasRingingCalls(false)
735                         .setHasHoldingCalls(false)
736                         .setHasAudioProcessingCalls(true)
737                         .setIsTonePlaying(false)
738                         .setForegroundCallIsVoip(false)
739                         .setSession(null)
740                         .build(),
741                 CallAudioModeStateMachine.AUDIO_PROCESSING_STATE_NAME, // expectedFinalStateName
742                 FOCUS_OFF, // expectedFocus
743                 CallAudioModeStateMachine.NEW_AUDIO_MODE_FOR_AUDIO_PROCESSING, // expectedMode
744                 OFF, // expectedRingingInteraction
745                 NO_CHANGE // expectedCallWaitingInteraction
746         ));
747 
748         result.add(new ModeTestParameters(
749                 "Call enters audio processing state from active call, 1",
750                 CallAudioModeStateMachine.ENTER_CALL_FOCUS_FOR_TESTING, // initialAudioState
751                 CallAudioModeStateMachine.NO_MORE_ACTIVE_OR_DIALING_CALLS, // messageType
752                 new MessageArgs.Builder()
753                         .setHasActiveOrDialingCalls(false)
754                         .setHasRingingCalls(false)
755                         .setHasHoldingCalls(false)
756                         .setHasAudioProcessingCalls(true)
757                         .setIsTonePlaying(false)
758                         .setForegroundCallIsVoip(false)
759                         .setSession(null)
760                         .build(),
761                 CallAudioModeStateMachine.AUDIO_PROCESSING_STATE_NAME, // expectedFinalStateName
762                 FOCUS_OFF, // expectedFocus
763                 CallAudioModeStateMachine.NEW_AUDIO_MODE_FOR_AUDIO_PROCESSING, // expectedMode
764                 NO_CHANGE, // expectedRingingInteraction
765                 NO_CHANGE // expectedCallWaitingInteraction
766         ));
767 
768         result.add(new ModeTestParameters(
769                 "Call enters audio processing state from active call, 2",
770                 CallAudioModeStateMachine.ENTER_CALL_FOCUS_FOR_TESTING, // initialAudioState
771                 CallAudioModeStateMachine.NEW_AUDIO_PROCESSING_CALL, // messageType
772                 new MessageArgs.Builder()
773                         .setHasActiveOrDialingCalls(false)
774                         .setHasRingingCalls(false)
775                         .setHasHoldingCalls(false)
776                         .setHasAudioProcessingCalls(true)
777                         .setIsTonePlaying(false)
778                         .setForegroundCallIsVoip(false)
779                         .setSession(null)
780                         .build(),
781                 CallAudioModeStateMachine.AUDIO_PROCESSING_STATE_NAME, // expectedFinalStateName
782                 FOCUS_OFF, // expectedFocus
783                 CallAudioModeStateMachine.NEW_AUDIO_MODE_FOR_AUDIO_PROCESSING, // expectedMode
784                 NO_CHANGE, // expectedRingingInteraction
785                 NO_CHANGE // expectedCallWaitingInteraction
786         ));
787 
788         result.add(new ModeTestParameters(
789                 "Call in audio processing gets hanged up",
790                 CallAudioModeStateMachine.ENTER_AUDIO_PROCESSING_FOCUS_FOR_TESTING, // initialAudioS
791                 CallAudioModeStateMachine.NO_MORE_AUDIO_PROCESSING_CALLS, // messageType
792                 new MessageArgs.Builder()
793                         .setHasActiveOrDialingCalls(false)
794                         .setHasRingingCalls(false)
795                         .setHasHoldingCalls(false)
796                         .setHasAudioProcessingCalls(false)
797                         .setIsTonePlaying(false)
798                         .setForegroundCallIsVoip(false)
799                         .setSession(null)
800                         .build(),
801                 CallAudioModeStateMachine.UNFOCUSED_STATE_NAME, // expectedFinalStateName
802                 NO_CHANGE, // expectedFocus
803                 AudioManager.MODE_NORMAL, // expectedMode
804                 NO_CHANGE, // expectedRingingInteraction
805                 NO_CHANGE // expectedCallWaitingInteraction
806         ));
807 
808         result.add(new ModeTestParameters(
809                 "Notify user of a call in audio processing by simulating ringing, 1",
810                 CallAudioModeStateMachine.ENTER_AUDIO_PROCESSING_FOCUS_FOR_TESTING, // initialAudioS
811                 CallAudioModeStateMachine.NO_MORE_AUDIO_PROCESSING_CALLS, // messageType
812                 new MessageArgs.Builder()
813                         .setHasActiveOrDialingCalls(false)
814                         .setHasRingingCalls(true)
815                         .setHasHoldingCalls(false)
816                         .setHasAudioProcessingCalls(false)
817                         .setIsTonePlaying(false)
818                         .setForegroundCallIsVoip(false)
819                         .setSession(null)
820                         .build(),
821                 CallAudioModeStateMachine.RING_STATE_NAME, // expectedFinalStateName
822                 FOCUS_RING, // expectedFocus
823                 NO_CHANGE, // expectedMode
824                 ON, // expectedRingingInteraction
825                 // We expect a call to stopCallWaiting because it happens whenever the ringer starts
826                 OFF // expectedCallWaitingInteraction
827         ));
828 
829         result.add(new ModeTestParameters(
830                 "Notify user of a call in audio processing by simulating ringing, 2",
831                 CallAudioModeStateMachine.ENTER_AUDIO_PROCESSING_FOCUS_FOR_TESTING, // initialAudioS
832                 CallAudioModeStateMachine.NEW_RINGING_CALL, // messageType
833                 new MessageArgs.Builder()
834                         .setHasActiveOrDialingCalls(false)
835                         .setHasRingingCalls(true)
836                         .setHasHoldingCalls(false)
837                         .setHasAudioProcessingCalls(false)
838                         .setIsTonePlaying(false)
839                         .setForegroundCallIsVoip(false)
840                         .setSession(null)
841                         .build(),
842                 CallAudioModeStateMachine.RING_STATE_NAME, // expectedFinalStateName
843                 FOCUS_RING, // expectedFocus
844                 NO_CHANGE, // expectedMode
845                 ON, // expectedRingingInteraction
846                 // We expect a call to stopCallWaiting because it happens whenever the ringer starts
847                 OFF // expectedCallWaitingInteraction
848         ));
849 
850         result.add(new ModeTestParameters(
851                 "Audio processing call gets set to active manually",
852                 CallAudioModeStateMachine.ENTER_AUDIO_PROCESSING_FOCUS_FOR_TESTING, // initialAudioS
853                 CallAudioModeStateMachine.NO_MORE_AUDIO_PROCESSING_CALLS, // messageType
854                 new MessageArgs.Builder()
855                         .setHasActiveOrDialingCalls(true)
856                         .setHasRingingCalls(false)
857                         .setHasHoldingCalls(false)
858                         .setHasAudioProcessingCalls(false)
859                         .setIsTonePlaying(false)
860                         .setForegroundCallIsVoip(false)
861                         .setSession(null)
862                         .build(),
863                 CallAudioModeStateMachine.CALL_STATE_NAME, // expectedFinalStateName
864                 FOCUS_VOICE, // expectedFocus
865                 AudioManager.MODE_IN_CALL, // expectedMode
866                 NO_CHANGE, // expectedRingingInteraction
867                 NO_CHANGE // expectedCallWaitingInteraction
868         ));
869 
870         result.add(new ModeTestParameters(
871                 "No change to focus without signaling audio ops complete",
872                 CallAudioModeStateMachine.ENTER_TONE_OR_HOLD_FOCUS_FOR_TESTING, // initialAudioS
873                 CallAudioModeStateMachine.TONE_STOPPED_PLAYING, // messageType
874                 new MessageArgs.Builder()
875                         .setHasActiveOrDialingCalls(false)
876                         .setHasRingingCalls(false)
877                         .setHasHoldingCalls(false)
878                         .setHasAudioProcessingCalls(false)
879                         .setIsTonePlaying(false)
880                         .setForegroundCallIsVoip(false)
881                         .setSession(null)
882                         .build(),
883                 CallAudioModeStateMachine.UNFOCUSED_STATE_NAME, // expectedFinalStateName
884                 FOCUS_NO_CHANGE, // expectedFocus
885                 AudioManager.MODE_NORMAL, // expectedMode
886                 NO_CHANGE, // expectedRingingInteraction
887                 NO_CHANGE // expectedCallWaitingInteraction
888         ));
889 
890         result.add(new ModeTestParameters(
891                 "Abandon focus once audio ops are complete",
892                 CallAudioModeStateMachine.ABANDON_FOCUS_FOR_TESTING, // initialAudioS
893                 CallAudioModeStateMachine.AUDIO_OPERATIONS_COMPLETE, // messageType
894                 new MessageArgs.Builder()
895                         .setHasActiveOrDialingCalls(false)
896                         .setHasRingingCalls(false)
897                         .setHasHoldingCalls(false)
898                         .setHasAudioProcessingCalls(false)
899                         .setIsTonePlaying(false)
900                         .setForegroundCallIsVoip(false)
901                         .setSession(null)
902                         .build(),
903                 CallAudioModeStateMachine.UNFOCUSED_STATE_NAME, // expectedFinalStateName
904                 FOCUS_OFF, // expectedFocus
905                 NO_CHANGE, // expectedMode
906                 NO_CHANGE, // expectedRingingInteraction
907                 NO_CHANGE // expectedCallWaitingInteraction
908         ));
909 
910         return result;
911     }
912 
resetMocks()913     private void resetMocks() {
914         reset(mCallAudioManager, mAudioManager);
915     }
916 }
917