View Javadoc
1   /*
2    * Copyright (c) 2002-2025 Gargoyle Software Inc.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * https://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
14   */
15  package org.htmlunit.html;
16  
17  import java.util.ArrayList;
18  import java.util.List;
19  
20  import org.htmlunit.SimpleWebTestCase;
21  import org.htmlunit.junit.BrowserRunner;
22  import org.junit.Test;
23  import org.junit.runner.RunWith;
24  
25  /**
26   * Tests for {@link HtmlCheckBoxInput}.
27   *
28   * @author Mike Bresnahan
29   * @author Marc Guillemot
30   * @author Ahmed Ashour
31   */
32  @RunWith(BrowserRunner.class)
33  public class HtmlCheckBoxInputTest extends SimpleWebTestCase {
34  
35      /**
36       * Tests onclick event handlers. Given an onclick handler that does not cause the form to submit, this test
37       * verifies that HtmlCheckBix.click():
38       * <ul>
39       *   <li>sets the checkbox to the "checked" state</li>
40       *   <li>returns the same page</li>
41       * </ul>
42       * @throws Exception if the test fails
43       */
44      @Test
45      public void onClick() throws Exception {
46          final String html = DOCTYPE_HTML
47              + "<html><head><title>foo</title></head><body>\n"
48              + "<form id='form1' onSubmit='alert(\"bar\")' onReset='alert(\"reset\")'>\n"
49              + "  <input type='checkbox' name='checkbox' id='checkbox' "
50              + "onClick='alert(\"foo\");alert(event.type);'>Check me</input>\n"
51              + "</form></body></html>";
52  
53          final List<String> collectedAlerts = new ArrayList<>();
54          final HtmlPage page = loadPage(html, collectedAlerts);
55          final HtmlCheckBoxInput checkBox = page.getHtmlElementById("checkbox");
56          final HtmlPage secondPage = checkBox.click();
57  
58          final String[] expectedAlerts = {"foo", "click"};
59          assertEquals(expectedAlerts, collectedAlerts);
60  
61          assertSame(page, secondPage);
62          assertTrue(checkBox.isChecked());
63      }
64  
65      /**
66       * Tests onclick event handlers. Given an onclick handler that causes the form to submit, this test
67       * verifies that HtmlCheckBix.click():
68       * <ul>
69       *   <li>sets the checkbox to the "checked" state</li>
70       *   <li>returns the new page</li>
71       * </ul>
72       * @throws Exception if the test fails
73       */
74      @Test
75      public void onClickThatSubmitsForm() throws Exception {
76          final String html = DOCTYPE_HTML
77              + "<html><head><title>foo</title></head><body>\n"
78              + "<form id='form1' name='form1'>\n"
79              + "  <input type='checkbox' name='checkbox' id='checkbox' "
80              + "onClick='document.form1.submit()'>Check me</input>\n"
81              + "</form></body></html>";
82          final HtmlPage page = loadPage(html);
83          final HtmlCheckBoxInput checkBox = page.getHtmlElementById("checkbox");
84  
85          final HtmlPage secondPage = checkBox.click();
86  
87          assertNotSame(page, secondPage);
88          assertTrue(checkBox.isChecked());
89      }
90  
91      /**
92       * Verifies that asNormalizedText() returns "checked" or "unchecked" according to the state of the checkbox.
93       * @throws Exception if the test fails
94       */
95      @Test
96      public void asNormalizedText() throws Exception {
97          final String html = DOCTYPE_HTML
98              + "<html><head></head><body>\n"
99              + "<form id='form1'>\n"
100             + "  <input type='checkbox' name='checkbox' id='checkbox'>Check me</input>\n"
101             + "</form></body></html>";
102 
103         final HtmlPage page = loadPage(html);
104 
105         final HtmlCheckBoxInput checkBox = page.getHtmlElementById("checkbox");
106         assertEquals("unchecked", checkBox.asNormalizedText());
107         assertEquals("uncheckedCheck me", page.asNormalizedText());
108         checkBox.setChecked(true);
109         assertEquals("checked", checkBox.asNormalizedText());
110     }
111 }