1 38 package com.gargoylesoftware.htmlunit.html; 39 40 import org.mozilla.javascript.ScriptableObject; 41 42 import com.gargoylesoftware.htmlunit.WebClient; 43 import com.gargoylesoftware.htmlunit.WebTestCase; 44 import com.gargoylesoftware.htmlunit.javascript.host.HTMLElement; 45 46 53 public class HtmlTableRowTest extends WebTestCase { 54 58 public HtmlTableRowTest(final String name) { 59 super(name); 60 } 61 62 private static final String htmlContent = "<html><head><title>foo</title></head><body>" 63 + "<table id='table'><tr id='row'>" 64 + "<td id='cell' width='20'><input type='text' id='foo'/></td>" 65 + "</tr></table>" + "</body></html>"; 66 67 private WebClient client_; 68 69 private HtmlPage page_; 70 private HtmlTable table_; 71 private HtmlTableBody tbody_; 72 private HtmlTableRow row_; 73 private HtmlTableCell cell_; 74 private HtmlTableRow rowClone_; 75 private HtmlTableCell cellClone_; 76 77 78 public void setUp() throws Exception { 79 page_ = loadPage(htmlContent); 80 client_ = page_.getWebClient(); 81 82 table_ = (HtmlTable) page_.getHtmlElementById("table"); 83 tbody_ = (HtmlTableBody) table_.getFirstChild(); 84 row_ = table_.getRow(0); 85 cell_ = row_.getCell(0); 86 87 rowClone_ = (HtmlTableRow) row_.cloneNode(true); 88 cellClone_ = rowClone_.getCell(0); 89 } 90 91 94 public void testClonePreservesOriginal() { 95 assertSame(tbody_, row_.getParentNode()); 96 assertSame(row_, cell_.getParentNode()); 97 assertSame(cell_, row_.getCell(0)); 98 assertEquals("row", row_.getId()); 99 assertEquals("cell", cell_.getId()); 100 } 101 102 106 public void testClonesAreDistinct() throws Exception { 107 assertNotSame(row_, rowClone_); 108 assertNotSame(cell_, cellClone_); 109 } 110 111 114 public void testCloneHasSamePage() { 115 assertSame(cell_.getPage(), cellClone_.getPage()); 116 assertSame(row_.getPage(), rowClone_.getPage()); 117 } 118 119 123 public void testClonedRowHasNullParent() throws Exception { 124 assertNull(rowClone_.getParentNode()); 125 } 126 127 131 public void testClonedRowHasDifferentChildren() throws Exception { 132 assertEquals(row_.getCells().size(), rowClone_.getCells().size()); 133 assertNotSame(row_.getFirstChild(), rowClone_.getFirstChild()); 134 } 135 136 139 public void testClonedCellHasDifferentChildren() { 140 assertNotSame(cell_.getParentNode(), cellClone_.getParentNode()); 141 assertNotNull(cell_.getFirstChild()); 142 assertNotSame(cell_.getFirstChild(), cellClone_.getFirstChild()); 143 } 144 145 149 public void testClonedCellHasClonedRowAsParent() throws Exception { 150 assertSame(rowClone_, cellClone_.getParentNode()); 151 } 152 153 156 public void testClonedRowHasDifferentId() { 157 assertFalse(row_.getId().equals(rowClone_.getId())); 158 assertEquals("", rowClone_.getId()); 159 } 160 161 164 public void testClonedCellHasDifferentId() { 165 assertFalse(cell_.getId().equals(cellClone_.getId())); 166 assertEquals("", cellClone_.getId()); 167 } 168 169 173 public void testClonedCellChildHasDifferentId() { 174 final HtmlElement cellChild = (HtmlElement) cell_.getFirstChild(); 175 final HtmlElement cellChildClone = (HtmlElement) cellClone_.getFirstChild(); 176 assertFalse(cellChild.getId().equals(cellChildClone.getId())); 177 assertEquals("", cellChildClone.getId()); 178 } 179 180 183 public void testCloneAttributesCopiedFromOriginal() { 184 assertEquals("20", cell_.getAttributeValue("width")); 185 assertEquals("20", cellClone_.getAttributeValue("width")); 186 } 187 188 192 public void testCloneAttributeIsIndependentOfOriginal() { 193 cellClone_.setAttributeValue("a", "one"); 194 assertFalse("one".equals(cell_.getAttributeValue("a"))); 195 } 196 197 201 public void testOriginalAttributeIsIndependentOfClone() { 202 cell_.setAttributeValue("a", "one"); 203 assertFalse("one".equals(cellClone_.getAttributeValue("a"))); 204 } 205 206 210 public void testCloneValueIsIndependentOfOriginal() { 211 cellClone_.setNodeValue("one"); 212 assertFalse("one".equals(cell_.getNodeValue())); 213 } 214 215 218 public void testCloneIdIsIndependentOfOriginal() { 219 cellClone_.setNodeValue("one"); 220 assertFalse("one".equals(cell_.getNodeValue())); 221 } 222 223 225 229 public void testScriptCanGetOriginalCell() { 230 final String cmd = "document.getElementById('cell')"; 231 final Object object = client_.getScriptEngine().execute(page_, cmd, "test"); 232 233 final HtmlElement cellElement = ((HTMLElement) object).getHtmlElementOrDie(); 234 assertSame(cell_, cellElement); 235 } 236 237 241 public void testCellScriptObjectIsReturnedByScript() { 242 final String cmd = "document.getElementById('cell')"; 243 final HTMLElement jselement = (HTMLElement) client_.getScriptEngine() 244 .execute(page_, cmd, "test"); 245 246 assertSame(jselement, cell_.getScriptObject()); 247 } 248 249 253 public void testScriptCanSetJsPropertyOnCell() { 254 final String cmd = "document.getElementById('cell').a='original';document.getElementById('cell')"; 255 final Object object = client_.getScriptEngine().execute(page_, cmd, "test"); 256 257 final HTMLElement jselement = ((HTMLElement) object); 258 assertEquals("original", ScriptableObject.getProperty(jselement, "a")); 259 260 assertSame(jselement, cell_.getScriptObject()); 261 } 262 263 266 public void testCloneScriptCanSetDisabledOnCell() { 267 final String cmd = "document.getElementById('cell').disabled='true'"; 268 client_.getScriptEngine().execute(page_, cmd, "test"); 269 assertEquals("disabled", cell_.getAttributeValue("disabled")); 270 } 271 272 275 public void testCloneScriptCanSetAttributeOnCell() { 276 final String cmd = "document.getElementById('cell').setAttribute('a','original')"; 277 client_.getScriptEngine().execute(page_, cmd, "test"); 278 assertEquals("original", cell_.getAttributeValue("a")); 279 } 280 281 283 287 public void testCloneScriptSetAttributeIndependentOfOriginal() { 288 final String cmd = "document.getElementById('cell').setAttribute('a','original')"; 289 client_.getScriptEngine().execute(page_, cmd, "test"); 290 291 assertEquals("original", cell_.getAttributeValue("a")); 292 assertFalse("original".equals(cellClone_.getAttributeValue("a"))); 293 } 294 295 299 public void testCloneScriptSetDisabledIndependentOfOriginal() { 300 final String cmd = "document.getElementById('cell').disabled = 'true'"; 301 client_.getScriptEngine().execute(page_, cmd, "test"); 302 303 assertEquals("disabled", cell_.getAttributeValue("disabled")); 304 assertFalse("disabled".equals(cellClone_.getAttributeValue("disabled"))); 305 } 306 307 312 public void testCloneHasDifferentScriptableObject() { 313 314 final String cmd = "document.getElementById('cell')"; client_.getScriptEngine().execute(page_, cmd, "test"); 317 318 assertNotSame(cell_.getScriptObject(), cellClone_.getScriptObject()); 319 } 320 321 325 public void testScriptDomOperations() { 326 final String cmd = "document.getElementById('foo').value = 'Input!';document.getElementById('foo')"; 327 client_.getScriptEngine().execute(page_, cmd, "test"); 328 329 final HtmlElement input = (HtmlElement) cell_.getFirstChild(); 330 assertEquals("Input!", input.getAttributeValue("value")); 331 332 final HtmlElement inputClone = (HtmlElement) cellClone_.getFirstChild(); 333 assertFalse("Input!".equals(inputClone.getAttributeValue("value"))); 334 } 335 } 336 | Popular Tags |