1 38 package com.gargoylesoftware.htmlunit.javascript.host; 39 40 import java.util.ArrayList; 41 import java.util.Arrays; 42 import java.util.Collections; 43 import java.util.List; 44 45 import com.gargoylesoftware.htmlunit.CollectingAlertHandler; 46 import com.gargoylesoftware.htmlunit.MockWebConnection; 47 import com.gargoylesoftware.htmlunit.WebClient; 48 import com.gargoylesoftware.htmlunit.WebTestCase; 49 import com.gargoylesoftware.htmlunit.html.HtmlPage; 50 51 52 58 public class CharacterDataImplTest extends WebTestCase { 59 63 public CharacterDataImplTest( final String name ) { 64 super(name); 65 } 66 67 68 72 public void testCharacterDataImpl_textNode() throws Exception { 73 final WebClient webClient = new WebClient(); 74 final MockWebConnection webConnection = new MockWebConnection( webClient ); 75 76 final String firstContent 77 = "<html><head><title>First</title><script>" 78 + "function doTest() {\n" 79 + " var div1=document.getElementById('div1');\n" 80 + " var text1=div1.firstChild;\n" 81 + " alert(text1.data);\n" 82 + " alert(text1.length);\n" 83 + " alert(text1.nodeType);\n" 84 + " alert(text1.nodeValue);\n" 85 + " alert(text1.nodeName);\n" 86 + "}\n" 87 + "</script></head><body onload='doTest()'>" 88 + "<div id='div1'>Some Text</div></body></html>"; 89 90 webConnection.setResponse( 91 URL_FIRST, firstContent, 200, "OK", "text/html", Collections.EMPTY_LIST ); 92 webClient.setWebConnection( webConnection ); 93 94 final List collectedAlerts = new ArrayList(); 95 webClient.setAlertHandler( new CollectingAlertHandler(collectedAlerts) ); 96 97 final HtmlPage firstPage = ( HtmlPage )webClient.getPage( URL_FIRST ); 98 assertEquals( "First", firstPage.getTitleText() ); 99 100 final List expectedAlerts = Arrays.asList( new String[]{ 101 "Some Text", "9", "3", "Some Text", "#text" 102 } ); 103 assertEquals( expectedAlerts, collectedAlerts ); 104 } 105 106 107 111 public void testCharacterDataImpl_setData() throws Exception { 112 final WebClient webClient = new WebClient(); 113 final MockWebConnection webConnection = new MockWebConnection( webClient ); 114 115 final String firstContent 116 = "<html><head><title>First</title><script>" 117 + "function doTest() {\n" 118 + " var div1=document.getElementById('div1');\n" 119 + " var text1=div1.firstChild;\n" 120 + " text1.data='Some New Text';\n" 121 + " alert(text1.data);\n" 122 + " alert(text1.nodeValue);\n" 123 + "}\n" 124 + "</script></head><body onload='doTest()'>" 125 + "<div id='div1'>Some Text</div></body></html>"; 126 127 webConnection.setResponse( 128 URL_FIRST, firstContent, 200, "OK", "text/html", Collections.EMPTY_LIST ); 129 webClient.setWebConnection( webConnection ); 130 131 final List collectedAlerts = new ArrayList(); 132 webClient.setAlertHandler( new CollectingAlertHandler(collectedAlerts) ); 133 134 final HtmlPage firstPage = ( HtmlPage )webClient.getPage( URL_FIRST ); 135 assertEquals( "First", firstPage.getTitleText() ); 136 137 final List expectedAlerts = Arrays.asList( new String[]{ 138 "Some New Text", "Some New Text" 139 } ); 140 assertEquals( expectedAlerts, collectedAlerts ); 141 } 142 143 144 148 public void testCharacterDataImpl_setNodeValue() throws Exception { 149 final WebClient webClient = new WebClient(); 150 final MockWebConnection webConnection = new MockWebConnection( webClient ); 151 152 final String firstContent 153 = "<html><head><title>First</title><script>" 154 + "function doTest() {\n" 155 + " var div1=document.getElementById('div1');\n" 156 + " var text1=div1.firstChild;\n" 157 + " text1.nodeValue='Some New Text';\n" 158 + " alert(text1.data);\n" 159 + " alert(text1.nodeValue);\n" 160 + "}\n" 161 + "</script></head><body onload='doTest()'>" 162 + "<div id='div1'>Some Text</div></body></html>"; 163 164 webConnection.setResponse( 165 URL_FIRST, firstContent, 200, "OK", "text/html", Collections.EMPTY_LIST ); 166 webClient.setWebConnection( webConnection ); 167 168 final List collectedAlerts = new ArrayList(); 169 webClient.setAlertHandler( new CollectingAlertHandler(collectedAlerts) ); 170 171 final HtmlPage firstPage = ( HtmlPage )webClient.getPage( URL_FIRST ); 172 assertEquals( "First", firstPage.getTitleText() ); 173 174 final List expectedAlerts = Arrays.asList( new String[]{ 175 "Some New Text", "Some New Text" 176 } ); 177 assertEquals( expectedAlerts, collectedAlerts ); 178 } 179 180 181 185 public void testCharacterDataImpl_appendData() throws Exception { 186 final WebClient webClient = new WebClient(); 187 final MockWebConnection webConnection = new MockWebConnection( webClient ); 188 189 final String firstContent 190 = "<html><head><title>First</title><script>" 191 + "function doTest() {\n" 192 + " var div1=document.getElementById('div1');\n" 193 + " var text1=div1.firstChild;\n" 194 + " text1.appendData(' Appended');\n" 195 + " alert(text1.data);\n" 196 + "}\n" 197 + "</script></head><body onload='doTest()'>" 198 + "<div id='div1'>Some Text</div></body></html>"; 199 200 webConnection.setResponse( 201 URL_FIRST, firstContent, 200, "OK", "text/html", Collections.EMPTY_LIST ); 202 webClient.setWebConnection( webConnection ); 203 204 final List collectedAlerts = new ArrayList(); 205 webClient.setAlertHandler( new CollectingAlertHandler(collectedAlerts) ); 206 207 final HtmlPage firstPage = ( HtmlPage )webClient.getPage( URL_FIRST ); 208 assertEquals( "First", firstPage.getTitleText() ); 209 210 final List expectedAlerts = Arrays.asList( new String[]{ 211 "Some Text Appended" 212 } ); 213 assertEquals( expectedAlerts, collectedAlerts ); 214 } 215 216 217 221 public void testCharacterDataImpl_deleteData() throws Exception { 222 final WebClient webClient = new WebClient(); 223 final MockWebConnection webConnection = new MockWebConnection( webClient ); 224 225 final String firstContent 226 = "<html><head><title>First</title><script>" 227 + "function doTest() {\n" 228 + " var div1=document.getElementById('div1');\n" 229 + " var text1=div1.firstChild;\n" 230 + " text1.deleteData(5, 11);\n" 231 + " alert(text1.data);\n" 232 + "}\n" 233 + "</script></head><body onload='doTest()'>" 234 + "<div id='div1'>Some Not So New Text</div></body></html>"; 235 236 webConnection.setResponse( 237 URL_FIRST, firstContent, 200, "OK", "text/html", Collections.EMPTY_LIST ); 238 webClient.setWebConnection( webConnection ); 239 240 final List collectedAlerts = new ArrayList(); 241 webClient.setAlertHandler( new CollectingAlertHandler(collectedAlerts) ); 242 243 final HtmlPage firstPage = ( HtmlPage )webClient.getPage( URL_FIRST ); 244 assertEquals( "First", firstPage.getTitleText() ); 245 246 final List expectedAlerts = Arrays.asList( new String[]{ 247 "Some Text" 248 } ); 249 assertEquals( expectedAlerts, collectedAlerts ); 250 } 251 252 253 257 public void testCharacterDataImpl_insertData() throws Exception { 258 final WebClient webClient = new WebClient(); 259 final MockWebConnection webConnection = new MockWebConnection( webClient ); 260 261 final String firstContent 262 = "<html><head><title>First</title><script>" 263 + "function doTest() {\n" 264 + " var div1=document.getElementById('div1');\n" 265 + " var text1=div1.firstChild;\n" 266 + " text1.insertData(5, 'New ');\n" 267 + " alert(text1.data);\n" 268 + "}\n" 269 + "</script></head><body onload='doTest()'>" 270 + "<div id='div1'>Some Text</div></body></html>"; 271 272 webConnection.setResponse( 273 URL_FIRST, firstContent, 200, "OK", "text/html", Collections.EMPTY_LIST ); 274 webClient.setWebConnection( webConnection ); 275 276 final List collectedAlerts = new ArrayList(); 277 webClient.setAlertHandler( new CollectingAlertHandler(collectedAlerts) ); 278 279 final HtmlPage firstPage = ( HtmlPage )webClient.getPage( URL_FIRST ); 280 assertEquals( "First", firstPage.getTitleText() ); 281 282 final List expectedAlerts = Arrays.asList( new String[]{ 283 "Some New Text" 284 } ); 285 assertEquals( expectedAlerts, collectedAlerts ); 286 } 287 288 289 293 public void testCharacterDataImpl_replaceData() throws Exception { 294 final WebClient webClient = new WebClient(); 295 final MockWebConnection webConnection = new MockWebConnection( webClient ); 296 297 final String firstContent 298 = "<html><head><title>First</title><script>" 299 + "function doTest() {\n" 300 + " var div1=document.getElementById('div1');\n" 301 + " var text1=div1.firstChild;\n" 302 + " text1.replaceData(5, 3, 'New');\n" 303 + " alert(text1.data);\n" 304 + "}\n" 305 + "</script></head><body onload='doTest()'>" 306 + "<div id='div1'>Some Old Text</div></body></html>"; 307 308 webConnection.setResponse( 309 URL_FIRST, firstContent, 200, "OK", "text/html", Collections.EMPTY_LIST ); 310 webClient.setWebConnection( webConnection ); 311 312 final List collectedAlerts = new ArrayList(); 313 webClient.setAlertHandler( new CollectingAlertHandler(collectedAlerts) ); 314 315 final HtmlPage firstPage = ( HtmlPage )webClient.getPage( URL_FIRST ); 316 assertEquals( "First", firstPage.getTitleText() ); 317 318 final List expectedAlerts = Arrays.asList( new String[]{ 319 "Some New Text" 320 } ); 321 assertEquals( expectedAlerts, collectedAlerts ); 322 } 323 324 325 329 public void testCharacterDataImpl_substringData() throws Exception { 330 final WebClient webClient = new WebClient(); 331 final MockWebConnection webConnection = new MockWebConnection( webClient ); 332 333 final String firstContent 334 = "<html><head><title>First</title><script>" 335 + "function doTest() {\n" 336 + " var div1=document.getElementById('div1');\n" 337 + " var text1=div1.firstChild;\n" 338 + " alert(text1.substringData(5, 3));\n" 339 + " alert(text1.data);\n" 340 + "}\n" 341 + "</script></head><body onload='doTest()'>" 342 + "<div id='div1'>Some New Text</div></body></html>"; 343 344 webConnection.setResponse( 345 URL_FIRST, firstContent, 200, "OK", "text/html", Collections.EMPTY_LIST ); 346 webClient.setWebConnection( webConnection ); 347 348 final List collectedAlerts = new ArrayList(); 349 webClient.setAlertHandler( new CollectingAlertHandler(collectedAlerts) ); 350 351 final HtmlPage firstPage = ( HtmlPage )webClient.getPage( URL_FIRST ); 352 assertEquals( "First", firstPage.getTitleText() ); 353 354 final List expectedAlerts = Arrays.asList( new String[]{ 355 "New", "Some New Text" 356 } ); 357 assertEquals( expectedAlerts, collectedAlerts ); 358 } 359 360 361 365 public void testTextImpl_splitText() throws Exception { 366 final WebClient webClient = new WebClient(); 367 final MockWebConnection webConnection = new MockWebConnection( webClient ); 368 369 final String firstContent 370 = "<html><head><title>First</title><script>" 371 + "function doTest() {\n" 372 + " var div1=document.getElementById('div1');\n" 373 + " var text1=div1.firstChild;\n" 374 + " var text2=text1.splitText(5);\n" 375 + " alert(text1.data);\n" 376 + " alert(text2.data);\n" 377 + " alert(text1.nextSibling==text2);\n" 378 + "}\n" 379 + "</script></head><body onload='doTest()'>" 380 + "<div id='div1'>Some Text</div></body></html>"; 381 382 webConnection.setResponse( 383 URL_FIRST, firstContent, 200, "OK", "text/html", Collections.EMPTY_LIST ); 384 webClient.setWebConnection( webConnection ); 385 386 final List collectedAlerts = new ArrayList(); 387 webClient.setAlertHandler( new CollectingAlertHandler(collectedAlerts) ); 388 389 final HtmlPage firstPage = ( HtmlPage )webClient.getPage( URL_FIRST ); 390 assertEquals( "First", firstPage.getTitleText() ); 391 392 final List expectedAlerts = Arrays.asList( new String[]{ 393 "Some ", "Text", "true" 394 } ); 395 assertEquals( expectedAlerts, collectedAlerts ); 396 } 397 } 398 | Popular Tags |