aboutsummaryrefslogtreecommitdiffstats
path: root/files/ant/ant_test_rules_r2.xml
blob: 00ebfea008528d73b48f0dc88de7e3a49c9d234e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<?xml version="1.0" encoding="UTF-8"?>
<project name="android_test_rules" default="run-tests">

    <import file="ant_rules_r2.xml" />

    <property name="tested.project.absolute.dir" location="${tested.project.dir}" />
    <property name="instrumentation.dir" value="instrumented" />
    <property name="instrumentation.absolute.dir" location="${instrumentation.dir}" />

    <property name="test.runner" value="android.test.InstrumentationTestRunner" />
    <!-- Application package of the tested project extracted from its manifest file -->
    <xpath input="${tested.project.absolute.dir}/AndroidManifest.xml"
                expression="/manifest/@package" output="tested.manifest.package" />

    <!-- TODO: make it more configurable in the next CL's - now it is default for auto-generated
         project -->
    <property name="emma.dump.file"
                  value="/data/data/${tested.manifest.package}/files/coverage.ec" />

    <macrodef name="run-tests-helper">
        <attribute name="emma.enabled" default="false" />
        <element name="extra-instrument-args" optional="yes" />
        <sequential>
            <echo>Running tests ...</echo>
            <exec executable="${adb}" failonerror="true">
                <arg value="shell" />
                <arg value="am" />
                   <arg value="instrument" />
                <arg value="-w" />
                <arg value="-e" />
                   <arg value="coverage" />
                   <arg value="@{emma.enabled}" />
                <extra-instrument-args />
                <arg value="${manifest.package}/${test.runner}" />
            </exec>
        </sequential>
    </macrodef>

    <!-- Invoking this target sets the value of extensible.classpath, which is being added to javac
         classpath in target 'compile' (android_rules.xml) -->
    <target name="-set-coverage-classpath">
        <property name="extensible.classpath"
                      location="${instrumentation.absolute.dir}/classes" />
    </target>

    <!-- Ensures that tested project is installed on the device before we run the tests.
         Used for ordinary tests, without coverage measurement -->
    <target name="-install-tested-project">
        <property name="do.not.compile.again" value="true" />
        <subant target="install">
            <fileset dir="${tested.project.absolute.dir}" includes="build.xml" />
        </subant>
    </target>

    <target name="run-tests" depends="-install-tested-project, install"
                description="Runs tests from the package defined in test.package property">
        <run-tests-helper />
    </target>

    <target name="-install-instrumented">
        <property name="do.not.compile.again" value="true" />
        <subant target="-install-with-emma">
            <property name="out.absolute.dir" value="${instrumentation.absolute.dir}" />
            <fileset dir="${tested.project.absolute.dir}" includes="build.xml" />
        </subant>
    </target>

    <target name="coverage" depends="-set-coverage-classpath, -install-instrumented, install"
                description="Runs the tests against the instrumented code and generates
                            code coverage report">
        <run-tests-helper emma.enabled="true">
            <extra-instrument-args>
                <arg value="-e" />
                   <arg value="coverageFile" />
                   <arg value="${emma.dump.file}" />
            </extra-instrument-args>
        </run-tests-helper>
        <echo>Downloading coverage file into project directory...</echo>
        <exec executable="${adb}" failonerror="true">
            <arg value="pull" />
            <arg value="${emma.dump.file}" />
            <arg value="coverage.ec" />
        </exec>
        <echo>Extracting coverage report...</echo>
        <emma>
            <report sourcepath="${tested.project.absolute.dir}/${source.dir}"
                              verbosity="${verbosity}">
                <!-- TODO: report.dir or something like should be introduced if necessary -->
                <infileset dir=".">
                    <include name="coverage.ec" />
                    <include name="coverage.em" />
                </infileset>
                <!-- TODO: reports in other, indicated by user formats -->
                <html outfile="coverage.html" />
           </report>
        </emma>
        <echo>Cleaning up temporary files...</echo>
        <delete dir="${instrumentation.absolute.dir}" />
        <delete file="coverage.ec" />
        <delete file="coverage.em" />
        <echo>Saving the report file in ${basedir}/coverage/coverage.html</echo>
    </target>

</project>