summaryrefslogtreecommitdiff
path: root/test/win32ole/test_win32ole_event.rb
blob: 276a01816737867ce1c00a5dacf775c6c13dc1ac (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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
begin
  require 'win32ole'
rescue LoadError
end
require 'test/unit'

if defined?(WIN32OLE_EVENT)
  class TestWIN32OLE_EVENT < Test::Unit::TestCase
    module IE
    end
    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

    def message_loop
      WIN32OLE_EVENT.message_loop
      sleep 0.1
    end

    def wait_ie
      while @ie.readyState != IE::READYSTATE_COMPLETE
        message_loop
      end
    end

    def setup
      WIN32OLE_EVENT.message_loop
      @ie = WIN32OLE.new("InternetExplorer.Application")
      if !defined?(IE::READYSTATE_COMPLETE)
        WIN32OLE.const_load(@ie, IE)
      end
      @ie.visible = true
      message_loop
      @event = ""
      @event2 = ""
      @event3 = ""
      @f = create_temp_html
    end

    def default_handler(event, *args)
      @event += event
    end

    def test_s_new
      assert_raise(TypeError) {
        ev = WIN32OLE_EVENT.new("A")
      }
    end

    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  
        message_loop
      end
      assert_match(/BeforeNavigate/, @event)
      assert_match(/NavigateComplete/, @event)
    end

    def test_on_event
      ev = WIN32OLE_EVENT.new(@ie, 'DWebBrowserEvents')
      ev.on_event {|*args| default_handler(*args)}
      @ie.navigate("file:///#{@f}")
      wait_ie
      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.navigate("file:///#{@f}")
      wait_ie
      assert_equal("handler2", @event2)
    end

    def test_on_event3
      ev = WIN32OLE_EVENT.new(@ie, 'DWebBrowserEvents')
      ev.on_event {|*args| handler1}
      ev.on_event {|*args| handler2}
      @ie.navigate("file:///#{@f}")
      wait_ie
      assert_equal("handler2", @event2)
    end

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

    def test_unadvise
      ev = WIN32OLE_EVENT.new(@ie, 'DWebBrowserEvents')
      ev.on_event {|*args| default_handler(*args)}
      @ie.navigate("file:///#{@f}")
      wait_ie
      assert_match(/BeforeNavigate/, @event)
      ev.unadvise
      @event = ""
      @ie.navigate("file:///#{@f}")
      wait_ie
      assert_equal("", @event);
      assert_raise(WIN32OLERuntimeError) {
        ev.on_event {|*args| default_handler(*args)}
      }
    end

    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

    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}")
      wait_ie
      assert_equal(bl, @ie.locationURL)
    end

    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}")
      wait_ie
      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}")
      wait_ie
      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}")
      wait_ie
      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}")
      wait_ie
      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}")
      wait_ie
      assert_equal(bl, @ie.locationURL)
    end

    def test_off_event
      ev = WIN32OLE_EVENT.new(@ie)
      ev.on_event{handler1}
      ev.off_event
      @ie.navigate("file:///#{@f}")
      wait_ie
      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}")
      wait_ie
      assert_equal("", @event2)
    end

    def handler1
      @event2 = "handler1"
    end

    def handler2
      @event2 = "handler2"
    end

    def handler3(url)
      @event3 += url
    end

    def teardown
      @ie.quit
      message_loop
      @ie = nil
      i = 0
      begin 
        i += 1
        File.unlink(@f) if i < 10
      rescue Errno::EACCES
        message_loop
        retry
      end
      message_loop
      GC.start
      message_loop
    end

    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

  end
end