(* Functional tests for Safe Terminal Instructions 1. Open in Script Editor 2. Click the green "Run" button, or press Command + R Copyright © 2006 Nir Soffer License: GNU GPL, see COPYING *) -- Loader standard header property parent : load script file ((path to scripts folder from user domain as string) & "ASUnit.scpt") property suite : makeTestSuite("Safe Terminal GUI Tests") -- Test properties property testFolder : folder of file (document 1's path as POSIX file) of application "Finder" as alias property payload : "payload" property delaySeconds : 0.5 -- Require Mac OS X 10.3 or later if osVersion() < "1030" then display dialog "This script requires Mac OS X 10.3 or later." buttons  {"Cancel"} default button 1 with icon 2 return end if -- Require UI sctipting enabled tell application "System Events" if not UI elements enabled then my helpOnActivatingUIScripting() return end if end tell script |starting terminal| property parent : makeFixture(me) on setUp() tell application "Terminal" to quit tell me to activate end setUp on tearDown() tell application "Terminal" to quit end tearDown script |open new shell| property parent : makeTestCase(me) tell application "Terminal" to activate delay delaySeconds should((count of windows) of application "Terminal" ³ 1,  "no shell windows found.") end script end script script |open script launching Terminal| property parent : makeFixture(me) on setUp() tell application "Terminal" to quit mainScript()'s deletePayload() end setUp on tearDown() tell application "Terminal" to quit mainScript()'s deletePayload() end tearDown script |show alert window| property parent : makeTestCase(me) mainScript()'s openScript() should(mainScript()'s detectAlertWindow(), "no alert window") mainScript()'s clickCancel() end script script |cancel script| property parent : makeTestCase(me) mainScript()'s openScript() mainScript()'s clickCancel() shouldnt(mainScript()'s payloadExists(), "script did execute") end script script |cancel script quit terminal| property parent : makeTestCase(me) mainScript()'s openScript() mainScript()'s clickCancel() shouldnt(mainScript()'s terminalIsRunning(), "Terminal is running") end script script |execute script| property parent : makeTestCase(me) mainScript()'s openScript() mainScript()'s clickExecute() should(mainScript()'s payloadExists(), "script did not execute") end script script |execute script does not open new shell| property parent : makeTestCase(me) mainScript()'s openScript() mainScript()'s clickExecute() should((count of windows) of application "Terminal" = 1, "more than one window opened") end script end script script |open script while Terminal running| property parent : makeFixture(me) on setUp() tell application "Terminal" to run mainScript()'s deletePayload() end setUp on tearDown() tell application "Terminal" to quit mainScript()'s deletePayload() end tearDown script |show alert window| property parent : makeTestCase(me) mainScript()'s openScript() should(mainScript()'s detectAlertWindow(), "no alert window") mainScript()'s clickCancel() end script script |cancel script| property parent : makeTestCase(me) mainScript()'s openScript() mainScript()'s clickCancel() shouldnt(mainScript()'s payloadExists(), "script did execute") end script script |cancel script does not quit terminal| property parent : makeTestCase(me) mainScript()'s openScript() mainScript()'s clickCancel() should(mainScript()'s terminalIsRunning(), "terminal is not running") end script script |execute script| property parent : makeTestCase(me) mainScript()'s openScript() mainScript()'s clickExecute() should(mainScript()'s payloadExists(), "script did not execute") end script end script -- Test helpers on openScript() tell application "Finder" to open file "script.command" of folder testFolder delay delaySeconds end openScript on deletePayload() tell application "Finder" try delete file payload of testFolder on error number -1728 -- ignore no such file error end try end tell end deletePayload on payloadExists() tell application "Finder" return exists file payload of testFolder end tell end payloadExists on terminalIsRunning() tell application "Finder" return exists application process "Terminal" end tell end terminalIsRunning on detectAlertWindow() (* The buttons, text and size are affected by localization, so the check is rather fuzzy, looking for dialog window with 2 buttons. *) tell application "System Events" set alertWindow to window 1 of process "Terminal" set windowProperties to properties of alertWindow return windowProperties's subrole is "AXDialog" and  windowProperties's title is "" and  (alertWindow's buttons count) is 2 end tell end detectAlertWindow on clickCancel() if not detectAlertWindow() then error "skip test: no alert window" tell application "System Events" tell process "Terminal" to click button 1 of window 1 end tell delay delaySeconds end clickCancel on clickExecute() if not detectAlertWindow() then error "skip test: no alert window" tell application "System Events" tell process "Terminal" to click button 2 of window 1 end tell delay delaySeconds end clickExecute -- Requirements checking on osVersion() -- get the system version, based on example code from Apple -- return a string like "1030" (for 10.3.0) set the hexData to system attribute "sysv" set hexString to {} repeat 4 times set hexString to ((hexData mod 16) as string) & hexString set hexData to hexData div 16 end repeat return hexString as string end osVersion on helpOnActivatingUIScripting() tell application "System Preferences" activate set current pane to pane "com.apple.preference.universalaccess" display dialog "This script utilizes Graphic User Interface Scripting which is currently disabled." & return & return & "Please select the checkbox \"Enable access for assistive devices\" in the Universal Access preference pane." with icon 2 buttons {"Cancel"} default button 1 end tell end helpOnActivatingUIScripting run makeTextTestRunner(suite)