FileDocCategorySizeDatePackage
BandwidthTestCase.javaAPI DocAndroid 5.1 API6343Thu Mar 12 22:22:12 GMT 2015android.test

BandwidthTestCase

public class BandwidthTestCase extends InstrumentationTestCase
A bandwidth test case that collects bandwidth statistics for tests that are annotated with {@link BandwidthTest} otherwise the test is executed as an {@link InstrumentationTestCase}

Fields Summary
private static final String
TAG
private static final String
REPORT_KEY_PACKETS_SENT
private static final String
REPORT_KEY_PACKETS_RECEIVED
private static final String
REPORT_KEY_BYTES_SENT
private static final String
REPORT_KEY_BYTES_RECEIVED
private static final String
REPORT_KEY_OPERATIONS
Constructors Summary
Methods Summary
private android.os.BundlegetBandwidthStats(NetworkStats.Entry entry)

        Bundle bundle = new Bundle();
        bundle.putLong(REPORT_KEY_BYTES_RECEIVED, entry.rxBytes);
        bundle.putLong(REPORT_KEY_BYTES_SENT, entry.txBytes);
        bundle.putLong(REPORT_KEY_PACKETS_RECEIVED, entry.rxPackets);
        bundle.putLong(REPORT_KEY_PACKETS_SENT, entry.txPackets);
        bundle.putLong(REPORT_KEY_OPERATIONS, entry.operations);
        return bundle;
    
private voidrunMethod(java.lang.reflect.Method runMethod, int tolerance, boolean isRepetitive)

        //This is a copy of {@link InstrumentationTestCase#runMethod}
        Throwable exception = null;

        int runCount = 0;
        do {
            try {
                runMethod.invoke(this, (Object[]) null);
                exception = null;
            } catch (InvocationTargetException e) {
                e.fillInStackTrace();
                exception = e.getTargetException();
            } catch (IllegalAccessException e) {
                e.fillInStackTrace();
                exception = e;
            } finally {
                runCount++;
                // Report current iteration number, if test is repetitive
                if (isRepetitive) {
                    Bundle iterations = new Bundle();
                    iterations.putInt("currentiterations", runCount);
                    getInstrumentation().sendStatus(2, iterations);
                }
            }
        } while ((runCount < tolerance) && (isRepetitive || exception != null));

        if (exception != null) {
            throw exception;
        }
    
protected voidrunTest()


    
         
        //This is a copy of {@link InstrumentationTestCase#runTest} with
        //added logic to handle bandwidth measurements
        String fName = getName();
        assertNotNull(fName);
        Method method = null;
        Class testClass = null;
        try {
            // use getMethod to get all public inherited
            // methods. getDeclaredMethods returns all
            // methods of this class but excludes the
            // inherited ones.
            testClass = getClass();
            method = testClass.getMethod(fName, (Class[]) null);
        } catch (NoSuchMethodException e) {
            fail("Method \""+fName+"\" not found");
        }

        if (!Modifier.isPublic(method.getModifiers())) {
            fail("Method \""+fName+"\" should be public");
        }

        int runCount = 1;
        boolean isRepetitive = false;
        if (method.isAnnotationPresent(FlakyTest.class)) {
            runCount = method.getAnnotation(FlakyTest.class).tolerance();
        } else if (method.isAnnotationPresent(RepetitiveTest.class)) {
            runCount = method.getAnnotation(RepetitiveTest.class).numIterations();
            isRepetitive = true;
        }

        if (method.isAnnotationPresent(UiThreadTest.class)) {
            final int tolerance = runCount;
            final boolean repetitive = isRepetitive;
            final Method testMethod = method;
            final Throwable[] exceptions = new Throwable[1];
            getInstrumentation().runOnMainSync(new Runnable() {
                public void run() {
                    try {
                        runMethod(testMethod, tolerance, repetitive);
                    } catch (Throwable throwable) {
                        exceptions[0] = throwable;
                    }
                }
            });
            if (exceptions[0] != null) {
                throw exceptions[0];
            }
        } else if (method.isAnnotationPresent(BandwidthTest.class) ||
                testClass.isAnnotationPresent(BandwidthTest.class)) {
            /**
             * If bandwidth profiling fails for whatever reason the test
             * should be allow to execute to its completion.
             * Typically bandwidth profiling would fail when a lower level
             * component is missing, such as the kernel module, for a newly
             * introduced hardware.
             */
            try{
                TrafficStats.startDataProfiling(null);
            } catch(IllegalStateException isx){
                Log.w(TAG, "Failed to start bandwidth profiling");
            }
            runMethod(method, 1, false);
            try{
                NetworkStats stats = TrafficStats.stopDataProfiling(null);
                NetworkStats.Entry entry = stats.getTotal(null);
                getInstrumentation().sendStatus(2, getBandwidthStats(entry));
            } catch (IllegalStateException isx){
                Log.w(TAG, "Failed to collect bandwidth stats");
            }
        } else {
            runMethod(method, runCount, isRepetitive);
        }