Search in sources :

Example 71 with DataUsageRequest

use of android.net.DataUsageRequest in project android_frameworks_base by crdroidandroid.

the class NetworkStatsServiceTest method testRegisterUsageCallback.

public void testRegisterUsageCallback() throws Exception {
    // pretend that wifi network comes online; service should ask about full
    // network state, and poll any existing interfaces before updating.
    expectCurrentTime();
    expectDefaultSettings();
    expectNetworkState(buildWifiState());
    expectNetworkStatsSummary(buildEmptyStats());
    expectNetworkStatsUidDetail(buildEmptyStats());
    expectNetworkStatsPoll();
    expectBandwidthControlCheck();
    replay();
    mService.forceUpdateIfaces();
    // verify service has empty history for wifi
    assertNetworkTotal(sTemplateWifi, 0L, 0L, 0L, 0L, 0);
    verifyAndReset();
    String callingPackage = "the.calling.package";
    // very small; should be overriden by framework
    long thresholdInBytes = 1L;
    DataUsageRequest inputRequest = new DataUsageRequest(DataUsageRequest.REQUEST_ID_UNSET, sTemplateWifi, thresholdInBytes);
    // Create a messenger that waits for callback activity
    ConditionVariable cv = new ConditionVariable(false);
    LatchedHandler latchedHandler = new LatchedHandler(Looper.getMainLooper(), cv);
    Messenger messenger = new Messenger(latchedHandler);
    // Allow binder to connect
    IBinder mockBinder = createMock(IBinder.class);
    mockBinder.linkToDeath((IBinder.DeathRecipient) anyObject(), anyInt());
    EasyMock.replay(mockBinder);
    // Force poll
    expectCurrentTime();
    expectDefaultSettings();
    expectNetworkStatsSummary(buildEmptyStats());
    expectNetworkStatsUidDetail(buildEmptyStats());
    expectNetworkStatsPoll();
    replay();
    // Register and verify request and that binder was called
    DataUsageRequest request = mService.registerUsageCallback(callingPackage, inputRequest, messenger, mockBinder);
    assertTrue(request.requestId > 0);
    assertTrue(Objects.equals(sTemplateWifi, request.template));
    // 2 MB
    long minThresholdInBytes = 2 * 1024 * 1024;
    assertEquals(minThresholdInBytes, request.thresholdInBytes);
    // Send dummy message to make sure that any previous message has been handled
    mHandler.sendMessage(mHandler.obtainMessage(-1));
    mHandlerThread.waitForIdle(WAIT_TIMEOUT);
    verifyAndReset();
    // Make sure that the caller binder gets connected
    EasyMock.verify(mockBinder);
    EasyMock.reset(mockBinder);
    // modify some number on wifi, and trigger poll event
    // not enough traffic to call data usage callback
    incrementCurrentTime(HOUR_IN_MILLIS);
    expectCurrentTime();
    expectDefaultSettings();
    expectNetworkStatsSummary(new NetworkStats(getElapsedRealtime(), 1).addIfaceValues(TEST_IFACE, 1024L, 1L, 2048L, 2L));
    expectNetworkStatsUidDetail(buildEmptyStats());
    expectNetworkStatsPoll();
    replay();
    forcePollAndWaitForIdle();
    // verify service recorded history
    verifyAndReset();
    assertNetworkTotal(sTemplateWifi, 1024L, 1L, 2048L, 2L, 0);
    // make sure callback has not being called
    assertEquals(INVALID_TYPE, latchedHandler.mLastMessageType);
    // and bump forward again, with counters going higher. this is
    // important, since it will trigger the data usage callback
    incrementCurrentTime(DAY_IN_MILLIS);
    expectCurrentTime();
    expectDefaultSettings();
    expectNetworkStatsSummary(new NetworkStats(getElapsedRealtime(), 1).addIfaceValues(TEST_IFACE, 4096000L, 4L, 8192000L, 8L));
    expectNetworkStatsUidDetail(buildEmptyStats());
    expectNetworkStatsPoll();
    replay();
    forcePollAndWaitForIdle();
    // verify service recorded history
    assertNetworkTotal(sTemplateWifi, 4096000L, 4L, 8192000L, 8L, 0);
    verifyAndReset();
    // Wait for the caller to ack receipt of CALLBACK_LIMIT_REACHED
    assertTrue(cv.block(WAIT_TIMEOUT));
    assertEquals(NetworkStatsManager.CALLBACK_LIMIT_REACHED, latchedHandler.mLastMessageType);
    cv.close();
    // Allow binder to disconnect
    expect(mockBinder.unlinkToDeath((IBinder.DeathRecipient) anyObject(), anyInt())).andReturn(true);
    EasyMock.replay(mockBinder);
    // Unregister request
    mService.unregisterUsageRequest(request);
    // Wait for the caller to ack receipt of CALLBACK_RELEASED
    assertTrue(cv.block(WAIT_TIMEOUT));
    assertEquals(NetworkStatsManager.CALLBACK_RELEASED, latchedHandler.mLastMessageType);
    // Make sure that the caller binder gets disconnected
    EasyMock.verify(mockBinder);
}
Also used : ConditionVariable(android.os.ConditionVariable) DataUsageRequest(android.net.DataUsageRequest) IBinder(android.os.IBinder) NetworkStats(android.net.NetworkStats) Messenger(android.os.Messenger)

Example 72 with DataUsageRequest

use of android.net.DataUsageRequest in project android_frameworks_base by crdroidandroid.

the class NetworkStatsObserversTest method testRegister_highThreshold_accepted.

