dash-sanity.html 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <html>
  2. <head>
  3. <script type="text/javascript">
  4. function assert(cond, msg) { if (!cond) { throw msg; } }
  5. window.onload = function() {
  6. try {
  7. var ctx = document.getElementById("c1").getContext("2d");
  8. assert(0 === ctx.getLineDash().length,
  9. "Default dash is [ ] (none)");
  10. assert(0 === ctx.lineDashOffset,
  11. "Default dashOffset is 0 (none)");
  12. ctx.setLineDash([ 2 ]);
  13. assert(2 === ctx.getLineDash().length &&
  14. 2 === ctx.getLineDash()[0] &&
  15. 2 === ctx.getLineDash()[1],
  16. "dash = [ 2 ] works");
  17. ctx.setLineDash([ 2 ]);
  18. ctx.setLineDash([ ]);
  19. assert(0 === ctx.getLineDash().length,
  20. "dash = [ ] works");
  21. ctx.setLineDash([ 2 ]);
  22. ctx.setLineDash([ 0, 0, 0 ]);
  23. assert(6 === ctx.getLineDash().length,
  24. 0 === ctx.getLineDash()[0] &&
  25. 0 === ctx.getLineDash()[1] &&
  26. 0 === ctx.getLineDash()[2] &&
  27. 0 === ctx.getLineDash()[3] &&
  28. 0 === ctx.getLineDash()[4] &&
  29. 0 === ctx.getLineDash()[5],
  30. "dash = [ 0, 0, 0 ] works");
  31. ctx.setLineDash([ 2 ]);
  32. assert(0 === ctx.lineDashOffset, "dashOffset is 0");
  33. ctx.lineDashOffset = 1;
  34. assert(1 === ctx.lineDashOffset, "Setting dashOffset succeeded");
  35. ctx.setLineDash([ ]);
  36. assert(1 === ctx.lineDashOffset, "Changing dash does not reset dashOffset");
  37. // NB: might want to add a |.dash = number| special case,
  38. // don't test that it fails here. Might also want to add a
  39. // |.dash = [0]| special case for resetting, so don't test
  40. // that either.
  41. var badVals = [ -1,
  42. null,
  43. undefined,
  44. "",
  45. "string",
  46. { obj: true },
  47. function() {}
  48. ]
  49. ctx.setLineDash([ 2 ]);
  50. for (var i = 0; i < badVals.length; ++i) {
  51. var error = false;
  52. try { ctx.setLineDash(badVals[i]); }
  53. catch(e) { error = true; }
  54. assert(error &&
  55. 2 === ctx.getLineDash().length &&
  56. 2 === ctx.getLineDash()[0] &&
  57. 2 === ctx.getLineDash()[1],
  58. "Expected setLineDash("+ badVals[i] +") to throw exception and not change dash");
  59. }
  60. var ignoredVals = [
  61. [ "array of string" ],
  62. [ -1 ],
  63. [ 2, "string" ],
  64. ];
  65. ctx.setLineDash([ 2 ]);
  66. for (var i = 0; i < ignoredVals.length; ++i) {
  67. ctx.setLineDash(ignoredVals[i]);
  68. assert(2 === ctx.getLineDash().length &&
  69. 2 === ctx.getLineDash()[0] &&
  70. 2 === ctx.getLineDash()[1],
  71. "Expected |setLineDash(" + ignoredVals[i] + ") to not change dash");
  72. }
  73. ctx.setLineDash([ 2 ]);
  74. ctx.save();
  75. ctx.setLineDash([ 1, 1, 1, 1 ]);
  76. ctx.restore();
  77. assert(2 === ctx.getLineDash().length &&
  78. 2 === ctx.getLineDash()[0] &&
  79. 2 === ctx.getLineDash()[1],
  80. "dash was saved then restored");
  81. } catch (e) {
  82. document.body.innerHTML = "FAIL: "+ e.toString();
  83. return;
  84. }
  85. document.body.innerHTML = "Pass";
  86. }
  87. </script>
  88. </head>
  89. <body>
  90. <div><canvas id="c1" width="300" height="300"></canvas></div>
  91. </body>
  92. </html>