DtmfLocalTonePlayerTest.java revision 5507671b2ecba6c2e2de7df1a2177776f4a8db46
1/* * Copyright (C) 2017 The Android Open Source Project
2 *
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *      http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License
14 */
15
16package com.android.server.telecom.tests;
17
18import android.media.AudioManager;
19import android.media.ToneGenerator;
20import android.test.suitebuilder.annotation.SmallTest;
21
22import com.android.server.telecom.Call;
23import com.android.server.telecom.DtmfLocalTonePlayer;
24import com.android.server.telecom.R;
25
26import org.mockito.Mock;
27
28import static org.mockito.Matchers.anyInt;
29import static org.mockito.Matchers.eq;
30import static org.mockito.Mockito.never;
31import static org.mockito.Mockito.verify;
32import static org.mockito.Mockito.when;
33
34public class DtmfLocalTonePlayerTest extends TelecomTestCase {
35    private static final int TIMEOUT = 2000;
36    @Mock DtmfLocalTonePlayer.ToneGeneratorProxy mToneProxy;
37    @Mock Call mCall;
38
39    DtmfLocalTonePlayer mPlayer;
40
41    public void setUp() throws Exception {
42        super.setUp();
43        mContext = mComponentContextFixture.getTestDouble().getApplicationContext();
44        mPlayer = new DtmfLocalTonePlayer(mToneProxy);
45        when(mCall.getContext()).thenReturn(mContext);
46    }
47
48    @SmallTest
49    public void testSupportedStart() {
50        when(mContext.getResources().getBoolean(R.bool.allow_local_dtmf_tones)).thenReturn(true);
51        when(mToneProxy.isPresent()).thenReturn(true);
52        mPlayer.onForegroundCallChanged(null, mCall);
53        waitForHandlerAction(mPlayer.getHandler(), TIMEOUT);
54        verify(mToneProxy).create();
55    }
56
57    @SmallTest
58    public void testUnsupportedStart() {
59        when(mContext.getResources().getBoolean(R.bool.allow_local_dtmf_tones)).thenReturn(false);
60        when(mToneProxy.isPresent()).thenReturn(true);
61        mPlayer.onForegroundCallChanged(null, mCall);
62        waitForHandlerAction(mPlayer.getHandler(), TIMEOUT);
63        verify(mToneProxy, never()).create();
64    }
65
66    @SmallTest
67    public void testPlayToneWhenUninitialized() {
68        when(mContext.getResources().getBoolean(R.bool.allow_local_dtmf_tones)).thenReturn(false);
69        when(mToneProxy.isPresent()).thenReturn(false);
70        mPlayer.onForegroundCallChanged(null, mCall);
71        mPlayer.playTone(mCall, '9');
72        waitForHandlerAction(mPlayer.getHandler(), TIMEOUT);
73        verify(mToneProxy, never()).startTone(anyInt(), anyInt());
74    }
75
76    @SmallTest
77    public void testPlayToneWhenInitialized() {
78        when(mContext.getResources().getBoolean(R.bool.allow_local_dtmf_tones)).thenReturn(true);
79        when(mToneProxy.isPresent()).thenReturn(true);
80        mPlayer.onForegroundCallChanged(null, mCall);
81        mPlayer.playTone(mCall, '9');
82        waitForHandlerAction(mPlayer.getHandler(), TIMEOUT);
83        verify(mToneProxy).startTone(eq(ToneGenerator.TONE_DTMF_9), eq(-1));
84    }
85
86    @SmallTest
87    public void testStopToneWhenUninitialized() {
88        when(mContext.getResources().getBoolean(R.bool.allow_local_dtmf_tones)).thenReturn(false);
89        when(mToneProxy.isPresent()).thenReturn(false);
90        mPlayer.onForegroundCallChanged(null, mCall);
91        mPlayer.stopTone(mCall);
92        waitForHandlerAction(mPlayer.getHandler(), TIMEOUT);
93        verify(mToneProxy, never()).stopTone();
94    }
95
96    @SmallTest
97    public void testStopToneWhenInitialized() {
98        when(mContext.getResources().getBoolean(R.bool.allow_local_dtmf_tones)).thenReturn(true);
99        when(mToneProxy.isPresent()).thenReturn(true);
100        mPlayer.onForegroundCallChanged(null, mCall);
101        mPlayer.stopTone(mCall);
102        waitForHandlerAction(mPlayer.getHandler(), TIMEOUT);
103        verify(mToneProxy).stopTone();
104    }
105
106    @SmallTest
107    public void testProperTeardown() {
108        when(mContext.getResources().getBoolean(R.bool.allow_local_dtmf_tones)).thenReturn(true);
109        when(mToneProxy.isPresent()).thenReturn(true);
110        mPlayer.onForegroundCallChanged(null, mCall);
111        mPlayer.onForegroundCallChanged(mCall, null);
112        waitForHandlerAction(mPlayer.getHandler(), TIMEOUT);
113        verify(mToneProxy).release();
114    }
115}
116