public void testRegister_highThreshold_accepted() throws Exception {
    long highThresholdBytes = 2 * THRESHOLD_BYTES;
    DataUsageRequest inputRequest = new DataUsageRequest(DataUsageRequest.REQUEST_ID_UNSET, sTemplateWifi, highThresholdBytes);
    DataUsageRequest request = mStatsObservers.register(inputRequest, mMessenger, mockBinder, Process.SYSTEM_UID, NetworkStatsAccess.Level.DEVICE);
    assertTrue(request.requestId > 0);
    assertTrue(Objects.equals(sTemplateWifi, request.template));
    assertEquals(highThresholdBytes, request.thresholdInBytes);
}
Also used : DataUsageRequest(android.net.DataUsageRequest)

Example 73 with DataUsageRequest

use of android.net.DataUsageRequest in project android_frameworks_base by crdroidandroid.

the class NetworkStatsObserversTest method testRegister_twoRequests_twoIds.

public void testRegister_twoRequests_twoIds() throws Exception {
    DataUsageRequest inputRequest = new DataUsageRequest(DataUsageRequest.REQUEST_ID_UNSET, sTemplateWifi, THRESHOLD_BYTES);
    DataUsageRequest request1 = mStatsObservers.register(inputRequest, mMessenger, mockBinder, Process.SYSTEM_UID, NetworkStatsAccess.Level.DEVICE);
    assertTrue(request1.requestId > 0);
    assertTrue(Objects.equals(sTemplateWifi, request1.template));
    assertEquals(THRESHOLD_BYTES, request1.thresholdInBytes);
    DataUsageRequest request2 = mStatsObservers.register(inputRequest, mMessenger, mockBinder, Process.SYSTEM_UID, NetworkStatsAccess.Level.DEVICE);
    assertTrue(request2.requestId > request1.requestId);
    assertTrue(Objects.equals(sTemplateWifi, request2.template));
    assertEquals(THRESHOLD_BYTES, request2.thresholdInBytes);
}
Also used : DataUsageRequest(android.net.DataUsageRequest)

Example 74 with DataUsageRequest

use of android.net.DataUsageRequest in project android_frameworks_base by crdroidandroid.

the class NetworkStatsObserversTest method testUpdateStats_defaultAccess_notifiesSameUid.

public void testUpdateStats_defaultAccess_notifiesSameUid() throws Exception {
    DataUsageRequest inputRequest = new DataUsageRequest(DataUsageRequest.REQUEST_ID_UNSET, sTemplateImsi1, THRESHOLD_BYTES);
    DataUsageRequest request = mStatsObservers.register(inputRequest, mMessenger, mockBinder, UID_RED, NetworkStatsAccess.Level.DEFAULT);
    assertTrue(request.requestId > 0);
    assertTrue(Objects.equals(sTemplateImsi1, request.template));
    assertEquals(THRESHOLD_BYTES, request.thresholdInBytes);
    NetworkIdentitySet identSet = new NetworkIdentitySet();
    identSet.add(new NetworkIdentity(TYPE_MOBILE, TelephonyManager.NETWORK_TYPE_UNKNOWN, IMSI_1, null, /* networkId */
    false, /* roaming */
    true));
    mActiveUidIfaces.put(TEST_IFACE, identSet);
    // Baseline
    NetworkStats xtSnapshot = null;
    NetworkStats uidSnapshot = new NetworkStats(TEST_START, 2).addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, ROAMING_NO, BASE_BYTES, 2L, BASE_BYTES, 2L, 0L);
    mStatsObservers.updateStats(xtSnapshot, uidSnapshot, mActiveIfaces, mActiveUidIfaces, VPN_INFO, TEST_START);
    // Delta
    uidSnapshot = new NetworkStats(TEST_START + 2 * MINUTE_IN_MILLIS, 2).addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, ROAMING_NO, BASE_BYTES + THRESHOLD_BYTES, 2L, BASE_BYTES + THRESHOLD_BYTES, 2L, 0L);
    mStatsObservers.updateStats(xtSnapshot, uidSnapshot, mActiveIfaces, mActiveUidIfaces, VPN_INFO, TEST_START);
    waitForObserverToIdle();
    assertTrue(mCv.block(WAIT_TIMEOUT));
    assertEquals(NetworkStatsManager.CALLBACK_LIMIT_REACHED, mHandler.mLastMessageType);
}
Also used : DataUsageRequest(android.net.DataUsageRequest) NetworkIdentity(android.net.NetworkIdentity) NetworkStats(android.net.NetworkStats)

Example 75 with DataUsageRequest

use of android.net.DataUsageRequest in project android_frameworks_base by crdroidandroid.

the class NetworkStatsServiceTest method testUnregisterUsageCallback_unknown_noop.

public void testUnregisterUsageCallback_unknown_noop() throws Exception {
    String callingPackage = "the.calling.package";
    // 10 MB
    long thresholdInBytes = 10 * 1024 * 1024;
    DataUsageRequest unknownRequest = new DataUsageRequest(2, /* requestId */
    sTemplateImsi1, thresholdInBytes);
    mService.unregisterUsageRequest(unknownRequest);
}
Also used : DataUsageRequest(android.net.DataUsageRequest)

Aggregations

DataUsageRequest (android.net.DataUsageRequest)75 NetworkStats (android.net.NetworkStats)32 NetworkIdentity (android.net.NetworkIdentity)28 Messenger (android.os.Messenger)9 NetworkTemplate (android.net.NetworkTemplate)5 Binder (android.os.Binder)5 Looper (android.os.Looper)5 RemoteException (android.os.RemoteException)5 ConditionVariable (android.os.ConditionVariable)4 IBinder (android.os.IBinder)3 Test (org.junit.Test)2