00f7461c82239c936b8e7d89c9d9c80e389c120b.svn-base 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <html>
  2. <head>
  3. <meta charset="utf-8">
  4. <title>Mocha Tests</title>
  5. <link rel="stylesheet" href="lib/mocha.css" />
  6. <script src="../../node_modules/bluebird/js/browser/bluebird.js"></script>
  7. <script src="../../dist/html2canvas.js"></script>
  8. <script src="../assets/jquery-1.6.2.js"></script>
  9. <script src="lib/expect.js"></script>
  10. <script src="lib/mocha.js"></script>
  11. </head>
  12. <body>
  13. <div id="mocha"></div>
  14. <script>mocha.setup('bdd')</script>
  15. <div style="background: green; width: 40px; height:40px;" id="block"></div>
  16. <div style="visibility: hidden">
  17. <iframe src="iframe1.htm" style="height: 350px; width: 450px; border: 0;" scrolling="no" id="frame1"></iframe>
  18. <iframe src="iframe2.htm" style="height: 350px; width: 450px; border: 0;" scrolling="yes" id="frame2"></iframe>
  19. </div>
  20. <script>
  21. describe("Cropping", function() {
  22. it("window view with body", function(done) {
  23. html2canvas(document.body, {type: 'view'}).then(function(canvas) {
  24. expect(canvas.width).to.equal(window.innerWidth);
  25. expect(canvas.height).to.equal(window.innerHeight);
  26. done();
  27. }).catch(function(error) {
  28. done(error);
  29. });
  30. });
  31. it("window view with documentElement", function(done) {
  32. html2canvas(document.documentElement, {type: 'view'}).then(function(canvas) {
  33. expect(canvas.width).to.equal(window.innerWidth);
  34. expect(canvas.height).to.equal(window.innerHeight);
  35. done();
  36. }).catch(function(error) {
  37. done(error);
  38. });
  39. });
  40. it("iframe with body", function(done) {
  41. html2canvas(document.querySelector("#frame1").contentWindow.document.body, {type: 'view'}).then(function(canvas) {
  42. expect(canvas.width).to.equal(450);
  43. expect(canvas.height).to.equal(350);
  44. validCanvasPixels(canvas);
  45. done();
  46. }).catch(function(error) {
  47. done(error);
  48. });
  49. });
  50. it("iframe with document element", function(done) {
  51. html2canvas(document.querySelector("#frame1").contentWindow.document.documentElement, {type: 'view'}).then(function(canvas) {
  52. expect(canvas.width).to.equal(450);
  53. expect(canvas.height).to.equal(350);
  54. validCanvasPixels(canvas);
  55. done();
  56. }).catch(function(error) {
  57. done(error);
  58. });
  59. });
  60. it("with node", function(done) {
  61. html2canvas(document.querySelector("#block")).then(function(canvas) {
  62. expect(canvas.width).to.equal(40);
  63. expect(canvas.height).to.equal(40);
  64. validCanvasPixels(canvas);
  65. done();
  66. }).catch(function(error) {
  67. done(error);
  68. });
  69. });
  70. it("with node and size", function(done) {
  71. html2canvas(document.querySelector("#block"), {width: 20, height: 20}).then(function(canvas) {
  72. expect(canvas.width).to.equal(20);
  73. expect(canvas.height).to.equal(20);
  74. validCanvasPixels(canvas);
  75. done();
  76. }).catch(function(error) {
  77. done(error);
  78. });
  79. });
  80. document.querySelector("#frame2").addEventListener("load", function() {
  81. document.querySelector("#frame2").contentWindow.scrollTo(0, 350);
  82. describe("with scrolled content", function() {
  83. it("iframe with body", function(done) {
  84. html2canvas(document.querySelector("#frame2").contentWindow.document.body, {type: 'view'}).then(function(canvas) {
  85. // phantomjs issue https://github.com/ariya/phantomjs/issues/10581
  86. if (canvas.height !== 1200) {
  87. expect(canvas.width).to.equal(450);
  88. expect(canvas.height).to.equal(350);
  89. validCanvasPixels(canvas);
  90. }
  91. done();
  92. }).catch(function(error) {
  93. done(error);
  94. });
  95. });
  96. it("iframe with document element", function(done) {
  97. html2canvas(document.querySelector("#frame2").contentWindow.document.documentElement, {type: 'view'}).then(function(canvas) {
  98. // phantomjs issue https://github.com/ariya/phantomjs/issues/10581
  99. if (canvas.height !== 1200) {
  100. expect(canvas.width).to.equal(450);
  101. expect(canvas.height).to.equal(350);
  102. validCanvasPixels(canvas);
  103. }
  104. done();
  105. }).catch(function(error) {
  106. done(error);
  107. });
  108. });
  109. });
  110. }, false);
  111. });
  112. function validCanvasPixels(canvas) {
  113. var ctx = canvas.getContext("2d");
  114. var data = ctx.getImageData(0, 0, canvas.width, canvas.height).data;
  115. for (var i = 0, len = data.length; i < len; i+=4) {
  116. if (data[i] !== 0 || data[i+1] !== 128 || data[i+2] !== 0 || data[i+3] !== 255) {
  117. expect().fail("Invalid canvas data");
  118. }
  119. }
  120. }
  121. mocha.checkLeaks();
  122. mocha.globals(['jQuery']);
  123. if (window.mochaPhantomJS) {
  124. mochaPhantomJS.run();
  125. }
  126. else {
  127. mocha.run();
  128. }
  129. mocha.suite.afterAll(function() {
  130. document.body.setAttribute('data-complete', 'true');
  131. });
  132. </script>
  133. </body>
  134. </html>