summaryrefslogtreecommitdiff
path: root/InCallUI/tests/src/com/android/incallui/TestTelecomCall.java
blob: 48ac6e18fb92b8410e3347e8a68b1c0eb049fb04 (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
149
150
151
152
153
154
155
156
157
158
159
160
161
/*
 * 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;

import com.google.common.base.Preconditions;

import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.telecom.DisconnectCause;
import android.telecom.GatewayInfo;
import android.telecom.PhoneAccountHandle;
import android.telecom.StatusHints;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * Wrapper class which uses reflection to create instances of {@link android.telecom.Call} for use
 * with unit testing.  Since {@link android.telecom.Call} is final, it cannot be mocked using
 * mockito, and since all setter methods are hidden, it is necessary to use reflection.  In the
 * future, it would be desirable to replace this if a different mocking solution is used.
 */
public class TestTelecomCall {

    private android.telecom.Call mCall;

    public static @Nullable TestTelecomCall createInstance(String callId,
            Uri handle,
            int handlePresentation,
            String callerDisplayName,
            int callerDisplayNamePresentation,
            PhoneAccountHandle accountHandle,
            int capabilities,
            int properties,
            DisconnectCause disconnectCause,
            long connectTimeMillis,
            GatewayInfo gatewayInfo,
            int videoState,
            StatusHints statusHints,
            Bundle extras,
            Bundle intentExtras) {

        try {
            // Phone and InCall adapter are @hide, so we cannot refer to them directly.
            Class<?> phoneClass = Class.forName("android.telecom.Phone");
            Class<?> incallAdapterClass = Class.forName("android.telecom.InCallAdapter");
            Class<?> callClass = android.telecom.Call.class;
            Constructor<?> cons = callClass
                    .getDeclaredConstructor(phoneClass, String.class, incallAdapterClass);
            cons.setAccessible(true);

            android.telecom.Call call = (android.telecom.Call) cons.newInstance(null, callId, null);

            // Create an instance of the call details.
            Class<?> callDetailsClass = android.telecom.Call.Details.class;
            Constructor<?> detailsCons = callDetailsClass.getDeclaredConstructor(
                    String.class, /* telecomCallId */
                    Uri.class, /* handle */
                    int.class, /* handlePresentation */
                    String.class, /* callerDisplayName */
                    int.class, /* callerDisplayNamePresentation */
                    PhoneAccountHandle.class, /* accountHandle */
                    int.class, /* capabilities */
                    int.class, /* properties */
                    DisconnectCause.class, /* disconnectCause */
                    long.class, /* connectTimeMillis */
                    GatewayInfo.class, /* gatewayInfo */
                    int.class, /* videoState */
                    StatusHints.class, /* statusHints */
                    Bundle.class, /* extras */
                    Bundle.class /* intentExtras */);
            detailsCons.setAccessible(true);

            android.telecom.Call.Details details = (android.telecom.Call.Details)
                    detailsCons.newInstance(callId, handle, handlePresentation, callerDisplayName,
                            callerDisplayNamePresentation, accountHandle, capabilities, properties,
                            disconnectCause, connectTimeMillis, gatewayInfo, videoState,
                            statusHints,
                            extras, intentExtras);

            // Finally, set this as the details of the call.
            Field detailsField = call.getClass().getDeclaredField("mDetails");
            detailsField.setAccessible(true);
            detailsField.set(call, details);

            return new TestTelecomCall(call);
        } catch (NoSuchMethodException nsm) {
            return null;
        } catch (ClassNotFoundException cnf) {
            return null;
        } catch (IllegalAccessException e) {
            return null;
        } catch (InstantiationException e) {
            return null;
        } catch (InvocationTargetException e) {
            return null;
        } catch (NoSuchFieldException e) {
            return null;
        }
    }

    private TestTelecomCall(android.telecom.Call call) {
        mCall = call;
    }

    public android.telecom.Call getCall() {
        return mCall;
    }

    public void forceDetailsUpdate() {
        Preconditions.checkNotNull(mCall);

        try {
            Method method = mCall.getClass().getDeclaredMethod("fireDetailsChanged",
                    android.telecom.Call.Details.class);
            method.setAccessible(true);
            method.invoke(mCall, mCall.getDetails());
        } catch (NoSuchMethodException e) {
            Log.e(this, "forceDetailsUpdate", e);
        } catch (InvocationTargetException e) {
            Log.e(this, "forceDetailsUpdate", e);
        } catch (IllegalAccessException e) {
            Log.e(this, "forceDetailsUpdate", e);
        }
    }

    public void setCapabilities(int capabilities) {
        Preconditions.checkNotNull(mCall);
        try {
            Field field = mCall.getDetails().getClass().getDeclaredField("mCallCapabilities");
            field.setAccessible(true);
            field.set(mCall.getDetails(), capabilities);
        } catch (IllegalAccessException e) {
            Log.e(this, "setProperties", e);
        } catch (NoSuchFieldException e) {
            Log.e(this, "setProperties", e);
        }
    }

    public void setCall(android.telecom.Call call) {
        mCall = call;
    }
}