1 /* 2 * Copyright (C) 2011 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package com.android.tests.dataidle; 17 18 import android.content.Context; 19 import android.net.INetworkStatsService; 20 import android.net.INetworkStatsSession; 21 import android.net.NetworkStats; 22 import android.net.NetworkStats.Entry; 23 import android.net.NetworkTemplate; 24 import android.net.TrafficStats; 25 import android.os.Bundle; 26 import android.os.RemoteException; 27 import android.os.ServiceManager; 28 import android.telephony.TelephonyManager; 29 import android.test.InstrumentationTestCase; 30 import android.util.Log; 31 32 /** 33 * A test that dumps data usage to instrumentation out, used for measuring data usage for idle 34 * devices. 35 */ 36 public class DataIdleTest extends InstrumentationTestCase { 37 38 private TelephonyManager mTelephonyManager; 39 private INetworkStatsService mStatsService; 40 41 private static final String LOG_TAG = "DataIdleTest"; 42 private final static int INSTRUMENTATION_IN_PROGRESS = 2; 43 setUp()44 protected void setUp() throws Exception { 45 super.setUp(); 46 Context c = getInstrumentation().getTargetContext(); 47 mStatsService = INetworkStatsService.Stub.asInterface( 48 ServiceManager.getService(Context.NETWORK_STATS_SERVICE)); 49 mTelephonyManager = (TelephonyManager) c.getSystemService(Context.TELEPHONY_SERVICE); 50 } 51 52 /** 53 * Test that dumps all the data usage metrics for wifi to instrumentation out. 54 */ testWifiIdle()55 public void testWifiIdle() { 56 NetworkTemplate template = NetworkTemplate.buildTemplateWifiWildcard(); 57 fetchStats(template); 58 } 59 60 /** 61 * Test that dumps all the data usage metrics for all mobile to instrumentation out. 62 */ testMobile()63 public void testMobile() { 64 String subscriberId = mTelephonyManager.getSubscriberId(); 65 NetworkTemplate template = NetworkTemplate.buildTemplateMobileAll(subscriberId); 66 fetchStats(template); 67 } 68 69 /** 70 * Helper method that fetches all the network stats available and reports it 71 * to instrumentation out. 72 * @param template {@link NetworkTemplate} to match. 73 */ fetchStats(NetworkTemplate template)74 private void fetchStats(NetworkTemplate template) { 75 INetworkStatsSession session = null; 76 try { 77 mStatsService.forceUpdate(); 78 session = mStatsService.openSession(); 79 final NetworkStats stats = session.getSummaryForAllUid( 80 template, Long.MIN_VALUE, Long.MAX_VALUE, false); 81 reportStats(stats); 82 } catch (RemoteException e) { 83 Log.w(LOG_TAG, "Failed to fetch network stats."); 84 } finally { 85 TrafficStats.closeQuietly(session); 86 } 87 } 88 89 /** 90 * Print network data usage stats to instrumentation out 91 * @param stats {@link NetworkorStats} to print 92 */ reportStats(NetworkStats stats)93 void reportStats(NetworkStats stats) { 94 Bundle result = new Bundle(); 95 long rxBytes = 0; 96 long txBytes = 0; 97 long rxPackets = 0; 98 long txPackets = 0; 99 for (int i = 0; i < stats.size(); ++i) { 100 // Label will be iface_uid_tag_set 101 Entry statsEntry = stats.getValues(i, null); 102 // Debugging use. 103 /* 104 String labelTemplate = String.format("%s_%d_%d_%d", statsEntry.iface, statsEntry.uid, 105 statsEntry.tag, statsEntry.set) + "_%s"; 106 result.putLong(String.format(labelTemplate, "rxBytes"), statsEntry.rxBytes); 107 result.putLong(String.format(labelTemplate, "txBytes"), statsEntry.txBytes); 108 */ 109 rxPackets += statsEntry.rxPackets; 110 rxBytes += statsEntry.rxBytes; 111 txPackets += statsEntry.txPackets; 112 txBytes += statsEntry.txBytes; 113 } 114 result.putLong("Total rx Bytes", rxBytes); 115 result.putLong("Total tx Bytes", txBytes); 116 result.putLong("Total rx Packets", rxPackets); 117 result.putLong("Total tx Packets", txPackets); 118 getInstrumentation().sendStatus(INSTRUMENTATION_IN_PROGRESS, result); 119 120 } 121 } 122