23
23
*/
24
24
package com .sun .jna .platform .win32 ;
25
25
26
+ import com .sun .jna .Pointer ;
27
+ import com .sun .jna .platform .win32 .COM .COMLateBindingObject ;
28
+ import com .sun .jna .platform .win32 .COM .COMUtils ;
29
+ import com .sun .jna .platform .win32 .COM .Dispatch ;
30
+ import com .sun .jna .platform .win32 .COM .IDispatch ;
31
+ import com .sun .jna .ptr .PointerByReference ;
26
32
import java .util .Map ;
27
33
28
34
import org .junit .After ;
@@ -37,13 +43,41 @@ public static void main(String[] args) {
37
43
jUnitCore .run (WininetUtilTest .class );
38
44
}
39
45
46
+ private static final Guid .CLSID CLSID_InternetExplorer = new Guid .CLSID ("{0002DF01-0000-0000-C000-000000000046}" );
47
+
48
+ private PointerByReference ieApp ;
49
+ private Dispatch ieDispatch ;
50
+
40
51
@ Before
41
52
public void setUp () throws Exception {
42
53
// Launch IE in a manner that should ensure it opens even if the system
43
54
// default browser is Chrome, Firefox, or something else.
44
55
// Launching IE to a page ensures there will be content in the WinInet
45
56
// cache.
46
- Runtime .getRuntime ().exec ("cmd /c start iexplore.exe -nomerge -nohome \" http://www.google.com\" " );
57
+ WinNT .HRESULT hr ;
58
+
59
+ hr = Ole32 .INSTANCE .CoInitializeEx (Pointer .NULL , Ole32 .COINIT_MULTITHREADED );
60
+ COMUtils .checkRC (hr );
61
+
62
+ // IE can not be launched directly anymore - so load it via COM
63
+
64
+ ieApp = new PointerByReference ();
65
+ hr = Ole32 .INSTANCE
66
+ .CoCreateInstance (CLSID_InternetExplorer , null , WTypes .CLSCTX_SERVER , IDispatch .IID_IDISPATCH , ieApp );
67
+ COMUtils .checkRC (hr );
68
+
69
+ ieDispatch = new Dispatch (ieApp .getValue ());
70
+ LocalLateBinding ie = new LocalLateBinding (ieDispatch );
71
+
72
+ ie .setProperty ("Visible" , true );
73
+
74
+ Variant .VARIANT url = new Variant .VARIANT ("http://www.google.com" );
75
+ Variant .VARIANT result = ie .invoke ("Navigate" , url );
76
+ OleAuto .INSTANCE .VariantClear (url );
77
+ OleAuto .INSTANCE .VariantClear (result );
78
+
79
+ ieDispatch .Release ();
80
+ Ole32 .INSTANCE .CoUninitialize ();
47
81
48
82
// There's no easy way to monitor IE and see when it's done loading
49
83
// google.com, so just give it 10 seconds.
@@ -77,6 +111,24 @@ public void testGetCache() throws Exception {
77
111
@ After
78
112
public void tearDown () throws Exception {
79
113
// only kill the freshly opened Google window, unless someone has two IE windows open to Google.
80
- Runtime .getRuntime ().exec ("taskkill.exe /f /im iexplore.exe /fi \" windowtitle eq Google*\" " );
114
+ Runtime .getRuntime ().exec ("taskkill.exe /f /im iexplore.exe" );
115
+ }
116
+
117
+ private static class LocalLateBinding extends COMLateBindingObject {
118
+
119
+ public LocalLateBinding (IDispatch iDispatch ) {
120
+ super (iDispatch );
121
+ }
122
+
123
+ @ Override
124
+ public void setProperty (String propertyName , boolean value ) {
125
+ super .setProperty (propertyName , value );
126
+ }
127
+
128
+ @ Override
129
+ public Variant .VARIANT invoke (String methodName , Variant .VARIANT arg ) {
130
+ return super .invoke (methodName , arg );
131
+ }
132
+
81
133
}
82
134
}
0 commit comments