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