Update VPN tests to mock out createAttributionContext

This allows VpnManagerServiceTest continues to work after
adding an attribution tag to VpnManagerService

Ignore-AOSP-First: This project will automerge from AOSP to
                   udc-mainline-prod but the dependency CL in
                   frameworks/base/ will not. Thus changes will be
                   merged to udc-dev first and then cherry picked
                   to AOSP
Bug: 278943609
Test: atest FrameworksNetTests:android.net.connectivity.com.android.server.VpnManagerServiceTest
Change-Id: I4228a7fb308b8d01aee53f0734f4c5753908473a
This commit is contained in:
Yan Yan
2023-06-07 19:05:00 +00:00
parent 177dade553
commit efe302556e

View File

@@ -75,12 +75,15 @@ import java.util.List;
@IgnoreUpTo(R) // VpnManagerService is not available before R
@SmallTest
public class VpnManagerServiceTest extends VpnTestBase {
private static final String CONTEXT_ATTRIBUTION_TAG = "VPN_MANAGER";
@Rule
public final DevSdkIgnoreRule mIgnoreRule = new DevSdkIgnoreRule();
private static final int TIMEOUT_MS = 2_000;
@Mock Context mContext;
@Mock Context mContextWithoutAttributionTag;
@Mock Context mSystemContext;
@Mock Context mUserAllContext;
private HandlerThread mHandlerThread;
@@ -144,6 +147,13 @@ public class VpnManagerServiceTest extends VpnTestBase {
mHandlerThread = new HandlerThread("TestVpnManagerService");
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(mSystemContext).when(mContext).createContextAsUser(UserHandle.SYSTEM, 0);
doReturn(mPackageManager).when(mContext).getPackageManager();
@@ -153,7 +163,7 @@ public class VpnManagerServiceTest extends VpnTestBase {
mockService(mContext, UserManager.class, Context.USER_SERVICE, mUserManager);
doReturn(SYSTEM_USER).when(mUserManager).getUserInfo(eq(SYSTEM_USER_ID));
mService = new VpnManagerService(mContext, mDeps);
mService = new VpnManagerService(mContextWithoutAttributionTag, mDeps);
mService.systemReady();
final ArgumentCaptor<BroadcastReceiver> intentReceiverCaptor =