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