1 38 package com.gargoylesoftware.htmlunit.javascript.host; 39 40 import java.io.IOException; 41 import java.util.ArrayList; 42 import java.util.Collections; 43 import java.util.List; 44 45 import com.gargoylesoftware.htmlunit.WebTestCase; 46 import com.gargoylesoftware.htmlunit.html.ClickableElement; 47 import com.gargoylesoftware.htmlunit.html.HtmlPage; 48 49 56 public class EventTest extends WebTestCase { 57 58 63 public EventTest( final String name ) { 64 super( name ); 65 } 66 67 72 public void testThisDefined() throws Exception { 73 final List expectedAlerts = Collections.singletonList("clickId"); 74 75 final String content 76 = "<html><head></head><body>\n" 77 + "<input type='button' id='clickId'/>\n" 78 + "<script>\n" 79 + "function handler(event) { alert(this.getAttribute('id')); }\n" 80 + "document.getElementById('clickId').onclick = handler;</script>\n" 81 + "</body></html>\n"; 82 onClickPageTest(content, expectedAlerts); 83 } 84 85 90 public void testSetPropOnThisDefined() throws Exception { 91 final List expectedAlerts = Collections.singletonList("foo"); 92 93 final String content 94 = "<html><head></head><body>\n" 95 + "<input type='button' id='clickId'/>\n" 96 + "<script>\n" 97 + "function handler(event) { alert(this.madeUpProperty); }\n" 98 + "document.getElementById('clickId').onclick = handler;\n" 99 + "document.getElementById('clickId').madeUpProperty = 'foo';\n" 100 + "</script>\n" 101 + "</body></html>\n"; 102 onClickPageTest(content, expectedAlerts); 103 } 104 105 110 public void testEventArgDefinedByWrapper() throws Exception { 111 final List expectedAlerts = Collections.singletonList("defined"); 112 113 final String content 114 = "<html><head></head><body>\n" 115 + "<input type='button' id='clickId' onclick=\"alert(event ? 'defined' : 'undefined')\"/>\n" 116 + "</body></html>\n"; 117 onClickPageTest(content, expectedAlerts); 118 } 119 120 124 public void testEventArgDefined() throws Exception { 125 final List expectedAlerts = Collections.singletonList("defined"); 126 final String content 127 = "<html><head></head><body>\n" 128 + "<input type='button' id='clickId'/>\n" 129 + "<script>\n" 130 + "function handler(event) { alert(event ? 'defined' : 'undefined'); }\n" 131 + "document.getElementById('clickId').onclick = handler;</script>\n" 132 + "</body></html>\n"; 133 onClickPageTest(content, expectedAlerts); 134 } 135 136 140 public void testEventCurrentTargetSameAsThis() throws Exception { 141 final List expectedAlerts = Collections.singletonList("pass"); 142 final String content 143 = "<html><head></head><body>\n" 144 + "<input type='button' id='clickId'/>\n" 145 + "<script>\n" 146 + "function handler(event) {\n" 147 + "alert(event.currentTarget == this ? 'pass' : event.currentTarget + '!=' + this); }\n" 148 + "document.getElementById('clickId').onclick = handler;</script>\n" 149 + "</body></html>\n"; 150 onClickPageTest(content, expectedAlerts); 151 } 152 153 private void onClickPageTest(final String content, final List expectedAlerts) throws Exception, IOException { 154 final List collectedAlerts = new ArrayList(); 155 final HtmlPage page = loadPage(content, collectedAlerts); 156 157 final ClickableElement clickable = (ClickableElement)page.getHtmlElementById("clickId"); 158 clickable.click(); 159 160 assertEquals(expectedAlerts, collectedAlerts); 161 } 162 163 167 public void testEventScope() throws Exception { 168 final List expectedAlerts = Collections.singletonList("frame1"); 169 final String content 170 = "<html><head></head>" 171 + "<body>" 172 + "<button name='button1' id='button1' onclick='alert(this.name)'>1</button>" 173 + "<iframe SRC='about:blank' name='frame1' id='frame1'></iframe>" 174 + "<script>" 175 + "document.getElementById('frame1').onload = document.getElementById('button1').onclick;" 176 + "</script>" 177 + "</body></html>"; 178 final List collectedAlerts = new ArrayList(); 179 loadPage(content, collectedAlerts); 180 createTestPageForRealBrowserIfNeeded(content, expectedAlerts); 181 182 assertEquals(expectedAlerts, collectedAlerts); 183 } 184 } 185 | Popular Tags |