1 38 package com.gargoylesoftware.htmlunit.javascript.host; 39 40 import java.net.URL; 41 import java.util.ArrayList; 42 import java.util.Arrays; 43 import java.util.List; 44 45 import com.gargoylesoftware.htmlunit.BrowserVersion; 46 import com.gargoylesoftware.htmlunit.KeyValuePair; 47 import com.gargoylesoftware.htmlunit.MockWebConnection; 48 import com.gargoylesoftware.htmlunit.WebClient; 49 import com.gargoylesoftware.htmlunit.WebTestCase; 50 import com.gargoylesoftware.htmlunit.html.HtmlAnchor; 51 import com.gargoylesoftware.htmlunit.html.HtmlButtonInput; 52 import com.gargoylesoftware.htmlunit.html.HtmlCheckBoxInput; 53 import com.gargoylesoftware.htmlunit.html.HtmlForm; 54 import com.gargoylesoftware.htmlunit.html.HtmlPage; 55 import com.gargoylesoftware.htmlunit.html.HtmlRadioButtonInput; 56 import com.gargoylesoftware.htmlunit.html.HtmlSubmitInput; 57 import com.gargoylesoftware.htmlunit.html.HtmlTextInput; 58 59 60 67 public class InputTest extends WebTestCase { 68 72 public InputTest( final String name ) { 73 super(name); 74 } 75 76 77 80 public void testStandardProperties_Text() throws Exception { 81 final String content 82 = "<html><head><title>foo</title><script>" 83 + "function doTest(){\n" 84 + " alert(document.form1.textfield1.value)\n" 85 + " alert(document.form1.textfield1.type)\n" 86 + " alert(document.form1.textfield1.name)\n" 87 + " alert(document.form1.textfield1.form.name)\n" 88 + " document.form1.textfield1.value='cat'\n" 89 + " alert(document.form1.textfield1.value)\n" 90 +"}\n" 91 + "</script></head><body onload='doTest()'>" 92 + "<p>hello world</p>" 93 + "<form name='form1'>" 94 + " <input type='text' name='textfield1' value='foo' />" 95 + "</form>" 96 + "</body></html>"; 97 98 final List collectedAlerts = new ArrayList(); 99 final HtmlPage page = loadPage(content, collectedAlerts); 100 assertEquals("foo", page.getTitleText()); 101 102 final List expectedAlerts = Arrays.asList( new String[]{ 103 "foo", "text", "textfield1", "form1", "cat" 104 } ); 105 106 assertEquals( expectedAlerts, collectedAlerts ); 107 } 108 109 110 113 public void testTextProperties() throws Exception { 114 final String content 115 = "<html><head><title>foo</title><script>" 116 + "function doTest(){\n" 117 + " alert(document.form1.button1.type)\n" 118 + " alert(document.form1.button2.type)\n" 119 + " alert(document.form1.checkbox1.type)\n" 120 + " alert(document.form1.fileupload1.type)\n" 121 + " alert(document.form1.hidden1.type)\n" 122 + " alert(document.form1.select1.type)\n" 123 + " alert(document.form1.select2.type)\n" 124 + " alert(document.form1.password1.type)\n" 125 + " alert(document.form1.reset1.type)\n" 126 + " alert(document.form1.reset2.type)\n" 127 + " alert(document.form1.submit1.type)\n" 128 + " alert(document.form1.submit2.type)\n" 129 + " alert(document.form1.textInput1.type)\n" 130 + " alert(document.form1.textarea1.type)\n" 131 + "}\n" 132 + "</script></head><body onload='doTest()'>" 133 + "<p>hello world</p>" 134 + "<form name='form1'>" 135 + " <input type='button' name='button1'></button>" 136 + " <button type='button' name='button2'></button>" 137 + " <input type='checkbox' name='checkbox1' />" 138 + " <input type='file' name='fileupload1' />" 139 + " <input type='hidden' name='hidden1' />" 140 + " <select name='select1'>" 141 + " <option>foo</option>" 142 + " </select>" 143 + " <select multiple='multiple' name='select2'>" 144 + " <option>foo</option>" 145 + " </select>" 146 + " <input type='password' name='password1' />" 147 + " <input type='radio' name='radio1' />" 148 + " <input type='reset' name='reset1' />" 149 + " <button type='reset' name='reset2'></button>" 150 + " <input type='submit' name='submit1' />" 151 + " <button type='submit' name='submit2'></button>" 152 + " <input type='text' name='textInput1' />" 153 + " <textarea name='textarea1'>foo</textarea>" 154 + "</form>" 155 + "</body></html>"; 156 157 final List collectedAlerts = new ArrayList(); 158 final HtmlPage page = loadPage(content, collectedAlerts); 159 assertEquals("foo", page.getTitleText()); 160 161 final List expectedAlerts = Arrays.asList( new String[]{ 162 "button", "button", "checkbox", "file", "hidden", "select-one", 163 "select-multiple", "password", "reset", "reset", "submit", 164 "submit", "text", "textarea" 165 } ); 166 createTestPageForRealBrowserIfNeeded(content, expectedAlerts); 167 168 assertEquals( expectedAlerts, collectedAlerts ); 169 } 170 171 172 175 public void testCheckedAttribute_Checkbox() throws Exception { 176 final String content 177 = "<html><head><title>foo</title><script>" 178 + "function test() {" 179 + " alert(document.form1.checkbox1.checked)\n" 180 + " document.form1.checkbox1.checked=true\n" 181 + " alert(document.form1.checkbox1.checked)\n" 182 + "}" 183 + "</script></head><body>" 184 + "<p>hello world</p>" 185 + "<form name='form1'>" 186 + " <input type='cheCKbox' name='checkbox1' id='checkbox1' value='foo' />" 187 + "</form>" 188 + "<a HREF='javascript:test()' id='clickme'>click me</a>\n" 189 + "</body></html>"; 190 191 final List collectedAlerts = new ArrayList(); 192 final HtmlPage page = loadPage(content, collectedAlerts); 193 final HtmlCheckBoxInput checkBox = (HtmlCheckBoxInput)page.getHtmlElementById("checkbox1"); 194 assertFalse( checkBox.isChecked() ); 195 ((HtmlAnchor)page.getHtmlElementById("clickme")).click(); 196 assertTrue( checkBox.isChecked() ); 197 198 final List expectedAlerts = Arrays.asList( new String[]{ 199 "false", "true" 200 } ); 201 202 assertEquals( expectedAlerts, collectedAlerts ); 203 } 204 205 206 209 public void testCheckedAttribute_Radio() throws Exception { 210 final String content 211 = "<html><head><title>foo</title><script>" 212 + "function test() {" 213 + " alert(document.form1.radio1[0].checked)\n" 214 + " alert(document.form1.radio1[1].checked)\n" 215 + " alert(document.form1.radio1[2].checked)\n" 216 + " document.form1.radio1[1].checked=true\n" 217 + " alert(document.form1.radio1[0].checked)\n" 218 + " alert(document.form1.radio1[1].checked)\n" 219 + " alert(document.form1.radio1[2].checked)\n" 220 + "}" 221 + "</script></head><body>" 222 + "<p>hello world</p>" 223 + "<form name='form1'>" 224 + " <input type='radio' name='radio1' id='radioA' value='a' checked='checked'/>" 225 + " <input type='RADIO' name='radio1' id='radioB' value='b' />" 226 + " <input type='radio' name='radio1' id='radioC' value='c' />" 227 + "</form>" 228 + "<a HREF='javascript:test()' id='clickme'>click me</a>\n" 229 + "</body></html>"; 230 231 final List collectedAlerts = new ArrayList(); 232 final HtmlPage page = loadPage(content, collectedAlerts); 233 final HtmlRadioButtonInput radioA 234 = (HtmlRadioButtonInput)page.getHtmlElementById("radioA"); 235 final HtmlRadioButtonInput radioB 236 = (HtmlRadioButtonInput)page.getHtmlElementById("radioB"); 237 final HtmlRadioButtonInput radioC 238 = (HtmlRadioButtonInput)page.getHtmlElementById("radioC"); 239 assertTrue( radioA.isChecked() ); 240 assertFalse( radioB.isChecked() ); 241 assertFalse( radioC.isChecked() ); 242 ((HtmlAnchor)page.getHtmlElementById("clickme")).click(); 243 assertFalse( radioA.isChecked() ); 244 assertTrue( radioB.isChecked() ); 245 assertFalse( radioC.isChecked() ); 246 247 final List expectedAlerts = Arrays.asList( new String[]{ 248 "true", "false", "false", "false", "true", "false" 249 } ); 250 251 assertEquals( expectedAlerts, collectedAlerts ); 252 } 253 254 255 258 public void testDisabledAttribute() throws Exception { 259 final String content 260 = "<html><head><title>foo</title><script>" 261 + "function test() {" 262 + " alert(document.form1.button1.disabled)\n" 263 + " alert(document.form1.button2.disabled)\n" 264 + " alert(document.form1.button3.disabled)\n" 265 + " document.form1.button1.disabled=true\n" 266 + " document.form1.button2.disabled=false\n" 267 + " document.form1.button3.disabled=true\n" 268 + " alert(document.form1.button1.disabled)\n" 269 + " alert(document.form1.button2.disabled)\n" 270 + " alert(document.form1.button3.disabled)\n" 271 + "}" 272 + "</script></head><body>" 273 + "<p>hello world</p>" 274 + "<form name='form1'>" 275 + " <input type='submit' name='button1' value='1'/>" 276 + " <input type='submit' name='button2' value='2' disabled/>" 277 + " <input type='submit' name='button3' value='3'/>" 278 + "</form>" 279 + "<a HREF='javascript:test()' id='clickme'>click me</a>\n" 280 + "</body></html>"; 281 282 final List collectedAlerts = new ArrayList(); 283 final HtmlPage page = loadPage(content, collectedAlerts); 284 final HtmlForm form = page.getFormByName("form1"); 285 286 final HtmlSubmitInput button1 287 = (HtmlSubmitInput)form.getInputByName("button1"); 288 final HtmlSubmitInput button2 289 = (HtmlSubmitInput)form.getInputByName("button2"); 290 final HtmlSubmitInput button3 291 = (HtmlSubmitInput)form.getInputByName("button3"); 292 assertFalse( button1.isDisabled() ); 293 assertTrue ( button2.isDisabled() ); 294 assertFalse( button3.isDisabled() ); 295 ((HtmlAnchor)page.getHtmlElementById("clickme")).click(); 296 assertTrue ( button1.isDisabled() ); 297 assertFalse( button2.isDisabled() ); 298 assertTrue ( button3.isDisabled() ); 299 300 final List expectedAlerts = Arrays.asList( new String[]{ 301 "false", "true", "false", "true", "false", "true" 302 } ); 303 304 assertEquals( expectedAlerts, collectedAlerts ); 305 } 306 307 308 311 public void testInputValue() throws Exception { 312 final String htmlContent = 313 "<html><head><title>foo</title><script>" 314 + "function doTest(){\n" 315 + " document.form1.textfield1.value = 'blue';" 316 + "}\n" 317 + "</script></head><body>" 318 + "<p>hello world</p>" 319 + "<form name='form1' method='post' onsubmit='doTest()'>" 320 + " <input type='text' name='textfield1' id='textfield1' value='foo' />" 321 + "</form>" 322 + "</body></html>"; 323 324 final WebClient client = new WebClient(BrowserVersion.MOZILLA_1_0); 325 326 final MockWebConnection webConnection = new MockWebConnection(client); 327 webConnection.setDefaultResponse(htmlContent); 328 client.setWebConnection(webConnection); 329 330 final URL url = URL_GARGOYLE; 331 final HtmlPage page = (HtmlPage) client.getPage(url); 332 333 final HtmlForm form = page.getFormByName("form1"); 334 form.submit(); 335 } 336 339 public void testInputSelect_NotDefinedAsPropertyAndFunction() throws Exception { 340 final String htmlContent = 341 "<html><head><title>foo</title><script>" 342 + "function doTest(){\n" 343 + " document.form1.textfield1.select();" 344 + "}\n" 345 + "</script></head><body>" 346 + "<p>hello world</p>" 347 + "<form name='form1' method='post' onsubmit='doTest()'>" 348 + " <input type='text' name='textfield1' id='textfield1' value='foo' />" 349 + "</form>" 350 + "</body></html>"; 351 352 final WebClient client = new WebClient(BrowserVersion.MOZILLA_1_0); 353 354 final MockWebConnection webConnection = new MockWebConnection(client); 355 webConnection.setDefaultResponse(htmlContent); 356 client.setWebConnection(webConnection); 357 358 final URL url = URL_GARGOYLE; 359 final HtmlPage page = (HtmlPage) client.getPage(url); 360 361 final HtmlForm form = page.getFormByName("form1"); 362 form.submit(); 363 } 364 365 368 public void testThisDotFormInOnClick() throws Exception { 369 final String htmlContent = "<html>" 370 + "<head><title>First</title></head>" 371 + "<body>" 372 + "<form name='form1'>" 373 + "<input type='submit' name='button1' onClick=\"this.form.target='_blank'; return false;\">" 374 + "</form>" 375 + "</body></html>"; 376 377 final HtmlPage page = loadPage(htmlContent); 378 assertEquals("First", page.getTitleText()); 379 380 assertEquals("", page.getFormByName("form1").getTargetAttribute()); 381 382 ((HtmlSubmitInput) page.getFormByName("form1").getInputByName("button1")).click(); 383 384 assertEquals("_blank", page.getFormByName("form1").getTargetAttribute()); 385 } 386 387 390 public void testFieldDotForm() throws Exception { 391 final String htmlContent = "<html>" 392 + "<head><title>foo</title><script>" 393 + "function test(){\n" 394 + " var f = document.form1;\n" 395 + " alert(f == f.mySubmit.form);\n" 396 + " alert(f == f.myText.form);\n" 397 + " alert(f == f.myPassword.form);\n" 398 + " alert(f == document.getElementById('myImage').form);\n" 399 + " alert(f == f.myButton.form);\n" 400 + "}\n" 401 + "</script></head>" 402 + "<body onload='test()'>" 403 + "<form name='form1'>" 404 + "<input type='submit' name='mySubmit'>" 405 + "<input type='text' name='myText'>" 406 + "<input type='password' name='myPassword'>" 407 + "<input type='button' name='myButton'>" 408 + "<input type='image' SRC='foo' name='myImage' id='myImage'>" 409 + "</form>" 410 + "</body></html>"; 411 412 final List collectedAlerts = new ArrayList(); 413 final List expectedAlerts = Arrays.asList(new String[] {"true", "true", "true", "true", "true"}); 414 createTestPageForRealBrowserIfNeeded(htmlContent, expectedAlerts); 415 loadPage(htmlContent, collectedAlerts); 416 assertEquals(expectedAlerts, collectedAlerts); 417 } 418 419 422 public void testInputNameChange() throws Exception { 423 final String htmlContent = "<html><head><title>foo</title><script>" 424 + "function doTest(){\n" 425 + " document.form1.textfield1.name = 'changed';" 426 + " alert(document.form1.changed.name);" 427 + "}\n" 428 + "</script></head><body>" 429 + "<p>hello world</p>" 430 + "<form name='form1' method='post' onsubmit='doTest()'>" 431 + " <input type='text' name='textfield1' id='textfield1' value='foo' />" 432 + " <input type='submit' name='button1' value='pushme' />" 433 + "</form>" 434 + "</body></html>"; 435 436 final List collectedAlerts = new ArrayList(); 437 final HtmlPage page = loadPage(htmlContent, collectedAlerts); 438 final MockWebConnection connection = (MockWebConnection) page.getWebClient() 439 .getWebConnection(); 440 441 final HtmlForm form = page.getFormByName("form1"); 442 form.getInputByName("button1").click(); 443 444 final List expectedAlerts = Arrays.asList(new String[] {"changed"}); 445 assertEquals(expectedAlerts, collectedAlerts); 446 447 final List expectedParameters = Arrays.asList(new Object[] { 448 new KeyValuePair("changed", "foo"), 449 new KeyValuePair("button1", "pushme") 450 }); 451 assertEquals(expectedParameters, connection.getLastParameters()); 452 } 453 454 457 public void testOnChange() throws Exception { 458 final String htmlContent = "<html><head><title>foo</title>" 459 + "</head><body>" 460 + "<p>hello world</p>" 461 + "<form name='form1'>" 462 + " <input type='text' name='text1' onchange='alert(this.value)'>" 463 + "<input name='myButton' type='button' onclick='document.form1.text1.value=\"from button\"'>" 464 + "</form>" 465 + "</body></html>"; 466 467 final List collectedAlerts = new ArrayList(); 468 final HtmlPage page = loadPage(htmlContent, collectedAlerts); 469 470 final HtmlForm form = page.getFormByName("form1"); 471 final HtmlTextInput textinput = (HtmlTextInput) form.getInputByName("text1"); 472 textinput.setValueAttribute("foo"); 473 final HtmlButtonInput button = (HtmlButtonInput) form.getInputByName("myButton"); 474 button.click(); 475 assertEquals("from button", textinput.getValueAttribute()); 476 477 final List expectedAlerts = Arrays.asList(new String[] {"foo"}); 478 assertEquals(expectedAlerts, collectedAlerts); 479 } 480 481 484 public void testOnChangeSetByJavaScript() throws Exception { 485 final String htmlContent = "<html><head><title>foo</title>" 486 + "</head><body>" 487 + "<p>hello world</p>" 488 + "<form name='form1'>" 489 + " <input type='text' name='text1' id='text1'>" 490 + "<input name='myButton' type='button' onclick='document.form1.text1.value=\"from button\"'>" 491 + "</form>" 492 + "<script>" 493 + "document.getElementById('text1').onchange= function(event) { alert(this.value) };" 494 + "</script>" 495 + "</body></html>"; 496 497 final List collectedAlerts = new ArrayList(); 498 final HtmlPage page = loadPage(htmlContent, collectedAlerts); 499 500 final HtmlForm form = page.getFormByName("form1"); 501 final HtmlTextInput textinput = (HtmlTextInput) form.getInputByName("text1"); 502 textinput.setValueAttribute("foo"); 503 final HtmlButtonInput button = (HtmlButtonInput) form.getInputByName("myButton"); 504 button.click(); 505 assertEquals("from button", textinput.getValueAttribute()); 506 507 final List expectedAlerts = Arrays.asList(new String[] {"foo"}); 508 assertEquals(expectedAlerts, collectedAlerts); 509 createTestPageForRealBrowserIfNeeded(htmlContent, expectedAlerts); 510 } 511 512 516 public void testDefautValue() throws Exception { 517 final String content 518 = "<html><head><title>First</title><script>\n" 519 + "function doTest() {\n" 520 + " alert(document.myForm.myRadio.value);\n" 521 + " alert(document.myForm.myCheckbox.value);\n" 522 + "}\n</script></head>" 523 + "<body onload='doTest()'>\n" 524 + "<form name='myForm' action='foo'>\n" 525 + "<input type='radio' name='myRadio'/>" 526 + "<input type='checkbox' name='myCheckbox'/>" 527 + "</form></body></html>"; 528 529 final List collectedAlerts = new ArrayList(); 530 loadPage(content, collectedAlerts); 531 532 final List expectedAlerts = Arrays.asList( new String[]{"on", "on"} ); 533 assertEquals( expectedAlerts, collectedAlerts ); 534 } 535 536 541 public void testChangeType() throws Exception { 542 final String content 543 = "<html><head><title>First</title><script>\n" 544 + "function doTest() {\n" 545 + " var input = document.myForm.myRadio;\n" 546 + " alert(input.type);\n" 547 + " input.type = 'hidden';\n" 548 + " alert(input.type);\n" 549 + "}\n</script></head>" 550 + "<body onload='doTest()'>\n" 551 + "<form name='myForm' action='foo'>\n" 552 + "<input type='radio' name='myRadio'/>" 553 + "</form></body></html>"; 554 555 final List expectedAlerts = Arrays.asList( new String[]{"radio", "hidden"} ); 556 createTestPageForRealBrowserIfNeeded(content, expectedAlerts); 557 558 final List collectedAlerts = new ArrayList(); 559 loadPage(content, collectedAlerts); 560 assertEquals( expectedAlerts, collectedAlerts ); 561 } 562 563 568 public void testDefaultValues() throws Exception { 569 if (true) { 570 notImplemented(); 571 return; 572 } 573 574 final String content 575 = "<html><head></head><body>\n" 576 + "<form name='myForm'>\n" 577 + "<input type='button' name='myButton'/>\n" 578 + "<input type='submit' name='mySubmit' value='submit it!'/>\n" 579 + "<input type='file' name='myFile'/>\n" 580 + "<input type='checkbox' name='myCheckbox' checked='true'/>\n" 581 + "<input type='radio' name='myRadio' checked='true'/>\n" 582 + "<input type='text' name='myText'/>\n" 583 + "<input type='password' name='myPwd'/>\n" 584 + "</form>\n" 585 + "<script>\n" 586 + "function details(_oInput)\n" 587 + "{\n" 588 + " alert(_oInput.type + ': '\n" 589 + " + _oInput.checked + ', ' \n" 590 + " + _oInput.defaultChecked + ', '\n" 591 + " + ((String(_oInput.click).indexOf('function') > 0) ? 'function' : 'unknown') + ', '\n" 592 + " + ((String(_oInput.select).indexOf('function') > 0) ? 'function' : 'unknown') + ', '\n" 593 + " + _oInput.defaultValue + ', '\n" 594 + " + _oInput.value\n" 595 + " );\n" 596 + "}\n" 597 + "var oForm = document.myForm;\n" 598 + "details(oForm.myButton);\n" 599 + "details(oForm.mySubmit);\n" 600 + "details(oForm.myFile);\n" 601 + "details(oForm.myCheckbox);\n" 602 + "details(oForm.myRadio);\n" 603 + "details(oForm.myText);\n" 604 + "details(oForm.myPwd);\n" 605 + "</script>\n" 606 + "</body></html>"; 607 final List expectedAlerts = Arrays.asList( new String[]{ 608 "button: false, false, function, function, , ", 609 "submit: false, false, function, function, submit it!, submit it!", 610 "file: false, false, function, function, , ", 611 "checkbox: true, true, function, function, , on", 612 "radio: true, true, function, function, , on", 613 "text: false, false, function, function, , ", 614 "password: false, false, function, function, , " 615 } ); 616 createTestPageForRealBrowserIfNeeded(content, expectedAlerts); 617 618 final List collectedAlerts = new ArrayList(); 619 loadPage(content, collectedAlerts); 620 assertEquals( expectedAlerts, collectedAlerts ); 621 } 622 } 623 | Popular Tags |