From 272e36a69de872b587f4463fda888dda5d3a1807 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Manuel=20Carrasco=20Mo=C3=B1ino?= Date: Wed, 10 Apr 2013 10:49:27 +0200 Subject: [PATCH] Change some browser dependencies so as we can use Deferred and Callbacks in JVM, added Jre tests for this stuff --- .../com/google/gwt/query/client/GQuery.java | 31 ++-- .../client/plugins/deferred/Callbacks.java | 81 +++----- .../query/client/GQueryDeferredTestGwt.java | 175 ------------------ .../gwt/query/client/GQueryGwtSuiteTest.java | 4 +- 4 files changed, 46 insertions(+), 245 deletions(-) diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/GQuery.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/GQuery.java index ba2b7861..13d49795 100644 --- a/gwtquery-core/src/main/java/com/google/gwt/query/client/GQuery.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/GQuery.java @@ -49,7 +49,6 @@ import com.google.gwt.query.client.js.JsMap; import com.google.gwt.query.client.js.JsNamedArray; import com.google.gwt.query.client.js.JsNodeArray; import com.google.gwt.query.client.js.JsObjectArray; -import com.google.gwt.query.client.js.JsRegexp; import com.google.gwt.query.client.js.JsUtils; import com.google.gwt.query.client.plugins.Effects; import com.google.gwt.query.client.plugins.Events; @@ -61,6 +60,7 @@ import com.google.gwt.query.client.plugins.deferred.Deferred; import com.google.gwt.query.client.plugins.effects.PropertiesAnimation.Easing; import com.google.gwt.query.client.plugins.events.EventsListener; import com.google.gwt.query.client.plugins.widgets.WidgetsUtils; +import com.google.gwt.regexp.shared.RegExp; import com.google.gwt.user.client.DOM; import com.google.gwt.user.client.Event; import com.google.gwt.user.client.EventListener; @@ -122,7 +122,7 @@ public class GQuery implements Lazy { /** * The body element in the current page. */ - public static final BodyElement body = Document.get().getBody(); + public static final BodyElement body = GWT.isClient() ? Document.get().getBody() : null; /** * Object to store element data (public so as we can access to it from tests). @@ -132,7 +132,7 @@ public class GQuery implements Lazy { /** * The document element in the current page. */ - public static final Document document = Document.get(); + public static final Document document = GWT.isClient() ? Document.get() : null; /** * Static reference Effects plugin @@ -162,15 +162,14 @@ public class GQuery implements Lazy { // Sizzle POS regex : usefull in some methods // TODO: Share this static with SelectorEngineSizzle - private static final String POS_REGEX = - ":(nth|eq|gt|lt|first|last|even|odd)(?:\\((\\d*)\\))?(?=[^\\-]|$)"; + private static RegExp posRegex = RegExp.compile("^:(nth|eq|gt|lt|first|last|even|odd)(?:\\((\\d*)\\))?(?=[^\\-]|$)$"); /** * Implementation class used for style manipulations. */ private static DocumentStyleImpl styleImpl; - private static JsRegexp tagNameRegex = new JsRegexp("<([\\w:]+)"); + private static RegExp tagNameRegex = RegExp.compile("<([\\w:]+)"); /** * Static reference to the Widgets plugin @@ -180,7 +179,7 @@ public class GQuery implements Lazy { /** * The window object. */ - public static final Element window = window(); + public static final Element window = GWT.isClient() ? window() : null; private static Element windowData = null; @@ -435,7 +434,7 @@ public class GQuery implements Lazy { protected static GQuery cleanHtmlString(String elem, Document doc) { - String tag = tagNameRegex.exec(elem).get(1); + String tag = tagNameRegex.exec(elem).getGroup(1); if (tag == null) { return $(doc.createTextNode(elem)); } @@ -701,11 +700,15 @@ public class GQuery implements Lazy { } public static Class registerPlugin(Class plugin, Plugin pluginFactory) { - if (plugins == null) { - plugins = JsMap.createObject().cast(); + // TODO: decide whether change plugins type to java.util.list + // Right now we only test static methods in gquery, so this is only needed when initializing + // plugins shortcuts in gquery. + if (GWT.isClient()) { + if (plugins == null) { + plugins = JsMap.createObject().cast(); + } + plugins.put(plugin, pluginFactory); } - - plugins.put(plugin, pluginFactory); return plugin; } @@ -1398,7 +1401,7 @@ public class GQuery implements Lazy { context = currentContext; } - GQuery pos = selector.matches(POS_REGEX) ? $(selector, context) : null; + GQuery pos = posRegex.test(selector) ? $(selector, context) : null; JsNodeArray result = JsNodeArray.create(); for (Element e : elements) { @@ -1452,7 +1455,7 @@ public class GQuery implements Lazy { JsNamedArray matches = JsNamedArray.create(); for (String selector : selectors) { if (!matches.exists(selector)) { - matches.put(selector, selector.matches(POS_REGEX) ? $(selector, context) : null); + matches.put(selector, posRegex.test(selector) ? $(selector, context) : null); } } diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/deferred/Callbacks.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/deferred/Callbacks.java index 1e43ad01..03108068 100644 --- a/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/deferred/Callbacks.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/deferred/Callbacks.java @@ -13,11 +13,11 @@ */ package com.google.gwt.query.client.plugins.deferred; -import com.google.gwt.core.shared.GWT; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + import com.google.gwt.query.client.Function; -import com.google.gwt.query.client.Properties; -import com.google.gwt.query.client.builders.JsonBuilder; -import com.google.gwt.query.client.js.JsObjectArray; /** * Implementation of jQuery.Callbacks for gwtquery. @@ -36,48 +36,18 @@ public class Callbacks { boolean f(Object ...objects); } - /** - * Interface representing the options of a Callbacks collection. - * - * To create an implementation of this interface just call: Callbacks.createOptions() - */ - public static interface CallbackOptions extends JsonBuilder { - boolean getMemory(); - boolean getOnce(); - boolean getStopOnFalse(); - boolean getUnique(); - CallbackOptions setMemory(); - CallbackOptions setOnce(); - CallbackOptions setStopOnFalse(); - CallbackOptions setUnique(); - } - - public static CallbackOptions createOptions() { - return GWT.create(CallbackOptions.class); - } - - private JsObjectArray stack = JsObjectArray.create(); + private List stack = new ArrayList(); private boolean done = false; - private JsObjectArray memory = null; + private List memory = null; - public final CallbackOptions opts; + private boolean isOnce, isMemory, isUnique, stopOnFalse; - /** - * Create a new Callbacks object with default options - */ public Callbacks() { - opts = createOptions(); - } - - /** - * Create a new Callbacks object with given options - */ - public Callbacks(CallbackOptions options) { - opts = options; + this(""); } - + /** * Create a new Callbacks object with options given as a space delimited string. * @@ -86,8 +56,10 @@ public class Callbacks { * once, memory, unique, stopOnFalse */ public Callbacks(String options) { - this(); - opts.load(Properties.create(options.replaceAll("[^\\S]+|$", ":true,"))); + isOnce = options.contains("once"); + isMemory = options.contains("memory"); + isUnique = options.contains("unique"); + stopOnFalse = options.contains("stopOnFalse"); } /** @@ -128,7 +100,7 @@ public class Callbacks { * lock */ public Callbacks lock() { - if (!opts.getMemory()) { + if (!isMemory) { disable(); } stack = null; @@ -140,14 +112,14 @@ public class Callbacks { */ public Callbacks fire(Object... o) { if (!done) { - done = opts.getOnce(); - if (stack != null) for (Object c : stack.elements()) { - if (!run(c, o) && opts.getStopOnFalse()) { + done = isOnce; + if (stack != null) for (Object c : stack) { + if (!run(c, o) && stopOnFalse) { break; } } - if (opts.getMemory()) { - memory = JsObjectArray.create().add(o); + if (isMemory) { + memory = new ArrayList(Arrays.asList(o)); } } return this; @@ -157,25 +129,26 @@ public class Callbacks { * Remove a callback or a collection of callbacks from a callback list. */ public Callbacks remove(Object... o) { - stack.remove(o); + stack.removeAll(Arrays.asList(o)); return this; } private void addAll(Object...o) { for (Object c : o) { - if (!done && stack != null && c != null && (!opts.getUnique() || !stack.contains(c))) { + if (!done && stack != null && c != null && (!isUnique || !stack.contains(c))) { stack.add(c); } // In jQuery add always is run when memory is true even when unique is set - if (opts.getMemory() && memory != null) { - run(c, memory.elements()); + if (isMemory && memory != null) { + run(c, memory.toArray()); } } } @SuppressWarnings({"unchecked", "rawtypes"}) private boolean run(Object c, Object...o) { - // Unbox array into array, it happens when running filters in Promise.then + // Unbox array inside array when there is only an element. + // It happens when running filters in Promise.then() if (o.length == 1 && o[0].getClass().isArray()) { o = (Object[])o[0]; } @@ -191,6 +164,6 @@ public class Callbacks { } public String status() { - return (stack == null ? 0 : stack.length()) + " " + done; + return (stack == null ? 0 : stack.size()) + " " + done; } -} +} \ No newline at end of file diff --git a/gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryDeferredTestGwt.java b/gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryDeferredTestGwt.java index f20426cb..3b8798d9 100644 --- a/gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryDeferredTestGwt.java +++ b/gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryDeferredTestGwt.java @@ -22,10 +22,7 @@ import static com.google.gwt.query.client.GQuery.document; import com.google.gwt.core.client.Duration; import com.google.gwt.junit.client.GWTTestCase; -import com.google.gwt.query.client.Promise.Deferred; import com.google.gwt.query.client.plugins.ajax.Ajax; -import com.google.gwt.query.client.plugins.deferred.Callbacks; -import com.google.gwt.query.client.plugins.deferred.Callbacks.Callback; import com.google.gwt.query.client.plugins.deferred.PromiseFunction; import com.google.gwt.user.client.Element; import com.google.gwt.user.client.Timer; @@ -61,118 +58,6 @@ public class GQueryDeferredTestGwt extends GWTTestCase { } } - private String result = ""; - public void testCallbacks() { - Function fn1 = new Function() { - public Object f(Object...arguments) { - String s = " f1:"; - for (Object o: arguments){ - s += " " + o; - } - result += s; - return false; - } - }; - - Callback fn2 = new Callback() { - public boolean f(Object... objects) { - String s = " f2:"; - for (Object o: objects){ - s += " " + o; - } - result += s; - return false; - } - }; - - com.google.gwt.core.client.Callback fn3 = new com.google.gwt.core.client.Callback() { - public void onFailure(Object reason) { - result += " f3_fail: " + reason; - } - public void onSuccess(Object objects) { - String s = " f3_success:"; - for (Object o: (Object[])objects){ - s += " " + o; - } - result += s; - } - }; - - result = ""; - Callbacks callbacks = new Callbacks(); - callbacks.add( fn1 ); - callbacks.fire( "foo" ); - assertEquals(" f1: foo", result); - - result = ""; - callbacks.add( fn2 ); - callbacks.fire( "bar" ); - assertEquals(" f1: bar f2: bar", result); - - result = ""; - callbacks.remove( fn2 ); - callbacks.fire( "foobar" ); - assertEquals(" f1: foobar", result); - - result = ""; - callbacks.add( fn1 ); - callbacks.fire( "foo" ); - assertEquals(" f1: foo f1: foo", result); - - result = ""; - callbacks = new Callbacks("unique"); - callbacks.add( fn1 ); - callbacks.add( fn1 ); - callbacks.fire( "foo" ); - assertEquals(" f1: foo", result); - - result = ""; - callbacks.add( fn3 ); - callbacks.fire( "bar" ); - assertEquals(" f1: bar f3_success: bar", result); - - result = ""; - callbacks = new Callbacks("memory"); - callbacks.add( fn1 ); - callbacks.fire( "foo" ); - callbacks.add( fn2 ); - callbacks.fire( "bar" ); - callbacks.remove(fn2); - callbacks.fire( "foobar" ); - assertEquals(" f1: foo f2: foo f1: bar f2: bar f1: foobar", result); - - result = ""; - callbacks = new Callbacks("stopOnFalse"); - callbacks.add( fn1 ); - callbacks.add( fn2 ); - callbacks.fire( "bar" ); - assertEquals(" f1: bar", result); - - result = ""; - callbacks.disable(); - callbacks.fire( "bar" ); - assertEquals("", result); - - result = ""; - callbacks = new Callbacks("memory once unique"); - callbacks.add( fn1 ); - callbacks.add( fn1 ); - callbacks.fire( "bar" ); - assertEquals(" f1: bar", result); - callbacks.fire( "foo" ); - assertEquals(" f1: bar", result); - callbacks.add( fn2 ); - callbacks.add( fn2 ); - assertEquals(" f1: bar f2: bar f2: bar", result); - callbacks.remove( fn1 ); - callbacks.add( fn1 ); - assertEquals(" f1: bar f2: bar f2: bar f1: bar", result); - callbacks.remove( fn1 ); - callbacks.disable(); - callbacks.add( fn1 ); - assertEquals(" f1: bar f2: bar f2: bar f1: bar", result); - } - public void testDeferredAjaxWhenDone() { String url = "https://www.googleapis.com/blogger/v2/blogs/user_id/posts/post_id?callback=?&key=NO-KEY"; @@ -286,26 +171,6 @@ public class GQueryDeferredTestGwt extends GWTTestCase { }); } - public void testThen() { - new PromiseFunction() { - public void f(final Deferred dfd) { - dfd.resolve(5); - } - }.done(new Function() { - public void f() { - assertEquals(5d, arguments(0)); - } - }).then(new Function() { - public Object f(Object... args) { - return (Double)args[0] * 2; - } - }).done(new Function() { - public void f() { - assertEquals(10d, arguments(0)); - } - }); - } - public void testDeferredAjaxThenDone() { final String url = "https://www.googleapis.com/blogger/v2/blogs/user_id/posts/post_id?callback=?&key=NO-KEY"; @@ -328,46 +193,6 @@ public class GQueryDeferredTestGwt extends GWTTestCase { }); } - public void testDeferredAjaxThenFail() { - delayTestFinish(5000); - GQuery - .when(new PromiseFunction() { - public void f(Deferred dfd) { - dfd.resolve("message"); - } - }) - .then(new Function() { - public Object f(Object... args) { - return new PromiseFunction() { - public void f(Deferred dfd) { - dfd.resolve(arguments); - } - }; - } - }) - .then(new Function() { - public Object f(Object... args) { - return new PromiseFunction() { - public void f(Deferred dfd) { - dfd.reject(arguments); - } - }; - } - }) - .done(new Function() { - public void f() { - finishTest(); - fail(); - } - }) - .fail(new Function() { - public void f() { - assertEquals("message", arguments(0)); - finishTest(); - } - }); - } - public void testDeferredQueueDelay() { final int delay = 300; final double init = Duration.currentTimeMillis(); diff --git a/gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryGwtSuiteTest.java b/gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryGwtSuiteTest.java index 4093efae..eeecd7a1 100644 --- a/gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryGwtSuiteTest.java +++ b/gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryGwtSuiteTest.java @@ -3,6 +3,7 @@ package com.google.gwt.query.client; import junit.framework.Test; import com.google.gwt.junit.tools.GWTTestSuite; +import com.google.gwt.query.client.deferred.DeferredTestGwt; import com.google.gwt.query.client.impl.SelectorEnginesTestGwt; /** @@ -16,6 +17,7 @@ public class GQueryGwtSuiteTest extends GWTTestSuite GWTTestSuite suite = new GWTTestSuite( "GQuery Suite" ); suite.addTestSuite(GQueryAjaxTestGwt.class); suite.addTestSuite(GQueryDeferredTestGwt.class); + suite.addTestSuite(DeferredTestGwt.class); suite.addTestSuite(GQuerySelectorsTestGwt.class); suite.addTestSuite(GQueryCoreTestGwt.class); suite.addTestSuite(GQueryCssTestGwt.class); @@ -26,6 +28,4 @@ public class GQueryGwtSuiteTest extends GWTTestSuite suite.addTestSuite(SelectorEnginesTestGwt.class); return suite; } - - } -- 2.39.5