AndroidJUnitLaunchConfigDelegatepublic class AndroidJUnitLaunchConfigDelegate extends com.android.ide.eclipse.adt.launch.LaunchConfigDelegate Run configuration that can execute JUnit tests on an Android platform.
Will deploy apps on target Android platform by reusing functionality from ADT
LaunchConfigDelegate, and then run JUnits tests by reusing functionality from JDT
JUnitLaunchConfigDelegate. |
Fields Summary |
---|
static final String | ATTR_INSTR_NAMELaunch config attribute that stores instrumentation runner. | private static final String | EMPTY_STRING |
Methods Summary |
---|
protected void | doLaunch(org.eclipse.debug.core.ILaunchConfiguration configuration, java.lang.String mode, org.eclipse.core.runtime.IProgressMonitor monitor, org.eclipse.core.resources.IProject project, com.android.ide.eclipse.adt.launch.AndroidLaunch androidLaunch, com.android.ide.eclipse.adt.launch.AndroidLaunchConfiguration config, com.android.ide.eclipse.adt.launch.AndroidLaunchController controller, org.eclipse.core.resources.IFile applicationPackage, com.android.ide.eclipse.common.project.AndroidManifestParser manifestParser) //$NON-NLS-1$
String runner = getRunner(project, configuration, manifestParser);
if (runner == null) {
AdtPlugin.displayError("Android Launch",
"An instrumention test runner is not specified!");
androidLaunch.stopLaunch();
return;
}
// get the target app's package
String targetAppPackage = getTargetPackage(manifestParser, runner);
if (targetAppPackage == null) {
AdtPlugin.displayError("Android Launch",
String.format("A target package for instrumention test runner %1$s could not be found!",
runner));
androidLaunch.stopLaunch();
return;
}
String testAppPackage = manifestParser.getPackage();
AndroidJUnitLaunchInfo junitLaunchInfo = new AndroidJUnitLaunchInfo(project,
testAppPackage, runner);
junitLaunchInfo.setTestClass(getTestClass(configuration));
junitLaunchInfo.setTestPackage(getTestPackage(configuration));
junitLaunchInfo.setTestMethod(getTestMethod(configuration));
junitLaunchInfo.setLaunch(androidLaunch);
IAndroidLaunchAction junitLaunch = new AndroidJUnitLaunchAction(junitLaunchInfo);
controller.launch(project, mode, applicationPackage, testAppPackage, targetAppPackage,
manifestParser.getDebuggable(), manifestParser.getApiLevelRequirement(),
junitLaunch, config, androidLaunch, monitor);
| private java.lang.String | getRunner(org.eclipse.core.resources.IProject project, org.eclipse.debug.core.ILaunchConfiguration configuration, com.android.ide.eclipse.common.project.AndroidManifestParser manifestParser)Gets a instrumentation runner for the launch.
If a runner is stored in the given configuration , will return that.
Otherwise, will try to find the first valid runner for the project.
If a runner can still not be found, will return null .
try {
String runner = getRunnerFromConfig(configuration);
if (runner != null) {
return runner;
}
final InstrumentationRunnerValidator instrFinder = new InstrumentationRunnerValidator(
BaseProjectHelper.getJavaProject(project), manifestParser);
runner = instrFinder.getValidInstrumentationTestRunner();
if (runner != null) {
AdtPlugin.printErrorToConsole(project,
String.format("Warning: No instrumentation runner found for the launch, " +
"using %1$s", runner));
return runner;
}
AdtPlugin.printErrorToConsole(project,
String.format("ERROR: Application does not specify a %1$s instrumentation or does not declare uses-library %2$s",
AndroidConstants.CLASS_INSTRUMENTATION_RUNNER,
AndroidConstants.LIBRARY_TEST_RUNNER));
return null;
} catch (CoreException e) {
AdtPlugin.log(e, "Error when retrieving instrumentation info"); //$NON-NLS-1$
}
return null;
| private java.lang.String | getRunnerFromConfig(org.eclipse.debug.core.ILaunchConfiguration configuration)
return getStringLaunchAttribute(ATTR_INSTR_NAME, configuration);
| private java.lang.String | getStringLaunchAttribute(java.lang.String attributeName, org.eclipse.debug.core.ILaunchConfiguration configuration)Helper method to retrieve a string attribute from the launch configuration
try {
String attrValue = configuration.getAttribute(attributeName, EMPTY_STRING);
if (attrValue.length() < 1) {
return null;
}
return attrValue;
} catch (CoreException e) {
AdtPlugin.log(e, String.format("Error when retrieving launch info %1$s", //$NON-NLS-1$
attributeName));
}
return null;
| private java.lang.String | getTargetPackage(com.android.ide.eclipse.common.project.AndroidManifestParser manifestParser, java.lang.String runner)Get the target Android application's package for the given instrumentation runner, or
null if it could not be found.
for (Instrumentation instr : manifestParser.getInstrumentations()) {
if (instr.getName().equals(runner)) {
return instr.getTargetPackage();
}
}
return null;
| private java.lang.String | getTestClass(org.eclipse.debug.core.ILaunchConfiguration configuration)Returns the test class stored in the launch configuration.
return getStringLaunchAttribute(IJavaLaunchConfigurationConstants.ATTR_MAIN_TYPE_NAME,
configuration);
| private java.lang.String | getTestMethod(org.eclipse.debug.core.ILaunchConfiguration configuration)Returns the test method stored in the launch configuration.
return getStringLaunchAttribute(JUnitLaunchConfigurationConstants.ATTR_TEST_METHOD_NAME,
configuration);
| private java.lang.String | getTestPackage(org.eclipse.debug.core.ILaunchConfiguration configuration)Returns the test package stored in the launch configuration, or null if not
specified.
// try to retrieve a package name from the JUnit container attribute
String containerHandle = getStringLaunchAttribute(
JUnitLaunchConfigurationConstants.ATTR_TEST_CONTAINER, configuration);
if (containerHandle != null && containerHandle.length() > 0) {
IJavaElement element = JavaCore.create(containerHandle);
// containerHandle could be a IProject, check if its a java package
if (element.getElementType() == IJavaElement.PACKAGE_FRAGMENT) {
return element.getElementName();
}
}
return null;
| static void | setJUnitDefaults(org.eclipse.debug.core.ILaunchConfigurationWorkingCopy config)Helper method to set JUnit-related attributes expected by JDT JUnit runner
// set the test runner to JUnit3 to placate JDT JUnit runner logic
config.setAttribute(JUnitLaunchConfigurationConstants.ATTR_TEST_RUNNER_KIND,
TestKindRegistry.JUNIT3_TEST_KIND_ID);
|
|