Search in sources :

Example 1 with BroadcastInterceptingContext

use of com.android.internal.util.test.BroadcastInterceptingContext in project platform_frameworks_base by android.

the class NetworkPolicyManagerServiceTest method callSystemReady.

@Before
public void callSystemReady() throws Exception {
    MockitoAnnotations.initMocks(this);
    final Context context = InstrumentationRegistry.getContext();
    setCurrentTimeMillis(TEST_START);
    // intercept various broadcasts, and pretend that uids have packages
    mServiceContext = new BroadcastInterceptingContext(context) {

        @Override
        public PackageManager getPackageManager() {
            return mPackageManager;
        }

        @Override
        public void startActivity(Intent intent) {
        // ignored
        }
    };
    mPolicyDir = context.getFilesDir();
    if (mPolicyDir.exists()) {
        IoUtils.deleteContents(mPolicyDir);
    }
    doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            mUidObserver = (IUidObserver) invocation.getArguments()[0];
            Log.d(TAG, "set mUidObserver to " + mUidObserver);
            return null;
        }
    }).when(mActivityManager).registerUidObserver(any(), anyInt());
    mService = new NetworkPolicyManagerService(mServiceContext, mActivityManager, mStatsService, mNetworkManager, mTime, mPolicyDir, true);
    mService.bindConnectivityManager(mConnManager);
    mService.bindNotificationManager(mNotifManager);
    mPolicyListener = new NetworkPolicyListenerAnswer(mService);
    // Sets some common expectations.
    when(mPackageManager.getPackageInfo(anyString(), anyInt())).thenAnswer(new Answer<PackageInfo>() {

        @Override
        public PackageInfo answer(InvocationOnMock invocation) throws Throwable {
            final String packageName = (String) invocation.getArguments()[0];
            final PackageInfo info = new PackageInfo();
            final Signature signature;
            if ("android".equals(packageName)) {
                signature = new Signature("F00D");
            } else {
                signature = new Signature("DEAD");
            }
            info.signatures = new Signature[] { signature };
            return info;
        }
    });
    when(mPackageManager.getApplicationInfoAsUser(anyString(), anyInt(), anyInt())).thenReturn(new ApplicationInfo());
    when(mPackageManager.getPackagesForUid(UID_A)).thenReturn(new String[] { PKG_NAME_A });
    when(mNetworkManager.isBandwidthControlEnabled()).thenReturn(true);
    expectCurrentTime();
    // Prepare NPMS.
    mService.systemReady();
    // catch INetworkManagementEventObserver during systemReady()
    ArgumentCaptor<INetworkManagementEventObserver> networkObserver = ArgumentCaptor.forClass(INetworkManagementEventObserver.class);
    verify(mNetworkManager).registerObserver(networkObserver.capture());
    mNetworkObserver = networkObserver.getValue();
}
Also used : BroadcastInterceptingContext(com.android.internal.util.test.BroadcastInterceptingContext) Context(android.content.Context) PackageInfo(android.content.pm.PackageInfo) BroadcastInterceptingContext(com.android.internal.util.test.BroadcastInterceptingContext) ApplicationInfo(android.content.pm.ApplicationInfo) Intent(android.content.Intent) Matchers.anyString(org.mockito.Matchers.anyString) IUidObserver(android.app.IUidObserver) NetworkPolicyManagerService(com.android.server.net.NetworkPolicyManagerService) PackageManager(android.content.pm.PackageManager) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Signature(android.content.pm.Signature) INetworkManagementEventObserver(android.net.INetworkManagementEventObserver) Before(org.junit.Before)

Example 2 with BroadcastInterceptingContext

use of com.android.internal.util.test.BroadcastInterceptingContext in project platform_frameworks_base by android.

the class NetworkManagementServiceTest method setUp.

