1 19 20 package org.openide.awt; 21 22 import java.awt.Color ; 23 import java.awt.Component ; 24 import java.awt.Font ; 25 import java.awt.Graphics ; 26 import java.awt.image.BufferedImage ; 27 import java.io.IOException ; 28 import java.io.InputStream ; 29 import java.io.StringBufferInputStream ; 30 import java.io.StringReader ; 31 import java.lang.reflect.InvocationTargetException ; 32 import java.net.HttpURLConnection ; 33 import java.net.URL ; 34 import java.net.URLConnection ; 35 import java.net.URLStreamHandler ; 36 import java.util.concurrent.Semaphore ; 37 import javax.swing.JFrame ; 38 import javax.swing.SwingUtilities ; 39 import junit.framework.TestCase; 40 41 45 public class SwingBrowserTest extends TestCase { 46 47 public SwingBrowserTest(String testName) { 48 super(testName); 49 } 50 51 protected void setUp() throws Exception { 52 53 } 54 55 public void testSimpleUseOfBrowser() throws Exception { 56 System.out.println("testSimpleUseOfBrowser"); 57 final HtmlBrowser.Impl impl = new SwingBrowserImpl(); 59 final JFrame f = new JFrame (); 60 final URL url = new URL ("test", "localhost", -1, "simple", new MyStreamHandler()); 61 62 SwingUtilities.invokeAndWait(new Runnable () { 63 public void run() { 64 Component comp = impl.getComponent(); 65 f.add(comp); 66 f.setVisible(true); 67 impl.setURL(url); 68 } 69 }); 70 waitForLoading(url, f, impl); 71 72 } 73 74 public void testDeadlockWithDebug() throws Exception { 75 System.out.println("testDeadlockWithDebug"); 76 String oldPropValue = System.getProperty("org.openide.awt.SwingBrowserImpl.do-not-block-awt"); 78 System.setProperty("org.openide.awt.SwingBrowserImpl.do-not-block-awt", "true"); 79 80 final HtmlBrowser.Impl impl = new SwingBrowserImpl(); 81 final JFrame f = new JFrame (); 82 final Semaphore s = new Semaphore (1); 83 final URL url = new URL ("test", "localhost", -1, "simple", new MyStreamHandler(s, null)); 84 85 s.acquire(); 86 SwingUtilities.invokeAndWait(new Runnable () { 87 public void run() { 88 Component comp = impl.getComponent(); 89 f.add(comp); 90 f.setVisible(true); 91 impl.setURL(url); 92 } 93 }); 94 System.out.println("browser visible, URL set"); 95 97 SwingUtilities.invokeAndWait(new Runnable () { 100 public void run() { 101 impl.reloadDocument(); 102 } 103 }); 104 System.out.println("reload called"); 105 s.release(); 106 System.setProperty("org.openide.awt.SwingBrowserImpl.do-not-block-awt", (oldPropValue != null)? oldPropValue: "false"); 107 108 waitForLoading(url, f, impl); 109 } 110 111 public void testDeadlockWithDebugWithoutProperty() throws Exception { 112 System.out.println("testDeadlockWithDebug"); 113 115 final HtmlBrowser.Impl impl = new SwingBrowserImpl(); 116 final JFrame f = new JFrame (); 117 final Semaphore s = new Semaphore (1); 118 final URL url = new URL ("test", "localhost", -1, "simple", new MyStreamHandler(s, null)); 119 120 s.acquire(); 121 SwingUtilities.invokeAndWait(new Runnable () { 122 public void run() { 123 Component comp = impl.getComponent(); 124 f.add(comp); 125 f.setVisible(true); 126 impl.setURL(url); 127 } 128 }); 129 System.out.println("browser visible, URL set"); 130 132 SwingUtilities.invokeAndWait(new Runnable () { 135 public void run() { 136 impl.reloadDocument(); 137 } 138 }); 139 System.out.println("reload called"); 140 s.release(); 141 142 waitForLoading(url, f, impl); 143 } 144 145 public void testDeadlockWithJdk6() throws Exception { 146 System.out.println("testDeadlockWithJdk6"); 147 final HtmlBrowser.Impl impl = new SwingBrowserImpl(); 150 final JFrame f = new JFrame (); 151 final Semaphore s = new Semaphore (1); 152 final Semaphore s2 = new Semaphore (1); 153 final URL url = new URL ("test", "localhost", -1, "simple", new MyStreamHandler(s, s2)); 154 final URL url2 = new URL ("test", "localhost", -1, "simple2", new MyStreamHandler(null, null)); 155 156 s.acquire(); 157 SwingUtilities.invokeAndWait(new Runnable () { 158 public void run() { 159 Component comp = impl.getComponent(); 160 f.add(comp); 161 f.setVisible(true); 162 impl.setURL(url); 163 } 164 }); 165 System.out.println("browser visible, URL set"); 166 168 SwingUtilities.invokeAndWait(new Runnable () { 169 public void run() { 170 System.out.println("before 2nd setURL"); 171 s.release(); 173 try { 174 s2.acquire(); 175 } catch (InterruptedException ex) { 176 ex.printStackTrace(); 177 fail(ex.getMessage()); 178 } 179 180 impl.setURL(url2); 181 s2.release(); 182 System.out.println("after 2nd setURL"); 183 } 184 }); 185 System.out.println("new URL requested"); 186 187 SwingUtilities.invokeAndWait(new Runnable () { 188 public void run() { 189 impl.getURL(); 190 } 191 }); 192 System.out.println("getURL called"); 193 waitForLoading(url2, f, impl); 194 } 195 196 private void waitForLoading(final URL url, final JFrame f, final HtmlBrowser.Impl impl) 197 throws InvocationTargetException , InterruptedException { 198 199 for (int i = 0; i < 10 && f.isVisible(); i++) { 200 SwingUtilities.invokeAndWait(new Runnable () { 201 public void run() { 202 URL current = impl.getURL(); 203 if (url.equals(current)) { 204 f.setVisible(false); 205 f.dispose(); 206 } 207 } 208 }); 209 Thread.sleep(i*100); 210 } 211 } 212 213 private static class MyStreamHandler extends URLStreamHandler { 214 private Semaphore sIn; 215 private Semaphore sOut; 216 217 MyStreamHandler() {} 218 MyStreamHandler(Semaphore s, Semaphore s2) { 219 sIn = s; 220 sOut = s2; 221 try { 222 if (sOut != null) { 223 sOut.acquire(); 224 } 225 } catch (InterruptedException ex) { 226 ex.printStackTrace(); 227 fail(ex.getMessage()); 228 } 229 } 230 231 protected URLConnection openConnection(URL u) throws IOException { 232 return new MyConnection(sIn, sOut, u); 233 } 234 } 235 236 private static class MyConnection extends HttpURLConnection { 237 private Semaphore sIn; 238 private Semaphore sOut; 239 240 protected MyConnection(Semaphore s, Semaphore s2, URL u) { 241 super(u); 242 sIn = s; 243 sOut = s2; 244 } 245 246 public void connect() throws IOException { 247 } 248 249 public InputStream getInputStream() throws IOException { 250 return new StringBufferInputStream ("blabla"); 251 } 252 253 public void disconnect() { 254 } 256 257 public boolean usingProxy() { 258 return false; 259 } 260 261 public int getResponseCode() throws IOException { 262 System.out.println("connecting "+toString()+" ... isEDT = "+SwingUtilities.isEventDispatchThread()); 263 if (sIn != null) { 265 try { 266 sIn.acquire(); 267 if (sOut != null) { 268 sOut.release(); 269 } 270 System.out.println("aquired in lock, released out "+toString()); 271 } catch (InterruptedException ex) { 272 ex.printStackTrace(); 273 fail(ex.getMessage()); 274 } 275 sIn.release(); 276 } 277 System.out.println("... connected "+toString()); 278 279 return super.getResponseCode(); 280 } 281 282 public String toString() { 283 return "MyConnection ["+url.toExternalForm()+" sIn "+sIn+" sOut "+sOut+"]"; 284 } 285 286 287 } 288 } 289 | Popular Tags |