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.RingtoneManager;
20 import android.net.Uri;
21 import android.test.AndroidTestCase;
22 import android.test.suitebuilder.annotation.SmallTest;
23 
24 import com.android.contacts.common.compat.CompatUtils;
25 import com.android.incallui.Call;
26 import com.android.incallui.Call.State;
27 import com.android.incallui.CallList;
28 
29 import org.mockito.Mock;
30 import org.mockito.Mockito;
31 import org.mockito.MockitoAnnotations;
32 
33 @SmallTest
34 public class DialerRingtoneManagerTest extends AndroidTestCase {
35 
36     private static final Uri RINGTONE_URI = RingtoneManager
37             .getDefaultUri(RingtoneManager.TYPE_RINGTONE);
38 
39     @Mock private InCallTonePlayer mInCallTonePlayer;
40     @Mock private CallList mCallList;
41     @Mock private Call mCall;
42     private DialerRingtoneManager mRingtoneManagerEnabled;
43     private DialerRingtoneManager mRingtoneManagerDisabled;
44 
45     @Override
setUp()46     public void setUp() throws Exception {
47         super.setUp();
48         MockitoAnnotations.initMocks(this);
49         mRingtoneManagerEnabled = new DialerRingtoneManager(mInCallTonePlayer, mCallList);
50         mRingtoneManagerEnabled.setDialerRingingEnabledForTesting(true);
51         mRingtoneManagerDisabled = new DialerRingtoneManager(mInCallTonePlayer, mCallList);
52         mRingtoneManagerDisabled.setDialerRingingEnabledForTesting(false);
53     }
54 
testNullInCallTonePlayer()55     public void testNullInCallTonePlayer() {
56         try {
57             new DialerRingtoneManager(null, mCallList);
58             fail();
59         } catch (NullPointerException e) {}
60     }
61 
testNullCallList()62     public void testNullCallList() {
63         try {
64             new DialerRingtoneManager(mInCallTonePlayer, null);
65             fail();
66         } catch (NullPointerException e) {}
67     }
68 
testShouldPlayRingtone_M()69     public void testShouldPlayRingtone_M() {
70         if (CompatUtils.isNCompatible()) {
71             return;
72         }
73         assertFalse(mRingtoneManagerEnabled.shouldPlayRingtone(0, RINGTONE_URI));
74     }
75 
testShouldPlayRingtone_N_NullUri()76     public void testShouldPlayRingtone_N_NullUri() {
77         if (!CompatUtils.isNCompatible()) {
78             return;
79         }
80         assertFalse(mRingtoneManagerEnabled.shouldPlayRingtone(State.INCOMING, null));
81     }
82 
testShouldPlayRingtone_N_Disabled()83     public void testShouldPlayRingtone_N_Disabled() {
84         if (!CompatUtils.isNCompatible()) {
85             return;
86         }
87         assertFalse(mRingtoneManagerDisabled.shouldPlayRingtone(State.INCOMING, RINGTONE_URI));
88     }
89 
testShouldPlayRingtone_N_NotIncoming()90     public void testShouldPlayRingtone_N_NotIncoming() {
91         if (!CompatUtils.isNCompatible()) {
92             return;
93         }
94         assertFalse(mRingtoneManagerEnabled.shouldPlayRingtone(State.ACTIVE, RINGTONE_URI));
95     }
96 
97     // Specific case for call waiting since that needs its own sound
testShouldPlayRingtone_N_CallWaitingByState()98     public void testShouldPlayRingtone_N_CallWaitingByState() {
99         if (!CompatUtils.isNCompatible()) {
100             return;
101         }
102         assertFalse(mRingtoneManagerEnabled.shouldPlayRingtone(State.CALL_WAITING, RINGTONE_URI));
103     }
104 
testShouldPlayRingtone_N_CallWaitingByActiveCall()105     public void testShouldPlayRingtone_N_CallWaitingByActiveCall() {
106         if (!CompatUtils.isNCompatible()) {
107             return;
108         }
109         Mockito.when(mCallList.getActiveCall()).thenReturn(mCall);
110         assertFalse(mRingtoneManagerEnabled.shouldPlayRingtone(State.INCOMING, RINGTONE_URI));
111     }
112 
testShouldPlayRingtone_N()113     public void testShouldPlayRingtone_N() {
114         if (!CompatUtils.isNCompatible()) {
115             return;
116         }
117         assertTrue(mRingtoneManagerEnabled.shouldPlayRingtone(State.INCOMING, RINGTONE_URI));
118     }
119 
testShouldPlayCallWaitingTone_M()120     public void testShouldPlayCallWaitingTone_M() {
121         if (CompatUtils.isNCompatible()) {
122             return;
123         }
124         assertFalse(mRingtoneManagerEnabled.shouldPlayCallWaitingTone(0));
125     }
126 
testShouldPlayCallWaitingTone_N_Disabled()127     public void testShouldPlayCallWaitingTone_N_Disabled() {
128         if (!CompatUtils.isNCompatible()) {
129             return;
130         }
131         assertFalse(mRingtoneManagerDisabled.shouldPlayCallWaitingTone(State.CALL_WAITING));
132     }
133 
testShouldPlayCallWaitingTone_N_NotCallWaiting()134     public void testShouldPlayCallWaitingTone_N_NotCallWaiting() {
135         if (!CompatUtils.isNCompatible()) {
136             return;
137         }
138         assertFalse(mRingtoneManagerEnabled.shouldPlayCallWaitingTone(State.ACTIVE));
139     }
140 
141     // Specific case for incoming since it plays its own sound
testShouldPlayCallWaitingTone_N_Incoming()142     public void testShouldPlayCallWaitingTone_N_Incoming() {
143         if (!CompatUtils.isNCompatible()) {
144             return;
145         }
146         assertFalse(mRingtoneManagerEnabled.shouldPlayCallWaitingTone(State.INCOMING));
147     }
148 
testShouldPlayCallWaitingTone_N_AlreadyPlaying()149     public void testShouldPlayCallWaitingTone_N_AlreadyPlaying() {
150         if (!CompatUtils.isNCompatible()) {
151             return;
152         }
153         Mockito.when(mInCallTonePlayer.isPlayingTone()).thenReturn(true);
154         assertFalse(mRingtoneManagerEnabled.shouldPlayCallWaitingTone(State.CALL_WAITING));
155     }
156 
testShouldPlayCallWaitingTone_N_ByState()157     public void testShouldPlayCallWaitingTone_N_ByState() {
158         if (!CompatUtils.isNCompatible()) {
159             return;
160         }
161         assertTrue(mRingtoneManagerEnabled.shouldPlayCallWaitingTone(State.CALL_WAITING));
162     }
163 
testShouldPlayCallWaitingTone_N_ByActiveCall()164     public void testShouldPlayCallWaitingTone_N_ByActiveCall() {
165         if (!CompatUtils.isNCompatible()) {
166             return;
167         }
168         Mockito.when(mCallList.getActiveCall()).thenReturn(mCall);
169         assertTrue(mRingtoneManagerEnabled.shouldPlayCallWaitingTone(State.INCOMING));
170     }
171 
testPlayCallWaitingTone_M()172     public void testPlayCallWaitingTone_M() {
173         if (CompatUtils.isNCompatible()) {
174             return;
175         }
176         mRingtoneManagerEnabled.playCallWaitingTone();
177         Mockito.verify(mInCallTonePlayer, Mockito.never()).play(Mockito.anyInt());
178     }
179 
testPlayCallWaitingTone_N_NotEnabled()180     public void testPlayCallWaitingTone_N_NotEnabled() {
181         if (!CompatUtils.isNCompatible()) {
182             return;
183         }
184         mRingtoneManagerDisabled.playCallWaitingTone();
185         Mockito.verify(mInCallTonePlayer, Mockito.never()).play(Mockito.anyInt());
186     }
187 
testPlayCallWaitingTone_N()188     public void testPlayCallWaitingTone_N() {
189         if (!CompatUtils.isNCompatible()) {
190             return;
191         }
192         mRingtoneManagerEnabled.playCallWaitingTone();
193         Mockito.verify(mInCallTonePlayer).play(Mockito.anyInt());
194     }
195 
testStopCallWaitingTone_M()196     public void testStopCallWaitingTone_M() {
197         if (CompatUtils.isNCompatible()) {
198             return;
199         }
200         mRingtoneManagerEnabled.stopCallWaitingTone();
201         Mockito.verify(mInCallTonePlayer, Mockito.never()).stop();
202     }
203 
testStopCallWaitingTone_N_NotEnabled()204     public void testStopCallWaitingTone_N_NotEnabled() {
205         if (!CompatUtils.isNCompatible()) {
206             return;
207         }
208         mRingtoneManagerDisabled.stopCallWaitingTone();
209         Mockito.verify(mInCallTonePlayer, Mockito.never()).stop();
210     }
211 
testStopCallWaitingTone_N()212     public void testStopCallWaitingTone_N() {
213         if (!CompatUtils.isNCompatible()) {
214             return;
215         }
216         mRingtoneManagerEnabled.stopCallWaitingTone();
217         Mockito.verify(mInCallTonePlayer).stop();
218     }
219 }
220