KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > gargoylesoftware > htmlunit > javascript > host > EventTest


1 /*
2  * Copyright (c) 2002, 2005 Gargoyle Software Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * 1. Redistributions of source code must retain the above copyright notice,
8  * this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright notice,
10  * this list of conditions and the following disclaimer in the documentation
11  * and/or other materials provided with the distribution.
12  * 3. The end-user documentation included with the redistribution, if any, must
13  * include the following acknowledgment:
14  *
15  * "This product includes software developed by Gargoyle Software Inc.
16  * (http://www.GargoyleSoftware.com/)."
17  *
18  * Alternately, this acknowledgment may appear in the software itself, if
19  * and wherever such third-party acknowledgments normally appear.
20  * 4. The name "Gargoyle Software" must not be used to endorse or promote
21  * products derived from this software without prior written permission.
22  * For written permission, please contact info@GargoyleSoftware.com.
23  * 5. Products derived from this software may not be called "HtmlUnit", nor may
24  * "HtmlUnit" appear in their name, without prior written permission of
25  * Gargoyle Software Inc.
26  *
27  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
28  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
29  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GARGOYLE
30  * SOFTWARE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
31  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
33  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
36  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37  */

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 /**
50  * Tests that when DOM events such as "onclick" have access
51  * to an Event object with context information.
52  *
53  * @version $Revision: 1.3 $
54  * @author <a HREF="mailto:chriseldredge@comcast.net">Chris Eldredge</a>
55  */

56 public class EventTest extends WebTestCase {
57
58     /**
59      * Create an instance
60      *
61      * @param name Name of the test
62      */

63     public EventTest( final String name ) {
64         super( name );
65     }
66
67     /**
68      * Verify the "this" object refers to the Element being clicked when an
69      * event handler is invoked.
70      * @throws Exception if the test fails
71      */

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     /**
86      * Verify setting a previously undefined/non-existant property on an Element
87      * is accessible from inside an event handler
88      * @throws Exception if the test fails
89      */

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     /**
106      * Verify that javascript snippets have a variable named 'event' available
107      * to them.
108      * @throws Exception if the test fails
109      */

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     /**
121      * Verify that when event handler is invoked an argument is passed in.
122      * @throws Exception if the test fails
123      */

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     /**
137      * event.currentTarget == this inside javascript event handler
138      * @throws Exception if the test fails
139      */

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     /**
164      * Test that this refers to the element on which the event applies
165      * @throws Exception if the test fails
166      */

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