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 org.htmlunit.SimpleWebTestCase;
18  import org.htmlunit.junit.annotation.Alerts;
19  import org.junit.jupiter.api.Test;
20  
21  /**
22   * Tests for {@link HtmlOptionGroup}.
23   *
24   * @author Ahmed Ashour
25   * @author Daniel Gredler
26   */
27  public class HtmlOptionGroupTest extends SimpleWebTestCase {
28  
29      /**
30       * @throws Exception if the test fails
31       */
32      @Test
33      public void enclosingSelect() throws Exception {
34          final String html = DOCTYPE_HTML
35              + "<html><head>\n"
36              + "</head><body>\n"
37              + "  <select>\n"
38              + "    <optgroup id='myId' label='my label'>\n"
39              + "      <option>My Option</option>\n"
40              + "    </optgroup>\n"
41              + "  </select>\n"
42              + "</body></html>";
43  
44          final HtmlPage page = loadPage(html);
45          final HtmlOptionGroup optionGroup = page.getHtmlElementById("myId");
46          assertNotNull(optionGroup.getEnclosingSelect());
47      }
48  
49      /**
50       * @throws Exception if an error occurs
51       */
52      @Test
53      @Alerts({"false", "false", "true", "false", "true", "false", "false", "false"})
54      public void disabled() throws Exception {
55          final String html = DOCTYPE_HTML
56              + "<html><body onload='test()'><form name='f'>\n"
57              + "  <select name='s' id='s'>\n"
58              + "    <optgroup id='g1' label='group 1'>\n"
59              + "      <option value='o11' id='o11'>One</option>\n"
60              + "      <option value='o12' id='o12'>Two</option>\n"
61              + "    </optgroup>\n"
62              + "    <optgroup id='g2' label='group 2' disabled='disabled'>\n"
63              + "      <option value='o21' id='o21'>One</option>\n"
64              + "      <option value='o22' id='o22'>Two</option>\n"
65              + "    </optgroup>\n"
66              + "  </select>\n"
67              + "  <script>\n"
68              + "    function test() {\n"
69              + "      var g1 = document.getElementById('g1');\n"
70              + "      var o11 = document.getElementById('o11');\n"
71              + "      var g2 = document.getElementById('g2');\n"
72              + "      var o21 = document.getElementById('o21');\n"
73              + "      alert(g1.disabled);\n"
74              + "      alert(o11.disabled);\n"
75              + "      alert(g2.disabled);\n"
76              + "      alert(o21.disabled);\n"
77              + "      g1.disabled = true;\n"
78              + "      g2.disabled = false;\n"
79              + "      alert(g1.disabled);\n"
80              + "      alert(o11.disabled);\n"
81              + "      alert(g2.disabled);\n"
82              + "      alert(o21.disabled);\n"
83              + "    }\n"
84              + "  </script>\n"
85              + "</form></body></html>";
86  
87          final HtmlPage page = loadPageWithAlerts(html);
88          assertEquals(true, ((HtmlOptionGroup) page.getElementById("g1")).isDisabled());
89          assertFalse(((HtmlOptionGroup) page.getElementById("g2")).isDisabled());
90      }
91  
92  }