Home | History | Annotate | Download | only in dataidle
      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.NetworkStats.Entry;
     21 import android.net.NetworkTemplate;
     22 import android.net.NetworkStats;
     23 import android.os.Bundle;
     24 import android.os.RemoteException;
     25 import android.os.ServiceManager;
     26 import android.telephony.TelephonyManager;
     27 import android.test.InstrumentationTestCase;
     28 import android.test.InstrumentationTestRunner;
     29 import android.util.Log;
     30 
     31 /**
     32  * A test that dumps data usage to instrumentation out, used for measuring data usage for idle
     33  * devices.
     34  */
     35 public class DataIdleTest extends InstrumentationTestCase {
     36 
     37     private TelephonyManager mTelephonyManager;
     38     private INetworkStatsService mStatsService;
     39 
     40     private static final String LOG_TAG = "DataIdleTest";
     41     private final static int INSTRUMENTATION_IN_PROGRESS = 2;
     42 
     43     protected void setUp() throws Exception {
     44         super.setUp();
     45         Context c = getInstrumentation().getTargetContext();
     46         mStatsService = INetworkStatsService.Stub.asInterface(
     47                 ServiceManager.getService(Context.NETWORK_STATS_SERVICE));
     48         mTelephonyManager = (TelephonyManager) c.getSystemService(Context.TELEPHONY_SERVICE);
     49     }
     50 
     51     /**
     52      * Test that dumps all the data usage metrics for wifi to instrumentation out.
     53      */
     54     public void testWifiIdle() {
     55         NetworkTemplate template = NetworkTemplate.buildTemplateWifi();
     56         fetchStats(template);
     57     }
     58 
     59     /**
     60      * Test that dumps all the data usage metrics for all mobile to instrumentation out.
     61      */
     62     public void testMobile() {
     63         String subscriberId = mTelephonyManager.getSubscriberId();
     64         NetworkTemplate template = NetworkTemplate.buildTemplateMobileAll(subscriberId);
     65         fetchStats(template);
     66     }
     67 
     68     /**
     69      * Helper method that fetches all the network stats available and reports it
     70      * to instrumentation out.
     71      * @param template {link {@link NetworkTemplate} to match.
     72      */
     73     private void fetchStats(NetworkTemplate template) {
     74         try {
     75             mStatsService.forceUpdate();
     76             NetworkStats stats = mStatsService.getSummaryForAllUid(template, Long.MIN_VALUE,
     77                     Long.MAX_VALUE, false);
     78             reportStats(stats);
     79         } catch (RemoteException e) {
     80             Log.w(LOG_TAG, "Failed to fetch network stats.");
     81         }
     82     }
     83 
     84     /**
     85      * Print network data usage stats to instrumentation out
     86      * @param stats {@link NetworkorStats} to print
     87      */
     88     void reportStats(NetworkStats stats) {
     89         Bundle result = new Bundle();
     90         long rxBytes = 0;
     91         long txBytes = 0;
     92         long rxPackets = 0;
     93         long txPackets = 0;
     94         for (int i = 0; i < stats.size(); ++i) {
     95             // Label will be iface_uid_tag_set
     96             Entry  statsEntry = stats.getValues(i, null);
     97             // Debugging use.
     98             /*
     99             String labelTemplate = String.format("%s_%d_%d_%d", statsEntry.iface, statsEntry.uid,
    100                     statsEntry.tag, statsEntry.set) + "_%s";
    101             result.putLong(String.format(labelTemplate, "rxBytes"), statsEntry.rxBytes);
    102             result.putLong(String.format(labelTemplate, "txBytes"), statsEntry.txBytes);
    103             */
    104             rxPackets += statsEntry.rxPackets;
    105             rxBytes += statsEntry.rxBytes;
    106             txPackets += statsEntry.txPackets;
    107             txBytes += statsEntry.txBytes;
    108         }
    109         result.putLong("Total rx Bytes", rxBytes);
    110         result.putLong("Total tx Bytes", txBytes);
    111         result.putLong("Total rx Packets", rxPackets);
    112         result.putLong("Total tx Packets", txPackets);
    113         getInstrumentation().sendStatus(INSTRUMENTATION_IN_PROGRESS, result);
    114 
    115     }
    116 }
    117