summaryrefslogtreecommitdiff
path: root/test/win32ole/test_win32ole_event.rb
blob: 1b0285cb58773f7d80d6d97d1b9c7e8945810043 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
begin
  require 'win32ole'
rescue
end
require 'test/unit'
if defined?(WIN32OLE_EVENT)
  class TestWIN32OLE_EVENT < Test::Unit::TestCase
    def setup
      @ie = WIN32OLE.new("InternetExplorer.Application")
      @ie.visible = true
      @event = ""
      @event2 = ""
      @event3 = ""
    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)}
      @ie.gohome
      while @ie.busy
        WIN32OLE_EVENT.message_loop
      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}
      @ie.gohome
      while @ie.busy
        WIN32OLE_EVENT.message_loop
      end
      assert_equal("handler2", @event2)
    end

    def test_on_event3
      ev = WIN32OLE_EVENT.new(@ie, 'DWebBrowserEvents')
      ev.on_event {handler1}
      ev.on_event {handler2}
      @ie.gohome
      while @ie.busy
        WIN32OLE_EVENT.message_loop
      end
      assert_equal("handler2", @event2)
    end

    def test_on_event4
      ev = WIN32OLE_EVENT.new(@ie, 'DWebBrowserEvents')
      ev.on_event{handler1}
      ev.on_event{handler2}
      ev.on_event('NavigateComplete'){|*args| handler3(*args)}
      @ie.gohome
      while @ie.busy
        WIN32OLE_EVENT.message_loop
      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)}
      @ie.gohome
      while @ie.busy
        WIN32OLE_EVENT.message_loop
      end
      assert_match(/BeforeNavigate/, @event)
      assert(/NavigateComplete/ !~ @event)
      assert(@event!="")
    end

    def handler1
      @event2 = "handler1"
    end

    def handler2
      @event2 = "handler2"
    end

    def handler3(url)
      @event3 += url
    end

    def teardown
      @ie.quit
      @ie = nil
      GC.start
      sleep 1
    end
  end
end