Author: rj...@google.com
Date: Mon Jul 13 14:05:32 2009
New Revision: 5725

Added:
    trunk/user/src/com/google/gwt/junit/FakeMessagesMaker.java
    trunk/user/test/com/google/gwt/junit/FakeMessagesMakerTest.java
    trunk/user/test/com/google/gwt/junit/JUnitSuite.java
Modified:
    trunk/user/src/com/google/gwt/user/client/ui/HTMLPanel.java
    trunk/user/test/com/google/gwt/user/client/ui/HTMLPanelTest.java

Log:
HTMLPanel now allows an arbitrary element to be set as its root, via
new constructor HTMLPanel(String tag, String html). Reviewed by jgw

   http://gwt-code-reviews.appspot.com/39802

Introduces FakeMessagesMaker, to allow JUnit (non-GWTTestCase) testing
of objects that rely on generated Messages objects. Reviewed by scottb

   http://gwt-code-reviews.appspot.com/48809



Added: trunk/user/src/com/google/gwt/junit/FakeMessagesMaker.java
==============================================================================
--- (empty file)
+++ trunk/user/src/com/google/gwt/junit/FakeMessagesMaker.java  Mon Jul 13  
14:05:32 2009
@@ -0,0 +1,63 @@
+/*
+ * 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.junit;
+
+import com.google.gwt.i18n.client.Messages;
+
+import java.lang.reflect.InvocationHandler;
+import java.lang.reflect.Method;
+import java.lang.reflect.Proxy;
+import java.util.Arrays;
+
+/**
+ * Helper to make a fake implementation of any {...@link Messages} interface  
via
+ * reflection, for use in JUnit tests. (This will not work in  
GWTTestCase.) All
+ * calls to the returned object return the method name followed by the  
passed
+ * parameters as a list surrounded by [].
+ * <p>
+ * Note that the default message text is very consciously not made  
available
+ * through the fake, to help tests ensure that specific translations of
+ * localized text are not relied upon.
+ * <p>
+ * Sample use:
+ *
+ * <pre>interface MyMessages extends Messages {
+ *   &#64;DefaultMessage("Isn''t this the fakiest?")
+ *   &#64;Description("A sample message to be tested.")
+ *   String myMessage();
+ * }
+ *
+ * public void testSimple() {
+ *  MyMessages messages = FakeMessagesMaker.create(MyMessages.class);
+ *  assertEquals("myMessage", messages.myMessage());
+ * }
+ * </pre>
+ */
+public class FakeMessagesMaker implements InvocationHandler {
+  public static <T extends Messages> T create(Class<T> messagesClass) {
+    return messagesClass.cast(Proxy.newProxyInstance(
+        FakeMessagesMaker.class.getClassLoader(), new Class[]  
{messagesClass},
+        new FakeMessagesMaker()));
+  }
+
+  public Object invoke(Object proxy, Method method, Object[] args)
+      throws Throwable {
+    String name = method.getName();
+
+    return (args == null || args.length == 0) ? name : name
+        + Arrays.asList(args);
+  }
+}

Modified: trunk/user/src/com/google/gwt/user/client/ui/HTMLPanel.java
==============================================================================
--- trunk/user/src/com/google/gwt/user/client/ui/HTMLPanel.java (original)
+++ trunk/user/src/com/google/gwt/user/client/ui/HTMLPanel.java Mon Jul 13  
14:05:32 2009
@@ -40,16 +40,29 @@
    }

    /**
-   * Creates an HTML panel with the specified HTML contents. Any element  
within
-   * this HTML that has a specified id can contain a child widget.
+   * Creates an HTML panel with the specified HTML contents inside a DIV
+   * element. Any element within this HTML that has a specified id can  
contain a
+   * child widget.
     *
     * @param html the panel's HTML
     */
    public HTMLPanel(String html) {
-    setElement(DOM.createDiv());
-    DOM.setInnerHTML(getElement(), html);
+    this("div", html);
    }

