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   * Unit tests for {@link HTMLDetailsElement}.
25   *
26   * @author Ronald Brill
27   */
28  @RunWith(BrowserRunner.class)
29  public class HTMLDetailsElementTest extends WebDriverTestCase {
30  
31      /**
32       * @throws Exception if an error occurs
33       */
34      @Test
35      @Alerts({"false", "null", "true", "", "false", "null", "true", "",
36               "true", "", "true", "TrUE", "false", "null"})
37      public void open() throws Exception {
38          final String html = DOCTYPE_HTML
39              + "<html>\n"
40              + "  <head>\n"
41              + "    <script>\n"
42              + LOG_TITLE_FUNCTION
43              + "      function test() {\n"
44              + "        var det = document.getElementById('detail');\n"
45              + "        log(det.open);\n"
46              + "        log(det.getAttribute('open'));\n"
47  
48              + "        det.open = true;\n"
49              + "        log(det.open);\n"
50              + "        log(det.getAttribute('open'));\n"
51  
52              + "        det.open = false;\n"
53              + "        log(det.open);\n"
54              + "        log(det.getAttribute('open'));\n"
55  
56              + "        det.open = 'true';\n"
57              + "        log(det.open);\n"
58              + "        log(det.getAttribute('open'));\n"
59  
60              + "        det.open = 'faLSE';\n"
61              + "        log(det.open);\n"
62              + "        log(det.getAttribute('open'));\n"
63  
64              + "        det.setAttribute('open', 'TrUE');\n"
65              + "        log(det.open);\n"
66              + "        log(det.getAttribute('open'));\n"
67  
68              + "        det.removeAttribute('open');\n"
69              + "        log(det.open);\n"
70              + "        log(det.getAttribute('open'));\n"
71              + "      }\n"
72              + "    </script>\n"
73              + "  </head>\n"
74              + "  <body onload='test()'>\n"
75              + "    <details id='detail'>\n"
76              + "      <summary>Automated Status: Operational</summary>\n"
77              + "      <p>Velocity: 12m/s</p>\n"
78              + "      <p>Direction: North</p>\n"
79              + "    </details>\n"
80              + "  </body>\n"
81              + "</html>";
82  
83          loadPageVerifyTitle2(html);
84      }
85  
86      /**
87       * @throws Exception if an error occurs
88       */
89      @Test
90      @Alerts({"false", "null", "false", "null", "true", "", "true", "blah", "false", "null"})
91      public void openString() throws Exception {
92          final String html = DOCTYPE_HTML
93              + "<html>\n"
94              + "  <head>\n"
95              + "    <script>\n"
96              + LOG_TITLE_FUNCTION
97              + "      function test() {\n"
98              + "        var det = document.getElementById('detail');\n"
99              + "        log(det.open);\n"
100             + "        log(det.getAttribute('open'));\n"
101 
102             + "        det.open = '';\n"
103             + "        log(det.open);\n"
104             + "        log(det.getAttribute('open'));\n"
105 
106             + "        det.open = 'abc';\n"
107             + "        log(det.open);\n"
108             + "        log(det.getAttribute('open'));\n"
109 
110             + "        det.setAttribute('open', 'blah');\n"
111             + "        log(det.open);\n"
112             + "        log(det.getAttribute('open'));\n"
113 
114             + "        det.removeAttribute('open');\n"
115             + "        log(det.open);\n"
116             + "        log(det.getAttribute('open'));\n"
117             + "      }\n"
118             + "    </script>\n"
119             + "  </head>\n"
120             + "  <body onload='test()'>\n"
121             + "    <details id='detail'>\n"
122             + "      <summary>Automated Status: Operational</summary>\n"
123             + "      <p>Velocity: 12m/s</p>\n"
124             + "      <p>Direction: North</p>\n"
125             + "    </details>\n"
126             + "  </body>\n"
127             + "</html>";
128 
129         loadPageVerifyTitle2(html);
130     }
131 
132     /**
133      * @throws Exception if an error occurs
134      */
135     @Test
136     @Alerts(DEFAULT = {"", "null", "", "", "abc", "abc", "blah", "blah", "", "null"},
137             FF_ESR = {"undefined", "null", "", "null", "abc", "null", "abc", "blah", "abc", "null"})
138     public void name() throws Exception {
139         final String html = DOCTYPE_HTML
140             + "<html>\n"
141             + "  <head>\n"
142             + "    <script>\n"
143             + LOG_TITLE_FUNCTION
144             + "      function test() {\n"
145             + "        var det = document.getElementById('detail');\n"
146             + "        log(det.name);\n"
147             + "        log(det.getAttribute('name'));\n"
148 
149             + "        det.name = '';\n"
150             + "        log(det.name);\n"
151             + "        log(det.getAttribute('name'));\n"
152 
153             + "        det.name = 'abc';\n"
154             + "        log(det.name);\n"
155             + "        log(det.getAttribute('name'));\n"
156 
157             + "        det.setAttribute('name', 'blah');\n"
158             + "        log(det.name);\n"
159             + "        log(det.getAttribute('name'));\n"
160 
161             + "        det.removeAttribute('name');\n"
162             + "        log(det.name);\n"
163             + "        log(det.getAttribute('name'));\n"
164             + "      }\n"
165             + "    </script>\n"
166             + "  </head>\n"
167             + "  <body onload='test()'>\n"
168             + "    <details id='detail'>\n"
169             + "      <summary>Automated Status: Operational</summary>\n"
170             + "      <p>Velocity: 12m/s</p>\n"
171             + "      <p>Direction: North</p>\n"
172             + "    </details>\n"
173             + "  </body>\n"
174             + "</html>";
175 
176         loadPageVerifyTitle2(html);
177     }
178 }