Test runner cleanup: delete unused old classes.

Change-Id: Iaaabd47d4074f936a811fc1b6575088d70842564
This commit is contained in:
Brett Chabot
2011-06-01 18:38:44 -07:00
parent 287bd83f9e
commit 877d428e39
16 changed files with 0 additions and 1423 deletions

View File

@@ -1,79 +0,0 @@
/*
* Copyright (C) 2007 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.test;
import java.io.PrintStream;
import android.os.Bundle;
import junit.framework.AssertionFailedError;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.runner.BaseTestRunner;
import junit.textui.ResultPrinter;
/**
* Subclass of ResultPrinter that adds test case results to a bundle.
*
* {@hide} - This class is deprecated, and will be going away. Please don't use it.
*/
public class BundlePrinter extends ResultPrinter {
private Bundle mResults;
private boolean mFailure;
private boolean mError;
public BundlePrinter(PrintStream writer, Bundle result) {
super(writer);
mResults = result;
}
@Override
public void addError(Test test, Throwable t) {
mResults.putString(getComboName(test), BaseTestRunner.getFilteredTrace(t));
mFailure = true;
super.addError(test, t);
}
@Override
public void addFailure(Test test, AssertionFailedError t) {
mResults.putString(getComboName(test), BaseTestRunner.getFilteredTrace(t));
mError = true;
super.addFailure(test, t);
}
@Override
public void endTest(Test test) {
if (!mFailure && !mError) {
mResults.putString(getComboName(test), "passed");
}
super.endTest(test);
}
@Override
public void startTest(Test test) {
mFailure = false;
mError = false;
super.startTest(test);
}
private String getComboName(Test test) {
return test.getClass().getName() + ":" + ((TestCase) test).getName();
}
}

View File

@@ -1,63 +0,0 @@
/*
* Copyright (C) 2008 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.test;
import junit.framework.*;
import junit.framework.TestCase;
import junit.runner.BaseTestRunner;
import android.os.Bundle;
/**
* A {@link TestListener} that adds test case results to a bundle.
*
* {@hide} - This class is deprecated, and will be going away. Please don't use it.
*/
public class BundleTestListener implements TestListener {
private Bundle mBundle;
private boolean mFailed;
public BundleTestListener(Bundle bundle) {
mBundle = bundle;
}
public void addError(Test test, Throwable t) {
mBundle.putString(getComboName(test), BaseTestRunner.getFilteredTrace(t));
mFailed = true;
}
public void addFailure(Test test, junit.framework.AssertionFailedError t) {
mBundle.putString(getComboName(test), BaseTestRunner.getFilteredTrace(t));
mFailed = true;
}
public void endTest(Test test) {
if (!mFailed) {
mBundle.putString(getComboName(test), "passed");
}
}
public void startTest(Test test) {
mFailed = false;
}
private String getComboName(Test test) {
return test.getClass().getName() + ":" + ((TestCase) test).getName();
}
}

View File

@@ -1,35 +0,0 @@
/*
* Copyright (C) 2007 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.test;
/**
* @hide - This is part of a framework that is under development and should not be used for
* active development.
*/
public class ServiceLocator {
private static TestBrowserController mTestBrowserController =
new TestBrowserControllerImpl();
public static TestBrowserController getTestBrowserController() {
return mTestBrowserController;
}
static void setTestBrowserController(TestBrowserController testBrowserController) {
mTestBrowserController = testBrowserController;
}
}

View File

