0001: /*
0002: * Copyright (c) 2002-2008 Gargoyle Software Inc. All rights reserved.
0003: *
0004: * Redistribution and use in source and binary forms, with or without
0005: * modification, are permitted provided that the following conditions are met:
0006: *
0007: * 1. Redistributions of source code must retain the above copyright notice,
0008: * this list of conditions and the following disclaimer.
0009: * 2. Redistributions in binary form must reproduce the above copyright notice,
0010: * this list of conditions and the following disclaimer in the documentation
0011: * and/or other materials provided with the distribution.
0012: * 3. The end-user documentation included with the redistribution, if any, must
0013: * include the following acknowledgment:
0014: *
0015: * "This product includes software developed by Gargoyle Software Inc.
0016: * (http://www.GargoyleSoftware.com/)."
0017: *
0018: * Alternately, this acknowledgment may appear in the software itself, if
0019: * and wherever such third-party acknowledgments normally appear.
0020: * 4. The name "Gargoyle Software" must not be used to endorse or promote
0021: * products derived from this software without prior written permission.
0022: * For written permission, please contact info@GargoyleSoftware.com.
0023: * 5. Products derived from this software may not be called "HtmlUnit", nor may
0024: * "HtmlUnit" appear in their name, without prior written permission of
0025: * Gargoyle Software Inc.
0026: *
0027: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
0028: * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
0029: * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GARGOYLE
0030: * SOFTWARE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
0031: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
0032: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
0033: * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
0034: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
0035: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
0036: * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
0037: */
0038: package com.gargoylesoftware.htmlunit.html;
0039:
0040: import java.util.ArrayList;
0041: import java.util.Iterator;
0042: import java.util.List;
0043: import java.util.Map;
0044: import java.util.NoSuchElementException;
0045:
0046: import com.gargoylesoftware.htmlunit.BrowserVersion;
0047: import com.gargoylesoftware.htmlunit.WebTestCase;
0048:
0049: /**
0050: * Unit tests for {@link HtmlElement}.
0051: *
0052: * @version $Revision: 2132 $
0053: * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
0054: * @author Denis N. Antonioli
0055: * @author Daniel Gredler
0056: * @author Ahmed Ashour
0057: */
0058: public class HtmlElementTest extends WebTestCase {
0059:
0060: /**
0061: * Creates an instance.
0062: * @param name The name of the test.
0063: */
0064: public HtmlElementTest(final String name) {
0065: super (name);
0066: }
0067:
0068: /**
0069: * Test hasAttribute() on an element with the attribute.
0070: * @throws Exception if the test fails
0071: */
0072: public void testElementHasAttributeWith() throws Exception {
0073: final String content = "<html><head></head><body id='tag'>text</body></html>";
0074: final List collectedAlerts = new ArrayList();
0075: final HtmlPage page = loadPage(content, collectedAlerts);
0076:
0077: final HtmlElement node = page.getDocumentHtmlElement()
0078: .getHtmlElementById("tag");
0079: assertEquals("Element should have attribute", true, node
0080: .hasAttribute("id"));
0081: }
0082:
0083: /**
0084: * Test hasAttribute() on an element without the attributes.
0085: * @throws Exception if the test fails
0086: */
0087: public void testElementHasAttributeNone() throws Exception {
0088: final String content = "<html><head></head><body id='tag'>text</body></html>";
0089: final List collectedAlerts = new ArrayList();
0090: final HtmlPage page = loadPage(content, collectedAlerts);
0091:
0092: final HtmlElement node = page.getDocumentHtmlElement()
0093: .getHtmlElementById("tag");
0094: assertEquals("Element should not have attribute", false, node
0095: .hasAttribute("foo"));
0096: }
0097:
0098: /**
0099: * Test hasAttribute() on an element with the attribute.
0100: * @throws Exception if the test fails
0101: */
0102: public void testElementHasAttributeNSWith() throws Exception {
0103: final String content = "<html><head></head><body xmlns:ns='http://foobar' id='tag' ns:foo='bar'>text</body></html>";
0104: final List collectedAlerts = new ArrayList();
0105: final HtmlPage page = loadPage(content, collectedAlerts);
0106:
0107: final HtmlElement node = page.getDocumentHtmlElement()
0108: .getHtmlElementById("tag");
0109: assertEquals("Element should have attribute", true, node
0110: .hasAttributeNS("http://foobar", "foo"));
0111: }
0112:
0113: /**
0114: * Test hasAttribute() on an element without the attributes.
0115: * @throws Exception if the test fails
0116: */
0117: public void testElementHasAttributeNSNone() throws Exception {
0118: final String content = "<html><head></head><body id='tag'>text</body></html>";
0119: final List collectedAlerts = new ArrayList();
0120: final HtmlPage page = loadPage(content, collectedAlerts);
0121:
0122: final HtmlElement node = page.getDocumentHtmlElement()
0123: .getHtmlElementById("tag");
0124: assertEquals("Element should not have attribute", false, node
0125: .hasAttributeNS("http://foobar", "foo"));
0126: }
0127:
0128: /**
0129: * Test getAttribute() on an element with the attribute.
0130: * @throws Exception if the test fails
0131: */
0132: public void testElementGetAttributeWith() throws Exception {
0133: final String content = "<html><head></head><body id='tag'>text</body></html>";
0134: final List collectedAlerts = new ArrayList();
0135: final HtmlPage page = loadPage(content, collectedAlerts);
0136:
0137: final HtmlElement node = page.getDocumentHtmlElement()
0138: .getHtmlElementById("tag");
0139: assertEquals("Element should have attribute", "tag", node
0140: .getAttribute("id"));
0141: }
0142:
0143: /**
0144: * Test getAttribute() on an element without the attributes.
0145: * @throws Exception if the test fails
0146: */
0147: public void testElementGetAttributeNone() throws Exception {
0148: final String content = "<html><head></head><body id='tag'>text</body></html>";
0149: final List collectedAlerts = new ArrayList();
0150: final HtmlPage page = loadPage(content, collectedAlerts);
0151:
0152: final HtmlElement node = page.getDocumentHtmlElement()
0153: .getHtmlElementById("tag");
0154: assertEquals("Element should not have attribute", "", node
0155: .getAttribute("foo"));
0156: }
0157:
0158: /**
0159: * Test getAttribute() on an element with the attribute.
0160: * @throws Exception if the test fails
0161: */
0162: public void testElementGetAttributeNSWith() throws Exception {
0163: final String content = "<html><head></head><body xmlns:ns='http://foobar' id='tag' ns:foo='bar'>text</body></html>";
0164: final List collectedAlerts = new ArrayList();
0165: final HtmlPage page = loadPage(content, collectedAlerts);
0166:
0167: final HtmlElement node = page.getDocumentHtmlElement()
0168: .getHtmlElementById("tag");
0169: assertEquals("Element should have attribute", "bar", node
0170: .getAttributeNS("http://foobar", "foo"));
0171: }
0172:
0173: /**
0174: * Test getAttribute() on an element without the attributes.
0175: * @throws Exception if the test fails
0176: */
0177: public void testElementGetAttributeNSNone() throws Exception {
0178: final String content = "<html><head></head><body id='tag'>text</body></html>";
0179: final List collectedAlerts = new ArrayList();
0180: final HtmlPage page = loadPage(content, collectedAlerts);
0181:
0182: final HtmlElement node = page.getDocumentHtmlElement()
0183: .getHtmlElementById("tag");
0184: assertEquals("Element should not have attribute", "", node
0185: .getAttributeNS("http://foobar", "foo"));
0186: }
0187:
0188: /**
0189: * Test getNamespaceURI on an attribute that has a namespace.
0190: * @throws Exception if the test fails
0191: */
0192: public void testElementGetNamespaceURIWith() throws Exception {
0193: final String content = "<html><head></head><body xmlns:ns='http://foobar' id='tag' ns:foo='bar'>text</body></html>";
0194: final List collectedAlerts = new ArrayList();
0195: final HtmlPage page = loadPage(content, collectedAlerts);
0196:
0197: final HtmlElement node = page.getDocumentHtmlElement()
0198: .getHtmlElementById("tag");
0199: final Iterator attrIter = node.getAttributeEntriesIterator();
0200: while (attrIter.hasNext()) {
0201: final HtmlAttr attr = (HtmlAttr) attrIter.next();
0202: if (attr.getName().equals("ns:foo")) {
0203: assertEquals("Element should have a namespace URI",
0204: "http://foobar", attr.getNamespaceURI());
0205: return;
0206: }
0207: }
0208: assertFalse("Attribute ns:foo not found.", true);
0209: }
0210:
0211: /**
0212: * Test getNamespaceURI on an attribute that has a namespace.
0213: * @throws Exception if the test fails
0214: */
0215: public void testElementGetNamespaceURINone() throws Exception {
0216: final String content = "<html><head></head><body xmlns:ns='http://foobar' id='tag' ns:foo='bar'>text</body></html>";
0217: final List collectedAlerts = new ArrayList();
0218: final HtmlPage page = loadPage(content, collectedAlerts);
0219:
0220: final HtmlElement node = page.getDocumentHtmlElement()
0221: .getHtmlElementById("tag");
0222: final Iterator attrIter = node.getAttributeEntriesIterator();
0223: while (attrIter.hasNext()) {
0224: final HtmlAttr attr = (HtmlAttr) attrIter.next();
0225: if (attr.getName().equals("id")) {
0226: assertEquals("Element should not have a namespace URI",
0227: null, attr.getNamespaceURI());
0228: return;
0229: }
0230: }
0231: assertFalse("Attribute ns:foo not found.", true);
0232: }
0233:
0234: /**
0235: * Test getLocalName on an attribute that has a local name.
0236: * @throws Exception if the test fails
0237: */
0238: public void testElementGetLocalNameWith() throws Exception {
0239: final String content = "<html><head></head><body xmlns:ns='http://foobar' id='tag' ns:foo='bar'>text</body></html>";
0240: final List collectedAlerts = new ArrayList();
0241: final HtmlPage page = loadPage(content, collectedAlerts);
0242:
0243: final HtmlElement node = page.getDocumentHtmlElement()
0244: .getHtmlElementById("tag");
0245: final Iterator attrIter = node.getAttributeEntriesIterator();
0246: while (attrIter.hasNext()) {
0247: final HtmlAttr attr = (HtmlAttr) attrIter.next();
0248: if (attr.getName().equals("ns:foo")) {
0249: assertEquals("Element should have a local name", "foo",
0250: attr.getLocalName());
0251: return;
0252: }
0253: }
0254: assertFalse("Attribute ns:foo not found.", true);
0255: }
0256:
0257: /**
0258: * Test getLocalName on an attribute that has a local name.
0259: * @throws Exception if the test fails
0260: */
0261: public void testElementGetLocalNameNone() throws Exception {
0262: final String content = "<html><head></head><body xmlns:ns='http://foobar' id='tag' ns:foo='bar'>text</body></html>";
0263: final List collectedAlerts = new ArrayList();
0264: final HtmlPage page = loadPage(content, collectedAlerts);
0265:
0266: final HtmlElement node = page.getDocumentHtmlElement()
0267: .getHtmlElementById("tag");
0268: final Iterator attrIter = node.getAttributeEntriesIterator();
0269: while (attrIter.hasNext()) {
0270: final HtmlAttr attr = (HtmlAttr) attrIter.next();
0271: if (attr.getName().equals("id")) {
0272: // This is not standard, but to change it now would break backwards compatibility.
0273: assertEquals("Element should not have a local name",
0274: "id", attr.getLocalName());
0275: return;
0276: }
0277: }
0278: assertFalse("Attribute ns:foo not found.", true);
0279: }
0280:
0281: /**
0282: * Test getPrefix on an attribute that has a prefix.
0283: * @throws Exception if the test fails
0284: */
0285: public void testElementGetPrefixWith() throws Exception {
0286: final String content = "<html><head></head><body xmlns:ns='http://foobar' id='tag' ns:foo='bar'>text</body></html>";
0287: final List collectedAlerts = new ArrayList();
0288: final HtmlPage page = loadPage(content, collectedAlerts);
0289:
0290: final HtmlElement node = page.getDocumentHtmlElement()
0291: .getHtmlElementById("tag");
0292: final Iterator attrIter = node.getAttributeEntriesIterator();
0293: while (attrIter.hasNext()) {
0294: final HtmlAttr attr = (HtmlAttr) attrIter.next();
0295: if (attr.getName().equals("ns:foo")) {
0296: assertEquals("Element should have a prefix", "ns", attr
0297: .getPrefix());
0298: return;
0299: }
0300: }
0301: assertFalse("Attribute ns:foo not found.", true);
0302: }
0303:
0304: /**
0305: * Test getPrefix on an attribute that has a prefix.
0306: * @throws Exception if the test fails
0307: */
0308: public void testElementGetPrefixNone() throws Exception {
0309: final String content = "<html><head></head><body xmlns:ns='http://foobar' id='tag' ns:foo='bar'>text</body></html>";
0310: final List collectedAlerts = new ArrayList();
0311: final HtmlPage page = loadPage(content, collectedAlerts);
0312:
0313: final HtmlElement node = page.getDocumentHtmlElement()
0314: .getHtmlElementById("tag");
0315: final Iterator attrIter = node.getAttributeEntriesIterator();
0316: while (attrIter.hasNext()) {
0317: final HtmlAttr attr = (HtmlAttr) attrIter.next();
0318: if (attr.getName().equals("id")) {
0319: assertEquals("Element should not have a prefix", null,
0320: attr.getPrefix());
0321: return;
0322: }
0323: }
0324: assertFalse("Attribute ns:foo not found.", true);
0325: }
0326:
0327: /**
0328: * Test setPrefix on an attribute that has a prefix.
0329: * @throws Exception if the test fails
0330: */
0331: public void testElementSetPrefix() throws Exception {
0332: final String content = "<html><head></head><body xmlns:ns='http://foobar' id='tag' ns:foo='bar'>text</body></html>";
0333: final List collectedAlerts = new ArrayList();
0334: final HtmlPage page = loadPage(content, collectedAlerts);
0335:
0336: final HtmlElement node = page.getDocumentHtmlElement()
0337: .getHtmlElementById("tag");
0338: final Iterator attrIter = node.getAttributeEntriesIterator();
0339: while (attrIter.hasNext()) {
0340: final HtmlAttr attr = (HtmlAttr) attrIter.next();
0341: if (attr.getName().equals("ns:foo")) {
0342: attr.setPrefix("other");
0343: assertEquals("Element should have a changed prefix",
0344: "other", attr.getPrefix());
0345: assertEquals("setPrefix should change qualified name",
0346: "other:foo", attr.getName());
0347: return;
0348: }
0349: }
0350: assertFalse("Attribute ns:foo not found.", true);
0351: }
0352:
0353: /**
0354: * Test setAttribute() on an element with the attribute.
0355: * @throws Exception if the test fails
0356: */
0357: public void testElementSetAttributeWith() throws Exception {
0358: final String content = "<html><head></head><body id='tag'>text</body></html>";
0359: final List collectedAlerts = new ArrayList();
0360: final HtmlPage page = loadPage(content, collectedAlerts);
0361:
0362: final HtmlElement node = page.getDocumentHtmlElement()
0363: .getHtmlElementById("tag");
0364: node.setAttribute("id", "other");
0365: assertEquals("Element should have attribute", "other", node
0366: .getAttribute("id"));
0367: }
0368:
0369: /**
0370: * Test setAttribute() on an element without the attributes.
0371: * @throws Exception if the test fails
0372: */
0373: public void testElementSetAttributeNone() throws Exception {
0374: final String content = "<html><head></head><body id='tag'>text</body></html>";
0375: final List collectedAlerts = new ArrayList();
0376: final HtmlPage page = loadPage(content, collectedAlerts);
0377:
0378: final HtmlElement node = page.getDocumentHtmlElement()
0379: .getHtmlElementById("tag");
0380: node.setAttribute("foo", "other");
0381: assertEquals("Element should have attribute", "other", node
0382: .getAttribute("foo"));
0383: }
0384:
0385: /**
0386: * Test setAttribute() on an element with the attribute.
0387: * @throws Exception if the test fails
0388: */
0389: public void testElementSetAttributeNSWith() throws Exception {
0390: final String content = "<html><head></head><body xmlns:ns='http://foobar' id='tag' ns:foo='bar'>text</body></html>";
0391: final List collectedAlerts = new ArrayList();
0392: final HtmlPage page = loadPage(content, collectedAlerts);
0393:
0394: final HtmlElement node = page.getDocumentHtmlElement()
0395: .getHtmlElementById("tag");
0396: node.setAttributeNS("http://foobar", "ns:foo", "other");
0397: assertEquals("Element should have attribute", "other", node
0398: .getAttributeNS("http://foobar", "foo"));
0399: }
0400:
0401: /**
0402: * Test setAttribute() on an element without the attributes.
0403: * @throws Exception if the test fails
0404: */
0405: public void testElementSetAttributeNSNone() throws Exception {
0406: final String content = "<html><head></head><body id='tag'>text</body></html>";
0407: final List collectedAlerts = new ArrayList();
0408: final HtmlPage page = loadPage(content, collectedAlerts);
0409:
0410: final HtmlElement node = page.getDocumentHtmlElement()
0411: .getHtmlElementById("tag");
0412: node.setAttributeNS("http://foobar", "ns:foo", "other");
0413: assertEquals("Element should not have attribute", "other", node
0414: .getAttributeNS("http://foobar", "foo"));
0415: }
0416:
0417: /**
0418: * Test removeAttribute() on an element with the attribute.
0419: * @throws Exception if the test fails
0420: */
0421: public void testElementRemoveAttributeWith() throws Exception {
0422: final String content = "<html><head></head><body id='tag'>text</body></html>";
0423: final List collectedAlerts = new ArrayList();
0424: final HtmlPage page = loadPage(content, collectedAlerts);
0425:
0426: final HtmlElement node = page.getDocumentHtmlElement()
0427: .getHtmlElementById("tag");
0428: node.removeAttribute("id");
0429: assertEquals("Element should not have removed attribute", "",
0430: node.getAttribute("id"));
0431: }
0432:
0433: /**
0434: * Test removeAttribute() on an element without the attributes.
0435: * @throws Exception if the test fails
0436: */
0437: public void testElementRemoveAttributeNone() throws Exception {
0438: final String content = "<html><head></head><body id='tag'>text</body></html>";
0439: final List collectedAlerts = new ArrayList();
0440: final HtmlPage page = loadPage(content, collectedAlerts);
0441:
0442: final HtmlElement node = page.getDocumentHtmlElement()
0443: .getHtmlElementById("tag");
0444: node.removeAttribute("foo");
0445: assertEquals("Element should not have attribute", "", node
0446: .getAttribute("foo"));
0447: }
0448:
0449: /**
0450: * Test removeAttribute() on an element with the attribute.
0451: * @throws Exception if the test fails
0452: */
0453: public void testElementRemoveAttributeNSWith() throws Exception {
0454: final String content = "<html><head></head><body xmlns:ns='http://foobar' id='tag' ns:foo='bar'>text</body></html>";
0455: final List collectedAlerts = new ArrayList();
0456: final HtmlPage page = loadPage(content, collectedAlerts);
0457:
0458: final HtmlElement node = page.getDocumentHtmlElement()
0459: .getHtmlElementById("tag");
0460: node.removeAttributeNS("http://foobar", "foo");
0461: assertEquals("Element should not have removed attribute", "",
0462: node.getAttributeNS("http://foobar", "foo"));
0463: }
0464:
0465: /**
0466: * Test removeAttribute() on an element without the attributes.
0467: * @throws Exception if the test fails
0468: */
0469: public void testElementRemoveAttributeNSNone() throws Exception {
0470: final String content = "<html><head></head><body id='tag'>text</body></html>";
0471: final List collectedAlerts = new ArrayList();
0472: final HtmlPage page = loadPage(content, collectedAlerts);
0473:
0474: final HtmlElement node = page.getDocumentHtmlElement()
0475: .getHtmlElementById("tag");
0476: node.removeAttributeNS("http://foobar", "foo");
0477: assertEquals("Element should not have attribute", "", node
0478: .getAttributeNS("http://foobar", "foo"));
0479: }
0480:
0481: /**
0482: * Verifies that cloned node attributes have the same initial values, but changes can be made
0483: * to the clone without affecting the original node, and that the id attribute is treated the
0484: * same as all the other attributes. See bug 1707726.
0485: * @throws Exception If an error occurs.
0486: */
0487: public void testClonedNodeAttributes() throws Exception {
0488: final String html = "<html><body id='a' title='b'><script>\n"
0489: + "var x = document.body.cloneNode(true);\n"
0490: + "alert(document.body==x);\n"
0491: + "alert(document.getElementById('a')==document.body);\n"
0492: + "alert(document.body.id);\n" + "alert(x.id);\n"
0493: + "alert(document.body.title);\n" + "alert(x.title);\n"
0494: + "x.title='c';\n" + "alert(document.body.title);\n"
0495: + "alert(x.title);\n" + "</script></body></html>";
0496: final List collectedAlerts = new ArrayList();
0497: loadPage(html, collectedAlerts);
0498: final String[] expectedAlerts = { "false", "true", "a", "a",
0499: "b", "b", "b", "c" };
0500: assertEquals(expectedAlerts, collectedAlerts);
0501: }
0502:
0503: /**
0504: * @throws Exception if the test fails
0505: */
0506: public void testGetEnclosingForm() throws Exception {
0507: final String htmlContent = ""
0508: + "<html><head><title>foo</title></head><body>\n"
0509: + "<form id='form1'>\n"
0510: + "<table><tr><td><input type='text' id='foo'/></td></tr></table>\n"
0511: + "</form></body></html>";
0512: final HtmlPage page = loadPage(htmlContent);
0513: final HtmlForm form = (HtmlForm) page
0514: .getHtmlElementById("form1");
0515:
0516: final HtmlInput input = (HtmlInput) form
0517: .getHtmlElementById("foo");
0518: assertSame(form, input.getEnclosingForm());
0519: }
0520:
0521: /**
0522: * @throws Exception if the test fails
0523: */
0524: public void testGetEnclosing() throws Exception {
0525: final String htmlContent = ""
0526: + "<html><head><title>foo</title></head><body>\n"
0527: + "<form id='form1'>\n" + "<table id='table1'>\n"
0528: + "<tr id='tr1'><td id='td1'>foo</td></tr>\n"
0529: + "<tr id='tr2'><td id='td2'>foo</td></tr>\n"
0530: + "</table>\n" + "</form></body></html>";
0531: final HtmlPage page = loadPage(htmlContent);
0532:
0533: final HtmlElement td1 = page.getHtmlElementById("td1");
0534: assertEquals("tr1", td1.getEnclosingElement("tr").getId());
0535: assertEquals("tr1", td1.getEnclosingElement("TR").getId());
0536: assertEquals("table1", td1.getEnclosingElement("table").getId());
0537: assertEquals("form1", td1.getEnclosingElement("form").getId());
0538:
0539: final HtmlElement td2 = page.getHtmlElementById("td2");
0540: assertEquals("tr2", td2.getEnclosingElement("tr").getId());
0541: assertEquals("tr2", td2.getEnclosingElement("TR").getId());
0542: assertEquals("table1", td2.getEnclosingElement("table").getId());
0543: assertEquals("form1", td2.getEnclosingElement("form").getId());
0544: }
0545:
0546: /**
0547: * @throws Exception if the test fails
0548: */
0549: public void testAsText_WithComments() throws Exception {
0550: final String htmlContent = "<html><head><title>foo</title></head><body>\n"
0551: + "<p id='p1'>foo<!--bar--></p>\n" + "</body></html>";
0552: final HtmlPage page = loadPage(htmlContent);
0553: final HtmlElement element = page.getHtmlElementById("p1");
0554: assertEquals("foo", element.asText());
0555: }
0556:
0557: /**
0558: */
0559: public void testConstants() {
0560: assertEquals("", HtmlElement.ATTRIBUTE_NOT_DEFINED);
0561: assertEquals("", HtmlElement.ATTRIBUTE_VALUE_EMPTY);
0562: assertTrue(
0563: "Not the same object",
0564: HtmlElement.ATTRIBUTE_NOT_DEFINED != HtmlElement.ATTRIBUTE_VALUE_EMPTY);
0565: }
0566:
0567: /**
0568: * @deprecated
0569: */
0570: public void testEmtpyMapEntryWrappingIterator() {
0571: final Map attributsMap = HtmlElement.createAttributeMap(0);
0572: final HtmlElement.MapEntryWrappingIterator it = new HtmlElement.MapEntryWrappingIterator(
0573: attributsMap.entrySet().iterator(),
0574: HtmlAttrTest.HTML_ELEMENT);
0575: assertFalse(it.hasNext());
0576: try {
0577: it.next();
0578: fail("Travel empty iterator");
0579: } catch (final NoSuchElementException nsee) {
0580: // nothing, it's normal
0581: }
0582: }
0583:
0584: /**
0585: * @deprecated
0586: */
0587: public void testMapEntryWrappingIterator() {
0588: final Map attributsMap = HtmlElement.createAttributeMap(1);
0589: HtmlElement.addAttributeToMap(null, attributsMap, null, "key",
0590: "value");
0591: final HtmlElement.MapEntryWrappingIterator it = new HtmlElement.MapEntryWrappingIterator(
0592: attributsMap.entrySet().iterator(),
0593: HtmlAttrTest.HTML_ELEMENT);
0594:
0595: assertTrue(it.hasNext());
0596: final HtmlAttr me = (HtmlAttr) it.next();
0597: assertEquals("key", me.getName());
0598: assertEquals("value", me.getHtmlValue());
0599: assertInstanceOf(me, HtmlAttr.class);
0600:
0601: assertFalse(it.hasNext());
0602: try {
0603: it.next();
0604: fail("Travel empty iterator");
0605: } catch (final NoSuchElementException nsee) {
0606: // nothing, it's normal
0607: }
0608: }
0609:
0610: /**
0611: * @deprecated
0612: */
0613: public void testMapEntryWrappingIteratorDelegatesRemove() {
0614: final boolean removed[] = { false };
0615:
0616: final Iterator mockIterator = new Iterator() {
0617: public boolean hasNext() {
0618: return false;
0619: }
0620:
0621: public Object next() {
0622: return null;
0623: }
0624:
0625: public void remove() {
0626: removed[0] = true;
0627: }
0628: };
0629:
0630: final HtmlElement.MapEntryWrappingIterator it = new HtmlElement.MapEntryWrappingIterator(
0631: mockIterator, HtmlAttrTest.HTML_ELEMENT);
0632:
0633: assertFalse(removed[0]);
0634: it.remove();
0635: assertTrue(removed[0]);
0636:
0637: // just for clover coverage
0638: assertFalse(mockIterator.hasNext());
0639: assertNull(mockIterator.next());
0640: }
0641:
0642: static class HtmlAttributeChangeListenerTestImpl implements
0643: HtmlAttributeChangeListener {
0644: private final List collectedValues_ = new ArrayList();
0645:
0646: public void attributeAdded(final HtmlAttributeChangeEvent event) {
0647: collectedValues_.add("attributeAdded: "
0648: + event.getHtmlElement().getTagName() + ','
0649: + event.getName() + ',' + event.getValue());
0650: }
0651:
0652: public void attributeRemoved(
0653: final HtmlAttributeChangeEvent event) {
0654: collectedValues_.add("attributeRemoved: "
0655: + event.getHtmlElement().getTagName() + ','
0656: + event.getName() + ',' + event.getValue());
0657: }
0658:
0659: public void attributeReplaced(
0660: final HtmlAttributeChangeEvent event) {
0661: collectedValues_.add("attributeReplaced: "
0662: + event.getHtmlElement().getTagName() + ','
0663: + event.getName() + ',' + event.getValue());
0664: }
0665:
0666: List getCollectedValues() {
0667: return collectedValues_;
0668: }
0669: }
0670:
0671: /**
0672: * @throws Exception if the test fails
0673: */
0674: public void testHtmlAttributeChangeListener_AddAttribute()
0675: throws Exception {
0676: final String htmlContent = "<html><head><title>foo</title>\n"
0677: + "<script>\n"
0678: + " function clickMe() {\n"
0679: + " var p1 = document.getElementById('p1');\n"
0680: + " p1.setAttribute('title', 'myTitle');\n"
0681: + " }\n"
0682: + "</script>\n"
0683: + "</head>\n"
0684: + "<body id='myBody'>\n"
0685: + "<p id='p1'></p>\n"
0686: + "<input id='myButton' type='button' onclick='clickMe()'>\n"
0687: + "</body></html>";
0688:
0689: final String[] expectedValues = {
0690: "attributeAdded: p,title,myTitle",
0691: "attributeAdded: p,title,myTitle",
0692: "attributeAdded: p,title,myTitle" };
0693: final HtmlPage page = loadPage(htmlContent);
0694: final HtmlBody body = (HtmlBody) page
0695: .getHtmlElementById("myBody");
0696: final HtmlElement p1 = page.getHtmlElementById("p1");
0697:
0698: final HtmlAttributeChangeListenerTestImpl listenerImpl = new HtmlAttributeChangeListenerTestImpl();
0699: p1.addHtmlAttributeChangeListener(listenerImpl);
0700: body.addHtmlAttributeChangeListener(listenerImpl);
0701: page.addHtmlAttributeChangeListener(listenerImpl);
0702: final HtmlButtonInput myButton = (HtmlButtonInput) page
0703: .getHtmlElementById("myButton");
0704:
0705: myButton.click();
0706: assertEquals(expectedValues, listenerImpl.getCollectedValues());
0707: }
0708:
0709: /**
0710: * @throws Exception if the test fails
0711: */
0712: public void testHtmlAttributeChangeListener_ReplaceAttribute()
0713: throws Exception {
0714: final String htmlContent = "<html><head><title>foo</title>\n"
0715: + "<script>\n"
0716: + " function clickMe() {\n"
0717: + " var p1 = document.getElementById('p1');\n"
0718: + " p1.setAttribute('title', p1.getAttribute('title') + 'a');\n"
0719: + " }\n"
0720: + "</script>\n"
0721: + "</head>\n"
0722: + "<body id='myBody'>\n"
0723: + "<p id='p1' title='myTitle'></p>\n"
0724: + "<input id='myButton' type='button' onclick='clickMe()'>\n"
0725: + "</body></html>";
0726:
0727: final String[] expectedValues = {
0728: "attributeReplaced: p,title,myTitle",
0729: "attributeReplaced: p,title,myTitle",
0730: "attributeReplaced: p,title,myTitle" };
0731: final HtmlPage page = loadPage(htmlContent);
0732: final HtmlBody body = (HtmlBody) page
0733: .getHtmlElementById("myBody");
0734: final HtmlElement p1 = page.getHtmlElementById("p1");
0735: final HtmlAttributeChangeListenerTestImpl listenerImpl = new HtmlAttributeChangeListenerTestImpl();
0736: page.addHtmlAttributeChangeListener(listenerImpl);
0737: body.addHtmlAttributeChangeListener(listenerImpl);
0738: p1.addHtmlAttributeChangeListener(listenerImpl);
0739: final HtmlButtonInput myButton = (HtmlButtonInput) page
0740: .getHtmlElementById("myButton");
0741:
0742: myButton.click();
0743: assertEquals(expectedValues, listenerImpl.getCollectedValues());
0744: assertEquals("myTitle" + 'a', p1.getAttributeValue("title"));
0745: }
0746:
0747: /**
0748: * @throws Exception if the test fails
0749: */
0750: public void testHtmlAttributeChangeListener_RemoveAttribute()
0751: throws Exception {
0752: final String htmlContent = "<html><head><title>foo</title>\n"
0753: + "<script>\n"
0754: + " function clickMe() {\n"
0755: + " var p1 = document.getElementById('p1');\n"
0756: + " p1.removeAttribute('title');\n"
0757: + " }\n"
0758: + "</script>\n"
0759: + "</head>\n"
0760: + "<body id='myBody'>\n"
0761: + "<p id='p1' title='myTitle'></p>\n"
0762: + "<input id='myButton' type='button' onclick='clickMe()'>\n"
0763: + "</body></html>";
0764:
0765: final String[] expectedValues = {
0766: "attributeRemoved: p,title,myTitle",
0767: "attributeRemoved: p,title,myTitle",
0768: "attributeRemoved: p,title,myTitle" };
0769: final HtmlPage page = loadPage(htmlContent);
0770: final HtmlBody body = (HtmlBody) page
0771: .getHtmlElementById("myBody");
0772: final HtmlElement p1 = page.getHtmlElementById("p1");
0773: final HtmlAttributeChangeListenerTestImpl listenerImpl = new HtmlAttributeChangeListenerTestImpl();
0774: page.addHtmlAttributeChangeListener(listenerImpl);
0775: body.addHtmlAttributeChangeListener(listenerImpl);
0776: p1.addHtmlAttributeChangeListener(listenerImpl);
0777: final HtmlButtonInput myButton = (HtmlButtonInput) page
0778: .getHtmlElementById("myButton");
0779:
0780: myButton.click();
0781: assertEquals(expectedValues, listenerImpl.getCollectedValues());
0782: assertSame(HtmlElement.ATTRIBUTE_NOT_DEFINED, p1
0783: .getAttributeValue("title"));
0784: }
0785:
0786: /**
0787: * @throws Exception if the test fails
0788: */
0789: public void testHtmlAttributeChangeListener_RemoveListener()
0790: throws Exception {
0791: final String htmlContent = "<html><head><title>foo</title>\n"
0792: + "<script>\n"
0793: + " function clickMe() {\n"
0794: + " var p1 = document.getElementById('p1');\n"
0795: + " p1.setAttribute('title', p1.getAttribute('title') + 'a');\n"
0796: + " }\n"
0797: + "</script>\n"
0798: + "</head>\n"
0799: + "<body>\n"
0800: + "<p id='p1' title='myTitle'></p>\n"
0801: + "<input id='myButton' type='button' onclick='clickMe()'>\n"
0802: + "</body></html>";
0803:
0804: final String[] expectedValues = { "attributeReplaced: p,title,myTitle" };
0805: final HtmlPage page = loadPage(htmlContent);
0806: final HtmlElement p1 = page.getHtmlElementById("p1");
0807: final HtmlAttributeChangeListenerTestImpl listenerImpl = new HtmlAttributeChangeListenerTestImpl();
0808: p1.addHtmlAttributeChangeListener(listenerImpl);
0809: final HtmlButtonInput myButton = (HtmlButtonInput) page
0810: .getHtmlElementById("myButton");
0811:
0812: myButton.click();
0813: p1.removeHtmlAttributeChangeListener(listenerImpl);
0814: myButton.click();
0815: assertEquals(expectedValues, listenerImpl.getCollectedValues());
0816: assertEquals("myTitle" + 'a' + 'a', p1
0817: .getAttributeValue("title"));
0818: }
0819:
0820: /**
0821: * @throws Exception If the test fails
0822: */
0823: public void testMouseOver() throws Exception {
0824: final String content = "<html>\n"
0825: + "<head>\n"
0826: + "<script>\n"
0827: + " function mouseOverMe() {\n"
0828: + " document.getElementById('myTextarea').value+='mouseover-';\n"
0829: + " }\n" + "</script>\n" + "</head>\n"
0830: + "<body id='myBody' onmouseover='mouseOverMe()'>\n"
0831: + "<textarea id='myTextarea'></textarea>\n"
0832: + "</body></html>";
0833: final HtmlPage page = loadPage(content);
0834: final HtmlBody body = (HtmlBody) page
0835: .getHtmlElementById("myBody");
0836: body.mouseOver();
0837: final HtmlTextArea textArea = (HtmlTextArea) page
0838: .getHtmlElementById("myTextarea");
0839: assertEquals("mouseover-", textArea.getText());
0840: }
0841:
0842: /**
0843: * @throws Exception If the test fails
0844: */
0845: public void testMouseMove() throws Exception {
0846: final String content = "<html>\n"
0847: + "<head>\n"
0848: + "<script>\n"
0849: + " function mouseMoveMe() {\n"
0850: + " document.getElementById('myTextarea').value+='mousemove-';\n"
0851: + " }\n" + "</script>\n" + "</head>\n"
0852: + "<body id='myBody' onmousemove='mouseMoveMe()'>\n"
0853: + "<textarea id='myTextarea'></textarea>\n"
0854: + "</body></html>";
0855: final HtmlPage page = loadPage(content);
0856: final HtmlBody body = (HtmlBody) page
0857: .getHtmlElementById("myBody");
0858: body.mouseMove();
0859: final HtmlTextArea textArea = (HtmlTextArea) page
0860: .getHtmlElementById("myTextarea");
0861: assertEquals("mousemove-", textArea.getText());
0862: }
0863:
0864: /**
0865: * @throws Exception If the test fails
0866: */
0867: public void testMouseOut() throws Exception {
0868: final String content = "<html>\n"
0869: + "<head>\n"
0870: + "<script>\n"
0871: + " function mouseOutMe() {\n"
0872: + " document.getElementById('myTextarea').value+='mouseout-';\n"
0873: + " }\n" + "</script>\n" + "</head>\n"
0874: + "<body id='myBody' onmouseout='mouseOutMe()'>\n"
0875: + "<textarea id='myTextarea'></textarea>\n"
0876: + "</body></html>";
0877: final HtmlPage page = loadPage(content);
0878: final HtmlBody body = (HtmlBody) page
0879: .getHtmlElementById("myBody");
0880: body.mouseOut();
0881: final HtmlTextArea textArea = (HtmlTextArea) page
0882: .getHtmlElementById("myTextarea");
0883: assertEquals("mouseout-", textArea.getText());
0884: }
0885:
0886: /**
0887: * @throws Exception If the test fails
0888: */
0889: public void testMouseDown() throws Exception {
0890: testMouseDown(BrowserVersion.FIREFOX_2, "mousedown-0");
0891: testMouseDown(BrowserVersion.INTERNET_EXPLORER_6_0,
0892: "mousedown-1");
0893: }
0894:
0895: private void testMouseDown(final BrowserVersion browserVersion,
0896: final String expected) throws Exception {
0897: final String content = "<html>\n"
0898: + "<head>\n"
0899: + "<script>\n"
0900: + " function mouseDownMe(e) {\n"
0901: + " document.getElementById('myTextarea').value+='mousedown-' + e.button;\n"
0902: + " }\n"
0903: + "</script>\n"
0904: + "</head>\n"
0905: + "<body id='myBody' onmousedown='mouseDownMe(event)'>\n"
0906: + "<textarea id='myTextarea'></textarea>\n"
0907: + "</body></html>";
0908: final HtmlPage page = loadPage(browserVersion, content,
0909: new ArrayList());
0910: final HtmlBody body = (HtmlBody) page
0911: .getHtmlElementById("myBody");
0912: body.mouseDown();
0913: final HtmlTextArea textArea = (HtmlTextArea) page
0914: .getHtmlElementById("myTextarea");
0915: assertEquals(expected, textArea.getText());
0916: }
0917:
0918: /**
0919: * @throws Exception If the test fails
0920: */
0921: public void testMouseUp() throws Exception {
0922: final String content = "<html>\n"
0923: + "<head>\n"
0924: + "<script>\n"
0925: + " function mouseUpMe() {\n"
0926: + " document.getElementById('myTextarea').value+='mouseup-';\n"
0927: + " }\n" + "</script>\n" + "</head>\n"
0928: + "<body id='myBody' onmouseup='mouseUpMe()'>\n"
0929: + "<textarea id='myTextarea'></textarea>\n"
0930: + "</body></html>";
0931: final HtmlPage page = loadPage(content);
0932: final HtmlBody body = (HtmlBody) page
0933: .getHtmlElementById("myBody");
0934: body.mouseUp();
0935: final HtmlTextArea textArea = (HtmlTextArea) page
0936: .getHtmlElementById("myTextarea");
0937: assertEquals("mouseup-", textArea.getText());
0938: }
0939:
0940: /**
0941: * @throws Exception If the test fails
0942: */
0943: public void testRightClick() throws Exception {
0944: testRightClick(BrowserVersion.INTERNET_EXPLORER_7_0,
0945: "mousedown-2-mouseup-2-contextmenu-0-");
0946: testRightClick(BrowserVersion.FIREFOX_2,
0947: "mousedown-3-mouseup-3-contextmenu-3-");
0948: }
0949:
0950: private void testRightClick(final BrowserVersion browserVersion,
0951: final String expected) throws Exception {
0952: final String content = "<html>\n"
0953: + "<head>\n"
0954: + "<script>\n"
0955: + " function divMouseEvent(e) {\n"
0956: + " var textarea = document.getElementById('myTextarea');\n"
0957: + " if (window.event)\n"
0958: + " textarea.value += event.type + '-' + event.button + '-';\n"
0959: + " else\n"
0960: + " textarea.value += e.type + '-' + e.which + '-';\n"
0961: + " }\n"
0962: + " function loadFunction(e) {\n"
0963: + " document.getElementById('myDiv').onmousedown = divMouseEvent;\n"
0964: + " document.getElementById('myDiv').onmouseup = divMouseEvent;\n"
0965: + " document.getElementById('myDiv').oncontextmenu = divMouseEvent;\n"
0966: + " }\n" + "</script>\n" + "</head>\n"
0967: + "<body onload='loadFunction()'>\n"
0968: + " <div id='myDiv'>Hello</div><br>\n"
0969: + " <textarea id='myTextarea'></textarea>\n"
0970: + "</body></html>";
0971: final HtmlPage page = loadPage(browserVersion, content, null);
0972: final HtmlDivision div = (HtmlDivision) page
0973: .getHtmlElementById("myDiv");
0974: div.rightClick();
0975: final HtmlTextArea textArea = (HtmlTextArea) page
0976: .getHtmlElementById("myTextarea");
0977: assertEquals(expected, textArea.getText());
0978: }
0979:
0980: /**
0981: * Test the mouse down, then mouse up.
0982: *
0983: * @throws Exception If the test fails
0984: */
0985: public void testMouse_Down_Up() throws Exception {
0986: testMouse_Down_Up(BrowserVersion.INTERNET_EXPLORER_7_0,
0987: "mousedown-1-mouseup-1-");
0988: testMouse_Down_Up(BrowserVersion.FIREFOX_2,
0989: "mousedown-1-mouseup-1-");
0990: }
0991:
0992: private void testMouse_Down_Up(final BrowserVersion browserVersion,
0993: final String expected) throws Exception {
0994: final String content = "<html>\n"
0995: + "<head>\n"
0996: + "<script>\n"
0997: + " function divMouseEvent(e) {\n"
0998: + " var textarea = document.getElementById('myTextarea');\n"
0999: + " if (window.event)\n"
1000: + " textarea.value += event.type + '-' + event.button + '-';\n"
1001: + " else\n"
1002: + " textarea.value += e.type + '-' + e.which + '-';\n"
1003: + " }\n"
1004: + " function loadFunction(e) {\n"
1005: + " document.getElementById('myDiv').onmousedown=divMouseEvent;\n"
1006: + " document.getElementById('myDiv').onmouseup =divMouseEvent;\n"
1007: + " }\n" + "</script>\n" + "</head>\n"
1008: + "<body onload='loadFunction()'>\n"
1009: + " <div id='myDiv'>Hello</div><br>\n"
1010: + " <textarea id='myTextarea'></textarea>\n"
1011: + "</body></html>";
1012: final HtmlPage page = loadPage(browserVersion, content, null);
1013: final HtmlDivision div = (HtmlDivision) page
1014: .getHtmlElementById("myDiv");
1015: div.mouseDown();
1016: div.mouseUp();
1017: final HtmlTextArea textArea = (HtmlTextArea) page
1018: .getHtmlElementById("myTextarea");
1019: assertEquals(expected, textArea.getText());
1020: }
1021:
1022: /**
1023: * @throws Exception if the test fails
1024: */
1025: public void testAsXml_separateLineforEmptyElements()
1026: throws Exception {
1027: final String content = "<html><head><title>foo</title></head>\n"
1028: + "<body><table><tr><td></tr></table>\n"
1029: + "</body></html>";
1030: final List collectedAlerts = new ArrayList();
1031: final HtmlPage page = loadPage(content, collectedAlerts);
1032: assertTrue(page.asXml().indexOf("/> ") == -1);
1033: }
1034: }
|