multiiframeloadmonitor_test.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. // Copyright 2008 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.module('goog.net.MultiIframeLoadMonitorTest');
  15. goog.setTestOnly('goog.net.MultiIframeLoadMonitorTest');
  16. var IframeLoadMonitor = goog.require('goog.net.IframeLoadMonitor');
  17. var MultiIframeLoadMonitor = goog.require('goog.net.MultiIframeLoadMonitor');
  18. var Promise = goog.require('goog.Promise');
  19. var PropertyReplacer = goog.require('goog.testing.PropertyReplacer');
  20. var TagName = goog.require('goog.dom.TagName');
  21. var Timer = goog.require('goog.Timer');
  22. var dom = goog.require('goog.dom');
  23. var testSuite = goog.require('goog.testing.testSuite');
  24. var stubs = new PropertyReplacer();
  25. var TEST_FRAME_SRCS =
  26. ['iframeloadmonitor_test_frame.html', 'iframeloadmonitor_test_frame2.html'];
  27. var frameParent;
  28. testSuite({
  29. setUpPage: function() { frameParent = dom.getElement('frame_parent'); },
  30. tearDown: function() {
  31. dom.removeChildren(frameParent);
  32. stubs.reset();
  33. },
  34. testMultiIframeLoadMonitor: function() {
  35. var frames = [dom.createDom(TagName.IFRAME), dom.createDom(TagName.IFRAME)];
  36. var loaded = false;
  37. var monitorPromise = new Promise(function(resolve, reject) {
  38. new MultiIframeLoadMonitor(frames, function() {
  39. loaded = true;
  40. resolve();
  41. });
  42. });
  43. assertFalse(loaded);
  44. frameParent.appendChild(frames[0]);
  45. frameParent.appendChild(frames[1]);
  46. return monitorPromise.then(function() { assertTrue(loaded); });
  47. },
  48. testMultiIframeLoadMonitor_withContentCheck: function() {
  49. var frames = [dom.createDom(TagName.IFRAME), dom.createDom(TagName.IFRAME)];
  50. var loaded = false;
  51. var monitorPromise = new Promise(function(resolve, reject) {
  52. new MultiIframeLoadMonitor(frames, function() {
  53. loaded = true;
  54. resolve();
  55. }, true);
  56. });
  57. frameParent.appendChild(frames[0]);
  58. frameParent.appendChild(frames[1]);
  59. return Timer.promise(10)
  60. .then(function() {
  61. assertFalse(
  62. 'Monitor should not fire until all iframes have content.',
  63. loaded);
  64. frames[0].src = TEST_FRAME_SRCS[0];
  65. return Timer.promise(10);
  66. })
  67. .then(function() {
  68. assertFalse(
  69. 'Monitor should not fire until all iframes have content.',
  70. loaded);
  71. frames[1].src = TEST_FRAME_SRCS[1];
  72. return monitorPromise;
  73. })
  74. .then(function() { assertTrue(loaded); });
  75. },
  76. testStopMonitoring: function() {
  77. var iframeLoadMonitorsCreated = 0;
  78. var disposeCalls = 0;
  79. // Replace the IframeLoadMonitor implementation with a fake.
  80. function FakeIframeLoadMonitor() {
  81. iframeLoadMonitorsCreated++;
  82. return {
  83. attachEvent: function() {},
  84. dispose: function() { disposeCalls++; },
  85. isLoaded: function() { return false; }
  86. };
  87. }
  88. FakeIframeLoadMonitor.LOAD_EVENT = 'ifload';
  89. stubs.replace(goog.net, 'IframeLoadMonitor', FakeIframeLoadMonitor);
  90. var frames = [dom.createDom(TagName.IFRAME), dom.createDom(TagName.IFRAME)];
  91. var monitor = new MultiIframeLoadMonitor(frames, function() {
  92. fail('should not invoke callback for unloaded frames');
  93. }, true);
  94. assertEquals(frames.length, iframeLoadMonitorsCreated);
  95. assertEquals(0, disposeCalls);
  96. monitor.stopMonitoring();
  97. assertEquals(frames.length, disposeCalls);
  98. }
  99. });