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.javascript.host.html;
16  
17  import org.htmlunit.WebDriverTestCase;
18  import org.htmlunit.junit.annotation.Alerts;
19  import org.junit.jupiter.api.Test;
20  
21  /**
22   * Tests for {@link HTMLLegendElement}.
23   *
24   * @author Daniel Gredler
25   * @author Ronald Brill
26   * @author Frank Danek
27   */
28  public class HTMLLegendElementTest extends WebDriverTestCase {
29  
30      /**
31       * @throws Exception if an error occurs
32       */
33      @Test
34      @Alerts({"", "A", "a", "A", "a8", "8Afoo", "8", "@"})
35      public void accessKey() throws Exception {
36          final String html = DOCTYPE_HTML
37              + "<html><body><legend id='a1'>a1</legend><legend id='a2' accesskey='A'>a2</legend><script>\n"
38              + LOG_TITLE_FUNCTION
39              + "var a1 = document.getElementById('a1'), a2 = document.getElementById('a2');\n"
40              + "log(a1.accessKey);\n"
41              + "log(a2.accessKey);\n"
42              + "a1.accessKey = 'a';\n"
43              + "a2.accessKey = 'A';\n"
44              + "log(a1.accessKey);\n"
45              + "log(a2.accessKey);\n"
46              + "a1.accessKey = 'a8';\n"
47              + "a2.accessKey = '8Afoo';\n"
48              + "log(a1.accessKey);\n"
49              + "log(a2.accessKey);\n"
50              + "a1.accessKey = '8';\n"
51              + "a2.accessKey = '@';\n"
52              + "log(a1.accessKey);\n"
53              + "log(a2.accessKey);\n"
54              + "</script></body></html>";
55  
56          loadPageVerifyTitle2(html);
57      }
58  
59      /**
60       * @throws Exception if the test fails
61       */
62      @Test
63      @Alerts("[object HTMLFormElement]")
64      public void form() throws Exception {
65          final String html = DOCTYPE_HTML
66              + "<html><body><form><fieldset><legend id='a'>a</legend></fieldset></form><script>\n"
67              + LOG_TITLE_FUNCTION
68              + "log(document.getElementById('a').form);\n"
69              + "</script></body></html>";
70  
71          loadPageVerifyTitle2(html);
72      }
73  
74      /**
75       * @throws Exception if an error occurs
76       */
77      @Test
78      @Alerts({"left", "right", "bottom", "top", "wrong", ""})
79      public void getAlign() throws Exception {
80          final String html = DOCTYPE_HTML
81              + "<html><body>\n"
82              + "  <form><fieldset>\n"
83              + "    <legend id='i1' align='left' ></legend>\n"
84              + "    <legend id='i2' align='right' ></legend>\n"
85              + "    <legend id='i3' align='bottom' ></legend>\n"
86              + "    <legend id='i4' align='top' ></legend>\n"
87              + "    <legend id='i5' align='wrong' ></legend>\n"
88              + "    <legend id='i6' ></legend>\n"
89              + "  </fieldset></form>\n"
90  
91              + "<script>\n"
92              + LOG_TITLE_FUNCTION
93              + "  for (var i = 1; i <= 6; i++) {\n"
94              + "    log(document.getElementById('i' + i).align);\n"
95              + "  }\n"
96              + "</script>\n"
97              + "</body></html>";
98  
99          loadPageVerifyTitle2(html);
100     }
101 
102     /**
103      * @throws Exception if an error occurs
104      */
105     @Test
106     @Alerts({"CenTer", "8", "foo", "left", "right", "bottom", "top"})
107     public void setAlign() throws Exception {
108         final String html = DOCTYPE_HTML
109             + "<html><body>\n"
110             + "  <form><fieldset>\n"
111             + "    <legend id='i1' align='left' ></legend>\n"
112             + "  </fieldset></form>\n"
113 
114             + "<script>\n"
115             + LOG_TITLE_FUNCTION
116             + "  function setAlign(elem, value) {\n"
117             + "    try {\n"
118             + "      elem.align = value;\n"
119             + "    } catch(e) { logEx(e); }\n"
120             + "    log(elem.align);\n"
121             + "  }\n"
122 
123             + "  var elem = document.getElementById('i1');\n"
124             + "  setAlign(elem, 'CenTer');\n"
125 
126             + "  setAlign(elem, '8');\n"
127             + "  setAlign(elem, 'foo');\n"
128 
129             + "  setAlign(elem, 'left');\n"
130             + "  setAlign(elem, 'right');\n"
131             + "  setAlign(elem, 'bottom');\n"
132             + "  setAlign(elem, 'top');\n"
133             + "</script>\n"
134             + "</body></html>";
135 
136         loadPageVerifyTitle2(html);
137     }
138 }