Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

GQueryWidgetsTestGwt.java 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 static com.google.gwt.query.client.GQuery.document;
  19. import com.google.gwt.dom.client.Element;
  20. import com.google.gwt.event.dom.client.ClickEvent;
  21. import com.google.gwt.event.dom.client.ClickHandler;
  22. import com.google.gwt.junit.client.GWTTestCase;
  23. import com.google.gwt.query.client.plugins.Widgets;
  24. import com.google.gwt.query.client.plugins.widgets.WidgetFactory;
  25. import com.google.gwt.query.client.plugins.widgets.WidgetsUtils;
  26. import com.google.gwt.user.client.ui.Button;
  27. import com.google.gwt.user.client.ui.FlowPanel;
  28. import com.google.gwt.user.client.ui.HTML;
  29. import com.google.gwt.user.client.ui.Label;
  30. import com.google.gwt.user.client.ui.RootPanel;
  31. import com.google.gwt.user.client.ui.Widget;
  32. /**
  33. * Test class for testing gwtquery widgets plugin api.
  34. */
  35. public class GQueryWidgetsTestGwt extends GWTTestCase {
  36. static Element e = null;
  37. static HTML testPanel = null;
  38. int testSubmitEventCont = 0;
  39. public String getModuleName() {
  40. return "com.google.gwt.query.QueryTest";
  41. }
  42. public void testGWTQueryCoreWidgets() {
  43. final FlowPanel p = new FlowPanel();
  44. Button b = new Button("test");
  45. RootPanel.get().add(b);
  46. RootPanel.get().add(p);
  47. int nitems = 4;
  48. final String label1 = "I'm the label ";
  49. final String label2 = "Finally I'm just a simple label";
  50. for (int i = 0; i < nitems; i++) {
  51. Label l = new Label(label1 + i);
  52. p.add(l);
  53. }
  54. $("<div>whatever</div").appendTo($(p));
  55. b.addClickHandler(new ClickHandler() {
  56. public void onClick(ClickEvent event) {
  57. $(".gwt-Label", p).each(new Function() {
  58. @Override
  59. public void f(Widget w) {
  60. Label l = (Label) w;
  61. l.setText(label2);
  62. }
  63. });
  64. }
  65. });
  66. $(".gwt-Label", p).each(new Function() {
  67. @Override
  68. public Object f(Widget w, int i) {
  69. assertEquals(label1 + i, ((Label)w).getText());
  70. return null;
  71. }
  72. });
  73. $(b).click();
  74. $(".gwt-Label", p).each(new Function() {
  75. public void f(Element e) {
  76. assertEquals(label2, $(e).text());
  77. }
  78. });
  79. $("div", p).each(new Function() {
  80. public void f(Element e) {
  81. // Just to avoid the exception when non-widget elements match
  82. }
  83. public void f(Widget w) {
  84. if (w instanceof Label) {
  85. assertEquals(label2, $(w).text());
  86. }
  87. }
  88. });
  89. p.removeFromParent();
  90. b.removeFromParent();
  91. }
  92. class TestButtonWidgetFactory implements WidgetFactory<Button> {
  93. public Button create(Element e) {
  94. Button button = new Button();
  95. button.getElement().setInnerText(e.getInnerText());
  96. WidgetsUtils.replaceOrAppend(e, button);
  97. return button;
  98. }
  99. }
  100. public void testGQueryWidgets() {
  101. final Button b1 = new Button("click-me");
  102. RootPanel.get().add(b1);
  103. GQuery g = $(b1);
  104. Button b2 = (Button) g.as(Widgets.Widgets).widget();
  105. assertEquals(b1, b2);
  106. b2 = $("<button>Click-me</button>").appendTo(document)
  107. .as(Widgets.Widgets).widgets(new TestButtonWidgetFactory(), null).widget();
  108. b2.addClickHandler(new ClickHandler() {
  109. public void onClick(ClickEvent event) {
  110. $(b1).css("color", "red");
  111. }
  112. });
  113. b2.click();
  114. assertEquals("red", $(b1).css("color", false));
  115. }
  116. public void testSelectorWidget() {
  117. final Button b1 = new Button("Button click");
  118. RootPanel.get().add(b1);
  119. GQuery g = $(b1);
  120. assertTrue(g.isVisible());
  121. g.hide();
  122. assertFalse(g.isVisible());
  123. b1.removeFromParent();
  124. }
  125. }