1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
13 /** @type {base.WindowMessageDispatcher} */
14 var windowMessageDispatcher = null;
16 QUnit.module('WindowMessageDispatcher', {
17 beforeEach: function() {
18 windowMessageDispatcher = new base.WindowMessageDispatcher();
20 afterEach: function() {
21 base.dispose(windowMessageDispatcher);
22 windowMessageDispatcher = null;
26 // Window messages are asynchronous. The message we post may still be in the
27 // pipeline by the time we return from the main test routine. So we return a
28 // promise to wait for the delivery of the message.
29 QUnit.test('handler should be invoked after registration', function(assert) {
30 var deferred = new base.Deferred();
32 /** @param {Event} event */
33 var handler = function(event) {
34 assert.equal(event.data['source'], 'testSource');
35 assert.equal(event.data['command'], 'testCommand');
37 // Resolve the promise to signal the completion of the test.
41 windowMessageDispatcher.registerMessageHandler('testSource', handler);
43 var message = {'source': 'testSource', 'command': 'testCommand'};
44 window.postMessage(message, '*');
46 return deferred.promise();
49 // In this test we test message dispatching to 'testSource1' before and after
50 // the registration and unregistration of the message handler. The messages
51 // for 'testSource2' are used as a synchronization mechanism.
52 // Here is the workflow of this test:
53 // 1. Register message handlers for 'testSource1' and 'testSource2'.
54 // 2. Post a message to testSource1.
55 // 3. When the message is delivered, unregister message handler for
56 // 'testSource1' and send a message to 'testSource2'.
57 // 4. When the message to 'testSource2' is delivered. Send a message
58 // each to 'testSource1' and 'testSource2'.
59 // 5. When the second message to 'testSource2' is delivered. Make sure
60 // that the second message to 'testSource1' has not been delivered.
61 QUnit.test('handler should not be invoked after unregistration',
63 var deferred = new base.Deferred();
67 /** @param {Event} event */
68 var handler1 = function(event) {
70 deferred.reject('handler1 should not be called more than once.');
73 assert.equal(event.data['source'], 'testSource1');
74 assert.equal(event.data['command'], 'testCommand1');
78 // 3. When the message is delivered, unregister message handler for
79 // 'testSource1' and send a message to 'testSource2'.
80 windowMessageDispatcher.unregisterMessageHandler('testSource1');
82 {'source': 'testSource2', 'command': 'testCommand2'}, '*');
85 /** @param {Event} event */
86 var handler2 = function(event) {
87 assert.equal(event.data['source'], 'testSource2');
88 assert.equal(event.data['command'], 'testCommand2');
94 // 4. When the message to 'testSource2' is delivered. Send a message
95 // each to 'testSource1' and 'testSource2'.
96 assert.equal(callCount1, 1);
98 {'source': 'testSource1', 'command': 'testCommand1'}, '*');
100 {'source': 'testSource2', 'command': 'testCommand2'}, '*');
103 // 5. When the second message to 'testSource2' is delivered. Make sure
104 // that the second message to 'testSource1' has not been delivered.
105 assert.equal(callCount1, 1);
109 deferred.reject('handler1 was never called.');
113 // 1. Register message handlers for 'testSource1' and 'testSource2'.
114 windowMessageDispatcher.registerMessageHandler('testSource1', handler1);
115 windowMessageDispatcher.registerMessageHandler('testSource2', handler2);
117 // 2. Post a message to testSource1.
119 {'source': 'testSource1', 'command': 'testCommand1'}, '*');
121 return deferred.promise();