/** childNodes, hasChildNodes. */
  public void testChildNodeList() {
    Document doc = Document.get();
    BodyElement body = doc.getBody();

    // <div>foo<button/>bar</div>
    DivElement div = doc.createDivElement();
    Text txt0 = doc.createTextNode("foo");
    ButtonElement btn0 = doc.createButtonElement();
    Text txt1 = doc.createTextNode("bar");

    body.appendChild(div);
    div.appendChild(txt0);
    div.appendChild(btn0);
    div.appendChild(txt1);

    NodeList<Node> children = div.getChildNodes();
    assertEquals(3, children.getLength());
    assertEquals(txt0, children.getItem(0));
    assertEquals(btn0, children.getItem(1));
    assertEquals(txt1, children.getItem(2));

    assertEquals(3, div.getChildCount());
    assertEquals(txt0, div.getChild(0));
    assertEquals(btn0, div.getChild(1));
    assertEquals(txt1, div.getChild(2));

    assertFalse(txt0.hasChildNodes());
    assertTrue(div.hasChildNodes());
  }
  /** appendChild, insertBefore, removeChild, replaceChild. */
  public void testAppendRemoveReplace() {
    Document doc = Document.get();
    BodyElement body = doc.getBody();

    // <div>foo<button/>bar</div>
    DivElement div = doc.createDivElement();
    Text txt0 = doc.createTextNode("foo");
    ButtonElement btn0 = doc.createButtonElement();
    Text txt1 = doc.createTextNode("bar");

    body.appendChild(div);
    div.appendChild(txt0);
    div.appendChild(btn0);
    div.appendChild(txt1);

    // appendChild, insertBefore
    ButtonElement btn1 = doc.createButtonElement();

    // <div>foo<btn0/>bar<btn1/></div>
    div.appendChild(btn1);
    assertEquals(btn1, div.getLastChild());

    // <div>foo<button/>bar<button/></div>
    div.insertBefore(btn1, txt1);
    assertEquals(4, div.getChildNodes().getLength());
    assertEquals(btn1, div.getChildNodes().getItem(2));

    // removeChild
    // <div>foo<btn0/>bar</div> (back to original)
    div.removeChild(btn1);
    assertEquals(3, div.getChildNodes().getLength());

    // replaceChild
    // <div>foo<btn1/>bar</div>
    div.replaceChild(btn1, btn0);
    assertEquals(btn1, txt0.getNextSibling());
    assertEquals(btn1, txt1.getPreviousSibling());

    // insertAfter
    // <div>foo<btn1/><btn0/>bar</div>
    div.insertAfter(btn0, btn1);
    assertEquals(btn0, btn1.getNextSibling());

    // insertFirst
    // <div><btn1/>foo<btn0/>bar</div>
    div.insertFirst(btn1);
    assertEquals(btn1, div.getFirstChild());

    // removeFromParent
    // <div>foo<btn0/>bar</div>
    btn1.removeFromParent();
    assertNull(btn1.getParentElement());
    assertEquals(txt0, div.getFirstChild());
  }
  /** ownerDocument. */
  @DoNotRunWith(Platform.HtmlUnitBug)
  public void testOwnerDocument() {
    Document doc = Document.get();
    BodyElement body = doc.getBody();

    // <div>foo<button/>bar</div>
    DivElement div = doc.createDivElement();
    Text txt0 = doc.createTextNode("foo");
    ButtonElement btn0 = doc.createButtonElement();
    Text txt1 = doc.createTextNode("bar");

    body.appendChild(div);
    div.appendChild(txt0);
    div.appendChild(btn0);
    div.appendChild(txt1);

    // ownerDocument
    assertEquals(doc, div.getOwnerDocument());
    assertEquals(doc, txt0.getOwnerDocument());
  }
  /** hasParentElement, getParentElement. */
  public void testNodeParentElement() {
    Document doc = Document.get();
    BodyElement body = doc.getBody();
    DivElement div = doc.createDivElement();
    Text text = doc.createTextNode("foo");

    // An unattached node should have no parent element.
    assertFalse(text.hasParentElement());
    assertFalse(div.hasParentElement());
    assertNull(text.getParentElement());
    assertNull(div.getParentElement());

    // Test attached cases.
    body.appendChild(div);
    div.appendChild(text);
    assertTrue(div.hasParentElement());
    assertTrue(text.hasParentElement());
    assertEquals(body, div.getParentElement());
    assertEquals(div, text.getParentElement());
  }
  /** getParentNode, firstChild, lastChild, nextSibling, previousSibling. */
  public void testParentAndSiblings() {
    Document doc = Document.get();
    BodyElement body = doc.getBody();

    // <div>foo<button/>bar</div>
    DivElement div = doc.createDivElement();
    Text txt0 = doc.createTextNode("foo");
    ButtonElement btn0 = doc.createButtonElement();
    Text txt1 = doc.createTextNode("bar");

    body.appendChild(div);
    div.appendChild(txt0);
    div.appendChild(btn0);
    div.appendChild(txt1);

    assertEquals(div, btn0.getParentNode());

    assertEquals(txt0, div.getFirstChild());
    assertEquals(txt1, div.getLastChild());
    assertEquals(btn0, txt0.getNextSibling());
    assertEquals(btn0, txt1.getPreviousSibling());
    assertEquals(null, txt0.getPreviousSibling());
    assertEquals(null, txt1.getNextSibling());
  }
  /** isOrHasChild. */
  public void testIsOrHasChild() {
    Document doc = Document.get();
    DivElement div = doc.createDivElement();
    DivElement childDiv = doc.createDivElement();
    Text text = doc.createTextNode("foo");

    assertFalse(div.isOrHasChild(childDiv));
    assertFalse(div.isOrHasChild(text));
    assertFalse(childDiv.isOrHasChild(text));

    assertTrue(div.isOrHasChild(div));
    assertTrue(text.isOrHasChild(text));

    div.appendChild(childDiv);
    childDiv.appendChild(text);
    assertTrue(div.isOrHasChild(childDiv));
    assertTrue(div.isOrHasChild(text));

    assertFalse(childDiv.isOrHasChild(div));
    assertFalse(text.isOrHasChild(childDiv));
    assertFalse(text.isOrHasChild(div));

    BodyElement body = doc.getBody();
    body.appendChild(div);
    assertTrue(body.isOrHasChild(body));
    assertTrue(body.isOrHasChild(div));
    assertTrue(body.isOrHasChild(childDiv));
    assertTrue(body.isOrHasChild(text));

    assertTrue(div.isOrHasChild(div));
    assertTrue(div.isOrHasChild(childDiv));
    assertTrue(div.isOrHasChild(text));

    assertFalse(childDiv.isOrHasChild(div));
    assertFalse(text.isOrHasChild(div));
  }
