Update Real.au3

Update Real.au3

; #FUNCTION# ====================================================================================================================
; Version........: 0.0.0.1
; Name...........: Mask/Real
; Description ...: Aplica uma máscara de número real 0.00 em uma entrada numérica
; Syntax.........:
; Parameters ....:
; Return values .:
; Author ........: Luismar Chechelaky
; Creation.......: 2017/12/23
; Modified.......:
; Remarks .......:
; Related .......:
; Link ..........: https://github.com/chechelaky/AutoIt/blob/master/Diversos/Mask/Real.au3
; Example .......: https://github.com/chechelaky/AutoIt/blob/master/Diversos/Mask/Real_Exemplo.au3
; ===============================================================================================================================

Func Real($num = "")
	If $number == "" Then Return ""
	If $number == "." Then Return "0."
	$number = StringRegExpReplace(StringReplace($number, ",", "."), "[^0-9.]", "")
	Local $dot = StringInStr($number, ".")
	If Not $dot Then Return $number
	Return StringReplace(StringMid($number, 1, $dot - 1), ".", "") & "." & StringReplace(StringMid($number, $dot + 1), ".", "")
EndFunc   ;==>Real


Write profile to both game file locations

++ corrected language regarding flash player opening

#include <String.au3>
#include <File.au3>
#include <Array.au3>

Global $string, $password, $email, $data, $path, $search, $file, $root
$root = "HKEY_CLASSES_ROOT\muledump"

Func _write()
	RegWrite($root,"","REG_SZ","URL: muledump Protocol")
	RegWrite($root,"URL Protocol","REG_SZ","")
	RegWrite($root & "\shell")
	RegWrite($root & "\shell\open")
	RegWrite($root & "\shell\open\command","","REG_SZ", @AutoItExe & ' "' & @ScriptFullPath & '" %1')
	If RegRead("HKEY_CLASSES_ROOT\muledump","") Then
		MsgBox(64,"mulelogin","muledump: URL handler installed")
	Else
		MsgBox(16,"mulelogin","Unable to write to registry" & @CRLF & "Try again with admin rights")
	EndIf
	Exit
EndFunc

Func _install()
	Local $k
	$k = RegEnumKey($root, 1)
	If @error == 2 Then
		MsgBox(16,"mulelogin","Unable to open the registry" & @CRLF & "Try again with admin rights")
		Exit
	EndIf
	If @error == 1 Then _write()
	$k = MsgBox(6 + 32, 'mulelogin', _
		'URL handler is already installed. Uninstall?' & @CRLF & @CRLF & _
		'"Cancel" to do nothing' & @CRLF & _
		'"Try Again" to reinstall' & @CRLF & _
		'"Continue" to uninstall')
	If $k == 10 Then _write()
	If $k == 11 Then
		RegDelete($root)
		if @error <> 0 Then
			MsgBox(16,"mulelogin","Unable to delete the key" & @CRLF & "Try again with admin rights")
		Else
			MsgBox(64,"mulelogin","muledump: URL handler uninstalled")
		EndIf
	EndIf
	Exit
EndFunc

Func _length($string)
	Local $binlength, $declength, $array, $result
	$declength = StringLen($string)/2
    While $declength > 0
        $binlength &= Mod($declength,2)
        $declength = Floor($declength/2)
    WEnd
	$binlength = StringReverse($binlength)
	if @error <> 0 Then
		$binlength = _StringReverse($binlength)
	EndIf
	$binlength = $binlength & "1"
	$array = StringSplit($binlength,"")
	For $i = 1 To $array[0]
        $array[0] -= 1
        If $array[$i] = "1" Then $result += 2 ^ ($array[0])
	Next
	Return Hex(Int($result),2)
EndFunc

Func _build()
	;header
	$string  = "0x 00 BF" 						;Magic Number		 2 bytes
	$string &= "?? ?? ?? ??"					;Size				 4 bytes
	$string &= "54 43 53 4F 00 04 00 00 00 00"	;Marker				10 bytes
	$string &= "00 05"							;Name Size			 2 bytes
	$string &= "52 6F 74 4D 47"					;"RotMG"			 5 bytes
	$string &= "00 00 00"						;Padding			 3 bytes
	$string &= "03"								;AMF Version		 1 byte

	;data
	$string &= "11"								;Length				 1 byte
	$string &= "50 61 73 73 77 6F 72 64"		;"Password"			 8 bytes
	$string &= "06"								;Type (string) 		 1 byte
	$string &= _length($password)				;Length				 1 byte
	$string &= $password						;Actual Password	 ? bytes
	$string &= "00"								;AMF Padding		 1 byte

	$string &= "09"								;Length				 1 byte
	$string &= "47 55 49 44"					;"GUID"				 4 bytes
	$string &= "06"								;Type (string)		 1 byte
	$string &= _length($email)					;Length				 1 byte
	$string &=  $email							;Email				 ? bytes
	$string &= "00"								;AMF Padding		 1 byte

	$string = StringReplace($string," ","")
	$string = StringRegExpReplace($string,"\?{8}",Hex(Int(StringLen(StringMid($string,15))/2)))
EndFunc

If $CmdLine[0] = 0 Then _install()

$data = StringReplace($CmdLine[1],"muledump:","")
$data = StringSplit($data,"-")
$email = $data[1]
$password = $data[2]

_build()

Local $paths[2] = ["www.realmofthemadgod.com", "realmofthemadgodhrd.appspot.com"]
$path = @AppDataDir & "\Macromedia\Flash Player\#SharedObjects\"
$search = FileFindFirstFile($path & "*")
$searchPath = FileFindNextFile($search)
For $gameDir In $paths
	$gameFilePath = $path & $searchPath & "\" & $gameDir & "\RotMG.sol"
	$file = FileOpen($gameFilePath,18)
	FileWrite($file,$string)
	FileClose($file)
Next
FileClose($search)

ShellExecute("http://www.realmofthemadgod.com/")
; replace the line above if you're using a projector
; for example, with totalcmd + swfview
;ShellExecute('C:\Program Files\Total Commander\Totalcmd.exe', '/S=L:Pswfview e:\temp\rotmg\loader.swf')
; or to open the latest swf with the Adobe Flash Projector
;ShellExecute('C:\flashplayer_16_sa.exe', 'https://realmofthemadgodhrd.appspot.com/AssembleeGameClient'&BinaryToString(InetRead("http://www.realmofthemadgod.com/version.txt"))&'.swf')

#include <String.au3>
#include <File.au3>
#include <Array.au3>

Global $string, $password, $email, $data, $path, $search, $file, $root
$root = "HKEY_CLASSES_ROOT\muledump"

Func _write()
	RegWrite($root,"","REG_SZ","URL: muledump Protocol")
	RegWrite($root,"URL Protocol","REG_SZ","")
	RegWrite($root & "\shell")
	RegWrite($root & "\shell\open")
	RegWrite($root & "\shell\open\command","","REG_SZ", @AutoItExe & ' "' & @ScriptFullPath & '" %1')
	If RegRead("HKEY_CLASSES_ROOT\muledump","") Then
		MsgBox(64,"mulelogin","muledump: URL handler installed")
	Else
		MsgBox(16,"mulelogin","Unable to write to registry" & @CRLF & "Try again with admin rights")
	EndIf
	Exit
EndFunc

Func _install()
	Local $k
	$k = RegEnumKey($root, 1)
	If @error == 2 Then
		MsgBox(16,"mulelogin","Unable to open the registry" & @CRLF & "Try again with admin rights")
		Exit
	EndIf
	If @error == 1 Then _write()
	$k = MsgBox(6 + 32, 'mulelogin', _
		'URL handler is already installed. Uninstall?' & @CRLF & @CRLF & _
		'"Cancel" to do nothing' & @CRLF & _
		'"Try Again" to reinstall' & @CRLF & _
		'"Continue" to uninstall')
	If $k == 10 Then _write()
	If $k == 11 Then
		RegDelete($root)
		if @error <> 0 Then
			MsgBox(16,"mulelogin","Unable to delete the key" & @CRLF & "Try again with admin rights")
		Else
			MsgBox(64,"mulelogin","muledump: URL handler uninstalled")
		EndIf
	EndIf
	Exit
