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
|
2008-07-26 08:27:57 -04:00
|
|
|
module IE
|
|
|
|
end
|
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
|
|
|
|
|
2008-07-25 07:49:28 -04:00
|
|
|
def message_loop
|
|
|
|
WIN32OLE_EVENT.message_loop
|
|
|
|
sleep 0.1
|
|
|
|
end
|
|
|
|
|
2008-07-26 08:27:57 -04:00
|
|
|
def wait_ie
|
|
|
|
while @ie.readyState != IE::READYSTATE_COMPLETE
|
|
|
|
message_loop
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2006-05-01 05:05:08 -04:00
|
|
|
def setup
|
2008-07-25 07:49:28 -04:00
|
|
|
WIN32OLE_EVENT.message_loop
|
2006-05-01 05:05:08 -04:00
|
|
|
@ie = WIN32OLE.new("InternetExplorer.Application")
|
2008-07-26 08:27:57 -04:00
|
|
|
if !defined?(IE::READYSTATE_COMPLETE)
|
|
|
|
WIN32OLE.const_load(@ie, IE)
|
|
|
|
end
|
2006-05-01 05:05:08 -04:00
|
|
|
@ie.visible = true
|
2008-07-25 07:49:28 -04:00
|
|
|
message_loop
|
2006-05-01 05:05:08 -04:00
|
|
|
@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
|
|
|
|
|
2008-07-10 08:22:16 -04:00
|
|
|
def test_s_new
|
|
|
|
assert_raise(TypeError) {
|
|
|
|
ev = WIN32OLE_EVENT.new("A")
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2008-07-08 07:13:09 -04:00
|
|
|
def test_s_new_without_itf
|
|
|
|
ev = WIN32OLE_EVENT.new(@ie)
|
|
|
|
ev.on_event {|*args| default_handler(*args)}
|
|
|
|
@ie.navigate("file:///#{@f}")
|
|
|
|
while @ie.busy
|
|
|
|
WIN32OLE_EVENT.new(@ie)
|
|
|
|
GC.start
|
2008-07-25 07:49:28 -04:00
|
|
|
message_loop
|
2008-07-08 07:13:09 -04:00
|
|
|
end
|
|
|
|
assert_match(/BeforeNavigate/, @event)
|
|
|
|
assert_match(/NavigateComplete/, @event)
|
|
|
|
end
|
|
|
|
|
2006-05-01 05:05:08 -04:00
|
|
|
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}")
|
2008-07-26 08:27:57 -04:00
|
|
|
wait_ie
|
2006-05-01 05:05:08 -04:00
|
|
|
assert_match(/BeforeNavigate/, @event)
|
|
|
|
assert_match(/NavigateComplete/, @event)
|
|
|
|
end
|
|
|
|
|
2008-08-03 06:43:44 -04:00
|
|
|
def test_on_event_symbol
|
|
|
|
ev = WIN32OLE_EVENT.new(@ie)
|
|
|
|
ev.on_event(:BeforeNavigate2) {|*args|
|
|
|
|
handler1
|
|
|
|
}
|
|
|
|
@ie.navigate("file:///#{@f}")
|
|
|
|
wait_ie
|
|
|
|
assert_equal("handler1", @event2)
|
|
|
|
end
|
|
|
|
|
2006-05-01 05:05:08 -04:00
|
|
|
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}")
|
2008-07-26 08:27:57 -04:00
|
|
|
wait_ie
|
2006-05-01 05:05:08 -04:00
|
|
|
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}")
|
2008-07-26 08:27:57 -04:00
|
|
|
wait_ie
|
2006-05-01 05:05:08 -04:00
|
|
|
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}")
|
2008-07-26 08:27:57 -04:00
|
|
|
wait_ie
|
2006-05-01 05:05:08 -04:00
|
|
|
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}")
|
2008-07-26 08:27:57 -04:00
|
|
|
wait_ie
|
2006-05-01 05:05:08 -04:00
|
|
|
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}")
|
2008-07-26 08:27:57 -04:00
|
|
|
wait_ie
|
2007-09-04 07:48:53 -04:00
|
|
|
assert_match(/BeforeNavigate/, @event)
|
|
|
|
ev.unadvise
|
|
|
|
@event = ""
|
|
|
|
@ie.navigate("file:///#{@f}")
|
2008-07-26 08:27:57 -04:00
|
|
|
wait_ie
|
2007-09-04 07:48:53 -04:00
|
|
|
assert_equal("", @event);
|
|
|
|
assert_raise(WIN32OLERuntimeError) {
|
|
|
|
ev.on_event {|*args| default_handler(*args)}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2008-07-08 10:05:38 -04:00
|
|
|
def test_non_exist_event
|
|
|
|
assert_raise(RuntimeError) {
|
|
|
|
ev = WIN32OLE_EVENT.new(@ie, 'XXXX')
|
|
|
|
}
|
|
|
|
dict = WIN32OLE.new('Scripting.Dictionary')
|
|
|
|
assert_raise(RuntimeError) {
|
|
|
|
ev = WIN32OLE_EVENT.new(dict)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2008-07-11 20:31:32 -04:00
|
|
|
def test_on_event_with_outargs
|
|
|
|
ev = WIN32OLE_EVENT.new(@ie)
|
|
|
|
# ev.on_event_with_outargs('BeforeNavigate'){|*args|
|
|
|
|
# args.last[5] = true # Cancel = true
|
|
|
|
# }
|
|
|
|
ev.on_event_with_outargs('BeforeNavigate2'){|*args|
|
|
|
|
args.last[6] = true # Cancel = true
|
|
|
|
}
|
|
|
|
bl = @ie.locationURL
|
|
|
|
@ie.navigate("file:///#{@f}")
|
2008-07-26 08:27:57 -04:00
|
|
|
wait_ie
|
2008-07-11 20:31:32 -04:00
|
|
|
assert_equal(bl, @ie.locationURL)
|
|
|
|
end
|
|
|
|
|
2008-07-13 08:36:26 -04:00
|
|
|
def test_on_event_hash_return
|
|
|
|
ev = WIN32OLE_EVENT.new(@ie)
|
|
|
|
ev.on_event('BeforeNavigate2'){|*args|
|
|
|
|
{:return => 1, :Cancel => true}
|
|
|
|
}
|
|
|
|
bl = @ie.locationURL
|
|
|
|
@ie.navigate("file:///#{@f}")
|
2008-07-26 08:27:57 -04:00
|
|
|
wait_ie
|
2008-07-13 08:36:26 -04:00
|
|
|
assert_equal(bl, @ie.locationURL)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_on_event_hash_return2
|
|
|
|
ev = WIN32OLE_EVENT.new(@ie)
|
|
|
|
ev.on_event('BeforeNavigate2'){|*args|
|
|
|
|
{:Cancel => true}
|
|
|
|
}
|
|
|
|
bl = @ie.locationURL
|
|
|
|
@ie.navigate("file:///#{@f}")
|
2008-07-26 08:27:57 -04:00
|
|
|
wait_ie
|
2008-07-13 08:36:26 -04:00
|
|
|
assert_equal(bl, @ie.locationURL)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_on_event_hash_return3
|
|
|
|
ev = WIN32OLE_EVENT.new(@ie)
|
|
|
|
ev.on_event('BeforeNavigate2'){|*args|
|
|
|
|
{'Cancel' => true}
|
|
|
|
}
|
|
|
|
bl = @ie.locationURL
|
|
|
|
@ie.navigate("file:///#{@f}")
|
2008-07-26 08:27:57 -04:00
|
|
|
wait_ie
|
2008-07-13 08:36:26 -04:00
|
|
|
assert_equal(bl, @ie.locationURL)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_on_event_hash_return4
|
|
|
|
ev = WIN32OLE_EVENT.new(@ie)
|
|
|
|
ev.on_event('BeforeNavigate2'){|*args|
|
|
|
|
{'return' => 2, 'Cancel' => true}
|
|
|
|
}
|
|
|
|
bl = @ie.locationURL
|
|
|
|
@ie.navigate("file:///#{@f}")
|
2008-07-26 08:27:57 -04:00
|
|
|
wait_ie
|
2008-07-13 08:36:26 -04:00
|
|
|
assert_equal(bl, @ie.locationURL)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_on_event_hash_return5
|
|
|
|
ev = WIN32OLE_EVENT.new(@ie)
|
|
|
|
ev.on_event('BeforeNavigate2'){|*args|
|
|
|
|
{6 => true}
|
|
|
|
}
|
|
|
|
bl = @ie.locationURL
|
|
|
|
@ie.navigate("file:///#{@f}")
|
2008-07-26 08:27:57 -04:00
|
|
|
wait_ie
|
2008-07-13 08:36:26 -04:00
|
|
|
assert_equal(bl, @ie.locationURL)
|
|
|
|
end
|
|
|
|
|
2008-07-25 07:49:28 -04:00
|
|
|
def test_off_event
|
|
|
|
ev = WIN32OLE_EVENT.new(@ie)
|
|
|
|
ev.on_event{handler1}
|
|
|
|
ev.off_event
|
|
|
|
@ie.navigate("file:///#{@f}")
|
2008-07-26 08:27:57 -04:00
|
|
|
wait_ie
|
2008-07-25 07:49:28 -04:00
|
|
|
assert_equal("", @event2)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_off_event_arg
|
|
|
|
ev = WIN32OLE_EVENT.new(@ie)
|
|
|
|
ev.on_event('BeforeNavigate2'){handler1}
|
|
|
|
ev.off_event('BeforeNavigate2')
|
|
|
|
@ie.navigate("file:///#{@f}")
|
2008-07-26 08:27:57 -04:00
|
|
|
wait_ie
|
2008-07-25 07:49:28 -04:00
|
|
|
assert_equal("", @event2)
|
|
|
|
end
|
|
|
|
|
2008-08-03 06:43:44 -04:00
|
|
|
def test_off_event_sym_arg
|
|
|
|
ev = WIN32OLE_EVENT.new(@ie)
|
|
|
|
ev.on_event('BeforeNavigate2'){handler1}
|
|
|
|
ev.off_event(:BeforeNavigate2)
|
|
|
|
@ie.navigate("file:///#{@f}")
|
|
|
|
wait_ie
|
|
|
|
assert_equal("", @event2)
|
|
|
|
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
|
2008-07-26 08:27:57 -04:00
|
|
|
message_loop
|
2006-05-01 05:05:08 -04:00
|
|
|
@ie = nil
|
2008-07-25 07:49:28 -04:00
|
|
|
i = 0
|
2008-07-13 08:36:26 -04:00
|
|
|
begin
|
2008-07-25 07:49:28 -04:00
|
|
|
i += 1
|
|
|
|
File.unlink(@f) if i < 10
|
2008-07-14 07:38:09 -04:00
|
|
|
rescue Errno::EACCES
|
2008-07-25 07:49:28 -04:00
|
|
|
message_loop
|
|
|
|
retry
|
2008-07-13 08:36:26 -04:00
|
|
|
end
|
2008-07-25 07:49:28 -04:00
|
|
|
message_loop
|
2006-05-01 05:05:08 -04:00
|
|
|
GC.start
|
2008-07-25 07:49:28 -04:00
|
|
|
message_loop
|
2006-05-01 05:05:08 -04:00
|
|
|
end
|
2008-07-26 08:27:57 -04:00
|
|
|
|
|
|
|
class Handler1
|
|
|
|
attr_reader :val1, :val2, :val3, :val4
|
|
|
|
def initialize
|
|
|
|
@val1 = nil
|
|
|
|
@val2 = nil
|
|
|
|
@val3 = nil
|
|
|
|
@val4 = nil
|
|
|
|
end
|
|
|
|
def onStatusTextChange(t)
|
|
|
|
@val1 = t
|
|
|
|
end
|
|
|
|
def onProgressChange(p, pmax)
|
|
|
|
@val2 = p
|
|
|
|
@val3 = pmax
|
|
|
|
end
|
|
|
|
def onPropertyChange(p)
|
|
|
|
@val4 = p
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class Handler2
|
|
|
|
attr_reader :ev
|
|
|
|
def initialize
|
|
|
|
@ev = ""
|
|
|
|
end
|
|
|
|
def method_missing(ev, *arg)
|
|
|
|
@ev += ev
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_handler1
|
|
|
|
ev = WIN32OLE_EVENT.new(@ie)
|
|
|
|
h1 = Handler1.new
|
|
|
|
ev.handler = h1
|
|
|
|
@ie.navigate("file:///#{@f}")
|
|
|
|
wait_ie
|
|
|
|
assert(h1.val1)
|
|
|
|
assert_equal(h1.val1, ev.handler.val1)
|
|
|
|
assert(h1.val2)
|
|
|
|
assert(h1.val3)
|
|
|
|
assert(h1.val4)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_handler2
|
|
|
|
ev = WIN32OLE_EVENT.new(@ie)
|
|
|
|
h2 = Handler2.new
|
|
|
|
ev.handler = h2
|
|
|
|
@ie.navigate("file:///#{@f}")
|
|
|
|
wait_ie
|
|
|
|
assert(h2.ev != "")
|
|
|
|
end
|
|
|
|
|
2006-05-01 05:05:08 -04:00
|
|
|
end
|
|
|
|
end
|