<?xml version="1.0" encoding="utf-8"?>
<!--
********************************** DO NOT EDIT **********************************

This file will be replaced during upgrades so DO NOT EDIT this file. If you need to
adjust the process, reading and understanding this file is the first step.

In most cases, the adjustments can be achieved by setting properties or providing one
of the "hooks" in the form of a "-before-" or "-after-" target. Whenever possible, look
for one of these solutions.

Failing that, you can copy whole targets to your build.xml file and it will overrride
the target provided here. Doing that can create problems for upgrading to newer
versions of Cmd so it is not recommended but it will be easier to manage than editing
this file in most cases.
-->
<project name="cmd-test">
    <target name="-before-test-init"/>
    <target name="-test-init">
        <condition property="environment.dir" value="${app.dir}">
            <isset property="${app.dir}"/>
        </condition>

        <property name="environment.dir" value="${package.dir}"/>
        <property name="cmd-test.specs.dir" value="${environment.dir}/test/specs"/>
        <property name="cmd-test.specs.test-json" value="${cmd-test.specs.dir}/test.json"/>
    </target>
    <target name="-after-test-init"/>
    <target name="test-init"
            depends="init,-before-test-init,-test-init,-after-test-init"/>

    <target name="-before-test-console"/>
    <target name="-test-console" depends="test-bootstrap">
        <echo>Launching Sencha Test Console</echo>
        <echo>using cmd.dir : ${cmd.dir}</echo>
        <x-sencha-command
                dir="cmd-test.specs.dir">
            <property name="cmd-test.run.launch-browser" 
                      value="${cmd-test.run.launch-browser}"/>
            test
                console
                    -configFile
                    ${cmd-test.specs.test-json}
        </x-sencha-command>
    </target>
    <target name="-after-test-console"/>
    <target name="test-console"
            depends="test-init,-before-test-console,-test-console,-after-test-console"
            description="Run tests"/>

    <target name="-before-test-run"/>
    <target name="-test-run" depends="test-bootstrap">
        <echo>Launching Sencha Test Runner</echo>
        <echo>using cmd.dir : ${cmd.dir}</echo>
        <x-sencha-command
                dir="cmd-test.specs.dir">
            test
                run
                    -configFile
                    ${cmd-test.specs.test-json}
        </x-sencha-command>
    </target>
    <target name="-after-test-run"/>
    <target name="test-run"
            depends="test-init,-before-test-run,-test-run,-after-test-run"
            description="Run tests"/>


    <target name="-before-test-bootstrap"/>
    <target name="-test-bootstrap">
        <local name="local.runner.dir"/>
        <local name="console.runner.dir"/>
        <local name="specfiles.list"/>
        <local name="base.path"/>

        <property name="local.runner.dir" value="${environment.dir}/test/local"/>
        <property name="console.runner.dir" value="${environment.dir}/test/specs"/>

        <property name="base.path" value="${local.runner.dir}"/>
        <property name="bootstrap.specs.file" value="../bootstrap-specs.js"/>

        <x-bootstrap file="${base.path}/../bootstrap.js"
                     basedir="${base.path}"
                     coreFilesFile="${base.path}/../bootstrap-files.js"
                     classMetadataFile="${base.path}/../bootstrap-data.js"
                     overridesFile="${base.path}/../bootstrap-data.js"
                     includeBoot="true"
                     includeManifest="true"
                     includeCoreFiles="true"
                     appendCoreFiles="false"
                     includeMetadata="false"
                     appendClassMetadata="false"
                     appendOverrides="true"
                     overrideExcludeTags="">
<![CDATA[
Ext.Boot.loadSync([
    "../bootstrap-files.js",
    "../bootstrap-data.js",
    "../bootstrap-launch.js"
]);
]]>
         </x-bootstrap>

        <!--
        for the local runner, we also need to include all spec files in the bootstrap
        info
        -->
        <x-script-def name="x-load-specs">
            <attribute name="base"/>
            <attribute name="property"/>
            <attribute name="configprop"/>
            <script src="${cmd.dir}/ant/ant-util.js"/>
            <![CDATA[
                    importPackage(com.sencha.util);
                    importPackage(com.sencha.logging);
                    var logger = SenchaLogManager.getLogger(),
					specDir = project.getProperty('cmd-test.specs.dir') + '',
                    propName = attributes.get("property") + '',
                    basePath = attributes.get("base") + '',
                    configPropName = attributes.get("configprop"),
                    config = readConfig(joinPath(specDir, 'test.json')),
                    getScriptFiles = function(path, basePath) {
                        var converted = [],
                            files = FileUtil.getAllFilesByExtension(path, ".js"),
                            length = files.size(), file, f,
                            relPath;
                        for(f = 0; f < length; f++) {
                            file = files.get(f);
                            logger.debug("getting relpath from {} to {}", basePath, file);
                            relPath = PathUtil.getRelativePath(basePath, file);
                            relPath = PathUtil.convertPathCharsToUnix(relPath);
                            converted.push('"' + relPath + '"');
                        }
                        return converted;
                    },
                    files = config[configPropName] || [],
                    len = files.length, file, f,
                    relPath,
                    converted = [];

                    for(f = 0; f < len; f++) {
                        file = files[f];
                        if(!PathUtil.isAbsolute(file)) {
                            file = PathUtil.join(specDir, file);
                        }
                        file = PathUtil.getCanonicalPath(file);
                        converted = converted.concat(getScriptFiles(file, basePath));
                    }
                    project.setNewProperty(propName, StringUtil.join(converted, ',\n'));
                ]]>
        </x-script-def>
        <x-load-specs base="${local.runner.dir}" property="specfiles.list" configprop="specs"/>
<echo file="${local.runner.dir}/${bootstrap.specs.file}"><![CDATA[
Ext.Boot.load({
    charset: "UTF-8",
    url: [
${specfiles.list}
    ],
    success: function() {
        Ext.Boot.load('../start-tests.js');
    }
});
]]>
</echo>
    </target>
    <target name="-after-test-bootstrap"/>
    <target name="test-bootstrap"
            depends="test-init,-before-test-bootstrap,-test-bootstrap,-after-test-bootstrap"
            description="Re-generate the bootstrap info for local and console test harnesses"/>

</project>
