1 38 package com.gargoylesoftware.htmlunit.javascript; 39 40 import java.io.File; 41 import java.util.ArrayList; 42 import java.util.Collections; 43 import java.util.HashSet; 44 import java.util.List; 45 import java.util.Map; 46 import java.util.Set; 47 import java.util.TreeSet; 48 49 import com.gargoylesoftware.htmlunit.CollectingAlertHandler; 50 import com.gargoylesoftware.htmlunit.MockWebConnection; 51 import com.gargoylesoftware.htmlunit.ScriptException; 52 import com.gargoylesoftware.htmlunit.WebClient; 53 import com.gargoylesoftware.htmlunit.WebTestCase; 54 import com.gargoylesoftware.htmlunit.html.HtmlPage; 55 56 66 public class SimpleScriptableTest extends WebTestCase { 67 71 public SimpleScriptableTest( final String name ) { 72 super(name); 73 } 74 75 76 79 public void testCallInheritedFunction() throws Exception { 80 final WebClient client = new WebClient(); 81 final MockWebConnection webConnection = new MockWebConnection( client ); 82 83 final String content 84 = "<html><head><title>foo</title><script>" 85 + "function doTest() {\n" 86 + " document.form1.textfield1.focus();\n" 87 + " alert('past focus');\n" 88 + "}\n" 89 + "</script></head><body onload='doTest()'>" 90 + "<p>hello world</p>" 91 + "<form name='form1'>" 92 + " <input type='text' name='textfield1' id='textfield1' value='foo' />" 93 + "</form>" 94 + "</body></html>"; 95 96 webConnection.setDefaultResponse( content ); 97 client.setWebConnection( webConnection ); 98 99 final List expectedAlerts = Collections.singletonList("past focus"); 100 createTestPageForRealBrowserIfNeeded(content, expectedAlerts); 101 102 final List collectedAlerts = new ArrayList(); 103 client.setAlertHandler( new CollectingAlertHandler(collectedAlerts) ); 104 105 final HtmlPage page = ( HtmlPage )client.getPage(URL_GARGOYLE); 106 assertEquals("foo", page.getTitleText()); 107 assertEquals("focus not changed to textfield1", 108 page.getFormByName("form1").getInputByName("textfield1"), 109 page.getWebClient().getElementWithFocus()); 110 assertEquals( expectedAlerts, collectedAlerts ); 111 112 } 113 114 116 public void testHtmlJavaScriptMapping_AllJavaScriptClassesArePresent() { 117 final Map map = SimpleScriptable.getHtmlJavaScriptMapping(); 118 final String directoryName = "../../src/java/com/gargoylesoftware/htmlunit/javascript/host"; 119 final Set names = getFileNames(directoryName.replace('/', File.separatorChar)); 120 121 names.remove("CharacterDataImpl"); 123 names.remove("Document"); 124 names.remove("Event"); 125 names.remove("EventHandler"); 126 names.remove("History"); 127 names.remove("Location"); 128 names.remove("Navigator"); 129 names.remove("NodeImpl"); 130 names.remove("Screen"); 131 names.remove("Style"); 132 names.remove("ActiveXObject"); 133 names.remove("Window"); 134 names.remove("Attribute"); 135 names.remove("ScoperFunctionObject"); 136 names.remove("RowContainer"); 137 names.remove("FormField"); 138 139 assertEquals( new TreeSet(names), new TreeSet(map.values()) ); 140 } 141 142 private Set getFileNames( final String directoryName ) { 143 File directory = new File("."+File.separatorChar+directoryName); 144 if( directory.exists() == false ) { 145 directory = new File("./src/java/".replace('/', File.separatorChar)+directoryName ); 146 } 147 assertTrue("directory exists", directory.exists() ); 148 assertTrue("is a directory", directory.isDirectory() ); 149 150 final String fileNames[] = directory.list(); 151 final Set collection = new HashSet(); 152 153 for( int i=0; i<fileNames.length; i++ ) { 154 final String name = fileNames[i]; 155 if( name.endsWith(".java") ) { 156 collection.add( name.substring(0, name.length()-5) ); 157 } 158 } 159 return collection; 160 } 161 162 165 public void testSetNonWritableProperty() throws Exception { 166 final String content 167 = "<html><head><title>foo</title></head><body onload='document.body=123456'>" 168 + "</body></html>"; 169 170 try { 171 loadPage(content); 172 fail("Exception should have been thrown"); 173 } 174 catch (final ScriptException e) { 175 } 177 } 178 } 179 | Popular Tags |