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.

GQueryAjaxTestGwt.java 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*
  2. * Copyright 2013, 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.GWT;
  19. import com.google.gwt.dom.client.Element;
  20. import com.google.gwt.junit.DoNotRunWith;
  21. import com.google.gwt.junit.Platform;
  22. import com.google.gwt.junit.client.GWTTestCase;
  23. import com.google.gwt.query.client.builders.Name;
  24. import com.google.gwt.query.client.builders.XmlBuilder;
  25. import com.google.gwt.query.client.plugins.ajax.Ajax;
  26. import com.google.gwt.user.client.ui.HTML;
  27. import com.google.gwt.user.client.ui.RootPanel;
  28. /**
  29. * Test class for testing ajax stuff.
  30. */
  31. public class GQueryAjaxTestGwt extends GWTTestCase {
  32. static Element e = null;
  33. static HTML testPanel = null;
  34. public String getModuleName() {
  35. return "com.google.gwt.query.QueryTest";
  36. }
  37. public void gwtTearDown() {
  38. $(e).remove();
  39. e = null;
  40. }
  41. public void gwtSetUp() {
  42. if (e == null) {
  43. testPanel = new HTML();
  44. RootPanel.get().add(testPanel);
  45. e = testPanel.getElement();
  46. e.setId("core-tst");
  47. } else {
  48. e.setInnerHTML("");
  49. }
  50. }
  51. interface XmlExample extends XmlBuilder {
  52. interface T extends XmlBuilder {
  53. }
  54. enum E {
  55. FOO, BAR
  56. }
  57. String getA();
  58. Boolean getB();
  59. @Name("c")
  60. int getNumber();
  61. XmlExample[] getX();
  62. @Name("x")
  63. XmlExample getFirstX();
  64. XmlExample setA(String s);
  65. @Name("c")
  66. XmlExample setNumber(int i);
  67. T getEnum();
  68. T getBool();
  69. T getNum();
  70. }
  71. public void testXmlBuilder() {
  72. String xml = "<a a='ra' b='true' c='-1.48'><x a='xa1'> text</x><x a='xa2'/><enum>FOO</enum><bool>true</bool><num>333</num></a>";
  73. XmlExample x = GWT.create(XmlExample.class);
  74. x.parse(xml);
  75. assertTrue(x.getB());
  76. assertEquals("ra", x.getA());
  77. assertEquals(-1, x.getNumber());
  78. assertEquals("xa2", x.getX()[1].getA());
  79. assertEquals("xa1", x.getFirstX().getA());
  80. x.setA("X").setNumber(1234);
  81. assertEquals("X", x.getA());
  82. assertEquals(1234, x.getNumber());
  83. assertEquals(" text", x.getFirstX().getText());
  84. x.getX()[0].setText("pepe");
  85. assertEquals("pepe", x.getFirstX().getText());
  86. assertEquals(XmlExample.E.FOO, x.getEnum().getTextAsEnum(XmlExample.E.class));
  87. assertEquals(true, x.getBool().getTextAsBoolean());
  88. assertEquals(333d, x.getNum().getTextAsNumber());
  89. }
  90. interface Feed extends XmlBuilder {
  91. interface Tag extends XmlBuilder {
  92. }
  93. Tag getTitle();
  94. Tag getTagline();
  95. Tag getFullcount();
  96. Tag getModified();
  97. interface Link extends XmlBuilder {
  98. String getHref();
  99. String getType();
  100. }
  101. Link getLink();
  102. interface Entry extends XmlBuilder {
  103. interface Author extends XmlBuilder {
  104. Tag getEmail();
  105. Tag getName();
  106. }
  107. Tag getTitle();
  108. Tag getSummary();
  109. Link getLink();
  110. Tag getModified();
  111. Tag getIssued();
  112. Tag getId();
  113. Author getAuthor();
  114. }
  115. Entry[] getEntry();
  116. }
  117. // FIXME: gquery xml does not work well with htmlUnit, FF & Safari works
  118. // TODO: test in IE
  119. @DoNotRunWith({Platform.HtmlUnitLayout})
  120. @SuppressWarnings("deprecation")
  121. public void testXmlGmailExample() {
  122. String xml = "<?xml version='1.0' encoding='UTF-8'?>" +
  123. "<feed version='0.3' xmlns='http://purl.org/atom/ns#'>"
  124. + " <title>Gmail - Inbox for manolo@...</title>"
  125. + " <tagline>New messages in your Gmail Inbox</tagline>"
  126. + " <fullcount>1</fullcount>"
  127. + " <link rel='alternate' href='http://mail.google.com/mail' type='text/html' />"
  128. + " <modified>2012-11-07T10:32:52Z</modified>"
  129. + " <entry>"
  130. + " <title>Trending Startups and Updates</title>"
  131. + " <summary>AngelList Weekly Trending Startups Storenvy Tumblr for stores E-Commerce Platforms · San Francisco</summary>"
  132. + " <link rel='alternate' href='http://mail.google.com/mail?account_id=manolo@....&amp;message_id=13ad2e227da1488b&amp;view=conv&amp;extsrc=atom' type='text/html' />"
  133. + " <modified>2012-11-05T23:22:47Z</modified>"
  134. + " <issued>2012-11-05T23:22:47Z</issued>"
  135. + " <id>tag:gmail.google.com,2004:1417840183363061889</id>"
  136. + " <author>"
  137. + " <name>AName</name>"
  138. + " <email>AnEmail</email>"
  139. + " </author>"
  140. + " </entry>"
  141. + "</feed>";
  142. Feed f = GWT.create(Feed.class);
  143. f.parse(xml);
  144. assertEquals((int)f.getFullcount().getTextAsNumber(), f.getEntry().length);
  145. assertEquals(112, f.getModified().getTextAsDate().getYear());
  146. assertEquals("AName", f.getEntry()[0].getAuthor().getName().getText());
  147. }
  148. @DoNotRunWith({Platform.HtmlUnitLayout})
  149. public void testJsonNonCallbackResponse() {
  150. delayTestFinish(5000);
  151. String testJsonpUrl = "http://www.google.com";
  152. Ajax.getJSONP(testJsonpUrl, null, new Function(){
  153. public void f() {
  154. Properties p = arguments(0);
  155. assertNull(p);
  156. finishTest();
  157. }
  158. }, 500);
  159. }
  160. }