iframeloadmonitor_test.js 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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.IframeLoadMonitorTest');
  15. goog.setTestOnly('goog.net.IframeLoadMonitorTest');
  16. var IframeLoadMonitor = goog.require('goog.net.IframeLoadMonitor');
  17. var Promise = goog.require('goog.Promise');
  18. var TagName = goog.require('goog.dom.TagName');
  19. var Timer = goog.require('goog.Timer');
  20. var dom = goog.require('goog.dom');
  21. var events = goog.require('goog.events');
  22. var testSuite = goog.require('goog.testing.testSuite');
  23. var TEST_FRAME_SRC = 'iframeloadmonitor_test_frame.html';
  24. var frameParent;
  25. testSuite({
  26. setUpPage: function() { frameParent = dom.getElement('frame_parent'); },
  27. tearDown: function() { dom.removeChildren(frameParent); },
  28. testIframeLoadMonitor: function() {
  29. var frame = dom.createDom(TagName.IFRAME);
  30. var monitor = new IframeLoadMonitor(frame);
  31. var monitorPromise = new Promise(function(resolve, reject) {
  32. events.listen(monitor, IframeLoadMonitor.LOAD_EVENT, resolve);
  33. });
  34. assertFalse(monitor.isLoaded());
  35. frameParent.appendChild(frame);
  36. return monitorPromise.then(function(e) {
  37. assertEquals(IframeLoadMonitor.LOAD_EVENT, e.type);
  38. assertTrue(monitor.isLoaded());
  39. });
  40. },
  41. testIframeLoadMonitor_withContentCheck: function() {
  42. var frame = dom.createDom(TagName.IFRAME);
  43. var monitor = new IframeLoadMonitor(frame, true);
  44. var monitorPromise = new Promise(function(resolve, reject) {
  45. events.listen(monitor, IframeLoadMonitor.LOAD_EVENT, resolve);
  46. });
  47. assertFalse(monitor.isLoaded());
  48. frameParent.appendChild(frame);
  49. return Timer.promise(10)
  50. .then(function() {
  51. assertFalse(
  52. 'Monitor should not fire before content has loaded.',
  53. monitor.isLoaded());
  54. frame.src = TEST_FRAME_SRC;
  55. return monitorPromise;
  56. })
  57. .then(function(e) {
  58. assertEquals(IframeLoadMonitor.LOAD_EVENT, e.type);
  59. assertTrue(monitor.isLoaded());
  60. });
  61. }
  62. });