diff options
Diffstat (limited to 'trunk/test/win32ole')
-rw-r--r-- | trunk/test/win32ole/err_in_callback.rb | 11 | ||||
-rw-r--r-- | trunk/test/win32ole/test_err_in_callback.rb | 82 | ||||
-rw-r--r-- | trunk/test/win32ole/test_folderitem2_invokeverb.rb | 89 | ||||
-rw-r--r-- | trunk/test/win32ole/test_nil2vtempty.rb | 36 | ||||
-rw-r--r-- | trunk/test/win32ole/test_ole_methods.rb | 36 | ||||
-rw-r--r-- | trunk/test/win32ole/test_propertyputref.rb | 30 | ||||
-rw-r--r-- | trunk/test/win32ole/test_win32ole.rb | 445 | ||||
-rw-r--r-- | trunk/test/win32ole/test_win32ole_event.rb | 333 | ||||
-rw-r--r-- | trunk/test/win32ole/test_win32ole_method.rb | 138 | ||||
-rw-r--r-- | trunk/test/win32ole/test_win32ole_param.rb | 91 | ||||
-rw-r--r-- | trunk/test/win32ole/test_win32ole_type.rb | 249 | ||||
-rw-r--r-- | trunk/test/win32ole/test_win32ole_typelib.rb | 110 | ||||
-rw-r--r-- | trunk/test/win32ole/test_win32ole_variable.rb | 61 | ||||
-rw-r--r-- | trunk/test/win32ole/test_win32ole_variant.rb | 638 | ||||
-rw-r--r-- | trunk/test/win32ole/test_win32ole_variant_m.rb | 35 | ||||
-rw-r--r-- | trunk/test/win32ole/test_win32ole_variant_with_ie.rb | 53 | ||||
-rw-r--r-- | trunk/test/win32ole/test_word.rb | 72 |
17 files changed, 2509 insertions, 0 deletions
diff --git a/trunk/test/win32ole/err_in_callback.rb b/trunk/test/win32ole/err_in_callback.rb new file mode 100644 index 0000000000..d09870c95d --- /dev/null +++ b/trunk/test/win32ole/err_in_callback.rb @@ -0,0 +1,11 @@ +require 'win32ole' +ie = WIN32OLE.new('InternetExplorer.Application') +ie.visible = true +WIN32OLE_EVENT.message_loop +sleep 0.2 +ev = WIN32OLE_EVENT.new(ie) + +ev.on_event('BeforeNavigate2') {|*args| + foo +} +ie.navigate(ARGV.shift) diff --git a/trunk/test/win32ole/test_err_in_callback.rb b/trunk/test/win32ole/test_err_in_callback.rb new file mode 100644 index 0000000000..11ea6273fd --- /dev/null +++ b/trunk/test/win32ole/test_err_in_callback.rb @@ -0,0 +1,82 @@ +# +# test Win32OLE avoids cfp consistency error when the exception raised +# in WIN32OLE_EVENT handler block. [ruby-dev:35450] +# + +begin + require 'win32ole' +rescue LoadError +end +require 'rbconfig' +if defined?(WIN32OLE) + require 'mkmf' + require 'test/unit' + class TestErrInCallBack < Test::Unit::TestCase + def setup + @ruby = nil + if File.exist?("./" + CONFIG["RUBY_INSTALL_NAME"] + CONFIG["EXEEXT"]) + sep = File::ALT_SEPARATOR || "/" + @ruby = "." + sep + CONFIG["RUBY_INSTALL_NAME"] + @iopt = $:.map {|e| + " -I " + e + }.join("") + @script = File.dirname(__FILE__) + "/err_in_callback.rb" + @param = create_temp_html + @param = "file:///" + @param.gsub(/\\/, '/') + end + end + + def create_temp_html + fso = WIN32OLE.new('Scripting.FileSystemObject') + dummy_file = fso.GetTempName + ".html" + cfolder = fso.getFolder(".") + @str = "This is test HTML file for Win32OLE (#{Time.now})" + f = cfolder.CreateTextFile(dummy_file) + f.writeLine("<html><body><div id='str'>#{@str}</div></body></html>") + f.close + @f = dummy_file + dummy_path = cfolder.path + "\\" + dummy_file + dummy_path + end + + def test_err_in_callback + if @ruby + cmd = "#{@ruby} -v #{@iopt} #{@script} #{@param} > test_err_in_callback.log 2>&1" + system(cmd) + str = "" + open("test_err_in_callback.log") {|ifs| + str = ifs.read + } + assert_match(/NameError/, str) + end + end + + def ie_quit + sh = WIN32OLE.new('Shell.Application') + sh.windows.each do |w| + if w.ole_type.name == 'IWebBrowser2' + 20.times do |i| + if w.locationURL != "" && w.document + break + end + WIN32OLE_EVENT.message_loop + sleep 1 + end + e = w.document.getElementById("str") + if e && e.innerHTML == @str + w.quit + WIN32OLE_EVENT.message_loop + sleep 0.2 + end + end + end + end + + def teardown + WIN32OLE_EVENT.message_loop + ie_quit + File.unlink(@f) + File.unlink("test_err_in_callback.log") + end + end +end diff --git a/trunk/test/win32ole/test_folderitem2_invokeverb.rb b/trunk/test/win32ole/test_folderitem2_invokeverb.rb new file mode 100644 index 0000000000..07cb9802d6 --- /dev/null +++ b/trunk/test/win32ole/test_folderitem2_invokeverb.rb @@ -0,0 +1,89 @@ +# +# This script check that Win32OLE can execute InvokeVerb method of FolderItem2. +# + +begin + require 'win32ole' +rescue LoadError +end +require 'test/unit' + +if defined?(WIN32OLE) + class TestInvokeVerb < Test::Unit::TestCase + def setup + # + # make dummy.txt file for InvokeVerb test. + # + + @fso = WIN32OLE.new('Scripting.FileSystemObject') + @dummy_file = @fso.GetTempName + @cfolder = @fso.getFolder(".") + f = @cfolder.CreateTextFile(@dummy_file) + f.close + @dummy_path = @cfolder.path + "\\" + @dummy_file + + @shell=WIN32OLE.new('Shell.Application') + @nsp = @shell.NameSpace(@cfolder.path) + @fi2 = @nsp.parseName(@dummy_file) + + @shortcut = nil + + # + # Search the 'Create Shortcut (&S)' string in Japanese. + # Yes, I know the string in the Windows 2000 Japanese Edition. + # But I do not know about the string in any other Windows. + # + verbs = @fi2.verbs + verbs.extend(Enumerable) + @cp = WIN32OLE.codepage + begin + WIN32OLE.codepage = 932 + rescue + end + @shortcut = verbs.collect{|verb| + verb.name + }.find {|name| + name.unpack("C*") == [131, 86, 131, 135, 129, 91, 131, 103, 131, 74, 131, 98, 131, 103, 130, 204, 141, 236, 144, 172, 40, 38, 83, 41] + # /.*\(\&S\)$/ =~ name + } + end + + def find_link(path) + arlink = [] + @cfolder.files.each do |f| + if /\.lnk$/ =~ f.path + linkinfo = @nsp.parseName(f.name).getLink + arlink.push f if linkinfo.path == path + end + end + arlink + end + + def test_invokeverb + links = find_link(@dummy_path) + assert(0, links.size) + + assert(@shortcut) + + # Now create shortcut to @dummy_path + arg = WIN32OLE_VARIANT.new(@shortcut) + @fi2.InvokeVerb(arg) + + # Now search shortcut to @dummy_path + links = find_link(@dummy_path) + assert(1, links.size) + @lpath = links[0].path + end + + def teardown + if @lpath + @fso.deleteFile(@lpath) + end + if @dummy_path + @fso.deleteFile(@dummy_path) + end + WIN32OLE.codepage = @cp + end + + end +end diff --git a/trunk/test/win32ole/test_nil2vtempty.rb b/trunk/test/win32ole/test_nil2vtempty.rb new file mode 100644 index 0000000000..4be3902b6d --- /dev/null +++ b/trunk/test/win32ole/test_nil2vtempty.rb @@ -0,0 +1,36 @@ +# This is test script to check that WIN32OLE should convert nil to VT_EMPTY in second try. +# [ruby-talk:137054] +begin + require 'win32ole' +rescue LoadError +end +require 'test/unit' + +if defined?(WIN32OLE) + class TestNIL2VT_EMPTY < Test::Unit::TestCase + def setup + fs = WIN32OLE.new('Scripting.FileSystemObject') + @path = fs.GetFolder(".").path + end + def test_openSchema + con = nil + begin + con = WIN32OLE.new('ADODB.Connection') + con.connectionString = "Provider=MSDASQL;Extended Properties=" + con.connectionString +="\"DRIVER={Microsoft Text Driver (*.txt; *.csv)};DBQ=#{@path}\"" + con.open + rescue + con = nil + end + if con + rs = con.openSchema(4, [nil,nil,"DUMMY", "TABLE"]) + assert(rs) + assert_equal("_Recordset", rs.ole_type.name) + + rs = con.openSchema(4, [WIN32OLE_VARIANT::Empty, WIN32OLE_VARIANT::Empty, "DUMMY", "TABLE"]) + assert(rs) + assert_equal("_Recordset", rs.ole_type.name) + end + end + end +end diff --git a/trunk/test/win32ole/test_ole_methods.rb b/trunk/test/win32ole/test_ole_methods.rb new file mode 100644 index 0000000000..ca1c03b010 --- /dev/null +++ b/trunk/test/win32ole/test_ole_methods.rb @@ -0,0 +1,36 @@ +# +# This is test for [ruby-talk:196897] +# +begin + require 'win32ole' +rescue LoadError +end +require "test/unit" + +if defined?(WIN32OLE) + class TestWIN32OLE_FOR_PROPERTYPUTREF < Test::Unit::TestCase + + def setup + @obj = WIN32OLE.new('Scripting.Dictionary') + end + + def test_ole_methods + x = @obj.ole_methods.select {|m| + m.invoke_kind == 'PROPERTYPUTREF' + } + assert(x.size > 0) + assert_equal(1, x.size) + assert_equal('Item', x[0].name) + end + + def test_ole_put_methods + x = @obj.ole_put_methods.select {|m| + m.invoke_kind == 'PROPERTYPUTREF' + } + assert(x.size > 0) + assert_equal(1, x.size) + assert_equal('Item', x[0].name) + end + + end +end diff --git a/trunk/test/win32ole/test_propertyputref.rb b/trunk/test/win32ole/test_propertyputref.rb new file mode 100644 index 0000000000..24459afe9b --- /dev/null +++ b/trunk/test/win32ole/test_propertyputref.rb @@ -0,0 +1,30 @@ +require 'test/unit' +begin + require 'win32ole' +rescue LoadError +end + +if defined?(WIN32OLE) + class TestWIN32OLE_PROPERTYPUTREF < Test::Unit::TestCase + def setup + begin + @sapi = WIN32OLE.new('SAPI.SpVoice') + @sv = @sapi.voice + rescue WIN32OLERuntimeError + @sapi = nil + end + end + def test_sapi + if @sapi + new_id = @sapi.getvoices.item(0).Id + @sapi.voice = @sapi.getvoices.item(0) + assert_equal(new_id, @sapi.voice.Id) + end + end + def teardown + if @sapi + @sapi.voice = @sv + end + end + end +end diff --git a/trunk/test/win32ole/test_win32ole.rb b/trunk/test/win32ole/test_win32ole.rb new file mode 100644 index 0000000000..d9e595347c --- /dev/null +++ b/trunk/test/win32ole/test_win32ole.rb @@ -0,0 +1,445 @@ +# + +begin + require 'win32ole' +rescue LoadError +end +require 'test/unit' + +if defined?(WIN32OLE) + module CONST1 + end + module CONST2 + end + + module TestCaseForDict + def test_convert_bignum + @dict1.add("a", 9999999999) + @dict1.add("b", 999999999) + @dict1.add("c", @dict1.item("b") * 10 + 9) + assert_equal(9999999999, @dict1.item("a")) + assert_equal(9999999999, @dict1.item("c")) + end + def test_add + @dict1.add("a", 1000) + assert_equal(1000, @dict1.item("a")) + end + def test_setproperty_equal_ended + @dict1.compareMode = 1 + @dict1.add("one", 1) + assert_equal(1, @dict1.item("ONE")) + @dict2.add("one", 1) + assert_nil(@dict2.item("ONE")) + assert_equal(1, @dict2.item("one")) + end + def test_non_exist_property + assert_raise(WIN32OLERuntimeError) { + @dict1.unknown_property = 1 + } + end + + def test_raise_message + exc = assert_raise(WIN32OLERuntimeError) { + @dict1.add + } + assert_match(/^\(in OLE method `add': \)/, exc.message) #` + + exc = assert_raise(WIN32OLERuntimeError) { + @dict1._invoke(1, [], []) + } + assert_match(/^\(in OLE method `<dispatch id:1>': \)/, exc.message) #` + + exc = assert_raise(WIN32OLERuntimeError) { + @dict1.compareMode = -1 + } + assert_match(/^\(in setting property `compareMode': \)/, exc.message) #` + end + + def test_ole_methods + methods = @dict1.ole_methods + mnames = methods.collect {|m| + m.name + } + assert(mnames.include?("Add")) + end + + def test_ole_func_methods + methods = @dict1.ole_func_methods + mnames = methods.collect {|m| + m.name + } + assert(mnames.include?("Add")) + end + + def test_ole_put_methods + methods = @dict1.ole_put_methods + mnames = methods.collect {|m| + m.name + } + assert(mnames.include?("CompareMode")) + end + + def test_ole_get_methods + methods = @dict1.ole_get_methods + mnames = methods.collect {|m| + m.name + } + assert(mnames.include?("Count")) + end + + def test_ole_mehtod_help + minfo = @dict1.ole_method_help("Add") + assert_equal(2, minfo.size_params) + end + + def test_ole_typelib + tlib = @dict1.ole_typelib + assert_equal("Microsoft Scripting Runtime", tlib.name); + end + + def test_each + @dict1.add("one", 1) + @dict1.add("two", 2) + i = 0 + @dict1.keys.each do |item| + i += 1 + end + assert_equal(2, i) + end + + def test_bracket + @dict1.add("foo", "FOO") + assert_equal("FOO", @dict1.item("foo")) + assert_equal("FOO", @dict1["foo"]) + end + + def test_bracket_equal + @dict1.add("foo", "FOO") + @dict1["foo"] = "BAR" + assert_equal("BAR", @dict1["foo"]) + end + + def test_invoke_with_array + @dict1.add("ary1", [1,2,3]) + assert_equal([1,2,3], @dict1["ary1"]) + + @dict1.add("ary2", [[1,2,"a"], [3,4,"b"]]) + assert_equal([[1,2,"a"], [3,4,"b"]], @dict1["ary2"]) + + @dict1.add("ary3", [[[1]]]) + assert_equal([[[1]]], @dict1["ary3"]) + + @dict1.add("ary4", [[[1], [2], [3]], [[4], [5], [6]]]) + assert_equal([[[1],[2], [3]], [[4], [5], [6]]], @dict1["ary4"]) + end + end + + class TestWin32OLE < Test::Unit::TestCase + include TestCaseForDict + def setup + @dict1 = WIN32OLE.new('Scripting.Dictionary') + @dict2 = WIN32OLE.new('Scripting.Dictionary') + end + def test_s_new + assert_instance_of(WIN32OLE, @dict1) + assert_instance_of(WIN32OLE, @dict2) + end + + def test_s_new_DCOM + rshell = WIN32OLE.new("Shell.Application") + assert_instance_of(WIN32OLE, rshell) + end + + def test_s_new_from_clsid + shell = WIN32OLE.new("{13709620-C279-11CE-A49E-444553540000}") + assert_instance_of(WIN32OLE, shell) + exc = assert_raise(WIN32OLERuntimeError) { + WIN32OLE.new("{000}") + } + assert_match(/unknown OLE server: `\{000\}'/, exc.message) #` + end + + def test_s_connect + obj = WIN32OLE.connect("winmgmts:") + assert_instance_of(WIN32OLE, obj) + end + + def test_invoke_accept_symbol_hash_key + fso = WIN32OLE.new('Scripting.FileSystemObject') + afolder = fso.getFolder(".") + bfolder = fso.getFolder({"FolderPath" => "."}) + cfolder = fso.getFolder({:FolderPath => "."}) + assert_equal(afolder.path, bfolder.path) + assert_equal(afolder.path, cfolder.path) + fso = nil + end + + def test_setproperty + installer = WIN32OLE.new("WindowsInstaller.Installer") + record = installer.CreateRecord(2) + # this is the way to set property with argument in Win32OLE. + record.setproperty( "StringData", 1, 'dddd') + assert_equal('dddd', record.StringData(1)) + end + + def test_ole_type + fso = WIN32OLE.new('Scripting.FileSystemObject') + tobj = fso.ole_type + assert_match(/^IFileSystem/, tobj.name) + end + + def test_ole_obj_help + fso = WIN32OLE.new('Scripting.FileSystemObject') + tobj = fso.ole_obj_help + assert_match(/^IFileSystem/, tobj.name) + end + + + def test_invoke_hash_key_non_str_sym + fso = WIN32OLE.new('Scripting.FileSystemObject') + begin + bfolder = fso.getFolder({1 => "."}) + assert(false) + rescue TypeError + assert(true) + end + fso = nil + end + + def test_get_win32ole_object + shell = WIN32OLE.new('Shell.Application') + folder = shell.nameSpace(0) + assert_instance_of(WIN32OLE, folder) + end + + def test_invoke_accept_multi_hash_key + shell = WIN32OLE.new('Shell.Application') + folder = shell.nameSpace(0) + item = folder.items.item(0) + name = folder.getDetailsOf(item, 0) + assert_equal(item.name, name) + name = folder.getDetailsOf({:vItem => item, :iColumn => 0}) + assert_equal(item.name, name) + name = folder.getDetailsOf({"vItem" => item, :iColumn => 0}) + assert_equal(item.name, name) + end + + def test_ole_invoke_with_named_arg_last + shell = WIN32OLE.new('Shell.Application') + folder = shell.nameSpace(0) + item = folder.items.item(0) + name = folder.getDetailsOf(item, {:iColumn => 0}) + assert_equal(item.name, name) + end + + def test__invoke + shell=WIN32OLE.new('Shell.Application') + assert_equal(shell.NameSpace(0).title, shell._invoke(0x60020002, [0], [WIN32OLE::VARIANT::VT_VARIANT]).title) + end + + def test_ole_query_interface + shell=WIN32OLE.new('Shell.Application') + assert_raise(ArgumentError) { + shell2 = shell.ole_query_interface + } + shell2 = shell.ole_query_interface('{A4C6892C-3BA9-11D2-9DEA-00C04FB16162}') + assert_instance_of(WIN32OLE, shell2) + end + + def test_ole_respond_to + fso = WIN32OLE.new('Scripting.FileSystemObject') + assert(fso.ole_respond_to?('getFolder')) + assert(fso.ole_respond_to?('GETFOLDER')) + assert(fso.ole_respond_to?(:getFolder)) + assert(!fso.ole_respond_to?('XXXXX')) + assert_raise(TypeError) { + assert_raise(fso.ole_respond_to?(1)) + } + end + + def test_invoke + fso = WIN32OLE.new('Scripting.FileSystemObject') + assert(fso.invoke(:getFolder, ".")) + assert(fso.invoke('getFolder', ".")) + end + + def test_s_const_load + assert(!defined?(CONST1::SsfWINDOWS)) + shell=WIN32OLE.new('Shell.Application') + WIN32OLE.const_load(shell, CONST1) + assert_equal(36, CONST1::SsfWINDOWS) + + assert(!defined?(CONST2::SsfWINDOWS)) + WIN32OLE.const_load("Microsoft Shell Controls And Automation", CONST2) + assert_equal(36, CONST2::SsfWINDOWS) + end + + def test_s_create_guid + guid = WIN32OLE.create_guid + assert_match(/^\{[A-Z0-9]{8}\-[A-Z0-9]{4}\-[A-Z0-9]{4}\-[A-Z0-9]{4}\-[A-Z0-9]{12}/, + guid) + end + + # + # WIN32OLE.codepage is initialized according to Encoding.default_external. + # + # def test_s_codepage + # assert_equal(WIN32OLE::CP_ACP, WIN32OLE.codepage) + # end + + def test_s_codepage_set + cp = WIN32OLE.codepage + WIN32OLE.codepage = WIN32OLE::CP_UTF8 + assert_equal(WIN32OLE::CP_UTF8, WIN32OLE.codepage) + WIN32OLE.codepage = cp + end + + def test_s_codepage_changed + cp = WIN32OLE.codepage + fso = WIN32OLE.new("Scripting.FileSystemObject") + fname = fso.getTempName + begin + obj = WIN32OLE_VARIANT.new([0x3042].pack("U*").force_encoding("UTF-8")) + WIN32OLE.codepage = WIN32OLE::CP_UTF8 + assert_equal("\xE3\x81\x82".force_encoding("CP65001"), obj.value) + + begin + WIN32OLE.codepage = 932 # Windows-31J + rescue WIN32OLERuntimeError + end + if (WIN32OLE.codepage == 932) + assert_equal("\x82\xA0".force_encoding("CP932"), obj.value) + end + + begin + WIN32OLE.codepage = 20932 # MS EUC-JP + rescue WIN32OLERuntimeError + end + if (WIN32OLE.codepage == 20932) + assert_equal("\xA4\xA2".force_encoding("CP20932"), obj.value) + end + + WIN32OLE.codepage = cp + file = fso.opentextfile(fname, 2, true) + file.write [0x3042].pack("U*").force_encoding("UTF-8") + file.close + str = "" + open(fname, "r:ascii-8bit") {|ifs| + str = ifs.read + } + assert_equal("\202\240", str) + + # This test fail if codepage 20932 (euc) is not installed. + begin + WIN32OLE.codepage = 20932 + rescue WIN32OLERuntimeError + end + if (WIN32OLE.codepage == 20932) + WIN32OLE.codepage = cp + file = fso.opentextfile(fname, 2, true) + file.write [164, 162].pack("c*").force_encoding("EUC-JP") + file.close + open(fname, "r:ascii-8bit") {|ifs| + str = ifs.read + } + assert_equal("\202\240", str) + end + + ensure + WIN32OLE.codepage = cp + if (File.exist?(fname)) + File.unlink(fname) + end + end + end + + def test_s_locale + assert_equal(WIN32OLE::LOCALE_SYSTEM_DEFAULT, WIN32OLE.locale) + end + + def test_s_locale_set + begin + WIN32OLE.locale = 1041 + assert_equal(1041, WIN32OLE.locale) + WIN32OLE.locale = WIN32OLE::LOCALE_SYSTEM_DEFAULT + assert_raise(WIN32OLERuntimeError) { + WIN32OLE.locale = 111 + } + assert_equal(WIN32OLE::LOCALE_SYSTEM_DEFAULT, WIN32OLE.locale) + ensure + WIN32OLE.locale = WIN32OLE::LOCALE_SYSTEM_DEFAULT + end + end + + def test_s_locale_change + begin + WIN32OLE.locale = 0x0411 + obj = WIN32OLE_VARIANT.new("\\100,000", WIN32OLE::VARIANT::VT_CY) + assert_equal("100000", obj.value) + assert_raise(WIN32OLERuntimeError) { + obj = WIN32OLE_VARIANT.new("$100.000", WIN32OLE::VARIANT::VT_CY) + } + + WIN32OLE.locale = 1033 + obj = WIN32OLE_VARIANT.new("$100,000", WIN32OLE::VARIANT::VT_CY) + assert_equal("100000", obj.value) + ensure + WIN32OLE.locale = WIN32OLE::LOCALE_SYSTEM_DEFAULT + end + end + + def test_const_CP_ACP + assert_equal(0, WIN32OLE::CP_ACP) + end + + def test_const_CP_OEMCP + assert_equal(1, WIN32OLE::CP_OEMCP) + end + + def test_const_CP_MACCP + assert_equal(2, WIN32OLE::CP_MACCP) + end + + def test_const_CP_THREAD_ACP + assert_equal(3, WIN32OLE::CP_THREAD_ACP) + end + + def test_const_CP_SYMBOL + assert_equal(42, WIN32OLE::CP_SYMBOL) + end + + def test_const_CP_UTF7 + assert_equal(65000, WIN32OLE::CP_UTF7) + end + + def test_const_CP_UTF8 + assert_equal(65001, WIN32OLE::CP_UTF8) + end + + def test_const_LOCALE_SYSTEM_DEFAULT + assert_equal(0x0800, WIN32OLE::LOCALE_SYSTEM_DEFAULT); + end + + def test_const_LOCALE_USER_DEFAULT + assert_equal(0x0400, WIN32OLE::LOCALE_USER_DEFAULT); + end + end + + # test of subclass of WIN32OLE + class MyDict < WIN32OLE + def MyDict.new + super('Scripting.Dictionary') + end + end + class TestMyDict < Test::Unit::TestCase + include TestCaseForDict + def setup + @dict1 = MyDict.new + @dict2 = MyDict.new + end + def test_s_new + assert_instance_of(MyDict, @dict1) + assert_instance_of(MyDict, @dict2) + end + end +end diff --git a/trunk/test/win32ole/test_win32ole_event.rb b/trunk/test/win32ole/test_win32ole_event.rb new file mode 100644 index 0000000000..5b2a1af420 --- /dev/null +++ b/trunk/test/win32ole/test_win32ole_event.rb @@ -0,0 +1,333 @@ +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_event_symbol + ev = WIN32OLE_EVENT.new(@ie) + ev.on_event(:BeforeNavigate2) {|*args| + handler1 + } + @ie.navigate("file:///#{@f}") + wait_ie + assert_equal("handler1", @event2) + 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 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 + + 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 diff --git a/trunk/test/win32ole/test_win32ole_method.rb b/trunk/test/win32ole/test_win32ole_method.rb new file mode 100644 index 0000000000..161b10233f --- /dev/null +++ b/trunk/test/win32ole/test_win32ole_method.rb @@ -0,0 +1,138 @@ +begin + require 'win32ole' +rescue LoadError +end +require "test/unit" + +if defined?(WIN32OLE_METHOD) + class TestWIN32OLE_METHOD < Test::Unit::TestCase + + def setup + ole_type = WIN32OLE_TYPE.new("Microsoft Shell Controls And Automation", "Shell") + @m_open = WIN32OLE_METHOD.new(ole_type, "open") + @m_namespace = WIN32OLE_METHOD.new(ole_type, "namespace") + @m_parent = WIN32OLE_METHOD.new(ole_type, "parent") + @m_invoke = WIN32OLE_METHOD.new(ole_type, "invoke") + @m_browse_for_folder = WIN32OLE_METHOD.new(ole_type, "BrowseForFolder") + + ole_type = WIN32OLE_TYPE.new("Microsoft Scripting Runtime", "File") + @m_file_name = WIN32OLE_METHOD.new(ole_type, "name") + + ole_type = WIN32OLE_TYPE.new("Microsoft Internet Controls", "WebBrowser") + @m_navigate_complete = WIN32OLE_METHOD.new(ole_type, "NavigateComplete") + end + + def test_initialize + ole_type = WIN32OLE_TYPE.new("Microsoft Shell Controls And Automation", "Shell") + assert_raise(ArgumentError) { + method = WIN32OLE_METHOD.new("foo") + } + assert_raise(ArgumentError) { + method = WIN32OLE_METHOD.new(ole_type) + } + assert_raise(WIN32OLERuntimeError) { + method = WIN32OLE_METHOD.new(ole_type, "NonExistMethod") + } + assert_raise(TypeError) { + method = WIN32OLE_METHOD.new(ole_type, 1) + } + method = WIN32OLE_METHOD.new(ole_type, "Open") + assert_instance_of(WIN32OLE_METHOD, method) + method = WIN32OLE_METHOD.new(ole_type, "open") + assert_instance_of(WIN32OLE_METHOD, method) + end + + def test_name + assert_equal("Open", @m_open.name) + end + + def test_return_type + assert_equal("VOID", @m_open.return_type) + assert_equal("Folder", @m_namespace.return_type) + end + + def test_return_vtype + assert_equal(24, @m_open.return_vtype) + assert_equal(26, @m_namespace.return_vtype) + end + + def test_return_type_detail + assert_equal(['VOID'], @m_open.return_type_detail) + assert_equal(['PTR', 'USERDEFINED', 'Folder'], @m_namespace.return_type_detail) + end + + def test_invoke_kind + assert_equal('FUNC', @m_open.invoke_kind) + assert_equal('FUNC', @m_namespace.invoke_kind) + assert_equal('PROPERTYGET', @m_parent.invoke_kind) + end + + def test_invkind + assert_equal(1, @m_namespace.invkind) + assert_equal(2, @m_parent.invkind) + end + + def test_visible? + assert(@m_namespace.visible?) + assert(!@m_invoke.visible?) + end + + def test_event? + assert(@m_navigate_complete.event?) + assert(!@m_namespace.event?) + end + + def test_event_interface + assert_equal("DWebBrowserEvents", @m_navigate_complete.event_interface) + assert_equal(nil, @m_namespace.event_interface) + end + + def test_helpstring + assert_equal("Get special folder from ShellSpecialFolderConstants", @m_namespace.helpstring) + end + + def test_helpfile + assert_equal("", @m_namespace.helpfile) + assert_match(/VBENLR.*\.CHM$/i, @m_file_name.helpfile) + end + + def test_helpcontext + assert_equal(0, @m_namespace.helpcontext) + assert_equal(2181996, @m_file_name.helpcontext) + end + + def test_dispid + assert_equal(1610743810, @m_namespace.dispid) + end + + def test_offset_vtbl + assert_equal(24, @m_invoke.offset_vtbl) + end + + def test_size_params + assert_equal(1, @m_open.size_params) + assert_equal(4, @m_browse_for_folder.size_params) + end + + def test_size_opt_params + assert_equal(0, @m_open.size_opt_params) + assert_equal(1, @m_browse_for_folder.size_opt_params) + end + + def test_params + params = @m_browse_for_folder.params + assert_instance_of(Array, params) + assert_equal(4, params.size) + assert_instance_of(WIN32OLE_PARAM, params[0]) + end + + def test_to_s + assert_equal(@m_namespace.name, @m_namespace.to_s) + end + + def test_inspect + assert_equal("#<WIN32OLE_METHOD:NameSpace>", @m_namespace.inspect) + end + + end +end diff --git a/trunk/test/win32ole/test_win32ole_param.rb b/trunk/test/win32ole/test_win32ole_param.rb new file mode 100644 index 0000000000..1c866ca53e --- /dev/null +++ b/trunk/test/win32ole/test_win32ole_param.rb @@ -0,0 +1,91 @@ +begin + require 'win32ole' +rescue LoadError +end +require "test/unit" + +if defined?(WIN32OLE_PARAM) + class TestWIN32OLE_PARAM < Test::Unit::TestCase + + def setup + ole_type = WIN32OLE_TYPE.new("Microsoft Internet Controls", "WebBrowser") + m_navigate = WIN32OLE_METHOD.new(ole_type, "Navigate") + m_before_navigate = WIN32OLE_METHOD.new(ole_type, "BeforeNavigate") + params = m_navigate.params + @param_url = params[0] + @param_flags = params[1] + @param_cancel = m_before_navigate.params[5] + + ole_type = WIN32OLE_TYPE.new("Microsoft Shell Controls And Automation", "ShellLinkObject") + m_geticonlocation = WIN32OLE_METHOD.new(ole_type, "GetIconLocation") + @param_pbs = m_geticonlocation.params[0] + + ole_type = WIN32OLE_TYPE.new("Microsoft HTML Object Library", "FontNames") + m_count = WIN32OLE_METHOD.new(ole_type, "Count") + @param_p = m_count.params[0] + + ole_type = WIN32OLE_TYPE.new("Microsoft Scripting Runtime", "FileSystemObject") + m_copyfile = WIN32OLE_METHOD.new(ole_type, "CopyFile") + @param_overwritefiles = m_copyfile.params[2] + end + + def test_s_new + assert_raise(ArgumentError) { + WIN32OLE_PARAM.new("hoge") + } + end + + + def test_name + assert_equal('URL', @param_url.name) + assert_equal('Flags', @param_flags.name) + assert_equal('Cancel', @param_cancel.name) + end + + def test_ole_type + assert_equal('BSTR', @param_url.ole_type) + assert_equal('VARIANT', @param_flags.ole_type) + end + + def test_ole_type_detail + assert_equal(['BSTR'], @param_url.ole_type_detail) + assert_equal(['PTR', 'VARIANT'], @param_flags.ole_type_detail) + end + + def test_input? + assert(@param_url.input?) + assert(@param_cancel.input?) + assert(!@param_pbs.input?) + end + + def test_output? + assert(!@param_url.output?) + assert(@param_cancel.output?) + assert(@param_pbs.output?) + end + + def test_optional? + assert(!@param_url.optional?) + assert(@param_flags.optional?) + end + + def test_retval? + assert(!@param_url.retval?) + assert(@param_p.retval?) + end + + def test_default + assert_equal(nil, @param_url.default) + assert_equal(true, @param_overwritefiles.default) + end + + def test_to_s + assert_equal(@param_url.name, @param_url.to_s) + end + + def test_inspect + assert_equal("#<WIN32OLE_PARAM:URL>", @param_url.inspect) + assert_equal("#<WIN32OLE_PARAM:OverWriteFiles=true>", @param_overwritefiles.inspect) + end + end +end diff --git a/trunk/test/win32ole/test_win32ole_type.rb b/trunk/test/win32ole/test_win32ole_type.rb new file mode 100644 index 0000000000..f8ea525ea9 --- /dev/null +++ b/trunk/test/win32ole/test_win32ole_type.rb @@ -0,0 +1,249 @@ +begin + require 'win32ole' +rescue LoadError +end +require "test/unit" + +if defined?(WIN32OLE_TYPE) + class TestWIN32OLE_TYPE < Test::Unit::TestCase + + def test_s_progids + progids = WIN32OLE_TYPE.progids + assert_instance_of(Array, progids) + assert(progids.size > 0) + assert_instance_of(String, progids[0]) + assert(progids.include?("Shell.Application.1")) + end + + def test_initialize + assert_raise(ArgumentError) { + WIN32OLE_TYPE.new + } + assert_raise(ArgumentError) { + WIN32OLE_TYPE.new("foo") + } + assert_raise(TypeError) { + WIN32OLE_TYPE.new(1, 2) + } + assert_raise(TypeError) { + WIN32OLE_TYPE.new("Microsoft Shell Controls And Automation", 1) + } + assert_raise(WIN32OLERuntimeError) { + WIN32OLE_TYPE.new("Microsoft Shell Controls And Automation", "foo") + } + assert_raise(WIN32OLERuntimeError) { + WIN32OLE_TYPE.new("Microsoft Shell Controls And Automation", "Application") + } + ole_type = WIN32OLE_TYPE.new("Microsoft Shell Controls And Automation", "Shell") + assert_instance_of(WIN32OLE_TYPE, ole_type) + assert_equal("Shell", ole_type.name) + assert_equal("Class", ole_type.ole_type) + assert_equal("{13709620-C279-11CE-A49E-444553540000}", ole_type.guid) + assert_equal("Shell.Application.1", ole_type.progid) + assert_equal(true, ole_type.visible?) + assert_equal("Shell", ole_type.to_s) + assert_equal(0, ole_type.major_version) + assert_equal(0, ole_type.minor_version) + assert_equal(5, ole_type.typekind) + assert_equal("Shell Object Type Information", ole_type.helpstring) + assert_equal(nil, ole_type.src_type) + assert_equal("", ole_type.helpfile) + assert_equal(0, ole_type.helpcontext) + assert_equal([], ole_type.variables) + assert(ole_type.ole_methods.select{|m|/NameSpace/i =~ m.name}.size > 0) + + ole_type2 = WIN32OLE_TYPE.new("{13709620-C279-11CE-A49E-444553540000}", "Shell") + assert_instance_of(WIN32OLE_TYPE, ole_type) + assert_equal(ole_type.name, ole_type2.name) + assert_equal(ole_type.ole_type, ole_type2.ole_type) + assert_equal(ole_type.guid, ole_type2.guid) + assert_equal(ole_type.progid, ole_type2.progid) + assert_equal(ole_type.visible?, ole_type2.visible?) + assert_equal(ole_type.to_s, ole_type2.to_s) + assert_equal(ole_type.major_version, ole_type2.major_version) + assert_equal(ole_type.minor_version, ole_type2.minor_version) + assert_equal(ole_type.typekind, ole_type2.typekind) + assert_equal(ole_type.helpstring, ole_type2.helpstring) + assert_equal(ole_type.src_type, ole_type2.src_type) + assert_equal(ole_type.helpfile, ole_type2.helpfile) + assert_equal(ole_type.helpcontext, ole_type2.helpcontext) + assert_equal(ole_type.variables.size, ole_type2.variables.size) + assert_equal(ole_type.ole_methods[0].name, ole_type2.ole_methods[0].name) + assert_equal(ole_type.ole_typelib.name, ole_type2.ole_typelib.name) + assert_equal(ole_type.implemented_ole_types.size, ole_type2.implemented_ole_types.size) + assert_equal(ole_type.inspect, ole_type2.inspect) + end + + def setup + @ole_type = WIN32OLE_TYPE.new("Microsoft Shell Controls And Automation", "Shell") + end + + def test_name + assert_equal("Shell", @ole_type.name) + end + + def test_ole_type + assert_equal("Class", @ole_type.ole_type) + end + + def test_guid + assert_equal("{13709620-C279-11CE-A49E-444553540000}", @ole_type.guid) + end + + def test_progid + assert_equal("Shell.Application.1", @ole_type.progid) + end + + def test_visible? + assert(@ole_type.visible?) + ole_type = WIN32OLE_TYPE.new("Microsoft Shell Controls And Automation", "IShellDispatch") + assert(!ole_type.visible?) + end + + def test_to_s + assert_equal(@ole_type.to_s, @ole_type.name) + end + + def test_major_version + assert_equal(0, @ole_type.major_version) + # ole_type = WIN32OLE_TYPE.new("Microsoft Word 11.0 Object Library", "Documents") + # assert_equal(8, ole_type.major_version) + end + + def test_minor_version + assert_equal(0, @ole_type.minor_version) + # ole_type = WIN32OLE_TYPE.new("Microsoft Word 11.0 Object Library", "Documents") + # assert_equal(3, ole_type.minor_version) + end + + def test_typekind + assert_equal(5, @ole_type.typekind) + end + + def test_helpstring + assert_equal("Shell Object Type Information", @ole_type.helpstring) + end + + def test_src_type + ole_type = WIN32OLE_TYPE.new("Microsoft Scripting Runtime", "DriveTypeConst") + assert_match(/__MIDL___MIDL_itf_scrrun_/, ole_type.src_type) + assert_equal(nil, @ole_type.src_type) + end + + def test_helpfile + assert_equal("", @ole_type.helpfile) + ole_type = WIN32OLE_TYPE.new("Microsoft Scripting Runtime", "Folders") + assert_match(/VBENLR98\.CHM$/i, ole_type.helpfile) + end + + def test_helpcontext + assert_equal(0, @ole_type.helpcontext) + ole_type = WIN32OLE_TYPE.new("Microsoft Scripting Runtime", "Folders") + assert_equal(2181929, ole_type.helpcontext) + end + + def test_variables + variables = @ole_type.variables + assert_instance_of(Array, variables) + assert(variables.size == 0) + + ole_type = WIN32OLE_TYPE.new("Microsoft Shell Controls And Automation", "ShellSpecialFolderConstants") + variables = ole_type.variables + assert_instance_of(Array, variables) + assert(variables.size > 0) + + assert_instance_of(WIN32OLE_VARIABLE, variables[0]) + end + + def test_ole_methods + methods = @ole_type.ole_methods + assert_instance_of(Array, methods) + assert(methods.size > 0) + assert_instance_of(WIN32OLE_METHOD, methods[0]); + assert(methods.collect{|m| m.name}.include?("Application")) + end + + def test_ole_typelib + tlib = @ole_type.ole_typelib + assert_instance_of(WIN32OLE_TYPELIB, tlib) + assert_equal("Microsoft Shell Controls And Automation", tlib.name) + end + + def test_implemented_ole_types + ole_types = @ole_type.implemented_ole_types + assert_instance_of(Array, ole_types) + assert_equal(1, ole_types.size) + assert_match(/^IShellDispatch5{0,1}$/, ole_types[0].name) + + ie_otype = WIN32OLE_TYPE.new("Microsoft Internet Controls", "InternetExplorer") + ole_types = ie_otype.implemented_ole_types + assert_equal(4, ole_types.size) + otype = ole_types.select{|t| t.name == "IWebBrowser2"} + assert_equal(1, otype.size) + otype = ole_types.select{|t| t.name == "IWebBrowserApp"} + assert_equal(1, otype.size) + otype = ole_types.select{|t| t.name == "DWebBrowserEvents2"} + assert_equal(1, otype.size) + otype = ole_types.select{|t| t.name == "DWebBrowserEvents"} + assert_equal(1, otype.size) + end + + def test_default_ole_types + ie_otype = WIN32OLE_TYPE.new("Microsoft Internet Controls", "InternetExplorer") + ole_types = ie_otype.default_ole_types + otype = ole_types.select{|t| t.name == "IWebBrowser2"} + assert_equal(1, otype.size) + otype = ole_types.select{|t| t.name == "IWebBrowserApp"} + assert_equal(0, otype.size) + otype = ole_types.select{|t| t.name == "DWebBrowserEvents2"} + assert_equal(1, otype.size) + otype = ole_types.select{|t| t.name == "DWebBrowserEvents"} + assert_equal(0, otype.size) + end + + def test_source_ole_types + ie_otype = WIN32OLE_TYPE.new("Microsoft Internet Controls", "InternetExplorer") + ole_types = ie_otype.source_ole_types + otype = ole_types.select{|t| t.name == "IWebBrowser2"} + assert_equal(0, otype.size) + otype = ole_types.select{|t| t.name == "IWebBrowserApp"} + assert_equal(0, otype.size) + otype = ole_types.select{|t| t.name == "DWebBrowserEvents2"} + assert_equal(1, otype.size) + otype = ole_types.select{|t| t.name == "DWebBrowserEvents"} + assert_equal(1, otype.size) + end + + def test_default_event_sources + ie_otype = WIN32OLE_TYPE.new("Microsoft Internet Controls", "InternetExplorer") + ole_types = ie_otype.default_event_sources + otype = ole_types.select{|t| t.name == "IWebBrowser2"} + assert_equal(0, otype.size) + otype = ole_types.select{|t| t.name == "IWebBrowserApp"} + assert_equal(0, otype.size) + otype = ole_types.select{|t| t.name == "DWebBrowserEvents2"} + assert_equal(1, otype.size) + otype = ole_types.select{|t| t.name == "DWebBrowserEvents"} + assert_equal(0, otype.size) + end + + def test_inspect + assert_equal("#<WIN32OLE_TYPE:Shell>", @ole_type.inspect) + end + # WIN32OLE_TYPE.typelibs will be obsoleted. + def test_s_typelibs + tlibs = WIN32OLE_TYPE.typelibs.sort + tlibs2 = WIN32OLE_TYPELIB.typelibs.collect{|t|t.name}.sort + assert_equal(tlibs2, tlibs) + end + + # WIN32OLE_TYPE.ole_classes will be obsoleted. + def test_s_ole_classes + ots1 = WIN32OLE_TYPE.ole_classes("Microsoft Shell Controls And Automation") + ots2 = WIN32OLE_TYPELIB.new("Microsoft Shell Controls And Automation").ole_types + otns1 = ots1.collect{|t| t.name}.sort + otns2 = ots2.collect{|t| t.name}.sort + assert_equal(otns2, otns1) + end + end +end diff --git a/trunk/test/win32ole/test_win32ole_typelib.rb b/trunk/test/win32ole/test_win32ole_typelib.rb new file mode 100644 index 0000000000..a1c3891396 --- /dev/null +++ b/trunk/test/win32ole/test_win32ole_typelib.rb @@ -0,0 +1,110 @@ +begin + require 'win32ole' +rescue LoadError +end +require "test/unit" + +if defined?(WIN32OLE_TYPELIB) + class TestWIN32OLE_TYPELIB < Test::Unit::TestCase + def test_s_typelibs + tlibs = WIN32OLE_TYPELIB.typelibs + assert_instance_of(Array, tlibs) + assert(tlibs.size > 0) + tlib = tlibs.find {|tlib| tlib.name == "Microsoft Shell Controls And Automation"} + assert(tlib) + end + + def test_initialize + assert_raise(ArgumentError) { + WIN32OLE_TYPELIB.new(1,2,3,4) + } + + assert_raise(TypeError) { + WIN32OLE_TYPELIB.new(100) + } + + tlib = WIN32OLE_TYPELIB.new("Microsoft Shell Controls And Automation") + assert_instance_of(WIN32OLE_TYPELIB, tlib) + + tlib = WIN32OLE_TYPELIB.new("Microsoft Shell Controls And Automation", 1.0) + assert_instance_of(WIN32OLE_TYPELIB, tlib) + + tlib = WIN32OLE_TYPELIB.new("Microsoft Shell Controls And Automation", 1, 0) + assert_instance_of(WIN32OLE_TYPELIB, tlib) + guid = tlib.guid + + tlib_by_guid = WIN32OLE_TYPELIB.new(guid, 1, 0) + assert_instance_of(WIN32OLE_TYPELIB, tlib_by_guid) + assert_equal("Microsoft Shell Controls And Automation" , tlib_by_guid.name) + + path = tlib.path + tlib_by_path = WIN32OLE_TYPELIB.new(path) + assert_equal("Microsoft Shell Controls And Automation" , tlib_by_path.name) + + assert_raise(WIN32OLERuntimeError) { + WIN32OLE_TYPELIB.new("Non Exist Type Library") + } + end + + def test_guid + tlib = WIN32OLE_TYPELIB.new("Microsoft Shell Controls And Automation") + assert_equal("{50A7E9B0-70EF-11D1-B75A-00A0C90564FE}", tlib.guid) + end + + def test_name + tlib = WIN32OLE_TYPELIB.new("Microsoft Shell Controls And Automation") + assert_equal("Microsoft Shell Controls And Automation", tlib.name) + tlib = WIN32OLE_TYPELIB.new("{50A7E9B0-70EF-11D1-B75A-00A0C90564FE}") + assert_equal("Microsoft Shell Controls And Automation", tlib.name) + end + + def test_version + tlib = WIN32OLE_TYPELIB.new("Microsoft Shell Controls And Automation") + assert_equal(1.0, tlib.version) + end + + def test_major_version + tlib = WIN32OLE_TYPELIB.new("Microsoft Shell Controls And Automation") + assert_equal(1, tlib.major_version) + end + + def test_minor_version + tlib = WIN32OLE_TYPELIB.new("Microsoft Shell Controls And Automation") + assert_equal(0, tlib.minor_version) + end + + def test_path + tlib = WIN32OLE_TYPELIB.new("Microsoft Shell Controls And Automation") + assert_match(/shell32\.dll$/i, tlib.path) + end + + def test_visible? + tlib = WIN32OLE_TYPELIB.new("Microsoft Shell Controls And Automation") + assert(tlib.visible?) + end + + def test_library_name + tlib = WIN32OLE_TYPELIB.new("Microsoft Shell Controls And Automation") + assert_equal("Shell32", tlib.library_name) + end + + def test_to_s + tlib = WIN32OLE_TYPELIB.new("Microsoft Shell Controls And Automation") + assert_equal("Microsoft Shell Controls And Automation", tlib.to_s) + end + + def test_ole_types + tlib = WIN32OLE_TYPELIB.new("Microsoft Shell Controls And Automation") + ole_types = tlib.ole_types + assert_instance_of(Array, ole_types) + assert(ole_types.size > 0) + assert_instance_of(WIN32OLE_TYPE, ole_types[0]) + end + + def test_inspect + tlib = WIN32OLE_TYPELIB.new("Microsoft Shell Controls And Automation") + assert_equal("#<WIN32OLE_TYPELIB:Microsoft Shell Controls And Automation>", tlib.inspect) + end + + end +end diff --git a/trunk/test/win32ole/test_win32ole_variable.rb b/trunk/test/win32ole/test_win32ole_variable.rb new file mode 100644 index 0000000000..43529d3bad --- /dev/null +++ b/trunk/test/win32ole/test_win32ole_variable.rb @@ -0,0 +1,61 @@ +begin + require 'win32ole' +rescue LoadError +end +require "test/unit" + +if defined?(WIN32OLE_VARIABLE) + class TestWIN32OLE_VARIABLE < Test::Unit::TestCase + + def setup + ole_type = WIN32OLE_TYPE.new("Microsoft Shell Controls And Automation", "ShellSpecialFolderConstants") + @var1 = ole_type.variables.find {|v| v.name == 'ssfDESKTOP'} + + variables = WIN32OLE_TYPE.new("Microsoft Windows Installer Object Library", "Installer").variables + @var2 = variables.find {|v| v.name == 'UILevel'} + end + + def test_name + assert_equal('ssfDESKTOP', @var1.name) + end + + def test_ole_type + assert_equal('INT', @var1.ole_type) + assert_equal('MsiUILevel', @var2.ole_type) + end + + def test_ole_type_detail + assert_equal(['INT'], @var1.ole_type_detail) + assert_equal(['USERDEFINED', 'MsiUILevel'], @var2.ole_type_detail) + end + + def test_ole_type_value + assert_equal(0, @var1.value) + assert_equal(nil, @var2.value) + end + + def test_ole_type_visible? + assert(@var1.visible?) + end + + def test_ole_type_variable_kind + assert_equal("CONSTANT", @var1.variable_kind) + assert_equal("DISPATCH", @var2.variable_kind) + end + + def test_ole_type_varkind + assert_equal(2, @var1.varkind) + assert_equal(3, @var2.varkind) + end + + def test_to_s + assert_equal(@var1.name, @var1.to_s) + end + + def test_inspect + assert_equal("#<WIN32OLE_VARIABLE:ssfDESKTOP=0>", @var1.inspect) + assert_equal("#<WIN32OLE_VARIABLE:UILevel=nil>", @var2.inspect) + end + + end +end diff --git a/trunk/test/win32ole/test_win32ole_variant.rb b/trunk/test/win32ole/test_win32ole_variant.rb new file mode 100644 index 0000000000..1226e0de50 --- /dev/null +++ b/trunk/test/win32ole/test_win32ole_variant.rb @@ -0,0 +1,638 @@ +begin + require 'win32ole' +rescue LoadError +end +require "test/unit" + +$MSGS = [] +def add_skip_message(msg) + $MSGS.push msg +end + +if defined?(WIN32OLE_VARIANT) + + class TestWIN32OLE_VARIANT < Test::Unit::TestCase + + def test_s_new + obj = WIN32OLE_VARIANT.new('foo') + assert_instance_of(WIN32OLE_VARIANT, obj) + end + + def test_s_new_no_argument + ex = nil + begin + obj = WIN32OLE_VARIANT.new + rescue ArgumentError + ex = $! + end + assert_instance_of(ArgumentError, ex) + assert_equal("wrong number of arguments (0 for 1..3)", ex.message); + end + + def test_s_new_one_argument + ex = nil + begin + obj = WIN32OLE_VARIANT.new('foo') + rescue + ex = $! + end + assert_equal(nil, ex); + end + + def test_s_new_with_nil + obj = WIN32OLE_VARIANT.new(nil, WIN32OLE::VARIANT::VT_I2) + assert_equal(0, obj.value) + assert_equal(WIN32OLE::VARIANT::VT_I2, obj.vartype) + + obj = WIN32OLE_VARIANT.new(nil, WIN32OLE::VARIANT::VT_I4) + assert_equal(0, obj.value) + assert_equal(WIN32OLE::VARIANT::VT_I4, obj.vartype) + + obj = WIN32OLE_VARIANT.new(nil, WIN32OLE::VARIANT::VT_R4) + assert_equal(0, obj.value) + assert_equal(WIN32OLE::VARIANT::VT_R4, obj.vartype) + + obj = WIN32OLE_VARIANT.new(nil, WIN32OLE::VARIANT::VT_R8) + assert_equal(0, obj.value) + assert_equal(WIN32OLE::VARIANT::VT_R8, obj.vartype) + + obj = WIN32OLE_VARIANT.new(nil, WIN32OLE::VARIANT::VT_CY) + assert_equal("0", obj.value) + assert_equal(WIN32OLE::VARIANT::VT_CY, obj.vartype) + + obj = WIN32OLE_VARIANT.new(nil, WIN32OLE::VARIANT::VT_DATE) + assert_equal("1899/12/30 00:00:00", obj.value) + assert_equal(WIN32OLE::VARIANT::VT_DATE, obj.vartype) + + obj = WIN32OLE_VARIANT.new(nil, WIN32OLE::VARIANT::VT_BSTR) + assert_equal("", obj.value) + assert_equal(WIN32OLE::VARIANT::VT_BSTR, obj.vartype) + + obj = WIN32OLE_VARIANT.new(nil, WIN32OLE::VARIANT::VT_DISPATCH) + assert_nil(obj.value) + assert_equal(WIN32OLE::VARIANT::VT_DISPATCH, obj.vartype) + + obj = WIN32OLE_VARIANT.new(nil, WIN32OLE::VARIANT::VT_BOOL) + assert_equal(false, obj.value) + assert_equal(WIN32OLE::VARIANT::VT_BOOL, obj.vartype) + + obj = WIN32OLE_VARIANT.new(nil, WIN32OLE::VARIANT::VT_VARIANT) + assert_nil(obj.value) + assert_equal(WIN32OLE::VARIANT::VT_VARIANT, obj.vartype) + + obj = WIN32OLE_VARIANT.new(nil, WIN32OLE::VARIANT::VT_I1) + assert_equal(0, obj.value) + assert_equal(WIN32OLE::VARIANT::VT_I1, obj.vartype) + + obj = WIN32OLE_VARIANT.new(nil, WIN32OLE::VARIANT::VT_UI1) + assert_equal(0, obj.value) + assert_equal(WIN32OLE::VARIANT::VT_UI1, obj.vartype) + + obj = WIN32OLE_VARIANT.new(nil, WIN32OLE::VARIANT::VT_UI2) + assert_equal(0, obj.value) + assert_equal(WIN32OLE::VARIANT::VT_UI2, obj.vartype) + + obj = WIN32OLE_VARIANT.new(nil, WIN32OLE::VARIANT::VT_UI4) + assert_equal(0, obj.value) + assert_equal(WIN32OLE::VARIANT::VT_UI4, obj.vartype) + + + if defined?(WIN32OLE::VARIANT::VT_I8) + obj = WIN32OLE_VARIANT.new(nil, WIN32OLE::VARIANT::VT_I8) + assert_equal(0, obj.value) + assert_equal(WIN32OLE::VARIANT::VT_I8, obj.vartype) + end + + if defined?(WIN32OLE::VARIANT::VT_UI8) + obj = WIN32OLE_VARIANT.new(nil, WIN32OLE::VARIANT::VT_UI8) + assert_equal(0, obj.value) + assert_equal(WIN32OLE::VARIANT::VT_UI8, obj.vartype) + end + + obj = WIN32OLE_VARIANT.new(nil, WIN32OLE::VARIANT::VT_INT) + assert_equal(0, obj.value) + assert_equal(WIN32OLE::VARIANT::VT_INT, obj.vartype) + + obj = WIN32OLE_VARIANT.new(nil, WIN32OLE::VARIANT::VT_UINT) + assert_equal(0, obj.value) + assert_equal(WIN32OLE::VARIANT::VT_UINT, obj.vartype) + end + + def test_s_new_with_non_nil + obj = WIN32OLE_VARIANT.new(2, WIN32OLE::VARIANT::VT_I2) + assert_equal(2, obj.value) + assert_equal(WIN32OLE::VARIANT::VT_I2, obj.vartype) + + obj = WIN32OLE_VARIANT.new(3, WIN32OLE::VARIANT::VT_I4) + assert_equal(3, obj.value) + assert_equal(WIN32OLE::VARIANT::VT_I4, obj.vartype) + + obj = WIN32OLE_VARIANT.new(4.5, WIN32OLE::VARIANT::VT_R4) + assert_equal(4.5, obj.value) + assert_equal(WIN32OLE::VARIANT::VT_R4, obj.vartype) + + obj = WIN32OLE_VARIANT.new(5.5, WIN32OLE::VARIANT::VT_R8) + assert_equal(5.5, obj.value) + assert_equal(WIN32OLE::VARIANT::VT_R8, obj.vartype) + + obj = WIN32OLE_VARIANT.new(600, WIN32OLE::VARIANT::VT_CY) + assert_equal("600", obj.value) + assert_equal(WIN32OLE::VARIANT::VT_CY, obj.vartype) + + obj = WIN32OLE_VARIANT.new("2001-06-15 12:17:34", WIN32OLE::VARIANT::VT_DATE) + assert_equal("2001/06/15 12:17:34", obj.value) + assert_equal(WIN32OLE::VARIANT::VT_DATE, obj.vartype) + + obj = WIN32OLE_VARIANT.new("foo", WIN32OLE::VARIANT::VT_BSTR) + assert_equal("foo", obj.value) + assert_equal(WIN32OLE::VARIANT::VT_BSTR, obj.vartype) + + obj = WIN32OLE_VARIANT.new(true, WIN32OLE::VARIANT::VT_BOOL) + assert_equal(true, obj.value) + assert_equal(WIN32OLE::VARIANT::VT_BOOL, obj.vartype) + + obj = WIN32OLE_VARIANT.new(2, WIN32OLE::VARIANT::VT_I1) + assert_equal(2, obj.value) + assert_equal(WIN32OLE::VARIANT::VT_I1, obj.vartype) + + obj = WIN32OLE_VARIANT.new(3, WIN32OLE::VARIANT::VT_UI1) + assert_equal(3, obj.value) + assert_equal(WIN32OLE::VARIANT::VT_UI1, obj.vartype) + + obj = WIN32OLE_VARIANT.new(4, WIN32OLE::VARIANT::VT_UI2) + assert_equal(4, obj.value) + assert_equal(WIN32OLE::VARIANT::VT_UI2, obj.vartype) + + obj = WIN32OLE_VARIANT.new(5, WIN32OLE::VARIANT::VT_UI4) + assert_equal(5, obj.value) + assert_equal(WIN32OLE::VARIANT::VT_UI4, obj.vartype) + + if defined?(WIN32OLE::VARIANT::VT_I8) + obj = WIN32OLE_VARIANT.new(-123456789012345, WIN32OLE::VARIANT::VT_I8) + assert_equal(-123456789012345, obj.value) + assert_equal(WIN32OLE::VARIANT::VT_I8, obj.vartype) + end + + if defined?(WIN32OLE::VARIANT::VT_UI8) + obj = WIN32OLE_VARIANT.new(123456789012345, WIN32OLE::VARIANT::VT_UI8) + assert_equal(123456789012345, obj.value) + assert_equal(WIN32OLE::VARIANT::VT_UI8, obj.vartype) + end + + obj = WIN32OLE_VARIANT.new(4, WIN32OLE::VARIANT::VT_INT) + assert_equal(4, obj.value) + assert_equal(WIN32OLE::VARIANT::VT_INT, obj.vartype) + + obj = WIN32OLE_VARIANT.new(5, WIN32OLE::VARIANT::VT_UINT) + assert_equal(5, obj.value) + assert_equal(WIN32OLE::VARIANT::VT_UINT, obj.vartype) + end + + def test_s_new_with_non_nil_byref + obj = WIN32OLE_VARIANT.new(2, WIN32OLE::VARIANT::VT_I2|WIN32OLE::VARIANT::VT_BYREF) + assert_equal(2, obj.value) + assert_equal(WIN32OLE::VARIANT::VT_I2|WIN32OLE::VARIANT::VT_BYREF, obj.vartype) + + obj = WIN32OLE_VARIANT.new(3, WIN32OLE::VARIANT::VT_I4|WIN32OLE::VARIANT::VT_BYREF) + assert_equal(3, obj.value) + assert_equal(WIN32OLE::VARIANT::VT_I4|WIN32OLE::VARIANT::VT_BYREF, obj.vartype) + + obj = WIN32OLE_VARIANT.new(4.5, WIN32OLE::VARIANT::VT_R4|WIN32OLE::VARIANT::VT_BYREF) + assert_equal(4.5, obj.value) + assert_equal(WIN32OLE::VARIANT::VT_R4|WIN32OLE::VARIANT::VT_BYREF, obj.vartype) + + obj = WIN32OLE_VARIANT.new(5.5, WIN32OLE::VARIANT::VT_R8|WIN32OLE::VARIANT::VT_BYREF) + assert_equal(5.5, obj.value) + assert_equal(WIN32OLE::VARIANT::VT_R8|WIN32OLE::VARIANT::VT_BYREF, obj.vartype) + + obj = WIN32OLE_VARIANT.new(600, WIN32OLE::VARIANT::VT_CY|WIN32OLE::VARIANT::VT_BYREF) + assert_equal("600", obj.value) + assert_equal(WIN32OLE::VARIANT::VT_CY|WIN32OLE::VARIANT::VT_BYREF, obj.vartype) + + obj = WIN32OLE_VARIANT.new("2001-06-15 12:17:34", WIN32OLE::VARIANT::VT_DATE|WIN32OLE::VARIANT::VT_BYREF) + assert_equal("2001/06/15 12:17:34", obj.value) + assert_equal(WIN32OLE::VARIANT::VT_DATE|WIN32OLE::VARIANT::VT_BYREF, obj.vartype) + + obj = WIN32OLE_VARIANT.new("foo", WIN32OLE::VARIANT::VT_BSTR|WIN32OLE::VARIANT::VT_BYREF) + assert_equal("foo", obj.value) + assert_equal(WIN32OLE::VARIANT::VT_BSTR|WIN32OLE::VARIANT::VT_BYREF, obj.vartype) + + obj = WIN32OLE_VARIANT.new(true, WIN32OLE::VARIANT::VT_BOOL|WIN32OLE::VARIANT::VT_BYREF) + assert_equal(true, obj.value) + assert_equal(WIN32OLE::VARIANT::VT_BOOL|WIN32OLE::VARIANT::VT_BYREF, obj.vartype) + + obj = WIN32OLE_VARIANT.new(2, WIN32OLE::VARIANT::VT_I1|WIN32OLE::VARIANT::VT_BYREF) + assert_equal(2, obj.value) + assert_equal(WIN32OLE::VARIANT::VT_I1|WIN32OLE::VARIANT::VT_BYREF, obj.vartype) + + obj = WIN32OLE_VARIANT.new(3, WIN32OLE::VARIANT::VT_UI1|WIN32OLE::VARIANT::VT_BYREF) + assert_equal(3, obj.value) + assert_equal(WIN32OLE::VARIANT::VT_UI1|WIN32OLE::VARIANT::VT_BYREF, obj.vartype) + + obj = WIN32OLE_VARIANT.new(4, WIN32OLE::VARIANT::VT_UI2|WIN32OLE::VARIANT::VT_BYREF) + assert_equal(4, obj.value) + assert_equal(WIN32OLE::VARIANT::VT_UI2|WIN32OLE::VARIANT::VT_BYREF, obj.vartype) + + obj = WIN32OLE_VARIANT.new(5, WIN32OLE::VARIANT::VT_UI4|WIN32OLE::VARIANT::VT_BYREF) + assert_equal(5, obj.value) + assert_equal(WIN32OLE::VARIANT::VT_UI4|WIN32OLE::VARIANT::VT_BYREF, obj.vartype) + + obj = WIN32OLE_VARIANT.new(4, WIN32OLE::VARIANT::VT_INT|WIN32OLE::VARIANT::VT_BYREF) + assert_equal(4, obj.value) + assert_equal(WIN32OLE::VARIANT::VT_INT|WIN32OLE::VARIANT::VT_BYREF, obj.vartype) + + obj = WIN32OLE_VARIANT.new(5, WIN32OLE::VARIANT::VT_UINT|WIN32OLE::VARIANT::VT_BYREF) + assert_equal(5, obj.value) + assert_equal(WIN32OLE::VARIANT::VT_UINT|WIN32OLE::VARIANT::VT_BYREF, obj.vartype) + end + + # This test is failed in cygwin. + # The tagVARIANT definition has no union member pllVal in cygwin. + def test_s_new_with_i8_byref + if defined?(WIN32OLE::VARIANT::VT_I8) && /mswin/ =~ RUBY_PLATFORM + obj = WIN32OLE_VARIANT.new(-123456789012345, WIN32OLE::VARIANT::VT_I8|WIN32OLE::VARIANT::VT_BYREF) + assert_equal(-123456789012345, obj.value) + assert_equal(WIN32OLE::VARIANT::VT_I8|WIN32OLE::VARIANT::VT_BYREF, obj.vartype) + else + STDERR.puts("\n#{__FILE__}:#{__LINE__}:#{self.class.name}.test_s_new_with_i8_byref is skipped") + end + end + + # This test is failed in cygwin. + # The tagVARIANT definition has no union member pullVal in cygwin. + def test_s_new_with_ui8_byref + if defined?(WIN32OLE::VARIANT::VT_UI8) && /mswin/ =~ RUBY_PLATFORM + obj = WIN32OLE_VARIANT.new(123456789012345, WIN32OLE::VARIANT::VT_UI8|WIN32OLE::VARIANT::VT_BYREF) + assert_equal(123456789012345, obj.value) + assert_equal(WIN32OLE::VARIANT::VT_UI8|WIN32OLE::VARIANT::VT_BYREF, obj.vartype) + else + STDERR.puts("\n#{__FILE__}:#{__LINE__}:#{self.class.name}.test_s_new_with_ui8_byref is skipped.") + end + end + + def test_value + obj = WIN32OLE_VARIANT.new('foo') + assert_equal('foo', obj.value) + end + + def test_s_new_2_argument + obj = WIN32OLE_VARIANT.new('foo', WIN32OLE::VARIANT::VT_BSTR|WIN32OLE::VARIANT::VT_BYREF) + assert_equal('foo', obj.value); + end + + def test_s_new_2_argument2 + obj = WIN32OLE_VARIANT.new('foo', WIN32OLE::VARIANT::VT_BSTR) + assert_equal('foo', obj.value); + end + + def test_s_new_dispatch_array + vt = WIN32OLE::VARIANT::VT_ARRAY|WIN32OLE::VARIANT::VT_DISPATCH + obj = WIN32OLE_VARIANT.new(nil, vt) + assert_equal(vt, obj.vartype) + assert_nil(obj.value) + + vt = WIN32OLE::VARIANT::VT_ARRAY|WIN32OLE::VARIANT::VT_DISPATCH|WIN32OLE::VARIANT::VT_BYREF + obj = WIN32OLE_VARIANT.new(nil, vt) + assert_equal(vt, obj.vartype) + assert_nil(obj.value) + end + + def test_s_new_array + # should not occur stack over flow + ar = (1..500000).to_a.map{|i| [i]} + ar2 = WIN32OLE_VARIANT.new(ar) + assert_equal(ar, ar2.value) + end + + def test_s_array + obj = WIN32OLE_VARIANT.array([2,3], WIN32OLE::VARIANT::VT_I4) + assert_instance_of(WIN32OLE_VARIANT, obj) + assert_equal(WIN32OLE::VARIANT::VT_I4|WIN32OLE::VARIANT::VT_ARRAY, obj.vartype) + assert_equal([[0, 0, 0],[0, 0, 0]], obj.value) + + obj = WIN32OLE_VARIANT.array([2,3], WIN32OLE::VARIANT::VT_I4|WIN32OLE::VARIANT::VT_BYREF) + assert_equal(WIN32OLE::VARIANT::VT_I4|WIN32OLE::VARIANT::VT_BYREF|WIN32OLE::VARIANT::VT_ARRAY, obj.vartype) + assert_equal([[0, 0, 0],[0, 0, 0]], obj.value) + + obj = WIN32OLE_VARIANT.array([2,3], WIN32OLE::VARIANT::VT_I4|WIN32OLE::VARIANT::VT_ARRAY) + assert_instance_of(WIN32OLE_VARIANT, obj) + assert_equal(WIN32OLE::VARIANT::VT_I4|WIN32OLE::VARIANT::VT_ARRAY, obj.vartype) + assert_equal([[0, 0, 0],[0, 0, 0]], obj.value) + + assert_equal(0, obj[0,0]) + obj[0,0] = 10 + assert_equal([[10, 0, 0],[0, 0, 0]], obj.value) + obj[0,1] = "13.2" + assert_equal([[10, 13, 0],[0, 0, 0]], obj.value) + + obj = WIN32OLE_VARIANT.array([3, 2], WIN32OLE::VARIANT::VT_VARIANT) + obj[0,0] = 10 + obj[0,1] = "string" + obj[1,0] = 12.735 + assert_equal([[10, "string"],[12.735, nil],[nil,nil]], obj.value) + + obj = WIN32OLE_VARIANT.array([2,3], WIN32OLE::VARIANT::VT_DISPATCH) + assert_equal([[nil, nil, nil],[nil,nil,nil]], obj.value) + + end + + def test_s_array_exc + assert_raise(TypeError) { + obj = WIN32OLE_VARIANT.array(2, WIN32OLE::VARIANT::VT_I4) + } + end + + def test_conversion_num2str + obj = WIN32OLE_VARIANT.new(124, WIN32OLE::VARIANT::VT_BSTR) + assert_equal("124", obj.value); + end + + def test_conversion_float2int + obj = WIN32OLE_VARIANT.new(12.345, WIN32OLE::VARIANT::VT_I4) + assert_equal(12, obj.value) + obj = WIN32OLE_VARIANT.new(12.345, WIN32OLE::VARIANT::VT_I4|WIN32OLE::VARIANT::VT_BYREF) + assert_equal(12, obj.value) + end + + def test_conversion_str2num + obj = WIN32OLE_VARIANT.new("12.345", WIN32OLE::VARIANT::VT_R8) + assert_equal(12.345, obj.value) + end + + def test_conversion_ole_variant2ole_variant + obj = WIN32OLE_VARIANT.new("12.345", WIN32OLE::VARIANT::VT_R4) + obj = WIN32OLE_VARIANT.new(obj, WIN32OLE::VARIANT::VT_I4) + assert_equal(12, obj.value) + end + + def test_conversion_str2date + obj = WIN32OLE_VARIANT.new("2004-12-24 12:24:45", WIN32OLE::VARIANT::VT_DATE) + assert_equal("2004/12/24 12:24:45", obj.value) + end + + def test_conversion_time2date + dt = Time.mktime(2004, 12, 24, 12, 24, 45) + obj = WIN32OLE_VARIANT.new(dt, WIN32OLE::VARIANT::VT_DATE) + assert_equal("2004/12/24 12:24:45", obj.value) + end + + def test_conversion_str2cy + begin + WIN32OLE.locale = 0x0411 # set locale Japanese + obj = WIN32OLE_VARIANT.new("\\10,000", WIN32OLE::VARIANT::VT_CY) + assert_equal("10000", obj.value) + ensure + WIN32OLE.locale = WIN32OLE::LOCALE_SYSTEM_DEFAULT + end + end + + def test_create_vt_array + obj = WIN32OLE_VARIANT.new([1.2, 2.3], WIN32OLE::VARIANT::VT_ARRAY|WIN32OLE::VARIANT::VT_R8) + assert_equal([1.2, 2.3], obj.value) + assert_equal(WIN32OLE::VARIANT::VT_ARRAY|WIN32OLE::VARIANT::VT_R8, obj.vartype) + + obj = WIN32OLE_VARIANT.new([1.2, 2.3], WIN32OLE::VARIANT::VT_ARRAY|WIN32OLE::VARIANT::VT_R8|WIN32OLE::VARIANT::VT_BYREF) + assert_equal([1.2, 2.3], obj.value) + assert_equal(WIN32OLE::VARIANT::VT_ARRAY|WIN32OLE::VARIANT::VT_R8|WIN32OLE::VARIANT::VT_BYREF, obj.vartype) + end + + def test_create_vt_array2 + obj = WIN32OLE_VARIANT.new([1.2, "a"], WIN32OLE::VARIANT::VT_ARRAY) + assert_equal([1.2, "a"], obj.value) + assert_equal(WIN32OLE::VARIANT::VT_ARRAY|WIN32OLE::VARIANT::VT_VARIANT, obj.vartype) + + obj = WIN32OLE_VARIANT.new([1.2, "a"]) + assert_equal([1.2, "a"], obj.value) + assert_equal(WIN32OLE::VARIANT::VT_ARRAY|WIN32OLE::VARIANT::VT_VARIANT, obj.vartype) + end + + + def test_create_vt_nested_array + obj = WIN32OLE_VARIANT.new([[1.2, "a", "b"], [3.4, "C", "D"]], WIN32OLE::VARIANT::VT_ARRAY) + assert_equal([[1.2, "a", "b"], [3.4, "C", "D"]], obj.value) + + obj = WIN32OLE_VARIANT.new([[1.2, "a", "b"], [3.4, "C", "D"]]) + assert_equal([[1.2, "a", "b"], [3.4, "C", "D"]], obj.value) + + obj = WIN32OLE_VARIANT.new([[1.2, "a", "b"], [3.4, "C", "D"], [5.6, "E", "F"]]) + assert_equal([[1.2, "a", "b"], [3.4, "C", "D"], [5.6, "E", "F"]], obj.value) + + obj = WIN32OLE_VARIANT.new([[[1.2], [3.4]], [[5.6], [7.8]], [[9.1],[9.2]]]) + assert_equal([[[1.2], [3.4]], [[5.6], [7.8]], [[9.1],[9.2]]], obj.value) + end + + def test_create_vt_array3 + obj = WIN32OLE_VARIANT.new([]) + assert_equal([], obj.value) + + obj = WIN32OLE_VARIANT.new([[]]) + assert_equal([[]], obj.value) + + obj = WIN32OLE_VARIANT.new([[],[]]) + assert_equal([[],[]], obj.value) + + obj = WIN32OLE_VARIANT.new([], WIN32OLE::VARIANT::VT_ARRAY|WIN32OLE::VARIANT::VT_BYREF) + assert_equal([], obj.value) + + obj = WIN32OLE_VARIANT.new([[]], WIN32OLE::VARIANT::VT_ARRAY|WIN32OLE::VARIANT::VT_BYREF) + assert_equal([[]], obj.value) + + obj = WIN32OLE_VARIANT.new([[],[]], WIN32OLE::VARIANT::VT_ARRAY|WIN32OLE::VARIANT::VT_BYREF) + assert_equal([[],[]], obj.value) + end + + def test_create_vt_array_nil + vartype = WIN32OLE::VARIANT::VT_ARRAY|WIN32OLE::VARIANT::VT_DISPATCH|WIN32OLE::VARIANT::VT_BYREF + obj = WIN32OLE_VARIANT.new(nil, vartype) + assert_nil(obj.value) + assert_equal(vartype, obj.vartype) + + vartype = WIN32OLE::VARIANT::VT_ARRAY|WIN32OLE::VARIANT::VT_DISPATCH + obj = WIN32OLE_VARIANT.new(nil, vartype) + assert_nil(obj.value) + assert_equal(vartype, obj.vartype) + end + + def test_create_vt_array_str + vartype = WIN32OLE::VARIANT::VT_ARRAY|WIN32OLE::VARIANT::VT_BSTR + obj = WIN32OLE_VARIANT.new(["abc", "123"], vartype) + assert_equal(vartype, obj.vartype) + assert_equal(["abc", "123"], obj.value) + + vartype = WIN32OLE::VARIANT::VT_ARRAY|WIN32OLE::VARIANT::VT_BYREF|WIN32OLE::VARIANT::VT_BSTR + obj = WIN32OLE_VARIANT.new(["abc", "123"], vartype) + assert_equal(vartype, obj.vartype) + assert_equal(["abc", "123"], obj.value) + end + + def test_create_vt_array_exc + exc = assert_raise(TypeError) { + obj = WIN32OLE_VARIANT.new(1, WIN32OLE::VARIANT::VT_ARRAY); + } + assert_match(/wrong argument type Fixnum \(expected Array\)/, exc.message) + end + + def test_create_vt_array_str2ui1array + obj = WIN32OLE_VARIANT.new("ABC", WIN32OLE::VARIANT::VT_ARRAY|WIN32OLE::VARIANT::VT_UI1) + assert_equal("ABC", obj.value) + + obj.value = "DEF" + assert_equal("DEF", obj.value) + obj[0] = 71 + assert_equal("GEF", obj.value) + + obj = WIN32OLE_VARIANT.new([65, 0].pack("C*"), WIN32OLE::VARIANT::VT_ARRAY|WIN32OLE::VARIANT::VT_UI1) + assert_equal([65, 0].pack("C*"), obj.value) + + obj = WIN32OLE_VARIANT.new("abc", WIN32OLE::VARIANT::VT_ARRAY|WIN32OLE::VARIANT::VT_UI1|WIN32OLE::VARIANT::VT_BYREF) + assert_equal("abc", obj.value) + obj.value = "DEF" + assert_equal("DEF", obj.value) + assert_equal(WIN32OLE::VARIANT::VT_ARRAY|WIN32OLE::VARIANT::VT_UI1|WIN32OLE::VARIANT::VT_BYREF, obj.vartype) + obj[1] = 71 + assert_equal("DGF", obj.value) + + end + + def test_create_vt_array_int + obj = WIN32OLE_VARIANT.new([65, 0], WIN32OLE::VARIANT::VT_ARRAY|WIN32OLE::VARIANT::VT_UI1) + assert_equal([65, 0].pack("C*"), obj.value) + + obj = WIN32OLE_VARIANT.new([65, 0]) + assert_equal([65, 0], obj.value) + + obj = WIN32OLE_VARIANT.new([65, 0], WIN32OLE::VARIANT::VT_I2|WIN32OLE::VARIANT::VT_ARRAY) + assert_equal([65, 0], obj.value) + + end + + def test_vt_array_bracket + obj = WIN32OLE_VARIANT.new([[1,2,3],[4,5,6]]) + assert_equal(1, obj[0,0]) + assert_equal(2, obj[0,1]) + assert_equal(3, obj[0,2]) + assert_equal(4, obj[1,0]) + assert_equal(5, obj[1,1]) + assert_equal(6, obj[1,2]) + + assert_raise(WIN32OLERuntimeError) { + obj[0,4] + } + assert_raise(WIN32OLERuntimeError) { + obj[0,-1] + } + assert_raise(ArgumentError) { + obj[0] + } + + obj[0,0] = 7 + obj[1,2] = 8 + assert_equal([[7,2,3], [4,5,8]], obj.value) + + assert_raise(WIN32OLERuntimeError) { + obj[0,4] = 9 + } + assert_raise(WIN32OLERuntimeError) { + obj[0,-1] = 10 + } + assert_raise(ArgumentError) { + obj[0] = 11 + } + end + + def test_conversion_vt_date + obj = WIN32OLE_VARIANT.new(-657434, WIN32OLE::VARIANT::VT_DATE) + assert_equal("0100/01/01 00:00:00", obj.value) + + obj = WIN32OLE_VARIANT.new("1500/12/29 23:59:59", WIN32OLE::VARIANT::VT_DATE) + assert_equal("1500/12/29 23:59:59", obj.value) + + obj = WIN32OLE_VARIANT.new("1500/12/30 00:00:00", WIN32OLE::VARIANT::VT_DATE) + assert_equal("1500/12/30 00:00:00", obj.value) + + obj = WIN32OLE_VARIANT.new("1500/12/30 00:00:01", WIN32OLE::VARIANT::VT_DATE) + assert_equal("1500/12/30 00:00:01", obj.value) + + obj = WIN32OLE_VARIANT.new("1899/12/29 23:59:59", WIN32OLE::VARIANT::VT_DATE) + assert_equal("1899/12/29 23:59:59", obj.value) + + obj = WIN32OLE_VARIANT.new("1899/12/30 00:00:00", WIN32OLE::VARIANT::VT_DATE) + assert_equal("1899/12/30 00:00:00", obj.value) + + obj = WIN32OLE_VARIANT.new("1899/12/30 00:00:01", WIN32OLE::VARIANT::VT_DATE) + assert_equal("1899/12/30 00:00:01", obj.value) + + obj = WIN32OLE_VARIANT.new(0, WIN32OLE::VARIANT::VT_DATE) + assert_equal("1899/12/30 00:00:00", obj.value) + + obj = WIN32OLE_VARIANT.new("2008/12/29 23:59:59", WIN32OLE::VARIANT::VT_DATE) + assert_equal("2008/12/29 23:59:59", obj.value) + + obj = WIN32OLE_VARIANT.new("2008/12/30 00:00:00", WIN32OLE::VARIANT::VT_DATE) + assert_equal("2008/12/30 00:00:00", obj.value) + + obj = WIN32OLE_VARIANT.new("2008/12/30 00:00:01", WIN32OLE::VARIANT::VT_DATE) + assert_equal("2008/12/30 00:00:01", obj.value) + + obj = WIN32OLE_VARIANT.new("9999/12/31 23:59:59", WIN32OLE::VARIANT::VT_DATE) + assert_equal("9999/12/31 23:59:59", obj.value) + end + + def test_create_nil_dispatch + var = WIN32OLE_VARIANT.new(nil, WIN32OLE::VARIANT::VT_DISPATCH) + assert_nil(var.value) + end + + def test_create_variant_byref + obj = WIN32OLE_VARIANT.new("Str", WIN32OLE::VARIANT::VT_VARIANT|WIN32OLE::VARIANT::VT_BYREF); + assert_equal("Str", obj.value); + end + + def test_vartype + obj = WIN32OLE_VARIANT.new("Str") + assert_equal(WIN32OLE::VARIANT::VT_BSTR, obj.vartype) + end + + def test_set_value + obj = WIN32OLE_VARIANT.new(10) + obj.value = 12 + assert_equal(12, obj.value) + assert_equal(WIN32OLE::VARIANT::VT_I4, obj.vartype) + obj.value = "14" + assert_equal(14, obj.value) + assert_equal(WIN32OLE::VARIANT::VT_I4, obj.vartype) + obj.value = 11.2 + assert_equal(11, obj.value) + assert_equal(WIN32OLE::VARIANT::VT_I4, obj.vartype) + + obj = WIN32OLE_VARIANT.new([1,2]) + assert_raise(WIN32OLERuntimeError) { + obj.value = [3,4] + } + + obj = WIN32OLE_VARIANT.new("2007/01/01", WIN32OLE::VARIANT::VT_DATE) + assert_raise(WIN32OLERuntimeError) { + obj.value = "hogehoge" + } + assert_equal("2007/01/01 00:00:00", obj.value) + + obj2 = WIN32OLE_VARIANT.new("2006/01/01", WIN32OLE::VARIANT::VT_DATE) + obj.value = obj2 + assert_equal("2006/01/01 00:00:00", obj.value) + end + + def test_c_nothing + assert_nil(WIN32OLE_VARIANT::Nothing.value) + end + + def test_c_empty + assert_nil(WIN32OLE_VARIANT::Empty.value) + end + + def test_c_null + assert_nil(WIN32OLE_VARIANT::Null.value) + end + + end +end + diff --git a/trunk/test/win32ole/test_win32ole_variant_m.rb b/trunk/test/win32ole/test_win32ole_variant_m.rb new file mode 100644 index 0000000000..b3e48c20f0 --- /dev/null +++ b/trunk/test/win32ole/test_win32ole_variant_m.rb @@ -0,0 +1,35 @@ +begin + require 'win32ole' +rescue LoadError +end +require "test/unit" + +if defined?(WIN32OLE::VARIANT) + class TestWin32OLE_VARIANT_MODULE < Test::Unit::TestCase + include WIN32OLE::VARIANT + def test_variant + assert_equal(0, VT_EMPTY) + assert_equal(1, VT_NULL) + assert_equal(2, VT_I2) + assert_equal(3, VT_I4) + assert_equal(4, VT_R4) + assert_equal(5, VT_R8) + assert_equal(6, VT_CY) + assert_equal(7, VT_DATE) + assert_equal(8, VT_BSTR) + assert_equal(9, VT_DISPATCH) + assert_equal(10, VT_ERROR) + assert_equal(11, VT_BOOL) + assert_equal(12, VT_VARIANT) + assert_equal(13, VT_UNKNOWN) + assert_equal(16, VT_I1) + assert_equal(17, VT_UI1) + assert_equal(18, VT_UI2) + assert_equal(19, VT_UI4) + assert_equal(22, VT_INT) + assert_equal(23, VT_UINT) + assert_equal(0x2000, VT_ARRAY) + assert_equal(0x4000, VT_BYREF) + end + end +end diff --git a/trunk/test/win32ole/test_win32ole_variant_with_ie.rb b/trunk/test/win32ole/test_win32ole_variant_with_ie.rb new file mode 100644 index 0000000000..680f17ff54 --- /dev/null +++ b/trunk/test/win32ole/test_win32ole_variant_with_ie.rb @@ -0,0 +1,53 @@ +# This is test script to check WIN32OLE_VARIANT using Internet Explorer +begin + require 'win32ole' +rescue LoadError +end +require 'test/unit' + +if defined?(WIN32OLE) + class TestWIN32OLE_VARIANT_WITH_IE < Test::Unit::TestCase + 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 setup + @f = create_temp_html + @ie = WIN32OLE.new('InternetExplorer.Application') + @ie.visible = true + @ie.navigate("file:///#{@f}") + while @ie.busy + sleep 0.5 + end + end + def test_variant_ref_and_argv + @ie.execWB(19, 0, nil, -1) + size = WIN32OLE::ARGV[3] + assert(size >= 0) + + obj = WIN32OLE_VARIANT.new(nil, WIN32OLE::VARIANT::VT_VARIANT|WIN32OLE::VARIANT::VT_BYREF) + @ie.execWb(19, 0, nil, obj) + assert_equal(size, obj.value) + assert_equal(size, WIN32OLE::ARGV[3]) + + obj = WIN32OLE_VARIANT.new(-1, WIN32OLE::VARIANT::VT_VARIANT|WIN32OLE::VARIANT::VT_BYREF) + @ie.execWb(19, 0, nil, obj) + assert_equal(size, obj.value) + assert_equal(size, WIN32OLE::ARGV[3]) + end + + def teardown + File.unlink(@f) + if @ie + @ie.quit + @ie = nil + end + end + end +end diff --git a/trunk/test/win32ole/test_word.rb b/trunk/test/win32ole/test_word.rb new file mode 100644 index 0000000000..d5b2d91812 --- /dev/null +++ b/trunk/test/win32ole/test_word.rb @@ -0,0 +1,72 @@ +# +# This is test for [ruby-Bugs#3237] +# +begin + require 'win32ole' +rescue LoadError +end +require "test/unit" + +def word_installed? + installed = false + w = nil + if defined?(WIN32OLE) + begin + w = WIN32OLE.new('Word.Application') + installed = true + rescue + ensure + if w + w.quit + w = nil + end + end + end + return installed +end + +if defined?(WIN32OLE) + w = nil + dotest = word_installed? + if !dotest + STDERR.puts("\n#{__FILE__} skipped(Microsoft Word not found.)") + end + if dotest + class TestWIN32OLE_WITH_WORD < Test::Unit::TestCase + def setup + begin + @obj = WIN32OLE.new('Word.Application') + rescue WIN32OLERuntimeError + @obj = nil + if !$skipped + $skipped = true + end + end + end + + def test_ole_methods + if @obj + @obj.visible = true + @obj.wordbasic.disableAutoMacros(true) + assert(true) + else + end + end + + def test_s_connect + if @obj + obj2 = WIN32OLE.connect("Word.Application") + assert_instance_of(WIN32OLE, obj2) + obj2.visible = true + end + end + + def teardown + if @obj + @obj.quit + @obj = nil + end + end + end + end +end |