~jdong/nsp/trunk

« back to all changes in this revision

Viewing changes to FormGenerator/scriptaculous/src/javascripttest.rb

  • Committer: John Dong
  • Date: 2010-05-12 22:55:28 UTC
  • Revision ID: jdong@ubuntu.com-20100512225528-d7xrtpdnq45m3cq4
Commit form generation backend

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
require 'rake/tasklib'
 
2
require 'thread'
 
3
require 'webrick'
 
4
 
 
5
class Browser
 
6
  def supported?; true; end
 
7
  def setup ; end
 
8
  def open(url) ; end
 
9
  def teardown ; end
 
10
 
 
11
  def host
 
12
    require 'rbconfig'
 
13
    Config::CONFIG['host']
 
14
  end
 
15
  
 
16
  def macos?
 
17
    host.include?('darwin')
 
18
  end
 
19
  
 
20
  def windows?
 
21
    host.include?('mswin')
 
22
  end
 
23
  
 
24
  def linux?
 
25
    host.include?('linux')
 
26
  end
 
27
  
 
28
  def applescript(script)
 
29
    raise "Can't run AppleScript on #{host}" unless macos?
 
30
    system "osascript -e '#{script}' 2>&1 >/dev/null"
 
31
  end
 
32
end
 
33
 
 
34
class FirefoxBrowser < Browser
 
35
  def initialize(path='c:\Program Files\Mozilla Firefox\firefox.exe')
 
36
    @path = path
 
37
  end
 
38
 
 
39
  def visit(url)
 
40
    applescript('tell application "Firefox" to Get URL "' + url + '"') if macos? 
 
41
    system("#{@path} #{url}") if windows? 
 
42
    system("firefox #{url}") if linux?
 
43
  end
 
44
 
 
45
  def to_s
 
46
    "Firefox"
 
47
  end
 
48
end
 
49
 
 
50
class SafariBrowser < Browser
 
51
  def supported?
 
52
    macos?
 
53
  end
 
54
  
 
55
  def setup
 
56
    applescript('tell application "Safari" to make new document')
 
57
  end
 
58
  
 
59
  def visit(url)
 
60
    applescript('tell application "Safari" to set URL of front document to "' + url + '"')
 
61
  end
 
62
 
 
63
  def teardown
 
64
    #applescript('tell application "Safari" to close front document')
 
65
  end
 
66
 
 
67
  def to_s
 
68
    "Safari"
 
69
  end
 
70
end
 
71
 
 
72
class IEBrowser < Browser
 
73
  def initialize(path='C:\Program Files\Internet Explorer\IEXPLORE.EXE')
 
74
    @path = path
 
75
  end
 
76
  
 
77
  def setup
 
78
    if windows?
 
79
      puts %{
 
80
        MAJOR ANNOYANCE on Windows.
 
81
        You have to shut down the Internet Explorer manually after each test
 
82
        for the script to proceed.
 
83
        Any suggestions on fixing this is GREATLY appreaciated!
 
84
        Thank you for your understanding.
 
85
      }
 
86
    end
 
87
  end
 
88
 
 
89
  def supported?
 
90
    windows?
 
91
  end
 
92
  
 
93
  def visit(url)
 
94
    system("#{@path} #{url}") if windows? 
 
95
  end
 
96
 
 
97
  def to_s
 
98
    "Internet Explorer"
 
99
  end
 
100
end
 
101
 
 
102
class KonquerorBrowser < Browser
 
103
  def supported?
 
104
    linux?
 
105
  end
 
106
  
 
107
  def visit(url)
 
108
    system("kfmclient openURL #{url}")
 
109
  end
 
110
  
 
111
  def to_s
 
112
    "Konqueror"
 
113
  end
 
114
end
 
115
 
 
116
# shut up, webrick :-)
 
117
class ::WEBrick::HTTPServer
 
118
  def access_log(config, req, res)
 
119
    # nop
 
120
  end
 
121
end
 
122
class ::WEBrick::BasicLog
 
123
  def log(level, data)
 
124
    # nop
 
125
  end
 
126
end
 
127
 
 
128
class JavaScriptTestTask < ::Rake::TaskLib
 
129
 
 
130
  def initialize(name=:test)
 
131
    @name = name
 
132
    @tests = []
 
133
    @browsers = []
 
134
 
 
135
    @queue = Queue.new
 
136
 
 
137
    result = []
 
138
 
 
139
    @server = WEBrick::HTTPServer.new(:Port => 4711) # TODO: make port configurable
 
140
    @server.mount_proc("/results") do |req, res|
 
141
      @queue.push(req.query['result'])
 
142
      res.body = "OK"
 
143
    end
 
144
    yield self if block_given?
 
145
    define
 
146
  end
 
147
 
 
148
  def define
 
149
    task @name do
 
150
      trap("INT") { @server.shutdown }
 
151
      t = Thread.new { @server.start }
 
152
 
 
153
      # run all combinations of browsers and tests
 
154
      @browsers.each do |browser|
 
155
        if browser.supported?
 
156
          browser.setup
 
157
          @tests.each do |test|
 
158
            browser.visit("http://localhost:4711#{test}?resultsURL=http://localhost:4711/results&t=" + ("%.6f" % Time.now.to_f))
 
159
            result = @queue.pop
 
160
            puts "#{test} on #{browser}: #{result}"
 
161
          end
 
162
          browser.teardown
 
163
        else
 
164
          puts "Skipping #{browser}, not supported on this OS"
 
165
        end
 
166
        browser.teardown
 
167
      end
 
168
 
 
169
      @server.shutdown
 
170
      t.join
 
171
    end
 
172
  end
 
173
 
 
174
  def mount(path, dir=nil)
 
175
    dir = Dir.pwd + path unless dir
 
176
 
 
177
    @server.mount(path, WEBrick::HTTPServlet::FileHandler, dir)
 
178
  end
 
179
 
 
180
  # test should be specified as a url
 
181
  def run(test)
 
182
    @tests<<test
 
183
  end
 
184
 
 
185
  def browser(browser)
 
186
    browser =
 
187
      case(browser)
 
188
        when :firefox
 
189
          FirefoxBrowser.new
 
190
        when :safari
 
191
          SafariBrowser.new
 
192
        when :ie
 
193
          IEBrowser.new
 
194
        when :konqueror
 
195
          KonquerorBrowser.new
 
196
        else
 
197
          browser
 
198
      end
 
199
 
 
200
    @browsers<<browser
 
201
  end
 
202
end
 
203