Instancing.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. topSuite("Ext.draw.sprite.Instancing", ['Ext.draw.*'], function() {
  2. beforeEach(function() {
  3. // Silence warnings regarding Sencha download server
  4. spyOn(Ext.log, 'warn');
  5. });
  6. describe("'template' config", function () {
  7. it("should set the template's parent to the instancing sprite", function () {
  8. var template = new Ext.draw.sprite.Rect(),
  9. instancing = new Ext.draw.sprite.Instancing({
  10. template: template
  11. });
  12. expect(template.getParent()).toBe(instancing);
  13. instancing.destroy();
  14. });
  15. it("should destroy the template when destroyed", function () {
  16. var template = new Ext.draw.sprite.Rect(),
  17. instancing = new Ext.draw.sprite.Instancing({
  18. template: template
  19. });
  20. instancing.destroy();
  21. expect(instancing.destroyed).toBe(true);
  22. });
  23. });
  24. describe("'instances' config", function () {
  25. it("should create instances of the template from array of config objects", function () {
  26. var template = new Ext.draw.sprite.Circle({
  27. cx: 200,
  28. r: 60,
  29. fillStyle: '#00ff00'
  30. }),
  31. instancing = new Ext.draw.sprite.Instancing({
  32. template: template,
  33. instances: [
  34. {
  35. cy: 150,
  36. r: 30,
  37. fillStyle: '#ff0000'
  38. },
  39. {
  40. cy: 300
  41. }
  42. ]
  43. });
  44. expect(instancing.getCount()).toBe(2);
  45. expect(instancing.get(0).cx).toBe(200);
  46. expect(instancing.get(0).r).toBe(30);
  47. expect(instancing.get(1).fillStyle).toBe('#00ff00');
  48. instancing.destroy();
  49. });
  50. it("should destroy the template when destroyed", function () {
  51. var template = new Ext.draw.sprite.Rect(),
  52. instancing = new Ext.draw.sprite.Instancing({
  53. template: template
  54. });
  55. instancing.destroy();
  56. expect(instancing.destroyed).toBe(true);
  57. });
  58. });
  59. describe("hitTest", function () {
  60. var sprite, instancingSprite, surface, container;
  61. beforeEach(function () {
  62. container = new Ext.draw.Container();
  63. surface = new Ext.draw.Surface();
  64. sprite = new Ext.draw.sprite.Circle({
  65. hidden: false,
  66. globalAlpha: 1,
  67. fillOpacity: 1,
  68. strokeOpacity: 1,
  69. fillStyle: 'red',
  70. strokeStyle: 'red'
  71. });
  72. instancingSprite = new Ext.draw.sprite.Instancing({
  73. template: sprite
  74. });
  75. surface.add(instancingSprite);
  76. container.add(surface);
  77. });
  78. afterEach(function () {
  79. Ext.destroy(sprite, instancingSprite, surface, container);
  80. });
  81. it("should return an object with the 'sprite' property set to the instancing sprite, " +
  82. "'template' property set to the instancing template, " +
  83. "'instance' property set to the attributes of the instance, " +
  84. "'index' property set to the index of the instance, " +
  85. "and 'isInstance' property set to true", function () {
  86. instancingSprite.add({
  87. r: 50,
  88. cx: 300,
  89. cy: 300
  90. });
  91. instancingSprite.add({
  92. r: 100,
  93. cx: 100,
  94. cy: 100
  95. });
  96. var result = instancingSprite.hitTest([90, 90]);
  97. expect(result.isInstance).toBe(true);
  98. expect(result.instance).toBe(instancingSprite.get(1));
  99. expect(result.index).toBe(1);
  100. expect(result.template).toBe(sprite);
  101. expect(result.sprite).toBe(instancingSprite);
  102. });
  103. it("should return null for hidden instances", function () {
  104. instancingSprite.add({
  105. r: 100,
  106. cx: 100,
  107. cy: 100,
  108. hidden: true
  109. });
  110. var result = instancingSprite.hitTest([90, 90]);
  111. expect(result).toBe(null);
  112. });
  113. });
  114. });