KickJava   Java API By Example, From Geeks To Geeks.

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


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.List;
43
44 import com.gargoylesoftware.htmlunit.WebTestCase;
45
46 /**
47  * Tests for HtmlCheckBox
48  *
49  * @version $Revision: 1.6 $
50  * @author Mike Bresnahan
51  * @author Marc Guillemot
52  */

53 public class HtmlCheckBoxInputTest extends WebTestCase {
54     /**
55      * Create an instance
56      *
57      * @param name The name of the test
58      */

59     public HtmlCheckBoxInputTest( final String name ) {
60         super( name );
61     }
62
63     /**
64      * Verifies that a HtmlCheckBox is unchecked by default.
65      * The onClick tests make this assumption.
66      * @throws Exception if the test fails
67      */

68     public void test_defaultState() throws Exception {
69         final String htmlContent
70             = "<html><head><title>foo</title></head><body>"
71             + "<form id='form1'>"
72             + " <input type='checkbox' name='checkbox' id='checkbox'>Check me</input>"
73             + "</form></body></html>";
74         final HtmlPage page = loadPage(htmlContent);
75         final HtmlCheckBoxInput checkBox = ( HtmlCheckBoxInput )page.getHtmlElementById( "checkbox" );
76
77         assertFalse( checkBox.isChecked());
78     }
79
80     /**
81      * Given a onclick handler that does not cause the form to submit, this test
82      * verifies that HtmlCheckBix.click()
83      * <ul>
84      * <li>sets the checkbox to the "checked" state</li>
85      * <li>returns the same page</li>
86      * </ul>
87      * @throws Exception if the test fails
88      */

89     public void test_onClick() throws Exception {
90         final String htmlContent
91             = "<html><head><title>foo</title></head><body>"
92             + "<form id='form1' onSubmit='alert(\"bar\")' onReset='alert(\"reset\")'>"
93             + " <input type='checkbox' name='checkbox' id='checkbox' "
94             + "onClick='alert(\"foo\")'>Check me</input>"
95             + "</form></body></html>";
96         final List collectedAlerts = new ArrayList();
97         final HtmlPage page = loadPage(htmlContent, collectedAlerts);
98         final HtmlCheckBoxInput checkBox = ( HtmlCheckBoxInput )page.getHtmlElementById( "checkbox" );
99
100         final HtmlPage secondPage = (HtmlPage)checkBox.click();
101
102         final List expectedAlerts = Arrays.asList( new String[]{"foo"} );
103         assertEquals( expectedAlerts, collectedAlerts );
104
105         assertSame( page, secondPage );
106         assertTrue( checkBox.isChecked());
107     }
108
109     /**
110      * Given a onclick handler that causes the form to submit, this test verifies that HtmlCheckBix.click()
111      * <ul>
112      * <li>sets the checkbox to the "checked" state</li>
113      * <li>returns the new page</li>
114      * </ul>
115      * @throws Exception if the test fails
116      */

117     public void test_onClickThatSubmitsForm() throws Exception {
118         final String htmlContent
119             = "<html><head><title>foo</title></head><body>"
120             + "<form id='form1' name='form1'>"
121             + " <input type='checkbox' name='checkbox' id='checkbox' "
122             + "onClick='document.form1.submit()'>Check me</input>"
123             + "</form></body></html>";
124         final HtmlPage page = loadPage(htmlContent);
125         final HtmlCheckBoxInput checkBox = ( HtmlCheckBoxInput )page.getHtmlElementById( "checkbox" );
126
127         final HtmlPage secondPage = (HtmlPage)checkBox.click();
128
129         assertNotSame( page, secondPage );
130         assertTrue( checkBox.isChecked());
131     }
132
133     /**
134      * Verifies that a asText() returns "checked" or "unckecked" according to the state of the checkbox.
135      * @throws Exception if the test fails
136      */

137     public void testAsText() throws Exception {
138         final String htmlContent
139             = "<html><head><title>foo</title></head><body>"
140             + "<form id='form1'>"
141             + " <input type='checkbox' name='checkbox' id='checkbox'>Check me</input>"
142             + "</form></body></html>";
143
144         final HtmlPage page = loadPage(htmlContent);
145
146         final HtmlCheckBoxInput checkBox = ( HtmlCheckBoxInput )page.getHtmlElementById( "checkbox" );
147         assertEquals("unchecked", checkBox.asText());
148         checkBox.setChecked(true);
149         assertEquals("checked", checkBox.asText());
150     }
151 }
152
Popular Tags