<?xml version="1.0"?>
<!--***********************************************************
 * 
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 * 
 ***********************************************************-->



<project basedir="." default="test">
    <property environment="env" />
    <property file="build.properties" />
    <property name="env.DISPLAY" value=":0.0"/>
	<property name="env.INPATH" value="testspace"/>
	<property name="env.TESTSPACE" value="${env.INPATH}" />
	<property name="env.JUNIT_HOME" value="external/junit" />
	<property name="junit.home" value="${env.JUNIT_HOME}" />
	<property name="testspace" value="${env.TESTSPACE}" />
    <property name="classes" value="${testspace}/class" />
	<property name="dist" value="." />
	<property name="test.name" value="BVT" />
	<property name="test.classes" value="testcase/gui/bvt/*.class" />
	<property name="test.output" value="${testspace}/output" />
	<property name="test.result" value="${test.output}/result" />
	<property name="test.report" value="${test.output}/report" />
	<property name="junit.style.dir" value="reportstyle" />

	<path id="junit.classpath">
		<fileset dir="${junit.home}" erroronmissingdir="false">
			<include name="*.jar" />
		</fileset>
	</path>

	<target name="testcommon.init">
		<mkdir dir="${classes}" />
		<copy includeemptydirs="false" todir="${classes}">
			<fileset dir="testcommon/source">
				<exclude name="**/*.java" />
			</fileset>
		</copy>
	</target>

	<target name="testcommon.compile" depends="testcommon.init, prepare.junit">
		<javac destdir="${classes}" debug="on" source="1.6" encoding="utf-8" includeantruntime="false">
			<src path="testcommon/source"/>
			<classpath>
				<path refid="junit.classpath"/>
			</classpath>
		</javac>
	</target>

	<target name="testgui.init">
		<mkdir dir="${classes}" />
		<copy includeemptydirs="false" todir="${classes}">
			<fileset dir="testgui/source">
				<exclude name="**/*.java" />
			</fileset>
		</copy>
	</target>

	<target name="testgui.compile" depends="testgui.init, prepare.junit">
		<javac destdir="${classes}" debug="on" source="1.6" encoding="utf-8" includeantruntime="false">
			<src path="testgui/source"/>
			<classpath>
				<pathelement location="${classes}" />
				<path refid="junit.classpath"/>
			</classpath>
		</javac>
	</target>

	<target name="testuno.init">
		<mkdir dir="${classes}" />
		<copy includeemptydirs="false" todir="${classes}">
			<fileset dir="testuno/source">
				<exclude name="**/*.java" />
			</fileset>
		</copy>
	</target>

	<target name="testuno.compile" depends="testuno.init">
		<path id="uno.classpath">
			<fileset dir="${openoffice.home}" erroronmissingdir="false">
				<include name="**/juh.jar" />
				<include name="**/unoil.jar" />
				<include name="**/ridl.jar" />
				<include name="**/jurt.jar" />
			</fileset>
		</path>
		<javac destdir="${classes}" debug="on" source="1.6" encoding="utf-8" includeantruntime="false">
			<src path="testuno/source"/>
			<classpath>
				<pathelement location="${classes}" />
				<path refid="junit.classpath"/>
				<path refid="uno.classpath"/>
			</classpath>
		</javac>
	</target>

	<target name="clean" description="Clean all output">
		<delete dir="${testspace}" />
	</target>

	<target name="check.junit">
		<available file="junit.jar" property="junit.jar.exists">
			<filepath refid="junit.classpath" />
		</available>
	</target>

	<target name="prepare.junit" depends="check.junit" unless="junit.jar.exists">
		<property name="junit.jar.repos" value="http://repo1.maven.org/maven2/junit/junit/4.10/junit-4.10.jar" />
		<mkdir dir="${junit.home}" />
		<get src="${junit.jar.repos}" dest="${junit.home}/junit.jar" skipexisting="true" />
	</target>

	<target name="compile" depends="testcommon.init, testcommon.compile, testgui.init, testgui.compile, testuno.init, testuno.compile" description="Compile source code">
	</target>

	<target name="dist">
		<tstamp />
		<property name="dist.archive" value="aoo_test_${DSTAMP}.zip" />
        <zip destfile="${dist}/${dist.archive}" basedir="." update="true" includes="testcommon/**,testgui/**,testuno/**,build.xml,build.example.properties,external/**,reportstyle/**" excludes="**/bin/**">
        </zip>
	</target>

	<target name="check.build" description="Check the build context">
		<condition property="openoffice.pack.dir" value="${env.SRC_ROOT}/instsetoo_native/${env.INPATH}/OpenOffice/archive/install/en-US">
			<isset property="env.SRC_ROOT" />
		</condition>
		<condition property="find.build.skip">
			<or>
				<isset property="openoffice.pack.url" />
				<isset property="openoffice.pack.dir" />
				<isset property="openoffice.home" />
			</or>
		</condition>
		<condition property="download.build.skip">
			<or>
				<isset property="openoffice.pack.dir" />
				<isset property="openoffice.home" />
			</or>
		</condition>
		<condition property="install.build.skip">
            <isset property="openoffice.home" />
		</condition>
	</target>

	<target name="find.build" unless="find.build.skip" description="Find the newest build on the remote server">
		<loadresource property="openoffice.build">
			<url url="${openoffice.build.url}" />
			<filterchain>
				<deletecharacters chars=" \t\r\n" />
			</filterchain>
		</loadresource>
        
        <echo message="openoffice.pack.url=${openoffice.archive.url}" file="${testspace}/.temp.properties" />
        <property file="${testspace}/.temp.properties"/>
        <property name="openoffice.build.name" value="${openoffice.build}" />
        <echo>#OpenOffice Build Name: ${openoffice.build.name}</echo>
        <echo>#OpenOffice Package URL: ${openoffice.pack.url}</echo>
	</target>

    <target name="download" unless="${skip}">
        <delete dir="${dest}" />
        <mkdir dir="${dest}" />
        <get src="${src}" dest="${dest}" verbose="false" usetimestamp="true" skipexisting="true" />
    </target>

	<target name="download.build" unless="download.build.skip" description="Download the specified build from the remote server">
		<property name="openoffice.pack.dir" value="${testspace}/download" />
        <property name="openoffice.pack.download.mark" value="${testspace}/download/url.txt"/>
        <loadfile property="openoffice.pack.download.url" srcFile="${openoffice.pack.download.mark}" quiet="true" failonerror="false"/>
        <condition property="download.build.skip" value="true" else="false">
            <equals arg1="${openoffice.pack.download.url}" arg2="${openoffice.pack.url}"/>
		</condition>
        
        <echo>#Build is Downloaded: ${download.build.skip}</echo>
        <antcall target="download" inheritAll="false">
            <param name="skip" value="${download.build.skip}"/>
            <param name="src" value="${openoffice.pack.url}"/>
            <param name="dest" value="${openoffice.pack.dir}"/>
        </antcall>
        <!--
		<get src="${openoffice.pack.url}" dest="${openoffice.pack.dir}" verbose="false" usetimestamp="true" skipexisting="true" />
        -->
        <echo message="${openoffice.pack.url}" file="${openoffice.pack.download.mark}" />
        <echo>#OpenOffice Package Dir: ${openoffice.pack.dir}</echo>
	</target>

	<target name="install.build" unless="install.build.skip" description="Install the build to the local">
		<property name="openoffice.install.dir" value="${testspace}/install/aoo" />
        <property name="openoffice.install.temp" value="${testspace}/install/temp" />
        <delete dir="${openoffice.install.temp}"/>
		<mkdir dir="${openoffice.install.temp}" />
		<unzip dest="${openoffice.install.temp}">
			<fileset dir="${openoffice.pack.dir}">
				<include name="**/Apache_OpenOffice*.zip" />
			</fileset>
		</unzip>
		<pathconvert property="gz.files" pathsep=" " setonempty="false">
			<path>
				<fileset dir="${openoffice.pack.dir}" includes="Apache_OpenOffice*.gz" />
			</path>
		</pathconvert>
		<exec dir="${openoffice.install.temp}" executable="tar" failifexecutionfails="false">
			<arg line="-zxpf ${gz.files}" />
		</exec>
        <pathconvert property="openoffice.root.dir" pathsep=" " setonempty="false">
			<path>
				<dirset dir="${openoffice.install.temp}">
                    <include name="*"/>
                </dirset>
			</path>
		</pathconvert>
        <delete dir="${openoffice.install.dir}"/>
        <move file="${openoffice.root.dir}" tofile="${openoffice.install.dir}"/>
        <delete dir="${openoffice.install.temp}"/>
		<pathconvert property="openoffice.bin" pathsep=" " setonempty="false">
			<path>
				<fileset dir="${openoffice.install.dir}" includes="**/*/soffice.bin" followsymlinks="false" />
			</path>
		</pathconvert>
		<dirname property="openoffice.bin.parent" file="${openoffice.bin}" />
		<property name="openoffice.home" location="${openoffice.bin.parent}/../" />
		<fail unless="openoffice.home" />
		<echo>#Openoffice Home: ${openoffice.home}</echo>
	</target>

	<target name="run.test" depends="compile, detect.testenv" description="Run junit">
		<tstamp>
			<format property="output.stamp" pattern="yyMMdd.hhmm" />
		</tstamp>

		<move file="${test.output}" tofile="${test.output}.${output.stamp}" failonerror="false" />
		<mkdir dir="${test.result}" />
		<mkdir dir="${test.report}" />
		<mkdir dir="${test.output}/temp" />
		
		<junit fork="yes" forkmode="once" tempdir="${test.output}/temp" printsummary="yes" showoutput="false" errorProperty="test.failed" failureProperty="test.failed" dir=".">
			<env key="DISPLAY" value="${env.DISPLAY}"/>
            <sysproperty key="openoffice.home" value="${openoffice.home}" />
			<sysproperty key="testspace" value="${testspace}" />
			<syspropertyset>
				<propertyref builtin="commandline" />
			</syspropertyset>
			<batchtest todir="${test.result}">
				<fileset dir="${classes}" includes="${test.classes}" />
			</batchtest>

			<formatter type="xml" />
			<classpath>
				<pathelement location="${classes}" />
                <pathelement location="testgui/data" />
                <pathelement location="testuno/data" />
				<path refid="junit.classpath"/>
				<path refid="uno.classpath"/>
			</classpath>
		</junit>
        
		<junitreport todir="${test.report}">
			<fileset dir="${test.result}">
				<include name="TEST-*.xml" />
			</fileset>
			<report format="frames" styledir="${junit.style.dir}" todir="${test.report}">
				<param name="TITLE" expression="${test.name}: ${openoffice.build.name}, ${test.os.name}-${test.os.version}-${test.os.arch}"/>
			</report>
		</junitreport>
		<property name="test.report.index" location="${test.report}/index.html" />
		<echo>Open ${test.report.index} in browser to view the test report.</echo>
	</target>

	<target name="test" depends="check.build,find.build,download.build,install.build,run.test" description="Run testing on the specified build. The build is automatically downloaded and installed according to the context.">
		<fail message="Test Failed" if="test.failed" />
	</target>
    
	<target name="detect.testenv">
		<exec outputproperty="test.os.name" executable="lsb_release" failifexecutionfails="false" os="Linux">
			<arg line="-is" />
		</exec>
		<exec outputproperty="test.os.version" executable="lsb_release" failifexecutionfails="false" os="Linux">
			<arg line="-rs" />
		</exec>
		<property name="test.os.name" value="${os.name}" />
		<property name="test.os.version" value="${os.version}" />
		<property name="test.os.arch" value="${os.arch}" />
		<echo>${test.os.name}-${test.os.version}-${test.os.arch}</echo>
        <property file="${openoffice.home}/program/versionrc" prefix="openoffice"/>
        <property file="${openoffice.home}/program/version.ini" prefix="openoffice"/>
        <property name="openoffice.build.name" value=""/>
	</target>


	<target name="report.test" unless="report.test.skip" description="Upload the testing result to report repository." depends="detect.testenv">
		<property name="report.to" value="${openoffice.build.name}/${test.name}/${test.os.name}-${test.os.version}-${test.os.arch}" />
		<echo>Uploading report to ${report.repos}/${report.to}</echo>
		<property name="report.to.temp" location="${testspace}/.temp.ouput"/>
        <delete dir="${report.to.temp}" deleteonexit="true"/>
		<copy todir="${report.to.temp}/${report.to}">
			<fileset dir="${test.output}" />
		</copy>
		<scp todir="${report.repos}" trust="true">
			<fileset dir="${report.to.temp}"/>
		</scp>
        <delete dir="${report.to.temp}" deleteonexit="true"/>
	</target>

	<target name="detect.build" depends="find.build" description="Check if new build is available. If no new build is available, the target will be failed.">
		<property name="test.mark.file" location="${testspace}/${test.name}.build"/>
		<loadfile property="local.build" srcFile="${test.mark.file}" quiet="true" failonerror="false" />
		<fail message="The build has been tested! We don't want to test it twice.">
			<condition>
				<equals arg1="${openoffice.build}" arg2="${local.build}" trim="true" />
			</condition>
		</fail>
		<mkdir dir="${testspace}" />
		<echo file="${test.mark.file}">${openoffice.build}</echo>
	</target>

	<target name="routine.test" depends="detect.build,download.build,install.build,run.test,report.test" description="Periodically run testing.">
		<fail message="Test Failed" if="test.failed" />
	</target>
</project>
