194f922d3f21a37f0c043b980fe98672f8382c26.svn-base 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. <style>
  12. #block {
  13. width: 200px;
  14. height: 200px;
  15. }
  16. #green-block {
  17. width: 200px;
  18. height: 200px;
  19. background: green;
  20. }
  21. #background-block {
  22. width: 200px;
  23. height: 200px;
  24. background: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNgaGD4DwAChAGAJVtEDQAAAABJRU5ErkJggg==) red;
  25. }
  26. #gradient-block {
  27. width: 200px;
  28. height: 200px;
  29. background-image: -webkit-linear-gradient(top, #008000, #008000);
  30. background-image: -moz-linear-gradient(to bottom, #008000, #008000);
  31. background-image: linear-gradient(to bottom, #008000, #008000);
  32. }
  33. </style>
  34. </head>
  35. <body>
  36. <div id="mocha"></div>
  37. <script>mocha.setup('bdd')</script>
  38. <div id="block"></div>
  39. <div id="green-block"></div>
  40. <div id="background-block"></div>
  41. <div id="gradient-block"></div>
  42. <script>
  43. describe("options.background", function() {
  44. it("with hexcolor", function(done) {
  45. html2canvas(document.querySelector("#block"), {background: '#008000'}).then(function(canvas) {
  46. expect(canvas.width).to.equal(200);
  47. expect(canvas.height).to.equal(200);
  48. validCanvasPixels(canvas);
  49. done();
  50. }).catch(function(error) {
  51. done(error);
  52. });
  53. });
  54. it("with named color", function(done) {
  55. html2canvas(document.querySelector("#block"), {background: 'green'}).then(function(canvas) {
  56. expect(canvas.width).to.equal(200);
  57. expect(canvas.height).to.equal(200);
  58. validCanvasPixels(canvas);
  59. done();
  60. }).catch(function(error) {
  61. done(error);
  62. });
  63. });
  64. it("with element background", function(done) {
  65. html2canvas(document.querySelector("#green-block"), {background: 'red'}).then(function(canvas) {
  66. expect(canvas.width).to.equal(200);
  67. expect(canvas.height).to.equal(200);
  68. validCanvasPixels(canvas);
  69. done();
  70. }).catch(function(error) {
  71. done(error);
  72. });
  73. });
  74. });
  75. describe('element background', function() {
  76. it('with background-color', function(done) {
  77. html2canvas(document.querySelector("#green-block")).then(function(canvas) {
  78. expect(canvas.width).to.equal(200);
  79. expect(canvas.height).to.equal(200);
  80. validCanvasPixels(canvas);
  81. done();
  82. }).catch(function(error) {
  83. done(error);
  84. });
  85. });
  86. it('with background-image', function(done) {
  87. html2canvas(document.querySelector("#background-block")).then(function(canvas) {
  88. expect(canvas.width).to.equal(200);
  89. expect(canvas.height).to.equal(200);
  90. validCanvasPixels(canvas);
  91. done();
  92. }).catch(function(error) {
  93. done(error);
  94. });
  95. });
  96. it('with gradient background-image', function(done) {
  97. html2canvas(document.querySelector("#gradient-block")).then(function(canvas) {
  98. expect(canvas.width).to.equal(200);
  99. expect(canvas.height).to.equal(200);
  100. validCanvasPixels(canvas);
  101. done();
  102. }).catch(function(error) {
  103. done(error);
  104. });
  105. });
  106. });
  107. function validCanvasPixels(canvas) {
  108. var ctx = canvas.getContext("2d");
  109. var data = ctx.getImageData(0, 0, canvas.width, canvas.height).data;
  110. for (var i = 0, len = data.length; i < len; i+=4) {
  111. if (data[i] !== 0 || data[i+1] !== 128 || data[i+2] !== 0 || data[i+3] !== 255) {
  112. expect().fail("Invalid canvas data");
  113. }
  114. }
  115. }
  116. mocha.checkLeaks();
  117. mocha.globals(['jQuery']);
  118. if (window.mochaPhantomJS) {
  119. mochaPhantomJS.run();
  120. }
  121. else {
  122. mocha.run();
  123. }
  124. mocha.suite.afterAll(function() {
  125. document.body.setAttribute('data-complete', 'true');
  126. });
  127. </script>
  128. </body>
  129. </html>