| 1 |
// Copyright 2014 The Closure Library Authors. All Rights Reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS-IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
goog.provide('goog.labs.pubsub.BroadcastPubSubTest'); |
| 16 |
goog.setTestOnly('goog.labs.pubsub.BroadcastPubSubTest'); |
| 17 |
|
| 18 |
|
| 19 |
goog.require('goog.array'); |
| 20 |
goog.require('goog.debug.Logger'); |
| 21 |
goog.require('goog.json'); |
| 22 |
goog.require('goog.labs.pubsub.BroadcastPubSub'); |
| 23 |
goog.require('goog.storage.Storage'); |
| 24 |
goog.require('goog.structs.Map'); |
| 25 |
goog.require('goog.testing.MockClock'); |
| 26 |
goog.require('goog.testing.MockControl'); |
| 27 |
goog.require('goog.testing.events'); |
| 28 |
goog.require('goog.testing.events.Event'); |
| 29 |
goog.require('goog.testing.jsunit'); |
| 30 |
goog.require('goog.testing.mockmatchers'); |
| 31 |
goog.require('goog.testing.mockmatchers.ArgumentMatcher'); |
| 32 |
goog.require('goog.testing.recordFunction'); |
| 33 |
goog.require('goog.userAgent'); |
| 34 |
|
| 35 |
|
| 36 |
/** @type {goog.labs.pubsub.BroadcastPubSub} */ |
| 37 |
var broadcastPubSub; |
| 38 |
|
| 39 |
|
| 40 |
/** @type {goog.testing.MockControl} */ |
| 41 |
var mockControl; |
| 42 |
|
| 43 |
|
| 44 |
/** @type {goog.testing.MockClock} */ |
| 45 |
var mockClock; |
| 46 |
|
| 47 |
|
| 48 |
/** @type {goog.testing.MockInterface} */ |
| 49 |
var mockStorage; |
| 50 |
|
| 51 |
|
| 52 |
/** @type {goog.testing.MockInterface} */ |
| 53 |
var mockStorageCtor; |
| 54 |
|
| 55 |
|
| 56 |
/** @type {goog.structs.Map} */ |
| 57 |
var mockHtml5LocalStorage; |
| 58 |
|
| 59 |
|
| 60 |
/** @type {goog.testing.MockInterface} */ |
| 61 |
var mockHTML5LocalStorageCtor; |
| 62 |
|
| 63 |
|
| 64 |
/** @const {boolean} */ |
| 65 |
var isIe8 = goog.userAgent.IE && goog.userAgent.DOCUMENT_MODE == 8; |
| 66 |
|
| 67 |
|
| 68 |
/** |
| 69 |
* Sends a remote storage event with special handling for IE8. With IE8 an |
| 70 |
* event is pushed to the event queue stored in local storage as a result of |
| 71 |
* behaviour by the mockHtml5LocalStorage instanciated when using IE8 an event |
| 72 |
* is automatically generated in the local browser context. For other browsers |
| 73 |
* this simply creates a new browser event. |
| 74 |
* @param {{'args': !Array<string>, 'timestamp': number}} data Value stored |
| 75 |
* in localStorage which generated the remote event. |
| 76 |
*/ |
| 77 |
function remoteStorageEvent(data) { |
| 78 |
if (!isIe8) { |
| 79 |
var event = new goog.testing.events.Event('storage', window); |
| 80 |
event.key = goog.labs.pubsub.BroadcastPubSub.STORAGE_KEY_; |
| 81 |
event.newValue = goog.json.serialize(data); |
| 82 |
goog.testing.events.fireBrowserEvent(event); |
| 83 |
} else { |
| 84 |
var uniqueKey = |
| 85 |
goog.labs.pubsub.BroadcastPubSub.IE8_EVENTS_KEY_PREFIX_ + '1234567890'; |
| 86 |
var ie8Events = mockHtml5LocalStorage.get(uniqueKey); |
| 87 |
if (goog.isDefAndNotNull(ie8Events)) { |
| 88 |
ie8Events = JSON.parse(ie8Events); |
| 89 |
// Events should never overlap in IE8 mode. |
| 90 |
if (ie8Events.length > 0 && |
| 91 |
ie8Events[ie8Events.length - 1]['timestamp'] >= data['timestamp']) { |
| 92 |
data['timestamp'] = ie8Events[ie8Events.length - 1]['timestamp'] + |
| 93 |
goog.labs.pubsub.BroadcastPubSub.IE8_TIMESTAMP_UNIQUE_OFFSET_MS_; |
| 94 |
} |
| 95 |
} else { |
| 96 |
ie8Events = []; |
| 97 |
} |
| 98 |
ie8Events.push(data); |
| 99 |
// This will cause an event. |
| 100 |
mockHtml5LocalStorage.set(uniqueKey, goog.json.serialize(ie8Events)); |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
function setUp() { |
| 105 |
mockControl = new goog.testing.MockControl(); |
| 106 |
|
| 107 |
mockClock = new goog.testing.MockClock(true); |
| 108 |
// Time should never be 0... |
| 109 |
mockClock.tick(); |
| 110 |
/** @suppress {missingRequire} */ |
| 111 |
mockHTML5LocalStorageCtor = mockControl.createConstructorMock( |
| 112 |
goog.storage.mechanism, 'HTML5LocalStorage'); |
| 113 |
|
| 114 |
mockHtml5LocalStorage = new goog.structs.Map(); |
| 115 |
|
| 116 |
// The builtin localStorage returns null instead of undefined. |
| 117 |
var originalGetFn = |
| 118 |
goog.bind(mockHtml5LocalStorage.get, mockHtml5LocalStorage); |
| 119 |
mockHtml5LocalStorage.get = function(key) { |
| 120 |
var value = originalGetFn(key); |
| 121 |
if (!goog.isDef(value)) { |
| 122 |
return null; |
| 123 |
} |
| 124 |
return value; |
| 125 |
}; |
| 126 |
mockHtml5LocalStorage.key = function(idx) { |
| 127 |
return mockHtml5LocalStorage.getKeys()[idx]; |
| 128 |
}; |
| 129 |
mockHtml5LocalStorage.isAvailable = function() { return true; }; |
| 130 |
|
| 131 |
|
| 132 |
// IE has problems. IE9+ still dispatches storage events locally. IE8 also |
| 133 |
// doesn't include the key/value information. So for IE, every time we get a |
| 134 |
// "set" on localStorage we simulate for the appropriate browser. |
| 135 |
if (goog.userAgent.IE) { |
| 136 |
var target = isIe8 ? document : window; |
| 137 |
var originalSetFn = |
| 138 |
goog.bind(mockHtml5LocalStorage.set, mockHtml5LocalStorage); |
| 139 |
mockHtml5LocalStorage.set = function(key, value) { |
| 140 |
originalSetFn(key, value); |
| 141 |
var event = new goog.testing.events.Event('storage', target); |
| 142 |
if (!isIe8) { |
| 143 |
event.key = key; |
| 144 |
event.newValue = value; |
| 145 |
} |
| 146 |
goog.testing.events.fireBrowserEvent(event); |
| 147 |
}; |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
function tearDown() { |
| 152 |
mockControl.$tearDown(); |
| 153 |
mockClock.dispose(); |
| 154 |
broadcastPubSub = undefined; |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
function testConstructor() { |
| 159 |
mockHTML5LocalStorageCtor().$returns(mockHtml5LocalStorage); |
| 160 |
mockControl.$replayAll(); |
| 161 |
broadcastPubSub = new goog.labs.pubsub.BroadcastPubSub(); |
| 162 |
broadcastPubSub.logger_.setLevel(goog.debug.Logger.Level.OFF); |
| 163 |
assertNotNullNorUndefined( |
| 164 |
'BroadcastChannel instance must not be null', broadcastPubSub); |
| 165 |
assertTrue( |
| 166 |
'BroadcastChannel instance must have the expected type', |
| 167 |
broadcastPubSub instanceof goog.labs.pubsub.BroadcastPubSub); |
| 168 |
assertArrayEquals( |
| 169 |
goog.labs.pubsub.BroadcastPubSub.instances_, [broadcastPubSub]); |
| 170 |
broadcastPubSub.dispose(); |
| 171 |
mockControl.$verifyAll(); |
| 172 |
assertNotNullNorUndefined( |
| 173 |
'Storage should not be undefined or null in broadcastPubSub.', |
| 174 |
broadcastPubSub.storage_); |
| 175 |
assertArrayEquals(goog.labs.pubsub.BroadcastPubSub.instances_, []); |
| 176 |
} |
| 177 |
|
| 178 |
|
| 179 |
function testConstructor_noLocalStorage() { |
| 180 |
mockHTML5LocalStorageCtor().$returns( |
| 181 |
{isAvailable: function() { return false; }}); |
| 182 |
mockControl.$replayAll(); |
| 183 |
broadcastPubSub = new goog.labs.pubsub.BroadcastPubSub(); |
| 184 |
broadcastPubSub.logger_.setLevel(goog.debug.Logger.Level.OFF); |
| 185 |
assertNotNullNorUndefined( |
| 186 |
'BroadcastChannel instance must not be null', broadcastPubSub); |
| 187 |
assertTrue( |
| 188 |
'BroadcastChannel instance must have the expected type', |
| 189 |
broadcastPubSub instanceof goog.labs.pubsub.BroadcastPubSub); |
| 190 |
assertArrayEquals( |
| 191 |
goog.labs.pubsub.BroadcastPubSub.instances_, [broadcastPubSub]); |
| 192 |
broadcastPubSub.dispose(); |
| 193 |
mockControl.$verifyAll(); |
| 194 |
assertNull( |
| 195 |
'Storage should be null in broadcastPubSub.', broadcastPubSub.storage_); |
| 196 |
assertArrayEquals(goog.labs.pubsub.BroadcastPubSub.instances_, []); |
| 197 |
} |
| 198 |
|
| 199 |
|
| 200 |
/** Verify we cleanup after ourselves. */ |
| 201 |
function testDispose() { |
| 202 |
mockHTML5LocalStorageCtor().$returns(mockHtml5LocalStorage); |
| 203 |
mockHTML5LocalStorageCtor().$returns(mockHtml5LocalStorage); |
| 204 |
var mockStorage = mockControl.createLooseMock(goog.storage.Storage); |
| 205 |
|
| 206 |
var mockStorageCtor = |
| 207 |
mockControl.createConstructorMock(goog.storage, 'Storage'); |
| 208 |
|
| 209 |
mockStorageCtor(mockHtml5LocalStorage).$returns(mockStorage); |
| 210 |
mockStorageCtor(mockHtml5LocalStorage).$returns(mockStorage); |
| 211 |
|
| 212 |
if (isIe8) { |
| 213 |
mockStorage.remove(goog.labs.pubsub.BroadcastPubSub.IE8_EVENTS_KEY_); |
| 214 |
} |
| 215 |
|
| 216 |
mockControl.$replayAll(); |
| 217 |
broadcastPubSub = new goog.labs.pubsub.BroadcastPubSub(); |
| 218 |
broadcastPubSub.logger_.setLevel(goog.debug.Logger.Level.OFF); |
| 219 |
|
| 220 |
var broadcastPubSubExtra = new goog.labs.pubsub.BroadcastPubSub(); |
| 221 |
assertArrayEquals( |
| 222 |
goog.labs.pubsub.BroadcastPubSub.instances_, |
| 223 |
[broadcastPubSub, broadcastPubSubExtra]); |
| 224 |
|
| 225 |
assertFalse( |
| 226 |
'BroadcastChannel extra instance must not have been disposed of', |
| 227 |
broadcastPubSubExtra.isDisposed()); |
| 228 |
broadcastPubSubExtra.dispose(); |
| 229 |
assertTrue( |
| 230 |
'BroadcastChannel extra instance must have been disposed of', |
| 231 |
broadcastPubSubExtra.isDisposed()); |
| 232 |
assertFalse( |
| 233 |
'BroadcastChannel instance must not have been disposed of', |
| 234 |
broadcastPubSub.isDisposed()); |
| 235 |
|
| 236 |
assertArrayEquals( |
| 237 |
goog.labs.pubsub.BroadcastPubSub.instances_, [broadcastPubSub]); |
| 238 |
assertFalse( |
| 239 |
'BroadcastChannel instance must not have been disposed of', |
| 240 |
broadcastPubSub.isDisposed()); |
| 241 |
broadcastPubSub.dispose(); |
| 242 |
assertTrue( |
| 243 |
'BroadcastChannel instance must have been disposed of', |
| 244 |
broadcastPubSub.isDisposed()); |
| 245 |
assertArrayEquals(goog.labs.pubsub.BroadcastPubSub.instances_, []); |
| 246 |
mockControl.$verifyAll(); |
| 247 |
} |
| 248 |
|
| 249 |
|
| 250 |
/** |
| 251 |
* Tests related to remote events that an instance of BroadcastChannel |
| 252 |
* should handle. |
| 253 |
*/ |
| 254 |
function testHandleRemoteEvent() { |
| 255 |
mockHTML5LocalStorageCtor().$returns(mockHtml5LocalStorage); |
| 256 |
var foo = mockControl.createFunctionMock(); |
| 257 |
foo('x', 'y').$times(2); |
| 258 |
|
| 259 |
var context = {'foo': 'bar'}; |
| 260 |
var bar = goog.testing.recordFunction(); |
| 261 |
|
| 262 |
mockControl.$replayAll(); |
| 263 |
broadcastPubSub = new goog.labs.pubsub.BroadcastPubSub(); |
| 264 |
broadcastPubSub.logger_.setLevel(goog.debug.Logger.Level.OFF); |
| 265 |
var eventData = {'args': ['someTopic', 'x', 'y'], 'timestamp': goog.now()}; |
| 266 |
|
| 267 |
broadcastPubSub.subscribe('someTopic', foo); |
| 268 |
broadcastPubSub.subscribe('someTopic', bar, context); |
| 269 |
|
| 270 |
remoteStorageEvent(eventData); |
| 271 |
mockClock.tick(); |
| 272 |
|
| 273 |
assertEquals(1, bar.getCallCount()); |
| 274 |
assertEquals(context, bar.getLastCall().getThis()); |
| 275 |
assertArrayEquals(['x', 'y'], bar.getLastCall().getArguments()); |
| 276 |
|
| 277 |
broadcastPubSub.unsubscribe('someTopic', foo); |
| 278 |
eventData['timestamp'] = goog.now(); |
| 279 |
remoteStorageEvent(eventData); |
| 280 |
mockClock.tick(); |
| 281 |
|
| 282 |
assertEquals(2, bar.getCallCount()); |
| 283 |
assertEquals(context, bar.getLastCall().getThis()); |
| 284 |
assertArrayEquals(['x', 'y'], bar.getLastCall().getArguments()); |
| 285 |
|
| 286 |
broadcastPubSub.subscribe('someTopic', foo); |
| 287 |
broadcastPubSub.unsubscribe('someTopic', bar, context); |
| 288 |
eventData['timestamp'] = goog.now(); |
| 289 |
remoteStorageEvent(eventData); |
| 290 |
mockClock.tick(); |
| 291 |
|
| 292 |
assertEquals(2, bar.getCallCount()); |
| 293 |
broadcastPubSub.dispose(); |
| 294 |
mockControl.$verifyAll(); |
| 295 |
} |
| 296 |
|
| 297 |
|
| 298 |
function testHandleRemoteEventSubscribeOnce() { |
| 299 |
mockHTML5LocalStorageCtor().$returns(mockHtml5LocalStorage); |
| 300 |
var foo = mockControl.createFunctionMock(); |
| 301 |
foo('x', 'y'); |
| 302 |
|
| 303 |
mockControl.$replayAll(); |
| 304 |
broadcastPubSub = new goog.labs.pubsub.BroadcastPubSub(); |
| 305 |
broadcastPubSub.logger_.setLevel(goog.debug.Logger.Level.OFF); |
| 306 |
|
| 307 |
broadcastPubSub.subscribeOnce('someTopic', foo); |
| 308 |
assertEquals( |
| 309 |
'BroadcastChannel must have one subscriber', 1, |
| 310 |
broadcastPubSub.getCount()); |
| 311 |
|
| 312 |
remoteStorageEvent( |
| 313 |
{'args': ['someTopic', 'x', 'y'], 'timestamp': goog.now()}); |
| 314 |
mockClock.tick(); |
| 315 |
|
| 316 |
assertEquals( |
| 317 |
'BroadcastChannel must have no subscribers after receiving the event', 0, |
| 318 |
broadcastPubSub.getCount()); |
| 319 |
broadcastPubSub.dispose(); |
| 320 |
mockControl.$verifyAll(); |
| 321 |
} |
| 322 |
|
| 323 |
|
| 324 |
function testHandleQueuedRemoteEvents() { |
| 325 |
mockHTML5LocalStorageCtor().$returns(mockHtml5LocalStorage); |
| 326 |
var foo = mockControl.createFunctionMock(); |
| 327 |
var bar = mockControl.createFunctionMock(); |
| 328 |
|
| 329 |
foo('x', 'y'); |
| 330 |
bar('d', 'c'); |
| 331 |
|
| 332 |
mockControl.$replayAll(); |
| 333 |
|
| 334 |
broadcastPubSub = new goog.labs.pubsub.BroadcastPubSub(); |
| 335 |
broadcastPubSub.logger_.setLevel(goog.debug.Logger.Level.OFF); |
| 336 |
broadcastPubSub.subscribe('fooTopic', foo); |
| 337 |
broadcastPubSub.subscribe('barTopic', bar); |
| 338 |
|
| 339 |
var eventData = {'args': ['fooTopic', 'x', 'y'], 'timestamp': goog.now()}; |
| 340 |
remoteStorageEvent(eventData); |
| 341 |
|
| 342 |
eventData = {'args': ['barTopic', 'd', 'c'], 'timestamp': goog.now()}; |
| 343 |
remoteStorageEvent(eventData); |
| 344 |
mockClock.tick(); |
| 345 |
|
| 346 |
broadcastPubSub.dispose(); |
| 347 |
mockControl.$verifyAll(); |
| 348 |
} |
| 349 |
|
| 350 |
|
| 351 |
function testHandleRemoteEventsUnsubscribe() { |
| 352 |
mockHTML5LocalStorageCtor().$returns(mockHtml5LocalStorage); |
| 353 |
var foo = mockControl.createFunctionMock(); |
| 354 |
var bar = mockControl.createFunctionMock(); |
| 355 |
|
| 356 |
foo('x', 'y').$does(function() { |
| 357 |
broadcastPubSub.unsubscribe('barTopic', bar); |
| 358 |
}); |
| 359 |
|
| 360 |
mockControl.$replayAll(); |
| 361 |
|
| 362 |
broadcastPubSub = new goog.labs.pubsub.BroadcastPubSub(); |
| 363 |
broadcastPubSub.logger_.setLevel(goog.debug.Logger.Level.OFF); |
| 364 |
broadcastPubSub.subscribe('fooTopic', foo); |
| 365 |
broadcastPubSub.subscribe('barTopic', bar); |
| 366 |
|
| 367 |
var eventData = {'args': ['fooTopic', 'x', 'y'], 'timestamp': goog.now()}; |
| 368 |
remoteStorageEvent(eventData); |
| 369 |
mockClock.tick(); |
| 370 |
|
| 371 |
eventData = {'args': ['barTopic', 'd', 'c'], 'timestamp': goog.now()}; |
| 372 |
remoteStorageEvent(eventData); |
| 373 |
mockClock.tick(); |
| 374 |
|
| 375 |
broadcastPubSub.dispose(); |
| 376 |
mockControl.$verifyAll(); |
| 377 |
} |
| 378 |
|
| 379 |
|
| 380 |
function testHandleRemoteEventsCalledOnce() { |
| 381 |
mockHTML5LocalStorageCtor().$returns(mockHtml5LocalStorage); |
| 382 |
var foo = mockControl.createFunctionMock(); |
| 383 |
foo('x', 'y'); |
| 384 |
|
| 385 |
mockControl.$replayAll(); |
| 386 |
|
| 387 |
broadcastPubSub = new goog.labs.pubsub.BroadcastPubSub(); |
| 388 |
broadcastPubSub.logger_.setLevel(goog.debug.Logger.Level.OFF); |
| 389 |
broadcastPubSub.subscribeOnce('someTopic', foo); |
| 390 |
|
| 391 |
var eventData = {'args': ['someTopic', 'x', 'y'], 'timestamp': goog.now()}; |
| 392 |
remoteStorageEvent(eventData); |
| 393 |
mockClock.tick(); |
| 394 |
|
| 395 |
eventData = {'args': ['someTopic', 'x', 'y'], 'timestamp': goog.now()}; |
| 396 |
remoteStorageEvent(eventData); |
| 397 |
mockClock.tick(); |
| 398 |
|
| 399 |
broadcastPubSub.dispose(); |
| 400 |
mockControl.$verifyAll(); |
| 401 |
} |
| 402 |
|
| 403 |
|
| 404 |
function testHandleRemoteEventNestedPublish() { |
| 405 |
mockHTML5LocalStorageCtor().$returns(mockHtml5LocalStorage); |
| 406 |
var foo1 = mockControl.createFunctionMock(); |
| 407 |
foo1().$does(function() { |
| 408 |
remoteStorageEvent({'args': ['bar'], 'timestamp': goog.now()}); |
| 409 |
}); |
| 410 |
var foo2 = mockControl.createFunctionMock(); |
| 411 |
foo2(); |
| 412 |
var bar1 = mockControl.createFunctionMock(); |
| 413 |
bar1().$does(function() { broadcastPubSub.publish('baz'); }); |
| 414 |
var bar2 = mockControl.createFunctionMock(); |
| 415 |
bar2(); |
| 416 |
var baz1 = mockControl.createFunctionMock(); |
| 417 |
baz1(); |
| 418 |
var baz2 = mockControl.createFunctionMock(); |
| 419 |
baz2(); |
| 420 |
|
| 421 |
mockControl.$replayAll(); |
| 422 |
broadcastPubSub = new goog.labs.pubsub.BroadcastPubSub(); |
| 423 |
broadcastPubSub.logger_.setLevel(goog.debug.Logger.Level.OFF); |
| 424 |
|
| 425 |
broadcastPubSub.subscribe('foo', foo1); |
| 426 |
broadcastPubSub.subscribe('foo', foo2); |
| 427 |
broadcastPubSub.subscribe('bar', bar1); |
| 428 |
broadcastPubSub.subscribe('bar', bar2); |
| 429 |
broadcastPubSub.subscribe('baz', baz1); |
| 430 |
broadcastPubSub.subscribe('baz', baz2); |
| 431 |
|
| 432 |
remoteStorageEvent({'args': ['foo'], 'timestamp': goog.now()}); |
| 433 |
mockClock.tick(); |
| 434 |
broadcastPubSub.dispose(); |
| 435 |
mockControl.$verifyAll(); |
| 436 |
} |
| 437 |
|
| 438 |
|
| 439 |
/** |
| 440 |
* Local publish that originated from another instance of BroadcastChannel |
| 441 |
* in the same Javascript context. |
| 442 |
*/ |
| 443 |
function testSecondInstancePublish() { |
| 444 |
mockHTML5LocalStorageCtor().$returns(mockHtml5LocalStorage).$times(2); |
| 445 |
var foo = mockControl.createFunctionMock(); |
| 446 |
foo('x', 'y'); |
| 447 |
var context = {'foo': 'bar'}; |
| 448 |
var bar = goog.testing.recordFunction(); |
| 449 |
|
| 450 |
mockControl.$replayAll(); |
| 451 |
|
| 452 |
broadcastPubSub = new goog.labs.pubsub.BroadcastPubSub(); |
| 453 |
broadcastPubSub.logger_.setLevel(goog.debug.Logger.Level.OFF); |
| 454 |
broadcastPubSub.subscribe('someTopic', foo); |
| 455 |
broadcastPubSub.subscribe('someTopic', bar, context); |
| 456 |
|
| 457 |
var broadcastPubSub2 = new goog.labs.pubsub.BroadcastPubSub(); |
| 458 |
broadcastPubSub2.publish('someTopic', 'x', 'y'); |
| 459 |
mockClock.tick(); |
| 460 |
|
| 461 |
assertEquals(1, bar.getCallCount()); |
| 462 |
assertEquals(context, bar.getLastCall().getThis()); |
| 463 |
assertArrayEquals(['x', 'y'], bar.getLastCall().getArguments()); |
| 464 |
|
| 465 |
broadcastPubSub.dispose(); |
| 466 |
mockControl.$verifyAll(); |
| 467 |
} |
| 468 |
|
| 469 |
|
| 470 |
function testSecondInstanceNestedPublish() { |
| 471 |
mockHTML5LocalStorageCtor().$returns(mockHtml5LocalStorage).$times(2); |
| 472 |
var foo = mockControl.createFunctionMock(); |
| 473 |
foo('m', 'n').$does(function() { |
| 474 |
broadcastPubSub.publish('barTopic', 'd', 'c'); |
| 475 |
}); |
| 476 |
var bar = mockControl.createFunctionMock(); |
| 477 |
bar('d', 'c'); |
| 478 |
|
| 479 |
mockControl.$replayAll(); |
| 480 |
|
| 481 |
broadcastPubSub = new goog.labs.pubsub.BroadcastPubSub(); |
| 482 |
broadcastPubSub.logger_.setLevel(goog.debug.Logger.Level.OFF); |
| 483 |
broadcastPubSub.subscribe('fooTopic', foo); |
| 484 |
|
| 485 |
var broadcastPubSub2 = new goog.labs.pubsub.BroadcastPubSub(); |
| 486 |
broadcastPubSub2.subscribe('barTopic', bar); |
| 487 |
broadcastPubSub2.publish('fooTopic', 'm', 'n'); |
| 488 |
mockClock.tick(); |
| 489 |
mockClock.tick(); |
| 490 |
|
| 491 |
broadcastPubSub.dispose(); |
| 492 |
mockControl.$verifyAll(); |
| 493 |
} |
| 494 |
|
| 495 |
|
| 496 |
/** |
| 497 |
* Validate the localStorage data is being set as we expect. |
| 498 |
*/ |
| 499 |
function testLocalStorageData() { |
| 500 |
var topic = 'someTopic'; |
| 501 |
var anotherTopic = 'anotherTopic'; |
| 502 |
var now = goog.now(); |
| 503 |
|
| 504 |
mockHTML5LocalStorageCtor().$returns(mockHtml5LocalStorage); |
| 505 |
var mockStorage = mockControl.createLooseMock(goog.storage.Storage); |
| 506 |
|
| 507 |
var mockStorageCtor = |
| 508 |
mockControl.createConstructorMock(goog.storage, 'Storage'); |
| 509 |
|
| 510 |
mockStorageCtor(mockHtml5LocalStorage).$returns(mockStorage); |
| 511 |
if (!isIe8) { |
| 512 |
mockStorage.set( |
| 513 |
goog.labs.pubsub.BroadcastPubSub.STORAGE_KEY_, |
| 514 |
{'args': [topic, '10'], 'timestamp': now}); |
| 515 |
mockStorage.remove(goog.labs.pubsub.BroadcastPubSub.STORAGE_KEY_); |
| 516 |
mockStorage.set( |
| 517 |
goog.labs.pubsub.BroadcastPubSub.STORAGE_KEY_, |
| 518 |
{'args': [anotherTopic, '13'], 'timestamp': now}); |
| 519 |
mockStorage.remove(goog.labs.pubsub.BroadcastPubSub.STORAGE_KEY_); |
| 520 |
} else { |
| 521 |
var firstEventArray = [{'args': [topic, '10'], 'timestamp': now}]; |
| 522 |
var secondEventArray = [ |
| 523 |
{'args': [topic, '10'], 'timestamp': now}, { |
| 524 |
'args': [anotherTopic, '13'], |
| 525 |
'timestamp': now + |
| 526 |
goog.labs.pubsub.BroadcastPubSub.IE8_TIMESTAMP_UNIQUE_OFFSET_MS_ |
| 527 |
} |
| 528 |
]; |
| 529 |
|
| 530 |
mockStorage.get(goog.labs.pubsub.BroadcastPubSub.IE8_EVENTS_KEY_) |
| 531 |
.$returns(null); |
| 532 |
mockStorage.set( |
| 533 |
goog.labs.pubsub.BroadcastPubSub.IE8_EVENTS_KEY_, |
| 534 |
new goog.testing.mockmatchers.ArgumentMatcher(function(val) { |
| 535 |
return goog.testing.mockmatchers.flexibleArrayMatcher( |
| 536 |
firstEventArray, val); |
| 537 |
}, 'First event array')); |
| 538 |
|
| 539 |
// Make sure to clone or you're going to have a bad time. |
| 540 |
mockStorage.get(goog.labs.pubsub.BroadcastPubSub.IE8_EVENTS_KEY_) |
| 541 |
.$returns(goog.array.clone(firstEventArray)); |
| 542 |
|
| 543 |
mockStorage.set( |
| 544 |
goog.labs.pubsub.BroadcastPubSub.IE8_EVENTS_KEY_, |
| 545 |
new goog.testing.mockmatchers.ArgumentMatcher(function(val) { |
| 546 |
return goog.testing.mockmatchers.flexibleArrayMatcher( |
| 547 |
secondEventArray, val); |
| 548 |
}, 'Second event array')); |
| 549 |
|
| 550 |
mockStorage.remove(goog.labs.pubsub.BroadcastPubSub.IE8_EVENTS_KEY_); |
| 551 |
} |
| 552 |
|
| 553 |
var fn = goog.testing.recordFunction(); |
| 554 |
fn('10'); |
| 555 |
mockControl.$replayAll(); |
| 556 |
|
| 557 |
broadcastPubSub = new goog.labs.pubsub.BroadcastPubSub(); |
| 558 |
broadcastPubSub.logger_.setLevel(goog.debug.Logger.Level.OFF); |
| 559 |
broadcastPubSub.subscribe(topic, fn); |
| 560 |
|
| 561 |
broadcastPubSub.publish(topic, '10'); |
| 562 |
broadcastPubSub.publish(anotherTopic, '13'); |
| 563 |
|
| 564 |
mockClock.tick(); |
| 565 |
|
| 566 |
broadcastPubSub.dispose(); |
| 567 |
mockControl.$verifyAll(); |
| 568 |
} |
| 569 |
|
| 570 |
|
| 571 |
function testBrokenTimestamp() { |
| 572 |
mockHTML5LocalStorageCtor().$returns(mockHtml5LocalStorage); |
| 573 |
var fn = mockControl.createFunctionMock(); |
| 574 |
mockControl.$replayAll(); |
| 575 |
|
| 576 |
broadcastPubSub = new goog.labs.pubsub.BroadcastPubSub(); |
| 577 |
broadcastPubSub.logger_.setLevel(goog.debug.Logger.Level.OFF); |
| 578 |
|
| 579 |
broadcastPubSub.subscribe('someTopic', fn); |
| 580 |
|
| 581 |
remoteStorageEvent({'args': 'WAT?', 'timestamp': 'wat?'}); |
| 582 |
mockClock.tick(); |
| 583 |
|
| 584 |
broadcastPubSub.dispose(); |
| 585 |
mockControl.$verifyAll(); |
| 586 |
} |
| 587 |
|
| 588 |
|
| 589 |
/** Test response to bad localStorage data. */ |
| 590 |
function testBrokenEvent() { |
| 591 |
mockHTML5LocalStorageCtor().$returns(mockHtml5LocalStorage); |
| 592 |
var fn = mockControl.createFunctionMock(); |
| 593 |
mockControl.$replayAll(); |
| 594 |
|
| 595 |
broadcastPubSub = new goog.labs.pubsub.BroadcastPubSub(); |
| 596 |
broadcastPubSub.logger_.setLevel(goog.debug.Logger.Level.OFF); |
| 597 |
broadcastPubSub.subscribe('someTopic', fn); |
| 598 |
|
| 599 |
if (!isIe8) { |
| 600 |
var event = new goog.testing.events.Event('storage', window); |
| 601 |
event.key = 'FooBarBaz'; |
| 602 |
event.newValue = goog.json.serialize({'keyby': 'word'}); |
| 603 |
goog.testing.events.fireBrowserEvent(event); |
| 604 |
} else { |
| 605 |
var uniqueKey = |
| 606 |
goog.labs.pubsub.BroadcastPubSub.IE8_EVENTS_KEY_PREFIX_ + '1234567890'; |
| 607 |
// This will cause an event. |
| 608 |
mockHtml5LocalStorage.set(uniqueKey, 'Toothpaste!'); |
| 609 |
} |
| 610 |
mockClock.tick(); |
| 611 |
|
| 612 |
broadcastPubSub.dispose(); |
| 613 |
mockControl.$verifyAll(); |
| 614 |
} |
| 615 |
|
| 616 |
|
| 617 |
/** |
| 618 |
* The following tests are duplicated from pubsub because they depend |
| 619 |
* on functionality (mostly "publish") that has changed in BroadcastChannel. |
| 620 |
*/ |
| 621 |
function testPublish() { |
| 622 |
mockHTML5LocalStorageCtor().$returns(mockHtml5LocalStorage); |
| 623 |
var foo = mockControl.createFunctionMock(); |
| 624 |
foo('x', 'y'); |
| 625 |
|
| 626 |
var context = {'foo': 'bar'}; |
| 627 |
var bar = goog.testing.recordFunction(); |
| 628 |
|
| 629 |
var baz = mockControl.createFunctionMock(); |
| 630 |
baz('d', 'c'); |
| 631 |
|
| 632 |
mockControl.$replayAll(); |
| 633 |
broadcastPubSub = new goog.labs.pubsub.BroadcastPubSub(); |
| 634 |
broadcastPubSub.logger_.setLevel(goog.debug.Logger.Level.OFF); |
| 635 |
|
| 636 |
broadcastPubSub.subscribe('someTopic', foo); |
| 637 |
broadcastPubSub.subscribe('someTopic', bar, context); |
| 638 |
broadcastPubSub.subscribe('anotherTopic', baz, context); |
| 639 |
|
| 640 |
broadcastPubSub.publish('someTopic', 'x', 'y'); |
| 641 |
mockClock.tick(); |
| 642 |
|
| 643 |
assertTrue(broadcastPubSub.unsubscribe('someTopic', foo)); |
| 644 |
|
| 645 |
broadcastPubSub.publish('anotherTopic', 'd', 'c'); |
| 646 |
broadcastPubSub.publish('someTopic', 'x', 'y'); |
| 647 |
mockClock.tick(); |
| 648 |
|
| 649 |
broadcastPubSub.subscribe('differentTopic', foo); |
| 650 |
|
| 651 |
broadcastPubSub.publish('someTopic', 'x', 'y'); |
| 652 |
mockClock.tick(); |
| 653 |
|
| 654 |
assertEquals(3, bar.getCallCount()); |
| 655 |
goog.array.forEach(bar.getCalls(), function(call) { |
| 656 |
assertArrayEquals(['x', 'y'], call.getArguments()); |
| 657 |
assertEquals(context, call.getThis()); |
| 658 |
}); |
| 659 |
broadcastPubSub.dispose(); |
| 660 |
mockControl.$verifyAll(); |
| 661 |
} |
| 662 |
|
| 663 |
|
| 664 |
function testPublishEmptyTopic() { |
| 665 |
mockHTML5LocalStorageCtor().$returns(mockHtml5LocalStorage); |
| 666 |
var foo = mockControl.createFunctionMock(); |
| 667 |
foo(); |
| 668 |
|
| 669 |
mockControl.$replayAll(); |
| 670 |
broadcastPubSub = new goog.labs.pubsub.BroadcastPubSub(); |
| 671 |
broadcastPubSub.logger_.setLevel(goog.debug.Logger.Level.OFF); |
| 672 |
|
| 673 |
broadcastPubSub.publish('someTopic'); |
| 674 |
mockClock.tick(); |
| 675 |
|
| 676 |
broadcastPubSub.subscribe('someTopic', foo); |
| 677 |
broadcastPubSub.publish('someTopic'); |
| 678 |
mockClock.tick(); |
| 679 |
|
| 680 |
broadcastPubSub.unsubscribe('someTopic', foo); |
| 681 |
broadcastPubSub.publish('someTopic'); |
| 682 |
mockClock.tick(); |
| 683 |
|
| 684 |
broadcastPubSub.dispose(); |
| 685 |
mockControl.$verifyAll(); |
| 686 |
} |
| 687 |
|
| 688 |
|
| 689 |
function testSubscribeWhilePublishing() { |
| 690 |
mockHTML5LocalStorageCtor().$returns(mockHtml5LocalStorage); |
| 691 |
// It's OK for a subscriber to add a new subscriber to its own topic, |
| 692 |
// but the newly added subscriber shouldn't be called until the next |
| 693 |
// publish cycle. |
| 694 |
|
| 695 |
var fn1 = mockControl.createFunctionMock(); |
| 696 |
var fn2 = mockControl.createFunctionMock(); |
| 697 |
fn1() |
| 698 |
.$does(function() { broadcastPubSub.subscribe('someTopic', fn2); }) |
| 699 |
.$times(2); |
| 700 |
fn2(); |
| 701 |
|
| 702 |
mockControl.$replayAll(); |
| 703 |
broadcastPubSub = new goog.labs.pubsub.BroadcastPubSub(); |
| 704 |
broadcastPubSub.logger_.setLevel(goog.debug.Logger.Level.OFF); |
| 705 |
|
| 706 |
broadcastPubSub.subscribe('someTopic', fn1); |
| 707 |
assertEquals( |
| 708 |
'Topic must have one subscriber', 1, |
| 709 |
broadcastPubSub.getCount('someTopic')); |
| 710 |
|
| 711 |
broadcastPubSub.publish('someTopic'); |
| 712 |
mockClock.tick(); |
| 713 |
|
| 714 |
assertEquals( |
| 715 |
'Topic must have two subscribers', 2, |
| 716 |
broadcastPubSub.getCount('someTopic')); |
| 717 |
|
| 718 |
broadcastPubSub.publish('someTopic'); |
| 719 |
mockClock.tick(); |
| 720 |
|
| 721 |
assertEquals( |
| 722 |
'Topic must have three subscribers', 3, |
| 723 |
broadcastPubSub.getCount('someTopic')); |
| 724 |
broadcastPubSub.dispose(); |
| 725 |
mockControl.$verifyAll(); |
| 726 |
} |
| 727 |
|
| 728 |
|
| 729 |
function testUnsubscribeWhilePublishing() { |
| 730 |
mockHTML5LocalStorageCtor().$returns(mockHtml5LocalStorage); |
| 731 |
// It's OK for a subscriber to unsubscribe another subscriber from its |
| 732 |
// own topic, but the subscriber in question won't actually be removed |
| 733 |
// until after publishing is complete. |
| 734 |
|
| 735 |
|
| 736 |
var fn1 = mockControl.createFunctionMock(); |
| 737 |
var fn2 = mockControl.createFunctionMock(); |
| 738 |
var fn3 = mockControl.createFunctionMock(); |
| 739 |
|
| 740 |
fn1().$does(function() { |
| 741 |
assertTrue( |
| 742 |
'unsubscribe() must return true when removing a topic', |
| 743 |
broadcastPubSub.unsubscribe('X', fn2)); |
| 744 |
assertEquals( |
| 745 |
'Topic "X" must still have 3 subscribers', 3, |
| 746 |
broadcastPubSub.getCount('X')); |
| 747 |
}); |
| 748 |
fn2().$times(0); |
| 749 |
fn3().$does(function() { |
| 750 |
assertTrue( |
| 751 |
'unsubscribe() must return true when removing a topic', |
| 752 |
broadcastPubSub.unsubscribe('X', fn1)); |
| 753 |
assertEquals( |
| 754 |
'Topic "X" must still have 3 subscribers', 3, |
| 755 |
broadcastPubSub.getCount('X')); |
| 756 |
}); |
| 757 |
|
| 758 |
mockControl.$replayAll(); |
| 759 |
broadcastPubSub = new goog.labs.pubsub.BroadcastPubSub(); |
| 760 |
broadcastPubSub.logger_.setLevel(goog.debug.Logger.Level.OFF); |
| 761 |
|
| 762 |
broadcastPubSub.subscribe('X', fn1); |
| 763 |
broadcastPubSub.subscribe('X', fn2); |
| 764 |
broadcastPubSub.subscribe('X', fn3); |
| 765 |
|
| 766 |
assertEquals( |
| 767 |
'Topic "X" must have 3 subscribers', 3, broadcastPubSub.getCount('X')); |
| 768 |
|
| 769 |
broadcastPubSub.publish('X'); |
| 770 |
mockClock.tick(); |
| 771 |
|
| 772 |
assertEquals( |
| 773 |
'Topic "X" must have 1 subscriber after publishing', 1, |
| 774 |
broadcastPubSub.getCount('X')); |
| 775 |
assertEquals( |
| 776 |
'BroadcastChannel must not have any subscriptions pending removal', 0, |
| 777 |
broadcastPubSub.pubSub_.pendingKeys_.length); |
| 778 |
broadcastPubSub.dispose(); |
| 779 |
mockControl.$verifyAll(); |
| 780 |
} |
| 781 |
|
| 782 |
|
| 783 |
function testUnsubscribeSelfWhilePublishing() { |
| 784 |
mockHTML5LocalStorageCtor().$returns(mockHtml5LocalStorage); |
| 785 |
// It's OK for a subscriber to unsubscribe itself, but it won't actually |
| 786 |
// be removed until after publishing is complete. |
| 787 |
|
| 788 |
var fn = mockControl.createFunctionMock(); |
| 789 |
fn().$does(function() { |
| 790 |
assertTrue( |
| 791 |
'unsubscribe() must return true when removing a topic', |
| 792 |
broadcastPubSub.unsubscribe('someTopic', fn)); |
| 793 |
assertEquals( |
| 794 |
'Topic must still have 1 subscriber', 1, |
| 795 |
broadcastPubSub.getCount('someTopic')); |
| 796 |
}); |
| 797 |
|
| 798 |
mockControl.$replayAll(); |
| 799 |
broadcastPubSub = new goog.labs.pubsub.BroadcastPubSub(); |
| 800 |
broadcastPubSub.logger_.setLevel(goog.debug.Logger.Level.OFF); |
| 801 |
|
| 802 |
broadcastPubSub.subscribe('someTopic', fn); |
| 803 |
assertEquals( |
| 804 |
'Topic must have 1 subscriber', 1, broadcastPubSub.getCount('someTopic')); |
| 805 |
|
| 806 |
broadcastPubSub.publish('someTopic'); |
| 807 |
mockClock.tick(); |
| 808 |
|
| 809 |
assertEquals( |
| 810 |
'Topic must have no subscribers after publishing', 0, |
| 811 |
broadcastPubSub.getCount('someTopic')); |
| 812 |
assertEquals( |
| 813 |
'BroadcastChannel must not have any subscriptions pending removal', 0, |
| 814 |
broadcastPubSub.pubSub_.pendingKeys_.length); |
| 815 |
broadcastPubSub.dispose(); |
| 816 |
mockControl.$verifyAll(); |
| 817 |
} |
| 818 |
|
| 819 |
|
| 820 |
function testNestedPublish() { |
| 821 |
mockHTML5LocalStorageCtor().$returns(mockHtml5LocalStorage); |
| 822 |
var xFn1 = mockControl.createFunctionMock(); |
| 823 |
xFn1().$does(function() { |
| 824 |
broadcastPubSub.publish('Y'); |
| 825 |
broadcastPubSub.unsubscribe('X', arguments.callee); |
| 826 |
}); |
| 827 |
var xFn2 = mockControl.createFunctionMock(); |
| 828 |
xFn2(); |
| 829 |
|
| 830 |
var yFn1 = mockControl.createFunctionMock(); |
| 831 |
yFn1().$does(function() { |
| 832 |
broadcastPubSub.unsubscribe('Y', arguments.callee); |
| 833 |
}); |
| 834 |
var yFn2 = mockControl.createFunctionMock(); |
| 835 |
yFn2(); |
| 836 |
|
| 837 |
mockControl.$replayAll(); |
| 838 |
|
| 839 |
broadcastPubSub = new goog.labs.pubsub.BroadcastPubSub(); |
| 840 |
broadcastPubSub.logger_.setLevel(goog.debug.Logger.Level.OFF); |
| 841 |
broadcastPubSub.subscribe('X', xFn1); |
| 842 |
broadcastPubSub.subscribe('X', xFn2); |
| 843 |
broadcastPubSub.subscribe('Y', yFn1); |
| 844 |
broadcastPubSub.subscribe('Y', yFn2); |
| 845 |
|
| 846 |
broadcastPubSub.publish('X'); |
| 847 |
mockClock.tick(); |
| 848 |
|
| 849 |
broadcastPubSub.dispose(); |
| 850 |
mockControl.$verifyAll(); |
| 851 |
} |
| 852 |
|
| 853 |
|
| 854 |
function testSubscribeOnce() { |
| 855 |
mockHTML5LocalStorageCtor().$returns(mockHtml5LocalStorage); |
| 856 |
var fn = goog.testing.recordFunction(); |
| 857 |
|
| 858 |
mockControl.$replayAll(); |
| 859 |
broadcastPubSub = new goog.labs.pubsub.BroadcastPubSub(); |
| 860 |
broadcastPubSub.logger_.setLevel(goog.debug.Logger.Level.OFF); |
| 861 |
broadcastPubSub.subscribeOnce('someTopic', fn); |
| 862 |
|
| 863 |
assertEquals( |
| 864 |
'Topic must have one subscriber', 1, |
| 865 |
broadcastPubSub.getCount('someTopic')); |
| 866 |
|
| 867 |
broadcastPubSub.publish('someTopic'); |
| 868 |
mockClock.tick(); |
| 869 |
|
| 870 |
assertEquals( |
| 871 |
'Topic must have no subscribers', 0, |
| 872 |
broadcastPubSub.getCount('someTopic')); |
| 873 |
|
| 874 |
var context = {'foo': 'bar'}; |
| 875 |
broadcastPubSub.subscribeOnce('someTopic', fn, context); |
| 876 |
assertEquals( |
| 877 |
'Topic must have one subscriber', 1, |
| 878 |
broadcastPubSub.getCount('someTopic')); |
| 879 |
assertEquals( |
| 880 |
'Subscriber must not have been called yet', 1, fn.getCallCount()); |
| 881 |
|
| 882 |
broadcastPubSub.publish('someTopic'); |
| 883 |
mockClock.tick(); |
| 884 |
|
| 885 |
assertEquals( |
| 886 |
'Topic must have no subscribers', 0, |
| 887 |
broadcastPubSub.getCount('someTopic')); |
| 888 |
assertEquals('Subscriber must have been called', 2, fn.getCallCount()); |
| 889 |
assertEquals(context, fn.getLastCall().getThis()); |
| 890 |
assertArrayEquals([], fn.getLastCall().getArguments()); |
| 891 |
|
| 892 |
context = {'foo': 'bar'}; |
| 893 |
broadcastPubSub.subscribeOnce('someTopic', fn, context); |
| 894 |
assertEquals( |
| 895 |
'Topic must have one subscriber', 1, |
| 896 |
broadcastPubSub.getCount('someTopic')); |
| 897 |
assertEquals('Subscriber must not have been called', 2, fn.getCallCount()); |
| 898 |
|
| 899 |
broadcastPubSub.publish('someTopic', '17'); |
| 900 |
mockClock.tick(); |
| 901 |
|
| 902 |
assertEquals( |
| 903 |
'Topic must have no subscribers', 0, |
| 904 |
broadcastPubSub.getCount('someTopic')); |
| 905 |
assertEquals(context, fn.getLastCall().getThis()); |
| 906 |
assertEquals('Subscriber must have been called', 3, fn.getCallCount()); |
| 907 |
assertArrayEquals(['17'], fn.getLastCall().getArguments()); |
| 908 |
broadcastPubSub.dispose(); |
| 909 |
mockControl.$verifyAll(); |
| 910 |
} |
| 911 |
|
| 912 |
|
| 913 |
function testSubscribeOnce_boundFn() { |
| 914 |
mockHTML5LocalStorageCtor().$returns(mockHtml5LocalStorage); |
| 915 |
var fn = goog.testing.recordFunction(); |
| 916 |
var context = {'foo': 'bar'}; |
| 917 |
|
| 918 |
mockControl.$replayAll(); |
| 919 |
broadcastPubSub = new goog.labs.pubsub.BroadcastPubSub(); |
| 920 |
broadcastPubSub.logger_.setLevel(goog.debug.Logger.Level.OFF); |
| 921 |
|
| 922 |
broadcastPubSub.subscribeOnce('someTopic', goog.bind(fn, context)); |
| 923 |
assertEquals( |
| 924 |
'Topic must have one subscriber', 1, |
| 925 |
broadcastPubSub.getCount('someTopic')); |
| 926 |
assertNull('Subscriber must not have been called yet', fn.getLastCall()); |
| 927 |
|
| 928 |
broadcastPubSub.publish('someTopic', '17'); |
| 929 |
mockClock.tick(); |
| 930 |
assertEquals( |
| 931 |
'Topic must have no subscribers', 0, |
| 932 |
broadcastPubSub.getCount('someTopic')); |
| 933 |
assertEquals('Subscriber must have been called', 1, fn.getCallCount()); |
| 934 |
assertEquals( |
| 935 |
'Must receive correct argument.', '17', fn.getLastCall().getArgument(0)); |
| 936 |
assertEquals( |
| 937 |
'Must have appropriate context.', context, fn.getLastCall().getThis()); |
| 938 |
|
| 939 |
broadcastPubSub.dispose(); |
| 940 |
mockControl.$verifyAll(); |
| 941 |
} |
| 942 |
|
| 943 |
|
| 944 |
function testSubscribeOnce_partialFn() { |
| 945 |
mockHTML5LocalStorageCtor().$returns(mockHtml5LocalStorage); |
| 946 |
var fullFn = mockControl.createFunctionMock(); |
| 947 |
fullFn(true, '17'); |
| 948 |
|
| 949 |
mockControl.$replayAll(); |
| 950 |
broadcastPubSub = new goog.labs.pubsub.BroadcastPubSub(); |
| 951 |
broadcastPubSub.logger_.setLevel(goog.debug.Logger.Level.OFF); |
| 952 |
|
| 953 |
broadcastPubSub.subscribeOnce('someTopic', goog.partial(fullFn, true)); |
| 954 |
assertEquals( |
| 955 |
'Topic must have one subscriber', 1, |
| 956 |
broadcastPubSub.getCount('someTopic')); |
| 957 |
|
| 958 |
broadcastPubSub.publish('someTopic', '17'); |
| 959 |
mockClock.tick(); |
| 960 |
|
| 961 |
assertEquals( |
| 962 |
'Topic must have no subscribers', 0, |
| 963 |
broadcastPubSub.getCount('someTopic')); |
| 964 |
broadcastPubSub.dispose(); |
| 965 |
mockControl.$verifyAll(); |
| 966 |
} |
| 967 |
|
| 968 |
|
| 969 |
function testSelfResubscribe() { |
| 970 |
mockHTML5LocalStorageCtor().$returns(mockHtml5LocalStorage); |
| 971 |
var resubscribeFn = mockControl.createFunctionMock(); |
| 972 |
var resubscribe = function() { |
| 973 |
broadcastPubSub.subscribeOnce('someTopic', resubscribeFn); |
| 974 |
}; |
| 975 |
resubscribeFn('foo').$does(resubscribe); |
| 976 |
resubscribeFn('bar').$does(resubscribe); |
| 977 |
resubscribeFn('baz').$does(resubscribe); |
| 978 |
|
| 979 |
mockControl.$replayAll(); |
| 980 |
broadcastPubSub = new goog.labs.pubsub.BroadcastPubSub(); |
| 981 |
broadcastPubSub.logger_.setLevel(goog.debug.Logger.Level.OFF); |
| 982 |
|
| 983 |
broadcastPubSub.subscribeOnce('someTopic', resubscribeFn); |
| 984 |
assertEquals( |
| 985 |
'Topic must have 1 subscriber', 1, broadcastPubSub.getCount('someTopic')); |
| 986 |
|
| 987 |
broadcastPubSub.publish('someTopic', 'foo'); |
| 988 |
mockClock.tick(); |
| 989 |
assertEquals( |
| 990 |
'Topic must have 1 subscriber', 1, broadcastPubSub.getCount('someTopic')); |
| 991 |
assertEquals( |
| 992 |
'BroadcastChannel must not have any pending unsubscribe keys', 0, |
| 993 |
broadcastPubSub.pubSub_.pendingKeys_.length); |
| 994 |
|
| 995 |
broadcastPubSub.publish('someTopic', 'bar'); |
| 996 |
mockClock.tick(); |
| 997 |
assertEquals( |
| 998 |
'Topic must have 1 subscriber', 1, broadcastPubSub.getCount('someTopic')); |
| 999 |
assertEquals( |
| 1000 |
'BroadcastChannel must not have any pending unsubscribe keys', 0, |
| 1001 |
broadcastPubSub.pubSub_.pendingKeys_.length); |
| 1002 |
|
| 1003 |
broadcastPubSub.publish('someTopic', 'baz'); |
| 1004 |
mockClock.tick(); |
| 1005 |
assertEquals( |
| 1006 |
'Topic must have 1 subscriber', 1, broadcastPubSub.getCount('someTopic')); |
| 1007 |
assertEquals( |
| 1008 |
'BroadcastChannel must not have any pending unsubscribe keys', 0, |
| 1009 |
broadcastPubSub.pubSub_.pendingKeys_.length); |
| 1010 |
broadcastPubSub.dispose(); |
| 1011 |
mockControl.$verifyAll(); |
| 1012 |
} |
| 1013 |
|
| 1014 |
|
| 1015 |
function testClear() { |
| 1016 |
mockHTML5LocalStorageCtor().$returns(mockHtml5LocalStorage); |
| 1017 |
var fn = mockControl.createFunctionMock(); |
| 1018 |
mockControl.$replayAll(); |
| 1019 |
broadcastPubSub = new goog.labs.pubsub.BroadcastPubSub(); |
| 1020 |
broadcastPubSub.logger_.setLevel(goog.debug.Logger.Level.OFF); |
| 1021 |
|
| 1022 |
goog.array.forEach(['V', 'W', 'X', 'Y', 'Z'], function(topic) { |
| 1023 |
broadcastPubSub.subscribe(topic, fn); |
| 1024 |
}); |
| 1025 |
assertEquals( |
| 1026 |
'BroadcastChannel must have 5 subscribers', 5, |
| 1027 |
broadcastPubSub.getCount()); |
| 1028 |
|
| 1029 |
broadcastPubSub.clear('W'); |
| 1030 |
assertEquals( |
| 1031 |
'BroadcastChannel must have 4 subscribers', 4, |
| 1032 |
broadcastPubSub.getCount()); |
| 1033 |
|
| 1034 |
goog.array.forEach( |
| 1035 |
['X', 'Y'], function(topic) { broadcastPubSub.clear(topic); }); |
| 1036 |
assertEquals( |
| 1037 |
'BroadcastChannel must have 2 subscriber', 2, broadcastPubSub.getCount()); |
| 1038 |
|
| 1039 |
broadcastPubSub.clear(); |
| 1040 |
assertEquals( |
| 1041 |
'BroadcastChannel must have no subscribers', 0, |
| 1042 |
broadcastPubSub.getCount()); |
| 1043 |
broadcastPubSub.dispose(); |
| 1044 |
mockControl.$verifyAll(); |
| 1045 |
} |
| 1046 |
|
| 1047 |
function testNestedSubscribeOnce() { |
| 1048 |
mockHTML5LocalStorageCtor().$returns(mockHtml5LocalStorage); |
| 1049 |
|
| 1050 |
var x = mockControl.createFunctionMock(); |
| 1051 |
var y = mockControl.createFunctionMock(); |
| 1052 |
|
| 1053 |
x().$times(1); |
| 1054 |
y().$does(function() { |
| 1055 |
broadcastPubSub.publish('X'); |
| 1056 |
broadcastPubSub.publish('X'); |
| 1057 |
}); |
| 1058 |
|
| 1059 |
mockControl.$replayAll(); |
| 1060 |
|
| 1061 |
broadcastPubSub = new goog.labs.pubsub.BroadcastPubSub(); |
| 1062 |
broadcastPubSub.subscribeOnce('X', x); |
| 1063 |
broadcastPubSub.subscribe('Y', y); |
| 1064 |
broadcastPubSub.publish('Y'); |
| 1065 |
mockClock.tick(); |
| 1066 |
|
| 1067 |
broadcastPubSub.dispose(); |
| 1068 |
mockControl.$verifyAll(); |
| 1069 |
} |