Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 3a5b9323 authored by Jack Yu's avatar Jack Yu
Browse files

First unit test for DcController

bug: 25691379
Change-Id: I11d0a55f8efd1510478bd8dc6965a26118457802
parent 6f769551
Loading
Loading
Loading
Loading
+7 −7
Original line number Original line Diff line number Diff line
@@ -73,7 +73,7 @@ import java.util.HashMap;
 * NOTE: All DataConnection objects must be running on the same looper, which is the default
 * NOTE: All DataConnection objects must be running on the same looper, which is the default
 * as the coordinator has members which are used without synchronization.
 * as the coordinator has members which are used without synchronization.
 */
 */
public final class DataConnection extends StateMachine {
public class DataConnection extends StateMachine {
    private static final boolean DBG = true;
    private static final boolean DBG = true;
    private static final boolean VDBG = true;
    private static final boolean VDBG = true;


@@ -96,7 +96,7 @@ public final class DataConnection extends StateMachine {
    /**
    /**
     * Used internally for saving connecting parameters.
     * Used internally for saving connecting parameters.
     */
     */
    static class ConnectionParams {
    public static class ConnectionParams {
        int mTag;
        int mTag;
        ApnContext mApnContext;
        ApnContext mApnContext;
        int mProfileId;
        int mProfileId;
@@ -164,8 +164,8 @@ public final class DataConnection extends StateMachine {


    //***** Package visible variables
    //***** Package visible variables
    int mTag;
    int mTag;
    int mCid;
    public int mCid;
    HashMap<ApnContext, ConnectionParams> mApnContexts = null;
    public HashMap<ApnContext, ConnectionParams> mApnContexts = null;
    PendingIntent mReconnectIntent = null;
    PendingIntent mReconnectIntent = null;




@@ -277,7 +277,7 @@ public final class DataConnection extends StateMachine {
        mLinkProperties.setHttpProxy(proxy);
        mLinkProperties.setHttpProxy(proxy);
    }
    }


    static class UpdateLinkPropertyResult {
    public static class UpdateLinkPropertyResult {
        public DataCallResponse.SetupResult setupResult = DataCallResponse.SetupResult.SUCCESS;
        public DataCallResponse.SetupResult setupResult = DataCallResponse.SetupResult.SUCCESS;
        public LinkProperties oldLp;
        public LinkProperties oldLp;
        public LinkProperties newLp;
        public LinkProperties newLp;
@@ -321,7 +321,7 @@ public final class DataConnection extends StateMachine {
        return ret;
        return ret;
    }
    }


    UpdateLinkPropertyResult updateLinkProperty(DataCallResponse newState) {
    public UpdateLinkPropertyResult updateLinkProperty(DataCallResponse newState) {
        UpdateLinkPropertyResult result = new UpdateLinkPropertyResult(mLinkProperties);
        UpdateLinkPropertyResult result = new UpdateLinkPropertyResult(mLinkProperties);


        if (newState == null) return result;
        if (newState == null) return result;
@@ -715,7 +715,7 @@ public final class DataConnection extends StateMachine {
            result = DataCallResponse.SetupResult.ERR_RilError;
            result = DataCallResponse.SetupResult.ERR_RilError;
            result.mFailCause = DcFailCause.fromInt(response.status);
            result.mFailCause = DcFailCause.fromInt(response.status);
        } else {
        } else {
            if (DBG) log("onSetupConnectionCompleted received DataCallResponse: " + response);
            if (DBG) log("onSetupConnectionCompleted received successful DataCallResponse");
            mCid = response.cid;
            mCid = response.cid;


            mPcscfAddr = response.pcscf;
            mPcscfAddr = response.pcscf;
+3 −3
Original line number Original line Diff line number Diff line
@@ -45,7 +45,7 @@ import java.util.HashMap;
 * multiple data connections. For instance listening for unsolicited messages
 * multiple data connections. For instance listening for unsolicited messages
 * and then demultiplexing them to the appropriate DC.
 * and then demultiplexing them to the appropriate DC.
 */
 */
class DcController extends StateMachine {
public class DcController extends StateMachine {
    private static final boolean DBG = true;
    private static final boolean DBG = true;
    private static final boolean VDBG = false;
    private static final boolean VDBG = false;


@@ -114,7 +114,7 @@ class DcController extends StateMachine {
        }
        }
    }
    }


    static DcController makeDcc(Phone phone, DcTracker dct, Handler handler) {
    public static DcController makeDcc(Phone phone, DcTracker dct, Handler handler) {
        DcController dcc = new DcController("Dcc", phone, dct, handler);
        DcController dcc = new DcController("Dcc", phone, dct, handler);
        dcc.start();
        dcc.start();
        return dcc;
        return dcc;
@@ -135,7 +135,7 @@ class DcController extends StateMachine {
        mDcListAll.remove(dc);
        mDcListAll.remove(dc);
    }
    }


    void addActiveDcByCid(DataConnection dc) {
    public void addActiveDcByCid(DataConnection dc) {
        if (DBG && dc.mCid < 0) {
        if (DBG && dc.mCid < 0) {
            log("addActiveDcByCid dc.mCid < 0 dc=" + dc);
            log("addActiveDcByCid dc.mCid < 0 dc=" + dc);
        }
        }
+131 −0
Original line number Original line Diff line number Diff line
/*
 * 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.internal.telephony;

import android.net.LinkProperties;
import android.os.AsyncResult;
import android.os.Handler;
import android.os.HandlerThread;
import android.test.suitebuilder.annotation.SmallTest;

import com.android.internal.telephony.dataconnection.ApnContext;
import com.android.internal.telephony.dataconnection.DataCallResponse;
import com.android.internal.telephony.dataconnection.DataConnection;
import com.android.internal.telephony.dataconnection.DataConnection.ConnectionParams;
import com.android.internal.telephony.dataconnection.DataConnection.UpdateLinkPropertyResult;
import com.android.internal.telephony.dataconnection.DcController;
import com.android.internal.util.IState;
import com.android.internal.util.StateMachine;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;

import static com.android.internal.telephony.TelephonyTestUtils.waitForMs;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.eq;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

public class DcControllerTest extends TelephonyTest {

    private static final int DATA_CONNECTION_ACTIVE_PH_LINK_DORMANT = 1;
    private static final int EVENT_DATA_STATE_CHANGED = 0x00040007;

    @Mock
    DataConnection mDc;
    @Mock
    HashMap<ApnContext, ConnectionParams> mApnContexts;

    UpdateLinkPropertyResult mResult;

    private DcController mDcc;

    private class DcControllerTestHandler extends HandlerThread {

        private DcControllerTestHandler(String name) {
            super(name);
        }

        private Handler mHandler;

        @Override
        public void onLooperPrepared() {
            mHandler = new Handler();
            mDcc = DcController.makeDcc(mPhone, mDcTracker, mHandler);
            setReady(true);
        }
    }

    private IState getCurrentState() {
        try {
            Method method = StateMachine.class.getDeclaredMethod("getCurrentState");
            method.setAccessible(true);
            return (IState) method.invoke(mDcc);
        } catch (Exception e) {
            fail(e.toString());
            return null;
        }
    }

    @Before
    public void setUp() throws Exception {
        super.setUp(getClass().getSimpleName());

        doReturn("fake.action_detached").when(mPhone).getActionDetached();
        mDc.mApnContexts = mApnContexts;
        doReturn(1).when(mApnContexts).size();

        LinkProperties lp = new LinkProperties();
        mResult = new UpdateLinkPropertyResult(lp);
        doReturn(mResult).when(mDc).updateLinkProperty(any(DataCallResponse.class));

        new DcControllerTestHandler(TAG).start();
        waitUntilReady();
    }

    @After
    public void tearDown() throws Exception {
        super.tearDown();
    }

    @Test
    @SmallTest
    public void testDataDormant() {
        assertEquals("DccDefaultState", getCurrentState().getName());
        ArrayList<DataCallResponse> l = new ArrayList<DataCallResponse>();
        DataCallResponse dcResponse = DcTrackerTest.createDataCallResponse();
        dcResponse.active = DATA_CONNECTION_ACTIVE_PH_LINK_DORMANT;
        l.add(dcResponse);

        mDc.mCid = 1;
        mDcc.addActiveDcByCid(mDc);

        mDcc.sendMessage(EVENT_DATA_STATE_CHANGED, new AsyncResult(null, l, null));
        waitForMs(100);

        verify(mDcTracker, times(1)).sendStopNetStatPoll(eq(DctConstants.Activity.DORMANT));
    }
}
 No newline at end of file
+9 −9
Original line number Original line Diff line number Diff line
@@ -74,14 +74,14 @@ public class DcTrackerTest extends TelephonyTest {
    private final List<String> sApnTypes = Arrays.asList(
    private final List<String> sApnTypes = Arrays.asList(
            "default", "mms", "cbs", "fota", "supl", "ia", "emergency", "dun", "hipri", "ims");
            "default", "mms", "cbs", "fota", "supl", "ia", "emergency", "dun", "hipri", "ims");


    private final String FAKE_APN1 = "FAKE APN 1";
    private static final String FAKE_APN1 = "FAKE APN 1";
    private final String FAKE_APN2 = "FAKE APN 2";
    private static final String FAKE_APN2 = "FAKE APN 2";
    private final String FAKE_APN3 = "FAKE APN 3";
    private static final String FAKE_APN3 = "FAKE APN 3";
    private final String FAKE_IFNAME = "FAKE IFNAME";
    private static final String FAKE_IFNAME = "FAKE IFNAME";
    private final String FAKE_PCSCF_ADDRESS = "22.33.44.55";
    private static final String FAKE_PCSCF_ADDRESS = "22.33.44.55";
    private final String FAKE_GATEWAY = "11.22.33.44";
    private static final String FAKE_GATEWAY = "11.22.33.44";
    private final String FAKE_DNS = "55.66.77.88";
    private static final String FAKE_DNS = "55.66.77.88";
    private final String FAKE_ADDRESS = "99.88.77.66";
    private static final String FAKE_ADDRESS = "99.88.77.66";


    @Mock
    @Mock
    ISub mIsub;
    ISub mIsub;
@@ -303,7 +303,7 @@ public class DcTrackerTest extends TelephonyTest {
    }
    }


    // Create a successful data response
    // Create a successful data response
    private DataCallResponse createDataCallResponse() {
    public static DataCallResponse createDataCallResponse() {


        DataCallResponse dcResponse = new DataCallResponse();
        DataCallResponse dcResponse = new DataCallResponse();