portnetwork_test.js 2.5 KB

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