aboutsummaryrefslogtreecommitdiff
path: root/contexts/data/lib/closure-library/closure/goog/ui/iframemask_test.html
blob: eeea52b7137537c62dbc56b5cd0e5d49327db573 (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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
<!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.
-->
<!--

  @author gboyer@google.com (Garrett Boyer)
  @author nicksantos@google.com (Nick Santos) (Ported to Closure)
-->
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>goog.ui.IframeMask Unit Test</title>
<script src="../base.js"></script>
<script>
  goog.require('goog.testing.jsunit');
  goog.require('goog.testing.MockClock');
  goog.require('goog.testing.StrictMock');
  goog.require('goog.ui.IframeMask');
  goog.require('goog.ui.Popup');
  goog.require('goog.structs.Pool');
</script>
<style type="text/css">
#popup {
  position: absolute;
  left: 100px;
  top: 900px; /* so that you can see unit test failures */
  width: 300px;
  height: 400px;
}
</style>
</head>
<body>

<div id="sandbox"></div>

<script>

var iframeMask;
var mockClock;

function setUp() {
  goog.dom.getElement('sandbox').innerHTML = '<div id="popup"></div>';
  mockClock = new goog.testing.MockClock(true);

  iframeMask = new goog.ui.IframeMask();
}

function tearDown() {
  iframeMask.dispose();
  mockClock.dispose();

  assertNoIframes();
}

function findOneAndOnlyIframe() {
  var iframes = document.getElementsByTagName(goog.dom.TagName.IFRAME);
  assertEquals('There should be exactly 1 iframe in the document',
      1, iframes.length);
  return iframes[0];
}

function assertNoIframes() {
  assertEquals('Expected no iframes in the document', 0,
      goog.dom.getElementsByTagNameAndClass(goog.dom.TagName.IFRAME).length);
}

function testApplyFullScreenMask() {
  iframeMask.applyMask();

  var iframe = findOneAndOnlyIframe();
  assertEquals('block', iframe.style.display);
  assertEquals('absolute', iframe.style.position);

  // coerce zindex to a string
  assertEquals('1', iframe.style.zIndex + '');

  iframeMask.hideMask();
  assertEquals('none', iframe.style.display);
}

function testApplyOpacity() {
  iframeMask.setOpacity(0.3);
  iframeMask.applyMask();

  if (goog.userAgent.IE) {
    assertContains('Expected opactity to be set in the CSS style',
        '30', findOneAndOnlyIframe().style.cssText);
  } else {
    assertContains('Expected opactity to be set in the CSS style',
        '0.3', findOneAndOnlyIframe().style.cssText);
  }
}

function testApplyZIndex() {
  iframeMask.setZIndex(5);
  iframeMask.applyMask();

  // coerce zindex to a string
  assertEquals('5', findOneAndOnlyIframe().style.zIndex + '');
}

function testSnapElement() {
  iframeMask.setSnapElement(goog.dom.getElement('popup'));
  iframeMask.applyMask();

  var iframe = findOneAndOnlyIframe();
  var bounds = goog.style.getBounds(iframe);
  assertEquals(100, bounds.left);
  assertEquals(900, bounds.top);
  assertEquals(300, bounds.width);
  assertEquals(400, bounds.height);

  iframeMask.setSnapElement(document.documentElement);

  // Make sure that snapping to a different element changes the bounds.
  assertNotEquals('Snap element not updated',
      400, goog.style.getBounds(iframe).height);
}

function testAttachToPopup() {
  var popup = new goog.ui.Popup(goog.dom.getElement('popup'));
  iframeMask.listenOnTarget(popup, goog.ui.PopupBase.EventType.SHOW,
      goog.ui.PopupBase.EventType.HIDE, goog.dom.getElement('popup'));

  assertNoIframes();
  popup.setVisible(true);
  assertNoIframes();

  // Tick because the showing of the iframe mask happens asynchronously.
  // (Otherwise the handling of the mousedown can take so long that a bounce
  // occurs).
  mockClock.tick(1);

  var iframe = findOneAndOnlyIframe();
  var bounds = goog.style.getBounds(iframe);
  assertEquals(300, bounds.width);
  assertEquals(400, bounds.height);
  assertEquals('block', iframe.style.display);

  popup.setVisible(false);
  assertEquals('none', iframe.style.display);
}

function testQuickHidingPopup() {
  var popup = new goog.ui.Popup(goog.dom.getElement('popup'));
  iframeMask.listenOnTarget(popup, goog.ui.PopupBase.EventType.SHOW,
      goog.ui.PopupBase.EventType.HIDE);

  assertNoIframes();
  popup.setVisible(true);
  assertNoIframes();
  popup.setVisible(false);
  assertNoIframes();

  // Tick because the showing of the iframe mask happens asynchronously.
  // (Otherwise the handling of the mousedown can take so long that a bounce
  // occurs).
  mockClock.tick(1);
  assertNoIframes();
}

function testRemoveHandlers() {
  var popup = new goog.ui.Popup(goog.dom.getElement('popup'));
  iframeMask.listenOnTarget(popup, goog.ui.PopupBase.EventType.SHOW,
      goog.ui.PopupBase.EventType.HIDE);
  iframeMask.removeHandlers();
  popup.setVisible(true);

  // Tick because the showing of the iframe mask happens asynchronously.
  // (Otherwise the handling of the mousedown can take so long that a bounce
  // occurs).
  mockClock.tick(1);
  assertNoIframes();
}

function testIframePool() {
  var iframe = goog.dom.iframe.createBlank(goog.dom.getDomHelper());
  var mockPool = new goog.testing.StrictMock(goog.structs.Pool);
  mockPool.getObject();
  mockPool.$returns(iframe);

  mockPool.$replay();

  iframeMask.dispose();

  // Create a new iframe mask with a pool, and verify that it checks
  // its iframe out of the pool instead of creating one.
  iframeMask = new goog.ui.IframeMask(null, mockPool);
  iframeMask.applyMask();
  mockPool.$verify();
  findOneAndOnlyIframe();

  mockPool.$reset();
  
  mockPool.releaseObject(iframe);
  mockPool.$replay();

  // When the iframe mask has a pool, the pool is responsible for
  // removing the iframe from the DOM.
  iframeMask.hideMask();
  mockPool.$verify();
  findOneAndOnlyIframe()

  // And showing the iframe again should check it out of the pool again.
  mockPool.$reset();
  mockPool.getObject();
  mockPool.$returns(iframe);
  mockPool.$replay();
  
  iframeMask.applyMask();
  mockPool.$verify();

  // When the test is over, the iframe mask should be disposed. Make sure
  // that the pool removes the iframe from the page.
  mockPool.$reset();
  mockPool.releaseObject(iframe);
  mockPool.$does(function() {
    goog.dom.removeNode(iframe);
  });
  mockPool.$replay();
}

</script>

</body>
</html>