summaryrefslogtreecommitdiffstats
path: root/tests/src/com/android/incallui/MockCallListWrapper.java
blob: 369c4303f9ae27a4508104aa3ef108002556c758 (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
/*
 * Copyright (C) 2015 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;

import static org.mockito.Mockito.anyInt;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;

import android.telecom.PhoneAccountHandle;

import org.mockito.Mockito;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;

import java.util.HashSet;

/**
 * Provides an instance of a mock CallList, and provides utility methods to put the CallList into
 * various states (e.g. incoming call, active call, call waiting).
 */
public class MockCallListWrapper {
    private CallList mCallList;
    private HashSet<Integer> mCallSet = new HashSet<>();

    public MockCallListWrapper() {
        mCallList = Mockito.mock(CallList.class);
        mCallList = spy(new CallList());
        when(mCallList.getFirstCallWithState(anyInt())).thenAnswer(new Answer<Call>() {
            @Override
            public Call answer(InvocationOnMock i) throws Throwable {
                Object[] args = i.getArguments();
                final int state = (int) args[0];
                if (mCallSet.contains(state)) {
                    return getMockCall(state);
                } else {
                    return null;
                }
            }
        });
    }

    public CallList getCallList() {
        return mCallList;
    }

    public void setHasCall(int state, boolean hasCall) {
        if (hasCall) {
            mCallSet.add(state);
        } else {
            mCallSet.remove(state);
        }
    }

    private static Call getMockCall(int state) {
        return getMockCall(state, state != Call.State.SELECT_PHONE_ACCOUNT);
    }

    private static Call getMockCall(int state, boolean hasAccountHandle) {
        final Call call = Mockito.mock(Call.class);
        when(call.getState()).thenReturn(Integer.valueOf(state));
        if (hasAccountHandle) {
            when(call.getAccountHandle()).thenReturn(new PhoneAccountHandle(null, null));
        }
        return call;
    }
}