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 59 public class NodeImplTest extends WebTestCase { 60 61 64 public NodeImplTest(final String name) { 65 super(name); 66 } 67 68 71 public void test_hasChildNodes_true() throws Exception { 72 final String content = "<html><head><title>test_hasChildNodes</title>" 73 + "<script>" 74 + "function doTest(){" 75 + " alert(document.getElementById('myNode').hasChildNodes());" 76 + "}" 77 + "</script>" 78 + "</head><body onload='doTest()'>" 79 + "<p id='myNode'>hello world<span>Child Node</span></p>" 80 + "</body></html>"; 81 82 final List collectedAlerts = new ArrayList(); 83 final HtmlPage page = loadPage(content, collectedAlerts); 84 assertEquals("test_hasChildNodes", page.getTitleText()); 85 86 final List expectedAlerts = Arrays.asList(new String[]{ 87 "true" 88 }); 89 90 assertEquals(expectedAlerts, collectedAlerts); 91 } 92 95 public void test_hasChildNodes_false() throws Exception { 96 final String content = "<html><head><title>test_hasChildNodes</title>" 97 + "<script>" 98 + "function doTest(){" 99 + " alert(document.getElementById('myNode').hasChildNodes());" 100 + "}" 101 + "</script>" 102 + "</head><body onload='doTest()'>" 103 + "<p id='myNode'></p>" 104 + "</body></html>"; 105 106 final List collectedAlerts = new ArrayList(); 107 final HtmlPage page = loadPage(content, collectedAlerts); 108 assertEquals("test_hasChildNodes", page.getTitleText()); 109 110 final List expectedAlerts = Arrays.asList(new String[]{ 111 "false" 112 }); 113 114 assertEquals(expectedAlerts, collectedAlerts); 115 } 116 120 public void testRemoveChild() throws Exception { 121 final WebClient webClient = new WebClient(); 122 final MockWebConnection webConnection = new MockWebConnection( webClient ); 123 webClient.setWebConnection( webConnection ); 124 125 final String content 126 = "<html><head><title>foo</title><script>\n" 127 + "function doTest(){\n" 128 + " var form = document.forms['form1'];\n" 129 + " var div = form.firstChild;\n" 130 + " var removedDiv = form.removeChild(div);\n" 131 + " alert(div==removedDiv);\n" 132 + " alert(form.firstChild==null);\n" 133 + "}\n" 134 + "</script></head><body onload='doTest()'>\n" 135 + "<form name='form1'><div id='formChild'/></form>" 136 + "</body></html>"; 137 webConnection.setResponse( 138 URL_FIRST, content, 200, "OK", "text/html", Collections.EMPTY_LIST ); 139 140 final List collectedAlerts = new ArrayList(); 141 webClient.setAlertHandler( new CollectingAlertHandler(collectedAlerts) ); 142 143 final HtmlPage page = ( HtmlPage )webClient.getPage( URL_FIRST ); 144 assertEquals("foo", page.getTitleText()); 145 146 final List expectedAlerts = Arrays.asList( new String[]{ 147 "true", "true" 148 } ); 149 150 assertEquals( expectedAlerts, collectedAlerts ); 151 } 152 153 157 public void testReplaceChild() throws Exception { 158 final WebClient webClient = new WebClient(); 159 final MockWebConnection webConnection = new MockWebConnection( webClient ); 160 webClient.setWebConnection( webConnection ); 161 162 final String content 163 = "<html><head><title>foo</title><script>\n" 164 + "function doTest(){\n" 165 + " var form = document.forms['form1'];\n" 166 + " var div1 = form.firstChild;\n" 167 + " var div2 = document.getElementById('newChild');\n" 168 + " var removedDiv = form.replaceChild(div2,div1);\n" 169 + " alert(div1==removedDiv);\n" 170 + " alert(form.firstChild==div2);\n" 171 + "}\n" 172 + "</script></head><body onload='doTest()'>\n" 173 + "<form name='form1'><div id='formChild'/></form>" 174 + "</body><div id='newChild'/></html>"; 175 webConnection.setResponse( 176 URL_FIRST, content, 200, "OK", "text/html", Collections.EMPTY_LIST ); 177 178 final List collectedAlerts = new ArrayList(); 179 webClient.setAlertHandler( new CollectingAlertHandler(collectedAlerts) ); 180 181 final HtmlPage page = ( HtmlPage )webClient.getPage( URL_FIRST ); 182 assertEquals("foo", page.getTitleText()); 183 184 final List expectedAlerts = Arrays.asList( new String[]{ 185 "true", "true" 186 } ); 187 188 assertEquals( expectedAlerts, collectedAlerts ); 189 } 190 191 195 public void testNodeNameIsUppercase() throws Exception { 196 final String content = "<html><head><title>test_hasChildNodes</title>" 197 + "<script>" 198 + "function doTest(){" 199 + " alert(document.getElementById('myNode').nodeName);" 200 + "}" 201 + "</script>" 202 + "</head><body onload='doTest()'>" 203 + "<div id='myNode'>hello world<span>Child Node</span></div>" 204 + "</body></html>"; 205 206 final List collectedAlerts = new ArrayList(); 207 final HtmlPage page = loadPage(content, collectedAlerts); 208 assertEquals("test_hasChildNodes", page.getTitleText()); 209 210 final List expectedAlerts = Arrays.asList(new String[]{ 211 "DIV" 212 }); 213 214 assertEquals(expectedAlerts, collectedAlerts); 215 } 216 217 220 public void test_getChildNodes() throws Exception { 221 final String content = "<html><head><title>test_getChildNodes</title>" 222 + "<script>" 223 + "function doTest() {" 224 + "var aNode = document.getElementById('myNode');" 225 + "alert(aNode.childNodes.length);" 226 + "alert(aNode.childNodes[0].nodeName);" 227 + "alert(aNode.childNodes[0].childNodes.length);" 228 + "alert(aNode.childNodes[0].childNodes[0].nodeName);" 229 + "alert(aNode.childNodes[0].childNodes[1].nodeName);" 230 + "alert(aNode.childNodes[1].nodeName);" 231 + "}" 232 + "</script>" 233 + "</head><body onload='doTest()'>" 234 + "<div id='myNode'><span>Child Node 1-A" 235 + "<h1>Child Node 1-B</h1></span>" 236 + "<h2>Child Node 2-A</h2></div>" 237 + "</body></html>"; 238 239 final List expectedAlerts = Arrays.asList(new String[] { "2", "SPAN", "2", "#text", "H1", "H2" }); 240 createTestPageForRealBrowserIfNeeded(content, expectedAlerts); 241 242 final List collectedAlerts = new ArrayList(); 243 final HtmlPage page = loadPage(content, collectedAlerts); 244 assertEquals("test_getChildNodes", page.getTitleText()); 245 246 assertEquals(expectedAlerts, collectedAlerts); 247 } 248 } 249 | Popular Tags |