Adds UiBinderUtilTest.
Created this while failing to reproduce a bug. Might as well hold on to it.
Reviewed by: jgw
git-svn-id: https://google-web-toolkit.googlecode.com/svn/trunk@6316 8db76d5a-ed1c-0410-87a9-c151d255dfc7
diff --git a/user/test/com/google/gwt/uibinder/UiBinderGwtSuite.java b/user/test/com/google/gwt/uibinder/UiBinderGwtSuite.java
index f4b717e..f4006c1 100644
--- a/user/test/com/google/gwt/uibinder/UiBinderGwtSuite.java
+++ b/user/test/com/google/gwt/uibinder/UiBinderGwtSuite.java
@@ -1,12 +1,12 @@
/*
* Copyright 2009 Google Inc.
- *
+ *
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
- *
+ *
* http://www.apache.org/licenses/LICENSE-2.0
- *
+ *
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
@@ -16,6 +16,7 @@
package com.google.gwt.uibinder;
import com.google.gwt.junit.tools.GWTTestSuite;
+import com.google.gwt.uibinder.client.UiBinderUtilTest;
import com.google.gwt.uibinder.sample.client.UiBinderTest;
import junit.framework.Test;
@@ -26,12 +27,13 @@
public class UiBinderGwtSuite {
public static Test suite() {
GWTTestSuite suite = new GWTTestSuite("Test suite for UiBinder GWTTestCases");
-
+
+ suite.addTestSuite(UiBinderUtilTest.class);
suite.addTestSuite(UiBinderTest.class);
-
+
return suite;
}
-
+
private UiBinderGwtSuite() {
}
}
diff --git a/user/test/com/google/gwt/uibinder/client/UiBinderUtilTest.java b/user/test/com/google/gwt/uibinder/client/UiBinderUtilTest.java
new file mode 100644
index 0000000..6503081
--- /dev/null
+++ b/user/test/com/google/gwt/uibinder/client/UiBinderUtilTest.java
@@ -0,0 +1,159 @@
+/*
+ * Copyright 2009 Google Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package com.google.gwt.uibinder.client;
+
+import com.google.gwt.dom.client.DivElement;
+import com.google.gwt.dom.client.Document;
+import com.google.gwt.dom.client.Element;
+import com.google.gwt.dom.client.Style.Display;
+import com.google.gwt.dom.client.Style.Visibility;
+import com.google.gwt.junit.client.GWTTestCase;
+import com.google.gwt.user.client.DOM;
+import com.google.gwt.user.client.Random;
+import com.google.gwt.user.client.ui.RootPanel;
+
+/**
+ * Tests {@link UiBinderUtil}
+ */
+public class UiBinderUtilTest extends GWTTestCase {
+
+ @Override
+ public String getModuleName() {
+ return "com.google.gwt.uibinder.UiBinderTestModule";
+ }
+
+ public void testAttachToDomAndGetChildUnattached() {
+ go();
+ }
+
+ public void testAttachToDomAndGetChildUnderUnattached() {
+ DivElement div = Document.get().createDivElement();
+ try {
+ go(div);
+ } finally {
+ detach(div);
+ }
+ }
+
+ public void testAttachToDomAndGetChildUnderHidden() {
+ DivElement div = Document.get().createDivElement();
+ try {
+ RootPanel.getBodyElement().appendChild(div);
+ div.getStyle().setVisibility(Visibility.HIDDEN);
+ go(div);
+ } finally {
+ detach(div);
+ }
+ }
+
+ public void testAttachToDomAndGetChildUnderDisplayNone() {
+ DivElement div = Document.get().createDivElement();
+ try {
+ RootPanel.getBodyElement().appendChild(div);
+ div.getStyle().setDisplay(Display.NONE);
+ go(div);
+ } finally {
+ detach(div);
+ }
+ }
+
+ public void testAttachToDomAndGetChildUnderAttachedThenUnattached() {
+ DivElement div = Document.get().createDivElement();
+ detach(div);
+ try {
+ RootPanel.getBodyElement().appendChild(div);
+ go(div);
+ } finally {
+ detach(div);
+ }
+ }
+
+ /**
+ * Make sure this test's clean up method actually works.
+ */
+ public void testDetach() {
+ DivElement div = Document.get().createDivElement();
+ RootPanel.getBodyElement().appendChild(div);
+ detach(div);
+ assertNull(div.getParentNode());
+ }
+
+ private void assertStartsWith(String string, String prefix) {
+ assertTrue('"' + string + "\" should start with \"" + prefix + "\"",
+ string.startsWith(prefix));
+ }
+
+ private void findAndAssertTextBeforeFirstChild(Element div, String id,
+ String firstText) {
+ UiBinderUtil.TempAttachment t = UiBinderUtil.attachToDom(div);
+ Element child = Document.get().getElementById(id);
+ t.detach();
+ assertStartsWith(child.getInnerHTML(), firstText + "<");
+ }
+
+ private void detach(Element div) {
+ if (div != null) {
+ Element parent = div.getParentElement();
+ if (parent != null) {
+ parent.removeChild(div);
+ }
+ }
+ }
+
+ private void go() {
+ go(null);
+ }
+
+ private void go(Element underHere) {
+ Element div = null;
+ try {
+ String ableId = DOM.createUniqueId();
+ String bakerId = DOM.createUniqueId();
+ String charlieId = DOM.createUniqueId();
+ String deltaId = DOM.createUniqueId();
+
+ String ableText = "able" + Random.nextInt();
+ String bakerText = "baker" + Random.nextInt();
+ String charlieText = "charlie" + Random.nextInt();
+ String deltaText = "delta" + Random.nextInt();
+
+ StringBuilder b = new StringBuilder();
+ b.append("<div>");
+ b.append("<span id='").append(ableId).append("'>").append(ableText);
+ b.append("<span id='").append(bakerId).append("'>").append(bakerText);
+ b.append("<span id='").append(charlieId).append("'>").append(charlieText);
+ b.append("<span id='").append(deltaId).append("'>").append(deltaText);
+ b.append("</span>").append("</span>").append("</span>").append("</span>");
+ b.append("</div>");
+
+ div = UiBinderUtil.fromHtml(b.toString());
+ if (underHere != null) {
+ underHere.insertFirst(div);
+ }
+ findAndAssertTextBeforeFirstChild(div, ableId, ableText);
+ findAndAssertTextBeforeFirstChild(div, bakerId, bakerText);
+ findAndAssertTextBeforeFirstChild(div, charlieId, charlieText);
+ UiBinderUtil.TempAttachment t = UiBinderUtil.attachToDom(div);
+ Element e = Document.get().getElementById(deltaId);
+ t.detach();
+ assertEquals(deltaText, e.getInnerText());
+ } finally {
+ // tearDown isn't reliable enough, e.g. doesn't fire when exceptions
+ // happen
+ detach(div);
+ }
+ }
+}