Roll src/third_party/WebKit d9c6159:8139f33 (svn 201974:201975)
[chromium-blink-merge.git] / remoting / webapp / base / js / window_message_dispatcher_unittest.js
blobc9d9399b43af3946cc068fe6400954e4e42eeb44
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.
5 /**
6 * @fileoverview
7 */
9 (function() {
11 'use strict';
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;
24 });
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.
38 deferred.resolve();
41 windowMessageDispatcher.registerMessageHandler('testSource', handler);
43 var message = {'source': 'testSource', 'command': 'testCommand'};
44 window.postMessage(message, '*');
46 return deferred.promise();
47 });
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',
62 function(assert) {
63 var deferred = new base.Deferred();
64 var callCount1 = 0;
65 var callCount2 = 0;
67 /** @param {Event} event */
68 var handler1 = function(event) {
69 if (callCount1 > 0) {
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');
76 ++callCount1;
78 // 3. When the message is delivered, unregister message handler for
79 // 'testSource1' and send a message to 'testSource2'.
80 windowMessageDispatcher.unregisterMessageHandler('testSource1');
81 window.postMessage(
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');
90 ++callCount2;
92 switch (callCount2) {
93 case 1:
94 // 4. When the message to 'testSource2' is delivered. Send a message
95 // each to 'testSource1' and 'testSource2'.
96 assert.equal(callCount1, 1);
97 window.postMessage(
98 {'source': 'testSource1', 'command': 'testCommand1'}, '*');
99 window.postMessage(
100 {'source': 'testSource2', 'command': 'testCommand2'}, '*');
101 break;
102 case 2:
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);
106 deferred.resolve();
107 break;
108 default:
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.
118 window.postMessage(
119 {'source': 'testSource1', 'command': 'testCommand1'}, '*');
121 return deferred.promise();
124 })();