Skip to content

Commit 3ae29d4

Browse files
author
Tsu Chiang Chuang
committed
idle test app for bandwidth usage.
Change-Id: Ib35b27617369b360972e653db70bf5cf9303ddf1
1 parent 1d124d5 commit 3ae29d4

3 files changed

Lines changed: 161 additions & 0 deletions

File tree

tests/DataIdleTest/Android.mk

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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+
LOCAL_PATH := $(call my-dir)
17+
include $(CLEAR_VARS)
18+
19+
# We only want this apk build for tests.
20+
LOCAL_MODULE_TAGS := tests
21+
22+
LOCAL_PACKAGE_NAME := DataIdleTest
23+
LOCAL_JAVA_LIBRARIES := android.test.runner
24+
LOCAL_SRC_FILES := $(call all-java-files-under, src)
25+
26+
# We need to sign it to get access to the network usage history.
27+
LOCAL_CERTIFICATE := platform
28+
29+
include $(BUILD_PACKAGE)
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?xml version="1.0" encoding="utf-8"?>
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+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
17+
package="com.android.tests.dataidle"
18+
android:sharedUserId="android.uid.system">
19+
<uses-permission android:name="android.permission.INTERNET" />
20+
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
21+
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
22+
<uses-permission android:name="android.permission.READ_NETWORK_USAGE_HISTORY" />
23+
<application >
24+
<uses-library android:name="android.test.runner" />
25+
</application>
26+
27+
<instrumentation
28+
android:name="android.test.InstrumentationTestRunner"
29+
android:targetPackage="com.android.tests.dataidle"
30+
android:label="Idle Bandwidth Tests" />
31+
32+
</manifest>
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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

Comments
 (0)