@@ -1,127 +0,0 @@
/*
* Copyright (C) 2007 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.test;
import com.android.internal.R;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import junit.framework.Test;
import junit.framework.TestSuite;
import java.util.List;
/**
* @hide - This is part of a framework that is under development and should not be used for
* active development.
*/
public abstract class TestBrowserActivity extends ListActivity
implements android.test.TestBrowserView, AdapterView.OnItemClickListener,
TestSuiteProvider {
private TestBrowserController mTestBrowserController;
public static final String BUNDLE_EXTRA_PACKAGE = "package";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getListView().setOnItemClickListener(this);
mTestBrowserController = ServiceLocator.getTestBrowserController();
mTestBrowserController.setTargetPackageName(getPackageName());
mTestBrowserController.registerView(this);
mTestBrowserController.setTargetBrowserActivityClassName(this.getClass().getName());
// Apk paths used to search for test classes when using TestSuiteBuilders.
String[] apkPaths = {getPackageCodePath()};
ClassPathPackageInfoSource.setApkPaths(apkPaths);
}
@Override
protected void onStart() {
super.onStart();
TestSuite testSuite = getTestSuiteToBrowse();
mTestBrowserController.setTestSuite(testSuite);
String name = testSuite.getName();
if (name != null) {
setTitle(name.substring(name.lastIndexOf(".") + 1));
}
}
/**
* Subclasses will override this method and return the TestSuite specific to their .apk.
* When this method is invoked due to an intent fired from
* {@link #onItemClick(android.widget.AdapterView, android.view.View, int, long)} then get the
* targeted TestSuite from the intent.
*
* @return testSuite to browse
*/
@SuppressWarnings("unchecked")
private TestSuite getTestSuiteToBrowse() {
Intent intent = getIntent();
if (Intent.ACTION_RUN.equals(intent.getAction())) {
String testClassName = intent.getData().toString();
try {
Class<Test> testClass = (Class<Test>) getClassLoader().loadClass(testClassName);
return TestCaseUtil.createTestSuite(testClass);
} catch (ClassNotFoundException e) {
Log.e("TestBrowserActivity", "ClassNotFoundException for " + testClassName, e);
throw new RuntimeException(e);
} catch (IllegalAccessException e) {
Log.e("TestBrowserActivity", "IllegalAccessException for " + testClassName, e);
throw new RuntimeException(e);
} catch (InstantiationException e) {
Log.e("TestBrowserActivity", "InstantiationException for " + testClassName, e);
throw new RuntimeException(e);
}
} else {
// get test classes to browwes from subclass
return getTopTestSuite();
}
}
public TestSuite getTestSuite() {
return getTopTestSuite();
}
/**
* @return A TestSuite that should be run for a given application.
*/
public abstract TestSuite getTopTestSuite();
public void onItemClick(AdapterView parent, View v, int position, long id) {
Intent intent = mTestBrowserController.getIntentForTestAt(position);
intent.putExtra(BUNDLE_EXTRA_PACKAGE, getPackageName());
startActivity(intent);
}
public void setTestNames(List<String> testNames) {
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,
R.layout.test_list_item, testNames);
setListAdapter(arrayAdapter);
}
}

View File

@@ -1,38 +0,0 @@
/*
* Copyright (C) 2007 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.test;
import android.content.Intent;
import junit.framework.TestSuite;
/**
* @hide - This is part of a framework that is under development and should not be used for
* active development.
*/
public interface TestBrowserController {
String BUNDLE_EXTRA_TEST_METHOD_NAME = "testMethodName";
Intent getIntentForTestAt(int position);
void setTestSuite(TestSuite testSuite);
void registerView(TestBrowserView testBrowserView);
void setTargetBrowserActivityClassName(String targetBrowserActivityClassName);
void setTargetPackageName(String targetPackageName);
}

View File

