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 HTMLTableCaptionElement}.
25   *
26   * @author Daniel Gredler
27   * @author Ronald Brill
28   * @author Frank Danek
29   */
30  @RunWith(BrowserRunner.class)
31  public class HTMLTableCaptionElementTest extends WebDriverTestCase {
32  
33      /**
34       * @throws Exception if an error occurs
35       */
36      @Test
37      @Alerts({"left", "right", "bottom", "top", "wrong", ""})
38      public void getAlign() throws Exception {
39          final String html = DOCTYPE_HTML
40              + "<html><body>\n"
41              + "  <table>\n"
42              + "    <caption id='c1' align='left' ></caption>\n"
43              + "    <caption id='c2' align='right' ></caption>\n"
44              + "    <caption id='c3' align='bottom' ></caption>\n"
45              + "    <caption id='c4' align='top' ></caption>\n"
46              + "    <caption id='c5' align='wrong' ></caption>\n"
47              + "    <caption id='c6' ></caption>\n"
48              + "  </table>\n"
49  
50              + "<script>\n"
51              + LOG_TITLE_FUNCTION
52              + "  for (var i = 1; i <= 6; i++) {\n"
53              + "    log(document.getElementById('c' + i).align);\n"
54              + "  }\n"
55              + "</script>\n"
56              + "</body></html>";
57  
58          loadPageVerifyTitle2(html);
59      }
60  
61      /**
62       * @throws Exception if an error occurs
63       */
64      @Test
65      @Alerts({"CenTer", "8", "foo", "left", "right", "bottom", "top"})
66      public void setAlign() throws Exception {
67          final String html = DOCTYPE_HTML
68              + "<html><body>\n"
69              + "  <table>\n"
70              + "    <caption id='c1' align='left' ></caption>\n"
71              + "  </table>\n"
72  
73              + "<script>\n"
74              + LOG_TITLE_FUNCTION
75              + "  function setAlign(elem, value) {\n"
76              + "    try {\n"
77              + "      elem.align = value;\n"
78              + "    } catch(e) { logEx(e); }\n"
79              + "    log(elem.align);\n"
80              + "  }\n"
81  
82              + "  var elem = document.getElementById('c1');\n"
83              + "  setAlign(elem, 'CenTer');\n"
84  
85              + "  setAlign(elem, '8');\n"
86              + "  setAlign(elem, 'foo');\n"
87  
88              + "  setAlign(elem, 'left');\n"
89              + "  setAlign(elem, 'right');\n"
90              + "  setAlign(elem, 'bottom');\n"
91              + "  setAlign(elem, 'top');\n"
92              + "</script>\n"
93              + "</body></html>";
94  
95          loadPageVerifyTitle2(html);
96      }
97  
98      /**
99       * @throws Exception if an error occurs
100      */
101     @Test
102     @Alerts({"undefined", "undefined", "undefined", "middle", "8", "BOTtom"})
103     public void vAlign() throws Exception {
104         final String html = DOCTYPE_HTML
105             + "<html><body><table>\n"
106             + "  <caption id='c1' valign='top'>a</caption>\n"
107             + "  <caption id='c2' valign='baseline'>b</caption>\n"
108             + "  <caption id='c3' valign='3'>c</caption>\n"
109             + "  <tr>\n"
110             + "    <td>a</td>\n"
111             + "    <td>b</td>\n"
112             + "    <td>c</td>\n"
113             + "  </tr>\n"
114             + "</table>\n"
115             + "<script>\n"
116             + LOG_TITLE_FUNCTION
117             + "  function set(e, value) {\n"
118             + "    try {\n"
119             + "      e.vAlign = value;\n"
120             + "    } catch(e) { logEx(e); }\n"
121             + "  }\n"
122             + "  var c1 = document.getElementById('c1');\n"
123             + "  var c2 = document.getElementById('c2');\n"
124             + "  var c3 = document.getElementById('c3');\n"
125             + "  log(c1.vAlign);\n"
126             + "  log(c2.vAlign);\n"
127             + "  log(c3.vAlign);\n"
128             + "  set(c1, 'middle');\n"
129             + "  set(c2, 8);\n"
130             + "  set(c3, 'BOTtom');\n"
131             + "  log(c1.vAlign);\n"
132             + "  log(c2.vAlign);\n"
133             + "  log(c3.vAlign);\n"
134             + "</script>\n"
135             + "</body></html>";
136 
137         loadPageVerifyTitle2(html);
138     }
139 
140     /**
141      * @throws Exception if an error occurs
142      */
143     @Test
144     @Alerts({"<caption id=\"cap\">a</caption>", "new"})
145     public void outerHTML() throws Exception {
146         final String html = DOCTYPE_HTML
147             + "<html>\n"
148             + "  <head>\n"
149             + "    <script>\n"
150             + LOG_TITLE_FUNCTION
151             + "      function test() {\n"
152             + "        log(document.getElementById('cap').outerHTML);\n"
153             + "        document.getElementById('cap').outerHTML = '<div id=\"new\">text<div>';\n"
154             + "        log(document.getElementById('new').id);\n"
155             + "      }\n"
156             + "    </script>\n"
157             + "  </head>\n"
158             + "  <body onload='test()'>\n"
159             + "  <table>\n"
160             + "    <caption id='cap'>a</caption>\n"
161             + "    <tr>\n"
162             + "      <td>cell1</td>\n"
163             + "    </tr>\n"
164             + "  </table>\n"
165             + "  </body>\n"
166             + "</html>";
167 
168         loadPageVerifyTitle2(html);
169     }
170 }