summaryrefslogtreecommitdiffstats
path: root/tests/DataIdleTest/src/com/android/tests/dataidle/DataIdleTest.java
blob: 919e2b332da4b649ec6e0834aa52e43a4fb40156 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*
 * Copyright (C) 2011 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.android.tests.dataidle;

import android.content.Context;
import android.net.INetworkStatsService;
import android.net.INetworkStatsSession;
import android.net.NetworkStats;
import android.net.NetworkStats.Entry;
import android.net.NetworkTemplate;
import android.net.TrafficStats;
import android.os.Bundle;
import android.os.RemoteException;
import android.os.ServiceManager;
import android.telephony.TelephonyManager;
import android.test.InstrumentationTestCase;
import android.util.Log;

/**
 * A test that dumps data usage to instrumentation out, used for measuring data usage for idle
 * devices.
 */
public class DataIdleTest extends InstrumentationTestCase {

    private TelephonyManager mTelephonyManager;
    private INetworkStatsService mStatsService;

    private static final String LOG_TAG = "DataIdleTest";
    private final static int INSTRUMENTATION_IN_PROGRESS = 2;

    protected void setUp() throws Exception {
        super.setUp();
        Context c = getInstrumentation().getTargetContext();
        mStatsService = INetworkStatsService.Stub.asInterface(
                ServiceManager.getService(Context.NETWORK_STATS_SERVICE));
        mTelephonyManager = (TelephonyManager) c.getSystemService(Context.TELEPHONY_SERVICE);
    }

    /**
     * Test that dumps all the data usage metrics for wifi to instrumentation out.
     */
    public void testWifiIdle() {
        NetworkTemplate template = NetworkTemplate.buildTemplateWifiWildcard();
        fetchStats(template);
    }

    /**
     * Test that dumps all the data usage metrics for all mobile to instrumentation out.
     */
    public void testMobile() {
        String subscriberId = mTelephonyManager.getSubscriberId();
        NetworkTemplate template = NetworkTemplate.buildTemplateMobileAll(subscriberId);
        fetchStats(template);
    }

    /**
     * Helper method that fetches all the network stats available and reports it
     * to instrumentation out.
     * @param template {link {@link NetworkTemplate} to match.
     */
    private void fetchStats(NetworkTemplate template) {
        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);
        }
    }

    /**
     * Print network data usage stats to instrumentation out
     * @param stats {@link NetworkorStats} to print
     */
    void reportStats(NetworkStats stats) {
        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);

    }
}