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 53 public class HtmlCheckBoxInputTest extends WebTestCase { 54 59 public HtmlCheckBoxInputTest( final String name ) { 60 super( name ); 61 } 62 63 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 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 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 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 |