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
17package com.android.internal.telephony;
18
19import android.os.Handler;
20import android.os.Looper;
21import android.telephony.PhoneNumberUtils;
22import android.test.suitebuilder.annotation.SmallTest;
23import android.test.suitebuilder.annotation.MediumTest;
24import android.telephony.DisconnectCause;
25
26import static org.junit.Assert.*;
27import static org.mockito.Mockito.*;
28import org.junit.After;
29import org.junit.Before;
30import org.junit.Test;
31import org.mockito.Mock;
32import static com.android.internal.telephony.TelephonyTestUtils.waitForMs;
33
34public class GsmCdmaConnectionTest extends TelephonyTest {
35
36    private GsmCdmaConnection connection;
37
38    @Mock
39    DriverCall mDC;
40
41    @Before
42    public void setUp() throws Exception {
43        super.setUp(getClass().getSimpleName());
44        replaceInstance(Handler.class, "mLooper", mCT, Looper.getMainLooper());
45
46        mCT.mForegroundCall = new GsmCdmaCall(mCT);
47        mCT.mBackgroundCall = new GsmCdmaCall(mCT);
48        mCT.mRingingCall = new GsmCdmaCall(mCT);
49    }
50
51    @After
52    public void tearDown() throws Exception {
53        connection = null;
54        super.tearDown();
55    }
56
57    @Test @SmallTest
58    public void testFormatDialString(){
59        connection = new GsmCdmaConnection(mPhone, String.format(
60                "+1 (700).555-41NN%c1234", PhoneNumberUtils.PAUSE), mCT, null,
61                false /*isEmergencyCall*/);
62       /* case 1: If PAUSE/WAIT sequence at the end, ignore them */
63        String formattedDialStr = connection.formatDialString(
64                String.format("+1 (700).555-41NN1234%c", PhoneNumberUtils.PAUSE));
65        assertEquals("+1 (700).555-41NN1234", formattedDialStr);
66
67       /*  case 2: If consecutive PAUSE/WAIT sequence in the middle of the string,
68        *  and if there is any WAIT in PAUSE/WAIT sequence, treat them like WAIT.*/
69        formattedDialStr = connection.formatDialString("+1 (700).555-41NN,;1234");
70        assertEquals("+1 (700).555-41NN;1234", formattedDialStr);
71    }
72
73    @Test @SmallTest
74    public void testSanityGSM() {
75        connection = new GsmCdmaConnection(mPhone, String.format(
76                "+1 (700).555-41NN%c1234", PhoneNumberUtils.PAUSE), mCT, null,
77                false /*isEmergencyCall*/);
78        logd("Testing initial state of GsmCdmaConnection");
79        assertEquals(GsmCdmaCall.State.IDLE, connection.getState());
80        assertEquals(Connection.PostDialState.NOT_STARTED, connection.getPostDialState());
81        assertEquals(DisconnectCause.NOT_DISCONNECTED, DisconnectCause.NOT_DISCONNECTED);
82        assertEquals(0, connection.getDisconnectTime());
83        assertEquals(0, connection.getHoldDurationMillis());
84        assertEquals(PhoneConstants.PRESENTATION_ALLOWED, connection.getNumberPresentation());
85        assertFalse(connection.isMultiparty());
86        assertNotNull(connection.getRemainingPostDialString());
87        assertEquals("+1 (700).555-41NN,1234", connection.getOrigDialString());
88    }
89
90    @Test @SmallTest
91    public void testSanityCDMA() {
92        doReturn(PhoneConstants.PHONE_TYPE_CDMA).when(mPhone).getPhoneType();
93        connection = new GsmCdmaConnection(mPhone, String.format(
94                "+1 (700).555-41NN%c1234", PhoneNumberUtils.PAUSE), mCT, null,
95                false /*isEmergencyCall*/);
96        logd("Testing initial state of GsmCdmaConnection");
97        assertEquals(GsmCdmaCall.State.IDLE, connection.getState());
98        assertEquals(Connection.PostDialState.NOT_STARTED, connection.getPostDialState());
99        assertEquals(DisconnectCause.NOT_DISCONNECTED, DisconnectCause.NOT_DISCONNECTED);
100        assertEquals(0, connection.getDisconnectTime());
101        assertEquals(0, connection.getHoldDurationMillis());
102        assertEquals(PhoneConstants.PRESENTATION_ALLOWED, connection.getNumberPresentation());
103        assertFalse(connection.isMultiparty());
104        assertNotNull(connection.getRemainingPostDialString());
105        /* CDMA phone type dont have origDialString */
106        assertNull(connection.getOrigDialString());
107    }
108
109    @Test @SmallTest
110    public void testConnectionStateUpdate() {
111        connection = new GsmCdmaConnection(mPhone, String.format(
112                "+1 (700).555-41NN%c1234", PhoneNumberUtils.PAUSE), mCT, null,
113                false /*isEmergencyCall*/);
114        logd("Update the connection state from idle to active");
115        mDC.state = DriverCall.State.ACTIVE;
116        connection.update(mDC);
117        assertEquals(GsmCdmaCall.State.ACTIVE, connection.getState());
118        logd("update connection state from active to holding");
119        mDC.state = DriverCall.State.HOLDING;
120        connection.update(mDC);
121        assertEquals(GsmCdmaCall.State.HOLDING, connection.getState());
122        waitForMs(50);
123        assertTrue(connection.getHoldDurationMillis() >= 50);
124    }
125
126    @Test @MediumTest
127    public void testCDMAPostDialPause() {
128        doReturn(PhoneConstants.PHONE_TYPE_CDMA).when(mPhone).getPhoneType();
129        connection = new GsmCdmaConnection(mPhone, String.format(
130                "+1 (700).555-41NN%c1234", PhoneNumberUtils.PAUSE), mCT, null,
131                false /*isEmergencyCall*/);
132        logd("Mock connection state from alerting to active ");
133        mDC.state = DriverCall.State.ALERTING;
134        connection.update(mDC);
135        mDC.state = DriverCall.State.ACTIVE;
136        connection.update(mDC);
137        logd("process post dail sequence with pause");
138        assertEquals(Connection.PostDialState.PAUSE, connection.getPostDialState());
139        /* pause for 2000 ms + 50ms margin */
140        waitForMs(GsmCdmaConnection.PAUSE_DELAY_MILLIS_CDMA + 100);
141        assertEquals(Connection.PostDialState.COMPLETE, connection.getPostDialState());
142    }
143
144    @Test @MediumTest
145    public void testGSMPostDialPause() {
146        connection = new GsmCdmaConnection(mPhone, String.format(
147                "+1 (700).555-41NN%c1234", PhoneNumberUtils.PAUSE), mCT, null,
148                false /*isEmergencyCall*/);
149        logd("Mock connection state from alerting to active ");
150        mDC.state = DriverCall.State.ALERTING;
151        connection.update(mDC);
152        mDC.state = DriverCall.State.ACTIVE;
153        connection.update(mDC);
154        logd("process post dail sequence with pause");
155        assertEquals(Connection.PostDialState.STARTED, connection.getPostDialState());
156        /* pause for 2000 ms + 50ms margin */
157        waitForMs(GsmCdmaConnection.PAUSE_DELAY_MILLIS_GSM + 100);
158        assertEquals(Connection.PostDialState.COMPLETE, connection.getPostDialState());
159    }
160
161
162    @Test @SmallTest
163    public void testPostDialWait() {
164        doReturn(PhoneConstants.PHONE_TYPE_CDMA).when(mPhone).getPhoneType();
165        connection = new GsmCdmaConnection(mPhone,
166                String.format("+1 (700).555-41NN%c1234", PhoneNumberUtils.WAIT),mCT,null,
167                false /*isEmergencyCall*/);
168        logd("Mock connection state transition from alerting to active ");
169        mDC.state = DriverCall.State.ALERTING;
170        connection.update(mDC);
171        mDC.state = DriverCall.State.ACTIVE;
172        connection.update(mDC);
173        logd("Process the post dial sequence with wait ");
174        assertEquals(Connection.PostDialState.WAIT, connection.getPostDialState());
175        connection.proceedAfterWaitChar();
176        waitForMs(50);
177        assertEquals(Connection.PostDialState.COMPLETE, connection.getPostDialState());
178    }
179
180    @Test @SmallTest
181    public void testHangUpConnection() {
182        connection = new GsmCdmaConnection(mPhone, String.format(
183                "+1 (700).555-41NN%c1234", PhoneNumberUtils.PAUSE), mCT, null,
184                false /*isEmergencyCall*/);
185        mDC.state = DriverCall.State.ACTIVE;
186        connection.update(mDC);
187        logd("Hangup the connection locally");
188        connection.onDisconnect(DisconnectCause.LOCAL);
189        assertEquals(GsmCdmaCall.State.DISCONNECTED, connection.getState());
190        assertEquals(DisconnectCause.LOCAL, connection.getDisconnectCause());
191        assertTrue(connection.getDisconnectTime() <= System.currentTimeMillis());
192    }
193
194    @Test @SmallTest
195    public void testAddressUpdate() {
196        String[] testAddressMappingSet[] = {
197                /* {"0:inputAddress", "1:updateAddress", "2:ExpectResult"} */
198                {"12345", "12345", "12345"},
199                {"12345", "67890", "67890"},
200                {"12345*00000", "12345", "12345*00000"},
201                {"12345*00000", "67890", "67890"},
202                {"12345*00000", "12345*00000", "12345*00000"},
203                {"12345;11111*00000", "12345", "12345"},
204                {"12345*00000;11111", "12345", "12345*00000"},
205                {"18412345*00000", "18412345", "18412345*00000"},
206                {"+8112345*00000", "+8112345", "+8112345*00000"}};
207        mDC.state = DriverCall.State.ALERTING;
208        for (String[] testAddress : testAddressMappingSet) {
209            connection = new GsmCdmaConnection(mPhone, testAddress[0], mCT, null, false);
210            connection.setIsIncoming(true);
211            mDC.number = testAddress[1];
212            connection.update(mDC);
213            assertEquals(testAddress[2], connection.getAddress());
214        }
215    }
216
217    /**
218     * Ensures outgoing calls do not apply address changes.
219     */
220    @Test @SmallTest
221    public void testAddressUpdateOutgoing() {
222        mDC.state = DriverCall.State.ALERTING;
223        connection = new GsmCdmaConnection(mPhone, "12345", mCT, null, false);
224        connection.setIsIncoming(false);
225        mDC.number = "678";
226        connection.update(mDC);
227        assertEquals("12345", connection.getAddress());
228    }
229}
230