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.WebDriverTestCase;
18  import org.htmlunit.junit.annotation.Alerts;
19  import org.junit.jupiter.api.Test;
20  import org.openqa.selenium.By;
21  import org.openqa.selenium.WebDriver;
22  import org.openqa.selenium.htmlunit.HtmlUnitDriver;
23  
24  /**
25   * Tests for {@link HtmlOutput}.
26   *
27   * @author Ronald Brill
28   */
29  public class HtmlOutputTest extends WebDriverTestCase {
30  
31      /**
32       * @throws Exception if the test fails
33       */
34      @Test
35      @Alerts({"[object HTMLOutputElement]", "[object HTMLFormElement]"})
36      public void simpleScriptable() throws Exception {
37          final String html = DOCTYPE_HTML
38              + "<html><head>\n"
39              + "<script>\n"
40              + LOG_TITLE_FUNCTION
41              + "  function test() {\n"
42              + "    var o = document.getElementById('o');\n"
43              + "    log(o);\n"
44              + "    log(o.form);\n"
45              + "  }\n"
46              + "</script>\n"
47              + "</head><body onload='test()'>\n"
48              + "  <form>\n"
49              + "    <output id='o'>\n"
50              + "  </form>\n"
51              + "</body></html>";
52          final WebDriver driver = loadPageVerifyTitle2(html);
53          if (driver instanceof HtmlUnitDriver) {
54              final HtmlElement element = toHtmlElement(driver.findElement(By.id("o")));
55              assertTrue(element instanceof HtmlOutput || element instanceof HtmlUnknownElement);
56          }
57      }
58  
59      /**
60       * @throws Exception if an error occurs
61       */
62      @Test
63      @Alerts({"undefined", "undefined", "undefined", "center", "8", "foo"})
64      public void align() throws Exception {
65          final String html = DOCTYPE_HTML
66              + "<html><body>\n"
67              + "<form>\n"
68              + "  <output id='o1' align='left'>\n"
69              + "  <output id='o2' align='right'>\n"
70              + "  <output id='o3' align='3'>\n"
71              + "</form>\n"
72              + "<script>\n"
73              + LOG_TITLE_FUNCTION
74              + "  function set(fs, value) {\n"
75              + "    try {\n"
76              + "      fs.align = value;\n"
77              + "    } catch(e) { logEx(e); }\n"
78              + "  }\n"
79              + "  var o1 = document.getElementById('o1');\n"
80              + "  var o2 = document.getElementById('o2');\n"
81              + "  var o3 = document.getElementById('o3');\n"
82              + "  log(o1.align);\n"
83              + "  log(o2.align);\n"
84              + "  log(o3.align);\n"
85              + "  set(o1, 'center');\n"
86              + "  set(o2, '8');\n"
87              + "  set(o3, 'foo');\n"
88              + "  log(o1.align);\n"
89              + "  log(o2.align);\n"
90              + "  log(o3.align);\n"
91              + "</script>\n"
92              + "</body></html>";
93  
94          loadPageVerifyTitle2(html);
95      }
96  
97      /**
98       * @throws Exception if an error occurs
99       */
100     @Test
101     @Alerts({"false", "false", "false", "false", "false"})
102     public void willValidate() throws Exception {
103         final String html = DOCTYPE_HTML
104                 + "<html><head>\n"
105                 + "  <script>\n"
106                 + LOG_TITLE_FUNCTION
107                 + "    function test() {\n"
108                 + "      log(document.getElementById('o1').willValidate);\n"
109                 + "      log(document.getElementById('o2').willValidate);\n"
110                 + "      log(document.getElementById('o3').willValidate);\n"
111                 + "      log(document.getElementById('o4').willValidate);\n"
112                 + "      log(document.getElementById('o5').willValidate);\n"
113                 + "    }\n"
114                 + "  </script>\n"
115                 + "</head>\n"
116                 + "<body onload='test()'>\n"
117                 + "  <form>\n"
118                 + "    <output id='o1'>o1</output>\n"
119                 + "    <output id='o2' disabled>o2</output>\n"
120                 + "    <output id='o3' hidden>o3</output>\n"
121                 + "    <output id='o4' readonly>o4</output>\n"
122                 + "    <output id='o5' style='display: none'>o5</output>\n"
123                 + "  </form>\n"
124                 + "</body></html>";
125 
126         loadPageVerifyTitle2(html);
127     }
128 
129     /**
130      * @throws Exception if an error occurs
131      */
132     @Test
133     @Alerts({"true",
134              "false-false-false-false-false-false-false-false-false-true-false",
135              "false"})
136     public void validationEmpty() throws Exception {
137         validation("<output id='e1'>o1</output>\n", "");
138     }
139 
140     /**
141      * @throws Exception if an error occurs
142      */
143     @Test
144     @Alerts({"true",
145              "false-true-false-false-false-false-false-false-false-false-false",
146              "false"})
147     public void validationCustomValidity() throws Exception {
148         validation("<output id='e1'>o1</output>\n", "elem.setCustomValidity('Invalid');");
149     }
150 
151     /**
152      * @throws Exception if an error occurs
153      */
154     @Test
155     @Alerts({"true",
156              "false-true-false-false-false-false-false-false-false-false-false",
157              "false"})
158     public void validationBlankCustomValidity() throws Exception {
159         validation("<output id='e1'>o1</output>\n", "elem.setCustomValidity(' ');\n");
160     }
161 
162     /**
163      * @throws Exception if an error occurs
164      */
165     @Test
166     @Alerts({"true",
167              "false-false-false-false-false-false-false-false-false-true-false",
168              "false"})
169     public void validationResetCustomValidity() throws Exception {
170         validation("<output id='e1'>o1</output>\n",
171                 "elem.setCustomValidity('Invalid');elem.setCustomValidity('');");
172     }
173 
174     /**
175      * @throws Exception if an error occurs
176      */
177     @Test
178     @Alerts({"true",
179              "false-false-false-false-false-false-false-false-false-true-false",
180              "false"})
181     public void validationRequired() throws Exception {
182         validation("<output id='e1' required></output>\n", "");
183     }
184 
185     private void validation(final String htmlPart, final String jsPart) throws Exception {
186         final String html = DOCTYPE_HTML
187                 + "<html><head>\n"
188                 + "  <script>\n"
189                 + LOG_TITLE_FUNCTION
190                 + "    function logValidityState(s) {\n"
191                 + "      log(s.badInput"
192                         + "+ '-' + s.customError"
193                         + "+ '-' + s.patternMismatch"
194                         + "+ '-' + s.rangeOverflow"
195                         + "+ '-' + s.rangeUnderflow"
196                         + "+ '-' + s.stepMismatch"
197                         + "+ '-' + s.tooLong"
198                         + "+ '-' + s.tooShort"
199                         + " + '-' + s.typeMismatch"
200                         + " + '-' + s.valid"
201                         + " + '-' + s.valueMissing);\n"
202                 + "    }\n"
203                 + "    function test() {\n"
204                 + "      var elem = document.getElementById('e1');\n"
205                 + "      if (!elem.validity) { log('no checkValidity'); return }\n"
206                 + jsPart
207                 + "      log(elem.checkValidity());\n"
208                 + "      logValidityState(elem.validity);\n"
209                 + "      log(elem.willValidate);\n"
210                 + "    }\n"
211                 + "  </script>\n"
212                 + "</head>\n"
213                 + "<body onload='test()'>\n"
214                 + "  <form>\n"
215                 + htmlPart
216                 + "  </form>\n"
217                 + "</body></html>";
218 
219         loadPageVerifyTitle2(html);
220     }
221 }