[BR14] Tracker Data Saver status in ConnectivityService

BYPASS_INCLUSIVE_LANGUAGE_REASON=Using public API
Test: atest ConnectivityCoverageTests:android.net.connectivity.com.android.server.CSBpfNetMapsTest
      (on U and V devices respectively)
Bug: 297836825
Fix: 314858283

Change-Id: Ibeac8cd3a33468c6539234255328affc6b3f8ffe
This commit is contained in:
Junyu Lai
2023-12-04 07:52:19 +00:00
parent 6f95ab2558
commit 38c7503ab5
4 changed files with 128 additions and 156 deletions

View File

@@ -16,13 +16,6 @@
package android.net;
import static android.content.Context.RECEIVER_NOT_EXPORTED;
import static android.content.pm.ApplicationInfo.FLAG_PERSISTENT;
import static android.content.pm.ApplicationInfo.FLAG_SYSTEM;
import static android.net.ConnectivityManager.ACTION_RESTRICT_BACKGROUND_CHANGED;
import static android.net.ConnectivityManager.RESTRICT_BACKGROUND_STATUS_DISABLED;
import static android.net.ConnectivityManager.RESTRICT_BACKGROUND_STATUS_ENABLED;
import static android.net.ConnectivityManager.RESTRICT_BACKGROUND_STATUS_WHITELISTED;
import static android.net.ConnectivityManager.TYPE_NONE;
import static android.net.NetworkCapabilities.NET_CAPABILITY_CBS;
import static android.net.NetworkCapabilities.NET_CAPABILITY_DUN;
@@ -46,7 +39,6 @@ import static android.net.NetworkRequest.Type.TRACK_SYSTEM_DEFAULT;
import static com.android.testutils.MiscAsserts.assertThrows;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
@@ -70,10 +62,7 @@ import static org.mockito.Mockito.when;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.ApplicationInfo;
import android.net.ConnectivityManager.DataSaverStatusTracker;
import android.net.ConnectivityManager.NetworkCallback;
import android.os.Build.VERSION_CODES;
import android.os.Bundle;
@@ -527,44 +516,6 @@ public class ConnectivityManagerTest {
+ " attempts", ref.get());
}
@DevSdkIgnoreRule.IgnoreAfter(VERSION_CODES.UPSIDE_DOWN_CAKE)
@Test
public void testDataSaverStatusTracker() {
mockService(NetworkPolicyManager.class, Context.NETWORK_POLICY_SERVICE, mNpm);
// Mock proper application info.
doReturn(mCtx).when(mCtx).getApplicationContext();
final ApplicationInfo mockAppInfo = new ApplicationInfo();
mockAppInfo.flags = FLAG_PERSISTENT | FLAG_SYSTEM;
doReturn(mockAppInfo).when(mCtx).getApplicationInfo();
// Enable data saver.
doReturn(RESTRICT_BACKGROUND_STATUS_ENABLED).when(mNpm)
.getRestrictBackgroundStatus(anyInt());
final DataSaverStatusTracker tracker = new DataSaverStatusTracker(mCtx);
// Verify the data saver status is correct right after initialization.
assertTrue(tracker.getDataSaverEnabled());
// Verify the tracker register receiver with expected intent filter.
final ArgumentCaptor<IntentFilter> intentFilterCaptor =
ArgumentCaptor.forClass(IntentFilter.class);
verify(mCtx).registerReceiver(
any(), intentFilterCaptor.capture(), eq(RECEIVER_NOT_EXPORTED));
assertEquals(ACTION_RESTRICT_BACKGROUND_CHANGED,
intentFilterCaptor.getValue().getAction(0));
// Mock data saver status changed event and verify the tracker tracks the
// status accordingly.
doReturn(RESTRICT_BACKGROUND_STATUS_DISABLED).when(mNpm)
.getRestrictBackgroundStatus(anyInt());
tracker.onReceive(mCtx, new Intent(ACTION_RESTRICT_BACKGROUND_CHANGED));
assertFalse(tracker.getDataSaverEnabled());
doReturn(RESTRICT_BACKGROUND_STATUS_WHITELISTED).when(mNpm)
.getRestrictBackgroundStatus(anyInt());
tracker.onReceive(mCtx, new Intent(ACTION_RESTRICT_BACKGROUND_CHANGED));
assertTrue(tracker.getDataSaverEnabled());
}
private <T> void mockService(Class<T> clazz, String name, T service) {
doReturn(service).when(mCtx).getSystemService(name);
doReturn(name).when(mCtx).getSystemServiceName(clazz);