| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
package android.test; |
| 17 |
|
| 18 |
import android.net.NetworkStats; |
| 19 |
import android.net.TrafficStats; |
| 20 |
import android.os.Bundle; |
| 21 |
import android.util.Log; |
| 22 |
|
| 23 |
import java.lang.reflect.InvocationTargetException; |
| 24 |
import java.lang.reflect.Method; |
| 25 |
import java.lang.reflect.Modifier; |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
public class BandwidthTestCase extends InstrumentationTestCase { |
| 33 |
private static final String TAG = "BandwidthTestCase"; |
| 34 |
private static final String REPORT_KEY_PACKETS_SENT = "txPackets"; |
| 35 |
private static final String REPORT_KEY_PACKETS_RECEIVED = "rxPackets"; |
| 36 |
private static final String REPORT_KEY_BYTES_SENT = "txBytes"; |
| 37 |
private static final String REPORT_KEY_BYTES_RECEIVED = "rxBytes"; |
| 38 |
private static final String REPORT_KEY_OPERATIONS = "operations"; |
| 39 |
|
| 40 |
@Override |
| 41 |
protected void runTest() throws Throwable { |
| 42 |
|
| 43 |
|
| 44 |
String fName = getName(); |
| 45 |
assertNotNull(fName); |
| 46 |
Method method = null; |
| 47 |
Class testClass = null; |
| 48 |
try { |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
testClass = getClass(); |
| 54 |
method = testClass.getMethod(fName, (Class[]) null); |
| 55 |
} catch (NoSuchMethodException e) { |
| 56 |
fail("Method \""+fName+"\" not found"); |
| 57 |
} |
| 58 |
|
| 59 |
if (!Modifier.isPublic(method.getModifiers())) { |
| 60 |
fail("Method \""+fName+"\" should be public"); |
| 61 |
} |
| 62 |
|
| 63 |
int runCount = 1; |
| 64 |
boolean isRepetitive = false; |
| 65 |
if (method.isAnnotationPresent(FlakyTest.class)) { |
| 66 |
runCount = method.getAnnotation(FlakyTest.class).tolerance(); |
| 67 |
} else if (method.isAnnotationPresent(RepetitiveTest.class)) { |
| 68 |
runCount = method.getAnnotation(RepetitiveTest.class).numIterations(); |
| 69 |
isRepetitive = true; |
| 70 |
} |
| 71 |
|
| 72 |
if (method.isAnnotationPresent(UiThreadTest.class)) { |
| 73 |
final int tolerance = runCount; |
| 74 |
final boolean repetitive = isRepetitive; |
| 75 |
final Method testMethod = method; |
| 76 |
final Throwable[] exceptions = new Throwable[1]; |
| 77 |
getInstrumentation().runOnMainSync(new Runnable() { |
| 78 |
public void run() { |
| 79 |
try { |
| 80 |
runMethod(testMethod, tolerance, repetitive); |
| 81 |
} catch (Throwable throwable) { |
| 82 |
exceptions[0] = throwable; |
| 83 |
} |
| 84 |
} |
| 85 |
}); |
| 86 |
if (exceptions[0] != null) { |
| 87 |
throw exceptions[0]; |
| 88 |
} |
| 89 |
} else if (method.isAnnotationPresent(BandwidthTest.class) || |
| 90 |
testClass.isAnnotationPresent(BandwidthTest.class)) { |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
try{ |
| 99 |
TrafficStats.startDataProfiling(null); |
| 100 |
} catch(IllegalStateException isx){ |
| 101 |
Log.w(TAG, "Failed to start bandwidth profiling"); |
| 102 |
} |
| 103 |
runMethod(method, 1, false); |
| 104 |
try{ |
| 105 |
NetworkStats stats = TrafficStats.stopDataProfiling(null); |
| 106 |
NetworkStats.Entry entry = stats.getTotal(null); |
| 107 |
getInstrumentation().sendStatus(2, getBandwidthStats(entry)); |
| 108 |
} catch (IllegalStateException isx){ |
| 109 |
Log.w(TAG, "Failed to collect bandwidth stats"); |
| 110 |
} |
| 111 |
} else { |
| 112 |
runMethod(method, runCount, isRepetitive); |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
private void runMethod(Method runMethod, int tolerance, boolean isRepetitive) throws Throwable { |
| 117 |
|
| 118 |
Throwable exception = null; |
| 119 |
|
| 120 |
int runCount = 0; |
| 121 |
do { |
| 122 |
try { |
| 123 |
runMethod.invoke(this, (Object[]) null); |
| 124 |
exception = null; |
| 125 |
} catch (InvocationTargetException e) { |
| 126 |
e.fillInStackTrace(); |
| 127 |
exception = e.getTargetException(); |
| 128 |
} catch (IllegalAccessException e) { |
| 129 |
e.fillInStackTrace(); |
| 130 |
exception = e; |
| 131 |
} finally { |
| 132 |
runCount++; |
| 133 |
|
| 134 |
if (isRepetitive) { |
| 135 |
Bundle iterations = new Bundle(); |
| 136 |
iterations.putInt("currentiterations", runCount); |
| 137 |
getInstrumentation().sendStatus(2, iterations); |
| 138 |
} |
| 139 |
} |
| 140 |
} while ((runCount < tolerance) && (isRepetitive || exception != null)); |
| 141 |
|
| 142 |
if (exception != null) { |
| 143 |
throw exception; |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
private Bundle getBandwidthStats(NetworkStats.Entry entry){ |
| 148 |
Bundle bundle = new Bundle(); |
| 149 |
bundle.putLong(REPORT_KEY_BYTES_RECEIVED, entry.rxBytes); |
| 150 |
bundle.putLong(REPORT_KEY_BYTES_SENT, entry.txBytes); |
| 151 |
bundle.putLong(REPORT_KEY_PACKETS_RECEIVED, entry.rxPackets); |
| 152 |
bundle.putLong(REPORT_KEY_PACKETS_SENT, entry.txPackets); |
| 153 |
bundle.putLong(REPORT_KEY_OPERATIONS, entry.operations); |
| 154 |
return bundle; |
| 155 |
} |
| 156 |
} |
| 157 |
|