<project name="BuildTest" default="build" >
	<!--
		Build the tests for a given implementation. The resulting tests will contain:
		1. All the tests from "common" AND
		2. All the tests for the specific implementation.

		This build must be called with the name of the implementation rootdir set in property:
		"implementation.name". If not set then the tests from "common" alone will be used.
	-->
	<import file="build-import.xml"/>
	<property name="common.test.rootdir" location="${theme.src.dir}/test"/>
	<property name="test.output.dir" location="${build.rootdir}/test-classes"/>
	<property name="test.output.rootdir" location="${test.output.dir}/${implementation.name}"/>
	<property name="intern.target.dir" location="${test.output.rootdir}/intern"/>
	<property name="impl.intern.srcdir" location="${impl.src.dir}/src/test/intern"/>
	<property name="intern.srcdir" location="${common.test.rootdir}/intern"/>
	<property environment="env"/>

	<!--
		Intern config options
	-->
	<property name="intern.capabilities" value="recordVideo: false, recordScreenshots: false" />
	<property name="intern.tunnel" value="SauceLabsTunnel" />
	<property name="intern.tunnel.options" value="verbose: true" />
	<!-- set true to force build to fail if tests fail when running a local tunnel using tunnel = "SeleniumTunnel" -->
	<property name="intern.fail.local.test" value="false" />
	<!--
		Intern reporters
		================

		Needs to include the `	reporters: [],` bit (including the comma). See https://theintern.github.io/intern/#reporter-results.

		Some samples:

		simple reporters:
			reporters: [{id: 'Console'}], - just output to console
			reporters: [{id: 'JUnit' filename '/PATH/TO/junit.xml'}],
			reporters: [{id: 'Runner', filename: '/PATH/TO/runner.txt'}],

		coverage reporters:
			reporters: [{id: 'LcovHtml', directory: '/PATH/TO/'}],
			reporters: [{id: 'Lcov', filename: '/PATH/TO/lcov.info'}],

	-->
	<property name="intern.reporters" value=""/>

	<target name="build" description="Build unit tests" depends="init, intern">
		<antcall target="checkPropertiesExpanded"/>
		<jsunit srcDir="${intern.target.dir}"/>
	</target>

	<target name="intern">
		<copy todir="${intern.target.dir}" overwrite="true">
			<fileset dir="${intern.srcdir}">
				<include name="**/*.js"/>
			</fileset>
			<fileset dir="${impl.intern.srcdir}" erroronmissingdir="false">
				<include name="**/*.js"/>
			</fileset>
			<filterset>
				<filter token="RESOURCES" value="../../target/test-classes/${implementation.name}/intern/resources"/>
				<filter token="HERE" value="${here}"/>
			</filterset>
			<filterchain>
				<expandproperties/>
			</filterchain>
		</copy>

		<!-- NOTE: the two step copy here is so we do not try to apply filters to binary resources -->
		<copy todir="${intern.target.dir}/resources">
			<fileset dir="${intern.srcdir}/resources" includes="**/*.xml,**/*.html,**/*.css"/>
			<fileset dir="${impl.intern.srcdir}/resources" includes="**/*.xml,**/*.html,**/*.css" erroronmissingdir="false"/>
			<filterchain>
				<expandproperties/>
			</filterchain>
		</copy>
		<copy todir="${intern.target.dir}/resources">
			<fileset dir="${intern.srcdir}/resources" excludes="**/*.xml,**/*.html,**/*.css"/>
			<fileset dir="${impl.intern.srcdir}/resources" excludes="**/*.xml,**/*.html,**/*.css" erroronmissingdir="false"/>
		</copy>
		<javascript.lint jsdir="${intern.target.dir}"/>
	</target>


	<target name="clean" description="Cleans up all artifacts produced by this build">
		<delete dir="${intern.target.dir}"/>
		<delete dir="${test.output.dir}"/>
		<echo message="Clean from ${ant.file}"/>
	</target>


	<target name="init" depends="clean">
		<mkdir dir="${intern.target.dir}"/>
		<mkdir dir="${test.output.dir}"/>
	</target>

	<target name="test" depends="build" description="Run the JavaScript unit tests"/>

	<!--
		Runs javascript unit tests.
	-->
	<macrodef name="jsunit">
		<attribute name="srcDir" default="${src.dir}"/>
		<attribute name="configModule" default="intern"/>
		<attribute name="relativePathToSource" default="target/classes/theme/${implementation.name}/${script.debug.target.dir.name}"/>
		<attribute name="relativePathToTestdir" default="target/test-classes/${implementation.name}"/>
		<attribute name="relativePathToTests" default="@{relativePathToTestdir}/intern"/>
		<attribute name="failonerror" default="true"/>
		<sequential>
			<local name="unit.tests"/>
			<local name="unit.test.src.dir"/>
			<local name="unit.test.src.dir.raw"/>
			<local name="amd.src.dir"/>
			<local name="test.baseurl"/>
			<property name="test.environments" value="{ browserName: 'firefox' }, { browserName: 'internet explorer', version: '11.0', platform: 'Windows 10' }, { browserName: 'chrome' }"/>
			<property name="amd.src.dir" value="@{relativePathToSource}"/>
			<property name="unit.test.src.dir.raw" location="@{srcDir}"/>
			<pathconvert targetos="unix" property="unit.test.src.dir">
				<path>
					<pathelement location="${unit.test.src.dir.raw}"/>
				</path>
			</pathconvert>
			<fileset dir="${intern.target.dir}" id="unitTests">
				<include name="*.js"/>
			</fileset>
			<pathconvert pathsep=",&#10;" property="unit.tests" refid="unitTests">
				<chainedmapper>
					<regexpmapper from="([^\/]+)\.js" to="&quot;@{relativePathToTests}/\1&quot;" handledirsep="true"/>
				</chainedmapper>
			</pathconvert>
			<echo>Found unit tests: ${unit.tests}</echo>
			<pathconvert targetos="unix" property="test.baseurl">
				<path>
					<pathelement location="${intern.target.dir}"/>
				</path>
			</pathconvert>
			<copy todir="${test.output.rootdir}" overwrite="true" force="true">
				<fileset dir="${common.test.rootdir}">
					<include name="@{configModule}.js"/>
				</fileset>
				<filterchain>
					<expandproperties />
				</filterchain>
			</copy>

			<property name="intern.executable" location="${basedir}/node_modules/intern/runner.js"/>
			<echo>Running client side unit tests.
