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

Commit 6a49b240 authored by Robert Greenwalt's avatar Robert Greenwalt Committed by Android Partner Code Review
Browse files

Merge "Don't let other networks fool the Switcher" into mm-wireless-dev

parents c34634fa 3baedf2e
Loading
Loading
Loading
Loading
+4 −1
Original line number Diff line number Diff line
@@ -152,7 +152,8 @@ public class PhoneSwitcher extends Handler {

        NetworkFactory networkFactory = new PhoneSwitcherNetworkRequestListener(looper, context,
                netCap, this);
        networkFactory.setScoreFilter(50);
        // we want to see all requests
        networkFactory.setScoreFilter(101);
        networkFactory.register();

        log("PhoneSwitcher started");
@@ -223,6 +224,7 @@ public class PhoneSwitcher extends Handler {

        @Override
        protected void needNetworkFor(NetworkRequest networkRequest, int score) {
            if (VDBG) log("needNetworkFor " + networkRequest + ", " + score);
            Message msg = mPhoneSwitcher.obtainMessage(EVENT_REQUEST_NETWORK);
            msg.obj = networkRequest;
            msg.sendToTarget();
@@ -230,6 +232,7 @@ public class PhoneSwitcher extends Handler {

        @Override
        protected void releaseNetworkFor(NetworkRequest networkRequest) {
            if (VDBG) log("releaseNetworkFor " + networkRequest);
            Message msg = mPhoneSwitcher.obtainMessage(EVENT_RELEASE_NETWORK);
            msg.obj = networkRequest;
            msg.sendToTarget();
+5 −3
Original line number Diff line number Diff line
@@ -162,6 +162,10 @@ public class ContextFixture implements TestFixture<Context> {

        @Override
        public Object getSystemService(String name) {
            synchronized(mSystemServices) {
                Object service = mSystemServices.get(name);
                if (service != null) return service;
            }
            switch (name) {
                case Context.TELEPHONY_SERVICE:
                    return mTelephonyManager;
@@ -185,9 +189,7 @@ public class ContextFixture implements TestFixture<Context> {
                case Context.CONNECTIVITY_SERVICE:
                    return mConnectivityManager;
                default:
                    synchronized(mSystemServices) {
                        return mSystemServices.get(name);
                    }
                    return null;
            }
        }

+65 −3
Original line number Diff line number Diff line
@@ -436,7 +436,6 @@ public class PhoneSwitcherTest extends AndroidTestCase {
        final PhoneMock[] phones = new PhoneMock[numPhones];
        for (int i = 0; i < numPhones; i++) {
            commandsInterfaces[i] = new SimulatedCommands();
            //    phones[i] = new PhoneMock(contextMock, commandsInterfaces[i]);
        }

        PhoneSwitcher phoneSwitcher = new PhoneSwitcher(maxActivePhones, numPhones,
@@ -472,16 +471,79 @@ public class PhoneSwitcherTest extends AndroidTestCase {
        if (commandsInterfaces[0].isDataAllowed()) fail("data allowed");
        if (commandsInterfaces[1].isDataAllowed() == false) fail("data not allowed");

        testHandler.die();
        handlerThread.quit();
    }

    /**
     * Verify we don't send spurious DATA_ALLOWED calls when another NetworkFactory
     * wins (ie, switch to wifi).
     */
    @SmallTest
    public void testHigherPriorityDefault() throws Exception {
        mTestName = "testPrioritization";
        final int numPhones = 2;
        final int maxActivePhones = 1;
        final HandlerThread handlerThread = new HandlerThread("PhoneSwitcherTestThread");
        handlerThread.start();
        final ContextFixture contextFixture = new ContextFixture();
        String[] networkConfigString = getContext().getResources().getStringArray(
                com.android.internal.R.array.networkAttributes);
        contextFixture.putStringArrayResource(com.android.internal.R.array.networkAttributes,
                networkConfigString);
        final Context contextMock = contextFixture.getTestDouble();
        final ConnectivityServiceMock connectivityServiceMock =
                new ConnectivityServiceMock(contextMock);
        final ConnectivityManager cm =
                new ConnectivityManager(contextMock, connectivityServiceMock);
        contextFixture.setSystemService(Context.CONNECTIVITY_SERVICE, cm);
        final ITelephonyRegistry.Stub telRegistryMock = new TelephonyRegistryMock();
        final SubscriptionControllerMock subControllerMock =
                new SubscriptionControllerMock(contextMock, telRegistryMock, numPhones);
        final SimulatedCommands[] commandsInterfaces = new SimulatedCommands[numPhones];
        final PhoneMock[] phones = new PhoneMock[numPhones];
        for (int i = 0; i < numPhones; i++) {
            commandsInterfaces[i] = new SimulatedCommands();
        }

        PhoneSwitcher phoneSwitcher = new PhoneSwitcher(maxActivePhones, numPhones,
                contextMock, subControllerMock, handlerThread.getLooper(), telRegistryMock,
                commandsInterfaces, phones);

        TestHandler testHandler = TestHandler.makeHandler();
        Object activePhoneSwitchObject = new Object();
        testHandler.setActivePhoneSwitchObject(activePhoneSwitchObject);

        connectivityServiceMock.addDefaultRequest();
        subControllerMock.setSlotSubId(0, 0);
        subControllerMock.setSlotSubId(1, 1);
        subControllerMock.setDefaultDataSubId(0);
        waitABit();

        // Phone 0 should be active
        if (commandsInterfaces[0].isDataAllowed() == false) fail("data not allowed");
        if (commandsInterfaces[1].isDataAllowed()) fail("data allowed");

        connectivityServiceMock.setCurrentScoreForRequest(connectivityServiceMock.defaultRequest,
                100);
        waitABit();

        // should be no change
        if (commandsInterfaces[0].isDataAllowed() == false) fail("data not allowed");
        if (commandsInterfaces[1].isDataAllowed()) fail("data allowed");

        connectivityServiceMock.setCurrentScoreForRequest(connectivityServiceMock.defaultRequest,
                100);
        waitABit();

        // should be no change
        if (commandsInterfaces[0].isDataAllowed() == false) fail("data not allowed");
        if (commandsInterfaces[1].isDataAllowed()) fail("data allowed");

        testHandler.die();
        handlerThread.quit();
    }



    /**
     * Test MSMA testing prioritiziation
     * - leave multiple on (up to the limit)
+12 −6
Original line number Diff line number Diff line
@@ -1011,22 +1011,28 @@ public class ConnectivityServiceMock extends IConnectivityManager.Stub
        throw new RuntimeException("not implemented");
    }

    private NetworkRequest mDefaultRequest = null;
    @VisibleForTesting
    public NetworkRequest defaultRequest = null;
    @VisibleForTesting
    public synchronized void addDefaultRequest() {
        if (mDefaultRequest != null) return;
        if (defaultRequest != null) return;
        NetworkCapabilities netCap = new NetworkCapabilities();
        netCap.addCapability(NET_CAPABILITY_INTERNET);
        netCap.addCapability(NET_CAPABILITY_NOT_RESTRICTED);
        mDefaultRequest = requestNetwork(netCap, null, 0, new Binder(),
        defaultRequest = requestNetwork(netCap, null, 0, new Binder(),
                ConnectivityManager.TYPE_NONE);
    }

    @VisibleForTesting
    public synchronized void setCurrentScoreForRequest(NetworkRequest nr, int score) {
        sendUpdatedScoreToFactories(nr, score);
    }

    @VisibleForTesting
    public synchronized void removeDefaultRequest() {
        if (mDefaultRequest == null) return;
        releaseNetworkRequest(mDefaultRequest);
        mDefaultRequest = null;
        if (defaultRequest == null) return;
        releaseNetworkRequest(defaultRequest);
        defaultRequest = null;
    }