gjs r55 - in trunk: . test/js



Author: jobi
Date: Fri Oct 24 15:16:58 2008
New Revision: 55
URL: http://svn.gnome.org/viewvc/gjs?rev=55&view=rev

Log:
Add testSignals.js

Added:
   trunk/test/js/testSignals.js
Modified:
   trunk/Makefile-test.am

Modified: trunk/Makefile-test.am
==============================================================================
--- trunk/Makefile-test.am	(original)
+++ trunk/Makefile-test.am	Fri Oct 24 15:16:58 2008
@@ -75,4 +75,5 @@
 EXTRA_DIST +=				\
 	test/js/modules/jsUnit.js	\
 	test/js/testMainloop.js		\
+	test/js/testSignals.js		\
 	test/js/testTweener.js

Added: trunk/test/js/testSignals.js
==============================================================================
--- (empty file)
+++ trunk/test/js/testSignals.js	Fri Oct 24 15:16:58 2008
@@ -0,0 +1,138 @@
+const Signals = imports.signals;
+
+function Foo() {
+    this._init();
+}
+
+Foo.prototype = {
+    _init : function() {
+    }
+};
+
+Signals.addSignalMethods(Foo.prototype);
+
+function testSimple() {
+    var foo = new Foo();
+    var id = foo.connect('bar',
+                         function(theFoo, a, b) {
+                             log("Received signal 'bar'");
+                             theFoo.a = a;
+                             theFoo.b = b;
+                         });
+    foo.emit('bar', "This is a", "This is b");
+    assertEquals("This is a", foo.a);
+    assertEquals("This is b", foo.b);
+    foo.disconnect(id);
+    // this emission should do nothing
+    foo.emit('bar', "Another a", "Another b");
+    // so these values should be unchanged
+    assertEquals("This is a", foo.a);
+    assertEquals("This is b", foo.b);
+}
+
+function testDisconnectDuringEmit() {
+    var foo = new Foo();
+    var toRemove = [];
+    var firstId = foo.connect('bar',
+                         function(theFoo) {
+                             log("Received signal 'bar', disconnecting others");
+                             theFoo.disconnect(toRemove[0]);
+                             theFoo.disconnect(toRemove[1]);
+                         });
+    var id = foo.connect('bar',
+                     function(theFoo) {
+                         throw new Error("This should not have been called 1");
+                     });
+    toRemove.push(id);
+
+    id = foo.connect('bar',
+                     function(theFoo) {
+                         throw new Error("This should not have been called 2");
+                     });
+    toRemove.push(id);
+
+    // emit signal; what should happen is that the second two handlers are
+    // disconnected before they get invoked
+    foo.emit('bar');
+
+    // clean up the last handler
+    foo.disconnect(firstId);
+
+    // poke in private implementation to sanity-check
+    assertEquals('no handlers left', 0, foo._signalConnections.length);
+}
+
+function testMultipleSignals() {
+    var foo = new Foo();
+
+    foo.barHandlersCalled = 0;
+    foo.bonkHandlersCalled = 0;
+    foo.connect('bar',
+                function(theFoo) {
+                    log("Received signal 'bar'");
+                    theFoo.barHandlersCalled += 1;
+                });
+    foo.connect('bonk',
+                function(theFoo) {
+                    log("Received signal 'bonk'");
+                    theFoo.bonkHandlersCalled += 1;
+                });
+    foo.connect('bar',
+                function(theFoo) {
+                    log("Received signal 'bar' (handler 2)");
+                    theFoo.barHandlersCalled += 1;
+                });
+    foo.emit('bar');
+
+    assertEquals(2, foo.barHandlersCalled);
+    assertEquals(0, foo.bonkHandlersCalled);
+
+    foo.emit('bonk');
+
+    assertEquals(2, foo.barHandlersCalled);
+    assertEquals(1, foo.bonkHandlersCalled);
+
+    foo.emit('bar');
+
+    assertEquals(4, foo.barHandlersCalled);
+    assertEquals(1, foo.bonkHandlersCalled);
+
+    foo.disconnectAll();
+
+    // these post-disconnect emissions should do nothing
+    foo.emit('bar');
+    foo.emit('bonk');
+
+    assertEquals(4, foo.barHandlersCalled);
+    assertEquals(1, foo.bonkHandlersCalled);
+}
+
+function testExceptionInCallback() {
+    let foo = new Foo();
+
+    foo.bar1Called = 0;
+    foo.bar2Called = 0;
+    foo.connect('bar',
+                function(theFoo) {
+                    log("Received signal 'bar' (throwing exception)");
+                    theFoo.bar1Called += 1;
+                    throw new Error("Exception we are throwing on purpose");
+                });
+    foo.connect('bar',
+                function(theFoo) {
+                    log("Received signal 'bar' (handler 2)");
+                    theFoo.bar2Called += 1;
+                });
+
+    // exception in callback does not effect other callbacks
+    foo.emit('bar');
+    assertEquals(1, foo.bar1Called);
+    assertEquals(1, foo.bar2Called);
+
+    // exception in callback does not disconnect the callback
+    foo.emit('bar');
+    assertEquals(2, foo.bar1Called);
+    assertEquals(2, foo.bar2Called);
+}
+
+gjstestRun();



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]