EndFunc

Func _length($string)
	Local $binlength, $declength, $array, $result
	$declength = StringLen($string)/2
    While $declength > 0
        $binlength &= Mod($declength,2)
        $declength = Floor($declength/2)
    WEnd
	$binlength = StringReverse($binlength)
	if @error <> 0 Then
		$binlength = _StringReverse($binlength)
	EndIf
	$binlength = $binlength & "1"
	$array = StringSplit($binlength,"")
	For $i = 1 To $array[0]
        $array[0] -= 1
        If $array[$i] = "1" Then $result += 2 ^ ($array[0])
	Next
	Return Hex(Int($result),2)
EndFunc

Func _build()
	;header
	$string  = "0x 00 BF" 						;Magic Number		 2 bytes
	$string &= "?? ?? ?? ??"					;Size				 4 bytes
	$string &= "54 43 53 4F 00 04 00 00 00 00"	;Marker				10 bytes
	$string &= "00 05"							;Name Size			 2 bytes
	$string &= "52 6F 74 4D 47"					;"RotMG"			 5 bytes
	$string &= "00 00 00"						;Padding			 3 bytes
	$string &= "03"								;AMF Version		 1 byte

	;data
	$string &= "11"								;Length				 1 byte
	$string &= "50 61 73 73 77 6F 72 64"		;"Password"			 8 bytes
	$string &= "06"								;Type (string) 		 1 byte
	$string &= _length($password)				;Length				 1 byte
	$string &= $password						;Actual Password	 ? bytes
	$string &= "00"								;AMF Padding		 1 byte

	$string &= "09"								;Length				 1 byte
	$string &= "47 55 49 44"					;"GUID"				 4 bytes
	$string &= "06"								;Type (string)		 1 byte
	$string &= _length($email)					;Length				 1 byte
	$string &=  $email							;Email				 ? bytes
	$string &= "00"								;AMF Padding		 1 byte

	$string = StringReplace($string," ","")
	$string = StringRegExpReplace($string,"\?{8}",Hex(Int(StringLen(StringMid($string,15))/2)))
EndFunc

If $CmdLine[0] = 0 Then _install()

$data = StringReplace($CmdLine[1],"muledump:","")
$data = StringSplit($data,"-")
$email = $data[1]
$password = $data[2]

_build()

$path = @AppDataDir & "\Macromedia\Flash Player\#SharedObjects\"
$search = FileFindFirstFile($path & "*")
$path &= FileFindNextFile($search) & "\realmofthemadgodhrd.appspot.com\RotMG.sol"
FileClose($search)
$file = FileOpen($path,18)
FileWrite($file,$string)
FileClose($file)

ShellExecute("http://www.realmofthemadgod.com/")
; replace the line above if you're using a projector
; for example, with totalcmd + swfview
;ShellExecute('C:\Program Files\Total Commander\Totalcmd.exe', '/S=L:Pswfview e:\temp\rotmg\loader.swf')
; or to open the latest swf with the default Adobe Flash Projector
;ShellExecute('C:\flashplayer_16_sa.exe', 'https://realmofthemadgodhrd.appspot.com/AssembleeGameClient'&BinaryToString(InetRead("http://www.realmofthemadgod.com/version.txt"))&'.swf')


Changed installer window size to smaller.

;;  Copyright 2008 Nokia Siemens Networks Oyj
;;
;;  Licensed 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.

;; Robot Framework One Click Installer for Windows
;;
;; This installer installs Robot Framework and its preconditions Python and
;; Jython (optional). If installer is executed without arguments, it opens a
;; dialog explaining the options and asking the user to enter the installation
;; directory. If a command line options is given, it must be a path to an
;; existing directory and it is used as installation directory.
;;
;; For more information about this script see $USAGE below. To learn more
;; about silent Python installation, see url [1]. Silent Jython installation
;; options can be seen with command [2].
;;
;; [1] http://www.python.org/download/releases/2.5/msi/#automated
;; [2] java -jar jython_installer-2.2.x.jar --help

;;
;; Config
;;

; Expected patterns for installer names and target directory names.
$PYTHON_DIR_NAME = "Python"
$JYTHON_DIR_NAME = "Jython"
$PYTHON_PATTERN = "python-2.*.msi"
$ROBOT_PATTERN = "robotframework-2.*.exe"
$JYTHON_PATTERN = "jython_installer-2.*.jar"

$USAGE = "One Click Installer installs Robot Framework and its " & _
         "preconditions Python and Jython (optional)." & @CRLF & @CRLF & _
         "Please specify the directory where you want software to be " & _
         "installed. Also make sure that you have required Robot Framework " & _
         "and Python installers, and optionally Jython installer, in the " & _
         "same directory as this installer." & @CRLF & @CRLF & _
         "For more information see http://robotframework.org/."

;;
;; Main
;;

$base_dir = GetBaseDirectory()
$python_installer = GetRequiredInstaller($PYTHON_PATTERN, "Python")
$robot_installer = GetRequiredInstaller($ROBOT_PATTERN, "Robot Framework")
$jython_installer = GetOptionalInstaller($JYTHON_PATTERN)
$python_dir = GetPythonDir($base_dir & $PYTHON_DIR_NAME)
$jython_dir = GetJythonDir($base_dir & $JYTHON_DIR_NAME, $jython_installer)
InstallPython($python_installer, $python_dir)
InstallJython($jython_installer, $jython_dir)
SetPath($python_dir, $jython_dir)  ; This must be done before InstallRobot to always have msvcrt71.dll in PATH
InstallRobot($robot_installer)
Exit

;;
;; Functions
;;

