canvasgraphics_test.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. // Copyright 2015 The Closure Library Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS-IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. goog.provide('goog.graphics.CanvasGraphicsTest');
  15. goog.setTestOnly('goog.graphics.CanvasGraphicsTest');
  16. goog.require('goog.dom');
  17. goog.require('goog.graphics.CanvasGraphics');
  18. goog.require('goog.graphics.SolidFill');
  19. goog.require('goog.graphics.Stroke');
  20. goog.require('goog.testing.jsunit');
  21. var graphics;
  22. function shouldRunTests() {
  23. graphics = new goog.graphics.CanvasGraphics(100, 100);
  24. graphics.createDom();
  25. return graphics.canvas_.getContext;
  26. }
  27. function setUp() {
  28. graphics = new goog.graphics.CanvasGraphics(100, 100);
  29. graphics.createDom();
  30. goog.dom.getElement('root').appendChild(graphics.getElement());
  31. graphics.enterDocument();
  32. }
  33. function tearDown() {
  34. graphics.dispose();
  35. goog.dom.removeNode(graphics.getElement());
  36. }
  37. function testDrawRemoveRect() {
  38. var fill = new goog.graphics.SolidFill('red');
  39. var stroke = new goog.graphics.Stroke('blue');
  40. var element = graphics.drawRect(10, 10, 80, 80, stroke, fill);
  41. assertEquals(1, graphics.canvasElement.children_.length);
  42. graphics.removeElement(element);
  43. assertEquals(0, graphics.canvasElement.children_.length);
  44. }
  45. function testDrawRemoveNestedRect() {
  46. var fill = new goog.graphics.SolidFill('red');
  47. var stroke = new goog.graphics.Stroke('blue');
  48. var group = graphics.createGroup();
  49. assertEquals(1, graphics.canvasElement.children_.length);
  50. assertEquals(0, graphics.canvasElement.children_[0].children_.length);
  51. var element = graphics.drawRect(10, 10, 80, 80, stroke, fill, group);
  52. assertEquals(1, graphics.canvasElement.children_.length);
  53. assertEquals(1, graphics.canvasElement.children_[0].children_.length);
  54. graphics.removeElement(element);
  55. assertEquals(1, graphics.canvasElement.children_.length);
  56. assertEquals(0, graphics.canvasElement.children_[0].children_.length);
  57. }