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.junit.jupiter.api.Test;
22  
23  /**
24   * Tests for {@link HtmlCheckBoxInput}.
25   *
26   * @author Mike Bresnahan
27   * @author Marc Guillemot
28   * @author Ahmed Ashour
29   * @author Ronald Brill
30   */
31  public class HtmlCheckBoxInputTest extends SimpleWebTestCase {
32  
33      /**
34       * Tests onclick event handlers. Given an onclick handler that does not cause the form to submit, this test
35       * verifies that HtmlCheckBix.click():
36       * <ul>
37       *   <li>sets the checkbox to the "checked" state</li>
38       *   <li>returns the same page</li>
39       * </ul>
40       * @throws Exception if the test fails
41       */
42      @Test
43      public void onClick() throws Exception {
44          final String html = DOCTYPE_HTML
45              + "<html><head><title>foo</title></head><body>\n"
46              + "<form id='form1' onSubmit='alert(\"bar\")' onReset='alert(\"reset\")'>\n"
47              + "  <input type='checkbox' name='checkbox' id='checkbox' "
48              + "onClick='alert(\"foo\");alert(event.type);'>Check me</input>\n"
49              + "</form></body></html>";
50  
51          final List<String> collectedAlerts = new ArrayList<>();
52          final HtmlPage page = loadPage(html, collectedAlerts);
53          final HtmlCheckBoxInput checkBox = page.getHtmlElementById("checkbox");
54          final HtmlPage secondPage = checkBox.click();
55  
56          final String[] expectedAlerts = {"foo", "click"};
57          assertEquals(expectedAlerts, collectedAlerts);
58  
59          assertSame(page, secondPage);
60          assertTrue(checkBox.isChecked());
61      }
62  
63      /**
64       * Tests onclick event handlers. Given an onclick handler that causes the form to submit, this test
65       * verifies that HtmlCheckBix.click():
66       * <ul>
67       *   <li>sets the checkbox to the "checked" state</li>
68       *   <li>returns the new page</li>
69       * </ul>
70       * @throws Exception if the test fails
71       */
72      @Test
73      public void onClickThatSubmitsForm() throws Exception {
74          final String html = DOCTYPE_HTML
75              + "<html><head><title>foo</title></head><body>\n"
76              + "<form id='form1' name='form1'>\n"
77              + "  <input type='checkbox' name='checkbox' id='checkbox' "
78              + "onClick='document.form1.submit()'>Check me</input>\n"
79              + "</form></body></html>";
80          final HtmlPage page = loadPage(html);
81          final HtmlCheckBoxInput checkBox = page.getHtmlElementById("checkbox");
82  
83          final HtmlPage secondPage = checkBox.click();
84  
85          assertNotSame(page, secondPage);
86          assertTrue(checkBox.isChecked());
87      }
88  
89      /**
90       * Verifies that asNormalizedText() returns "checked" or "unchecked" according to the state of the checkbox.
91       * @throws Exception if the test fails
92       */
93      @Test
94      public void asNormalizedText() throws Exception {
95          final String html = DOCTYPE_HTML
96              + "<html><head></head><body>\n"
97              + "<form id='form1'>\n"
98              + "  <input type='checkbox' name='checkbox' id='checkbox'>Check me</input>\n"
99              + "</form></body></html>";
100 
101         final HtmlPage page = loadPage(html);
102 
103         final HtmlCheckBoxInput checkBox = page.getHtmlElementById("checkbox");
104         assertEquals("unchecked", checkBox.asNormalizedText());
105         assertEquals("uncheckedCheck me", page.asNormalizedText());
106         checkBox.setChecked(true);
107         assertEquals("checked", checkBox.asNormalizedText());
108     }
109 }