1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* ext/win32ole/win32ole.c (Init_win32ole): add

WIN32OLE_EVENT#off_event.

* test/win32ole/test_win32ole_event.rb: ditto.
	
* test/win32ole/test_win32ole_event.rb: some refactoring.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@18214 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
suke 2008-07-25 11:49:28 +00:00
parent f37a49e8c8
commit 4aec73e1ed
3 changed files with 110 additions and 38 deletions

View file

@ -1,3 +1,12 @@
Fri Jul 25 20:43:57 2008 Masaki Suketa <masaki.suketa@nifty.ne.jp>
* ext/win32ole/win32ole.c (Init_win32ole): add
WIN32OLE_EVENT#off_event.
* test/win32ole/test_win32ole_event.rb: ditto.
* test/win32ole/test_win32ole_event.rb: some refactoring.
Fri Jul 25 19:50:49 2008 Nobuyoshi Nakada <nobu@ruby-lang.org>
* regint.c (xmalloc, xrealloc, xfree): not to use ruby managed memory.

View file

@ -118,7 +118,7 @@
#define WC2VSTR(x) ole_wc2vstr((x), TRUE)
#define WIN32OLE_VERSION "1.2.7"
#define WIN32OLE_VERSION "1.2.8"
typedef HRESULT (STDAPICALLTYPE FNCOCREATEINSTANCEEX)
(REFCLSID, IUnknown*, DWORD, COSERVERINFO*, DWORD, MULTI_QI*);
@ -499,6 +499,7 @@ static VALUE foleparam_default(VALUE self);
static VALUE foleparam_inspect(VALUE self);
static long ole_search_event_at(VALUE ary, VALUE ev);
static VALUE ole_search_event(VALUE ary, VALUE ev, BOOL *is_default);
static void ole_delete_event(VALUE ary, VALUE ev);
static void hash2ptr_dispparams(VALUE hash, ITypeInfo *pTypeInfo, DISPID dispid, DISPPARAMS *pdispparams);
static VALUE hash2result(VALUE hash);
static void ary2ptr_dispparams(VALUE ary, DISPPARAMS *pdispparams);
@ -517,6 +518,7 @@ static void add_event_call_back(VALUE obj, VALUE event, VALUE data);
static VALUE ev_on_event(int argc, VALUE *argv, VALUE self, VALUE is_ary_arg);
static VALUE fev_on_event(int argc, VALUE *argv, VALUE self);
static VALUE fev_on_event_with_outargs(int argc, VALUE *argv, VALUE self);
static VALUE fev_off_event(int argc, VALUE *argv, VALUE self);
static VALUE fev_unadvise(VALUE self);
static VALUE evs_push(VALUE ev);
static VALUE evs_delete(long i);
@ -7433,6 +7435,16 @@ ole_search_event(VALUE ary, VALUE ev, BOOL *is_default)
return def_event;
}
static void
ole_delete_event(VALUE ary, VALUE ev)
{
long at = -1;
at = ole_search_event_at(ary, ev);
if (at >= 0) {
rb_ary_delete_at(ary, at);
}
}
static void
hash2ptr_dispparams(VALUE hash, ITypeInfo *pTypeInfo, DISPID dispid, DISPPARAMS *pdispparams)
{
@ -8165,6 +8177,40 @@ fev_on_event_with_outargs(int argc, VALUE *argv, VALUE self)
return ev_on_event(argc, argv, self, Qtrue);
}
/*
* call-seq:
* WIN32OLE_EVENT#off_event([event])
*
* removes the callback of event.
*
* ie = WIN32OLE.new('InternetExplorer.Application')
* ev = WIN32OLE_EVENT.new(ie)
* ev.on_event('BeforeNavigate2') {|*args|
* args.last[6] = true
* }
* ...
* ev.off_event('BeforeNavigate2')
* ...
*/
static VALUE
fev_off_event(int argc, VALUE *argv, VALUE self)
{
VALUE event = Qnil;
VALUE events;
rb_secure(4);
rb_scan_args(argc, argv, "01", &event);
if(!NIL_P(event)) {
Check_SafeStr(event);
}
events = rb_ivar_get(self, id_events);
if (NIL_P(events)) {
return Qnil;
}
ole_delete_event(events, event);
return Qnil;
}
/*
* call-seq:
* WIN32OLE_EVENT#unadvise -> nil
@ -8854,6 +8900,7 @@ Init_win32ole()
rb_define_method(cWIN32OLE_EVENT, "initialize", fev_initialize, -1);
rb_define_method(cWIN32OLE_EVENT, "on_event", fev_on_event, -1);
rb_define_method(cWIN32OLE_EVENT, "on_event_with_outargs", fev_on_event_with_outargs, -1);
rb_define_method(cWIN32OLE_EVENT, "off_event", fev_off_event, -1);
rb_define_method(cWIN32OLE_EVENT, "unadvise", fev_unadvise, 0);
cWIN32OLE_VARIANT = rb_define_class("WIN32OLE_VARIANT", rb_cObject);

View file

@ -17,9 +17,17 @@ if defined?(WIN32OLE_EVENT)
dummy_path
end
def message_loop
WIN32OLE_EVENT.message_loop
sleep 0.1
end
def setup
WIN32OLE_EVENT.message_loop
@ie = WIN32OLE.new("InternetExplorer.Application")
message_loop
@ie.visible = true
message_loop
@event = ""
@event2 = ""
@event3 = ""
@ -43,8 +51,7 @@ if defined?(WIN32OLE_EVENT)
while @ie.busy
WIN32OLE_EVENT.new(@ie)
GC.start
WIN32OLE_EVENT.message_loop
sleep 0.1
message_loop
end
assert_match(/BeforeNavigate/, @event)
assert_match(/NavigateComplete/, @event)
@ -57,7 +64,7 @@ if defined?(WIN32OLE_EVENT)
while @ie.busy
WIN32OLE_EVENT.new(@ie, 'DWebBrowserEvents')
GC.start
sleep 0.1
message_loop
end
assert_match(/BeforeNavigate/, @event)
assert_match(/NavigateComplete/, @event)
@ -69,8 +76,7 @@ if defined?(WIN32OLE_EVENT)
ev.on_event('BeforeNavigate') {|*args| handler2}
@ie.navigate("file:///#{@f}")
while @ie.busy
WIN32OLE_EVENT.message_loop
sleep 0.1
message_loop
end
assert_equal("handler2", @event2)
end
@ -81,8 +87,7 @@ if defined?(WIN32OLE_EVENT)
ev.on_event {|*args| handler2}
@ie.navigate("file:///#{@f}")
while @ie.busy
WIN32OLE_EVENT.message_loop
sleep 0.1
message_loop
end
assert_equal("handler2", @event2)
end
@ -94,8 +99,7 @@ if defined?(WIN32OLE_EVENT)
ev.on_event('NavigateComplete'){|*args| handler3(*args)}
@ie.navigate("file:///#{@f}")
while @ie.busy
WIN32OLE_EVENT.message_loop
sleep 0.1
message_loop
end
assert(@event3!="")
assert("handler2", @event2)
@ -107,8 +111,7 @@ if defined?(WIN32OLE_EVENT)
ev.on_event('NavigateComplete'){|*args| handler3(*args)}
@ie.navigate("file:///#{@f}")
while @ie.busy
WIN32OLE_EVENT.message_loop
sleep 0.1
message_loop
end
assert_match(/BeforeNavigate/, @event)
assert(/NavigateComplete/ !~ @event)
@ -120,16 +123,14 @@ if defined?(WIN32OLE_EVENT)
ev.on_event {|*args| default_handler(*args)}
@ie.navigate("file:///#{@f}")
while @ie.busy
WIN32OLE_EVENT.message_loop
sleep 0.1
message_loop
end
assert_match(/BeforeNavigate/, @event)
ev.unadvise
@event = ""
@ie.navigate("file:///#{@f}")
while @ie.busy
WIN32OLE_EVENT.message_loop
sleep 0.1
message_loop
end
assert_equal("", @event);
assert_raise(WIN32OLERuntimeError) {
@ -158,8 +159,7 @@ if defined?(WIN32OLE_EVENT)
bl = @ie.locationURL
@ie.navigate("file:///#{@f}")
while @ie.busy
sleep 0.1
WIN32OLE_EVENT.message_loop
message_loop
end
assert_equal(bl, @ie.locationURL)
end
@ -172,8 +172,7 @@ if defined?(WIN32OLE_EVENT)
bl = @ie.locationURL
@ie.navigate("file:///#{@f}")
while @ie.busy
sleep 0.1
WIN32OLE_EVENT.message_loop
message_loop
end
assert_equal(bl, @ie.locationURL)
end
@ -186,8 +185,7 @@ if defined?(WIN32OLE_EVENT)
bl = @ie.locationURL
@ie.navigate("file:///#{@f}")
while @ie.busy
sleep 0.1
WIN32OLE_EVENT.message_loop
message_loop
end
assert_equal(bl, @ie.locationURL)
end
@ -200,8 +198,7 @@ if defined?(WIN32OLE_EVENT)
bl = @ie.locationURL
@ie.navigate("file:///#{@f}")
while @ie.busy
sleep 0.1
WIN32OLE_EVENT.message_loop
message_loop
end
assert_equal(bl, @ie.locationURL)
end
@ -214,8 +211,7 @@ if defined?(WIN32OLE_EVENT)
bl = @ie.locationURL
@ie.navigate("file:///#{@f}")
while @ie.busy
sleep 0.1
WIN32OLE_EVENT.message_loop
message_loop
end
assert_equal(bl, @ie.locationURL)
end
@ -228,12 +224,35 @@ if defined?(WIN32OLE_EVENT)
bl = @ie.locationURL
@ie.navigate("file:///#{@f}")
while @ie.busy
sleep 0.1
WIN32OLE_EVENT.message_loop
message_loop
end
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}")
while @ie.busy
message_loop
end
WIN32OLE_EVENT.message_loop
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}")
while @ie.busy
message_loop
end
WIN32OLE_EVENT.message_loop
assert_equal("", @event2)
end
def handler1
@event2 = "handler1"
end
@ -248,21 +267,18 @@ if defined?(WIN32OLE_EVENT)
def teardown
@ie.quit
WIN32OLE_EVENT.message_loop
@ie = nil
WIN32OLE_EVENT.message_loop
sleep 0.1
i = 0
begin
File.unlink(@f)
i += 1
File.unlink(@f) if i < 10
rescue Errno::EACCES
WIN32OLE_EVENT.message_loop
sleep 0.1
File.unlink(@f)
message_loop
retry
end
message_loop
GC.start
WIN32OLE_EVENT.message_loop
sleep 0.1
message_loop
end
end
end