• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<?xml version="1.0" encoding="UTF-8"?>
2<project name="android_test_rules" default="run-tests">
3
4    <import file="ant_rules_r3.xml" />
5
6    <property name="tested.project.absolute.dir" location="${tested.project.dir}" />
7    <property name="instrumentation.dir" value="instrumented" />
8    <property name="instrumentation.absolute.dir" location="${instrumentation.dir}" />
9
10    <property name="test.runner" value="android.test.InstrumentationTestRunner" />
11    <!-- Application package of the tested project extracted from its manifest file -->
12    <xpath input="${tested.project.absolute.dir}/AndroidManifest.xml"
13                expression="/manifest/@package" output="tested.manifest.package" />
14
15    <!-- TODO: make it more configurable in the next CL's - now it is default for auto-generated
16         project -->
17    <property name="emma.dump.file"
18                  value="/data/data/${tested.manifest.package}/files/coverage.ec" />
19
20    <macrodef name="run-tests-helper">
21        <attribute name="emma.enabled" default="false" />
22        <element name="extra-instrument-args" optional="yes" />
23        <sequential>
24            <echo>Running tests ...</echo>
25            <exec executable="${adb}" failonerror="true">
26                <arg line="${adb.device.arg}" />
27                <arg value="shell" />
28                <arg value="am" />
29                   <arg value="instrument" />
30                <arg value="-w" />
31                <arg value="-e" />
32                   <arg value="coverage" />
33                   <arg value="@{emma.enabled}" />
34                <extra-instrument-args />
35                <arg value="${manifest.package}/${test.runner}" />
36            </exec>
37        </sequential>
38    </macrodef>
39
40    <!-- Invoking this target sets the value of extensible.classpath, which is being added to javac
41         classpath in target 'compile' (android_rules.xml) -->
42    <target name="-set-coverage-classpath">
43        <property name="extensible.classpath"
44                      location="${instrumentation.absolute.dir}/classes" />
45    </target>
46
47    <!-- Ensures that tested project is installed on the device before we run the tests.
48         Used for ordinary tests, without coverage measurement -->
49    <target name="-install-tested-project">
50        <property name="do.not.compile.again" value="true" />
51        <subant target="install">
52            <fileset dir="${tested.project.absolute.dir}" includes="build.xml" />
53        </subant>
54    </target>
55
56    <target name="run-tests" depends="-install-tested-project, install"
57                description="Runs tests from the package defined in test.package property">
58        <run-tests-helper />
59    </target>
60
61    <target name="-install-instrumented">
62        <property name="do.not.compile.again" value="true" />
63        <subant target="-install-with-emma">
64            <property name="out.absolute.dir" value="${instrumentation.absolute.dir}" />
65            <fileset dir="${tested.project.absolute.dir}" includes="build.xml" />
66        </subant>
67    </target>
68
69    <target name="coverage" depends="-set-coverage-classpath, -install-instrumented, install"
70                description="Runs the tests against the instrumented code and generates
71                            code coverage report">
72        <run-tests-helper emma.enabled="true">
73            <extra-instrument-args>
74                <arg value="-e" />
75                   <arg value="coverageFile" />
76                   <arg value="${emma.dump.file}" />
77            </extra-instrument-args>
78        </run-tests-helper>
79        <echo>Downloading coverage file into project directory...</echo>
80        <exec executable="${adb}" failonerror="true">
81            <arg line="${adb.device.arg}" />
82            <arg value="pull" />
83            <arg value="${emma.dump.file}" />
84            <arg value="coverage.ec" />
85        </exec>
86        <echo>Extracting coverage report...</echo>
87        <emma>
88            <report sourcepath="${tested.project.absolute.dir}/${source.dir}"
89                              verbosity="${verbosity}">
90                <!-- TODO: report.dir or something like should be introduced if necessary -->
91                <infileset dir=".">
92                    <include name="coverage.ec" />
93                    <include name="coverage.em" />
94                </infileset>
95                <!-- TODO: reports in other, indicated by user formats -->
96                <html outfile="coverage.html" />
97           </report>
98        </emma>
99        <echo>Cleaning up temporary files...</echo>
100        <delete dir="${instrumentation.absolute.dir}" />
101        <delete file="coverage.ec" />
102        <delete file="coverage.em" />
103        <echo>Saving the report file in ${basedir}/coverage/coverage.html</echo>
104    </target>
105
106</project>
107