| 1 |
// Copyright 2011 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.messaging.PortNetworkTest'); |
| 16 |
goog.setTestOnly('goog.messaging.PortNetworkTest'); |
| 17 |
|
| 18 |
goog.require('goog.Promise'); |
| 19 |
goog.require('goog.Timer'); |
| 20 |
goog.require('goog.labs.userAgent.browser'); |
| 21 |
goog.require('goog.messaging.PortChannel'); |
| 22 |
goog.require('goog.messaging.PortOperator'); |
| 23 |
goog.require('goog.testing.TestCase'); |
| 24 |
goog.require('goog.testing.jsunit'); |
| 25 |
|
| 26 |
var timer; |
| 27 |
|
| 28 |
function shouldRunTests() { |
| 29 |
// TODO(b/31221500): This test fails when run in a suite immediately after |
| 30 |
// portchannel_test. The workers take dozens of seconds to start up for some |
| 31 |
// reason. |
| 32 |
return !goog.labs.userAgent.browser.isEdge(); |
| 33 |
} |
| 34 |
|
| 35 |
function setUpPage() { |
| 36 |
// Use a relatively long timeout because workers can take a while to start up. |
| 37 |
goog.testing.TestCase.getActiveTestCase().promiseTimeout = 5 * 1000; |
| 38 |
} |
| 39 |
|
| 40 |
function setUp() { |
| 41 |
timer = new goog.Timer(50); |
| 42 |
} |
| 43 |
|
| 44 |
function tearDown() { |
| 45 |
goog.dispose(timer); |
| 46 |
} |
| 47 |
|
| 48 |
function testRouteMessageThroughWorkers() { |
| 49 |
if (!('MessageChannel' in goog.global)) { |
| 50 |
return; |
| 51 |
} |
| 52 |
|
| 53 |
var master = new goog.messaging.PortOperator('main'); |
| 54 |
master.addPort( |
| 55 |
'worker1', new goog.messaging.PortChannel( |
| 56 |
new Worker('testdata/portnetwork_worker1.js'))); |
| 57 |
master.addPort( |
| 58 |
'worker2', new goog.messaging.PortChannel( |
| 59 |
new Worker('testdata/portnetwork_worker2.js'))); |
| 60 |
var peerOrigin = window.location.protocol + '//' + window.location.host; |
| 61 |
master.addPort( |
| 62 |
'frame', goog.messaging.PortChannel.forEmbeddedWindow( |
| 63 |
window.frames['inner'], peerOrigin, timer)); |
| 64 |
|
| 65 |
var promise = new goog.Promise(function(resolve, reject) { |
| 66 |
master.dial('worker1').registerService('result', resolve, true); |
| 67 |
}); |
| 68 |
master.dial('worker2').send('sendToFrame', ['main']); |
| 69 |
|
| 70 |
return promise |
| 71 |
.then(function(msg) { |
| 72 |
assertArrayEquals(['main', 'worker2', 'frame', 'worker1'], msg); |
| 73 |
}) |
| 74 |
.thenAlways(function() { master.dispose(); }); |
| 75 |
} |