@@ -1,110 +0,0 @@
/*
* Copyright (C) 2007 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.test;
import android.content.Intent;
import android.net.Uri;
import com.google.android.collect.Lists;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import java.util.List;
/**
* @hide - This is part of a framework that is under development and should not be used for
* active development.
*/
public class TestBrowserControllerImpl implements TestBrowserController {
static final String TEST_RUNNER_ACTIVITY_CLASS_NAME =
"com.android.testharness.TestRunnerActivity";
private TestSuite mTestSuite;
private TestBrowserView mTestBrowserView;
private static final int RUN_ALL_INDEX = 0;
private String mTargetBrowserActivityClassName;
private String mTargetPackageName;
public void setTargetPackageName(String targetPackageName) {
mTargetPackageName = targetPackageName;
}
public Intent getIntentForTestAt(int position) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_RUN);
// We must add the following two flags to make sure that we create a new activity when
// we browse nested test suites.
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
String packageName = getDefaultPackageNameForTestRunner();
String className = "";
String testName = "";
if (shouldAllTestsBeRun(position)) {
testName = mTestSuite.getName();
className = TEST_RUNNER_ACTIVITY_CLASS_NAME;
} else {
Test test = TestCaseUtil.getTestAtIndex(mTestSuite, position - 1);
if (TestSuite.class.isAssignableFrom(test.getClass())) {
TestSuite testSuite = (TestSuite) test;
testName = testSuite.getName();
className = mTargetBrowserActivityClassName;
packageName = mTargetPackageName;
} else if (TestCase.class.isAssignableFrom(test.getClass())) {
TestCase testCase = (TestCase) test;
testName = testCase.getClass().getName();
className = TEST_RUNNER_ACTIVITY_CLASS_NAME;
String testMethodName = testCase.getName();
intent.putExtra(BUNDLE_EXTRA_TEST_METHOD_NAME, testMethodName);
}
}
intent.setClassName(packageName, className);
intent.setData(Uri.parse(testName));
return intent;
}
private String getDefaultPackageNameForTestRunner() {
return TEST_RUNNER_ACTIVITY_CLASS_NAME.substring(0,
TEST_RUNNER_ACTIVITY_CLASS_NAME.lastIndexOf("."));
}
private boolean shouldAllTestsBeRun(int position) {
return position == RUN_ALL_INDEX;
}
public void setTestSuite(TestSuite testSuite) {
mTestSuite = testSuite;
List<String> testCaseNames = Lists.newArrayList();
testCaseNames.add("Run All");
testCaseNames.addAll(TestCaseUtil.getTestCaseNames(testSuite, false));
mTestBrowserView.setTestNames(testCaseNames);
}
public void registerView(TestBrowserView testBrowserView) {
mTestBrowserView = testBrowserView;
}
public void setTargetBrowserActivityClassName(String targetBrowserActivityClassName) {
mTargetBrowserActivityClassName = targetBrowserActivityClassName;
}
}

View File

@@ -1,27 +0,0 @@
/*
* Copyright (C) 2007 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.test;
import java.util.List;
/**
* @hide - This is part of a framework that is under development and should not be used for
* active development.
*/
public interface TestBrowserView {
void setTestNames(List<String> testNames);
}

View File

