Source Code Cross Referenced for DomNodeTest.java in  » Testing » htmlunit » com » gargoylesoftware » htmlunit » html » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Testing » htmlunit » com.gargoylesoftware.htmlunit.html 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Copyright (c) 2002-2008 Gargoyle Software Inc. All rights reserved.
003:         *
004:         * Redistribution and use in source and binary forms, with or without
005:         * modification, are permitted provided that the following conditions are met:
006:         *
007:         * 1. Redistributions of source code must retain the above copyright notice,
008:         *    this list of conditions and the following disclaimer.
009:         * 2. Redistributions in binary form must reproduce the above copyright notice,
010:         *    this list of conditions and the following disclaimer in the documentation
011:         *    and/or other materials provided with the distribution.
012:         * 3. The end-user documentation included with the redistribution, if any, must
013:         *    include the following acknowledgment:
014:         *
015:         *       "This product includes software developed by Gargoyle Software Inc.
016:         *        (http://www.GargoyleSoftware.com/)."
017:         *
018:         *    Alternately, this acknowledgment may appear in the software itself, if
019:         *    and wherever such third-party acknowledgments normally appear.
020:         * 4. The name "Gargoyle Software" must not be used to endorse or promote
021:         *    products derived from this software without prior written permission.
022:         *    For written permission, please contact info@GargoyleSoftware.com.
023:         * 5. Products derived from this software may not be called "HtmlUnit", nor may
024:         *    "HtmlUnit" appear in their name, without prior written permission of
025:         *    Gargoyle Software Inc.
026:         *
027:         * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
028:         * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
029:         * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GARGOYLE
030:         * SOFTWARE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
031:         * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
032:         * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
033:         * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
034:         * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
035:         * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
036:         * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
037:         */
038:        package com.gargoylesoftware.htmlunit.html;
039:
040:        import java.util.ArrayList;
041:        import java.util.Iterator;
042:        import java.util.List;
043:
044:        import org.xml.sax.helpers.AttributesImpl;
045:
046:        import com.gargoylesoftware.htmlunit.ElementNotFoundException;
047:        import com.gargoylesoftware.htmlunit.WebTestCase;
048:        import com.gargoylesoftware.htmlunit.html.DomNode.DescendantElementsIterator;
049:
050:        /**
051:         *  Tests for {@link DomNode}.
052:         *
053:         * @version $Revision: 2132 $
054:         * @author Chris Erskine
055:         * @author Ahmed Ashour
056:         */
057:        public class DomNodeTest extends WebTestCase {
058:
059:            /**
060:             * Create an instance
061:             *
062:             * @param name Name of the test
063:             */
064:            public DomNodeTest(final String name) {
065:                super (name);
066:            }
067:
068:            /**
069:             * Test hasAttributes() on an element with attributes.
070:             * @throws Exception if the test fails
071:             */
072:            public void testElementHasAttributesWith() throws Exception {
073:                final String content = "<html><head></head><body id='tag'>text</body></html>";
074:                final List collectedAlerts = new ArrayList();
075:                final HtmlPage page = loadPage(content, collectedAlerts);
076:
077:                final DomNode node = page.getDocumentHtmlElement()
078:                        .getHtmlElementById("tag");
079:                assertEquals("Element should have attribute", true, node
080:                        .hasAttributes());
081:            }
082:
083:            /**
084:             * Test hasAttributes() on an element with no attributes.
085:             * @throws Exception if the test fails
086:             */
087:            public void testElementHasAttributesNone() throws Exception {
088:                final String content = "<html><head></head><body id='tag'>text</body></html>";
089:                final List collectedAlerts = new ArrayList();
090:                final HtmlPage page = loadPage(content, collectedAlerts);
091:
092:                final DomNode node = page.getDocumentHtmlElement()
093:                        .getHtmlElementById("tag");
094:                final DomNode parent = node.getParentDomNode();
095:                assertEquals("Element should not have attribute", false, parent
096:                        .hasAttributes());
097:            }
098:
099:            /**
100:             * Test hasAttributes on a node that is not defined to have attributes.
101:             * @throws Exception if the test fails
102:             */
103:            public void testNonElementHasAttributes() throws Exception {
104:                final String content = "<html><head></head><body id='tag'>text</body></html>";
105:                final List collectedAlerts = new ArrayList();
106:                final HtmlPage page = loadPage(content, collectedAlerts);
107:
108:                final DomNode node = page.getDocumentHtmlElement()
109:                        .getHtmlElementById("tag");
110:                final DomNode child = node.getFirstDomChild();
111:                assertEquals("Text should not have attribute", false, child
112:                        .hasAttributes());
113:            }
114:
115:            /**
116:             * Test getPrefix on a node that is not defined to have a prefix.
117:             * @throws Exception if the test fails
118:             */
119:            public void testNonElementGetPrefix() throws Exception {
120:                final String content = "<html><head></head><body id='tag'>text</body></html>";
121:                final List collectedAlerts = new ArrayList();
122:                final HtmlPage page = loadPage(content, collectedAlerts);
123:
124:                final DomNode node = page.getDocumentHtmlElement()
125:                        .getHtmlElementById("tag");
126:                final DomNode child = node.getFirstDomChild();
127:                assertEquals("Text should not have a prefix", null, child
128:                        .getPrefix());
129:            }
130:
131:            /**
132:             * Test getNamespaceURI on a node that is not defined to have a namespace.
133:             * @throws Exception if the test fails
134:             */
135:            public void testNonElementGetNamespaceURI() throws Exception {
136:                final String content = "<html><head></head><body id='tag'>text</body></html>";
137:                final List collectedAlerts = new ArrayList();
138:                final HtmlPage page = loadPage(content, collectedAlerts);
139:
140:                final DomNode node = page.getDocumentHtmlElement()
141:                        .getHtmlElementById("tag");
142:                final DomNode child = node.getFirstDomChild();
143:                assertEquals("Text should not have a prefix", null, child
144:                        .getNamespaceURI());
145:            }
146:
147:            /**
148:             * Test getLocalName on a node that is not defined to have a local name.
149:             * @throws Exception if the test fails
150:             */
151:            public void testNonElementGetLocalName() throws Exception {
152:                final String content = "<html><head></head><body id='tag'>text</body></html>";
153:                final List collectedAlerts = new ArrayList();
154:                final HtmlPage page = loadPage(content, collectedAlerts);
155:
156:                final DomNode node = page.getDocumentHtmlElement()
157:                        .getHtmlElementById("tag");
158:                final DomNode child = node.getFirstDomChild();
159:                assertEquals("Text should not have a prefix", null, child
160:                        .getLocalName());
161:            }
162:
163:            /**
164:             * Test setPrefix on a node that is not defined to have a prefix.
165:             * @throws Exception if the test fails
166:             */
167:            public void testNonElementSetPrefix() throws Exception {
168:                final String content = "<html><head></head><body id='tag'>text</body></html>";
169:                final List collectedAlerts = new ArrayList();
170:                final HtmlPage page = loadPage(content, collectedAlerts);
171:
172:                final DomNode node = page.getDocumentHtmlElement()
173:                        .getHtmlElementById("tag");
174:                final DomNode child = node.getFirstDomChild();
175:                child.setPrefix("bar"); // This does nothing.
176:                assertEquals("Text should not have a prefix", null, child
177:                        .getPrefix());
178:            }
179:
180:            /**
181:             * @throws Exception if the test fails
182:             */
183:            public void testRemoveAllChildren() throws Exception {
184:                final String content = "<html><head></head><body>\n"
185:                        + "<p id='tag'><table>\n" + "<tr><td>row 1</td></tr>\n"
186:                        + "<tr><td>row 2</td></tr>\n"
187:                        + "</table></p></body></html>";
188:                final List collectedAlerts = new ArrayList();
189:                final HtmlPage page = loadPage(content, collectedAlerts);
190:
191:                final DomNode node = page.getDocumentHtmlElement()
192:                        .getHtmlElementById("tag");
193:                node.removeAllChildren();
194:                assertEquals("Did not remove all nodes", null, node
195:                        .getFirstDomChild());
196:            }
197:
198:            /**
199:             * @throws Exception if the test fails
200:             */
201:            public void testReplace() throws Exception {
202:                final String content = "<html><head></head><body>\n"
203:                        + "<br><div id='tag'/><br><div id='tag2'/></body></html>";
204:                final HtmlPage page = loadPage(content);
205:
206:                final DomNode node = page.getDocumentHtmlElement()
207:                        .getHtmlElementById("tag");
208:
209:                final DomNode previousSibling = node.getPreviousDomSibling();
210:                final DomNode nextSibling = node.getNextDomSibling();
211:                final DomNode parent = node.getParentDomNode();
212:
213:                // position among parent's children
214:                final int position = readPositionAmongParentChildren(node);
215:
216:                final DomNode newNode = new DomText(page, "test");
217:                node.replace(newNode);
218:                assertSame("previous sibling", previousSibling, newNode
219:                        .getPreviousDomSibling());
220:                assertSame("next sibling", nextSibling, newNode
221:                        .getNextDomSibling());
222:                assertSame("parent", parent, newNode.getParentDomNode());
223:                assertSame(newNode, previousSibling.getNextDomSibling());
224:                assertSame(newNode, nextSibling.getPreviousDomSibling());
225:                assertEquals(position, readPositionAmongParentChildren(newNode));
226:
227:                final AttributesImpl attributes = new AttributesImpl();
228:                attributes.addAttribute(null, "id", "id", null, "tag2"); // with the same id as the node to replace
229:                final DomNode node2 = page.getHtmlElementById("tag2");
230:                assertEquals("div", node2.getNodeName());
231:
232:                final DomNode node3 = HTMLParser.getFactory(HtmlSpan.TAG_NAME)
233:                        .createElement(page, HtmlSpan.TAG_NAME, attributes);
234:                node2.replace(node3);
235:                assertEquals("span", page.getHtmlElementById("tag2")
236:                        .getTagName());
237:            }
238:
239:            /**
240:             * @throws Exception if the test fails
241:             */
242:            public void testGetNewNodeById() throws Exception {
243:                final String content = "<html><head></head><body>\n"
244:                        + "<br><div id='tag'/></body></html>";
245:                final HtmlPage page = loadPage(content);
246:
247:                final DomNode node = page.getDocumentHtmlElement()
248:                        .getHtmlElementById("tag");
249:
250:                final AttributesImpl attributes = new AttributesImpl();
251:                attributes.addAttribute(null, "id", "id", null, "newElt");
252:                final DomNode newNode = HTMLParser.getFactory(
253:                        HtmlDivision.TAG_NAME).createElement(page,
254:                        HtmlDivision.TAG_NAME, attributes);
255:                try {
256:                    page.getHtmlElementById("newElt");
257:                    fail("Element should not exist yet");
258:                } catch (final ElementNotFoundException e) {
259:                    // nothing to do, it's ok
260:                }
261:
262:                node.replace(newNode);
263:
264:                page.getHtmlElementById("newElt");
265:                try {
266:                    page.getHtmlElementById("tag");
267:                    fail("Element should not exist anymore");
268:                } catch (final ElementNotFoundException e) {
269:                    // nothing to do, it's ok
270:                }
271:
272:                newNode.insertBefore(node);
273:                page.getHtmlElementById("tag");
274:            }
275:
276:            /**
277:             * @throws Exception if the test fails
278:             */
279:            public void testAppendChild() throws Exception {
280:                final String content = "<html><head></head><body>\n"
281:                        + "<br><div><div id='tag'/></div><br></body></html>";
282:                final HtmlPage page = loadPage(content);
283:
284:                final DomNode node = page.getDocumentHtmlElement()
285:                        .getHtmlElementById("tag");
286:
287:                final DomNode parent = node.getParentDomNode();
288:
289:                // position among parent's children
290:                final int position = readPositionAmongParentChildren(node);
291:
292:                final DomNode newNode = new DomText(page, "test");
293:                parent.appendDomChild(newNode);
294:                assertSame("new node previous sibling", node, newNode
295:                        .getPreviousDomSibling());
296:                assertSame("new node next sibling", null, newNode
297:                        .getNextDomSibling());
298:                assertSame("next sibling", newNode, node.getNextDomSibling());
299:                assertSame("parent", parent, newNode.getParentDomNode());
300:                assertEquals(position + 1,
301:                        readPositionAmongParentChildren(newNode));
302:
303:                final DomNode newNode2 = new DomText(page, "test2");
304:                parent.appendDomChild(newNode2);
305:                page.getHtmlElementById("tag");
306:            }
307:
308:            /**
309:             * @throws Exception if the test fails
310:             */
311:            public void testInsertBefore() throws Exception {
312:                final String content = "<html><head></head><body>\n"
313:                        + "<br><div id='tag'/><br></body></html>";
314:                final HtmlPage page = loadPage(content);
315:
316:                final DomNode node = page.getDocumentHtmlElement()
317:                        .getHtmlElementById("tag");
318:
319:                final DomNode previousSibling = node.getPreviousDomSibling();
320:                final DomNode nextSibling = node.getNextDomSibling();
321:                final DomNode parent = node.getParentDomNode();
322:
323:                // position among parent's children
324:                final int position = readPositionAmongParentChildren(node);
325:
326:                final DomNode newNode = new DomText(page, "test");
327:                node.insertBefore(newNode);
328:                assertSame("new node previous sibling", previousSibling,
329:                        newNode.getPreviousDomSibling());
330:                assertSame("previous sibling", newNode, node
331:                        .getPreviousDomSibling());
332:                assertSame("new node next sibling", node, newNode
333:                        .getNextDomSibling());
334:                assertSame("next sibling", nextSibling, node
335:                        .getNextDomSibling());
336:                assertSame("parent", parent, newNode.getParentDomNode());
337:                assertSame(newNode, previousSibling.getNextDomSibling());
338:                assertSame(node, nextSibling.getPreviousDomSibling());
339:                assertEquals(position, readPositionAmongParentChildren(newNode));
340:            }
341:
342:            /**
343:             * Reads the position of the node among the children of its parent
344:             * @param node the node to look at
345:             * @return the position
346:             */
347:            private int readPositionAmongParentChildren(final DomNode node) {
348:                int i = 0;
349:                for (final Iterator iter = node.getParentDomNode()
350:                        .getChildIterator(); iter.hasNext();) {
351:                    final DomNode child = (DomNode) iter.next();
352:                    if (child == node) {
353:                        return i;
354:                    }
355:                    i++;
356:                }
357:
358:                return -1;
359:            }
360:
361:            /**
362:             * @throws Exception if the test fails
363:             */
364:            public void testGetByXPath() throws Exception {
365:                final String htmlContent = "<html><head><title>my title</title></head><body>\n"
366:                        + "<p id='p1'><ul><li>foo 1</li><li>foo 2</li></li></p>\n"
367:                        + "<div><span>bla</span></div>\n" + "</body></html>";
368:                final HtmlPage page = loadPage(htmlContent);
369:
370:                final List results = page.getByXPath("//title");
371:                assertEquals(1, results.size());
372:                final HtmlTitle title = (HtmlTitle) results.get(0);
373:                assertEquals("my title", title.asText());
374:
375:                final HtmlHead head = (HtmlHead) title.getParentDomNode();
376:                assertEquals(results, head.getByXPath("//title"));
377:                assertEquals(results, head.getByXPath("/title"));
378:                assertEquals(results, head.getByXPath("title"));
379:
380:                final HtmlParagraph p = (HtmlParagraph) page
381:                        .getFirstByXPath("//p");
382:                assertSame(p, page.getHtmlElementById("p1"));
383:                final List lis = p.getByXPath("ul/li");
384:                assertEquals(2, lis.size());
385:                assertEquals(lis, page.getByXPath("//ul/li"));
386:
387:                assertEquals(2, ((Number) p.getFirstByXPath("count(//li)"))
388:                        .intValue());
389:            }
390:
391:            /**
392:             * @throws Exception if the test fails
393:             */
394:            public void testGetFirstByXPath() throws Exception {
395:                final String htmlContent = "<html><head><title>my title</title></head><body>\n"
396:                        + "<p id='p1'><ul><li>foo 1</li><li>foo 2</li></li></p>\n"
397:                        + "<div><span>bla</span></div>\n" + "</body></html>";
398:                final HtmlPage page = loadPage(htmlContent);
399:
400:                final HtmlTitle title = (HtmlTitle) page
401:                        .getFirstByXPath("//title");
402:                assertEquals("my title", title.asText());
403:
404:                final HtmlHead head = (HtmlHead) title.getParentDomNode();
405:                assertSame(title, head.getFirstByXPath("//title"));
406:                assertSame(title, head.getFirstByXPath("/title"));
407:                assertSame(title, head.getFirstByXPath("title"));
408:
409:                final HtmlParagraph p = (HtmlParagraph) page
410:                        .getFirstByXPath("//p");
411:                assertSame(p, page.getHtmlElementById("p1"));
412:                final HtmlListItem listItem = (HtmlListItem) p
413:                        .getFirstByXPath("ul/li");
414:                assertSame(listItem, page.getFirstByXPath("//ul/li"));
415:
416:                assertEquals(2, ((Number) p.getFirstByXPath("count(//li)"))
417:                        .intValue());
418:            }
419:
420:            /**
421:             * Verifies that {@link DomNode#getAllHtmlChildElements()} returns descendant elements in the correct order.
422:             * @throws Exception If an error occurs.
423:             */
424:            public void testGetAllHtmlChildElementsOrder() throws Exception {
425:                final String html = "<html><body id='0'>\n"
426:                        + "<span id='I'><span id='I.1'><span id='I.1.a'/><span id='I.1.b'/><span id='I.1.c'/></span>\n"
427:                        + "<span id='I.2'><span id='I.2.a'/></span></span>\n"
428:                        + "<span id='II'/>\n"
429:                        + "<span id='III'><span id='III.1'><span id='III.1.a'/></span></span>\n"
430:                        + "</body></html>";
431:                final HtmlPage page = loadPage(html);
432:                final DescendantElementsIterator iterator = (DescendantElementsIterator) page
433:                        .getDocumentHtmlElement().getAllHtmlChildElements();
434:                assertEquals("", iterator.nextElement().getId());
435:                assertEquals("0", iterator.nextElement().getId());
436:                assertEquals("I", iterator.nextElement().getId());
437:                assertEquals("I.1", iterator.nextElement().getId());
438:                assertEquals("I.1.a", iterator.nextElement().getId());
439:                assertEquals("I.1.b", iterator.nextElement().getId());
440:                assertEquals("I.1.c", iterator.nextElement().getId());
441:                assertEquals("I.2", iterator.nextElement().getId());
442:                assertEquals("I.2.a", iterator.nextElement().getId());
443:                assertEquals("II", iterator.nextElement().getId());
444:                assertEquals("III", iterator.nextElement().getId());
445:                assertEquals("III.1", iterator.nextElement().getId());
446:                assertEquals("III.1.a", iterator.nextElement().getId());
447:                assertFalse(iterator.hasNext());
448:            }
449:
450:            static class DomChangeListenerTestImpl implements  DomChangeListener {
451:                private final List collectedValues_ = new ArrayList();
452:
453:                public void nodeAdded(final DomChangeEvent event) {
454:                    collectedValues_.add("nodeAdded: "
455:                            + event.getParentNode().getNodeName() + ','
456:                            + event.getChangedNode().getNodeName());
457:                }
458:
459:                public void nodeDeleted(final DomChangeEvent event) {
460:                    collectedValues_.add("nodeDeleted: "
461:                            + event.getParentNode().getNodeName() + ','
462:                            + event.getChangedNode().getNodeName());
463:                }
464:
465:                List getCollectedValues() {
466:                    return collectedValues_;
467:                }
468:            }
469:
470:            /**
471:             * @throws Exception if the test fails
472:             */
473:            public void testDomChangeListenerTestImpl_insertBefore()
474:                    throws Exception {
475:                final String htmlContent = "<html><head><title>foo</title>\n"
476:                        + "<script>\n"
477:                        + "  function clickMe() {\n"
478:                        + "    var p1 = document.getElementById('p1');\n"
479:                        + "    var div = document.createElement('DIV');\n"
480:                        + "    p1.insertBefore(div);\n"
481:                        + "  }\n"
482:                        + "</script>\n"
483:                        + "</head>\n"
484:                        + "<body>\n"
485:                        + "<p id='p1' title='myTitle'></p>\n"
486:                        + "<input id='myButton' type='button' onclick='clickMe()'>\n"
487:                        + "</body></html>";
488:
489:                final String[] expectedValues = { "nodeAdded: p,div",
490:                        "nodeAdded: p,div" };
491:                final HtmlPage page = loadPage(htmlContent);
492:                final HtmlElement p1 = page.getHtmlElementById("p1");
493:                final DomChangeListenerTestImpl listenerImpl = new DomChangeListenerTestImpl();
494:                p1.addDomChangeListener(listenerImpl);
495:                page.addDomChangeListener(listenerImpl);
496:                final HtmlButtonInput myButton = (HtmlButtonInput) page
497:                        .getHtmlElementById("myButton");
498:
499:                myButton.click();
500:                assertEquals(expectedValues, listenerImpl.getCollectedValues());
501:            }
502:
503:            /**
504:             * @throws Exception if the test fails
505:             */
506:            public void testDomChangeListenerTestImpl_appendChild()
507:                    throws Exception {
508:                final String htmlContent = "<html><head><title>foo</title>\n"
509:                        + "<script>\n"
510:                        + "  function clickMe() {\n"
511:                        + "    var p1 = document.getElementById('p1');\n"
512:                        + "    var div = document.createElement('DIV');\n"
513:                        + "    p1.appendChild(div);\n"
514:                        + "  }\n"
515:                        + "</script>\n"
516:                        + "</head>\n"
517:                        + "<body>\n"
518:                        + "<p id='p1' title='myTitle'></p>\n"
519:                        + "<input id='myButton' type='button' onclick='clickMe()'>\n"
520:                        + "</body></html>";
521:
522:                final String[] expectedValues = { "nodeAdded: p,div",
523:                        "nodeAdded: p,div" };
524:                final HtmlPage page = loadPage(htmlContent);
525:                final HtmlElement p1 = page.getHtmlElementById("p1");
526:                final DomChangeListenerTestImpl listenerImpl = new DomChangeListenerTestImpl();
527:                p1.addDomChangeListener(listenerImpl);
528:                page.addDomChangeListener(listenerImpl);
529:                final HtmlButtonInput myButton = (HtmlButtonInput) page
530:                        .getHtmlElementById("myButton");
531:
532:                myButton.click();
533:                assertEquals(expectedValues, listenerImpl.getCollectedValues());
534:            }
535:
536:            /**
537:             * @throws Exception if the test fails
538:             */
539:            public void testDomChangeListenerTestImpl_removeChild()
540:                    throws Exception {
541:                final String htmlContent = "<html><head><title>foo</title>\n"
542:                        + "<script>\n"
543:                        + "  function clickMe() {\n"
544:                        + "    var p1 = document.getElementById('p1');\n"
545:                        + "    var div = document.getElementById('myDiv');\n"
546:                        + "    div.removeChild(p1);\n"
547:                        + "  }\n"
548:                        + "</script>\n"
549:                        + "</head>\n"
550:                        + "<body>\n"
551:                        + "<div id='myDiv'><p id='p1' title='myTitle'></p></div>\n"
552:                        + "<input id='myButton' type='button' onclick='clickMe()'>\n"
553:                        + "</body></html>";
554:
555:                final String[] expectedValues = { "nodeDeleted: div,p",
556:                        "nodeDeleted: div,p" };
557:                final HtmlPage page = loadPage(htmlContent);
558:                final HtmlElement p1 = page.getHtmlElementById("p1");
559:                final DomChangeListenerTestImpl listenerImpl = new DomChangeListenerTestImpl();
560:                p1.addDomChangeListener(listenerImpl);
561:                page.addDomChangeListener(listenerImpl);
562:                final HtmlButtonInput myButton = (HtmlButtonInput) page
563:                        .getHtmlElementById("myButton");
564:
565:                myButton.click();
566:                assertEquals(expectedValues, listenerImpl.getCollectedValues());
567:            }
568:
569:            /**
570:             * @throws Exception if the test fails
571:             */
572:            public void testDomChangeListenerRegisterNewListener()
573:                    throws Exception {
574:                final String htmlContent = "<html><head><title>foo</title>\n"
575:                        + "<script>\n"
576:                        + "  function clickMe() {\n"
577:                        + "    var p1 = document.getElementById('p1');\n"
578:                        + "    var div = document.createElement('DIV');\n"
579:                        + "    p1.appendChild(div);\n"
580:                        + "  }\n"
581:                        + "</script>\n"
582:                        + "</head>\n"
583:                        + "<body>\n"
584:                        + "<p id='p1' title='myTitle'></p>\n"
585:                        + "<input id='myButton' type='button' onclick='clickMe()'>\n"
586:                        + "</body></html>";
587:
588:                final HtmlPage page = loadPage(htmlContent);
589:
590:                final List l = new ArrayList();
591:                final DomChangeListener listener2 = new DomChangeListenerTestImpl() {
592:                    public void nodeAdded(final DomChangeEvent event) {
593:                        l.add("in listener 2");
594:                    }
595:                };
596:                final DomChangeListener listener1 = new DomChangeListenerTestImpl() {
597:                    public void nodeAdded(final DomChangeEvent event) {
598:                        l.add("in listener 1");
599:                        page.addDomChangeListener(listener2);
600:                    }
601:                };
602:
603:                page.addDomChangeListener(listener1);
604:
605:                final HtmlButtonInput myButton = (HtmlButtonInput) page
606:                        .getHtmlElementById("myButton");
607:                myButton.click();
608:
609:                final String[] expectedValues = { "in listener 1" };
610:                assertEquals(expectedValues, l);
611:                l.clear();
612:
613:                myButton.click();
614:                final String[] expectedValues2 = { "in listener 1",
615:                        "in listener 2" };
616:                assertEquals(expectedValues2, l);
617:            }
618:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.