|
| 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 | + NetworkStats stats = mStatsService.getSummaryForAllUid(template, Long.MIN_VALUE, |
| 76 | + Long.MAX_VALUE, false); |
| 77 | + reportStats(stats); |
| 78 | + } catch (RemoteException e) { |
| 79 | + Log.w(LOG_TAG, "Failed to fetch network stats for wifi."); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Print network data usage stats to instrumentation out |
| 85 | + * @param stats {@link NetworkorStats} to print |
| 86 | + */ |
| 87 | + void reportStats(NetworkStats stats) { |
| 88 | + for (int i = 0; i < stats.size(); ++i) { |
| 89 | + Entry statsEntry = stats.getValues(i, null); |
| 90 | + Bundle result = new Bundle(); |
| 91 | + result.putInt("uid", statsEntry.uid); |
| 92 | + result.putInt("tag", statsEntry.tag); |
| 93 | + result.putInt("set", statsEntry.set); |
| 94 | + result.putString("iface", statsEntry.iface); |
| 95 | + result.putLong("rxBytes", statsEntry.rxBytes); |
| 96 | + result.putLong("txBytes", statsEntry.txBytes); |
| 97 | + getInstrumentation().sendStatus(INSTRUMENTATION_IN_PROGRESS, result); |
| 98 | + } |
| 99 | + } |
| 100 | +} |
0 commit comments