summaryrefslogtreecommitdiff
path: root/InCallUI/tests/src/com/android/incallui/ringtone/InCallTonePlayerTest.java
blob: bde5c50e4b67285d6b08bdaa042d30f2a2d1a0cc (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
/*
 * Copyright (C) 2016 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.incallui.ringtone;

import android.media.AudioManager;
import android.media.ToneGenerator;
import android.test.AndroidTestCase;
import android.test.suitebuilder.annotation.SmallTest;

import com.android.incallui.async.PausableExecutor;
import com.android.incallui.async.SingleProdThreadExecutor;

import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;

@SmallTest
public class InCallTonePlayerTest extends AndroidTestCase {

    @Mock private ToneGeneratorFactory mToneGeneratorFactory;
    @Mock private ToneGenerator mToneGenerator;
    private InCallTonePlayer mInCallTonePlayer;

    /*
     * InCallTonePlayer milestones:
     * 1) After tone starts playing
     * 2) After tone finishes waiting (could have timed out)
     * 3) After cleaning up state to allow new tone to play
     */
    private PausableExecutor mExecutor;

    @Override
    public void setUp() throws Exception {
        super.setUp();
        MockitoAnnotations.initMocks(this);
        Mockito.when(mToneGeneratorFactory.newInCallToneGenerator(Mockito.anyInt(),
                Mockito.anyInt())).thenReturn(mToneGenerator);
        mExecutor = new SingleProdThreadExecutor();
        mInCallTonePlayer = new InCallTonePlayer(mToneGeneratorFactory, mExecutor);
    }

    @Override
    public void tearDown() throws Exception {
        super.tearDown();
        // Stop any playing so the InCallTonePlayer isn't stuck waiting for the tone to complete
        mInCallTonePlayer.stop();
        // Ack all milestones to ensure that the prod thread doesn't block forever
        mExecutor.ackAllMilestonesForTesting();
    }

    public void testIsPlayingTone_False() {
        assertFalse(mInCallTonePlayer.isPlayingTone());
    }

    public void testIsPlayingTone_True() throws InterruptedException {
        mInCallTonePlayer.play(InCallTonePlayer.TONE_CALL_WAITING);
        mExecutor.awaitMilestoneForTesting();

        assertTrue(mInCallTonePlayer.isPlayingTone());
    }

    public void testPlay_InvalidTone() {
        try {
            mInCallTonePlayer.play(Integer.MIN_VALUE);
            fail();
        } catch (IllegalArgumentException e) {}
    }

    public void testPlay_CurrentlyPlaying() throws InterruptedException {
        mInCallTonePlayer.play(InCallTonePlayer.TONE_CALL_WAITING);
        mExecutor.awaitMilestoneForTesting();
        try {
            mInCallTonePlayer.play(InCallTonePlayer.TONE_CALL_WAITING);
            fail();
        } catch (IllegalStateException e) {}
    }

    public void testPlay_VoiceCallStream() throws InterruptedException {
        mInCallTonePlayer.play(InCallTonePlayer.TONE_CALL_WAITING);
        mExecutor.awaitMilestoneForTesting();
        Mockito.verify(mToneGeneratorFactory).newInCallToneGenerator(AudioManager.STREAM_VOICE_CALL,
                InCallTonePlayer.VOLUME_RELATIVE_HIGH_PRIORITY);
    }

    public void testPlay_Single() throws InterruptedException {
        mInCallTonePlayer.play(InCallTonePlayer.TONE_CALL_WAITING);
        mExecutor.awaitMilestoneForTesting();
        mExecutor.ackMilestoneForTesting();
        mInCallTonePlayer.stop();
        mExecutor.ackMilestoneForTesting();
        mExecutor.awaitMilestoneForTesting();
        mExecutor.ackMilestoneForTesting();

        Mockito.verify(mToneGenerator).startTone(ToneGenerator.TONE_SUP_CALL_WAITING);
    }

    public void testPlay_Consecutive() throws InterruptedException {
        mInCallTonePlayer.play(InCallTonePlayer.TONE_CALL_WAITING);
        mExecutor.awaitMilestoneForTesting();
        mExecutor.ackMilestoneForTesting();
        // Prevent waiting forever
        mInCallTonePlayer.stop();
        mExecutor.ackMilestoneForTesting();
        mExecutor.awaitMilestoneForTesting();
        mExecutor.ackMilestoneForTesting();

        mInCallTonePlayer.play(InCallTonePlayer.TONE_CALL_WAITING);
        mExecutor.awaitMilestoneForTesting();
        mExecutor.ackMilestoneForTesting();
        mInCallTonePlayer.stop();
        mExecutor.ackMilestoneForTesting();
        mExecutor.awaitMilestoneForTesting();
        mExecutor.ackMilestoneForTesting();

        Mockito.verify(mToneGenerator, Mockito.times(2))
                .startTone(ToneGenerator.TONE_SUP_CALL_WAITING);
    }

    public void testStop_NotPlaying() {
        // No crash
        mInCallTonePlayer.stop();
    }

    public void testStop() throws InterruptedException {
        mInCallTonePlayer.play(InCallTonePlayer.TONE_CALL_WAITING);
        mExecutor.awaitMilestoneForTesting();

        mInCallTonePlayer.stop();
        mExecutor.ackMilestoneForTesting();
        mExecutor.awaitMilestoneForTesting();

        assertFalse(mInCallTonePlayer.isPlayingTone());
    }
}