• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2016 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.incallui.ringtone;
18 
19 import android.media.AudioManager;
20 import android.media.ToneGenerator;
21 import android.test.AndroidTestCase;
22 import android.test.suitebuilder.annotation.SmallTest;
23 
24 import com.android.incallui.async.PausableExecutor;
25 import com.android.incallui.async.SingleProdThreadExecutor;
26 
27 import org.mockito.Mock;
28 import org.mockito.Mockito;
29 import org.mockito.MockitoAnnotations;
30 
31 @SmallTest
32 public class InCallTonePlayerTest extends AndroidTestCase {
33 
34     @Mock private ToneGeneratorFactory mToneGeneratorFactory;
35     @Mock private ToneGenerator mToneGenerator;
36     private InCallTonePlayer mInCallTonePlayer;
37 
38     /*
39      * InCallTonePlayer milestones:
40      * 1) After tone starts playing
41      * 2) After tone finishes waiting (could have timed out)
42      * 3) After cleaning up state to allow new tone to play
43      */
44     private PausableExecutor mExecutor;
45 
46     @Override
setUp()47     public void setUp() throws Exception {
48         super.setUp();
49         MockitoAnnotations.initMocks(this);
50         Mockito.when(mToneGeneratorFactory.newInCallToneGenerator(Mockito.anyInt(),
51                 Mockito.anyInt())).thenReturn(mToneGenerator);
52         mExecutor = new SingleProdThreadExecutor();
53         mInCallTonePlayer = new InCallTonePlayer(mToneGeneratorFactory, mExecutor);
54     }
55 
56     @Override
tearDown()57     public void tearDown() throws Exception {
58         super.tearDown();
59         // Stop any playing so the InCallTonePlayer isn't stuck waiting for the tone to complete
60         mInCallTonePlayer.stop();
61         // Ack all milestones to ensure that the prod thread doesn't block forever
62         mExecutor.ackAllMilestonesForTesting();
63     }
64 
testIsPlayingTone_False()65     public void testIsPlayingTone_False() {
66         assertFalse(mInCallTonePlayer.isPlayingTone());
67     }
68 
testIsPlayingTone_True()69     public void testIsPlayingTone_True() throws InterruptedException {
70         mInCallTonePlayer.play(InCallTonePlayer.TONE_CALL_WAITING);
71         mExecutor.awaitMilestoneForTesting();
72 
73         assertTrue(mInCallTonePlayer.isPlayingTone());
74     }
75 
testPlay_InvalidTone()76     public void testPlay_InvalidTone() {
77         try {
78             mInCallTonePlayer.play(Integer.MIN_VALUE);
79             fail();
80         } catch (IllegalArgumentException e) {}
81     }
82 
testPlay_CurrentlyPlaying()83     public void testPlay_CurrentlyPlaying() throws InterruptedException {
84         mInCallTonePlayer.play(InCallTonePlayer.TONE_CALL_WAITING);
85         mExecutor.awaitMilestoneForTesting();
86         try {
87             mInCallTonePlayer.play(InCallTonePlayer.TONE_CALL_WAITING);
88             fail();
89         } catch (IllegalStateException e) {}
90     }
91 
testPlay_VoiceCallStream()92     public void testPlay_VoiceCallStream() throws InterruptedException {
93         mInCallTonePlayer.play(InCallTonePlayer.TONE_CALL_WAITING);
94         mExecutor.awaitMilestoneForTesting();
95         Mockito.verify(mToneGeneratorFactory).newInCallToneGenerator(AudioManager.STREAM_VOICE_CALL,
96                 InCallTonePlayer.VOLUME_RELATIVE_HIGH_PRIORITY);
97     }
98 
testPlay_Single()99     public void testPlay_Single() throws InterruptedException {
100         mInCallTonePlayer.play(InCallTonePlayer.TONE_CALL_WAITING);
101         mExecutor.awaitMilestoneForTesting();
102         mExecutor.ackMilestoneForTesting();
103         mInCallTonePlayer.stop();
104         mExecutor.ackMilestoneForTesting();
105         mExecutor.awaitMilestoneForTesting();
106         mExecutor.ackMilestoneForTesting();
107 
108         Mockito.verify(mToneGenerator).startTone(ToneGenerator.TONE_SUP_CALL_WAITING);
109     }
110 
testPlay_Consecutive()111     public void testPlay_Consecutive() throws InterruptedException {
112         mInCallTonePlayer.play(InCallTonePlayer.TONE_CALL_WAITING);
113         mExecutor.awaitMilestoneForTesting();
114         mExecutor.ackMilestoneForTesting();
115         // Prevent waiting forever
116         mInCallTonePlayer.stop();
117         mExecutor.ackMilestoneForTesting();
118         mExecutor.awaitMilestoneForTesting();
119         mExecutor.ackMilestoneForTesting();
120 
121         mInCallTonePlayer.play(InCallTonePlayer.TONE_CALL_WAITING);
122         mExecutor.awaitMilestoneForTesting();
123         mExecutor.ackMilestoneForTesting();
124         mInCallTonePlayer.stop();
125         mExecutor.ackMilestoneForTesting();
126         mExecutor.awaitMilestoneForTesting();
127         mExecutor.ackMilestoneForTesting();
128 
129         Mockito.verify(mToneGenerator, Mockito.times(2))
130                 .startTone(ToneGenerator.TONE_SUP_CALL_WAITING);
131     }
132 
testStop_NotPlaying()133     public void testStop_NotPlaying() {
134         // No crash
135         mInCallTonePlayer.stop();
136     }
137 
testStop()138     public void testStop() throws InterruptedException {
139         mInCallTonePlayer.play(InCallTonePlayer.TONE_CALL_WAITING);
140         mExecutor.awaitMilestoneForTesting();
141 
142         mInCallTonePlayer.stop();
143         mExecutor.ackMilestoneForTesting();
144         mExecutor.awaitMilestoneForTesting();
145 
146         assertFalse(mInCallTonePlayer.isPlayingTone());
147     }
148 }
149