html2canvas/tests/qunit/unit/css.js

150 lines
4.4 KiB
JavaScript
Raw Normal View History

2012-03-04 19:02:03 +04:00
// declare vars (preventing JSHint messages)
2012-03-04 23:16:18 +04:00
/* this breaks the testing for IE<9, haven't really looked into why
2012-03-04 19:02:03 +04:00
var test = test || function(){},
QUnit = QUnit || {},
_html2canvas = _html2canvas || {};
2012-03-04 23:16:18 +04:00
*/
2012-03-04 19:02:03 +04:00
2012-03-03 18:38:05 +04:00
module("CSS");
2012-03-04 20:59:00 +04:00
$(function() {
var propsToTest = {},
numDivs = {};
2012-03-03 18:38:05 +04:00
2012-03-04 20:59:00 +04:00
propsToTest['border-width'] = ["borderTopWidth", "borderRightWidth", "borderBottomWidth", "borderLeftWidth"],
numDivs['border-width'] = $('#borders div').length;
2012-03-03 18:38:05 +04:00
/*
expecting = [
// #1
"1px",
"0px",
"1px",
"0px",
// #2
"16px",
"0px",
"16px",
"0px",
// #3
"1px",
"3px",
"5px",
"3px",
// #4
"3px",
"3px",
"3px",
"3px",
// #5
"80px",
"35px",
"480px",
"188px",
// #6
"3904px",
"3500px",
"2944px",
"2513px",
// #7
"18px",
"6px",
"80px",
"5px",
// #8
"1889px",
"666px",
"3904px",
"500px"
];
*/
2012-03-04 20:59:00 +04:00
test('border-width', propsToTest['border-width'].length * numDivs['border-width'], function() {
2012-03-03 18:38:05 +04:00
$('#borders div').each(function(i, el) {
2012-03-04 20:59:00 +04:00
$.each(propsToTest['border-width'], function(s, prop) {
2012-03-03 18:38:05 +04:00
var expect = $(el).css(prop);
// older IE's don't necessarily return px even with jQuery
if (expect === "thin") {
expect = "1px";
} else if (expect === "medium") {
expect = "3px";
} else if (expect === "thick") {
expect = "5px";
}
QUnit.equal( _html2canvas.Util.getCSS(el, prop), expect, "div #" + (i + 1) + " property " + prop + " equals " + expect );
2012-03-03 18:38:05 +04:00
});
});
});
2012-03-04 20:59:00 +04:00
propsToTest['padding width'] = ["paddingTop", "paddingRight", "paddingBottom", "paddingLeft"];
numDivs['padding width'] = $('#padding div').length;
2012-03-03 18:38:05 +04:00
2012-03-04 20:59:00 +04:00
test('padding width', propsToTest['padding width'].length * numDivs['padding width'] * 2, function() {
2012-03-03 18:38:05 +04:00
$('#padding div').each(function(i, el) {
2012-03-04 20:59:00 +04:00
$.each(propsToTest['padding width'], function(s, prop) {
2012-03-03 18:56:31 +04:00
var isPx = _html2canvas.Util.getCSS(el, prop).indexOf("px");
QUnit.notEqual( isPx, -1, "div #" + (i + 1) + " property " + prop + " is in pixels" );
2012-03-03 18:38:05 +04:00
QUnit.equal( _html2canvas.Util.getCSS(el, prop), $(el).css(prop), "div #" + (i + 1) + " property " + prop + " equals " + $(el).css(prop) );
});
});
2012-03-04 20:59:00 +04:00
});
2012-03-03 18:38:05 +04:00
2012-03-04 20:59:00 +04:00
propsToTest['background-position'] = ["backgroundPosition"];
numDivs['background-position'] = $('#backgroundPosition div').length;
2012-03-03 18:38:05 +04:00
2012-03-04 20:59:00 +04:00
test('background-position', propsToTest['background-position'].length * numDivs['background-position'] * 2, function() {
2012-03-03 18:38:05 +04:00
$('#backgroundPosition div').each(function(i, el) {
2012-03-04 20:59:00 +04:00
$.each(propsToTest['background-position'], function(s, prop) {
2012-03-03 18:38:05 +04:00
var img = new Image();
img.width = 50;
img.height = 50;
2012-03-03 18:38:05 +04:00
var item = _html2canvas.Util.getCSS(el, prop),
pos = _html2canvas.Util.BackgroundPosition(el, _html2canvas.Util.Bounds(el), img),
split;
if ( window.getComputedStyle ) {
split = $(el).css(prop).split(" ");
} else {
2012-03-04 19:02:03 +04:00
split = [$(el).css(prop+"X"),$(el).css(prop+"Y")];
}
2012-03-03 18:38:05 +04:00
var testEl = $('<div />').css({
'position': 'absolute',
'left': split[0],
'top': split[1]
});
testEl.appendTo(el);
QUnit.equal( pos.left, Math.round(parseFloat(testEl.css('left'), 10)), "div #" + (i + 1) + " background-position-x equals " + pos.left + " from " + item );
QUnit.equal( pos.top, Math.round(parseFloat(testEl.css('top'), 10)), "div #" + (i + 1) + " background-position-y equals " + pos.top );
2012-03-03 18:38:05 +04:00
testEl.remove();
});
});
});
// TODO add backgroundPosition % tests
2012-03-04 20:59:00 +04:00
2012-03-03 18:38:05 +04:00
});