Func GetBaseDirectory()
    If $CmdLine[0] == 0 Then
        $base = InputBox("Robot Framework One Click Installer", $USAGE, "C:\", "", 400, 250)
    ElseIf $CmdLine[0] == 1 Then
        $base = $CmdLine[1]
    Else
        Cancel("Usage: " & @ScriptName & " [basedir]")
    EndIf
    If $base == "" Then
        Cancel("Installation cancelled by user.")
    ElseIf FileExists($base) == 0 Then
        Cancel("Given directory doesn't exist.")
    ElseIf StringRight($base, 1) <> "\" Then
        $base = $base & "\"
    EndIf
    Return $base
EndFunc

Func Cancel($msg)
    MsgBox(0, "Installation Cancelled", $msg)
    Exit
EndFunc

Func GetRequiredInstaller($pattern, $name)
    $installer = GetOptionalInstaller($pattern)
    If $installer == "" Then
        Cancel("Could not find " & $name & " installer with pattern '" & $pattern & "'")
    Endif
    return $installer
EndFunc

Func GetOptionalInstaller($pattern)
    $found = FileFindFirstFile($pattern)
    If $found == -1 Then
        $installer = ""
    Else
        $installer = FileFindNextFile($found)
    EndIf
    FileClose($found)
    return $installer
EndFunc

Func GetPythonDir($dir)
    VerifyInstallationDir($dir, "Python")
    return $dir
EndFunc

Func GetJythonDir($dir, $installer)
    If $installer == "" Then
        return ""
    EndIf
    VerifyInstallationDir($dir, "Jython")
    return $dir
EndFunc

Func VerifyInstallationDir($dir, $name)
    If FileExists($dir) == 1 Then
        Cancel($name & " installation directory '" & $dir & "' already exists.")
    EndIf
EndFunc

Func InstallPython($installer, $dir)
    $cmd = "msiexec /i " & $installer & " TARGETDIR=" & $dir & " /qb!"
    RunWait($cmd)
EndFunc

Func InstallJython($installer, $dir)
    If $installer == "" Then
        return
    EndIf
    $cmd = "java -jar " & $installer & " -s -d " & $dir
    RunWait($cmd)
EndFunc

Func InstallRobot($installer)
    Run($installer)
    WaitSetupWindowAndClickEnter("This Wizard will install")
    WaitSetupWindowAndClickEnter("Select python installation")
    WaitSetupWindowAndClickEnter("Ready to install")
    WaitSetupWindowAndClickEnter("Postinstall script finished")
EndFunc

Func WaitSetupWindowAndClickEnter($exp_text)
    WinWaitActive("Setup", $exp_text)
    Send("{ENTER}")
EndFunc

Func SetPath($python, $jython)
    $update = $python & ";" & $python & "\Scripts;" & $jython
    $orig = RegRead("HKCU\Environment", "PATH")

    If $orig == "" Then
        $new = $update
    ElseIf StringRight($orig, 1) == ";" Then
        $new = $orig & $update
    Else
        $new = $orig & ";" & $update
    EndIf

    RegWrite("HKCU\Environment", "PATH", "REG_EXPAND_SZ", $new)
    EnvUpdate()
EndFunc

;;  Copyright 2008 Nokia Siemens Networks Oyj
;;
;;  Licensed 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.

;; Robot Framework One Click Installer for Windows
;;
;; This installer installs Robot Framework and its preconditions Python and
;; Jython (optional). If installer is executed without arguments, it opens a
;; dialog explaining the options and asking the user to enter the installation
;; directory. If a command line options is given, it must be a path to an
;; existing directory and it is used as installation directory.
;;
;; For more information about this script see $USAGE below. To learn more
;; about silent Python installation, see url [1]. Silent Jython installation
;; options can be seen with command [2].
;;
;; [1] http://www.python.org/download/releases/2.5/msi/#automated
;; [2] java -jar jython_installer-2.2.x.jar --help

;;
;; Config
;;

; Expected patterns for installer names and target directory names.
$PYTHON_DIR_NAME = "Python"
$JYTHON_DIR_NAME = "Jython"
$PYTHON_PATTERN = "python-2.*.msi"
$ROBOT_PATTERN = "robotframework-2.*.exe"
$JYTHON_PATTERN = "jython_installer-2.*.jar"

$USAGE = "One Click Installer installs Robot Framework and its " & _
         "preconditions Python and Jython (optional)." & @CRLF & @CRLF & _
         "Please specify the directory where you want software to be " & _
         "installed. Also make sure that you have required Robot Framework " & _
         "and Python installers, and optionally Jython installer, in the " & _
         "same directory as this installer." & @CRLF & @CRLF & _
         "For more information see http://robotframework.org/."

;;
;; Main
;;

$base_dir = GetBaseDirectory()
$python_installer = GetRequiredInstaller($PYTHON_PATTERN, "Python")
$robot_installer = GetRequiredInstaller($ROBOT_PATTERN, "Robot Framework")
$jython_installer = GetOptionalInstaller($JYTHON_PATTERN)
$python_dir = GetPythonDir($base_dir & $PYTHON_DIR_NAME)
$jython_dir = GetJythonDir($base_dir & $JYTHON_DIR_NAME, $jython_installer)
InstallPython($python_installer, $python_dir)
InstallJython($jython_installer, $jython_dir)
SetPath($python_dir, $jython_dir)  ; This must be done before InstallRobot to always have msvcrt71.dll in PATH
InstallRobot($robot_installer)
Exit

;;
;; Functions
;;

Func GetBaseDirectory()
    If $CmdLine[0] == 0 Then
        $base = InputBox("Robot Framework One Click Installer", $USAGE, "C:\", "", 500, 470)
    ElseIf $CmdLine[0] == 1 Then
        $base = $CmdLine[1]
    Else
        Cancel("Usage: " & @ScriptName & " [basedir]")
    EndIf
    If $base == "" Then
        Cancel("Installation cancelled by user.")
    ElseIf FileExists($base) == 0 Then
        Cancel("Given directory doesn't exist.")
    ElseIf StringRight($base, 1) <> "\" Then
        $base = $base & "\"
    EndIf
    Return $base
EndFunc

Func Cancel($msg)
    MsgBox(0, "Installation Cancelled", $msg)
    Exit
EndFunc

Func GetRequiredInstaller($pattern, $name)
    $installer = GetOptionalInstaller($pattern)
    If $installer == "" Then
        Cancel("Could not find " & $name & " installer with pattern '" & $pattern & "'")
    Endif
    return $installer
EndFunc

Func GetOptionalInstaller($pattern)
    $found = FileFindFirstFile($pattern)
    If $found == -1 Then
        $installer = ""
    Else
        $installer = FileFindNextFile($found)
    EndIf
    FileClose($found)
    return $installer
EndFunc

Func GetPythonDir($dir)
    VerifyInstallationDir($dir, "Python")
    return $dir
EndFunc

Func GetJythonDir($dir, $installer)
    If $installer == "" Then
        return ""
    EndIf
    VerifyInstallationDir($dir, "Jython")
    return $dir
EndFunc

Func VerifyInstallationDir($dir, $name)
    If FileExists($dir) == 1 Then
        Cancel($name & " installation directory '" & $dir & "' already exists.")
    EndIf
EndFunc

Func InstallPython($installer, $dir)
    $cmd = "msiexec /i " & $installer & " TARGETDIR=" & $dir & " /qb!"
    RunWait($cmd)
EndFunc

Func InstallJython($installer, $dir)
    If $installer == "" Then
        return
    EndIf
    $cmd = "java -jar " & $installer & " -s -d " & $dir
    RunWait($cmd)
EndFunc

Func InstallRobot($installer)
    Run($installer)
    WaitSetupWindowAndClickEnter("This Wizard will install")
    WaitSetupWindowAndClickEnter("Select python installation")
    WaitSetupWindowAndClickEnter("Ready to install")
    WaitSetupWindowAndClickEnter("Postinstall script finished")
EndFunc

Func WaitSetupWindowAndClickEnter($exp_text)
    WinWaitActive("Setup", $exp_text)
    Send("{ENTER}")
EndFunc

Func SetPath($python, $jython)
    $update = $python & ";" & $python & "\Scripts;" & $jython
    $orig = RegRead("HKCU\Environment", "PATH")

    If $orig == "" Then
        $new = $update
    ElseIf StringRight($orig, 1) == ";" Then
        $new = $orig & $update
    Else
        $new = $orig & ";" & $update
    EndIf

    RegWrite("HKCU\Environment", "PATH", "REG_EXPAND_SZ", $new)
    EnvUpdate()
EndFunc


Apply second patch from HARMONY-4159:
([testing] EHWA scenario improvement)

svn path=/harmony/enhanced/buildtest/branches/2.0/; revision=548222

;     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.

; Set variables
Select
	Case $CmdLine[0] = 4 OR $CmdLine[0] = 5
		Dim Const $eclipse_home = $CmdLine[1]
		Dim Const $tested_jre = $CmdLine[2]
		Dim Const $resultsDir = $CmdLine[3]
		Dim Const $delay_factor = Number($CmdLine[4])
		Dim $jre_options = ""
		If $CmdLine[0] = 5 Then
			$jre_options  = $CmdLine[5]
		EndIf
		If $delay_factor < 1 Then
            ConsoleWrite(@LF & "ERROR: Delay factor < 1! " & @LF & "EHWA FAILED!" & @LF)
            Exit(1)
        EndIf
	Case Else
		ConsoleWrite("ERROR! Wrong number of input parameters! STOP." & @LF & "EHWA FAILED!" & @LF)
		Sleep(3000)
		Exit(1)
EndSelect

; Set options
Opt("WinWaitDelay", 1000) ; wait for 1c
Opt("SendKeyDelay", 200*$delay_factor) ; wait for 150mc
Opt("WinTitleMatchMode", 4)
Opt("WinDetectHiddenText", 1)
Opt("TrayIconDebug", 1)

LogWrite(@LF & "--- Automated Eclipse EHWA scenario for Eclipse 3.2.1 ---" & @LF & @LF)

; Check if some Eclipse window is already olpened
Opt("WinTitleMatchMode", 2)
If WinExists("Eclipse SDK") Then
	LogWrite("ERROR! Some Eclipse window is already opened. STOP." & @LF & "EHWA FAILED!" & @LF)
	Sleep(3000)
	Exit(1)
EndIf

LogWrite("Eclipse Home = " & $eclipse_home & @LF)
LogWrite("Tested JRE = " &  $tested_jre & @LF)
LogWrite("Tested JRE options for Eclipse launching = " & $jre_options & @LF)
LogWrite("Delay factor = " & $delay_factor & @LF)

; Start
LogWrite(@LF & "Start:" & @LF)
LogWrite(@TAB & "Run Eclipse" & @LF)
$run =  $tested_jre & "\bin\java.exe "  & $jre_options & " -cp """ & $eclipse_home & "\startup.jar"" -Dosgi.install.area=""" & $eclipse_home & """ org.eclipse.core.launcher.Main -debug > " & $resultsDir & "\eclipse.log 2>&1"
Run(@ComSpec & " /c " & $run, "", @SW_HIDE)

; 1
LogWrite(@TAB & "Wait Eclipse 'Workspace Launcher' window to appear " & @LF)
WinWaitImpl("Workspace Launcher", "", 180)
If Not WinActive("Workspace Launcher") Then WinActivate("Workspace Launcher")
WinWaitActiveImpl("Workspace Launcher", "", 30)
LogWrite(@TAB & "window appeared " & @LF)

; 2
LogWrite(@TAB & "Select " & @WorkingDir & "\workspace as workspace" & @LF)
ControlFocus("Workspace Launcher", "", "Edit1")
ControlSetText("Workspace Launcher", "", "Edit1", @WorkingDir & "\workspace" )
LogWrite(@TAB & "Press OK button" & @LF)
ControlSend("Workspace Launcher", "", "", "{ENTER}")
WinWaitCloseImpl("Workspace Launcher", "", 60)

; 3
LogWrite(@TAB & "Wait 'Java - Eclipse SDK' window to appear " & @LF)
WinWaitImpl("Java - Eclipse SDK", "", 180)
If Not WinActive("Java - Eclipse SDK") Then WinActivate("Java - Eclipse SDK")
WinWaitActiveImpl("Java - Eclipse SDK", "", 30)
LogWrite(@TAB & "window appeared " & @LF)
SleepImpl(1000)
LogWrite(@TAB & "Close Welcome page " & @LF)
;Send("!wvp{ENTER}")
Send("!+{NUMPADSUB}c")
SleepImpl(3000)

; 4
LogWrite(@TAB & "Create new  Java project" & @LF)
Send("!fnr")

; 5
LogWrite(@TAB & "Wait 'New Project' window to appear" & @LF)
WinWaitImpl("New Project","",60)
If Not WinActive("New Project","") Then WinActivate("New Project","")
WinWaitActiveImpl("New Project","",30)
LogWrite(@TAB & "window appeared " & @LF)
LogWrite(@TAB & "Select Java project" & @LF)
ControlFocus("New Project", "", "Edit1")
ControlSend("New Project", "", "Edit1", "{ENTER}")
LogWrite(@TAB & "Press Next button" & @LF)
ControlFocus("New Project", "&Next >", "Button2",)
ControlClick("New Project", "&Next >", "Button2")
If @error=1 Then
	LogWrite(@TAB & "ERROR! Next button wasn't found. STOP." & @LF)
	PrintErrorScreen()
	ExitImpl(1)
EndIf
WinWaitCloseImpl("New Project", "", 60)

; 6
LogWrite(@TAB & "Wait 'New Java Project' window to appear " & @LF)
WinWaitImpl("New Java Project","",60)
If Not WinActive("New Java Project") Then WinActivate("New Java Project")
WinWaitActiveImpl("New Java Project","",30)
LogWrite(@TAB & "window appeared, " & @LF)
SleepImpl(1000)
LogWrite(@TAB & "Type EHWA as a project name" & @LF)
ControlFocus("New Java Project", "", "Edit2")
ControlSetText("New Java Project", "", "Edit2", "EHWA" )
LogWrite(@TAB & "Press Finish button" & @LF)
ControlFocus("New Java Project", "&Finish", "Button27",)
ControlClick("New Java Project", "&Finish", "Button27")
If @error=1 Then
	LogWrite(@TAB & "ERROR! Finish button wasn't found. STOP." & @LF)
	PrintErrorScreen()
	ExitImpl(1)
EndIf
LogWrite(@TAB & "Wait while project is being created ... " & @LF)
WinWaitClose("New Java Project", "", 60)
LogWrite(@TAB & "Project was created" & @LF)

; 7
WinWaitImpl("Java - Eclipse SDK", "", 60)
If Not WinActive("Java - Eclipse SDK", "") Then WinActivate("Java - Eclipse SDK","")
WinWaitActiveImpl("Java - Eclipse SDK", "", 30)
LogWrite(@TAB & "Create new Java class" & @LF)
$result=WinMenuSelectItem("Java - Eclipse SDK", "", "&File", "&New", "Class")
if $result=0 Then
	LogWrite(@TAB & "ERROR! Menu 'File -> New -> Class' wasn't found. STOP." & @LF)
	PrintErrorScreen()
	ExitImpl(1)
EndIf
LogWrite(@TAB & "Wait 'New Java Class' window to appear" & @LF)
WinWaitImpl("New Java Class","",60)
If Not WinActive("New Java Class") Then WinActivate("New Java Class")
WinWaitActiveImpl("New Java Class","",30)
LogWrite(@TAB & "window appeared " & @LF)
SleepImpl(1000)

; 8
LogWrite(@TAB & "Type EHWA as class name" & @LF)
ControlFocus("New Java Class", "", "Edit4")
ControlSetText("New Java Class", "", "Edit4", "EHWA" )

;9
LogWrite(@TAB & "Select 'public static void main(String[] args)' checkbox" & @LF)
ControlFocus("New Java Class", "", "Button15")
ControlCommand("New Java Class", "", "Button15", "Check", "")

; 10
LogWrite(@TAB & "Click Finish button" & @LF)
ControlFocus("New Java Class", "&Finish", "Button19")
ControlClick("New Java Class", "&Finish", "Button19")
If @error=1 Then
	LogWrite(@TAB & "ERROR! Finish button wasn't found. STOP." & @LF)
	PrintErrorScreen()
	ExitImpl(1)
EndIf
LogWrite(@TAB & "Wait while EHWA class is being created ..." & @LF)
WinWaitClose("New Java Class", "", 60)

; 11
WinWaitImpl("Java - EHWA.java - Eclipse SDK","",60)
If Not WinActive("New Java Class") Then WinActivate("Java - EHWA.java - Eclipse SDK")
WinWaitActiveImpl("Java - EHWA.java - Eclipse SDK","",30)
LogWrite(@TAB & "EHWA class was successfully created" & @LF)
SleepImpl(3000)
LogWrite(@TAB & "Add System.out.println(""Hello, world!""); string in the main method of EHWA class" & @LF)
WinWaitImpl("Java - EHWA.java - Eclipse SDK","",60)
If Not WinActive("Java - EHWA.java - Eclipse SDK","") Then WinActivate("Java - EHWA.java - Eclipse SDK","")
WinWaitActiveImpl("Java - EHWA.java - Eclipse SDK","",30)
Send("{DOWN 8}{TAB}")
Send("System.out.println(""Hello, world{!}"");")
SleepImpl(3000)

; 12
WinWaitImpl("Java - EHWA.java - Eclipse SDK","",60)
If Not WinActive("Java - EHWA.java - Eclipse SDK","") Then WinActivate("Java - EHWA.java - Eclipse SDK","")
WinWaitActiveImpl("Java - EHWA.java - Eclipse SDK","",30)
LogWrite(@TAB & "Save EHWA class " & @LF)
$result=WinMenuSelectItem("Java - EHWA.java - Eclipse SDK", "", "&File", "&Save")
if $result=0 Then
	LogWrite(@TAB & "ERROR! Menu 'File->Save' wasn't found. Stop." & @LF)
	PrintErrorScreen()
	ExitImpl(1)
EndIf
LogWrite(@TAB & "Open console" & @LF)
Send("!w")
Send("v")
Send("c")
SleepImpl(3000)

; 13
WinWaitImpl("Java - EHWA.java - Eclipse SDK","",60)
If Not WinActive("Java - EHWA.java - Eclipse SDK","") Then WinActivate("Java - EHWA.java - Eclipse SDK","")
WinWaitActiveImpl("Java - EHWA.java - Eclipse SDK","",30)
LogWrite(@TAB & "Run EHWA class" & @LF)
Send("!r")
Send("n")
LogWrite(@TAB & "Wait 'Run' window to appear " & @LF)
WinWaitImpl("Run","",60)
If Not WinActive("Run") Then WinActivate("Run")
WinWaitActiveImpl("Run","",30)
LogWrite(@TAB & "window appeared " & @LF)

; 14
LogWrite(@TAB & "Select Java application " & @LF)
ControlFocus("Run", "", "Edit1")
ControlSend("Run", "", "Edit1", "{ENTER}")
ControlFocus("Run", "", "SysTreeView321")
ControlSend("Run", "", "SysTreeView321", "{j 2}")
LogWrite(@TAB & "Create new run configuration" & @LF)
ControlSend("Run", "", "SysTreeView321", "{ENTER}")

; 15
WinWaitImpl("Run","",60)
If Not WinActive("Run","") Then WinActivate("Run","")
WinWaitActiveImpl("Run","",30)
LogWrite(@TAB & "Wait while EHWA run configuration is being created ..." & @LF)
$iter = 0
While StringInStr(WinGetText("Run"), "EHWA") = 0
	$iter = $iter+1
	Sleep(500)
	If $iter = 360 Then
		LogWrite(@TAB & "ERROR! Can't create EHWA run configuration diring 3 min. STOP." & @LF)
		PrintErrorScreen()
		ExitImpl(1)
	EndIf
WEnd
LogWrite(@TAB & "Run EHWA class" & @LF)
ControlFocus("Run", "&Run", "Button55")
ControlSend("Run", "&Run", "Button55", "{ENTER}")
WinWaitClose("Run", "", 60)

; 16
WinWaitActiveImpl("Java - EHWA.java - Eclipse SDK","",30)
LogWrite(@TAB & "Check Run result" & @LF)
$iter = 0
While StringInStr(WinGetText("Java - EHWA.java - Eclipse SDK"), "<terminated>") = 0
	$iter = $iter+1
	Sleep(500)
	If $iter = 360 Then
		LogWrite(@TAB & "ERROR! EHWA run didn't stop diring 3 min. STOP." & @LF)
		PrintErrorScreen()
		ExitImpl(1)
	EndIf
WEnd
SleepImpl(3000)
If Not WinExists("Java - EHWA.java - Eclipse SDK") Then
	LogWrite(@TAB & "ERROR! Window 'Java - EHWA.java - Eclipse SDK' doesn't exists. STOP." & @LF)
	PrintErrorScreen()
	ExitImpl(1)
EndIf

; Exit
ExitImpl(0)

; Auxiliary functions

Func SleepImpl($sleep_time)
    Sleep($sleep_time*$delay_factor)
EndFunc

Func LogWrite($string)
	Dim Const $logfile = $resultsDir & "\EHWA.log"
	Local Const $log = FileOpen($logfile, 1)
	FileWrite($logfile, $string)
	FileClose($log)
EndFunc

Func WinWaitImpl($wname, $wtext, $wdelay)
	Dim $newdelay = 120
	Dim $wreturn = WinWait($wname, $wtext, $wdelay)
	If $wreturn=0 Then
		LogWrite(@TAB & "WARNING! Window '" & $wname & "' didn't appear during " & $wdelay & "c. Add extra delay for " & $newdelay & "c." & @LF)
		$wreturn = WinWait($wname, $wtext, $newdelay)
		If $wreturn=0 Then
			LogWrite(@TAB & "ERROR! Window '" & $wname & "' didn't appear during additional " & $newdelay & "c. STOP." & @LF)
			PrintErrorScreen()
			ExitImpl(1)
		Else
			LogWrite(@TAB & "Window '" & $wname & "' appeared in additional time!" & @LF)
		EndIf
	EndIf
EndFunc

Func WinWaitActiveImpl($wname, $wtext, $wdelay)
	Dim $newdelay =  120
	Dim $wreturn = WinWaitActive($wname, $wtext, $wdelay)
	If $wreturn=0 Then
		LogWrite(@TAB & "WARNING! Window '" & $wname & "' didn't become active during " & $wdelay & "c. Add extra delay for " & $newdelay & "c." & @LF)
		$wreturn = WinWaitActive($wname, $wtext, $newdelay)
		If $wreturn=0 Then
			LogWrite(@TAB & "ERROR! Window '" & $wname & "' didn't become active during additional " & $newdelay & "c. STOP." & @LF)
			PrintErrorScreen()
			ExitImpl(1)
		Else
			LogWrite(@TAB & "Window '" & $wname & "' became active in additional time!" & @LF)
		EndIf
	EndIf
EndFunc

Func WinWaitCloseImpl($wname, $wtext, $wdelay)
	Dim $newdelay = 120
	LogWrite(@TAB & "Closing window '" & $wname & "'" & @LF)
	Dim $wreturn = WinWaitClose($wname, $wtext, $wdelay)
	If $wreturn=0 Then
		LogWrite(@TAB & "WARNING! Window '" & $wname & "' didn't close during " & $wdelay & "c. Add extra delay for " & $newdelay & "c." & @LF)
		$wreturn = WinWaitClose($wname, $wtext, $newdelay)
		If $wreturn=0 Then
			LogWrite(@TAB & "ERROR! Window '" & $wname & "' didn't close during additional " & $newdelay & "c. STOP." & @LF)
			PrintErrorScreen()
			ExitImpl(1)
		Else
			LogWrite(@TAB & "Window '" & $wname & "' closed in additional time!" & @LF)
		EndIf
	EndIf
EndFunc

Func ExitImpl($error_code)
	Opt("WinTitleMatchMode",2)
	LogWrite(@LF & "Exit Eclipse: " & @LF)
	if WinExists("Eclipse SDK") Then
		If Not WinActive("Eclipse SDK","") Then WinActivate("Eclipse SDK","")
		$wr = WinWaitActive("Eclipse SDK", "", 30)
		If $wr=1 Then
			$title = WinGetTitle("Eclipse SDK")
			$exit = WinMenuSelectItem($title, "", "&File", "E&xit" )
			if $exit=0 Then
				LogWrite(@TAB & "Menu """ & $title & "->File->Exit"" wasn't found" & @LF)
				Send("!f")
				Send("x")
			EndIf
			WinWaitClose("Progress Information", "", 120)
			$wr = WinWaitClose("Eclipse SDK", "", 60)
			If $wr=1 Then
				LogWrite(@TAB & "Eclipse was found and closed." & @LF)
			Else
				KillEclipse()
			EndIf
		Else
			KillEclipse()
		EndIf
	Else
		LogWrite(@TAB & "Eclipse wasn't found" & @LF)
		$error_code=1
	EndIf
	Sleep(5000)
	If $error_code=0 Then
		LogWrite(@LF & "EHWA PASSED!" & @LF)
	Else
		LogWrite(@LF & "EHWA FAILED!" & @LF)
	EndIf
	FileCopy(@WorkingDir & "/workspace/.metadata/.log", $resultsDir  & "/workspace.log", 1)
	Exit($error_code)
EndFunc

Func KillEclipse()
	LogWrite(@TAB & "Can't exit Eclipse! Trying to kill ..." & @LF)
	WinKill("Eclipse SDK")
	If Not WinExists("Eclipse SDK") Then
		LogWrite(@TAB & "Eclipse window was found and killed" & @LF)
	Else
		$pid = WinGetProcess("Eclipse SDK")
		If ProcessExists($pid) Then
			ProcessClose($pid)
		EndIf
		Sleep(3000)
		If ProcessExists($pid) Then
			LogWrite(@TAB & "Can't kill Eclipse!" & @LF)
		Else
			LogWrite(@TAB & "Eclipse was killed!" & @LF)
		EndIf
	EndIf
EndFunc

Func PrintErrorScreen()
	Opt("WinTitleMatchMode", 2)
	LogWrite(@LF & "An ERROR has ocurred!" & @LF)
	If Not WinExists("Eclipse SDK") Then
		ExitImpl(1)
	EndIf
	LogWrite(@LF & "Print error screen:" & @LF)
 	$file = FileOpen($resultsDir & "\error.bmp", 2)
	FileClose($file)
	Send("{PRINTSCREEN}")
	LogWrite(@TAB & "Open mspaint to save errror screen" & @LF)
	Run("mspaint " & $resultsDir & "\error.bmp")
	LogWrite(@TAB & "Wait for 'error - Paint' window" & @LF)
	$result = WinWait(" - Paint", "", 180)
	If $result=0 Then
			LogWrite(@TAB & "ERROR! Paint window didn't appear during 3 min. Can't save error screen." & @LF)
			ExitImpl(1)
	EndIf
	If Not WinActive(" - Paint") Then WinActivate("- Paint")
    WinWaitActive(" - Paint", "", 180)
	$result = WinMenuSelectItem(" - Paint", "", "&Edit", "&Paste")
	If $result = 0 Then
		LogWrite(@TAB & "Mspaint menu 'Edit -> Paste' wasn't found" & @LF)
		LogWrite(@TAB & "Can't save error picture!" & @LF)
		ExitImpl(1)
	EndIf
	LogWrite(@TAB & "Save error screen" & @LF)
	$result = WinMenuSelectItem(" - Paint", "", "&File", "&Save")
	if $result = 0 Then
		LogWrite(@TAB & "Mspaint menu 'File -> Save' wasn't found" & @LF)
		LogWrite(@TAB & "Can't save error picture!" & @LF)
		ExitImpl(1)
	EndIf
	LogWrite(@TAB & "Exit mspaint" & @LF)
	$result = WinMenuSelectItem(" - Paint", "", "&File", "E&xit")
	if $result = 0 Then
		LogWrite(@TAB & "Mspaint menu 'File -> Exit' wasn't found" & @LF)
		LogWrite(@TAB & "Can't save error picture!" & @LF)
		Exitimpl(1)
	EndIf
	WinWaitClose(" - Paint", "", 60)
	If WinExists("Eclipse SDK") Then
		LogWrite(@TAB & "Switch to Eclipse" & @LF)
		If Not WinActive("Eclipse SDK") Then WinActivate("Eclipse SDK")
		WinWaitActive("Eclipse SDK", "", 120)
	EndIf
	Opt("WinTitleMatchMode", 4)
EndFunc

;

;     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.

; Set variables
Select
	Case $CmdLine[0] = 4 OR $CmdLine[0] = 5
		Dim Const $eclipse_home = $CmdLine[1]
		Dim Const $tested_jre = $CmdLine[2]
		Dim Const $resultsDir = $CmdLine[3]
		Dim Const $delay_factor = Number($CmdLine[4])
		Dim $jre_options = ""
		If $CmdLine[0] = 5 Then
			$jre_options  = $CmdLine[5]
		EndIf
		If $delay_factor < 1 Then
            ConsoleWrite(@LF & "ERROR: Delay factor < 1! " & @LF & "EHWA FAILED!" & @LF)
            Exit(1)
        EndIf
	Case Else
		ConsoleWrite("ERROR! Wrong number of input parameters! STOP." & @LF & "EHWA FAILED!" & @LF)
		Sleep(3000)
		Exit(1)
EndSelect

; Set options
Opt("WinWaitDelay", 1000) ; wait for 1c
Opt("SendKeyDelay", 200*$delay_factor) ; wait for 150mc
Opt("WinTitleMatchMode", 4)
Opt("WinDetectHiddenText", 1)
Opt("TrayIconDebug", 1)

LogWrite(@LF & "--- Automated Eclipse EHWA scenario for Eclipse 3.2.1 ---" & @LF & @LF)

; Check if some Eclipse window is already olpened
Opt("WinTitleMatchMode", 2)
If WinExists("Eclipse SDK") Then
	LogWrite("ERROR! Some Eclipse window is already opened. STOP." & @LF & "EHWA FAILED!" & @LF)
	Sleep(3000)
	Exit(1)
EndIf

LogWrite("Eclipse Home = " & $eclipse_home & @LF)
LogWrite("Tested JRE = " &  $tested_jre & @LF)
LogWrite("Tested JRE options for Eclipse launching = " & $jre_options & @LF)
LogWrite("Delay factor = " & $delay_factor & @LF)

; Start
LogWrite(@LF & "Start:" & @LF)
LogWrite(@TAB & "Run Eclipse" & @LF)
$run =  $tested_jre & "\bin\java.exe "  & $jre_options & " -cp """ & $eclipse_home & "\startup.jar"" -Dosgi.install.area=""" & $eclipse_home & """ org.eclipse.core.launcher.Main -debug > " & $resultsDir & "\eclipse.log 2>&1"
Run(@ComSpec & " /c " & $run, "", @SW_HIDE)

; 1
LogWrite(@TAB & "Wait Eclipse 'Workspace Launcher' window to appear " & @LF)
WinWaitImpl("Workspace Launcher", "", 180)
If Not WinActive("Workspace Launcher") Then WinActivate("Workspace Launcher")
WinWaitActiveImpl("Workspace Launcher", "", 30)
LogWrite(@TAB & "window appeared " & @LF)

; 2
LogWrite(@TAB & "Select " & @WorkingDir & "\workspace as workspace" & @LF)
ControlFocus("Workspace Launcher", "", "Edit1")
ControlSetText("Workspace Launcher", "", "Edit1", @WorkingDir & "\workspace" )
LogWrite(@TAB & "Press OK button" & @LF)
ControlSend("Workspace Launcher", "", "", "{ENTER}")
WinWaitCloseImpl("Workspace Launcher", "", 60)

; 3
LogWrite(@TAB & "Wait 'Java - Eclipse SDK' window to appear " & @LF)
WinWaitImpl("Java - Eclipse SDK", "", 180)
If Not WinActive("Java - Eclipse SDK") Then WinActivate("Java - Eclipse SDK")
WinWaitActiveImpl("Java - Eclipse SDK", "", 30)
LogWrite(@TAB & "window appeared " & @LF)
SleepImpl(1000)
LogWrite(@TAB & "Close Welcome page " & @LF)
;Send("!wvp{ENTER}")
Send("!+{NUMPADSUB}c")
SleepImpl(3000)

; 4
LogWrite(@TAB & "Create new  Java project" & @LF)
Send("!fnr")

; 5
LogWrite(@TAB & "Wait 'New Project' window to appear" & @LF)
WinWaitImpl("New Project","",60)
If Not WinActive("New Project","") Then WinActivate("New Project","")
WinWaitActiveImpl("New Project","",30)
LogWrite(@TAB & "window appeared " & @LF)
LogWrite(@TAB & "Select Java project" & @LF)
ControlFocus("New Project", "", "Edit1")
ControlSend("New Project", "", "Edit1", "{ENTER}")
LogWrite(@TAB & "Press Next button" & @LF)
ControlFocus("New Project", "&Next >", "Button2",)
ControlClick("New Project", "&Next >", "Button2")
If @error=1 Then
	LogWrite(@TAB & "ERROR! Next button wasn't found. STOP." & @LF)
	PrintErrorScreen()
	ExitImpl(1)
EndIf
WinWaitCloseImpl("New Project", "", 60)

; 6
LogWrite(@TAB & "Wait 'New Java Project' window to appear " & @LF)
WinWaitImpl("New Java Project","",60)
If Not WinActive("New Java Project") Then WinActivate("New Java Project")
WinWaitActiveImpl("New Java Project","",30)
LogWrite(@TAB & "window appeared, " & @LF)
SleepImpl(1000)
LogWrite(@TAB & "Type EHWA as a project name" & @LF)
ControlFocus("New Java Project", "", "Edit2")
ControlSetText("New Java Project", "", "Edit2", "EHWA" )
LogWrite(@TAB & "Press Finish button" & @LF)
ControlFocus("New Java Project", "&Finish", "Button27",)
ControlClick("New Java Project", "&Finish", "Button27")
If @error=1 Then
	LogWrite(@TAB & "ERROR! Finish button wasn't found. STOP." & @LF)
	PrintErrorScreen()
	ExitImpl(1)
EndIf
LogWrite(@TAB & "Wait while project is being created ... " & @LF)
WinWaitClose("New Java Project", "", 60)
LogWrite(@TAB & "Project was created" & @LF)

; 7
WinWaitImpl("Java - Eclipse SDK", "", 60)
If Not WinActive("Java - Eclipse SDK", "") Then WinActivate("Java - Eclipse SDK","")
WinWaitActiveImpl("Java - Eclipse SDK", "", 30)
LogWrite(@TAB & "Create new Java class" & @LF)
$result=WinMenuSelectItem("Java - Eclipse SDK", "", "&File", "&New", "Class")
if $result=0 Then
	LogWrite(@TAB & "ERROR! Menu 'File -> New -> Class' wasn't found. STOP." & @LF)
	PrintErrorScreen()
	ExitImpl(1)
EndIf
LogWrite(@TAB & "Wait 'New Java Class' window to appear" & @LF)
WinWaitImpl("New Java Class","",60)
If Not WinActive("New Java Class") Then WinActivate("New Java Class")
WinWaitActiveImpl("New Java Class","",30)
LogWrite(@TAB & "window appeared " & @LF)
SleepImpl(1000)

; 8
LogWrite(@TAB & "Type EHWA as class name" & @LF)
ControlFocus("New Java Class", "", "Edit4")
ControlSetText("New Java Class", "", "Edit4", "EHWA" )

;9
LogWrite(@TAB & "Select 'public static void main(String[] args)' checkbox" & @LF)
ControlFocus("New Java Class", "", "Button15")
ControlCommand("New Java Class", "", "Button15", "Check", "")

; 10
LogWrite(@TAB & "Click Finish button" & @LF)
ControlFocus("New Java Class", "&Finish", "Button19")
ControlClick("New Java Class", "&Finish", "Button19")
If @error=1 Then
	LogWrite(@TAB & "ERROR! Finish button wasn't found. STOP." & @LF)
	PrintErrorScreen()
	ExitImpl(1)
EndIf
LogWrite(@TAB & "Wait while EHWA class is being created ..." & @LF)
WinWaitClose("New Java Class", "", 60)

; 11
WinWaitImpl("Java - EHWA.java - Eclipse SDK","",60)
If Not WinActive("New Java Class") Then WinActivate("Java - EHWA.java - Eclipse SDK")
WinWaitActiveImpl("Java - EHWA.java - Eclipse SDK","",30)
LogWrite(@TAB & "EHWA class was successfully created" & @LF)
SleepImpl(3000)
LogWrite(@TAB & "Add System.out.println(""Hello, world!""); string in the main method of EHWA class" & @LF)
ControlFocus("Java - EHWA.java - Eclipse SDK", "", "SWT_Window039")
ControlSend("Java - EHWA.java - Eclipse SDK", "", "SWT_Window039", "{DOWN 8}{TAB}")
ControlSend("Java - EHWA.java - Eclipse SDK", "", "SWT_Window039", "System.out.println(""Hello, world{!}"");")
SleepImpl(3000)

; 12
WinWaitImpl("Java - EHWA.java - Eclipse SDK","",60)
If Not WinActive("Java - EHWA.java - Eclipse SDK","") Then WinActivate("Java - EHWA.java - Eclipse SDK","")
WinWaitActiveImpl("Java - EHWA.java - Eclipse SDK","",30)
LogWrite(@TAB & "Save EHWA class " & @LF)
$result=WinMenuSelectItem("Java - EHWA.java - Eclipse SDK", "", "&File", "&Save")
if $result=0 Then
	LogWrite(@TAB & "ERROR! Menu 'File->Save' wasn't found. Stop." & @LF)
	PrintErrorScreen()
	ExitImpl(1)
EndIf
LogWrite(@TAB & "Open console" & @LF)
Send("!w")
Send("v")
Send("c")
SleepImpl(3000)

; 13
WinWaitImpl("Java - EHWA.java - Eclipse SDK","",60)
If Not WinActive("Java - EHWA.java - Eclipse SDK","") Then WinActivate("Java - EHWA.java - Eclipse SDK","")
WinWaitActiveImpl("Java - EHWA.java - Eclipse SDK","",30)
LogWrite(@TAB & "Run EHWA class" & @LF)
Send("!r")
Send("n")
LogWrite(@TAB & "Wait 'Run' window to appear " & @LF)
WinWaitImpl("Run","",60)
If Not WinActive("Run") Then WinActivate("Run")
WinWaitActiveImpl("Run","",30)
LogWrite(@TAB & "window appeared " & @LF)

; 14
LogWrite(@TAB & "Select Java application " & @LF)
ControlFocus("Run", "", "Edit1")
ControlSend("Run", "", "Edit1", "{ENTER}")
ControlFocus("Run", "", "SysTreeView321")
ControlSend("Run", "", "SysTreeView321", "{j 2}")
LogWrite(@TAB & "Create new run configuration" & @LF)
ControlSend("Run", "", "SysTreeView321", "{ENTER}")

; 15
WinWaitImpl("Run","",60)
If Not WinActive("Run","") Then WinActivate("Run","")
WinWaitActiveImpl("Run","",30)
LogWrite(@TAB & "Wait while EHWA run configuration is being created ..." & @LF)
$iter = 0
While StringInStr(WinGetText("Run"), "EHWA") = 0
	$iter = $iter+1
	Sleep(500)
	If $iter = 360 Then
		LogWrite(@TAB & "ERROR! Can't create EHWA run configuration diring 3 min. STOP." & @LF)
		PrintErrorScreen()
		ExitImpl(1)
	EndIf
WEnd
LogWrite(@TAB & "Run EHWA class" & @LF)
ControlFocus("Run", "&Run", "Button55")
ControlSend("Run", "&Run", "Button55", "{ENTER}")
WinWaitClose("Run", "", 60)

; 16
WinWaitActiveImpl("Java - EHWA.java - Eclipse SDK","",30)
LogWrite(@TAB & "Check Run result" & @LF)
$iter = 0
While StringInStr(WinGetText("Java - EHWA.java - Eclipse SDK"), "<terminated>") = 0
	$iter = $iter+1
	Sleep(500)
	If $iter = 360 Then
		LogWrite(@TAB & "ERROR! EHWA run didn't stop diring 3 min. STOP." & @LF)
		PrintErrorScreen()
		ExitImpl(1)
	EndIf
WEnd
SleepImpl(3000)
If Not WinExists("Java - EHWA.java - Eclipse SDK") Then
	LogWrite(@TAB & "ERROR! Window 'Java - EHWA.java - Eclipse SDK' doesn't exists. STOP." & @LF)
	PrintErrorScreen()
	ExitImpl(1)
EndIf

; Exit
ExitImpl(0)

; Auxiliary functions

Func SleepImpl($sleep_time)
    Sleep($sleep_time*$delay_factor)
EndFunc

Func LogWrite($string)
	Dim Const $logfile = $resultsDir & "\EHWA.log"
	Local Const $log = FileOpen($logfile, 1)
	FileWrite($logfile, $string)
	FileClose($log)
EndFunc

Func WinWaitImpl($wname, $wtext, $wdelay)
	Dim $newdelay = 120
	Dim $wreturn = WinWait($wname, $wtext, $wdelay)
	If $wreturn=0 Then
		LogWrite(@TAB & "WARNING! Window '" & $wname & "' didn't appear during " & $wdelay & "c. Add extra delay for " & $newdelay & "c." & @LF)
		$wreturn = WinWait($wname, $wtext, $newdelay)
		If $wreturn=0 Then
			LogWrite(@TAB & "ERROR! Window '" & $wname & "' didn't appear during additional " & $newdelay & "c. STOP." & @LF)
			PrintErrorScreen()
			ExitImpl(1)
		Else
			LogWrite(@TAB & "Window '" & $wname & "' appeared in additional time!" & @LF)
		EndIf
	EndIf
EndFunc

Func WinWaitActiveImpl($wname, $wtext, $wdelay)
	Dim $newdelay =  120
	Dim $wreturn = WinWaitActive($wname, $wtext, $wdelay)
	If $wreturn=0 Then
		LogWrite(@TAB & "WARNING! Window '" & $wname & "' didn't become active during " & $wdelay & "c. Add extra delay for " & $newdelay & "c." & @LF)
		$wreturn = WinWaitActive($wname, $wtext, $newdelay)
		If $wreturn=0 Then
			LogWrite(@TAB & "ERROR! Window '" & $wname & "' didn't become active during additional " & $newdelay & "c. STOP." & @LF)
			PrintErrorScreen()
			ExitImpl(1)
		Else
			LogWrite(@TAB & "Window '" & $wname & "' became active in additional time!" & @LF)
		EndIf
	EndIf
EndFunc

Func WinWaitCloseImpl($wname, $wtext, $wdelay)
	Dim $newdelay = 120
	LogWrite(@TAB & "Closing window '" & $wname & "'" & @LF)
	Dim $wreturn = WinWaitClose($wname, $wtext, $wdelay)
	If $wreturn=0 Then
		LogWrite(@TAB & "WARNING! Window '" & $wname & "' didn't close during " & $wdelay & "c. Add extra delay for " & $newdelay & "c." & @LF)
		$wreturn = WinWaitClose($wname, $wtext, $newdelay)
		If $wreturn=0 Then
			LogWrite(@TAB & "ERROR! Window '" & $wname & "' didn't close during additional " & $newdelay & "c. STOP." & @LF)
			PrintErrorScreen()
			ExitImpl(1)
		Else
			LogWrite(@TAB & "Window '" & $wname & "' closed in additional time!" & @LF)
		EndIf
	EndIf
EndFunc

Func ExitImpl($error_code)
	Opt("WinTitleMatchMode",2)
	LogWrite(@LF & "Exit Eclipse: " & @LF)
	if WinExists("Eclipse SDK") Then
		If Not WinActive("Eclipse SDK","") Then WinActivate("Eclipse SDK","")
		$wr = WinWaitActive("Eclipse SDK", "", 30)
		If $wr=1 Then
			$title = WinGetTitle("Eclipse SDK")
			$exit = WinMenuSelectItem($title, "", "&File", "E&xit" )
			if $exit=0 Then
				LogWrite(@TAB & "Menu """ & $title & "->File->Exit"" wasn't found" & @LF)
				Send("!f")
				Send("x")
			EndIf
			WinWaitClose("Progress Information", "", 120)
			$wr = WinWaitClose("Eclipse SDK", "", 60)
			If $wr=1 Then
				LogWrite(@TAB & "Eclipse was found and closed." & @LF)
			Else
				KillEclipse()
			EndIf
		Else
			KillEclipse()
		EndIf
	Else
		LogWrite(@TAB & "Eclipse wasn't found" & @LF)
		$error_code=1
	EndIf
	Sleep(5000)
	If $error_code=0 Then
		LogWrite(@LF & "EHWA PASSED!" & @LF)
	Else
		LogWrite(@LF & "EHWA FAILED!" & @LF)
	EndIf
	FileCopy(@WorkingDir & "/workspace/.metadata/.log", $resultsDir  & "/workspace.log", 1)
	Exit($error_code)
