Merge "ConnectivityManager: no double NetworkCallback registration"
This commit is contained in:
@ -2884,11 +2884,14 @@ 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");
|
||||
}
|
||||
// TODO: throw an exception if callback.networkRequest is not null.
|
||||
// http://b/20701525
|
||||
final int targetSdk = mContext.getApplicationInfo().targetSdkVersion;
|
||||
if ((targetSdk > VERSION_CODES.N_MR1) && (callback.networkRequest != null)) {
|
||||
// http://b/20701525
|
||||
throw new IllegalArgumentException("NetworkCallback already registered");
|
||||
}
|
||||
final NetworkRequest request;
|
||||
try {
|
||||
synchronized(sCallbacks) {
|
||||
|
@ -36,21 +36,36 @@ 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.net.ConnectivityManager;
|
||||
import android.net.NetworkCapabilities;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.pm.ApplicationInfo;
|
||||
import android.os.Build.VERSION_CODES;
|
||||
import android.net.ConnectivityManager.NetworkCallback;
|
||||
import android.support.test.filters.SmallTest;
|
||||
import android.support.test.runner.AndroidJUnit4;
|
||||
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
|
||||
@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);
|
||||
@ -173,4 +188,34 @@ 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);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user