FileDocCategorySizeDatePackage
TestRecorder.javaAPI DocAndroid 1.5 API6586Wed May 06 22:42:02 BST 2009android.test

TestRecorder

public class TestRecorder extends Object implements TestRunner.Listener, TestListener
{@hide} Not needed for 1.0 SDK.

Fields Summary
private static final int
DATABASE_VERSION
private static android.database.sqlite.SQLiteDatabase
sDb
private Set
mFailedTests
Constructors Summary
public TestRecorder()

    
Methods Summary
public voidaddError(junit.framework.Test test, java.lang.Throwable t)

        mFailedTests.add(test.toString());
        failed(test.toString(), t);
    
public voidaddFailure(junit.framework.Test test, junit.framework.AssertionFailedError t)

        mFailedTests.add(test.toString());
        failed(test.toString(), t.getMessage());
    
public voidendTest(junit.framework.Test test)

        finished(test.toString());
        if (!mFailedTests.contains(test.toString())) {
            passed(test.toString());
        }
        mFailedTests.remove(test.toString());
    
public voidfailed(java.lang.String className, java.lang.Throwable exception)

        StringWriter stringWriter = new StringWriter();
        PrintWriter printWriter = new PrintWriter(stringWriter);
        try {
            exception.printStackTrace(printWriter);
        } finally {
            printWriter.close();
        }
        ContentValues map = new ContentValues();
        map.put("result", "failed");
        map.put("exception", stringWriter.toString());

        getDatabase().update("tests", map, "name = '" + className + "'", null);
    
public voidfailed(java.lang.String className, java.lang.String reason)
Reports a test case failure.

param
className Name of the class/test.
param
reason Reason for failure.

        ContentValues map = new ContentValues();
        map.put("result", "failed");
        // The reason is put as the exception.
        map.put("exception", reason);
        getDatabase().update("tests", map, "name = '" + className + "'", null);
    
public voidfinished(java.lang.String className)

        ContentValues map = new ContentValues(1);
        map.put("finished", System.currentTimeMillis());

        getDatabase().update("tests", map, "name = '" + className + "'", null);
    
private static android.database.sqlite.SQLiteDatabasegetDatabase()


        
        if (sDb == null) {
            File dir = new File(Environment.getDataDirectory(), "test_results");

            /* TODO: add a DB version number and bootstrap/upgrade methods
            * if the format of the table changes.
            */
            String dbName = "TestHarness.db";
            File file = new File(dir, dbName);
            sDb = SQLiteDatabase.openOrCreateDatabase(file.getPath(), null);

            if (sDb.getVersion() == 0) {
                int code = FileUtils.setPermissions(file.getPath(),
                        FileUtils.S_IRUSR | FileUtils.S_IWUSR |
                                FileUtils.S_IRGRP | FileUtils.S_IWGRP |
                                FileUtils.S_IROTH | FileUtils.S_IWOTH, -1, -1);
    
                if (code != 0) {
                    Log.w("TestRecorder",
                            "Set permissions for " + file.getPath() + " returned = " + code);
                }
    
                try {
                    sDb.execSQL("CREATE TABLE IF NOT EXISTS tests (_id INT PRIMARY KEY," +
                            "name TEXT," +
                            "result TEXT," +
                            "exception TEXT," +
                            "started INTEGER," +
                            "finished INTEGER," +
                            "time INTEGER," +
                            "iterations INTEGER," +
                            "allocations INTEGER," +
                            "parent INTEGER);");
                    sDb.setVersion(DATABASE_VERSION);
                } catch (Exception e) {
                    Log.e("TestRecorder", "failed to create table 'tests'", e);
                    sDb = null;
                }
            }
        }

        return sDb;
    
public voidpassed(java.lang.String className)

        ContentValues map = new ContentValues();
        map.put("result", "passed");

        getDatabase().update("tests", map, "name = '" + className + "'", null);
    
public voidperformance(java.lang.String className, long itemTimeNS, int iterations, java.util.List intermediates)

        ContentValues map = new ContentValues();
        map.put("time", itemTimeNS);
        map.put("iterations", iterations);

        getDatabase().update("tests", map, "name = '" + className + "'", null);

        if (intermediates != null && intermediates.size() > 0) {
            int n = intermediates.size();
            for (int i = 0; i < n; i++) {
                TestRunner.IntermediateTime time = intermediates.get(i);

                getDatabase().execSQL("INSERT INTO tests (name, time, parent) VALUES ('" +
                        time.name + "', " + time.timeInNS + ", " +
                        "(SELECT _id FROM tests WHERE name = '" + className + "'));");
            }
        }
    
public voidstartTest(junit.framework.Test test)

        started(test.toString());
    
public voidstarted(java.lang.String className)

        ContentValues map = new ContentValues(2);
        map.put("name", className);
        map.put("started", System.currentTimeMillis());

        // try to update the row first in case we've ran this test before.
        int rowsAffected = getDatabase().update("tests", map, "name = '" + className + "'", null);

        if (rowsAffected == 0) {
            getDatabase().insert("tests", null, map);
        }