From 085da1ef0dddcc02604de16773f86ade6a567ef5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?"J.=20Rene=CC=81e=20Beach"?= Date: Wed, 20 Mar 2013 01:04:31 -0400 Subject: [PATCH] Drupal.displace tests MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: J. Renée Beach --- testswarm_tests/tests/core/drupal.displace.test.js | 159 ++++++++++++++++++++ testswarm_tests/testswarm_tests.module | 9 ++ 2 files changed, 168 insertions(+) create mode 100644 testswarm_tests/tests/core/drupal.displace.test.js diff --git a/testswarm_tests/tests/core/drupal.displace.test.js b/testswarm_tests/tests/core/drupal.displace.test.js new file mode 100644 index 0000000..67b5a21 --- /dev/null +++ b/testswarm_tests/tests/core/drupal.displace.test.js @@ -0,0 +1,159 @@ +/*jshint strict:true, browser:true, curly:true, eqeqeq:true, expr:true, forin:true, latedef:true, newcap:true, noarg:true, trailing: true, undef:true, unused:true */ +/*global Drupal: true, jQuery: true, QUnit:true*/ +(function ($, Drupal, window, document, undefined) { + "use strict"; + /** + * Tests Drupal.displace(). + */ + Drupal.tests.displace = { + getInfo: function() { + return { + name: 'Drupal displace utility.', + description: 'Tests for the viewport component displacement utility.', + group: 'System' + }; + }, + setup: function () { + // Append a set of elements to the body that will be used as displacing + // elements. Make sure that they are bigger than the toolbar. + $('
') + // Top. + .append( + $('
') + .css({ + 'background-color': 'red', + 'height': '200px', + 'left': 0, + 'position': 'fixed', + 'right': 0, + 'top': '10px', + 'width': '100%', + 'z-index': 10000 + }) + ) + // Right. + .append( + $('
') + .css({ + 'background-color': 'blue', + 'bottom': 0, + 'height': '100%', + 'position': 'fixed', + 'right': '10px', + 'top': 0, + 'width': '100px', + 'z-index': 10000 + }) + ) + // Bottom. + .append( + $('
') + .css({ + 'background-color': 'yellow', + 'bottom': '45px', + 'height': '100px', + 'left': 0, + 'position': 'fixed', + 'right': 0, + 'width': '100%', + 'z-index': 10000 + }) + ) + // Left. + .append( + $('
') + .css({ + 'background-color': 'orange', + 'bottom': 0, + 'height': '100%', + 'left': '10px', + 'position': 'fixed', + 'top': 0, + 'width': '300px', + 'z-index': 10000 + }) + ) + .appendTo('body'); + // Create a DOM element that will respond to displaced screen elements. + $('
') + .css({ + 'background-color': 'black', + 'top': '0', + 'max-height': '100%', + 'left': '0', + 'opacity': '0.4', + 'position': 'fixed', + 'max-width': '100%', + 'z-index': 10001 + }) + .appendTo('body'); + + // Register a drupalViewportOffsetChange handler on the document. + $(document).on('drupalViewportOffsetChange.testswarm', drupalViewportOffsetChangeHandler) + }, + teardown: function () { + $('#testswarm-displace-test-displacing-container').remove(); + $('#testswarm-displace-test-displaced').remove(); + $(document).off('drupalViewportOffsetChange.testswarm'); + }, + tests: { + displace: function ($, Drupal, window, document, undefined) { + return function() { + QUnit.expect(4); + + // Register the displacing elements. + Drupal.displace.registerElements({ + 'top': $('#testswarm-displace-test-displacing-top'), + 'right': $('#testswarm-displace-test-displacing-right'), + 'bottom': $('#testswarm-displace-test-displacing-bottom'), + 'left': $('#testswarm-displace-test-displacing-left') + }); + + // Trigger a calculation of the displacing elements and a + // drupalViewportOffsetChange event. + Drupal.displace(); + + // Confirm that the displaced element is shifted by the dimensions of + // displacing elements on each edge. + // + // Get the dimensions and placement for the displaced element. + var $displacedElement = $('#testswarm-displace-test-displaced'); + var displacedElementWidth = $displacedElement.outerWidth(); + var displacedElementHeight = $displacedElement.outerHeight(); + var displacedElementTop = $displacedElement.offset().top - window.scrollY; + var displacedElementLeft = $displacedElement.offset().left - window.scrollX; + var displacedElementRight = document.documentElement.clientWidth - (displacedElementLeft + displacedElementWidth); + var displacedElementBottom = document.documentElement.clientHeight - (displacedElementTop + displacedElementHeight); + // Get the dimensions and placement of the displacing elements. + var $displacingElementTop = $('#testswarm-displace-test-displacing-top'); + var displacingElementTop = ($displacingElementTop.offset().top - window.scrollY) + $displacingElementTop.outerHeight(); + var $displacingElementLeft = $('#testswarm-displace-test-displacing-left'); + var displacingElementLeft = ($displacingElementLeft.offset().left - window.scrollX) + $displacingElementLeft.outerWidth(); + var $displacingElementBottom = $('#testswarm-displace-test-displacing-bottom'); + var displacingElementBottom = document.documentElement.clientHeight - ($displacingElementBottom.offset().top - window.scrollY); + var $displacingElementRight = $('#testswarm-displace-test-displacing-right'); + var displacingElementRight = document.documentElement.clientWidth - ($displacingElementRight.offset().left - window.scrollX); + // Verify each edge. + QUnit.equal(displacedElementTop, displacingElementTop, Drupal.t('The top of the displaced element has the expected offset.')); + QUnit.equal(displacedElementRight, displacingElementRight, Drupal.t('The right of the displaced element has the expected offset.')); + QUnit.equal(displacedElementBottom, displacingElementBottom, Drupal.t('The bottom of the displaced element has the expected offset.')); + QUnit.equal(displacedElementLeft, displacingElementLeft, Drupal.t('The left of the displaced element has the expected offset.')); + }; + } + } + }; + + /** + * Responds to the drupalViewportOffsetChange event. + */ + function drupalViewportOffsetChangeHandler (event, offsets) { + $('#testswarm-displace-test-displaced') + .css({ + 'top': offsets.top, + 'right': offsets.right, + 'bottom': offsets.bottom, + 'left': offsets.left + }); + } + +})(jQuery, Drupal, this, this.document); diff --git a/testswarm_tests/testswarm_tests.module b/testswarm_tests/testswarm_tests.module index 8e7ed4e..db3d76a 100644 --- a/testswarm_tests/testswarm_tests.module +++ b/testswarm_tests/testswarm_tests.module @@ -45,6 +45,15 @@ function testswarm_tests_testswarm_tests() { 'path' => 'admin/structure/menu/manage/tools', 'permissions' => array('administer menu') ), + 'displace' => array( + 'module' => 'core', + 'description' => 'Test the Drupal displace utility.', + 'js' => array( + drupal_get_path('module', 'testswarm_tests') . '/tests/core/drupal.displace.test.js' => array(), + ), + 'path' => '', + 'permissions' => array(), + ), 'createpagecontent' => array( 'module' => 'core', 'description' => 'Testing summary on vertical tabs, test needs some more attention.', -- 1.7.10.4