blob: 9c5478d393880d524a2b887fe36e29040cca5f22 [file] [log] [blame]
David Hu8cc9a8e2011-12-13 15:57:42 -08001// Copyright 2011 Google Inc. All Rights Reserved.
2
3package android.test;
4
5import android.net.NetworkStats;
6import android.net.NetworkStats.Entry;
7import android.net.TrafficStats;
8import android.os.Bundle;
9
10import junit.framework.AssertionFailedError;
11import junit.framework.Test;
12import junit.framework.TestCase;
13import junit.framework.TestListener;
14
15import java.lang.reflect.Method;
16
17/**
18 * A specialized {@link android.app.Instrumentation} that can collect Bandwidth statistics during
19 * the execution of the test.
20 * This is used in place of {@link InstrumentationTestRunner}.
21 * i.e. adb shell am instrumentation -w -e class com.android.foo.FooTest \
22 * com.android.foo/android.test.BandwidthTestRunner
23 *
24 * @see NetworkStats and @see TrafficStats for details of the collected statistics
25 * @hide
26 */
27public class BandwidthTestRunner extends InstrumentationTestRunner implements TestListener {
28 private static final String REPORT_KEY_PACKETS_SENT = "txPackets";
29 private static final String REPORT_KEY_PACKETS_RECEIVED = "rxPackets";
30 private static final String REPORT_KEY_BYTES_SENT = "txBytes";
31 private static final String REPORT_KEY_BYTES_RECEIVED = "rxBytes";
32 private static final String REPORT_KEY_OPERATIONS = "operations";
33
34 private boolean mHasClassAnnotation;
35 private boolean mIsBandwidthTest;
36 private Bundle mTestResult;
37
38 @Override
39 public void onCreate(Bundle arguments) {
40 super.onCreate(arguments);
41 addTestListener(this);
42 }
43
44 public void addError(Test test, Throwable t) {
45 }
46
47 public void addFailure(Test test, AssertionFailedError t) {
48 }
49
50 public void startTest(Test test) {
51 String testClass = test.getClass().getName();
52 String testName = ((TestCase)test).getName();
53 Method testMethod = null;
54 try {
55 testMethod = test.getClass().getMethod(testName);
56 } catch (NoSuchMethodException e) {
57 // ignore- the test with given name does not exist. Will be handled during test
58 // execution
59 }
60 try {
61 // Look for BandwdthTest annotation on both test class and test method
62 if (testMethod != null ) {
63 if (testMethod.isAnnotationPresent(BandwidthTest.class) ){
64 mIsBandwidthTest = true;
65 TrafficStats.startDataProfiling(null);
66 } else if (test.getClass().isAnnotationPresent(BandwidthTest.class)){
67 mIsBandwidthTest = true;
68 TrafficStats.startDataProfiling(null);
69 }
70 }
71 } catch (SecurityException e) {
72 // ignore - the test with given name cannot be accessed. Will be handled during
73 // test execution
74 }
75 }
76
77 public void endTest(Test test) {
78 if (mIsBandwidthTest){
79 mTestResult = new Bundle();
80 mIsBandwidthTest=false;
81 NetworkStats stats = TrafficStats.stopDataProfiling(null);
82 Entry entry = stats.getTotal(null);
83 mTestResult.putLong(REPORT_KEY_BYTES_RECEIVED, entry.rxBytes);
84 mTestResult.putLong(REPORT_KEY_BYTES_SENT, entry.txBytes);
85 mTestResult.putLong(REPORT_KEY_PACKETS_RECEIVED, entry.rxPackets);
86 mTestResult.putLong(REPORT_KEY_PACKETS_SENT, entry.txPackets);
87 mTestResult.putLong(REPORT_KEY_OPERATIONS, entry.operations);
88 System.out.println(mTestResult.toString());
89 sendStatus(0, mTestResult);
90 }
91 }
92}