message.js 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. describe('MessageService', function() {
  2. let messageService;
  3. // Ignoring page reload request
  4. beforeAll(() => window.onbeforeunload = () => null);
  5. beforeEach(function() {
  6. // Load threema services
  7. module('3ema.services');
  8. // Inject the MessageService
  9. inject(function(MessageService) {
  10. messageService = MessageService;
  11. });
  12. });
  13. describe ('getAccess', () => {
  14. let test = (m, r) => {
  15. return expect(messageService.getAccess(m, r));
  16. };
  17. it('invalid arguments', () => {
  18. test().toEqual(jasmine.objectContaining({
  19. quote: false,
  20. copy: false,
  21. ack: false,
  22. dec: false,
  23. delete: false,
  24. download: false,
  25. }));
  26. })
  27. const testCases = [['contact', {id: 'ECHOECHO', type: 'contact'}, true],
  28. ['gateway contact', {id: '*THREEMA', type: 'contact'}, true],
  29. ['group', {id: 1, type: 'group'}, false],
  30. ['distributionList', {id: 1, type: 'distributionList'}, false]]
  31. testCases.forEach((testData) => {
  32. let type = testData[0];
  33. let receiver = testData[1];
  34. let canAckDec = testData[2];
  35. describe(type, () => {
  36. it('text messages ', () => {
  37. test({isOutbox: false, type: 'text'}, receiver)
  38. .toEqual(jasmine.objectContaining({
  39. quote: true,
  40. copy: true,
  41. ack: true && canAckDec,
  42. dec: true && canAckDec,
  43. delete: true,
  44. download: false,
  45. }));
  46. test({isOutbox: true, type: 'text'}, receiver)
  47. .toEqual(jasmine.objectContaining({
  48. quote: true,
  49. copy: true,
  50. ack: false,
  51. dec: false,
  52. delete: true,
  53. download: false,
  54. }));
  55. test({isOutbox: false, type: 'text', state: 'user-ack'}, receiver)
  56. .toEqual(jasmine.objectContaining({
  57. quote: true,
  58. copy: true,
  59. ack: false,
  60. dec: true && canAckDec,
  61. delete: true,
  62. download: false,
  63. }));
  64. test({isOutbox: false, type: 'text', state: 'user-dec'}, receiver)
  65. .toEqual(jasmine.objectContaining({
  66. quote: true,
  67. copy: true,
  68. ack: true && canAckDec,
  69. dec: false,
  70. delete: true,
  71. download: false,
  72. }));
  73. });
  74. it('location messages ', () => {
  75. test({isOutbox: false, type: 'text'}, receiver)
  76. .toEqual(jasmine.objectContaining({
  77. quote: true,
  78. copy: true,
  79. ack: true && canAckDec,
  80. dec: true && canAckDec,
  81. delete: true,
  82. download: false,
  83. }));
  84. test({isOutbox: true, type: 'text'}, receiver)
  85. .toEqual(jasmine.objectContaining({
  86. quote: true,
  87. copy: true,
  88. ack: false,
  89. dec: false,
  90. delete: true,
  91. download: false,
  92. }));
  93. test({isOutbox: false, type: 'text', state: 'user-ack'}, receiver)
  94. .toEqual(jasmine.objectContaining({
  95. quote: true,
  96. copy: true,
  97. ack: false,
  98. dec: true && canAckDec,
  99. delete: true,
  100. download: false,
  101. }));
  102. test({isOutbox: false, type: 'text', state: 'user-dec'}, receiver)
  103. .toEqual(jasmine.objectContaining({
  104. quote: true,
  105. copy: true,
  106. ack: true && canAckDec,
  107. dec: false,
  108. delete: true,
  109. download: false,
  110. }));
  111. });
  112. ['image', 'video', 'audio', 'file'].forEach((type) => {
  113. it('inbox ' + type, () => {
  114. test({isOutbox: false, type: type}, receiver)
  115. .toEqual(jasmine.objectContaining({
  116. quote: false,
  117. copy: false,
  118. ack: true && canAckDec,
  119. dec: true && canAckDec,
  120. delete: true,
  121. download: true,
  122. }));
  123. });
  124. it('inbox (caption) ' + type, () => {
  125. test({isOutbox: false, type: type, caption: 'test'}, receiver)
  126. .toEqual(jasmine.objectContaining({
  127. quote: true,
  128. copy: true,
  129. ack: true && canAckDec,
  130. dec: true && canAckDec,
  131. delete: true,
  132. download: true,
  133. }));
  134. });
  135. it('outbox ' + type, () => {
  136. test({isOutbox: false, type: type}, receiver)
  137. .toEqual(jasmine.objectContaining({
  138. quote: false,
  139. copy: false,
  140. ack: true && canAckDec,
  141. dec: true && canAckDec,
  142. delete: true,
  143. download: true,
  144. }));
  145. });
  146. it('outbox (caption) ' + type, () => {
  147. test({isOutbox: false, type: type, caption: 'test'}, receiver)
  148. .toEqual(jasmine.objectContaining({
  149. quote: true,
  150. copy: true,
  151. ack: true && canAckDec,
  152. dec: true && canAckDec,
  153. delete: true,
  154. download: true,
  155. }));
  156. });
  157. });
  158. });
  159. });
  160. });
  161. describe ('showStatusIcon', () => {
  162. let test = (m, r) => {
  163. return expect(messageService.showStatusIcon(m, r));
  164. };
  165. it ('invalid arguments', () => {
  166. test(null, null).toEqual(false);
  167. test({}, null).toEqual(false);
  168. test(null, {}).toEqual(false);
  169. test({}, {}).toEqual(false);
  170. });
  171. let testAll = (receiver, outboxTrue, outboxFalse, inboxTrue, inboxFalse) => {
  172. let testDirection = (outbox, t, f) => {
  173. let testDirections = (states, toBe) => {
  174. states.forEach((state) => {
  175. it(state, () => {
  176. test({isOutbox: outbox, state: state}, receiver).toBe(toBe);
  177. });
  178. });
  179. };
  180. testDirections(t, true);
  181. testDirections(f, false);
  182. };
  183. describe ('outbox', () => {
  184. testDirection(true, outboxTrue, outboxFalse);
  185. });
  186. describe ('inbox', () => {
  187. testDirection(false, inboxTrue, inboxFalse);
  188. });
  189. };
  190. describe ('normal contact', () => {
  191. testAll({id: 'ECHOECHO', type: 'contact'},
  192. ['delivered', 'read', 'send-failed', 'sent', 'pending', 'sending', 'user-ack', 'user-dec'],
  193. [],
  194. ['user-ack', 'user-dec'],
  195. ['delivered', 'read', 'send-failed', 'sent', 'pending', 'sending']
  196. );
  197. });
  198. describe ('gateway contact', () => {
  199. testAll({id: '*THREEMA', type: 'contact'},
  200. ['send-failed', 'pending', 'sending'],
  201. ['delivered', 'read', 'sent', 'user-ack', 'user-dec'],
  202. ['user-ack', 'user-dec'],
  203. ['delivered', 'read', 'send-failed', 'sent', 'pending', 'sending']
  204. );
  205. });
  206. describe ('Group', () => {
  207. testAll({id: 1, type: 'group'},
  208. ['send-failed', 'pending', 'sending'],
  209. ['delivered', 'read', 'sent', 'user-ack', 'user-dec'],
  210. [],
  211. ['delivered', 'read', 'send-failed', 'sent', 'pending', 'sending', 'user-ack', 'user-dec']
  212. );
  213. });
  214. describe ('Distribution List', () => {
  215. testAll({id: 1, type: 'distributionList'},
  216. [],
  217. ['send-failed', 'pending', 'sending', 'delivered', 'read', 'sent', 'user-ack', 'user-dec'],
  218. [],
  219. ['delivered', 'read', 'send-failed', 'sent', 'pending', 'sending', 'user-ack', 'user-dec']
  220. );
  221. });
  222. });
  223. });