If you get errors and need to run them locally the easiest way is to run a local webserver from the theme directory.
For your convenience we the WComponents theme comes with a jetty server ready to go. After building the tests run (from the theme project):

	mvn jetty:start

Now open a web browser and visit:

	http://localhost:8080/target/

Clicky the linky and the tests should run.
			</echo>
			<echo file="${build.rootdir}/index.html">
				<![CDATA[
					<a href="/node_modules/intern/client.html?config=target/test-classes/${implementation.name}/intern">Unit Tests</a>
				]]>
			</echo>
			<if>
				<and>
					<equals arg1="${intern.tunnel}" arg2="SauceLabsTunnel"/>
					<isset property="env.SAUCE_USERNAME"/>
					<isset property="env.SAUCE_ACCESS_KEY"/>
				</and>
				<then>
					<echo>node ${intern.executable} config=@{relativePathToTestdir}/@{configModule}</echo>
					<nodejs.exec cwd="${basedir}" failonerror="true" spawn="false">
						<arguments>
							<arg value="${intern.executable}"/>
							<arg value="config=@{relativePathToTestdir}/@{configModule}"/>
						</arguments>
					</nodejs.exec>
				</then>
				<elseif>
					<equals arg1="${intern.tunnel}" arg2="SeleniumTunnel"/>
					<then>
						<echo>node ${intern.executable} config=@{relativePathToTestdir}/@{configModule}</echo>
						<nodejs.exec cwd="${basedir}" failonerror="${intern.fail.local.test}" spawn="false">
							<arguments>
								<arg value="${intern.executable}"/>
								<arg value="config=@{relativePathToTestdir}/@{configModule}"/>
							</arguments>
						</nodejs.exec>
					</then>
				</elseif>
				<elseif>
					<equals arg1="${intern.tunnel}" arg2="NullTunnel"/>
					<then>
						<echo>
☠ ☠ ☠ ☠ ☠ ☠ ☠ ☠ ☠ ☠ ☠ ☠ ☠ ☠ ☠ ☠ ☠ ☠ ☠ ☠ ☠ ☠ ☠ ☠ ☠ ☠ ☠ ☠ ☠ ☠ ☠ ☠ ☠ ☠ ☠ ☠ ☠ ☠ ☠ ☠
Skipping unit tests because you have set a NullTunnel:

To manually start the tests try:
node ${intern.executable} config=@{relativePathToTestdir}/@{configModule}
☠ ☠ ☠ ☠ ☠ ☠ ☠ ☠ ☠ ☠ ☠ ☠ ☠ ☠ ☠ ☠ ☠ ☠ ☠ ☠ ☠ ☠ ☠ ☠ ☠ ☠ ☠ ☠ ☠ ☠ ☠ ☠ ☠ ☠ ☠ ☠ ☠ ☠ ☠ ☠
						</echo>
					</then>
				</elseif>
				<else>
					<echo>
☠ ☠ ☠ ☠ ☠ ☠ ☠ ☠ ☠ ☠ ☠ ☠ ☠ ☠ ☠ ☠ ☠ ☠ ☠ ☠ ☠ ☠ ☠ ☠ ☠ ☠ ☠ ☠ ☠ ☠ ☠ ☠ ☠ ☠ ☠ ☠ ☠ ☠ ☠ ☠
Skipping unit tests because you have not set environment variables:

SAUCE_USERNAME
SAUCE_ACCESS_KEY

See: https://theintern.github.io/intern/#hosted-selenium
☠ ☠ ☠ ☠ ☠ ☠ ☠ ☠ ☠ ☠ ☠ ☠ ☠ ☠ ☠ ☠ ☠ ☠ ☠ ☠ ☠ ☠ ☠ ☠ ☠ ☠ ☠ ☠ ☠ ☠ ☠ ☠ ☠ ☠ ☠ ☠ ☠ ☠ ☠ ☠
					</echo>
				</else>
			</if>


			<!--
			<echo>${phantomjs.binary} - -webdriver=4444</echo>
				<parallel>
					<daemons>
						<wc.exec executable="${phantomjs.binary}" needscmd="false" spawn="false">
							<arguments>
								<arg value="- -webdriver=4444"/>
							</arguments>
						</wc.exec>
					</daemons>
					<sequential>
						<sleep seconds="3"/>
						<echo>node ${intern.executable} config=@{relativePathToTestdir}/@{configModule}</echo>
						<nodejs.exec cwd="${basedir}" failonerror="true" spawn="false">
							<arguments>
								<arg value="${intern.executable}"/>
								<arg value="config=@{relativePathToTestdir}/@{configModule}"/>
							</arguments>
						</nodejs.exec>
					</sequential>
				</parallel>
				-->
		</sequential>
	</macrodef>
</project>