Пример #7
0
  private void displayTests() {
    final BodyElement body = Document.get().getBody();

    for (final Class<?> c : testClasses.keySet()) {
      final DivElement div = Document.get().createDivElement();
      div.getStyle().setDisplay(Display.INLINE_BLOCK);
      div.getStyle().setVerticalAlign(VerticalAlign.TOP);
      div.getStyle().setMarginRight(2, Unit.EM);
      div.getStyle().setProperty("maxHeight", "400px");
      div.getStyle().setOverflowY(Overflow.AUTO);

      StringBuilder b = new StringBuilder();
      final String id = toId(c);
      b.append("<h3><a id='" + id + "' href='#run:" + id + "'>")
          .append(c.getName())
          .append("</a></h3>")
          .append("<div class='results' id='" + TEST_RESULTS + id + "'> </div>");
      try {
        final String path = c.getProtectionDomain().getCodeSource().getLocation().getPath();
        b.append("<sup><a href='file://" + path + "'>").append(path).append("</a></sup>");
      } catch (final Exception ignored) {
      }
      div.setInnerHTML(b.toString());
      for (final Method m : testClasses.get(c)) {
        final String methodId = m.getName() + c.hashCode();
        b = new StringBuilder();
        b.append("<pre>");
        b.append("<a href='javascript:'>");
        b.append(m.getName());
        b.append("</a>");
        b.append('(');
        b.append(ReflectUtil.joinClasses(", ", m.getParameterTypes()));
        b.append(')');
        b.append("</pre>");
        b.append("<div id='" + methodId + "'> </div>");
        final Element el = Document.get().createDivElement().cast();
        el.setInnerHTML(b.toString());
        DOM.setEventListener(
            el,
            new EventListener() {
              @Override
              public void onBrowserEvent(final Event event) {
                if (event.getTypeInt() == Event.ONCLICK) {
                  runTest(m);
                }
              }
            });
        DOM.sinkEvents(el, Event.ONCLICK);
        div.appendChild(el);
      }
      body.appendChild(div);
      final Element anchor = Document.get().getElementById(id).cast();
      DOM.setEventListener(
          anchor,
          new EventListener() {
            @Override
            public void onBrowserEvent(final Event event) {
              final Map<Method, Boolean> res = testResults.get(c);

              for (final Method m : res.keySet().toArray(new Method[res.size()])) {
                res.put(m, null);
              }
              updateTestClass(c);

              for (final Method m : testClasses.get(c)) {
                Scheduler.get()
                    .scheduleDeferred(
                        new ScheduledCommand() {
                          @Override
                          public void execute() {
                            runTest(m);
                          }
                        });
              }
            }
          });
      DOM.sinkEvents(anchor, Event.ONCLICK);
    }
  }