Merge "Fix NetworkCallback expecting wrong network" into sc-dev
This commit is contained in:
@@ -26,7 +26,7 @@ interface IMyService {
|
|||||||
String checkNetworkStatus();
|
String checkNetworkStatus();
|
||||||
String getRestrictBackgroundStatus();
|
String getRestrictBackgroundStatus();
|
||||||
void sendNotification(int notificationId, String notificationType);
|
void sendNotification(int notificationId, String notificationType);
|
||||||
void registerNetworkCallback(in INetworkCallback cb);
|
void registerNetworkCallback(in NetworkRequest request, in INetworkCallback cb);
|
||||||
void unregisterNetworkCallback();
|
void unregisterNetworkCallback();
|
||||||
void scheduleJob(in JobInfo jobInfo);
|
void scheduleJob(in JobInfo jobInfo);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -49,6 +49,7 @@ import android.content.IntentFilter;
|
|||||||
import android.net.ConnectivityManager;
|
import android.net.ConnectivityManager;
|
||||||
import android.net.NetworkInfo.DetailedState;
|
import android.net.NetworkInfo.DetailedState;
|
||||||
import android.net.NetworkInfo.State;
|
import android.net.NetworkInfo.State;
|
||||||
|
import android.net.NetworkRequest;
|
||||||
import android.os.BatteryManager;
|
import android.os.BatteryManager;
|
||||||
import android.os.Binder;
|
import android.os.Binder;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
@@ -750,8 +751,10 @@ public abstract class AbstractRestrictBackgroundNetworkTestCase {
|
|||||||
fail("app2 receiver is not ready");
|
fail("app2 receiver is not ready");
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void registerNetworkCallback(INetworkCallback cb) throws Exception {
|
protected void registerNetworkCallback(final NetworkRequest request, INetworkCallback cb)
|
||||||
mServiceClient.registerNetworkCallback(cb);
|
throws Exception {
|
||||||
|
Log.i(TAG, "Registering network callback for request: " + request);
|
||||||
|
mServiceClient.registerNetworkCallback(request, cb);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void unregisterNetworkCallback() throws Exception {
|
protected void unregisterNetworkCallback() throws Exception {
|
||||||
|
|||||||
@@ -21,12 +21,11 @@ import android.content.ComponentName;
|
|||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.content.ServiceConnection;
|
import android.content.ServiceConnection;
|
||||||
|
import android.net.NetworkRequest;
|
||||||
import android.os.ConditionVariable;
|
import android.os.ConditionVariable;
|
||||||
import android.os.IBinder;
|
import android.os.IBinder;
|
||||||
import android.os.RemoteException;
|
import android.os.RemoteException;
|
||||||
|
|
||||||
import com.android.cts.net.hostside.IMyService;
|
|
||||||
|
|
||||||
public class MyServiceClient {
|
public class MyServiceClient {
|
||||||
private static final int TIMEOUT_MS = 5000;
|
private static final int TIMEOUT_MS = 5000;
|
||||||
private static final String PACKAGE = MyServiceClient.class.getPackage().getName();
|
private static final String PACKAGE = MyServiceClient.class.getPackage().getName();
|
||||||
@@ -94,12 +93,14 @@ public class MyServiceClient {
|
|||||||
return mService.getRestrictBackgroundStatus();
|
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);
|
mService.sendNotification(notificationId, notificationType);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void registerNetworkCallback(INetworkCallback cb) throws RemoteException {
|
public void registerNetworkCallback(final NetworkRequest request, INetworkCallback cb)
|
||||||
mService.registerNetworkCallback(cb);
|
throws RemoteException {
|
||||||
|
mService.registerNetworkCallback(request, cb);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void unregisterNetworkCallback() throws RemoteException {
|
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 android.net.NetworkCapabilities.NET_CAPABILITY_NOT_METERED;
|
||||||
|
|
||||||
import static com.android.cts.net.hostside.NetworkPolicyTestUtils.canChangeActiveNetworkMeteredness;
|
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.NetworkPolicyTestUtils.setRestrictBackground;
|
||||||
import static com.android.cts.net.hostside.Property.BATTERY_SAVER_MODE;
|
import static com.android.cts.net.hostside.Property.BATTERY_SAVER_MODE;
|
||||||
import static com.android.cts.net.hostside.Property.DATA_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.Network;
|
||||||
import android.net.NetworkCapabilities;
|
import android.net.NetworkCapabilities;
|
||||||
|
import android.net.NetworkRequest;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
import org.junit.After;
|
import org.junit.After;
|
||||||
@@ -195,11 +197,16 @@ public class NetworkCallbackTest extends AbstractRestrictBackgroundNetworkTestCa
|
|||||||
setBatterySaverMode(false);
|
setBatterySaverMode(false);
|
||||||
setRestrictBackground(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.
|
// Mark network as metered.
|
||||||
mMeterednessConfiguration.configureNetworkMeteredness(true);
|
mMeterednessConfiguration.configureNetworkMeteredness(true);
|
||||||
|
|
||||||
// Register callback
|
// 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
|
// Wait for onAvailable() callback to ensure network is available before the test
|
||||||
// and store the default network.
|
// and store the default network.
|
||||||
mNetwork = mTestNetworkCallback.expectAvailableCallbackAndGetNetwork();
|
mNetwork = mTestNetworkCallback.expectAvailableCallbackAndGetNetwork();
|
||||||
|
|||||||
@@ -208,7 +208,7 @@ public class NetworkPolicyTestUtils {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static NetworkCapabilities getActiveNetworkCapabilities() {
|
static NetworkCapabilities getActiveNetworkCapabilities() {
|
||||||
final Network activeNetwork = getConnectivityManager().getActiveNetwork();
|
final Network activeNetwork = getConnectivityManager().getActiveNetwork();
|
||||||
assertNotNull("No active network available", activeNetwork);
|
assertNotNull("No active network available", activeNetwork);
|
||||||
return getConnectivityManager().getNetworkCapabilities(activeNetwork);
|
return getConnectivityManager().getNetworkCapabilities(activeNetwork);
|
||||||
|
|||||||
@@ -92,7 +92,7 @@ public class MyService extends Service {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void registerNetworkCallback(INetworkCallback cb) {
|
public void registerNetworkCallback(final NetworkRequest request, INetworkCallback cb) {
|
||||||
if (mNetworkCallback != null) {
|
if (mNetworkCallback != null) {
|
||||||
Log.d(TAG, "unregister previous network callback: " + mNetworkCallback);
|
Log.d(TAG, "unregister previous network callback: " + mNetworkCallback);
|
||||||
unregisterNetworkCallback();
|
unregisterNetworkCallback();
|
||||||
@@ -140,7 +140,7 @@ public class MyService extends Service {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
mCm.registerNetworkCallback(makeNetworkRequest(), mNetworkCallback);
|
mCm.registerNetworkCallback(request, mNetworkCallback);
|
||||||
try {
|
try {
|
||||||
cb.asBinder().linkToDeath(() -> unregisterNetworkCallback(), 0);
|
cb.asBinder().linkToDeath(() -> unregisterNetworkCallback(), 0);
|
||||||
} catch (RemoteException e) {
|
} catch (RemoteException e) {
|
||||||
@@ -165,12 +165,6 @@ public class MyService extends Service {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
private NetworkRequest makeNetworkRequest() {
|
|
||||||
return new NetworkRequest.Builder()
|
|
||||||
.addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
|
|
||||||
.build();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public IBinder onBind(Intent intent) {
|
public IBinder onBind(Intent intent) {
|
||||||
return mBinder;
|
return mBinder;
|
||||||
|
|||||||
Reference in New Issue
Block a user