DataIdleTestpublic class DataIdleTest extends android.test.InstrumentationTestCase A test that dumps data usage to instrumentation out, used for measuring data usage for idle
devices. |
Fields Summary |
---|
private android.telephony.TelephonyManager | mTelephonyManager | private android.net.INetworkStatsService | mStatsService | private static final String | LOG_TAG | private static final int | INSTRUMENTATION_IN_PROGRESS |
Methods Summary |
---|
private void | fetchStats(android.net.NetworkTemplate template)Helper method that fetches all the network stats available and reports it
to instrumentation out.
INetworkStatsSession session = null;
try {
mStatsService.forceUpdate();
session = mStatsService.openSession();
final NetworkStats stats = session.getSummaryForAllUid(
template, Long.MIN_VALUE, Long.MAX_VALUE, false);
reportStats(stats);
} catch (RemoteException e) {
Log.w(LOG_TAG, "Failed to fetch network stats.");
} finally {
TrafficStats.closeQuietly(session);
}
| void | reportStats(android.net.NetworkStats stats)Print network data usage stats to instrumentation out
Bundle result = new Bundle();
long rxBytes = 0;
long txBytes = 0;
long rxPackets = 0;
long txPackets = 0;
for (int i = 0; i < stats.size(); ++i) {
// Label will be iface_uid_tag_set
Entry statsEntry = stats.getValues(i, null);
// Debugging use.
/*
String labelTemplate = String.format("%s_%d_%d_%d", statsEntry.iface, statsEntry.uid,
statsEntry.tag, statsEntry.set) + "_%s";
result.putLong(String.format(labelTemplate, "rxBytes"), statsEntry.rxBytes);
result.putLong(String.format(labelTemplate, "txBytes"), statsEntry.txBytes);
*/
rxPackets += statsEntry.rxPackets;
rxBytes += statsEntry.rxBytes;
txPackets += statsEntry.txPackets;
txBytes += statsEntry.txBytes;
}
result.putLong("Total rx Bytes", rxBytes);
result.putLong("Total tx Bytes", txBytes);
result.putLong("Total rx Packets", rxPackets);
result.putLong("Total tx Packets", txPackets);
getInstrumentation().sendStatus(INSTRUMENTATION_IN_PROGRESS, result);
| protected void | setUp()
super.setUp();
Context c = getInstrumentation().getTargetContext();
mStatsService = INetworkStatsService.Stub.asInterface(
ServiceManager.getService(Context.NETWORK_STATS_SERVICE));
mTelephonyManager = (TelephonyManager) c.getSystemService(Context.TELEPHONY_SERVICE);
| public void | testMobile()Test that dumps all the data usage metrics for all mobile to instrumentation out.
String subscriberId = mTelephonyManager.getSubscriberId();
NetworkTemplate template = NetworkTemplate.buildTemplateMobileAll(subscriberId);
fetchStats(template);
| public void | testWifiIdle()Test that dumps all the data usage metrics for wifi to instrumentation out.
NetworkTemplate template = NetworkTemplate.buildTemplateWifiWildcard();
fetchStats(template);
|
|