@Override
public void setUp() throws Exception {
    super.setUp();
    // TODO: make this unnecessary. runtest might already make it unnecessary.
    System.setProperty("dexmaker.dexcache", getContext().getCacheDir().toString());
    // Set up a sheltered test environment.
    BroadcastInterceptingContext context = new BroadcastInterceptingContext(getContext());
    mServerSocket = new LocalServerSocket(SOCKET_NAME);
    // Start the service and wait until it connects to our socket.
    mNMService = NetworkManagementService.create(context, SOCKET_NAME);
    mSocket = mServerSocket.accept();
    mOutputStream = mSocket.getOutputStream();
}
Also used : LocalServerSocket(android.net.LocalServerSocket) BroadcastInterceptingContext(com.android.internal.util.test.BroadcastInterceptingContext)

Example 3 with BroadcastInterceptingContext

use of com.android.internal.util.test.BroadcastInterceptingContext in project platform_frameworks_base by android.

the class NetworkStatsServiceTest method setUp.

@Before
public void setUp() throws Exception {
    MockitoAnnotations.initMocks(this);
    final Context context = InstrumentationRegistry.getContext();
    mServiceContext = new BroadcastInterceptingContext(context);
    mStatsDir = context.getFilesDir();
    if (mStatsDir.exists()) {
        IoUtils.deleteContents(mStatsDir);
    }
    PowerManager powerManager = (PowerManager) mServiceContext.getSystemService(Context.POWER_SERVICE);
    PowerManager.WakeLock wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);
    mService = new NetworkStatsService(mServiceContext, mNetManager, mAlarmManager, wakeLock, mTime, TelephonyManager.getDefault(), mSettings, new NetworkStatsObservers(), mStatsDir, getBaseDir(mStatsDir));
    mHandlerThread = new IdleableHandlerThread("HandlerThread");
    mHandlerThread.start();
    Handler.Callback callback = new NetworkStatsService.HandlerCallback(mService);
    mHandler = new Handler(mHandlerThread.getLooper(), callback);
    mService.setHandler(mHandler, callback);
    mService.bindConnectivityManager(mConnManager);
    mElapsedRealtime = 0L;
    expectCurrentTime();
    expectDefaultSettings();
    expectNetworkStatsUidDetail(buildEmptyStats());
    expectSystemReady();
    mService.systemReady();
    mSession = mService.openSession();
    assertNotNull("openSession() failed", mSession);
    // catch INetworkManagementEventObserver during systemReady()
    ArgumentCaptor<INetworkManagementEventObserver> networkObserver = ArgumentCaptor.forClass(INetworkManagementEventObserver.class);
    verify(mNetManager).registerObserver(networkObserver.capture());
    mNetworkObserver = networkObserver.getValue();
}
Also used : BroadcastInterceptingContext(com.android.internal.util.test.BroadcastInterceptingContext) Context(android.content.Context) PowerManager(android.os.PowerManager) BroadcastInterceptingContext(com.android.internal.util.test.BroadcastInterceptingContext) NetworkStatsService(com.android.server.net.NetworkStatsService) Handler(android.os.Handler) IdleHandler(android.os.MessageQueue.IdleHandler) INetworkManagementEventObserver(android.net.INetworkManagementEventObserver) Before(org.junit.Before)

Aggregations

BroadcastInterceptingContext (com.android.internal.util.test.BroadcastInterceptingContext)3 Context (android.content.Context)2 INetworkManagementEventObserver (android.net.INetworkManagementEventObserver)2 Before (org.junit.Before)2 IUidObserver (android.app.IUidObserver)1 Intent (android.content.Intent)1 ApplicationInfo (android.content.pm.ApplicationInfo)1 PackageInfo (android.content.pm.PackageInfo)1 PackageManager (android.content.pm.PackageManager)1 Signature (android.content.pm.Signature)1 LocalServerSocket (android.net.LocalServerSocket)1 Handler (android.os.Handler)1 IdleHandler (android.os.MessageQueue.IdleHandler)1 PowerManager (android.os.PowerManager)1 NetworkPolicyManagerService (com.android.server.net.NetworkPolicyManagerService)1 NetworkStatsService (com.android.server.net.NetworkStatsService)1 Matchers.anyString (org.mockito.Matchers.anyString)1 InvocationOnMock (org.mockito.invocation.InvocationOnMock)1