iframemask_test.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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.provide('goog.ui.IframeMaskTest');
  15. goog.setTestOnly('goog.ui.IframeMaskTest');
  16. goog.require('goog.dom');
  17. goog.require('goog.dom.TagName');
  18. goog.require('goog.dom.iframe');
  19. goog.require('goog.structs.Pool');
  20. goog.require('goog.style');
  21. goog.require('goog.testing.MockClock');
  22. goog.require('goog.testing.StrictMock');
  23. goog.require('goog.testing.jsunit');
  24. goog.require('goog.ui.IframeMask');
  25. goog.require('goog.ui.Popup');
  26. goog.require('goog.ui.PopupBase');
  27. goog.require('goog.userAgent');
  28. var iframeMask;
  29. var mockClock;
  30. function setUp() {
  31. goog.dom.getElement('sandbox').innerHTML = '<div id="popup"></div>';
  32. mockClock = new goog.testing.MockClock(true);
  33. iframeMask = new goog.ui.IframeMask();
  34. }
  35. function tearDown() {
  36. iframeMask.dispose();
  37. mockClock.dispose();
  38. assertNoIframes();
  39. }
  40. function findOneAndOnlyIframe() {
  41. var iframes = goog.dom.getElementsByTagName(goog.dom.TagName.IFRAME);
  42. assertEquals(
  43. 'There should be exactly 1 iframe in the document', 1, iframes.length);
  44. return iframes[0];
  45. }
  46. function assertNoIframes() {
  47. assertEquals(
  48. 'Expected no iframes in the document', 0,
  49. goog.dom.getElementsByTagNameAndClass(goog.dom.TagName.IFRAME).length);
  50. }
  51. function testApplyFullScreenMask() {
  52. iframeMask.applyMask();
  53. var iframe = findOneAndOnlyIframe();
  54. assertEquals('block', iframe.style.display);
  55. assertEquals('absolute', iframe.style.position);
  56. // coerce zindex to a string
  57. assertEquals('1', iframe.style.zIndex + '');
  58. iframeMask.hideMask();
  59. assertEquals('none', iframe.style.display);
  60. }
  61. function testApplyOpacity() {
  62. iframeMask.setOpacity(0.3);
  63. iframeMask.applyMask();
  64. if (goog.userAgent.IE && !goog.userAgent.isDocumentModeOrHigher(9)) {
  65. assertContains(
  66. 'Expected opactity to be set in the CSS style', '30',
  67. findOneAndOnlyIframe().style.cssText);
  68. } else {
  69. assertContains(
  70. 'Expected opactity to be set in the CSS style', '0.3',
  71. findOneAndOnlyIframe().style.cssText);
  72. }
  73. }
  74. function testApplyZIndex() {
  75. iframeMask.setZIndex(5);
  76. iframeMask.applyMask();
  77. // coerce zindex to a string
  78. assertEquals('5', findOneAndOnlyIframe().style.zIndex + '');
  79. }
  80. function testSnapElement() {
  81. iframeMask.setSnapElement(goog.dom.getElement('popup'));
  82. iframeMask.applyMask();
  83. var iframe = findOneAndOnlyIframe();
  84. var bounds = goog.style.getBounds(iframe);
  85. assertEquals(100, bounds.left);
  86. assertEquals(900, bounds.top);
  87. assertEquals(300, bounds.width);
  88. assertEquals(400, bounds.height);
  89. iframeMask.setSnapElement(document.documentElement);
  90. // Make sure that snapping to a different element changes the bounds.
  91. assertNotEquals(
  92. 'Snap element not updated', 400, goog.style.getBounds(iframe).height);
  93. }
  94. function testAttachToPopup() {
  95. var popup = new goog.ui.Popup(goog.dom.getElement('popup'));
  96. iframeMask.listenOnTarget(
  97. popup, goog.ui.PopupBase.EventType.SHOW, goog.ui.PopupBase.EventType.HIDE,
  98. goog.dom.getElement('popup'));
  99. assertNoIframes();
  100. popup.setVisible(true);
  101. assertNoIframes();
  102. // Tick because the showing of the iframe mask happens asynchronously.
  103. // (Otherwise the handling of the mousedown can take so long that a bounce
  104. // occurs).
  105. mockClock.tick(1);
  106. var iframe = findOneAndOnlyIframe();
  107. var bounds = goog.style.getBounds(iframe);
  108. assertEquals(300, bounds.width);
  109. assertEquals(400, bounds.height);
  110. assertEquals('block', iframe.style.display);
  111. popup.setVisible(false);
  112. assertEquals('none', iframe.style.display);
  113. }
  114. function testQuickHidingPopup() {
  115. var popup = new goog.ui.Popup(goog.dom.getElement('popup'));
  116. iframeMask.listenOnTarget(
  117. popup, goog.ui.PopupBase.EventType.SHOW,
  118. goog.ui.PopupBase.EventType.HIDE);
  119. assertNoIframes();
  120. popup.setVisible(true);
  121. assertNoIframes();
  122. popup.setVisible(false);
  123. assertNoIframes();
  124. // Tick because the showing of the iframe mask happens asynchronously.
  125. // (Otherwise the handling of the mousedown can take so long that a bounce
  126. // occurs).
  127. mockClock.tick(1);
  128. assertNoIframes();
  129. }
  130. function testRemoveHandlers() {
  131. var popup = new goog.ui.Popup(goog.dom.getElement('popup'));
  132. iframeMask.listenOnTarget(
  133. popup, goog.ui.PopupBase.EventType.SHOW,
  134. goog.ui.PopupBase.EventType.HIDE);
  135. iframeMask.removeHandlers();
  136. popup.setVisible(true);
  137. // Tick because the showing of the iframe mask happens asynchronously.
  138. // (Otherwise the handling of the mousedown can take so long that a bounce
  139. // occurs).
  140. mockClock.tick(1);
  141. assertNoIframes();
  142. }
  143. function testIframePool() {
  144. var iframe = goog.dom.iframe.createBlank(goog.dom.getDomHelper());
  145. var mockPool = new goog.testing.StrictMock(goog.structs.Pool);
  146. mockPool.getObject();
  147. mockPool.$returns(iframe);
  148. mockPool.$replay();
  149. iframeMask.dispose();
  150. // Create a new iframe mask with a pool, and verify that it checks
  151. // its iframe out of the pool instead of creating one.
  152. iframeMask = new goog.ui.IframeMask(null, mockPool);
  153. iframeMask.applyMask();
  154. mockPool.$verify();
  155. findOneAndOnlyIframe();
  156. mockPool.$reset();
  157. mockPool.releaseObject(iframe);
  158. mockPool.$replay();
  159. // When the iframe mask has a pool, the pool is responsible for
  160. // removing the iframe from the DOM.
  161. iframeMask.hideMask();
  162. mockPool.$verify();
  163. findOneAndOnlyIframe();
  164. // And showing the iframe again should check it out of the pool again.
  165. mockPool.$reset();
  166. mockPool.getObject();
  167. mockPool.$returns(iframe);
  168. mockPool.$replay();
  169. iframeMask.applyMask();
  170. mockPool.$verify();
  171. // When the test is over, the iframe mask should be disposed. Make sure
  172. // that the pool removes the iframe from the page.
  173. mockPool.$reset();
  174. mockPool.releaseObject(iframe);
  175. mockPool.$does(function() { goog.dom.removeNode(iframe); });
  176. mockPool.$replay();
  177. }