暫無描述

imageSpec.js 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /* eslint-disable */
  2. describe('Image: ', function() {
  3. var granimInstance, gradientColor, canvas,
  4. canvasWidthMiddle, canvasHeightMiddle;
  5. var event = document.createEvent('HTMLEvents');
  6. beforeEach(function(done) {
  7. setTimeout(function() {
  8. value = 0;
  9. done();
  10. }, 1);
  11. });
  12. it('should support async execution of test preparation and expectations with image setup', function(done) {
  13. canvas = setCanvas();
  14. granimInstance = new Granim({
  15. element: '#granim-canvas',
  16. name: 'basic-gradient',
  17. direction: 'top-bottom',
  18. opacity: [1, 1, 1],
  19. isPausedWhenNotInView: true,
  20. image: {
  21. source: 'img/800x200.jpg',
  22. position: ['left', 'center'],
  23. stretchMode: ['stretch', 'stretch'],
  24. blendingMode: 'multiply'
  25. },
  26. states: {
  27. 'default-state': {
  28. gradients: [
  29. ['#485563', '#29323c', '#29323c'],
  30. ['#556270', '#FF6B6B', '#FF6B6B']
  31. ]
  32. }
  33. }
  34. });
  35. document.createEvent('HTMLEvents');
  36. event.initEvent('resize', true, false);
  37. window.dispatchEvent(event);
  38. canvasWidthMiddle = (canvas.width - 50) / 2;
  39. canvasHeightMiddle = (canvas.height - 50) / 2;
  40. gradientColor = granimInstance.context.getImageData(canvasWidthMiddle, canvasHeightMiddle, 5, 5);
  41. expect(granimInstance).toBeDefined();
  42. done();
  43. });
  44. describe('Asynchronous specs: User inputs validation', function() {
  45. var originalTimeout;
  46. beforeEach(function() {
  47. originalTimeout = jasmine.DEFAULT_TIMEOUT_INTERVAL;
  48. jasmine.DEFAULT_TIMEOUT_INTERVAL = 10000;
  49. });
  50. it('throws error on invalid element "image.position"', function(){
  51. function updateInstanceWithWrongParams() {
  52. return new Granim({
  53. element: '#granim-canvas',
  54. name: 'basic-gradient',
  55. direction: 'top-bottom',
  56. opacity: [1, 1, 1],
  57. isPausedWhenNotInView: true,
  58. image: {
  59. source: 'img/800x200.jpg',
  60. // position should be [x, y], here it's [y, x]
  61. position: ['top', 'left']
  62. },
  63. states: {
  64. 'default-state': {
  65. gradients: [
  66. ['#485563', '#29323c', '#29323c'],
  67. ['#556270', '#FF6B6B', '#FF6B6B']
  68. ]
  69. }
  70. }
  71. });
  72. }
  73. expect(updateInstanceWithWrongParams).toThrowError(
  74. 'Granim: Input error on "image.position" option.\nCheck the API https://sarcadass.github.io/granim.js/api.html.'
  75. );
  76. });
  77. it('throws error on invalid element "image.stretchMode"', function(){
  78. function updateInstanceWithWrongParams() {
  79. return new Granim({
  80. element: '#granim-canvas',
  81. name: 'basic-gradient',
  82. direction: 'top-bottom',
  83. opacity: [1, 1, 1],
  84. isPausedWhenNotInView: true,
  85. image: {
  86. source: 'img/800x200.jpg',
  87. position: ['right', 'bottom'],
  88. // mistake on 'stretch-if-smaller'
  89. stretchMode: ['stretch-if-smaler', 'stretch']
  90. },
  91. states: {
  92. 'default-state': {
  93. gradients: [
  94. ['#485563', '#29323c', '#29323c']
  95. ]
  96. }
  97. }
  98. });
  99. }
  100. expect(updateInstanceWithWrongParams).toThrowError(
  101. 'Granim: Input error on "image.stretchMode" option.\nCheck the API https://sarcadass.github.io/granim.js/api.html.'
  102. );
  103. });
  104. afterEach(function() {
  105. jasmine.DEFAULT_TIMEOUT_INTERVAL = originalTimeout;
  106. });
  107. });
  108. describe('Asynchronous specs: Animation with an image', function() {
  109. var originalTimeout;
  110. beforeEach(function() {
  111. originalTimeout = jasmine.DEFAULT_TIMEOUT_INTERVAL;
  112. jasmine.DEFAULT_TIMEOUT_INTERVAL = 10000;
  113. });
  114. it('Gradient animation is working', function(done) {
  115. setTimeout(function() {
  116. var isSameGradientImage = compareImages(gradientColor,
  117. granimInstance.context.getImageData(canvasWidthMiddle, canvasHeightMiddle, 5, 5));
  118. expect(isSameGradientImage).toBe(false);
  119. done();
  120. }, 500);
  121. });
  122. it('ChangeBlendingMode method is working', function(done) {
  123. granimInstance.changeBlendingMode('screen');
  124. setTimeout(function() {
  125. expect(granimInstance.image.blendingMode).toEqual('screen');
  126. done();
  127. }, 200);
  128. });
  129. it('Gradient animation is working without "image.position" and/or "image.stretchMode" set', function(done) {
  130. canvas.style.height = '100px';
  131. granimInstance = new Granim({
  132. element: '#granim-canvas',
  133. name: 'basic-gradient',
  134. direction: 'radial',
  135. opacity: [1, 1, 1],
  136. isPausedWhenNotInView: true,
  137. image: { source: 'img/800x200.jpg' },
  138. states: {
  139. 'default-state': { gradients: [['#485563', '#29323c', '#29323c']] }
  140. }
  141. });
  142. expect(granimInstance).toBeDefined();
  143. done();
  144. });
  145. it('Different parameters are working 1/3', function(done) {
  146. granimInstance = new Granim({
  147. element: '#granim-canvas',
  148. name: 'basic-gradient',
  149. direction: 'radial',
  150. opacity: [1, 1, 1],
  151. isPausedWhenNotInView: true,
  152. image: {
  153. source: 'img/800x200.jpg',
  154. position: ['right', 'top'],
  155. stretchMode: ['stretch-if-bigger', 'stretch-if-bigger']
  156. },
  157. states: {
  158. 'default-state': { gradients: [['#485563', '#29323c', '#29323c']] }
  159. }
  160. });
  161. expect(granimInstance).toBeDefined();
  162. done();
  163. });
  164. it('Different parameters are working 2/3', function(done) {
  165. granimInstance = new Granim({
  166. element: '#granim-canvas',
  167. name: 'basic-gradient',
  168. direction: 'radial',
  169. opacity: [1, 1, 1],
  170. isPausedWhenNotInView: true,
  171. image: {
  172. source: 'img/800x200.jpg',
  173. position: ['center', 'bottom'],
  174. stretchMode: ['none', 'stretch']
  175. },
  176. states: {
  177. 'default-state': { gradients: [['#485563', '#29323c', '#29323c']] }
  178. }
  179. });
  180. expect(granimInstance).toBeDefined();
  181. done();
  182. });
  183. it('Different parameters are working 3/3', function(done) {
  184. canvas.style.height = '300px';
  185. granimInstance = new Granim({
  186. element: '#granim-canvas',
  187. name: 'basic-gradient',
  188. direction: 'radial',
  189. opacity: [1, 1, 1],
  190. isPausedWhenNotInView: true,
  191. image: {
  192. source: 'img/800x200.jpg',
  193. position: ['center', 'bottom'],
  194. stretchMode: ['stretch-if-smaller', 'stretch-if-smaller']
  195. },
  196. states: {
  197. 'default-state': { gradients: [['#485563', '#29323c', '#29323c']] }
  198. }
  199. });
  200. expect(granimInstance).toBeDefined();
  201. done();
  202. });
  203. afterEach(function() {
  204. jasmine.DEFAULT_TIMEOUT_INTERVAL = originalTimeout;
  205. });
  206. });
  207. });