EndFunc

Func KillEclipse()
	LogWrite(@TAB & "Can't exit Eclipse! Trying to kill ..." & @LF)
	WinKill("Eclipse SDK")
	If Not WinExists("Eclipse SDK") Then
		LogWrite(@TAB & "Eclipse window was found and killed" & @LF)
	Else
		$pid = WinGetProcess("Eclipse SDK")
		If ProcessExists($pid) Then
			ProcessClose($pid)
		EndIf
		Sleep(3000)
		If ProcessExists($pid) Then
			LogWrite(@TAB & "Can't kill Eclipse!" & @LF)
		Else
			LogWrite(@TAB & "Eclipse was killed!" & @LF)
		EndIf
	EndIf
EndFunc

Func PrintErrorScreen()
	Opt("WinTitleMatchMode", 2)
	LogWrite(@LF & "An ERROR has ocurred!" & @LF)
	If Not WinExists("Eclipse SDK") Then
		ExitImpl(1)
	EndIf
	LogWrite(@LF & "Print error screen:" & @LF)
 	$file = FileOpen($resultsDir & "\error.bmp", 2)
	FileClose($file)
	Send("{PRINTSCREEN}")
	LogWrite(@TAB & "Open mspaint to save errror screen" & @LF)
	Run("mspaint " & $resultsDir & "\error.bmp")
	LogWrite(@TAB & "Wait for 'error - Paint' window" & @LF)
	$result = WinWait(" - Paint", "", 180)
	If $result=0 Then
			LogWrite(@TAB & "ERROR! Paint window didn't appear during 3 min. Can't save error screen." & @LF)
			ExitImpl(1)
	EndIf
	If Not WinActive(" - Paint") Then WinActivate("- Paint")
    WinWaitActive(" - Paint", "", 180)
	$result = WinMenuSelectItem(" - Paint", "", "&Edit", "&Paste")
	If $result = 0 Then
		LogWrite(@TAB & "Mspaint menu 'Edit -> Paste' wasn't found" & @LF)
		LogWrite(@TAB & "Can't save error picture!" & @LF)
		ExitImpl(1)
	EndIf
	LogWrite(@TAB & "Save error screen" & @LF)
	$result = WinMenuSelectItem(" - Paint", "", "&File", "&Save")
	if $result = 0 Then
		LogWrite(@TAB & "Mspaint menu 'File -> Save' wasn't found" & @LF)
		LogWrite(@TAB & "Can't save error picture!" & @LF)
		ExitImpl(1)
	EndIf
	LogWrite(@TAB & "Exit mspaint" & @LF)
	$result = WinMenuSelectItem(" - Paint", "", "&File", "E&xit")
	if $result = 0 Then
		LogWrite(@TAB & "Mspaint menu 'File -> Exit' wasn't found" & @LF)
		LogWrite(@TAB & "Can't save error picture!" & @LF)
		Exitimpl(1)
	EndIf
	WinWaitClose(" - Paint", "", 60)
	If WinExists("Eclipse SDK") Then
		LogWrite(@TAB & "Switch to Eclipse" & @LF)
		If Not WinActive("Eclipse SDK") Then WinActivate("Eclipse SDK")
		WinWaitActive("Eclipse SDK", "", 120)
	EndIf
	Opt("WinTitleMatchMode", 4)
EndFunc

;