jquery.avatarSpec.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /**
  2. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  3. * SPDX-FileCopyrightText: 2015 ownCloud Inc.
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. describe('jquery.avatar tests', function() {
  7. var $div;
  8. var devicePixelRatio;
  9. beforeEach(function() {
  10. $('#testArea').append($('<div id="avatardiv">'));
  11. $div = $('#avatardiv');
  12. devicePixelRatio = window.devicePixelRatio;
  13. window.devicePixelRatio = 1;
  14. spyOn(window, 'Image').and.returnValue({
  15. onload: function() {
  16. },
  17. onerror: function() {
  18. }
  19. });
  20. });
  21. afterEach(function() {
  22. $div.remove();
  23. window.devicePixelRatio = devicePixelRatio;
  24. });
  25. describe('size', function() {
  26. it('undefined', function() {
  27. $div.avatar('foo');
  28. expect(Math.round($div.height())).toEqual(64);
  29. expect(Math.round($div.width())).toEqual(64);
  30. });
  31. it('undefined but div has height', function() {
  32. $div.height(9);
  33. $div.avatar('foo');
  34. expect(window.Image).toHaveBeenCalled();
  35. window.Image().onerror();
  36. expect(Math.round($div.height())).toEqual(9);
  37. expect(Math.round($div.width())).toEqual(9);
  38. });
  39. it('undefined but data size is set', function() {
  40. $div.data('size', 10);
  41. $div.avatar('foo');
  42. expect(window.Image).toHaveBeenCalled();
  43. window.Image().onerror();
  44. expect(Math.round($div.height())).toEqual(10);
  45. expect(Math.round($div.width())).toEqual(10);
  46. });
  47. it('defined', function() {
  48. $div.avatar('foo', 8);
  49. expect(window.Image).toHaveBeenCalled();
  50. window.Image().onerror();
  51. expect(Math.round($div.height())).toEqual(8);
  52. expect(Math.round($div.width())).toEqual(8);
  53. });
  54. });
  55. it('undefined user', function() {
  56. spyOn($div, 'imageplaceholder');
  57. spyOn($div, 'css');
  58. $div.avatar();
  59. expect($div.imageplaceholder).toHaveBeenCalledWith('?');
  60. expect($div.css).toHaveBeenCalledWith('background-color', '#b9b9b9');
  61. });
  62. describe('no avatar', function() {
  63. it('show placeholder for existing user', function() {
  64. spyOn($div, 'imageplaceholder');
  65. $div.avatar('foo', undefined, undefined, undefined, undefined, 'bar');
  66. expect(window.Image).toHaveBeenCalled();
  67. window.Image().onerror();
  68. expect($div.imageplaceholder).toHaveBeenCalledWith('foo', 'bar');
  69. });
  70. it('show placeholder for non existing user', function() {
  71. spyOn($div, 'imageplaceholder');
  72. spyOn($div, 'css');
  73. $div.avatar('foo');
  74. expect(window.Image).toHaveBeenCalled();
  75. window.Image().onerror();
  76. expect($div.imageplaceholder).toHaveBeenCalledWith('?');
  77. expect($div.css).toHaveBeenCalledWith('background-color', '#b9b9b9');
  78. });
  79. it('show no placeholder is ignored', function() {
  80. spyOn($div, 'imageplaceholder');
  81. spyOn($div, 'css');
  82. $div.avatar('foo', undefined, undefined, true);
  83. expect(window.Image).toHaveBeenCalled();
  84. window.Image().onerror();
  85. expect($div.imageplaceholder).toHaveBeenCalledWith('?');
  86. expect($div.css).toHaveBeenCalledWith('background-color', '#b9b9b9');
  87. });
  88. });
  89. describe('url generation', function() {
  90. beforeEach(function() {
  91. window.devicePixelRatio = 1;
  92. });
  93. it('default', function() {
  94. window.devicePixelRatio = 1;
  95. $div.avatar('foo', 32);
  96. expect(window.Image).toHaveBeenCalled();
  97. expect(window.Image().src).toEqual('http://localhost/index.php/avatar/foo/32');
  98. });
  99. it('high DPI icon', function() {
  100. window.devicePixelRatio = 4;
  101. $div.avatar('foo', 32);
  102. expect(window.Image).toHaveBeenCalled();
  103. expect(window.Image().src).toEqual('http://localhost/index.php/avatar/foo/128');
  104. });
  105. it('high DPI icon round up size', function() {
  106. window.devicePixelRatio = 1.9;
  107. $div.avatar('foo', 32);
  108. expect(window.Image).toHaveBeenCalled();
  109. expect(window.Image().src).toEqual('http://localhost/index.php/avatar/foo/61');
  110. });
  111. });
  112. describe('valid avatar', function() {
  113. beforeEach(function() {
  114. window.devicePixelRatio = 1;
  115. });
  116. it('default (no ie8 fix)', function() {
  117. $div.avatar('foo', 32);
  118. expect(window.Image).toHaveBeenCalled();
  119. window.Image().onload();
  120. expect(window.Image().height).toEqual(32);
  121. expect(window.Image().width).toEqual(32);
  122. expect(window.Image().src).toEqual('http://localhost/index.php/avatar/foo/32');
  123. });
  124. it('default high DPI icon', function() {
  125. window.devicePixelRatio = 1.9;
  126. $div.avatar('foo', 32);
  127. expect(window.Image).toHaveBeenCalled();
  128. window.Image().onload();
  129. expect(window.Image().height).toEqual(32);
  130. expect(window.Image().width).toEqual(32);
  131. expect(window.Image().src).toEqual('http://localhost/index.php/avatar/foo/61');
  132. });
  133. it('with ie8 fix (ignored)', function() {
  134. $div.avatar('foo', 32, true);
  135. expect(window.Image).toHaveBeenCalled();
  136. window.Image().onload();
  137. expect(window.Image().height).toEqual(32);
  138. expect(window.Image().width).toEqual(32);
  139. expect(window.Image().src).toEqual('http://localhost/index.php/avatar/foo/32');
  140. });
  141. it('unhide div', function() {
  142. $div.hide();
  143. $div.avatar('foo', 32);
  144. expect(window.Image).toHaveBeenCalled();
  145. window.Image().onload();
  146. expect(window.Image().height).toEqual(32);
  147. expect(window.Image().width).toEqual(32);
  148. expect(window.Image().src).toEqual('http://localhost/index.php/avatar/foo/32');
  149. });
  150. it('callback called', function() {
  151. var observer = {callback: function() { dump("FOO"); }};
  152. spyOn(observer, 'callback');
  153. $div.avatar('foo', 32, undefined, undefined, function() {
  154. observer.callback();
  155. });
  156. expect(window.Image).toHaveBeenCalled();
  157. window.Image().onload();
  158. expect(window.Image().height).toEqual(32);
  159. expect(window.Image().width).toEqual(32);
  160. expect(window.Image().src).toEqual('http://localhost/index.php/avatar/foo/32');
  161. expect(observer.callback).toHaveBeenCalled();
  162. });
  163. });
  164. });