+  /**
+   * Creates an HTML panel whose root element has the given tag, and with  
the
+   * specified HTML contents. Any element within this HTML that has a  
specified
+   * id can contain a child widget.
+   *
+   * @param tag the tag of the root element
+   * @param html the panel's HTML
+   */
+  public HTMLPanel(String tag, String html) {
+    setElement(DOM.createElement(tag));
+    DOM.setInnerHTML(getElement(), html);
+  }
+
    /**
     * Adds a child widget to the panel, contained within the HTML element
     * specified by a given id.

Added: trunk/user/test/com/google/gwt/junit/FakeMessagesMakerTest.java
==============================================================================
--- (empty file)
+++ trunk/user/test/com/google/gwt/junit/FakeMessagesMakerTest.java     Mon Jul 
 
13 14:05:32 2009
@@ -0,0 +1,47 @@
+/*
+ * 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.junit;
+
+import com.google.gwt.i18n.client.Messages;
+
+import junit.framework.TestCase;
+
+/**
+ * Tests of FakeMessagesMaker.
+ */
+public class FakeMessagesMakerTest extends TestCase {
+  interface MyMessages extends Messages {
+    @DefaultMessage("Isn''t this the fakiest?")
+    @Description("A sample message to be tested.")
+    String myMessage();
+
+    @DefaultMessage("Isn''t this the fakiest? Pick one: {1} or {2}?")
+    @Description("A sample message with parameters.")
+    String myArgumentedMessage(@Example("yes") String yes,
+        @Example("no") String no);
+  }
+
+  public void testSimple() {
+    MyMessages messages = FakeMessagesMaker.create(MyMessages.class);
+    assertEquals("myMessage", messages.myMessage());
+  }
+
+  public void testArgs() {
+    MyMessages messages = FakeMessagesMaker.create(MyMessages.class);
+    assertEquals("myArgumentedMessage[oui, non]",
+        messages.myArgumentedMessage("oui", "non"));
+  }
+}

Added: trunk/user/test/com/google/gwt/junit/JUnitSuite.java
==============================================================================
--- (empty file)
+++ trunk/user/test/com/google/gwt/junit/JUnitSuite.java        Mon Jul 13  
14:05:32 2009
@@ -0,0 +1,47 @@
+/*
+ * 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.junit;
+
+import com.google.gwt.junit.client.GWTTestCaseTest;
+import com.google.gwt.junit.tools.GWTTestSuite;
+
+import junit.framework.Test;
+
+/**
+ * Tests of the junit package.
+ */
+public class JUnitSuite {
+  public static Test suite() {
+    GWTTestSuite suite = new GWTTestSuite("Test for suite for  
com.google.gwt.junit");
+
+    suite.addTestSuite(FakeMessagesMakerTest.class);
+
+    // client
+    // Suppressed due to flakiness on Linux
+    // suite.addTestSuite(BenchmarkTest.class);
+    suite.addTestSuite(GWTTestCaseTest.class);
+
+    // These two are intended only to be run manually. See class comments
+    // suite.addTestSuite(ParallelRemoteTest.class);
+    // suite.addTestSuite(TestManualAsync.class);
+
+    // remote
+    // Run manually only, launches servers that die on port contention
+    // suite.addTestSuite(BrowserManagerServerTest.class);
+
+    return suite;
+  }
+}

Modified: trunk/user/test/com/google/gwt/user/client/ui/HTMLPanelTest.java
==============================================================================
--- trunk/user/test/com/google/gwt/user/client/ui/HTMLPanelTest.java     
(original)
+++ trunk/user/test/com/google/gwt/user/client/ui/HTMLPanelTest.java    Mon  
Jul 13 14:05:32 2009
@@ -1,12 +1,12 @@
  /*
   * Copyright 2008 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
@@ -117,6 +117,28 @@

      Node next = button.getElement().getNextSibling();
      assertEquals("bar", next.getNodeValue());
+  }
+
+  /**
+   * Tests arbitrary root tag
+   */
+  public void testCustomRootTag() {
+    HTMLPanel hp = new HTMLPanel("table", "<tr><td>Hello <span  
id='labelHere'></span></td></tr>");
+    InlineLabel label = new InlineLabel("World");
+    hp.addAndReplaceElement(label, "labelHere");
+
+    Element parent = label.getElement().getParentElement();
+    assertEquals("td", parent.getTagName().toLowerCase());
+
+    parent = parent.getParentElement();
+    assertEquals("tr", parent.getTagName().toLowerCase());
+
+    while (parent != null && parent != hp.getElement()) {
+      parent = parent.getParentElement();
+    }
+
+    assertNotNull(parent);
+    assertEquals("table", parent.getTagName().toLowerCase());
    }

    /**

--~--~---------~--~----~------------~-------~--~----~
http://groups.google.com/group/Google-Web-Toolkit-Contributors
-~----------~----~----~----~------~----~------~--~---

Reply via email to