KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > gargoylesoftware > htmlunit > html > HtmlButtonTest


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.html;
39
40 import java.util.ArrayList;
41 import java.util.Arrays;
42 import java.util.Collections;
43 import java.util.List;
44
45 import com.gargoylesoftware.htmlunit.BrowserVersion;
46 import com.gargoylesoftware.htmlunit.KeyValuePair;
47 import com.gargoylesoftware.htmlunit.MockWebConnection;
48 import com.gargoylesoftware.htmlunit.WebClient;
49 import com.gargoylesoftware.htmlunit.WebTestCase;
50
51 /**
52  * Tests for HtmlButton
53  *
54  * @version $Revision: 1.15 $
55  * @author <a HREF="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
56  * @author David K. Taylor
57  * @author Brad Clarke
58  */

59 public class HtmlButtonTest extends WebTestCase {
60     /**
61      * Create an instance
62      *
63      * @param name The name of the test
64      */

65     public HtmlButtonTest( final String name ) {
66         super( name );
67     }
68
69
70     /**
71      * @throws Exception if the test fails
72      */

73     public void testButtonClick_onClick() throws Exception {
74         final String htmlContent
75             = "<html><head><title>foo</title></head><body>"
76             + "<form id='form1' onSubmit='alert(\"bar\")' onReset='alert(\"reset\")'>"
77             + " <button type='button' name='button' id='button' "
78             + "onClick='alert(\"foo\")'>Push me</button>"
79             + "</form></body></html>";
80         final List collectedAlerts = new ArrayList();
81         final HtmlPage page = loadPage(htmlContent, collectedAlerts);
82         final HtmlButton button = ( HtmlButton )page.getHtmlElementById( "button" );
83
84         final HtmlPage secondPage = (HtmlPage)button.click();
85
86         final List expectedAlerts = Arrays.asList( new String[]{"foo"} );
87         assertEquals( expectedAlerts, collectedAlerts );
88
89         assertSame( page, secondPage );
90     }
91
92
93     /**
94      * @throws Exception if the test fails
95      */

96     public void testSubmitClick_onClick() throws Exception {
97         final String htmlContent
98             = "<html><head><title>foo</title></head><body>"
99             + "<form id='form1' onSubmit='alert(\"bar\")' onReset='alert(\"reset\")'>"
100             + " <button type='submit' name='button' id='button' "
101             + "onClick='alert(\"foo\")'>Push me</button>"
102             + "</form></body></html>";
103         final List collectedAlerts = new ArrayList();
104         final HtmlPage page = loadPage(htmlContent, collectedAlerts);
105         final HtmlButton button = ( HtmlButton )page.getHtmlElementById( "button" );
106
107         final HtmlPage secondPage = (HtmlPage)button.click();
108
109         final List expectedAlerts = Arrays.asList( new String[]{"foo", "bar"} );
110         assertEquals( expectedAlerts, collectedAlerts );
111
112         assertNotSame( page, secondPage );
113     }
114
115
116     /**
117      * @throws Exception if the test fails
118      */

119     public void testResetClick_onClick() throws Exception {
120         final String htmlContent
121             = "<html><head><title>foo</title></head><body>"
122             + "<form id='form1' onSubmit='alert(\"bar\")' onReset='alert(\"reset\")'>"
123             + " <button type='reset' name='button' id='button' "
124             + "onClick='alert(\"foo\")'>Push me</button>"
125             + "</form></body></html>";
126         final List collectedAlerts = new ArrayList();
127         final HtmlPage page = loadPage(htmlContent, collectedAlerts);
128         final HtmlButton button = ( HtmlButton )page.getHtmlElementById( "button" );
129
130         final HtmlPage secondPage = (HtmlPage)button.click();
131
132         final List expectedAlerts = Arrays.asList( new String[]{"foo", "reset"} );
133         assertEquals( expectedAlerts, collectedAlerts );
134
135         assertSame( page, secondPage );
136     }
137
138
139     /**
140      * @throws Exception if the test fails
141      */

142     public void testReset() throws Exception {
143         final String htmlContent
144             = "<html><head><title>foo</title></head><body>"
145             + "<form id='form1'>"
146             + "<input type='text' name='textfield1' id='textfield1' value='foo'/>"
147             + "<input type='password' name='password1' id='password1' value='foo'/>"
148             + "<input type='hidden' name='hidden1' id='hidden1' value='foo'/>"
149             + "<input type='radio' name='radioButton' value='foo' checked/>"
150             + "<input type='radio' name='radioButton' value='bar'/>"
151             + "<input type='checkbox' name='checkBox' value='check'/>"
152             + "<select id='select1'>"
153             + " <option id='option1' selected value='1'>Option1</option>"
154             + " <option id='option2' value='2'>Option2</option>"
155             + "</select>"
156             + "<textarea id='textarea1'>Foobar</textarea>"
157             + "<isindex prompt='Enter some text' id='isindex1'>"
158             + "<button type='reset' id='resetButton' value='pushme'/>"
159             + "</form></body></html>";
160         final HtmlPage page = loadPage(htmlContent);
161         final HtmlForm form = ( HtmlForm )page.getHtmlElementById( "form1" );
162         final HtmlButton resetInput = (HtmlButton)page.getHtmlElementById( "resetButton" );
163
164         // change all the values to something else
165
form.setCheckedRadioButton("radioButton", "bar");
166         ((HtmlCheckBoxInput)form.getInputByName("checkBox")).setChecked(true);
167         ((HtmlOption)page.getHtmlElementById("option1")).setSelected(false);
168         ((HtmlOption)page.getHtmlElementById("option2")).setSelected(true);
169         ((HtmlTextArea)page.getHtmlElementById("textarea1")).setText("Flintstone");
170         ((HtmlTextInput)page.getHtmlElementById("textfield1")).setValueAttribute("Flintstone");
171         ((HtmlHiddenInput)page.getHtmlElementById("hidden1")).setValueAttribute("Flintstone");
172         ((HtmlPasswordInput)page.getHtmlElementById("password1")).setValueAttribute("Flintstone");
173         ((HtmlIsIndex)page.getHtmlElementById("isindex1")).setValue("Flintstone");
174
175         // Check to make sure they did get changed
176
assertEquals( "bar", form.getCheckedRadioButton("radioButton").getValueAttribute());
177         assertTrue( ((HtmlCheckBoxInput)form.getInputByName("checkBox")).isChecked() );
178         assertFalse(((HtmlOption)page.getHtmlElementById("option1")).isSelected());
179         assertTrue(((HtmlOption)page.getHtmlElementById("option2")).isSelected());
180         assertEquals( "Flintstone", ((HtmlTextArea)page.getHtmlElementById("textarea1")).getText());
181         assertEquals( "Flintstone", ((HtmlTextInput)page.getHtmlElementById("textfield1")).getValueAttribute());
182         assertEquals( "Flintstone", ((HtmlHiddenInput)page.getHtmlElementById("hidden1")).getValueAttribute());
183         assertEquals( "Flintstone", ((HtmlIsIndex)page.getHtmlElementById("isindex1")).getValue());
184
185         final HtmlPage secondPage = (HtmlPage)resetInput.click();
186         assertSame( page, secondPage );
187
188         // Check to make sure all the values have been set back to their original values.
189
assertEquals( "foo", form.getCheckedRadioButton("radioButton").getValueAttribute());
190         assertFalse( ((HtmlCheckBoxInput)form.getInputByName("checkBox")).isChecked() );
191         assertTrue(((HtmlOption)page.getHtmlElementById("option1")).isSelected());
192         assertFalse(((HtmlOption)page.getHtmlElementById("option2")).isSelected());
193         assertEquals( "Foobar", ((HtmlTextArea)page.getHtmlElementById("textarea1")).getText());
194         assertEquals( "foo", ((HtmlTextInput)page.getHtmlElementById("textfield1")).getValueAttribute());
195         assertEquals( "foo", ((HtmlHiddenInput)page.getHtmlElementById("hidden1")).getValueAttribute());
196         assertEquals( "foo", ((HtmlPasswordInput)page.getHtmlElementById("password1")).getValueAttribute());
197         assertEquals( "", ((HtmlIsIndex)page.getHtmlElementById("isindex1")).getValue());
198     }
199     /**
200      * @throws Exception if the test fails
201      */

202     public void testButtonTypeSubmit() throws Exception {
203         final String htmlContent
204             = "<html><head><title>foo</title></head><body>"
205             + "<form id='form1' onSubmit='alert(\"bar\")' onReset='alert(\"reset\")'>"
206             + " <button type='submit' name='button' id='button' value='foo'"
207             + " >Push me</button>"
208             + "</form></body></html>";
209         final List collectedAlerts = new ArrayList();
210         final HtmlPage page = loadPage(htmlContent, collectedAlerts);
211         final HtmlButton button = ( HtmlButton )page.getHtmlElementById( "button" );
212
213         final HtmlPage secondPage = (HtmlPage)button.click();
214
215         final List expectedAlerts = Arrays.asList( new String[]{"bar"} );
216         assertEquals( expectedAlerts, collectedAlerts );
217
218         assertNotSame( page, secondPage );
219         final List expectedParameters = Arrays.asList( new Object[]{
220             new KeyValuePair("button", "foo")
221         } );
222         final List collectedParameters = getMockConnection(secondPage).getLastParameters();
223
224         assertEquals( expectedParameters, collectedParameters );
225     }
226
227
228     /**
229      * According to the html spec, the default type for a button is "submit"
230      * @throws Exception if the test fails
231      */

232     public void testDefaultButtonType_StandardsCompliantBrowser() throws Exception {
233         doTestDefaultButtonType(BrowserVersion.MOZILLA_1_0, "submit");
234     }
235
236     /**
237      * IE is different than the html spec and has a default type of "button"
238      * @throws Exception if the test fails
239      */

240     public void testDefaultButtonType_InternetExplorer() throws Exception {
241         doTestDefaultButtonType(BrowserVersion.INTERNET_EXPLORER_6_0, "button");
242     }
243     
244     private void doTestDefaultButtonType(final BrowserVersion browserVersion,
245             final String expectedType) throws Exception {
246         final String firstContent
247             = "<html><head><title>First</title></head><body>"
248             + "<form id='form1' action='http://second'>"
249             + " <button name='button' id='button' value='pushme'>PushMe</button>"
250             + "</form></body></html>";
251         final String secondContent
252             = "<html><head><title>Second</title></head><body'></body></html>";
253         final WebClient client = new WebClient(browserVersion);
254
255         final MockWebConnection webConnection = new MockWebConnection( client );
256         webConnection.setResponse(
257             URL_FIRST, firstContent, 200, "OK", "text/html", Collections.EMPTY_LIST );
258         webConnection.setResponse(
259             URL_SECOND,secondContent,200,"OK","text/html",Collections.EMPTY_LIST );
260         client.setWebConnection( webConnection );
261
262         final HtmlPage page = ( HtmlPage )client.getPage(URL_FIRST);
263         final HtmlButton button = ( HtmlButton )page.getHtmlElementById( "button" );
264         assertEquals( expectedType, button.getTypeAttribute() );
265         
266         final HtmlPage page2 = (HtmlPage)button.click();
267         final List expectedParameters;
268         final String expectedSecondPageTitle;
269         if( expectedType.equals("submit") ) {
270             expectedParameters = Collections.singletonList( new KeyValuePair("button", "pushme"));
271             expectedSecondPageTitle = "Second";
272         }
273         else {
274             expectedParameters = Collections.EMPTY_LIST;
275             expectedSecondPageTitle = "First";
276         }
277         assertEquals( expectedParameters, webConnection.getLastParameters() );
278         assertEquals( expectedSecondPageTitle, page2.getTitleText() );
279     }
280 }
281
Popular Tags