Fixed file descriptor leak in telephony unit tests
[android/platform/frameworks/opt/telephony.git] / tests / telephonytests / src / com / android / internal / telephony / dataconnection / DcControllerTest.java
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
17 package com.android.internal.telephony.dataconnection;
18
19 import static com.android.internal.telephony.TelephonyTestUtils.waitForMs;
20 import static com.android.internal.telephony.dataconnection.DcTrackerTest.FAKE_ADDRESS;
21 import static com.android.internal.telephony.dataconnection.DcTrackerTest.FAKE_DNS;
22 import static com.android.internal.telephony.dataconnection.DcTrackerTest.FAKE_GATEWAY;
23 import static com.android.internal.telephony.dataconnection.DcTrackerTest.FAKE_IFNAME;
24 import static com.android.internal.telephony.dataconnection.DcTrackerTest.FAKE_PCSCF_ADDRESS;
25
26 import static org.junit.Assert.assertEquals;
27 import static org.junit.Assert.fail;
28 import static org.mockito.Mockito.any;
29 import static org.mockito.Mockito.doReturn;
30 import static org.mockito.Mockito.eq;
31 import static org.mockito.Mockito.times;
32 import static org.mockito.Mockito.verify;
33
34 import android.net.LinkProperties;
35 import android.os.AsyncResult;
36 import android.os.Handler;
37 import android.os.HandlerThread;
38 import android.test.suitebuilder.annotation.SmallTest;
39
40 import com.android.internal.telephony.DctConstants;
41 import com.android.internal.telephony.TelephonyTest;
42 import com.android.internal.telephony.dataconnection.DataConnection.ConnectionParams;
43 import com.android.internal.telephony.dataconnection.DataConnection.UpdateLinkPropertyResult;
44 import com.android.internal.util.IState;
45 import com.android.internal.util.StateMachine;
46
47 import org.junit.After;
48 import org.junit.Before;
49 import org.junit.Test;
50 import org.mockito.Mock;
51
52 import java.lang.reflect.Method;
53 import java.util.ArrayList;
54 import java.util.HashMap;
55
56 public class DcControllerTest extends TelephonyTest {
57
58     private static final int DATA_CONNECTION_ACTIVE_PH_LINK_DORMANT = 1;
59     private static final int EVENT_DATA_STATE_CHANGED = 0x00040007;
60
61     @Mock
62     DataConnection mDc;
63     @Mock
64     HashMap<ApnContext, ConnectionParams> mApnContexts;
65
66     UpdateLinkPropertyResult mResult;
67
68     private DcController mDcc;
69     private DcControllerTestHandler mDcControllerTestHandler;
70
71     private class DcControllerTestHandler extends HandlerThread {
72
73         private DcControllerTestHandler(String name) {
74             super(name);
75         }
76
77         private Handler mHandler;
78
79         @Override
80         public void onLooperPrepared() {
81             mHandler = new Handler();
82             mDcc = DcController.makeDcc(mPhone, mDcTracker, mHandler);
83             setReady(true);
84         }
85     }
86
87     private IState getCurrentState() {
88         try {
89             Method method = StateMachine.class.getDeclaredMethod("getCurrentState");
90             method.setAccessible(true);
91             return (IState) method.invoke(mDcc);
92         } catch (Exception e) {
93             fail(e.toString());
94             return null;
95         }
96     }
97
98     @Before
99     public void setUp() throws Exception {
100         super.setUp(getClass().getSimpleName());
101
102         doReturn("fake.action_detached").when(mPhone).getActionDetached();
103         mDc.mApnContexts = mApnContexts;
104         doReturn(1).when(mApnContexts).size();
105
106         LinkProperties lp = new LinkProperties();
107         mResult = new UpdateLinkPropertyResult(lp);
108         doReturn(mResult).when(mDc).updateLinkProperty(any(DataCallResponse.class));
109
110         mDcControllerTestHandler = new DcControllerTestHandler(TAG);
111         mDcControllerTestHandler.start();
112         waitUntilReady();
113     }
114
115     @After
116     public void tearDown() throws Exception {
117         mDcControllerTestHandler.quit();
118         super.tearDown();
119     }
120
121     @Test
122     @SmallTest
123     public void testDataDormant() {
124         assertEquals("DccDefaultState", getCurrentState().getName());
125         ArrayList<DataCallResponse> l = new ArrayList<DataCallResponse>();
126         DataCallResponse dcResponse = new DataCallResponse(0, -1, 1,
127                 DATA_CONNECTION_ACTIVE_PH_LINK_DORMANT, "IP", FAKE_IFNAME,
128                 FAKE_ADDRESS, FAKE_DNS, FAKE_GATEWAY, FAKE_PCSCF_ADDRESS, 1440);
129         l.add(dcResponse);
130
131         mDc.mCid = 1;
132         mDcc.addActiveDcByCid(mDc);
133
134         mDcc.sendMessage(EVENT_DATA_STATE_CHANGED, new AsyncResult(null, l, null));
135         waitForMs(100);
136
137         verify(mDcTracker, times(1)).sendStopNetStatPoll(eq(DctConstants.Activity.DORMANT));
138     }
139 }