aboutsummaryrefslogtreecommitdiff
path: root/contexts/data/lib/closure-library/closure/goog/net/multiiframeloadmonitor_test.html
blob: 7b7696dd9210057aabd0db20ff2943196583332e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
<!DOCTYPE html>
<html>
<!--
Copyright 2008 The Closure Library Authors. All Rights Reserved.

Use of this source code is governed by the Apache License, Version 2.0.
See the COPYING file for details.
-->
<!--
-->
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
  <title>Closure Unit Tests - goog.net.MultiIframeLoadMonitor</title>
  <script src="../base.js"></script>
  <script>
    goog.require('goog.dom');
    goog.require('goog.events');
    goog.require('goog.net.MultiIframeLoadMonitor');
    goog.require('goog.testing.AsyncTestCase');
    goog.require('goog.testing.jsunit');
  </script>
</head>
<body>
<div id="frame_parent"></div>
<script>
  var TEST_FRAME_SRCS = ['iframeloadmonitor_test_frame.html',
      'iframeloadmonitor_test_frame2.html',
      'iframeloadmonitor_test_frame3.html'];

  // Create a new test case.
  var iframeLoaderTestCase = new goog.testing.AsyncTestCase(document.title);
  iframeLoaderTestCase.stepTimeout = 4 * 1000;

  // How many multpile frames finished loading
  iframeLoaderTestCase.multipleComplete_ = 0;

  iframeLoaderTestCase.numMonitors = 0;
  iframeLoaderTestCase.disposeCalled = 0;

  /** Sets up the test environment, adds tests and sets up the worker pools. */
  iframeLoaderTestCase.setUpPage = function() {
    this.log('Setting tests up');
    iframeLoaderTestCase.waitForAsync('loading iframes');

    var dom = goog.dom.getDomHelper();

    // Load multiple with callback
    var frame1 = dom.createDom('iframe');
    var frame2 = dom.createDom('iframe');
    var multiMonitor = new goog.net.MultiIframeLoadMonitor(
      [frame1, frame2], goog.bind(this.multipleCallback, this));
    this.log('Loading frames at: ' + TEST_FRAME_SRCS[0] + ' and '
        + TEST_FRAME_SRCS[1]);
    // Make sure they don't look loaded yet.
    assertEquals(0, this.multipleComplete_);
    var frameParent = dom.getElement('frame_parent');
    dom.appendChild(frameParent, frame1);
    frame1.src = TEST_FRAME_SRCS[0];
    dom.appendChild(frameParent, frame2);
    frame2.src = TEST_FRAME_SRCS[1];

    // Load multiple with callback and content check
    var frame3 = dom.createDom('iframe');
    var frame4 = dom.createDom('iframe');
    var multiMonitor = new goog.net.MultiIframeLoadMonitor(
      [frame3, frame4], goog.bind(this.multipleContentCallback, this), true);
    this.log('Loading frames with content check at: ' + TEST_FRAME_SRCS[1] +
        ' and ' + TEST_FRAME_SRCS[2]);
    dom.appendChild(frameParent, frame3);
    frame3.src = TEST_FRAME_SRCS[1];
    dom.appendChild(frameParent, frame4);
    frame4.src = TEST_FRAME_SRCS[2];

    this.add(new goog.testing.TestCase.Test(
        'test results', this.testResults, this));
    this.add(new goog.testing.TestCase.Test(
        'stopMonitoring', this.testStop, this));
  };


  /** Callback for the multiple frame load test case */
  iframeLoaderTestCase.multipleCallback = function() {
    this.log('multiple frames finished loading');
    this.multipleComplete_++;
    this.multipleCompleteNoContent_ = true;
    this.callbacksComplete();
  };

  /** Callback for the multiple frame with content load test case */
  iframeLoaderTestCase.multipleContentCallback = function() {
    this.log('multiple frames with content finished loading');
    this.multipleComplete_++;
    this.multipleCompleteContent_ = true;
    this.callbacksComplete();
  };

  /** Checks if all the load callbacks are done*/
  iframeLoaderTestCase.callbacksComplete = function() {
    if (this.multipleComplete_ == 2) {
      iframeLoaderTestCase.continueTesting();
    }
  }

  /** Tests the results. */
  iframeLoaderTestCase.testResults = function() {
    this.log('getting test results');
    assertTrue(this.multipleCompleteNoContent_);
    assertTrue(this.multipleCompleteContent_);
  };

  iframeLoaderTestCase.fakeLoadMonitor = function() {
    // Replaces IframeLoadMonitor with a fake version that just tracks
    // instantiations/disposals
    this.loadMonitorConstructor = goog.net.IframeLoadMonitor;
    var that = this;
    goog.net.IframeLoadMonitor = function() {
      that.numMonitors++;
      return {
        isLoaded: function() { return false; },
        dispose: function() { that.disposeCalled++; },
        attachEvent: function() {}
      };
    }
    goog.net.IframeLoadMonitor.LOAD_EVENT = 'ifload';
  };

  iframeLoaderTestCase.unfakeLoadMonitor = function() {
    goog.net.IframeLoadMonitor = this.loadMonitorConstructor;
  };


  iframeLoaderTestCase.testStop = function() {
    // create two unloaded frames, make sure that load monitors are loaded
    // behind the scenes, then make sure they are disposed properly.
    this.fakeLoadMonitor();
    var dom = goog.dom.getDomHelper();
    var frames = [dom.createDom('iframe'), dom.createDom('iframe')];
    var multiMonitor = new goog.net.MultiIframeLoadMonitor(
      frames,
      function() {
        fail("should not invoke callback for unloaded rames"); 
      });
    assertEquals(frames.length, this.numMonitors);
    assertEquals(0, this.disposeCalled);
    multiMonitor.stopMonitoring();
    assertEquals(frames.length, this.disposeCalled);
    this.unfakeLoadMonitor();
  };

  /** Used by the JsUnit test runner. */
  function testResults() {
    iframeLoaderTestCase.testResults();
  }

  /** Used by the JsUnit test runner. */
  function testDispose() {
    iframeLoaderTestCase.testDispose();
  }

  /** Used by the JsUnit test runner. */
  function setUpPage() {
    iframeLoaderTestCase.runTests();
  }

  /** Standalone Closure Test Runner. */
  if (typeof G_testRunner != 'undefined') {
    G_testRunner.initialize(iframeLoaderTestCase);
  }

</script>
</body>
</html>