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.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
28
29
30
31 @RunWith(BrowserRunner.class)
32 public class HtmlOutputTest extends WebDriverTestCase {
33
34
35
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
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
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
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
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
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
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
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 }