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

Commit 704520c3 authored by Hugo Benichi's avatar Hugo Benichi Committed by Gerrit Code Review
Browse files

Merge "Revert "ConnectivityManager: no double NetworkCallback registration"...

Merge "Revert "ConnectivityManager: no double NetworkCallback registration" because oit caused regressions http://b/35955593, http://b/35921499."
parents c337e32b 649e3248
Loading
Loading
Loading
Loading
+3 −6
Original line number Diff line number Diff line
@@ -2884,14 +2884,11 @@ public class ConnectivityManager {
        if (callback == null) {
            throw new IllegalArgumentException("null NetworkCallback");
        }
        if ((need == null) && (action != REQUEST)) {
        if (need == null && action != REQUEST) {
            throw new IllegalArgumentException("null NetworkCapabilities");
        }
        final int targetSdk = mContext.getApplicationInfo().targetSdkVersion;
        if ((targetSdk > VERSION_CODES.N_MR1) && (callback.networkRequest != null)) {
        // TODO: throw an exception if callback.networkRequest is not null.
        // http://b/20701525
            throw new IllegalArgumentException("NetworkCallback already registered");
        }
        final NetworkRequest request;
        try {
            synchronized(sCallbacks) {
+7 −52
Original line number Diff line number Diff line
@@ -36,36 +36,21 @@ import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.anyInt;
import static org.mockito.Mockito.when;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.os.Build.VERSION_CODES;
import android.net.ConnectivityManager.NetworkCallback;

import android.net.ConnectivityManager;
import android.net.NetworkCapabilities;

import android.support.test.filters.SmallTest;
import android.support.test.runner.AndroidJUnit4;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.junit.Test;



@RunWith(AndroidJUnit4.class)
@SmallTest
public class ConnectivityManagerTest {

    @Mock Context mCtx;
    @Mock IConnectivityManager mService;

    @Before
    public void setUp() {
        MockitoAnnotations.initMocks(this);
    }

    static NetworkCapabilities verifyNetworkCapabilities(
            int legacyType, int transportType, int... capabilities) {
        final NetworkCapabilities nc = ConnectivityManager.networkCapabilitiesForType(legacyType);
@@ -188,34 +173,4 @@ public class ConnectivityManagerTest {
        verifyUnrestrictedNetworkCapabilities(
                ConnectivityManager.TYPE_ETHERNET, TRANSPORT_ETHERNET);
    }

    @Test
    public void testNoDoubleCallbackRegistration() throws Exception {
        ConnectivityManager manager = new ConnectivityManager(mCtx, mService);
        NetworkRequest request = new NetworkRequest.Builder().clearCapabilities().build();
        NetworkCallback callback = new ConnectivityManager.NetworkCallback();
        ApplicationInfo info = new ApplicationInfo();
        info.targetSdkVersion = VERSION_CODES.N_MR1 + 1;

        when(mCtx.getApplicationInfo()).thenReturn(info);
        when(mService.requestNetwork(any(), any(), anyInt(), any(), anyInt())).thenReturn(request);

        manager.requestNetwork(request, callback);

        // Callback is already registered, reregistration should fail.
        Class<IllegalArgumentException> wantException = IllegalArgumentException.class;
        expectThrowable(() -> manager.requestNetwork(request, callback), wantException);
    }

    static void expectThrowable(Runnable block, Class<? extends Throwable> throwableType) {
        try {
            block.run();
        } catch (Throwable t) {
            if (t.getClass().equals(throwableType)) {
                return;
            }
            fail("expected exception of type " + throwableType + ", but was " + t.getClass());
        }
        fail("expected exception of type " + throwableType);
    }
}