1 38 package com.gargoylesoftware.htmlunit.javascript; 39 40 import java.util.ArrayList; 41 import java.util.Collections; 42 import java.util.List; 43 44 import com.gargoylesoftware.htmlunit.WebTestCase; 45 import com.gargoylesoftware.htmlunit.html.HtmlPage; 46 47 48 49 55 public class ThreadTest extends WebTestCase { 56 57 62 public ThreadTest(final String name) { 63 super(name); 64 } 65 66 67 70 public void testJavaScriptEngineInMultipleThreads() throws InterruptedException { 71 72 final TestThread thread1 = new TestThread("thread1"); 73 final TestThread thread2 = new TestThread("thread2"); 74 final TestThread thread3 = new TestThread("thread3"); 75 final TestThread thread4 = new TestThread("thread4"); 76 77 thread1.start(); 78 thread2.start(); 79 thread3.start(); 80 thread4.start(); 81 82 thread1.join(); 83 thread2.join(); 84 thread3.join(); 85 thread4.join(); 86 87 assertTrue("thread1 not successful", thread1.isSuccessful()); 88 assertTrue("thread2 not successful", thread2.isSuccessful()); 89 assertTrue("thread3 not successful", thread3.isSuccessful()); 90 assertTrue("thread4 not successful", thread4.isSuccessful()); 91 } 92 93 94 95 private static class TestThread extends Thread { 96 97 private boolean successful_ = false; 98 99 103 public TestThread(final String name) { 104 super(name); 105 } 106 107 108 public void run() { 109 try { 110 testCallInheritedFunction(); 111 successful_ = true; 112 } 113 catch (final Exception e) { 114 System.err.println(">>> Thread " + getName()); 115 e.printStackTrace(System.err); 116 successful_ = false; 117 } 118 } 119 120 121 public boolean isSuccessful() { 122 return successful_; 123 } 124 125 126 130 public void testCallInheritedFunction() throws Exception { 131 final String content 132 = "<html><head><title>foo</title><script>" 133 + "function doTest() {\n" 134 + " document.form1.textfield1.focus();\n" 135 + " alert('past focus');\n" 136 + "}\n" 137 + "</script></head><body onload='doTest()'>" 138 + "<p>hello world</p>" 139 + "<form name='form1'>" 140 + " <input type='text' name='textfield1' id='textfield1' value='foo' />" 141 + "</form>" 142 + "</body></html>"; 143 144 final List collectedAlerts = new ArrayList(); 145 final HtmlPage page = loadPage(content, collectedAlerts); 146 147 assertEquals("foo", page.getTitleText()); 148 assertEquals("focus not changed to textfield1", 149 page.getFormByName("form1").getInputByName("textfield1"), 150 page.getWebClient().getElementWithFocus()); 151 final List expectedAlerts = Collections.singletonList("past focus"); 152 assertEquals(expectedAlerts, collectedAlerts); 153 } 154 } 155 156 } 157 | Popular Tags |