1 /*
2  * Copyright (C) 2015 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;
18 
19 import static org.mockito.Mockito.anyInt;
20 import static org.mockito.Mockito.spy;
21 import static org.mockito.Mockito.when;
22 
23 import android.telecom.PhoneAccountHandle;
24 
25 import org.mockito.Mockito;
26 import org.mockito.invocation.InvocationOnMock;
27 import org.mockito.stubbing.Answer;
28 
29 import java.util.HashSet;
30 
31 /**
32  * Provides an instance of a mock CallList, and provides utility methods to put the CallList into
33  * various states (e.g. incoming call, active call, call waiting).
34  */
35 public class MockCallListWrapper {
36     private CallList mCallList;
37     private HashSet<Integer> mCallSet = new HashSet<>();
38 
MockCallListWrapper()39     public MockCallListWrapper() {
40         mCallList = Mockito.mock(CallList.class);
41         mCallList = spy(new CallList());
42         when(mCallList.getFirstCallWithState(anyInt())).thenAnswer(new Answer<Call>() {
43             @Override
44             public Call answer(InvocationOnMock i) throws Throwable {
45                 Object[] args = i.getArguments();
46                 final int state = (int) args[0];
47                 if (mCallSet.contains(state)) {
48                     return getMockCall(state);
49                 } else {
50                     return null;
51                 }
52             }
53         });
54     }
55 
getCallList()56     public CallList getCallList() {
57         return mCallList;
58     }
59 
setHasCall(int state, boolean hasCall)60     public void setHasCall(int state, boolean hasCall) {
61         if (hasCall) {
62             mCallSet.add(state);
63         } else {
64             mCallSet.remove(state);
65         }
66     }
67 
getMockCall(int state)68     private static Call getMockCall(int state) {
69         return getMockCall(state, state != Call.State.SELECT_PHONE_ACCOUNT);
70     }
71 
getMockCall(int state, boolean hasAccountHandle)72     private static Call getMockCall(int state, boolean hasAccountHandle) {
73         final Call call = Mockito.mock(Call.class);
74         when(call.getState()).thenReturn(Integer.valueOf(state));
75         if (hasAccountHandle) {
76             when(call.getAccountHandle()).thenReturn(new PhoneAccountHandle(null, null));
77         }
78         return call;
79     }
80 }
81