2006-05-01 05:05:08 -04:00
|
|
|
begin
|
|
|
|
require 'win32ole'
|
2006-05-06 19:40:08 -04:00
|
|
|
rescue LoadError
|
2006-05-01 05:05:08 -04:00
|
|
|
end
|
|
|
|
require 'test/unit'
|
2006-05-06 19:40:08 -04:00
|
|
|
|
2006-05-01 05:05:08 -04:00
|
|
|
if defined?(WIN32OLE_EVENT)
|
|
|
|
class TestWIN32OLE_EVENT < Test::Unit::TestCase
|
2007-02-08 08:00:06 -05:00
|
|
|
def create_temp_html
|
|
|
|
fso = WIN32OLE.new('Scripting.FileSystemObject')
|
|
|
|
dummy_file = fso.GetTempName + ".html"
|
|
|
|
cfolder = fso.getFolder(".")
|
|
|
|
f = cfolder.CreateTextFile(dummy_file)
|
|
|
|
f.writeLine("<html><body>This is test HTML file for Win32OLE.</body></html>")
|
|
|
|
f.close
|
|
|
|
dummy_path = cfolder.path + "\\" + dummy_file
|
|
|
|
dummy_path
|
|
|
|
end
|
|
|
|
|
2006-05-01 05:05:08 -04:00
|
|
|
def setup
|
|
|
|
@ie = WIN32OLE.new("InternetExplorer.Application")
|
|
|
|
@ie.visible = true
|
|
|
|
@event = ""
|
|
|
|
@event2 = ""
|
|
|
|
@event3 = ""
|
2007-02-08 08:00:06 -05:00
|
|
|
@f = create_temp_html
|
2006-05-01 05:05:08 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def default_handler(event, *args)
|
|
|
|
@event += event
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_on_event
|
|
|
|
ev = WIN32OLE_EVENT.new(@ie, 'DWebBrowserEvents')
|
|
|
|
ev.on_event {|*args| default_handler(*args)}
|
2007-02-08 08:00:06 -05:00
|
|
|
@ie.navigate("file:///#{@f}")
|
2006-05-01 05:05:08 -04:00
|
|
|
while @ie.busy
|
2007-02-08 08:00:06 -05:00
|
|
|
sleep 0.1
|
2006-05-01 05:05:08 -04:00
|
|
|
end
|
|
|
|
assert_match(/BeforeNavigate/, @event)
|
|
|
|
assert_match(/NavigateComplete/, @event)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_on_event2
|
|
|
|
ev = WIN32OLE_EVENT.new(@ie, 'DWebBrowserEvents')
|
|
|
|
ev.on_event('BeforeNavigate') {|*args| handler1}
|
|
|
|
ev.on_event('BeforeNavigate') {|*args| handler2}
|
2007-02-08 08:00:06 -05:00
|
|
|
@ie.navigate("file:///#{@f}")
|
2006-05-01 05:05:08 -04:00
|
|
|
while @ie.busy
|
2007-02-08 08:00:06 -05:00
|
|
|
sleep 0.1
|
2006-05-01 05:05:08 -04:00
|
|
|
end
|
|
|
|
assert_equal("handler2", @event2)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_on_event3
|
|
|
|
ev = WIN32OLE_EVENT.new(@ie, 'DWebBrowserEvents')
|
2006-06-25 04:16:44 -04:00
|
|
|
ev.on_event {|*args| handler1}
|
|
|
|
ev.on_event {|*args| handler2}
|
2007-02-08 08:00:06 -05:00
|
|
|
@ie.navigate("file:///#{@f}")
|
2006-05-01 05:05:08 -04:00
|
|
|
while @ie.busy
|
2007-02-08 08:00:06 -05:00
|
|
|
sleep 0.1
|
2006-05-01 05:05:08 -04:00
|
|
|
end
|
|
|
|
assert_equal("handler2", @event2)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_on_event4
|
|
|
|
ev = WIN32OLE_EVENT.new(@ie, 'DWebBrowserEvents')
|
2006-06-25 04:16:44 -04:00
|
|
|
ev.on_event{|*args| handler1}
|
|
|
|
ev.on_event{|*args| handler2}
|
2006-05-01 05:05:08 -04:00
|
|
|
ev.on_event('NavigateComplete'){|*args| handler3(*args)}
|
2007-02-08 08:00:06 -05:00
|
|
|
@ie.navigate("file:///#{@f}")
|
2006-05-01 05:05:08 -04:00
|
|
|
while @ie.busy
|
2007-02-08 08:00:06 -05:00
|
|
|
sleep 0.1
|
2006-05-01 05:05:08 -04:00
|
|
|
end
|
|
|
|
assert(@event3!="")
|
|
|
|
assert("handler2", @event2)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_on_event5
|
|
|
|
ev = WIN32OLE_EVENT.new(@ie, 'DWebBrowserEvents')
|
|
|
|
ev.on_event {|*args| default_handler(*args)}
|
|
|
|
ev.on_event('NavigateComplete'){|*args| handler3(*args)}
|
2007-02-08 08:00:06 -05:00
|
|
|
@ie.navigate("file:///#{@f}")
|
2006-05-01 05:05:08 -04:00
|
|
|
while @ie.busy
|
2007-02-08 08:00:06 -05:00
|
|
|
sleep 0.1
|
2006-05-01 05:05:08 -04:00
|
|
|
end
|
|
|
|
assert_match(/BeforeNavigate/, @event)
|
|
|
|
assert(/NavigateComplete/ !~ @event)
|
|
|
|
assert(@event!="")
|
|
|
|
end
|
|
|
|
|
2007-09-04 07:48:53 -04:00
|
|
|
def test_unadvise
|
|
|
|
ev = WIN32OLE_EVENT.new(@ie, 'DWebBrowserEvents')
|
|
|
|
ev.on_event {|*args| default_handler(*args)}
|
|
|
|
@ie.navigate("file:///#{@f}")
|
|
|
|
while @ie.busy
|
|
|
|
sleep 0.1
|
|
|
|
end
|
|
|
|
assert_match(/BeforeNavigate/, @event)
|
|
|
|
ev.unadvise
|
|
|
|
@event = ""
|
|
|
|
@ie.navigate("file:///#{@f}")
|
|
|
|
while @ie.busy
|
|
|
|
sleep 0.1
|
|
|
|
end
|
|
|
|
assert_equal("", @event);
|
|
|
|
assert_raise(WIN32OLERuntimeError) {
|
|
|
|
ev.on_event {|*args| default_handler(*args)}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2006-05-01 05:05:08 -04:00
|
|
|
def handler1
|
|
|
|
@event2 = "handler1"
|
|
|
|
end
|
|
|
|
|
|
|
|
def handler2
|
|
|
|
@event2 = "handler2"
|
|
|
|
end
|
|
|
|
|
|
|
|
def handler3(url)
|
|
|
|
@event3 += url
|
|
|
|
end
|
|
|
|
|
|
|
|
def teardown
|
|
|
|
@ie.quit
|
|
|
|
@ie = nil
|
2007-09-04 07:48:53 -04:00
|
|
|
File.unlink(@f)
|
2006-05-01 05:05:08 -04:00
|
|
|
GC.start
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|