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