Revision: 6316 Author: rj...@google.com Date: Wed Oct 7 13:03:24 2009 Log: Adds UiBinderUtilTest.
Created this while failing to reproduce a bug. Might as well hold on to it. Reviewed by: jgw http://code.google.com/p/google-web-toolkit/source/detail?r=6316 Added: /trunk/user/test/com/google/gwt/uibinder/client/UiBinderUtilTest.java Modified: /trunk/user/test/com/google/gwt/uibinder/UiBinderGwtSuite.java ======================================= --- /dev/null +++ /trunk/user/test/com/google/gwt/uibinder/client/UiBinderUtilTest.java Wed Oct 7 13:03:24 2009 @@ -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); + } + } +} ======================================= --- /trunk/user/test/com/google/gwt/uibinder/UiBinderGwtSuite.java Thu Sep 3 16:03:13 2009 +++ /trunk/user/test/com/google/gwt/uibinder/UiBinderGwtSuite.java Wed Oct 7 13:03:24 2009 @@ -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() { } } --~--~---------~--~----~------------~-------~--~----~ http://groups.google.com/group/Google-Web-Toolkit-Contributors -~----------~----~----~----~------~----~------~--~---