diff options
author | Manuel Carrasco Moñino <manuel.carrasco.m@gmail.com> | 2013-11-09 10:56:51 +0100 |
---|---|---|
committer | Manuel Carrasco Moñino <manuel.carrasco.m@gmail.com> | 2013-11-09 10:56:51 +0100 |
commit | f41e8b86ac4d1d3125f5a6247dfe0bad19dba0b7 (patch) | |
tree | 8bcb6aa48edb3b2872c084889ca052e69c3017fc /gwtquery-core/src/test/java/com | |
parent | e7b915e7516b43adb90871e1657ca91c6103ad3d (diff) | |
download | gwtquery-f41e8b86ac4d1d3125f5a6247dfe0bad19dba0b7.tar.gz gwtquery-f41e8b86ac4d1d3125f5a6247dfe0bad19dba0b7.zip |
Fix "then()" method so as it behaves like jQuery breaking the flow if previous fails. Add convenient method "or()" to continue the flow when we have different options to resolve an asynchronous process. Add "and()" method as an alias to "then()". "and()" and "or()" makes gQuery promises much more useful and declarative.
Diffstat (limited to 'gwtquery-core/src/test/java/com')
-rw-r--r-- | gwtquery-core/src/test/java/com/google/gwt/query/client/deferred/DeferredTest.java | 72 |
1 files changed, 68 insertions, 4 deletions
diff --git a/gwtquery-core/src/test/java/com/google/gwt/query/client/deferred/DeferredTest.java b/gwtquery-core/src/test/java/com/google/gwt/query/client/deferred/DeferredTest.java index 6e3da096..8537b34b 100644 --- a/gwtquery-core/src/test/java/com/google/gwt/query/client/deferred/DeferredTest.java +++ b/gwtquery-core/src/test/java/com/google/gwt/query/client/deferred/DeferredTest.java @@ -211,7 +211,7 @@ public class DeferredTest extends GWTTestCase { dfd.resolve("message"); } }) - .then(new FunctionDeferred() { + .and(new FunctionDeferred() { public void f(Deferred dfd) { dfd.resolve("then1 " + arguments[0]); } @@ -250,14 +250,24 @@ public class DeferredTest extends GWTTestCase { dfd.resolve("message"); } }) + .and(new Function(){ + public Object f(Object... data) { + return (arguments[0] + " then1"); + } + }) + .then(new Function(){ + public void f() { + // should return the previous value + } + }) .then(new FunctionDeferred() { public void f(Deferred dfd) { - dfd.resolve("then1 " + arguments[0]); + dfd.reject("then2 " + arguments[0]); } }) .then(new FunctionDeferred() { public void f(Deferred dfd) { - dfd.reject("then2 " + arguments[0]); + dfd.resolve("then3 " + arguments[0]); } }) .done(new Function() { @@ -268,7 +278,7 @@ public class DeferredTest extends GWTTestCase { }) .fail(new Function() { public void f() { - assertEquals("then2 then1 message", arguments(0)); + assertEquals("then2 message then1", arguments(0)); finishTest(); done = true; } @@ -278,6 +288,60 @@ public class DeferredTest extends GWTTestCase { assertTrue(done); } } + + public void testDeferredOr() { + done = false; + delayTestFinish(5000); + + GQuery + .when(new PromiseFunction() { + public void f(Deferred dfd) { + dfd.reject("reject-when"); + } + }) + .or(new FunctionDeferred() { + public void f(Deferred dfd) { + dfd.reject(arguments[0] + " reject-or1"); + } + }) + .or(new FunctionDeferred() { + public void f(Deferred dfd) { + dfd.reject(arguments[0] + " reject-or2"); + } + }) + .then(null, new FunctionDeferred() { + public void f(Deferred dfd) { + dfd.resolve(arguments[0] + " resolve-or3"); + } + }) + .or(new FunctionDeferred() { + public void f(Deferred dfd) { + dfd.resolve(arguments[0] + " or4"); + } + }) + .or(new FunctionDeferred() { + public void f(Deferred dfd) { + dfd.reject(arguments[0] + " or5"); + } + }) + .done(new Function() { + public void f() { + assertEquals("reject-when reject-or1 reject-or2 resolve-or3", arguments(0)); + finishTest(); + done = true; + } + }) + .fail(new Function() { + public void f() { + finishTest(); + fail(); + } + }); + + if (!GWT.isClient()) { + assertTrue(done); + } + } public void testProtected() { |