You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

GQueryJsInteropTestGwt.java 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. * Copyright 2011, The gwtquery team.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.google.gwt.query.client;
  17. import static com.google.gwt.query.client.GQuery.$;
  18. import com.google.gwt.core.client.JavaScriptObject;
  19. import com.google.gwt.core.client.ScriptInjector;
  20. import com.google.gwt.core.client.js.JsProperty;
  21. import com.google.gwt.core.client.js.JsType;
  22. import com.google.gwt.dom.client.Element;
  23. import com.google.gwt.junit.DoNotRunWith;
  24. import com.google.gwt.junit.Platform;
  25. import com.google.gwt.junit.client.GWTTestCase;
  26. import com.google.gwt.query.client.js.JsUtils;
  27. import com.google.gwt.user.client.DOM;
  28. import com.google.gwt.user.client.ui.HTML;
  29. import com.google.gwt.user.client.ui.RootPanel;
  30. /**
  31. * Test class for testing jsinterop
  32. */
  33. public class GQueryJsInteropTestGwt extends GWTTestCase {
  34. static Element e = null;
  35. static HTML testPanel = null;
  36. public String getModuleName() {
  37. return "com.google.gwt.query.QueryTest";
  38. }
  39. public void gwtTearDown() {
  40. $(e).remove();
  41. e = null;
  42. }
  43. public void gwtSetUp() {
  44. if (e == null || DOM.getElementById("core-tst") == null) {
  45. testPanel = new HTML();
  46. RootPanel.get().add(testPanel);
  47. e = testPanel.getElement();
  48. e.setId("core-tst");
  49. } else {
  50. e.setInnerHTML("");
  51. }
  52. }
  53. @JsType(prototype = "Window", isNative = true)
  54. public interface HTMLWindow {
  55. @JsProperty String getName();
  56. @JsProperty void setName(String name);
  57. }
  58. @JsType(prototype = "Window", isNative = true)
  59. public interface HWindow {
  60. @JsProperty HDocument document();
  61. }
  62. @JsType(prototype = "HTMLDocument", isNative = false)
  63. public interface HDocument {
  64. HElement createElement(String tag);
  65. }
  66. @JsType(prototype = "HTMLElement", isNative = false)
  67. public interface HElement {
  68. @JsProperty void id(String s);
  69. }
  70. // jsInterop only works in prod mode.
  71. @DoNotRunWith({Platform.HtmlUnitUnknown, Platform.Devel})
  72. public void testJsType() {
  73. JavaScriptObject jsw = ScriptInjector.TOP_WINDOW;
  74. // FIXME: this might not be necessary when GWT issue #9059 is fixed
  75. HWindow w = JsUtils.cast(jsw);
  76. assertNotNull(w);
  77. HDocument d = w.document();
  78. assertNotNull(d);
  79. HElement h = d.createElement("div");
  80. h.id("foo");
  81. // GQuery must support native elements created through jsInterop interfaces
  82. $(h).appendTo(e);
  83. GQuery f = $("#foo", e);
  84. assertEquals(1, f.size());
  85. }
  86. }