forked from niklasvh/html2canvas
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow multiple renders to occur simultaneously
- Loading branch information
Niklas von Hertzen
committed
Dec 24, 2014
1 parent
60368d4
commit 2f5f9f6
Showing
4 changed files
with
107 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
<html> | ||
<head> | ||
<meta charset="utf-8"> | ||
<title>Mocha Tests</title> | ||
<link rel="stylesheet" href="lib/mocha.css" /> | ||
<script src="../../dist/html2canvas.js"></script> | ||
<script src="../assets/jquery-1.6.2.js"></script> | ||
<script src="lib/expect.js"></script> | ||
<script src="lib/mocha.js"></script> | ||
<style> | ||
#block { | ||
width: 200px; | ||
height: 200px; | ||
} | ||
#green-block { | ||
width: 200px; | ||
height: 200px; | ||
background: green; | ||
} | ||
</style> | ||
</head> | ||
<body> | ||
<div id="mocha"></div> | ||
<div id="block"></div> | ||
<div id="green-block"></div> | ||
<script>mocha.setup('bdd')</script> | ||
<script> | ||
describe("Multiple renders", function() { | ||
it("render correctly", function(done) { | ||
var d = 0; | ||
for (var i = 0; i < 10; i++) { | ||
html2canvas(document.querySelector('#green-block')).then(function (canvas) { | ||
expect(canvas.width).to.equal(200); | ||
expect(canvas.height).to.equal(200); | ||
validCanvasPixels(canvas); | ||
canvas.width = canvas.height = 10; | ||
d++; | ||
if (d === 10) { | ||
done(); | ||
} | ||
}); | ||
} | ||
}); | ||
|
||
it("render correctly when non sequential", function(done) { | ||
var d = 0; | ||
for (var i = 0; i < 10; i++) { | ||
html2canvas(document.querySelector('#block'), {onclone: function(document) { | ||
return new Promise(function(resolve) { | ||
document.querySelector('#block').style.backgroundColor = 'green'; | ||
setTimeout(function() { | ||
resolve(); | ||
}, 100); | ||
}); | ||
}}).then(function (canvas) { | ||
expect(canvas.width).to.equal(200); | ||
expect(canvas.height).to.equal(200); | ||
validCanvasPixels(canvas); | ||
canvas.width = canvas.height = 10; | ||
d++; | ||
if (d === 10) { | ||
done(); | ||
} | ||
}); | ||
} | ||
}); | ||
}); | ||
|
||
function validCanvasPixels(canvas) { | ||
var ctx = canvas.getContext("2d"); | ||
var data = ctx.getImageData(0, 0, canvas.width, canvas.height).data; | ||
for (var i = 0, len = data.length; i < len; i+=4) { | ||
if (data[i] !== 0 || data[i+1] !== 128 || data[i+2] !== 0 || data[i+3] !== 255) { | ||
expect().fail("Invalid canvas data"); | ||
} | ||
} | ||
} | ||
|
||
mocha.checkLeaks(); | ||
mocha.globals(['jQuery']); | ||
if (window.mochaPhantomJS) { | ||
mochaPhantomJS.run(); | ||
} | ||
else { | ||
mocha.run(); | ||
} | ||
</script> | ||
</body> | ||
</html> |