*/\r
package com.google.gwt.query.client;\r
\r
-import static com.google.gwt.query.client.GQuery.*;\r
+import static com.google.gwt.query.client.GQuery.$;\r
+import static com.google.gwt.query.client.GQuery.window;\r
\r
import com.google.gwt.core.client.EntryPoint;\r
-import com.google.gwt.dom.client.InputElement;\r
-import com.google.gwt.event.dom.client.ClickEvent;\r
-import com.google.gwt.event.dom.client.ClickHandler;\r
-import com.google.gwt.query.client.css.CSS;\r
-import com.google.gwt.query.client.css.RGBColor;\r
+import com.google.gwt.core.client.Scheduler;\r
+import com.google.gwt.core.client.Scheduler.RepeatingCommand;\r
import com.google.gwt.query.client.js.JsUtils;\r
-import com.google.gwt.user.client.Timer;\r
-import com.google.gwt.user.client.Window;\r
-import com.google.gwt.user.client.ui.Button;\r
-import com.google.gwt.user.client.ui.RootPanel;\r
\r
/**\r
* This module is thought to emulate a test environment similar to\r
* GWTTestCase, but running it in development mode.\r
* \r
- * The main goal of it is to execute tests in a faster way, because you only need\r
- * to push reload in your browser.\r
+ * The main goal of it is to execute tests in a faster way, because you just\r
+ * push reload in your browser after changing any code.\r
* \r
* @author manolo\r
* \r
public void onModuleLoad() {\r
try {\r
gwtSetUp();\r
- testAttr_Issue97();\r
+ // Replace this with the method to run\r
+ testSomething();\r
} catch (Exception ex) {\r
ex.printStackTrace();\r
$(e).html("").after("<div>ERROR: " + ex.getMessage() + "</div>");\r
}\r
}\r
\r
- public void testAttr_Issue97() {\r
- $(e).html("<input type='checkbox' id='cb' name='cb' value='1' />");\r
- assertNull($("#cb:checked", e).val());\r
- \r
- $("#cb", e).attr("checked", "checked");\r
- assertEquals(1, $("#cb:checked", e).length());\r
- assertEquals(true, InputElement.as($("#cb", e).get(0)).isChecked());\r
- assertEquals("checked", $("#cb", e).get(0).getAttribute("checked"));\r
- assertEquals(true, $("#cb", e).get(0).getPropertyBoolean("checked"));\r
- \r
- $("#cb", e).removeAttr("checked");\r
- assertEquals(0, $("#cb:checked", e).length());\r
- assertEquals(false, InputElement.as($("#cb", e).get(0)).isChecked());\r
- assertEquals("", $("#cb", e).get(0).getAttribute("checked"));\r
- assertEquals(false, $("#cb", e).get(0).getPropertyBoolean("checked"));\r
- \r
- $("#cb", e).attr("checked", true);\r
- assertEquals(1, $("#cb:checked", e).length());\r
- assertEquals(true, InputElement.as($("#cb", e).get(0)).isChecked());\r
- assertEquals("checked", $("#cb", e).get(0).getAttribute("checked"));\r
- assertEquals(true, $("#cb", e).get(0).getPropertyBoolean("checked"));\r
- \r
- $("#cb", e).attr("checked", false);\r
- assertEquals(0, $("#cb:checked", e).length());\r
- assertEquals(false, InputElement.as($("#cb", e).get(0)).isChecked());\r
- assertEquals("", $("#cb", e).get(0).getAttribute("checked"));\r
- assertEquals(false, $("#cb", e).get(0).getPropertyBoolean("checked"));\r
- \r
- $("#cb", e).attr("checked", "");\r
- assertEquals(1, $("#cb:checked", e).length());\r
- assertEquals(true, InputElement.as($("#cb", e).get(0)).isChecked());\r
- assertEquals("checked", $("#cb", e).get(0).getAttribute("checked"));\r
- assertEquals(true, $("#cb", e).get(0).getPropertyBoolean("checked"));\r
- \r
- GQuery gq = $("<div></div>test<!-- a comment-->");\r
- gq.attr("class", "test1");\r
- \r
- assertEquals("test1", gq.get(0).getClassName());\r
- assertEquals("test1", gq.attr("class"));\r
- assertNull(gq.get(0).getPropertyString("class"));\r
- \r
- gq.removeAttr("class");\r
- assertEquals("", gq.get(0).getClassName());\r
- assertEquals("", gq.attr("class"));\r
- \r
- //test on value\r
- $("#cb", e).attr("value", "mail");\r
- assertEquals("mail", InputElement.as($("#cb", e).get(0)).getValue());\r
- assertEquals("mail", $("#cb", e).get(0).getAttribute("value"));\r
- \r
- $("#cb", e).removeAttr("value");\r
- \r
- // Now HtmlUnit returns a null, before it returned empty\r
- String val = InputElement.as($("#cb", e).get(0)).getValue();\r
- if (val == null) {\r
- val = "";\r
- }\r
- assertEquals("", val);\r
- assertEquals("", $("#cb", e).get(0).getAttribute("value"));\r
- \r
- try{\r
- $("#cb", e).attr("type", "hidden");\r
- fail("Cannnot change a type of an element already attached to the dom");\r
- }catch (Exception e){}\r
- \r
- gq = $("<input type='text' value='blop'></input>");\r
- gq.attr("type", "radio");\r
- assertEquals("radio", InputElement.as(gq.get(0)).getType());\r
- assertEquals("blop", InputElement.as(gq.get(0)).getValue());\r
- \r
- gq.attr(Properties.create("{class:'test2', disabled:true}"));\r
- InputElement ie = InputElement.as(gq.get(0));\r
- \r
- assertEquals("test2", ie.getClassName());\r
- assertEquals(true, ie.isDisabled());\r
- assertEquals("disabled", ie.getAttribute("disabled"));\r
- \r
- }\r
- \r
- public void testCheckedAttr_Issue97() {\r
- $(e).html("<input type='checkbox' id='cb' name='cb' value='1' />");\r
- assertEquals("", $("#cb").val());\r
- $("#cb").attr("checked", "checked");\r
- assertEquals("1", $("#cb").val());\r
- $("#cb").removeAttr("checked");\r
- assertEquals("", $("#cb").val());\r
+ public void testSomething() {\r
+ // Copy and paste any test from the gquery suite\r
}\r
- \r
+ \r
/**\r
- * Run jquery code via jsni. test.html loads jquery, example:\r
+ * Runs jquery code via jsni.\r
* \r
+ * Example:\r
* System.out.println(evalJQuery("$('div').size()"));\r
*/\r
private native <T> T evalJQuery(String command) /*-{\r
command = command.replace(/\$/g, "$wnd.$");\r
try {\r
- return eval(command);\r
+ return "" + eval(command);\r
} catch(e) {\r
$wnd.alert(command + " " + e);\r
return "";\r
}\r
}-*/;\r
\r
+ /**\r
+ * Loads jquery and schedule the execution of the method testCompare()\r
+ * which should have code to test something in both in jquery and gquery.\r
+ * \r
+ * Put this method in onModuleLoad, and replace below the method to execute\r
+ * after jquery is available\r
+ */\r
public void runTestJQuery() {\r
JsUtils.loadScript("jquery-1.6.2.js", "jq");\r
- new Timer(){\r
+ Scheduler.get().scheduleFixedDelay(new RepeatingCommand() {\r
private int cont = 0;\r
private native boolean loaded(String func) /*-{\r
return eval("$wnd." + func) ? true : false; \r
}-*/;\r
- public void run() {\r
- if (cont++ > 10 || loaded("$")) {\r
- testCompare();\r
- } else {\r
- schedule(100);\r
+ public boolean execute() {\r
+ if (cont++ > 10 || JsUtils.hasProperty(window, "$")) {\r
+ \r
+ // Replace with the method to run\r
+ testJQueryCompare();\r
+ return false;\r
}\r
+ return true;\r
}\r
- }.run();\r
+ }, 100);\r
}\r
\r
- public void testCompareJquery() {\r
- $(e).html("<div class='outer' style='border: 4px solid red; padding: 25px; width: auto; height: 150px'>");\r
- int gqw = $(".outer").width();\r
+ public void testJQueryCompare() {\r
+ $(e).html("<div id='parent' style='background-color: yellow; width: 100px; height: 200px; top:130px; position: absolute; left: 130px'><p id='child' style='background-color: pink; width: 100px; height: 100px; position: absolute; padding: 5px'>Content 1</p></div>");\r
+ GQuery g = $("#child");\r
+ Properties prop1;\r
+\r
+ prop1 = GQuery.$$("marginTop: '0', marginLeft: '0', top: '0%', left: '0%', width: '100px', height: '100px', padding: '5px'");\r
+ g.css(prop1);\r
+ validateCurCSSBoth("#child", prop1.keys());\r
+ }\r
+\r
+ public void validateSizesBoth(String html) {\r
+ $(e).html(html);\r
+ String gqw = "" + $(".outer").width();\r
String jqw = evalJQuery("$('.outer').width()");\r
- \r
- int gqh = $(".outer").height();\r
+ String gqh = "" + $(".outer").height();\r
String jqh = evalJQuery("$('.outer').height()");\r
- \r
- String msg = ".outer size: GQuery: " + gqw + "x" + gqh + " jQuery: " + jqw + "x" + jqh;\r
- Window.alert(msg);\r
+\r
+ System.out.println(".outer size: GQuery: " + gqw + "x" + gqh + " jQuery: " + jqw + "x" + jqh);\r
+ assertEquals(gqw, jqw);\r
+ assertEquals(gqh, jqh);\r
}\r
\r
- public void validateCssBoth(String selector, boolean force, String... attrs) {\r
- for (String attr: attrs) {\r
- String gs = $(selector).css(attr, force);\r
- String js = evalJQuery("$.css($('" + selector + "').get(0), '" + attr + "', " + force + ")");\r
- System.out.println(selector + " " + attr + " " + force + " g:" + gs + " j:" + js + " " + (gs.replaceAll("px", "").equals(js.replaceAll("px", ""))));\r
+ public void validateCssBoth(String selector, boolean force, String... props) {\r
+ for (String prop: props) {\r
+ String gs = $(selector).css(prop, force);\r
+ String js = evalJQuery("$.css($('" + selector + "').get(0), '" + prop + "', " + force + ")");\r
+ System.out.println(selector + " prop:" + prop + " force:" + force + " gQuery:" + gs + " jQuery:" + js);\r
assertEquals(gs.replaceAll("px", ""), js.replaceAll("px", ""));\r
}\r
}\r
- public void validateCurBoth(String selector, String... attrs) {\r
- for (String attr: attrs) {\r
- String gs = Double.toString($(selector).cur(attr, true)).replaceFirst("\\.\\d+$", "");\r
- String js = evalJQuery("$.cur($('" + selector + "').get(0), '" + attr + "', true)");\r
- System.out.println(selector + " " + attr + " " + gs + " " + js + " " + (gs.equals(js)));\r
+ \r
+ public void validateCurCSSBoth(String selector, String... props) {\r
+ for (String prop: props) {\r
+ String gs = Double.toString($(selector).cur(prop, true)).replaceFirst("\\.\\d+$", "");\r
+ String js = evalJQuery("$.curCSS($('" + selector + "').get(0), '" + prop + "', true)");\r
+ gs = gs.replaceAll("px$", "");\r
+ js = js.replaceAll("px$", "");\r
+ System.out.println(selector + " prop:" + prop + " gQuery:" + gs + " jQuery:" + js);\r
assertEquals(gs, js);\r
}\r
}\r
\r
- public void testCompare() {\r
- $(e).html("<div id='parent' style='background-color: yellow; width: 100px; height: 200px; top:130px; position: absolute; left: 130px'><p id='child' style='background-color: pink; width: 100px; height: 100px; position: absolute; padding: 5px'>Content 1</p></div>");\r
- GQuery g = $("#child");\r
- Properties prop1;\r
-\r
- prop1 = GQuery.$$("marginTop: '0', marginLeft: '0', top: '0%', left: '0%', width: '100px', height: '100px', padding: '5px'");\r
- g.css(prop1);\r
- validateCurBoth("#child", prop1.keys());\r
- }\r
- \r
- \r
// This method is used to initialize a huge html String, because\r
// java 1.5 has a limitation in the size of static strings.\r
private String getTestContent() {\r