diff options
author | Johan Sørensen <johan@johansorensen.com> | 2009-10-19 12:45:27 +0200 |
---|---|---|
committer | Johan Sørensen <johan@johansorensen.com> | 2009-11-04 15:23:30 +0100 |
commit | 5bc19b8bedd92bff49bfbf55fec92af456e23fa1 (patch) | |
tree | b9d2853a38a899ff74fee753285c982ed57a9016 /test/javascripts | |
parent | ef5dd29ec04d1036303ff8d80ac9c0e70d5f923d (diff) | |
download | gitorious-mainline-outdated-5bc19b8bedd92bff49bfbf55fec92af456e23fa1.zip gitorious-mainline-outdated-5bc19b8bedd92bff49bfbf55fec92af456e23fa1.tar.gz gitorious-mainline-outdated-5bc19b8bedd92bff49bfbf55fec92af456e23fa1.tar.bz2 |
Add a NotificationCenter object to de-couple some of the javascript via observers
While multiple NotificationCenters are supported, the
NotificationCenter.defaultCenter() method is provided for a common
default shared notification center.
Diffstat (limited to 'test/javascripts')
-rw-r--r-- | test/javascripts/notification_center_test.js | 86 |
1 files changed, 86 insertions, 0 deletions
diff --git a/test/javascripts/notification_center_test.js b/test/javascripts/notification_center_test.js new file mode 100644 index 0000000..1fbb5e7 --- /dev/null +++ b/test/javascripts/notification_center_test.js @@ -0,0 +1,86 @@ +/* +#-- +# Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies) +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. +#-- +*/ + +NotificationCenterTest = TestCase("Notification Center", { + testShouldHaveADefaultNotificationCenter: function() { + var nc = NotificationCenter.defaultCenter(); + assertNotNull(nc); + assertEquals("default notification center", nc.name); + assertEquals(nc.name, NotificationCenter.defaultCenter().name); + }, + + testShouldAddAnObserver: function() { + var nc = NotificationCenter.defaultCenter(); + var observingObject = { callback: function(){ "callback ran" } }; + var sendingObject = "sender"; + nc.addObserver("someIdentifier", observingObject, + observingObject.callback, sendingObject); + assertEquals(sendingObject, nc.observers["someIdentifier"][0].sender); + }, + + testShouldNotifyAnObserver: function() { + var nc = NotificationCenter.defaultCenter(); + var callbackResult = null; + var receiver = { callback: function(){ callbackResult = "callback ran"; } }; + var SendingObject = function() { + var self = this; + this.notify = function() { + return NotificationCenter.defaultCenter().notifyObservers("aTest", self); + } + }; + var sender = new SendingObject(); + nc.addObserver("aTest", receiver, receiver.callback, sender); + assertTrue(sender.notify()); + assertEquals("callback ran", callbackResult); + }, + + testShouldSendArgumentsToCallback: function() { + var nc = NotificationCenter.defaultCenter(); + var tally = 0; + var receiver = { + incrementByCallback: function(sender, amount) { + tally += amount + } + }; + nc.addObserver("incrementor", receiver, receiver.incrementByCallback, this, 3); + nc.notifyObservers("incrementor", this); + assertEquals(3, tally); + }, + + testShouldNotifyAllObservers: function() { + var nc = NotificationCenter.defaultCenter(); + var callbacksRan = []; + var receiver = { + callback: function(sender, id) { callbacksRan.push(id) } + }; + nc.addObserver("foo", receiver, receiver.callback, this, 1); + nc.addObserver("foo", receiver, receiver.callback, this, 2); + nc.addObserver("foo", receiver, receiver.callback, this, 3); + nc.notifyObservers("foo", this); + assertEquals([1,2,3], callbacksRan); + }, + + testShouldRemoveAnObserver: function() { + var nc = NotificationCenter.defaultCenter(); + nc.addObserver("foo", this, function(){}, this); + assertNotSame("undefined", typeof(nc.observers["foo"])); + nc.removeObservers("foo"); + assertSame("undefined", typeof(nc.observers["foo"])); + } +});
\ No newline at end of file |