DataConnectionTest.java revision 0852a954be5937a1b0bca94df0c2007d7ee3c0c7
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.dataconnection;
18
19import android.os.AsyncResult;
20import android.os.Handler;
21import android.os.HandlerThread;
22import android.os.Message;
23import android.telephony.ServiceState;
24import android.test.suitebuilder.annotation.SmallTest;
25
26import com.android.internal.telephony.PhoneConstants;
27import com.android.internal.telephony.RILConstants;
28import com.android.internal.telephony.RetryManager;
29import com.android.internal.telephony.TelephonyTest;
30import com.android.internal.telephony.dataconnection.DataConnection.ConnectionParams;
31import com.android.internal.telephony.dataconnection.DataConnection.DisconnectParams;
32import com.android.internal.util.IState;
33import com.android.internal.util.StateMachine;
34
35import org.junit.After;
36import org.junit.Before;
37import org.junit.Test;
38import org.mockito.Mock;
39
40import java.lang.reflect.Method;
41
42import static com.android.internal.telephony.TelephonyTestUtils.waitForMs;
43import static org.junit.Assert.assertEquals;
44import static org.mockito.Matchers.any;
45import static org.mockito.Matchers.eq;
46import static org.mockito.Mockito.doReturn;
47import static org.mockito.Mockito.times;
48import static org.mockito.Mockito.verify;
49
50public class DataConnectionTest extends TelephonyTest {
51
52    @Mock
53    DcTesterFailBringUpAll mDcTesterFailBringUpAll;
54    @Mock
55    ConnectionParams mCp;
56    @Mock
57    DisconnectParams mDcp;
58    @Mock
59    ApnContext mApnContext;
60    @Mock
61    DcFailBringUp mDcFailBringUp;
62
63    private DataConnection mDc;
64    private DcController mDcc;
65
66    private ApnSetting mApn1 = new ApnSetting(
67            2163,                   // id
68            "44010",                // numeric
69            "sp-mode",              // name
70            "spmode.ne.jp",         // apn
71            "",                     // proxy
72            "",                     // port
73            "",                     // mmsc
74            "",                     // mmsproxy
75            "",                     // mmsport
76            "",                     // user
77            "",                     // password
78            -1,                     // authtype
79            new String[]{"default", "supl"},     // types
80            "IP",                   // protocol
81            "IP",                   // roaming_protocol
82            true,                   // carrier_enabled
83            0,                      // bearer
84            0,                      // bearer_bitmask
85            0,                      // profile_id
86            false,                  // modem_cognitive
87            0,                      // max_conns
88            0,                      // wait_time
89            0,                      // max_conns_time
90            0,                      // mtu
91            "",                     // mvno_type
92            "");                    // mnvo_match_data
93
94    private class DataConnectionTestHandler extends HandlerThread {
95
96        private DataConnectionTestHandler(String name) {
97            super(name);
98        }
99
100        @Override
101        public void onLooperPrepared() {
102            Handler h = new Handler();
103
104            mDcc = DcController.makeDcc(mPhone, mDcTracker, h);
105            mDc = DataConnection.makeDataConnection(mPhone, 0, mDcTracker, mDcTesterFailBringUpAll,
106                    mDcc);
107
108            setReady(true);
109        }
110    }
111
112    @Before
113    public void setUp() throws Exception {
114        logd("+Setup!");
115        super.setUp(getClass().getSimpleName());
116
117        doReturn("fake.action_detached").when(mPhone).getActionDetached();
118        replaceInstance(ConnectionParams.class, "mApnContext", mCp, mApnContext);
119        replaceInstance(ConnectionParams.class, "mRilRat", mCp,
120                ServiceState.RIL_RADIO_TECHNOLOGY_UMTS);
121        doReturn(mApn1).when(mApnContext).getApnSetting();
122        doReturn(PhoneConstants.APN_TYPE_DEFAULT).when(mApnContext).getApnType();
123
124        mDcFailBringUp.saveParameters(0, 0, -2);
125        doReturn(mDcFailBringUp).when(mDcTesterFailBringUpAll).getDcFailBringUp();
126
127        mContextFixture.putStringArrayResource(com.android.internal.R.array.
128                config_mobile_tcp_buffers, new String[]{
129                "umts:131072,262144,1452032,4096,16384,399360",
130                "hspa:131072,262144,2441216,4096,16384,399360",
131                "hsupa:131072,262144,2441216,4096,16384,399360",
132                "hsdpa:131072,262144,2441216,4096,16384,399360",
133                "hspap:131072,262144,2441216,4096,16384,399360",
134                "edge:16384,32768,131072,4096,16384,65536",
135                "gprs:4096,8192,24576,4096,8192,24576",
136                "1xrtt:16384,32768,131070,4096,16384,102400",
137                "evdo:131072,262144,1048576,4096,16384,524288",
138                "lte:524288,1048576,8388608,262144,524288,4194304"});
139
140
141        mDcp.mApnContext = mApnContext;
142
143        new DataConnectionTestHandler(getClass().getSimpleName()).start();
144
145        waitUntilReady();
146        logd("-Setup!");
147    }
148
149    @After
150    public void tearDown() throws Exception {
151        logd("tearDown");
152        mDc = null;
153        mDcc = null;
154        super.tearDown();
155    }
156
157    private IState getCurrentState() throws Exception {
158        Method method = StateMachine.class.getDeclaredMethod("getCurrentState");
159        method.setAccessible(true);
160        return (IState) method.invoke(mDc);
161    }
162
163    private long getSuggestedRetryDelay(AsyncResult ar) throws Exception {
164        Class[] cArgs = new Class[1];
165        cArgs[0] = AsyncResult.class;
166        Method method = DataConnection.class.getDeclaredMethod("getSuggestedRetryDelay", cArgs);
167        method.setAccessible(true);
168        return (long) method.invoke(mDc, ar);
169    }
170
171    @Test
172    @SmallTest
173    public void testSanity() throws Exception {
174        assertEquals("DcInactiveState", getCurrentState().getName());
175    }
176
177    @Test
178    @SmallTest
179    public void testConnectEvent() throws Exception {
180        testSanity();
181
182        mDc.sendMessage(DataConnection.EVENT_CONNECT, mCp);
183        waitForMs(100);
184
185        verify(mCT, times(1)).registerForVoiceCallStarted(any(Handler.class),
186                eq(DataConnection.EVENT_DATA_CONNECTION_VOICE_CALL_STARTED), eq(null));
187        verify(mCT, times(1)).registerForVoiceCallEnded(any(Handler.class),
188                eq(DataConnection.EVENT_DATA_CONNECTION_VOICE_CALL_ENDED), eq(null));
189
190        verify(mSimulatedCommandsVerifier, times(1)).setupDataCall(
191                eq(ServiceState.RIL_RADIO_TECHNOLOGY_UMTS), eq(0), eq("spmode.ne.jp"),
192                eq(""), eq(""), eq(0), eq("IP"), any(Message.class));
193
194        assertEquals("DcActiveState", getCurrentState().getName());
195    }
196
197    @Test
198    @SmallTest
199    public void testDisconnectEvent() throws Exception {
200        testConnectEvent();
201
202        mDc.sendMessage(DataConnection.EVENT_DISCONNECT, mDcp);
203        waitForMs(100);
204
205        verify(mSimulatedCommandsVerifier, times(1)).deactivateDataCall(eq(1),
206                eq(RILConstants.DEACTIVATE_REASON_NONE), any(Message.class));
207
208        assertEquals("DcInactiveState", getCurrentState().getName());
209    }
210
211    @Test
212    @SmallTest
213    public void testModemSuggestRetry() throws Exception {
214        DataCallResponse response = new DataCallResponse();
215        response.suggestedRetryTime = 0;
216        AsyncResult ar = new AsyncResult(null, response, null);
217        assertEquals(response.suggestedRetryTime, getSuggestedRetryDelay(ar));
218
219        response.suggestedRetryTime = 1000;
220        assertEquals(response.suggestedRetryTime, getSuggestedRetryDelay(ar));
221
222        response.suggestedRetryTime = 9999;
223        assertEquals(response.suggestedRetryTime, getSuggestedRetryDelay(ar));
224    }
225
226    @Test
227    @SmallTest
228    public void testModemNotSuggestRetry() throws Exception {
229        DataCallResponse response = new DataCallResponse();
230        response.suggestedRetryTime = -1;
231        AsyncResult ar = new AsyncResult(null, response, null);
232        assertEquals(RetryManager.NO_SUGGESTED_RETRY_DELAY, getSuggestedRetryDelay(ar));
233
234        response.suggestedRetryTime = -5;
235        assertEquals(RetryManager.NO_SUGGESTED_RETRY_DELAY, getSuggestedRetryDelay(ar));
236
237        response.suggestedRetryTime = Integer.MIN_VALUE;
238        assertEquals(RetryManager.NO_SUGGESTED_RETRY_DELAY, getSuggestedRetryDelay(ar));
239    }
240
241    @Test
242    @SmallTest
243    public void testModemSuggestNoRetry() throws Exception {
244        DataCallResponse response = new DataCallResponse();
245        response.suggestedRetryTime = Integer.MAX_VALUE;
246        AsyncResult ar = new AsyncResult(null, response, null);
247        assertEquals(RetryManager.NO_RETRY, getSuggestedRetryDelay(ar));
248    }
249}