@@ -1,253 +0,0 @@
/*
* Copyright (C) 2007 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.test;
import android.app.ListActivity;
import android.content.Intent;
import android.database.MatrixCursor;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.CursorAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import java.util.Arrays;
import java.util.Comparator;
/**
* Activity base class to use to implement your application's tests.
*
* <p>Implement the getTestSuite() method to return the name of your
* test suite class.
*
* <p>See the android.test package documentation (click the more... link)
* for a full description
*
* {@hide} Not needed for SDK
*/
public abstract class TestListActivity extends ListActivity {
/** Supplied in the intent extras if we are running performance tests. */
public static final String PERFORMANCE_TESTS = "android.test.performance";
/** "Mode" group in the menu. */
static final int MODE_GROUP = Menu.FIRST;
/** Our suite */
String mSuite;
/** Our children tests */
String[] mTests;
/** which mode, REGRESSION, PERFORMANCE or PROFILING */
private int mMode = TestRunner.REGRESSION;
/** "Regression" menu item */
private MenuItem mRegressionItem;
/** "Performance" menu item */
private MenuItem mPerformanceItem;
/** "Profiling" menu item */
private MenuItem mProfilingItem;
private final Comparator<String> sComparator = new Comparator<String>() {
public final int compare(String a, String b) {
String s1 = makeCompareName(a);
String s2 = makeCompareName(b);
return s1.compareToIgnoreCase(s2);
}
};
/**
* Constructor that doesn't do much.
*/
public TestListActivity() {
super();
}
/**
* Subclasses should implement this to return the names of the classes
* of their tests.
*
* @return test suite class name
*/
public abstract String getTestSuite();
/**
* Typical onCreate(Bundle icicle) implementation.
*/
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
Intent intent = getIntent();
mMode = intent.getIntExtra(TestListActivity.PERFORMANCE_TESTS, mMode);
if (intent.getAction().equals(Intent.ACTION_MAIN)) {
// if we were called as MAIN, get the test suites,
mSuite = getTestSuite();
} else if (intent.getAction().equals(Intent.ACTION_RUN)) {
// We should have been provided a status channel. Bail out and
// run the test instead. This is how the TestHarness gets us
// loaded in our process for "Run All Tests."
Intent ntent = new Intent(Intent.ACTION_RUN,
intent.getData() != null
? intent.getData()
: Uri.parse(getTestSuite()));
ntent.setClassName("com.android.testharness",
"com.android.testharness.RunTest");
ntent.putExtras(intent);
ntent.putExtra("package", getPackageName());
startActivity(ntent);
finish();
return;
} else if (intent.getAction().equals(Intent.ACTION_VIEW)) {
// otherwise use the one in the intent
mSuite = intent.getData() != null ? intent.getData().toString()
: null;
}
String[] children = TestRunner.getChildren(this, mSuite);
Arrays.sort(children, sComparator);
int len = children.length;
mTests = new String[len];
System.arraycopy(children, 0, mTests, 0, len);
setTitle(TestRunner.getTitle(mSuite));
MatrixCursor cursor = new MatrixCursor(new String[] { "name", "_id" });
addTestRows(cursor);
CursorAdapter adapter = new SimpleCursorAdapter(
this,
com.android.internal.R.layout.simple_list_item_1,
cursor,
new String[] {"name"},
new int[] {com.android.internal.R.id.text1});
setListAdapter(adapter);
}
private void addTestRows(MatrixCursor cursor) {
int id = 0;
cursor.newRow().add("Run All").add(id++);
for (String test : mTests) {
String title = TestRunner.getTitle(test);
String prefix = TestRunner.isTestSuite(this, test)
? "Browse " : "Run ";
// I'd rather do this with an icon column, but I don't know how
cursor.newRow().add(prefix + title).add(id++);
}
}
@Override
protected void onResume() {
super.onResume();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
mRegressionItem = menu.add(MODE_GROUP, -1, 0, "Regression Mode");
mPerformanceItem = menu.add(MODE_GROUP, -1, 0, "Performance Mode");
mProfilingItem = menu.add(MODE_GROUP, -1, 0, "Profiling Mode");
menu.setGroupCheckable(MODE_GROUP, true, true);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item == mRegressionItem) {
mMode = TestRunner.REGRESSION;
} else if (item == mPerformanceItem) {
mMode = TestRunner.PERFORMANCE;
} else if (item == mProfilingItem) {
mMode = TestRunner.PROFILING;
}
return true;
}
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
switch (mMode) {
case TestRunner.REGRESSION:
mRegressionItem.setChecked(true);
break;
case TestRunner.PERFORMANCE:
mPerformanceItem.setChecked(true);
break;
case TestRunner.PROFILING:
mProfilingItem.setChecked(true);
break;
}
return true;
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
Intent intent = new Intent();
if (position == 0) {
if (false) {
intent.setClassName("com.android.testharness",
"com.android.testharness.RunAll");
intent.putExtra("tests", new String[]{mSuite});
} else {
intent.setClassName("com.android.testharness",
"com.android.testharness.RunTest");
intent.setAction(Intent.ACTION_RUN);
intent.setData(Uri.parse(mSuite));
}
} else {
String test = mTests[position - 1];
if (TestRunner.isTestSuite(this, test)) {
intent.setClassName(getPackageName(), this.getClass().getName());
intent.setAction(Intent.ACTION_VIEW);
} else {
intent.setClassName("com.android.testharness",
"com.android.testharness.RunTest");
}
intent.setData(Uri.parse(test));
}
intent.putExtra(PERFORMANCE_TESTS, mMode);
intent.putExtra("package", getPackageName());
startActivity(intent);
}
private String makeCompareName(String s) {
int index = s.lastIndexOf('.');
if (index == -1) {
return s;
}
return s.substring(index + 1);
}
}

