1
2
3
4
5
6
7
8
9
10
11
12
13
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
26
27
28
29 public class HtmlOutputTest extends WebDriverTestCase {
30
31
32
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
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
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
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
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
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
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
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 }