Revision: 8858
Author: [email protected]
Date: Thu Sep 23 13:21:05 2010
Log: Adds test that events can be registered and that they fire correctly.

This is a followup to ROO-1434 that implements a TODO to test the events.

Review at http://gwt-code-reviews.appspot.com/878803

Review by: [email protected]
http://code.google.com/p/google-web-toolkit/source/detail?r=8858

Modified:
/trunk/user/test/com/google/gwt/requestfactory/client/RequestFactoryStringTest.java /trunk/user/test/com/google/gwt/requestfactory/client/RequestFactoryTest.java /trunk/user/test/com/google/gwt/requestfactory/client/RequestFactoryTestBase.java

=======================================
--- /trunk/user/test/com/google/gwt/requestfactory/client/RequestFactoryStringTest.java Wed Sep 22 07:25:43 2010 +++ /trunk/user/test/com/google/gwt/requestfactory/client/RequestFactoryStringTest.java Thu Sep 23 13:21:05 2010
@@ -16,6 +16,7 @@
 package com.google.gwt.requestfactory.client;

 import com.google.gwt.requestfactory.client.impl.ProxyImpl;
+import com.google.gwt.requestfactory.shared.EntityProxyChange;
 import com.google.gwt.requestfactory.shared.Receiver;
 import com.google.gwt.requestfactory.shared.Request;
 import com.google.gwt.requestfactory.shared.ServerFailure;
@@ -106,6 +107,11 @@
   public void testDummyCreate() {
     delayTestFinish(5000);

+    final SimpleFooEventHandler<SimpleFooStringProxy> handler =
+      new SimpleFooEventHandler<SimpleFooStringProxy>();
+    EntityProxyChange.registerForProxyType(
+        req.getEventBus(), SimpleFooStringProxy.class, handler);
+
final SimpleFooStringProxy foo = req.create(SimpleFooStringProxy.class);
     Object futureId = foo.getId();
     assertEquals(futureId, foo.getId());
@@ -120,6 +126,10 @@
         assertEquals(futureId, foo.getId());
         assertTrue(((ProxyImpl) foo).isFuture());

+        assertEquals(1, handler.acquireEventCount);
+        assertEquals(1, handler.createEventCount);
+        assertEquals(2, handler.totalEventCount);
+
         checkStableIdEquals(foo, returned);
         finishTestAndReset();
       }
