1/*
2 * Copyright (C) 2018 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
17package android.telephony.ims;
18
19import static junit.framework.Assert.assertEquals;
20import static junit.framework.Assert.assertTrue;
21
22import static org.mockito.ArgumentMatchers.any;
23import static org.mockito.ArgumentMatchers.eq;
24import static org.mockito.Mockito.spy;
25import static org.mockito.Mockito.verify;
26
27import android.os.Handler;
28import android.os.Looper;
29import android.os.Message;
30import android.os.Messenger;
31import android.support.test.runner.AndroidJUnit4;
32import android.telecom.TelecomManager;
33import android.telephony.ims.aidl.IImsMmTelFeature;
34import android.telephony.ims.feature.ImsFeature;
35import android.telephony.ims.feature.MmTelFeature;
36import android.telephony.ims.stub.ImsCallSessionImplBase;
37import android.test.suitebuilder.annotation.SmallTest;
38
39import com.android.ims.internal.IImsCallSession;
40import com.android.internal.telephony.ims.ImsTestBase;
41
42import org.junit.After;
43import org.junit.Before;
44import org.junit.Test;
45import org.junit.runner.RunWith;
46import org.mockito.ArgumentCaptor;
47import org.mockito.Mockito;
48
49@RunWith(AndroidJUnit4.class)
50public class MmTelFeatureTests extends ImsTestBase {
51
52    private static final int TEST_CAPABILITY = 1;
53    private static final int TEST_RADIO_TECH = 0;
54
55    private static final int TEST_TTY_RESULT = 0;
56    private static final int TEST_SEND_DTMF_RESULT = 1;
57    private static final int TEST_RESULT_MAX = 2;
58
59    private static final int TEST_RESULT_DELAY_MS = 5000;
60
61    private android.telephony.ims.TestMmTelFeature mFeature;
62    private IImsMmTelFeature mFeatureBinder;
63    private ImsFeature.CapabilityCallback mCapabilityCallback;
64    private MmTelFeature.Listener mListener;
65
66    // set to true when the handler receives a message back from the Feature.
67    private boolean[] mHandlerResults;
68
69    private class TestHandler extends Handler {
70
71        TestHandler(Looper looper) {
72            super(looper);
73        }
74
75        @Override
76        public void handleMessage(Message msg) {
77            switch (msg.what) {
78                case TEST_TTY_RESULT:
79                    mHandlerResults[TEST_TTY_RESULT] = true;
80                    break;
81                case TEST_SEND_DTMF_RESULT:
82                    mHandlerResults[TEST_SEND_DTMF_RESULT] = true;
83                    break;
84            }
85        }
86    }
87    private final Handler mHandler = new TestHandler(Looper.getMainLooper());
88    private final Messenger mHandlerMessenger = new Messenger(mHandler);
89
90    @Before
91    public void setup() throws Exception {
92        super.setUp();
93        mFeature = new TestMmTelFeature();
94        mFeatureBinder = mFeature.getBinder();
95        mCapabilityCallback = spy(new ImsFeature.CapabilityCallback());
96        mListener = spy(new MmTelFeature.Listener());
97        mFeatureBinder.setListener(mListener);
98        mHandlerResults = new boolean[TEST_RESULT_MAX];
99    }
100
101    @After
102    public void tearDown() {
103        mFeature = null;
104        mFeatureBinder = null;
105    }
106
107    @SmallTest
108    @Test
109    public void testQueryCapabilityConfiguration() throws Exception {
110        mFeature.queryConfigurationResult = true;
111
112        mFeatureBinder.queryCapabilityConfiguration(TEST_CAPABILITY, TEST_RADIO_TECH,
113                mCapabilityCallback);
114
115        verify(mCapabilityCallback).onQueryCapabilityConfiguration(eq(TEST_CAPABILITY),
116                eq(TEST_RADIO_TECH), eq(true));
117    }
118
119    @SmallTest
120    @Test
121    public void testNewIncomingCall() throws Exception {
122        IImsCallSession sessionBinder = Mockito.mock(IImsCallSession.class);
123        ImsCallSessionImplBase session = new ImsCallSessionImplBase();
124        session.setServiceImpl(sessionBinder);
125
126        mFeature.incomingCall(session);
127        ArgumentCaptor<IImsCallSession> captor = ArgumentCaptor.forClass(IImsCallSession.class);
128        verify(mListener).onIncomingCall(captor.capture(), any());
129
130        assertEquals(sessionBinder, captor.getValue());
131    }
132
133    @SmallTest
134    @Test
135    public void testSetTtyMessageMessenger() throws Exception {
136        Message resultMessage = Message.obtain(mHandler, TEST_TTY_RESULT);
137        resultMessage.replyTo = mHandlerMessenger;
138        mFeatureBinder.setUiTtyMode(TelecomManager.TTY_MODE_FULL, resultMessage);
139        waitForHandlerAction(mHandler, TEST_RESULT_DELAY_MS);
140        assertTrue(mHandlerResults[TEST_TTY_RESULT]);
141    }
142
143    @SmallTest
144    @Test
145    public void testSendDtmfMessageMessenger() throws Exception {
146        Message resultMessage = Message.obtain(mHandler, TEST_SEND_DTMF_RESULT);
147        resultMessage.replyTo = mHandlerMessenger;
148        IImsCallSession callSession = mFeatureBinder.createCallSession(null);
149        callSession.sendDtmf('0', resultMessage);
150        waitForHandlerAction(mHandler, TEST_RESULT_DELAY_MS);
151        assertTrue(mHandlerResults[TEST_SEND_DTMF_RESULT]);
152    }
153}
154