Merge "Update VPN tests to mock out createAttributionContext"

This commit is contained in:
Yan Yan
2023-06-13 17:34:57 +00:00
committed by Gerrit Code Review

View File

@@ -75,12 +75,15 @@ import java.util.List;
@IgnoreUpTo(R) // VpnManagerService is not available before R @IgnoreUpTo(R) // VpnManagerService is not available before R
@SmallTest @SmallTest
public class VpnManagerServiceTest extends VpnTestBase { public class VpnManagerServiceTest extends VpnTestBase {
private static final String CONTEXT_ATTRIBUTION_TAG = "VPN_MANAGER";
@Rule @Rule
public final DevSdkIgnoreRule mIgnoreRule = new DevSdkIgnoreRule(); public final DevSdkIgnoreRule mIgnoreRule = new DevSdkIgnoreRule();
private static final int TIMEOUT_MS = 2_000; private static final int TIMEOUT_MS = 2_000;
@Mock Context mContext; @Mock Context mContext;
@Mock Context mContextWithoutAttributionTag;
@Mock Context mSystemContext; @Mock Context mSystemContext;
@Mock Context mUserAllContext; @Mock Context mUserAllContext;
private HandlerThread mHandlerThread; private HandlerThread mHandlerThread;
@@ -144,6 +147,13 @@ public class VpnManagerServiceTest extends VpnTestBase {
mHandlerThread = new HandlerThread("TestVpnManagerService"); mHandlerThread = new HandlerThread("TestVpnManagerService");
mDeps = new VpnManagerServiceDependencies(); mDeps = new VpnManagerServiceDependencies();
// The attribution tag is a dependency for IKE library to collect VPN metrics correctly
// and thus should not be changed without updating the IKE code.
doReturn(mContext)
.when(mContextWithoutAttributionTag)
.createAttributionContext(CONTEXT_ATTRIBUTION_TAG);
doReturn(mUserAllContext).when(mContext).createContextAsUser(UserHandle.ALL, 0); doReturn(mUserAllContext).when(mContext).createContextAsUser(UserHandle.ALL, 0);
doReturn(mSystemContext).when(mContext).createContextAsUser(UserHandle.SYSTEM, 0); doReturn(mSystemContext).when(mContext).createContextAsUser(UserHandle.SYSTEM, 0);
doReturn(mPackageManager).when(mContext).getPackageManager(); doReturn(mPackageManager).when(mContext).getPackageManager();
@@ -153,7 +163,7 @@ public class VpnManagerServiceTest extends VpnTestBase {
mockService(mContext, UserManager.class, Context.USER_SERVICE, mUserManager); mockService(mContext, UserManager.class, Context.USER_SERVICE, mUserManager);
doReturn(SYSTEM_USER).when(mUserManager).getUserInfo(eq(SYSTEM_USER_ID)); doReturn(SYSTEM_USER).when(mUserManager).getUserInfo(eq(SYSTEM_USER_ID));
mService = new VpnManagerService(mContext, mDeps); mService = new VpnManagerService(mContextWithoutAttributionTag, mDeps);
mService.systemReady(); mService.systemReady();
final ArgumentCaptor<BroadcastReceiver> intentReceiverCaptor = final ArgumentCaptor<BroadcastReceiver> intentReceiverCaptor =