Fix NetworkCallback expecting wrong network
By current wifi design, wifi will report disconnected and re-connected when changing from non-metered to metered. However, the cts test app listens for all network, which will get mobile network as active network when wifi is changing meteredness. This is not expected. And causes test failures when DUT has both wifi and mobile connections. Thus, this change pass request to track currently active network to the test app and register only for transport types of currently active network to prevent from getting unexpected network. Test: atest CtsHostsideNetworkTests:com.android.cts.net.HostsideNetworkCallbackTests Bug: 182516128 Merged-In: I2dce6035b13472bbdc2609009d690aac96280033 Change-Id: I2dce6035b13472bbdc2609009d690aac96280033 (cherry-picked from ag/14029457)
This commit is contained in:
@@ -24,6 +24,6 @@ interface IMyService {
|
||||
String checkNetworkStatus();
|
||||
String getRestrictBackgroundStatus();
|
||||
void sendNotification(int notificationId, String notificationType);
|
||||
void registerNetworkCallback(in INetworkCallback cb);
|
||||
void registerNetworkCallback(in NetworkRequest request, in INetworkCallback cb);
|
||||
void unregisterNetworkCallback();
|
||||
}
|
||||
|
||||
@@ -47,6 +47,7 @@ import android.content.IntentFilter;
|
||||
import android.net.ConnectivityManager;
|
||||
import android.net.NetworkInfo.DetailedState;
|
||||
import android.net.NetworkInfo.State;
|
||||
import android.net.NetworkRequest;
|
||||
import android.os.BatteryManager;
|
||||
import android.os.Binder;
|
||||
import android.os.Bundle;
|
||||
@@ -713,8 +714,10 @@ public abstract class AbstractRestrictBackgroundNetworkTestCase {
|
||||
fail("app2 receiver is not ready");
|
||||
}
|
||||
|
||||
protected void registerNetworkCallback(INetworkCallback cb) throws Exception {
|
||||
mServiceClient.registerNetworkCallback(cb);
|
||||
protected void registerNetworkCallback(final NetworkRequest request, INetworkCallback cb)
|
||||
throws Exception {
|
||||
Log.i(TAG, "Registering network callback for request: " + request);
|
||||
mServiceClient.registerNetworkCallback(request, cb);
|
||||
}
|
||||
|
||||
protected void unregisterNetworkCallback() throws Exception {
|
||||
|
||||
@@ -20,12 +20,11 @@ import android.content.ComponentName;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.ServiceConnection;
|
||||
import android.net.NetworkRequest;
|
||||
import android.os.ConditionVariable;
|
||||
import android.os.IBinder;
|
||||
import android.os.RemoteException;
|
||||
|
||||
import com.android.cts.net.hostside.IMyService;
|
||||
|
||||
public class MyServiceClient {
|
||||
private static final int TIMEOUT_MS = 5000;
|
||||
private static final String PACKAGE = MyServiceClient.class.getPackage().getName();
|
||||
@@ -93,12 +92,14 @@ public class MyServiceClient {
|
||||
return mService.getRestrictBackgroundStatus();
|
||||
}
|
||||
|
||||
public void sendNotification(int notificationId, String notificationType) throws RemoteException {
|
||||
public void sendNotification(int notificationId, String notificationType)
|
||||
throws RemoteException {
|
||||
mService.sendNotification(notificationId, notificationType);
|
||||
}
|
||||
|
||||
public void registerNetworkCallback(INetworkCallback cb) throws RemoteException {
|
||||
mService.registerNetworkCallback(cb);
|
||||
public void registerNetworkCallback(final NetworkRequest request, INetworkCallback cb)
|
||||
throws RemoteException {
|
||||
mService.registerNetworkCallback(request, cb);
|
||||
}
|
||||
|
||||
public void unregisterNetworkCallback() throws RemoteException {
|
||||
|
||||
@@ -19,6 +19,7 @@ package com.android.cts.net.hostside;
|
||||
import static android.net.NetworkCapabilities.NET_CAPABILITY_NOT_METERED;
|
||||
|
||||
import static com.android.cts.net.hostside.NetworkPolicyTestUtils.canChangeActiveNetworkMeteredness;
|
||||
import static com.android.cts.net.hostside.NetworkPolicyTestUtils.getActiveNetworkCapabilities;
|
||||
import static com.android.cts.net.hostside.NetworkPolicyTestUtils.setRestrictBackground;
|
||||
import static com.android.cts.net.hostside.Property.BATTERY_SAVER_MODE;
|
||||
import static com.android.cts.net.hostside.Property.DATA_SAVER_MODE;
|
||||
@@ -29,6 +30,7 @@ import static org.junit.Assume.assumeTrue;
|
||||
|
||||
import android.net.Network;
|
||||
import android.net.NetworkCapabilities;
|
||||
import android.net.NetworkRequest;
|
||||
import android.util.Log;
|
||||
|
||||
import org.junit.After;
|
||||
@@ -195,11 +197,16 @@ public class NetworkCallbackTest extends AbstractRestrictBackgroundNetworkTestCa
|
||||
setBatterySaverMode(false);
|
||||
setRestrictBackground(false);
|
||||
|
||||
// Get transports of the active network, this has to be done before changing meteredness,
|
||||
// since wifi will be disconnected when changing from non-metered to metered.
|
||||
final NetworkCapabilities networkCapabilities = getActiveNetworkCapabilities();
|
||||
|
||||
// Mark network as metered.
|
||||
mMeterednessConfiguration.configureNetworkMeteredness(true);
|
||||
|
||||
// Register callback
|
||||
registerNetworkCallback((INetworkCallback.Stub) mTestNetworkCallback);
|
||||
registerNetworkCallback(new NetworkRequest.Builder()
|
||||
.setCapabilities(networkCapabilities).build(), mTestNetworkCallback);
|
||||
// Wait for onAvailable() callback to ensure network is available before the test
|
||||
// and store the default network.
|
||||
mNetwork = mTestNetworkCallback.expectAvailableCallbackAndGetNetwork();
|
||||
|
||||
@@ -201,7 +201,7 @@ public class NetworkPolicyTestUtils {
|
||||
}
|
||||
}
|
||||
|
||||
private static NetworkCapabilities getActiveNetworkCapabilities() {
|
||||
static NetworkCapabilities getActiveNetworkCapabilities() {
|
||||
final Network activeNetwork = getConnectivityManager().getActiveNetwork();
|
||||
assertNotNull("No active network available", activeNetwork);
|
||||
return getConnectivityManager().getNetworkCapabilities(activeNetwork);
|
||||
|
||||
@@ -90,7 +90,7 @@ public class MyService extends Service {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void registerNetworkCallback(INetworkCallback cb) {
|
||||
public void registerNetworkCallback(final NetworkRequest request, INetworkCallback cb) {
|
||||
if (mNetworkCallback != null) {
|
||||
Log.d(TAG, "unregister previous network callback: " + mNetworkCallback);
|
||||
unregisterNetworkCallback();
|
||||
@@ -138,7 +138,7 @@ public class MyService extends Service {
|
||||
}
|
||||
}
|
||||
};
|
||||
mCm.registerNetworkCallback(makeNetworkRequest(), mNetworkCallback);
|
||||
mCm.registerNetworkCallback(request, mNetworkCallback);
|
||||
try {
|
||||
cb.asBinder().linkToDeath(() -> unregisterNetworkCallback(), 0);
|
||||
} catch (RemoteException e) {
|
||||
@@ -156,12 +156,6 @@ public class MyService extends Service {
|
||||
}
|
||||
};
|
||||
|
||||
private NetworkRequest makeNetworkRequest() {
|
||||
return new NetworkRequest.Builder()
|
||||
.addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
|
||||
.build();
|
||||
}
|
||||
|
||||
@Override
|
||||
public IBinder onBind(Intent intent) {
|
||||
return mBinder;
|
||||
|
||||
Reference in New Issue
Block a user