@@ -229,11 +239,18 @@
   public void testMethodWithSideEffects() {
     delayTestFinish(5000);

+    final SimpleFooEventHandler<SimpleFooStringProxy> handler =
+      new SimpleFooEventHandler<SimpleFooStringProxy>();
+    EntityProxyChange.registerForProxyType(
+        req.getEventBus(), SimpleFooStringProxy.class, handler);
+
     req.simpleFooStringRequest().findSimpleFooStringById("999x").fire(
         new Receiver<SimpleFooStringProxy>() {

           @Override
           public void onSuccess(SimpleFooStringProxy newFoo) {
+            assertEquals(1, handler.acquireEventCount);
+            assertEquals(1, handler.totalEventCount);
final Request<Long> mutateRequest = req.simpleFooStringRequest().countSimpleFooWithUserNameSideEffect(
                 newFoo);
             newFoo = mutateRequest.edit(newFoo);
@@ -243,7 +260,9 @@
               public void onSuccess(Long response) {
                 assertCannotFire(mutateRequest);
                 assertEquals(new Long(1L), response);
-                // TODO: listen to create events also
+                assertEquals(1, handler.acquireEventCount);
+                assertEquals(1, handler.updateEventCount);
+                assertEquals(2, handler.totalEventCount);

                 // confirm that the instance method did have the desired
                 // sideEffect.
@@ -252,6 +271,9 @@
                       @Override
public void onSuccess(SimpleFooStringProxy finalFoo) {
                         assertEquals("Ray", finalFoo.getUserName());
+                        assertEquals(1, handler.acquireEventCount);
+                        assertEquals(2, handler.updateEventCount);
+                        assertEquals(3, handler.totalEventCount);
                         finishTestAndReset();
                       }
                     });
=======================================
--- /trunk/user/test/com/google/gwt/requestfactory/client/RequestFactoryTest.java Thu Sep 23 05:32:15 2010 +++ /trunk/user/test/com/google/gwt/requestfactory/client/RequestFactoryTest.java Thu Sep 23 13:21:05 2010
@@ -17,6 +17,7 @@

 import com.google.gwt.requestfactory.client.impl.ProxyImpl;
 import com.google.gwt.requestfactory.shared.EntityProxy;
+import com.google.gwt.requestfactory.shared.EntityProxyChange;
 import com.google.gwt.requestfactory.shared.EntityProxyId;
 import com.google.gwt.requestfactory.shared.Receiver;
 import com.google.gwt.requestfactory.shared.Request;
@@ -136,6 +137,11 @@
   public void  testDummyCreate() {
     delayTestFinish(5000);

+    final SimpleFooEventHandler<SimpleFooProxy> handler =
+      new SimpleFooEventHandler<SimpleFooProxy>();
+    EntityProxyChange.registerForProxyType(
+        req.getEventBus(), SimpleFooProxy.class, handler);
+
     final SimpleFooProxy foo = req.create(SimpleFooProxy.class);
     Object futureId = foo.getId();
     assertEquals(futureId, foo.getId());
@@ -150,6 +156,10 @@
         assertEquals(futureId, foo.getId());
         assertTrue(((ProxyImpl) foo).isFuture());

+        assertEquals(1, handler.acquireEventCount);
+        assertEquals(1, handler.createEventCount);
+        assertEquals(2, handler.totalEventCount);
+
         checkStableIdEquals(foo, returned);
         finishTestAndReset();
       }
@@ -326,11 +336,19 @@
    */
   public void testMethodWithSideEffects() {
     delayTestFinish(5000);
+
+    final SimpleFooEventHandler<SimpleFooProxy> handler =
+      new SimpleFooEventHandler<SimpleFooProxy>();
+    EntityProxyChange.registerForProxyType(
+        req.getEventBus(), SimpleFooProxy.class, handler);
+
     req.simpleFooRequest().findSimpleFooById(999L).fire(
         new Receiver<SimpleFooProxy>() {

           @Override
           public void onSuccess(SimpleFooProxy newFoo) {
+            assertEquals(1, handler.acquireEventCount);
+            assertEquals(1, handler.totalEventCount);
final Request<Long> mutateRequest = req.simpleFooRequest().countSimpleFooWithUserNameSideEffect(
                 newFoo);
             newFoo = mutateRequest.edit(newFoo);
@@ -340,7 +358,9 @@
               public void onSuccess(Long response) {
                 assertCannotFire(mutateRequest);
                 assertEquals(new Long(1L), response);
-                // TODO: listen to create events also
+                assertEquals(1, handler.acquireEventCount);
+                assertEquals(1, handler.updateEventCount);
+                assertEquals(2, handler.totalEventCount);

                 // confirm that the instance method did have the desired
                 // sideEffect.
@@ -349,6 +369,9 @@
                       @Override
                       public void onSuccess(SimpleFooProxy finalFoo) {
                         assertEquals("Ray", finalFoo.getUserName());
+                        assertEquals(1, handler.acquireEventCount);
+                        assertEquals(2, handler.updateEventCount);
+                        assertEquals(3, handler.totalEventCount);
                         finishTestAndReset();
                       }
                     });
=======================================
--- /trunk/user/test/com/google/gwt/requestfactory/client/RequestFactoryTestBase.java Wed Sep 22 07:25:43 2010 +++ /trunk/user/test/com/google/gwt/requestfactory/client/RequestFactoryTestBase.java Thu Sep 23 13:21:05 2010
@@ -20,6 +20,7 @@
 import com.google.gwt.event.shared.SimpleEventBus;
 import com.google.gwt.junit.client.GWTTestCase;
 import com.google.gwt.requestfactory.shared.EntityProxy;
+import com.google.gwt.requestfactory.shared.EntityProxyChange;
 import com.google.gwt.requestfactory.shared.Receiver;
 import com.google.gwt.requestfactory.shared.SimpleRequestFactory;

@@ -33,6 +34,29 @@

   protected SimpleRequestFactory req;
   protected EventBus eventBus;
+
+  /**
+   *  Class for counting events.
+   */
+  protected class SimpleFooEventHandler<P extends EntityProxy>
+      implements EntityProxyChange.Handler<P> {
+    int acquireEventCount = 0;
+    int createEventCount = 0;
+    int deleteEventCount = 0;
+    int totalEventCount = 0;
+    int updateEventCount = 0;
+
+    public void onProxyChange(EntityProxyChange<P> event) {
+      totalEventCount++;
+      switch (event.getWriteOperation()) {
+        case ACQUIRE: acquireEventCount++; break;
+        case CREATE: createEventCount++; break;
+        case DELETE: deleteEventCount++; break;
+        case UPDATE: updateEventCount++; break;
+        default: break;
+      }
+    }
+  }

   @Override
   public void gwtSetUp() {

--
http://groups.google.com/group/Google-Web-Toolkit-Contributors

Reply via email to