FileDocCategorySizeDatePackage
AutomaticActivity.javaAPI DocAndroid 5.1 API11313Thu Mar 12 22:22:42 GMT 2015com.android.test.hwuicompare

AutomaticActivity

public class AutomaticActivity extends CompareActivity

Fields Summary
private static final String
LOG_TAG
private static final float
ERROR_DISPLAY_THRESHOLD
protected static final boolean
DRAW_BITMAPS
private static final float
ERROR_CHANGE_THRESHOLD
Threshold of error change required to consider a test regressed/improved
private static final float[]
ERROR_CUTOFFS
private final float[]
mErrorRates
private float
mTotalTests
private float
mTotalError
private int
mTestsRegressed
private int
mTestsImproved
private android.widget.ImageView
mSoftwareImageView
private android.widget.ImageView
mHardwareImageView
private final ArrayList
mFinalCallbacks
Runnable
mRunnable
JSONObject
mOutputJson
JSONObject
mInputJson
final HashMap
mModifierResults
final HashMap
mIndividualResults
final HashMap
mModifierDiffResults
final HashMap
mIndividualDiffResults
Constructors Summary
Methods Summary
private static voidaddForAllModifiers(java.lang.String fullName, float error, java.lang.String[] modifierNames, java.util.HashMap modifierResults)
Inserts the error value into all TestResult objects, associated with each of its modifiers

        for (String modifierName : modifierNames) {
            TestResult r = modifierResults.get(fullName);
            if (r == null) {
                modifierResults.put(modifierName, new TestResult(modifierName, error));
            } else {
                r.addInto(error);
            }
        }
    
private voidbeginTest()

       
        mFinalCallbacks.add(new FinalCallback() {
            @Override
            void report(String name, float value) {
                Log.d(LOG_TAG, name + " " + value);
            };
        });

        File inputFile = new File(Environment.getExternalStorageDirectory(),
                "CanvasCompareInput.json");
        if (inputFile.exists() && inputFile.canRead() && inputFile.length() > 0) {
            try {
                FileInputStream inputStream = new FileInputStream(inputFile);
                Log.d(LOG_TAG, "Parsing input file...");
                StringBuffer content = new StringBuffer((int)inputFile.length());
                byte[] buffer = new byte[1024];
                while (inputStream.read(buffer) != -1) {
                    content.append(new String(buffer));
                }
                mInputJson = new JSONObject(content.toString());
                inputStream.close();
                Log.d(LOG_TAG, "Parsed input file with " + mInputJson.length() + " entries");
            } catch (JSONException e) {
                Log.e(LOG_TAG, "error parsing input json", e);
            } catch (IOException e) {
                Log.e(LOG_TAG, "error reading input json from sd", e);
            }
        }

        mOutputJson = new JSONObject();
    
private voidfinishTest()

        for (FinalCallback c : mFinalCallbacks) {
            c.report("averageError", (mTotalError / mTotalTests));
            for (int i = 1; i < ERROR_CUTOFFS.length; i++) {
                c.report(String.format("tests with error over %1.3f", ERROR_CUTOFFS[i]),
                        mErrorRates[i]);
            }
            if (mInputJson != null) {
                c.report("tests regressed", mTestsRegressed);
                c.report("tests improved", mTestsImproved);
            }
            c.complete();
        }

        try {
            if (mOutputJson != null) {
                String outputString = mOutputJson.toString(4);
                File outputFile = new File(Environment.getExternalStorageDirectory(),
                        "CanvasCompareOutput.json");
                FileOutputStream outputStream = new FileOutputStream(outputFile);
                outputStream.write(outputString.getBytes());
                outputStream.close();
                Log.d(LOG_TAG, "Saved output file with " + mOutputJson.length() + " entries");
            }
        } catch (JSONException e) {
            Log.e(LOG_TAG, "error during JSON stringify", e);
        } catch (IOException e) {
            Log.e(LOG_TAG, "error storing JSON output on sd", e);
        }

        logTestResultHash("Modifier change vs previous", mModifierDiffResults);
        logTestResultHash("Invidual test change vs previous", mIndividualDiffResults);
        logTestResultHash("Modifier average test results", mModifierResults);
        logTestResultHash("Individual test results", mIndividualResults);

        Toast.makeText(getApplicationContext(), "done!", Toast.LENGTH_SHORT).show();
        finish();
    
protected booleanforceRecreateBitmaps()

        // disable, unless needed for drawing into imageviews
        return DRAW_BITMAPS;
    
private voidhandleError(java.lang.String[] modifierNames, float error)

        String fullName = "";
        for (String s : modifierNames) {
            fullName = fullName.concat("." + s);
        }
        fullName = fullName.substring(1);

        float deltaError = 0;
        if (mInputJson != null) {
            try {
                deltaError = error - (float)mInputJson.getDouble(fullName);
            } catch (JSONException e) {
                Log.w(LOG_TAG, "Warning: unable to read from input json", e);
            }
            if (deltaError > ERROR_CHANGE_THRESHOLD) mTestsRegressed++;
            if (deltaError < -ERROR_CHANGE_THRESHOLD) mTestsImproved++;
            mIndividualDiffResults.put(fullName, new TestResult(fullName, deltaError));
            addForAllModifiers(fullName, deltaError, modifierNames, mModifierDiffResults);
        }

        mIndividualResults.put(fullName, new TestResult(fullName, error));
        addForAllModifiers(fullName, error, modifierNames, mModifierResults);

        try {
            if (mOutputJson != null) {
                mOutputJson.put(fullName, error);
            }
        } catch (JSONException e) {
            Log.e(LOG_TAG, "exception during JSON recording", e);
            mOutputJson = null;
        }

        for (int i = 0; i < ERROR_CUTOFFS.length; i++) {
            if (error <= ERROR_CUTOFFS[i]) break;
            mErrorRates[i]++;
        }
        mTotalError += error;
        mTotalTests++;
    
private static voidlogTestResultHash(java.lang.String label, java.util.HashMap map)

        Log.d(LOG_TAG, "---------------");
        Log.d(LOG_TAG, label + ":");
        Log.d(LOG_TAG, "---------------");
        TreeSet<TestResult> set = new TreeSet<TestResult>(new Comparator<TestResult>() {
            @Override
            public int compare(TestResult lhs, TestResult rhs) {
                if (lhs == rhs) return 0; // don't need to worry about complex equality

                int cmp = Float.compare(lhs.getAverage(), rhs.getAverage());
                if (cmp != 0) {
                    return cmp;
                }
                return lhs.mLabel.compareTo(rhs.mLabel);
            }
        });

        for (TestResult t : map.values()) {
            set.add(t);
        }

        for (TestResult t : set.descendingSet()) {
            if (Math.abs(t.getAverage()) > ERROR_DISPLAY_THRESHOLD) {
                Log.d(LOG_TAG, String.format("%2.4f : %s", t.getAverage(), t.mLabel));
            }
        }
        Log.d(LOG_TAG, "");
    
protected voidonCreate(android.os.Bundle savedInstanceState)

        super.onCreate(savedInstanceState);
        setContentView(R.layout.automatic_layout);

        mSoftwareImageView = (ImageView) findViewById(R.id.software_image_view);
        mHardwareImageView = (ImageView) findViewById(R.id.hardware_image_view);

        onCreateCommon(mRunnable);
        beginTest();
    
protected voidonPause()


    
       
        super.onPause();
        mHandler.removeCallbacks(mRunnable);
    
public voidsetFinalCallback(com.android.test.hwuicompare.AutomaticActivity$FinalCallback c)

        mFinalCallbacks.add(c);