View File

@@ -1,186 +0,0 @@
/*
* Copyright (C) 2006 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.test;
import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;
import android.os.Environment;
import android.os.FileUtils;
import android.test.TestRunner.IntermediateTime;
import android.util.Log;
import junit.framework.Test;
import junit.framework.TestListener;
import java.io.File;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
/**
* {@hide} Not needed for 1.0 SDK.
*/
public class TestRecorder implements TestRunner.Listener, TestListener {
private static final int DATABASE_VERSION = 1;
private static SQLiteDatabase sDb;
private Set<String> mFailedTests = new HashSet<String>();
private static SQLiteDatabase getDatabase() {
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 TestRecorder() {
}
public void started(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);
}
}
public void finished(String className) {
ContentValues map = new ContentValues(1);
map.put("finished", System.currentTimeMillis());
getDatabase().update("tests", map, "name = '" + className + "'", null);
}
public void performance(String className, long itemTimeNS, int iterations, List<IntermediateTime> 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 void passed(String className) {
ContentValues map = new ContentValues();
map.put("result", "passed");
getDatabase().update("tests", map, "name = '" + className + "'", null);
}
public void failed(String className, 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);
}
/**
* Reports a test case failure.
*
* @param className Name of the class/test.
* @param reason Reason for failure.
*/
public void failed(String className, String reason) {
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 void addError(Test test, Throwable t) {
mFailedTests.add(test.toString());
failed(test.toString(), t);
}
public void addFailure(Test test, junit.framework.AssertionFailedError t) {
mFailedTests.add(test.toString());
failed(test.toString(), t.getMessage());
}
public void endTest(Test test) {
finished(test.toString());
if (!mFailedTests.contains(test.toString())) {
passed(test.toString());
}
mFailedTests.remove(test.toString());
}
public void startTest(Test test) {
started(test.toString());
}
}

View File

@@ -1,41 +0,0 @@
/*
* Copyright (C) 2007 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.test;
import java.util.List;
/**
* @hide - This is part of a framework that is under development and should not be used for
* active development.
*/
public interface TestRunnerView {
void setTestNames(List<String> testNames);
void setItemColorAt(int position, int color);
void setFailureCount(int failureCount);
void setRunCount(int runCount);
void setErrorCount(int errorCount);
void setTotalCount(int totalCount);
void setProgressBarColor(int color);
void testsCompleted();
}

Binary file not shown.

Before

Width:  |  Height:  |  Size: 964 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 883 B

View File

