mirror of
https://github.com/niklasvh/html2canvas.git
synced 2023-08-10 21:13:10 +03:00
"tests: splitted Generate from CSS; if not supported pass test"
This commit is contained in:
parent
f47f9025b7
commit
e82d703288
@ -22,6 +22,7 @@
|
||||
<script type="text/javascript" src="../../src/renderers/Canvas.js"></script>
|
||||
|
||||
<script src="unit/css.js"></script>
|
||||
<script src="unit/generate.js"></script>
|
||||
|
||||
|
||||
<style>
|
||||
|
@ -145,45 +145,5 @@ $(function() {
|
||||
});
|
||||
|
||||
// TODO add backgroundPosition % tests
|
||||
|
||||
propsToTest['background-gradient'] = ["backgroundImage"];
|
||||
numDivs['background-gradient'] = $('#backgroundGradients div').length;
|
||||
|
||||
test('background-gradient', propsToTest['background-gradient'].length * numDivs['background-gradient'], function() {
|
||||
|
||||
$('#backgroundGradients div').each(function(i, el) {
|
||||
$.each(propsToTest['background-gradient'], function(s, prop) {
|
||||
var src, img, canvas, ctx, id, data, len, red, green, blue, overallColor = 0;
|
||||
|
||||
src = _html2canvas.Util.getCSS(el, prop),
|
||||
img = _html2canvas.Generate.Gradient(src, {
|
||||
width: 50,
|
||||
height: 50
|
||||
});
|
||||
|
||||
canvas = document.createElement('canvas');
|
||||
canvas.width = 50;
|
||||
canvas.height = 50;
|
||||
ctx = canvas.getContext('2d');
|
||||
ctx.drawImage(img, 0, 0);
|
||||
id = ctx.getImageData(0, 0, 50, 50);
|
||||
data = id.data;
|
||||
len = data.length;
|
||||
|
||||
//console.log(img);
|
||||
|
||||
for (var i = 0; i < len; i += 4) {
|
||||
red = data[i]; // red
|
||||
green = data[i + 1]; // green
|
||||
blue = data[i + 2]; // blue
|
||||
// i+3 is alpha (the fourth element)
|
||||
|
||||
overallColor += (red + green + blue) / 3;
|
||||
}
|
||||
overallColor /= (len / 4);
|
||||
|
||||
QUnit.notEqual(overallColor, 255, 'No Background Gradient - CSS was ' + src);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
60
tests/qunit/unit/generate.js
Normal file
60
tests/qunit/unit/generate.js
Normal file
@ -0,0 +1,60 @@
|
||||
// declare vars (preventing JSHint messages)
|
||||
var test = test || function(){},
|
||||
QUnit = QUnit || {},
|
||||
_html2canvas = _html2canvas || {};
|
||||
|
||||
|
||||
module("Generate");
|
||||
$(function() {
|
||||
|
||||
var propsToTest = {},
|
||||
numDivs = {};
|
||||
|
||||
|
||||
propsToTest['Generate.Gradient'] = ["backgroundImage"];
|
||||
numDivs['Generate.Gradient'] = $('#backgroundGradients div').length;
|
||||
|
||||
test('Generate.Gradient', propsToTest['Generate.Gradient'].length * numDivs['Generate.Gradient'], function() {
|
||||
|
||||
$('#backgroundGradients div').each(function(i, el) {
|
||||
$.each(propsToTest['Generate.Gradient'], function(s, prop) {
|
||||
var src, img, canvas, ctx, id, data, len, red, green, blue, overallColor = 0;
|
||||
|
||||
src = _html2canvas.Util.getCSS(el, prop);
|
||||
|
||||
if (/^(-webkit|-o|-moz|-ms|linear)-/.test(src)) {
|
||||
|
||||
img = _html2canvas.Generate.Gradient(src, {
|
||||
width: 50,
|
||||
height: 50
|
||||
});
|
||||
|
||||
canvas = document.createElement('canvas');
|
||||
canvas.width = 50;
|
||||
canvas.height = 50;
|
||||
ctx = canvas.getContext('2d');
|
||||
ctx.drawImage(img, 0, 0);
|
||||
id = ctx.getImageData(0, 0, 50, 50);
|
||||
data = id.data;
|
||||
len = data.length;
|
||||
|
||||
//console.log(img);
|
||||
|
||||
for (var i = 0; i < len; i += 4) {
|
||||
red = data[i]; // red
|
||||
green = data[i + 1]; // green
|
||||
blue = data[i + 2]; // blue
|
||||
// i+3 is alpha (the fourth element)
|
||||
|
||||
overallColor += (red + green + blue) / 3;
|
||||
}
|
||||
overallColor /= (len / 4);
|
||||
|
||||
QUnit.notEqual(overallColor, 255, 'Background Gradient drawn with CSS: ' + src);
|
||||
} else {
|
||||
QUnit.ok(true, 'No CSS Background Gradient support');
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
Loading…
Reference in New Issue
Block a user