UNPKG

sku-pg

Version:

Skulpt is a Javascript implementation of Python 2.x. Python that runs in your browser!

231 lines (185 loc) 6.19 kB
<!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>