Module: Repla::Test::Helper

Defined in:
lib/repla/test/lib/helper.rb

Overview

Test helper

Constant Summary collapse

APPLESCRIPT_DIRECTORY =
File.join(File.dirname(__FILE__), '..',
'applescript')
CONFIRMDIALOGAPPLESCRIPT_FILE =
File.join(APPLESCRIPT_DIRECTORY,
'confirm_dialog.scpt')
WINDOWIDAPPLESCRIPT_FILE =
File.join(APPLESCRIPT_DIRECTORY,
'window_id.scpt')
CANCELDIALOGAPPLESCRIPT_FILE =
File.join(APPLESCRIPT_DIRECTORY,
'cancel_dialog.scpt')
QUITAPPLESCRIPT_FILE =
File.join(APPLESCRIPT_DIRECTORY,
'quit.scpt')
ISRUNNINGAPPLESCRIPT_FILE =
File.join(APPLESCRIPT_DIRECTORY,
'is_running.scpt')
SWITCHWINDOWSAPPLESCRIPT_FILE =
File.join(APPLESCRIPT_DIRECTORY,
'switch_windows.scpt')
WINDOWBOUNDSAPPLESCRIPT_FILE =
File.join(APPLESCRIPT_DIRECTORY,
'window_bounds.scpt')
SETWINDOWBOUNDSAPPLESCRIPT_FILE =
File.join(APPLESCRIPT_DIRECTORY,
'set_window_bounds'\
'.scpt')

Class Method Summary collapse

Class Method Details

.app_running?Boolean

Returns:

  • (Boolean)


48
49
50
51
# File 'lib/repla/test/lib/helper.rb', line 48

def self.app_running?
  result = run_applescript(ISRUNNINGAPPLESCRIPT_FILE)
  result == 'true'
end

.cancel_dialogObject



35
36
37
38
# File 'lib/repla/test/lib/helper.rb', line 35

def self.cancel_dialog
  run_applescript(CANCELDIALOGAPPLESCRIPT_FILE)
  sleep TEST_PAUSE_TIME # Give dialog time
end

.confirm_dialogObject



22
23
24
25
# File 'lib/repla/test/lib/helper.rb', line 22

def self.confirm_dialog
  run_applescript(CONFIRMDIALOGAPPLESCRIPT_FILE)
  sleep TEST_PAUSE_TIME # Give dialog time
end

.quitObject



42
43
44
# File 'lib/repla/test/lib/helper.rb', line 42

def self.quit
  run_applescript(QUITAPPLESCRIPT_FILE)
end

.run_applescript(script, arguments = nil) ⇒ Object

TODO: ‘self.run_applescript` should be private but now all of a sudden instances method can’t call private class methods?



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/repla/test/lib/helper.rb', line 76

def self.run_applescript(script, arguments = nil)
  command = "osascript #{Shellwords.escape(script)}"
  if arguments
    arguments.each do |argument|
      if argument
        argument = argument.to_s
        command = command + ' ' + Shellwords.escape(argument)
      end
    end
  end

  result = `#{command}`

  result.chomp!

  return nil if result.empty?

  return result.to_i if result.integer?

  return result.to_f if result.float?

  result
end

.run_javascript(javascript) ⇒ Object



13
14
15
16
17
18
# File 'lib/repla/test/lib/helper.rb', line 13

def self.run_javascript(javascript)
  # `osascript` requires redirecting stderr to stdout for some reason
  `osascript -l JavaScript -e \
  #{Shellwords.escape(javascript)} 2>&1`.chomp!
  # return `node -e #{Shellwords.escape(javascript)}`
end

.set_window_bounds(bounds, window_id = nil) ⇒ Object



68
69
70
71
72
# File 'lib/repla/test/lib/helper.rb', line 68

def self.set_window_bounds(bounds, window_id = nil)
  arguments = [bounds]
  arguments = arguments.push(window_id) if window_id
  run_applescript(SETWINDOWBOUNDSAPPLESCRIPT_FILE, arguments)
end

.switch_windowsObject



55
56
57
# File 'lib/repla/test/lib/helper.rb', line 55

def self.switch_windows
  run_applescript(SWITCHWINDOWSAPPLESCRIPT_FILE)
end

.window_bounds(window_id = nil) ⇒ Object



61
62
63
# File 'lib/repla/test/lib/helper.rb', line 61

def self.window_bounds(window_id = nil)
  run_applescript(WINDOWBOUNDSAPPLESCRIPT_FILE, [window_id])
end

.window_idObject



29
30
31
# File 'lib/repla/test/lib/helper.rb', line 29

def self.window_id
  run_applescript(WINDOWIDAPPLESCRIPT_FILE)
end