@@ -1,33 +0,0 @@
/*
* Copyright (C) 2007 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.test;
import junit.framework.TestSuite;
public class StubTestBrowserActivity extends TestBrowserActivity {
private static TestSuite mTestSuite;
static void setTopTestSuite(TestSuite testSuite) {
mTestSuite = testSuite;
}
@Override
public TestSuite getTopTestSuite() {
return mTestSuite;
}
}

View File

@@ -1,275 +0,0 @@
/*
* Copyright (C) 2007 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.test;
import android.app.Activity;
import android.app.Instrumentation;
import android.content.Intent;
import android.net.Uri;
import android.os.RemoteException;
import android.os.ServiceManager;
import android.view.IWindowManager;
import android.widget.ListView;
import com.google.android.collect.Lists;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import java.util.List;
public class TestBrowserActivityTest extends InstrumentationTestCase {
private TestBrowserActivity mTestBrowserActivity;
private StubTestBrowserController mTestBrowserController;
@Override
protected void setUp() throws Exception {
super.setUp();
StubTestBrowserActivity.setTopTestSuite(null);
mTestBrowserController = new StubTestBrowserController();
ServiceLocator.setTestBrowserController(mTestBrowserController);
}
@Override
protected void tearDown() throws Exception {
if (mTestBrowserActivity != null) {
mTestBrowserActivity.finish();
}
mTestBrowserActivity = null;
super.tearDown();
}
public void testEmptyListContent() throws Exception {
StubTestBrowserActivity.setTopTestSuite(new TestSuite());
mTestBrowserActivity = createActivity();
ListView listView = getListView();
// There is always an item on the list for running all tests.
assertEquals("Unexpected number of items on list view.", 1, listView.getCount());
assertEquals("Stubbed Test Browser", mTestBrowserActivity.getTitle().toString());
}
public void testOneListContent() throws Exception {
List<String> testCaseNames = Lists.newArrayList("AllTests");
StubTestBrowserActivity.setTopTestSuite(createTestSuite(testCaseNames));
mTestBrowserActivity = createActivity();
ListView listView = getListView();
assertListViewContents(testCaseNames, listView);
}
public void testListWithTestCases() throws Exception {
List<String> testCaseNames = Lists.newArrayList("AllTests", "Apples", "Bananas", "Oranges");
StubTestBrowserActivity.setTopTestSuite(createTestSuite(testCaseNames));
mTestBrowserActivity = createActivity();
ListView listView = getListView();
assertListViewContents(testCaseNames, listView);
}
public void testListWithTestSuite() throws Exception {
List<String> testCaseNames = Lists.newArrayList(OneTestTestCase.class.getSimpleName());
StubTestBrowserActivity.setTopTestSuite(new OneTestInTestSuite());
mTestBrowserActivity = createActivity();
ListView listView = getListView();
assertListViewContents(testCaseNames, listView);
}
public void testSelectATestCase() throws Exception {
List<String> testCaseNames = Lists.newArrayList("AllTests");
TestSuite testSuite = createTestSuite(testCaseNames);
StubTestBrowserActivity.setTopTestSuite(testSuite);
mTestBrowserController.setTestCase(OneTestTestCase.class);
mTestBrowserActivity = createActivity();
Instrumentation.ActivityMonitor activityMonitor = getInstrumentation().addMonitor(
TestBrowserControllerImpl.TEST_RUNNER_ACTIVITY_CLASS_NAME, null, false);
try {
assertEquals(0, activityMonitor.getHits());
ListView listView = getListView();
int invokedTestCaseIndex = 0;
listView.performItemClick(listView, invokedTestCaseIndex, 0);
Activity activity = activityMonitor.waitForActivityWithTimeout(2000);
assertNotNull(activity);
try {
assertEquals(1, activityMonitor.getHits());
assertEquals(invokedTestCaseIndex, mTestBrowserController.getLastPosition());
} finally {
activity.finish();
}
} finally {
getInstrumentation().removeMonitor(activityMonitor);
}
}
public void testCreateFromIntentWithOneTest() throws Exception {
List<String> testCaseNames = Lists.newArrayList("testOne");
mTestBrowserActivity = launchTestBrowserActivity(new TestSuite(OneTestTestCase.class));
ListView listView = getListView();
assertListViewContents(testCaseNames, listView);
}
public void testUpdateListOnStart() throws Exception {
StubTestBrowserActivity.setTopTestSuite(new TestSuite());
mTestBrowserActivity = createActivity();
ListView listView = getListView();
assertEquals("Unexpected number of items on list view.", 1, listView.getCount());
List<String> testCaseNames = Lists.newArrayList("AllTests");
StubTestBrowserActivity.setTopTestSuite(createTestSuite(testCaseNames));
getInstrumentation().runOnMainSync(new Runnable() {
public void run() {
((StubTestBrowserActivity) mTestBrowserActivity).onStart();
}
});
listView = getListView();
assertListViewContents(testCaseNames, listView);
}
public void testTitleHasTestSuiteName() throws Exception {
final String testSuiteName = "com.android.TestSuite";
StubTestBrowserActivity.setTopTestSuite(new TestSuite(testSuiteName));
mTestBrowserActivity = createActivity();
assertEquals("TestSuite", mTestBrowserActivity.getTitle().toString());
}
private TestSuite createTestSuite(List<String> testCaseNames) {
return createTestSuite(testCaseNames.toArray(new String[testCaseNames.size()]));
}
private TestSuite createTestSuite(String... testCaseNames) {
TestSuite testSuite = new TestSuite();
for (String testCaseName : testCaseNames) {
testSuite.addTest(new FakeTestCase(testCaseName));
}
return testSuite;
}
public static class FakeTestCase extends TestCase {
public FakeTestCase(String name) {
super(name);
}
}
public static class OneTestTestCase extends TestCase {
public void testOne() throws Exception {
}
}
public static class OneTestInTestSuite extends TestSuite {
public static Test suite() {
TestSuite suite = new TestSuite(OneTestInTestSuite.class.getName());
suite.addTestSuite(OneTestTestCase.class);
return suite;
}
}
private void assertListViewContents(List<String> expectedTestCaseNames, ListView listView) {
assertEquals("Run All", listView.getItemAtPosition(0).toString());
assertEquals("Unexpected number of items on list view.",
expectedTestCaseNames.size() + 1, listView.getCount());
for (int i = 0; i < expectedTestCaseNames.size(); i++) {
String expectedTestCaseName = expectedTestCaseNames.get(i);
String actualTestCaseName = listView.getItemAtPosition(i + 1).toString();
assertEquals("Unexpected test case name. Index: " + i,
expectedTestCaseName, actualTestCaseName);
}
}
private ListView getListView() {
return mTestBrowserActivity.getListView();
}
private TestBrowserActivity createActivity() throws RemoteException {
return launchActivity(getAndroidPackageName(), StubTestBrowserActivity.class, null);
}
private Intent createIntent(TestSuite testSuite) {
Intent intent = new Intent(Intent.ACTION_RUN);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
String className = StubTestBrowserActivity.class.getName();
String packageName = getAndroidPackageName();
intent.setClassName(packageName, className);
intent.setData(Uri.parse(testSuite.getName()));
return intent;
}
private String getAndroidPackageName() {
String packageName = getInstrumentation().getTargetContext().getPackageName();
return packageName;
}
private TestBrowserActivity launchTestBrowserActivity(TestSuite testSuite)
throws RemoteException {
getInstrumentation().setInTouchMode(false);
TestBrowserActivity activity =
(TestBrowserActivity) getInstrumentation().startActivitySync(
createIntent(testSuite));
getInstrumentation().waitForIdleSync();
return activity;
}
private static class StubTestBrowserController extends TestBrowserControllerImpl {
private int mPosition;
private Class<? extends TestCase> mTestCaseClass;
public Intent getIntentForTestAt(int position) {
mPosition = position;
Intent intent = new Intent();
intent.setAction(Intent.ACTION_RUN);
String className = TestBrowserControllerImpl.TEST_RUNNER_ACTIVITY_CLASS_NAME;
String testName = mTestCaseClass.getClass().getName();
String packageName = className.substring(0, className.lastIndexOf("."));
intent.setClassName(packageName, className);
intent.setData(Uri.parse(testName));
return intent;
}
public void setTestCase(Class<? extends TestCase> testCaseClass) {
mTestCaseClass = testCaseClass;
}
public int getLastPosition() {
return mPosition;
}
}
}

View File

@@ -1,134 +0,0 @@
/*
* Copyright (C) 2007 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.test;
import android.content.Intent;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import java.util.Arrays;
import java.util.List;
public class TestBrowserControllerImplTest extends TestCase {
private TestBrowserControllerImpl mTestBrowserController;
private TestBrowserViewStub mTestBrowserView;
@Override
protected void setUp() throws Exception {
super.setUp();
mTestBrowserController = new TestBrowserControllerImpl();
mTestBrowserView = new TestBrowserViewStub();
mTestBrowserController.registerView(mTestBrowserView);
}
public void testSetTestSuite() throws Exception {
TestSuite testSuite = new TestSuite();
testSuite.addTestSuite(DummyTestCase.class);
mTestBrowserController.setTestSuite(testSuite);
verifyTestNames(Arrays.asList("Run All", DummyTestCase.class.getSimpleName()),
mTestBrowserView.getTestNames());
}
private static void verifyTestNames(List<String> expectedTestNames,
List<String> actualTestNames) {
assertEquals(expectedTestNames.size(), actualTestNames.size());
// We use endsWith instead of equals because the return value of
// class.getSimpleName(), when called on an inner class, varies
// from one vm to another.
// This allows the test to pass in multiple environments.
for (int i = 0; i < expectedTestNames.size(); i++) {
assertTrue(actualTestNames.get(i).endsWith(expectedTestNames.get(i)));
}
}
public void testGetIntentForTestSuite() throws Exception {
TestSuite testSuite = new TestSuite();
testSuite.addTestSuite(DummyTestCase.class);
String targetBrowserActvityClassName = "com.android.bogus.DummyActivity";
String expectedTargetPackageName = "com.android.bogus";
mTestBrowserController.setTargetBrowserActivityClassName(targetBrowserActvityClassName);
mTestBrowserController.setTestSuite(testSuite);
mTestBrowserController.setTargetPackageName(expectedTargetPackageName);
Intent intent = mTestBrowserController.getIntentForTestAt(1);
verifyIntent(intent, DummyTestCase.class, expectedTargetPackageName);
assertEquals(targetBrowserActvityClassName, intent.getComponent().getClassName());
}
public void testGetIntentForTestCase() throws Exception {
TestSuite testSuite = new TestSuite();
testSuite.addTest(new DummyTestCase());
mTestBrowserController.setTestSuite(testSuite);
Intent intent = mTestBrowserController.getIntentForTestAt(1);
verifyIntent(intent, DummyTestCase.class, "com.android.testharness");
assertEquals(TestBrowserControllerImpl.TEST_RUNNER_ACTIVITY_CLASS_NAME,
intent.getComponent().getClassName());
assertEquals("testDummyTest",
intent.getStringExtra(TestBrowserController.BUNDLE_EXTRA_TEST_METHOD_NAME));
}
public void testGetIntentForRunAll() throws Exception {
TestSuite testSuite = new DummyTestSuite();
testSuite.addTestSuite(DummyTestCase.class);
mTestBrowserController.setTestSuite(testSuite);
Intent intent = mTestBrowserController.getIntentForTestAt(0);
verifyIntent(intent, DummyTestSuite.class, "com.android.testharness");
}
private static void verifyIntent(Intent intent, Class testClass, String expectedPackageName) {
assertEquals(Intent.ACTION_RUN, intent.getAction());
assertEquals(Intent.FLAG_ACTIVITY_NEW_TASK,
intent.getFlags() & Intent.FLAG_ACTIVITY_NEW_TASK);
assertEquals(Intent.FLAG_ACTIVITY_MULTIPLE_TASK,
intent.getFlags() & Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
assertEquals(testClass.getName(), intent.getData().toString());
assertEquals(expectedPackageName, intent.getComponent().getPackageName());
}
private static class DummyTestSuite extends TestSuite {
private DummyTestSuite() {
super(DummyTestSuite.class.getName());
}
}
private static class DummyTestCase extends TestCase {
private DummyTestCase() {
super("testDummyTest");
}
public void testDummyTest() throws Exception {
}
}
private class TestBrowserViewStub implements TestBrowserView {
private List<String> mTestNames;
public void setTestNames(List<String> testNames) {
mTestNames = testNames;
}
public List<String> getTestNames() {
return mTestNames;
}
}
}

View File

@@ -1,22 +0,0 @@
// Copyright 2007 The Android Open Source Project
package android.test;
import junit.framework.TestSuite;
public class TestBrowserTests extends TestBrowserActivity {
@Override
public TestSuite getTopTestSuite() {
return suite();
}
public static TestSuite suite() {
TestSuite testSuite = new TestSuite(TestBrowserTests.class.getName());
testSuite.addTestSuite(TestBrowserControllerImplTest.class);
testSuite.addTestSuite(TestCaseUtilTest.class);
return testSuite;
}
}