1 38 package com.gargoylesoftware.htmlunit.html; 39 40 import java.util.ArrayList; 41 import java.util.Arrays; 42 import java.util.List; 43 44 import com.gargoylesoftware.htmlunit.WebTestCase; 45 46 47 48 54 public class FocusableElementTest extends WebTestCase { 55 56 private static final String COMMON_ID = " id='focusId'"; 57 private static final String COMMON_EVENTS = " onblur=\"alert('foo onblur')\" onfocus=\"alert('foo onfocus')\""; 58 private static final String COMMON_ATTRIBUTES = COMMON_ID + COMMON_EVENTS; 59 60 64 public FocusableElementTest(final String name) { 65 super(name); 66 } 67 68 69 77 private void onClickPageTest(final String htmlContent) throws Exception { 78 79 final List collectedAlerts = new ArrayList(); 80 final HtmlPage page = loadPage(htmlContent, collectedAlerts); 81 82 final FocusableElement element = (FocusableElement) page.getHtmlElementById("focusId"); 83 84 element.focus(); 85 element.blur(); 86 87 final List expectedAlerts = 88 Arrays.asList(new String[]{"foo onfocus", "foo onblur", "foo onfocus", "foo onblur"}); 89 assertEquals(expectedAlerts, collectedAlerts); 90 } 91 92 93 99 private void onClickBodyTest(final String htmlBodyContent) throws Exception { 100 onClickPageTest( 101 "<html><head><title>foo</title></head><body>" + 102 htmlBodyContent + 103 "<script type=\"text/javascript\" language=\"JavaScript\">\n" + 104 "<!--\n" + 105 "document.getElementById('focusId').focus();\n" + 106 "document.getElementById('focusId').blur();\n" + 107 "// -->\n" + 108 "</script></body></html>"); 109 } 110 111 112 119 private void onClickSimpleTest(final String tagName, final String tagAttributes) throws Exception { 120 onClickBodyTest( 121 "<" + tagName + COMMON_ATTRIBUTES + 122 " " + tagAttributes + ">Text</" + tagName + ">"); 123 } 124 125 126 131 public void testAnchor_onblur_onfocus() throws Exception { 132 onClickSimpleTest("a", "href=\".\""); 133 } 134 135 136 141 public void testArea_onblur_onfocus() throws Exception { 142 onClickBodyTest( 143 "<map><area " + COMMON_ATTRIBUTES + 144 " shape=\"rect\" coords=\"0,0,1,1\" HREF=\".\">" + 145 "</area></map>"); 146 } 147 148 149 154 public void testButton_onblur_onfocus() throws Exception { 155 onClickSimpleTest("button", "name=\"foo\" value=\"bar\" type=\"button\""); 156 } 157 158 159 164 public void testLabelContainsInput_onblur_onfocus() throws Exception { 165 onClickBodyTest( 166 "<form><label " + COMMON_ID + ">" + 167 "Foo<input type=\"text\" name=\"foo\"" + COMMON_EVENTS + "></label></form>"); 168 } 169 170 171 176 public void testLabelReferencesInput_onblur_onfocus() throws Exception { 177 onClickBodyTest( 178 "<form><label " + COMMON_ID + " for=\"fooId\">Foo</label>" + 179 "<input type=\"text\" name=\"foo\" id=\"fooId\"" + COMMON_EVENTS + "></form>"); 180 } 181 182 183 188 public void testSelect_onblur_onfocus() throws Exception { 189 onClickBodyTest("<form><select " + COMMON_ATTRIBUTES + "><option>1</option></select></form>"); 190 } 191 192 193 198 public void testTextarea_onblur_onfocus() throws Exception { 199 onClickBodyTest("<form><textarea " + COMMON_ATTRIBUTES + ">Text</textarea></form>"); 200 } 201 } 202 | Popular Tags |