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 static android.net.NetworkStats.METERED_YES;
19 
20 import android.app.usage.NetworkStats;
21 import android.app.usage.NetworkStatsManager;
22 import android.content.Context;
23 import android.net.NetworkTemplate;
24 import android.os.Bundle;
25 import android.telephony.TelephonyManager;
26 import android.test.InstrumentationTestCase;
27 import android.util.Log;
28 
29 import java.util.Set;
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 NetworkStatsManager mStatsManager;
39 
40     private static final String LOG_TAG = "DataIdleTest";
41     private final static int INSTRUMENTATION_IN_PROGRESS = 2;
42 
setUp()43     protected void setUp() throws Exception {
44         super.setUp();
45         Context c = getInstrumentation().getTargetContext();
46         mStatsManager = c.getSystemService(NetworkStatsManager.class);
47         mTelephonyManager = (TelephonyManager) c.getSystemService(Context.TELEPHONY_SERVICE);
48     }
49 
50     /**
51      * Test that dumps all the data usage metrics for wifi to instrumentation out.
52      */
testWifiIdle()53     public void testWifiIdle() {
54         final NetworkTemplate template = new NetworkTemplate
55                 .Builder(NetworkTemplate.MATCH_WIFI)
56                 .build();
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         final String subscriberId = mTelephonyManager.getSubscriberId();
65         NetworkTemplate template = new NetworkTemplate
66                 .Builder(NetworkTemplate.MATCH_MOBILE)
67                 .setMeteredness(METERED_YES)
68                 .setSubscriberIds(Set.of(subscriberId)).build();
69         fetchStats(template);
70     }
71 
72     /**
73      * Helper method that fetches all the network stats available and reports it
74      * to instrumentation out.
75      * @param template {@link NetworkTemplate} to match.
76      */
fetchStats(NetworkTemplate template)77     private void fetchStats(NetworkTemplate template) {
78         try {
79             mStatsManager.forceUpdate();
80             final NetworkStats.Bucket bucket =
81                     mStatsManager.querySummaryForDevice(template, Long.MIN_VALUE, Long.MAX_VALUE);
82             reportStats(bucket);
83         } catch (RuntimeException e) {
84             Log.w(LOG_TAG, "Failed to fetch network stats.");
85         }
86     }
87 
88     /**
89      * Print network data usage stats to instrumentation out
90      * @param bucket {@link NetworkStats} to print
91      */
reportStats(NetworkStats.Bucket bucket)92     void reportStats(NetworkStats.Bucket bucket) {
93         Bundle result = new Bundle();
94         result.putLong("Total rx Bytes", bucket.getRxBytes());
95         result.putLong("Total tx Bytes", bucket.getTxBytes());
96         result.putLong("Total rx Packets", bucket.getRxPackets());
97         result.putLong("Total tx Packets", bucket.getTxPackets());
98         getInstrumentation().sendStatus(INSTRUMENTATION_IN_PROGRESS, result);
99 
100     }
101 }
102