ENHANCEMENT: activitycreator generates 'tests' project for instrumentation.
activitycreator script is enhanced to generate 'tests' project along with the main project. The current behavior of generating Activity is extended by generating ActivityTest in 'tests/src' folder. The 'tests' folder follows the example provided asis in 'ApiDemos'. ApiDemos was used as reference project to mimic the project layout for building tests using Instrumentation.
From 'tests' project, type:
"adb shell am instrument -w [your.package].tests/android.test.InstrumentationTestRunner"
to run all tests using Android InstrumentationTestRunner.
NOTE: 'tests' is a separate AndroidProject by all means. It has its own AndroidManifest.xml, build.xml, src, res etc.,
AMEND:
Fixed style issues, javadoc
Fixed build.template to generate tests/build.xml
Removed build.tests.template since its obsolete now.
This commit is contained in:
@@ -41,6 +41,7 @@ obj/framework.aidl tools/lib/framework.aidl
|
||||
# sdk scripts
|
||||
development/tools/scripts/AndroidManifest.template tools/lib/AndroidManifest.template
|
||||
development/tools/scripts/AndroidManifest.alias.template tools/lib/AndroidManifest.alias.template
|
||||
development/tools/scripts/AndroidManifest.tests.template tools/lib/AndroidManifest.tests.template
|
||||
development/tools/scripts/build.template tools/lib/build.template
|
||||
development/tools/scripts/build.alias.template tools/lib/build.alias.template
|
||||
development/tools/scripts/default.properties.template tools/lib/default.properties.template
|
||||
@@ -48,6 +49,7 @@ development/tools/scripts/iml.template tools/lib/iml.template
|
||||
development/tools/scripts/ipr.template tools/lib/ipr.template
|
||||
development/tools/scripts/iws.template tools/lib/iws.template
|
||||
development/tools/scripts/java_file.template tools/lib/java_file.template
|
||||
development/tools/scripts/java_tests_file.template tools/lib/java_tests_file.template
|
||||
development/tools/scripts/layout.template tools/lib/layout.template
|
||||
development/tools/scripts/strings.template tools/lib/strings.template
|
||||
development/tools/scripts/alias.template tools/lib/alias.template
|
||||
|
||||
@@ -266,16 +266,37 @@ public class ActivityCreator {
|
||||
* Installs a destination file that is based on the template file at source.
|
||||
* For each match of each key in keywords will be replaced with its
|
||||
* corresponding value in the destination file.
|
||||
*
|
||||
*
|
||||
* Invokes ActivityCreator#installTemplate(String, String, java.util.Map, boolean, String) with
|
||||
* the main project output directory (#mOutDir) as the last argument.
|
||||
*
|
||||
* @param source the path to the source template file
|
||||
* @param dest the path to the destination file
|
||||
* @param keywords in the destination file, the keys will be replaced by their values
|
||||
* @param force True to force writing the file even if it already exists
|
||||
* @param force True to force writing the file even if it already exists
|
||||
*
|
||||
* @see ActivityCreator#installTemplate(String, String, java.util.Map, boolean, String)
|
||||
*/
|
||||
private void installTemplate(String source, String dest,
|
||||
Map<String, String> keywords, boolean force) {
|
||||
installTemplate(source, dest, keywords, force, mOutDir);
|
||||
}
|
||||
|
||||
/**
|
||||
* Installs a destination file that is based on the template file at source.
|
||||
* For each match of each key in keywords will be replaced with its
|
||||
* corresponding value in the destination file.
|
||||
*
|
||||
* @param source the path to the source template file
|
||||
* @param dest the path to the destination file
|
||||
* @param keywords in the destination file, the keys will be replaced by their values
|
||||
* @param force True to force writing the file even if it already exists
|
||||
* @param outDir the output directory to copy the template file to
|
||||
*/
|
||||
private void installTemplate(String source, String dest,
|
||||
Map<String, String> keywords, boolean force, String outDir) {
|
||||
final String sourcePath = mLibDir + File.separator + source;
|
||||
final String destPath = mOutDir + File.separator + dest;
|
||||
final String destPath = outDir + File.separator + dest;
|
||||
|
||||
final File destPathFile = new File(destPath);
|
||||
if (!force && destPathFile.exists()) {
|
||||
@@ -283,27 +304,27 @@ public class ActivityCreator {
|
||||
destPathFile.getName());
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
try {
|
||||
BufferedWriter out = new BufferedWriter(new FileWriter(destPathFile));
|
||||
BufferedReader in = new BufferedReader(new FileReader(sourcePath));
|
||||
String line;
|
||||
|
||||
|
||||
while ((line = in.readLine()) != null) {
|
||||
for (String key : keywords.keySet()) {
|
||||
line = line.replace(key, keywords.get(key));
|
||||
}
|
||||
|
||||
|
||||
out.write(line);
|
||||
out.newLine();
|
||||
}
|
||||
|
||||
|
||||
out.close();
|
||||
in.close();
|
||||
} catch (Exception e) {
|
||||
printHelpAndExit("ERROR: Could not access %1$s: %2$s", destPath, e.getMessage());
|
||||
}
|
||||
|
||||
|
||||
println("Added file %1$s", destPath);
|
||||
}
|
||||
|
||||
@@ -329,12 +350,14 @@ public class ActivityCreator {
|
||||
private void setupProject() {
|
||||
String packageName = null;
|
||||
String activityName = null;
|
||||
String activityTestName = null;
|
||||
try {
|
||||
/* Grab package and Activity names */
|
||||
int lastPeriod = mPackageFull.lastIndexOf('.');
|
||||
packageName = mPackageFull.substring(0, lastPeriod);
|
||||
if (lastPeriod < mPackageFull.length() - 1) {
|
||||
activityName = mPackageFull.substring(lastPeriod+1);
|
||||
activityTestName = activityName + "Test";
|
||||
}
|
||||
|
||||
if (packageName.indexOf('.') == -1) {
|
||||
@@ -346,9 +369,15 @@ public class ActivityCreator {
|
||||
|
||||
println("Package: %1$s", packageName);
|
||||
println("Output directory: %1$s", mOutDir);
|
||||
String testsOutDir = mOutDir + File.separator + "tests";
|
||||
println("Tests directory: %1$s", testsOutDir);
|
||||
|
||||
if (activityName != null) {
|
||||
println("Activity name: %1$s", activityName);
|
||||
}
|
||||
if (activityTestName != null) {
|
||||
println("ActivityTest name: %1$s", activityTestName);
|
||||
}
|
||||
|
||||
final HashMap<String, String> keywords = createBaseKeywordMap();
|
||||
keywords.put("PACKAGE", packageName);
|
||||
@@ -370,8 +399,17 @@ public class ActivityCreator {
|
||||
installTemplate("java_file.template", srcDir + File.separator
|
||||
+ activityName + ".java", keywords);
|
||||
}
|
||||
/* Make ActivityTest java file */
|
||||
createDirs(srcDir, testsOutDir);
|
||||
if (isDirEmpty(srcDir, "Java", testsOutDir) && activityTestName != null) {
|
||||
installTemplate("java_tests_file.template", srcDir + File.separator
|
||||
+ activityTestName + ".java", keywords, false, testsOutDir);
|
||||
}
|
||||
createDirs("bin");
|
||||
createDirs("libs");
|
||||
createDirs("bin", testsOutDir);
|
||||
createDirs("libs", testsOutDir);
|
||||
createDirs("res", testsOutDir);
|
||||
|
||||
/* Make res files */
|
||||
final String valuesDir = "res" + File.separator + "values";
|
||||
@@ -393,7 +431,16 @@ public class ActivityCreator {
|
||||
keywords);
|
||||
|
||||
installTemplate("build.template", "build.xml", keywords);
|
||||
installTemplate("default.properties.template", "default.properties", keywords, true /*force*/);
|
||||
installTemplate("default.properties.template", "default.properties",
|
||||
keywords, true /*force*/);
|
||||
|
||||
/* Make AndroidManifest.xml and build.xml files for tests*/
|
||||
installTemplate("AndroidManifest.tests.template", "AndroidManifest.xml",
|
||||
keywords, false, testsOutDir);
|
||||
|
||||
installTemplate("build.template", "build.xml", keywords, false, testsOutDir);
|
||||
installTemplate("default.properties.template", "default.properties",
|
||||
keywords, true /*force*/, testsOutDir);
|
||||
|
||||
if (mIde.equals("intellij")) {
|
||||
/* IntelliJ files */
|
||||
@@ -500,10 +547,27 @@ public class ActivityCreator {
|
||||
/**
|
||||
* Creates the path in the output directory along with any parent paths
|
||||
* that don't exist.
|
||||
*
|
||||
* Invokes ActivityCreator#createDirs(String, String) with
|
||||
* the main project output directory (#mOutDir) as the last argument.
|
||||
*
|
||||
* @param path the directory out/path that is created.
|
||||
*
|
||||
* @see com.android.activitycreator.ActivityCreator#createDirs(String, String)
|
||||
*/
|
||||
public void createDirs(String path) {
|
||||
final File pathFile = new File(mOutDir + File.separator + path);
|
||||
createDirs(path, mOutDir);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates the path in the output directory along with any parent paths
|
||||
* that don't exist.
|
||||
*
|
||||
* @param path the directory out/path that is created.
|
||||
* @param dir the directory in which the path to be created
|
||||
*/
|
||||
public void createDirs(String path, String dir) {
|
||||
final File pathFile = new File(dir + File.separator + path);
|
||||
boolean existedBefore = true;
|
||||
|
||||
if (!pathFile.exists()) {
|
||||
@@ -529,28 +593,45 @@ public class ActivityCreator {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Checks whether the path in the output directory is empty
|
||||
*
|
||||
*
|
||||
* Invokes ActivityCreator#isDirEmpty(String, String, String) with
|
||||
* the main project output directory (#mOutDir) as the last argument.
|
||||
*
|
||||
* @param path the out/path directory that is checked
|
||||
* @param message the logical name for what this path points to (used in
|
||||
* warning message)
|
||||
* @return whether the directory is empty
|
||||
* @see com.android.activitycreator.ActivityCreator#isDirEmpty(String, String, String)
|
||||
*/
|
||||
public boolean isDirEmpty(String path, String message) {
|
||||
File pathFile = new File(mOutDir + File.separator + path);
|
||||
|
||||
return isDirEmpty(path, message, mOutDir);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks whether the path in the output directory is empty
|
||||
*
|
||||
* @param path the out/path directory that is checked
|
||||
* @param message the logical name for what this path points to (used in
|
||||
* warning message)
|
||||
* @param outDir the output director to check
|
||||
* @return whether the directory is empty
|
||||
*/
|
||||
public boolean isDirEmpty(String path, String message, String outDir) {
|
||||
File pathFile = new File(outDir + File.separator + path);
|
||||
|
||||
String[] pathListing = pathFile.list();
|
||||
if ((pathListing != null) && (pathListing.length > 0)) {
|
||||
println("WARNING: There are already some %1$s files present. None will be created!",
|
||||
message);
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Strips the string of beginning and trailing characters (multiple
|
||||
* characters will be stripped, example stripString("..test...", '.')
|
||||
@@ -588,9 +669,10 @@ public class ActivityCreator {
|
||||
* Returns true if the project is an alias project.
|
||||
* <p/>
|
||||
* Alias projects require both the --url and the --label options.
|
||||
* @return boolean true if the project requested is an alias project
|
||||
*/
|
||||
private boolean isAliasProject() {
|
||||
return (isStringEmpty(mAliasData) == false && isStringEmpty(mApplicationLabel) == false);
|
||||
return (!isStringEmpty(mAliasData) && !isStringEmpty(mApplicationLabel));
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
21
tools/scripts/AndroidManifest.tests.template
Normal file
21
tools/scripts/AndroidManifest.tests.template
Normal file
@@ -0,0 +1,21 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<!-- package name must be unique so suffix with "tests" so package loader doesn't ignore us -->
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
package="PACKAGE.tests"
|
||||
android:versionCode="1"
|
||||
android:versionName="1.0">
|
||||
<!-- We add an application tag here just so that we can indicate that
|
||||
this package needs to link against the android.test library,
|
||||
which is needed when building test cases. -->
|
||||
<application>
|
||||
<uses-library android:name="android.test.runner" />
|
||||
</application>
|
||||
<!--
|
||||
This declares that this application uses the instrumentation test runner targeting
|
||||
the package of PACKAGE. To run the tests use the command:
|
||||
"adb shell am instrument -w PACKAGE.tests/android.test.InstrumentationTestRunner"
|
||||
-->
|
||||
<instrumentation android:name="android.test.InstrumentationTestRunner"
|
||||
android:targetPackage="PACKAGE"
|
||||
android:label="Tests for ACTIVITY_NAME"/>
|
||||
</manifest>
|
||||
@@ -20,7 +20,8 @@
|
||||
<!-- The intermediates directory, Eclipse uses "bin"
|
||||
for its own output, so we do the same. -->
|
||||
<property name="outdir" value="bin" />
|
||||
|
||||
<property name="outdir-main" value="../${outdir}" />
|
||||
|
||||
<!-- ************************************************************************************* -->
|
||||
<!-- No user servicable parts below. -->
|
||||
|
||||
@@ -60,6 +61,11 @@
|
||||
else="${basedir}/${outdir-classes}" >
|
||||
<os family="windows"/>
|
||||
</condition>
|
||||
<condition property="outdir-main-classes"
|
||||
value="${outdir-main}/classes">
|
||||
<available file="${outdir-main}/classes"
|
||||
type="dir"/>
|
||||
</condition>
|
||||
|
||||
<!-- Create R.java in the source directory -->
|
||||
<property name="outdir-r" value="src" />
|
||||
@@ -160,6 +166,7 @@
|
||||
bootclasspath="${android-jar}">
|
||||
<classpath>
|
||||
<fileset dir="${external-libs}" includes="*.jar"/>
|
||||
<pathelement path="${outdir-main-classes}"/>
|
||||
</classpath>
|
||||
</javac>
|
||||
</target>
|
||||
|
||||
21
tools/scripts/java_tests_file.template
Normal file
21
tools/scripts/java_tests_file.template
Normal file
@@ -0,0 +1,21 @@
|
||||
package PACKAGE;
|
||||
|
||||
import android.test.ActivityInstrumentationTestCase;
|
||||
|
||||
/**
|
||||
* This is a simple framework for a test of an Application. See
|
||||
* {@link android.test.ApplicationTestCase ApplicationTestCase} for more information on
|
||||
* how to write and extend Application tests.
|
||||
* <p/>
|
||||
* To run this test, you can type:
|
||||
* adb shell am instrument -w \
|
||||
* -e class PACKAGE.ACTIVITY_NAMETest \
|
||||
* PACKAGE.tests/android.test.InstrumentationTestRunner
|
||||
*/
|
||||
public class ACTIVITY_NAMETest extends ActivityInstrumentationTestCase<ACTIVITY_NAME> {
|
||||
|
||||
public ACTIVITY_NAMETest() {
|
||||
super("PACKAGE", ACTIVITY_NAME.class);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user