api-dialog-spec.ts 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. import { expect } from 'chai';
  2. import { dialog, BrowserWindow } from 'electron/main';
  3. import { closeAllWindows } from './window-helpers';
  4. import { ifit, delay } from './spec-helpers';
  5. describe('dialog module', () => {
  6. describe('showOpenDialog', () => {
  7. afterEach(closeAllWindows);
  8. ifit(process.platform !== 'win32')('should not throw for valid cases', () => {
  9. expect(() => {
  10. dialog.showOpenDialog({ title: 'i am title' });
  11. }).to.not.throw();
  12. expect(() => {
  13. const w = new BrowserWindow();
  14. dialog.showOpenDialog(w, { title: 'i am title' });
  15. }).to.not.throw();
  16. });
  17. it('throws errors when the options are invalid', () => {
  18. expect(() => {
  19. dialog.showOpenDialog({ properties: false as any });
  20. }).to.throw(/Properties must be an array/);
  21. expect(() => {
  22. dialog.showOpenDialog({ title: 300 as any });
  23. }).to.throw(/Title must be a string/);
  24. expect(() => {
  25. dialog.showOpenDialog({ buttonLabel: [] as any });
  26. }).to.throw(/Button label must be a string/);
  27. expect(() => {
  28. dialog.showOpenDialog({ defaultPath: {} as any });
  29. }).to.throw(/Default path must be a string/);
  30. expect(() => {
  31. dialog.showOpenDialog({ message: {} as any });
  32. }).to.throw(/Message must be a string/);
  33. });
  34. });
  35. describe('showSaveDialog', () => {
  36. afterEach(closeAllWindows);
  37. ifit(process.platform !== 'win32')('should not throw for valid cases', () => {
  38. expect(() => {
  39. dialog.showSaveDialog({ title: 'i am title' });
  40. }).to.not.throw();
  41. expect(() => {
  42. const w = new BrowserWindow();
  43. dialog.showSaveDialog(w, { title: 'i am title' });
  44. }).to.not.throw();
  45. });
  46. it('throws errors when the options are invalid', () => {
  47. expect(() => {
  48. dialog.showSaveDialog({ title: 300 as any });
  49. }).to.throw(/Title must be a string/);
  50. expect(() => {
  51. dialog.showSaveDialog({ buttonLabel: [] as any });
  52. }).to.throw(/Button label must be a string/);
  53. expect(() => {
  54. dialog.showSaveDialog({ defaultPath: {} as any });
  55. }).to.throw(/Default path must be a string/);
  56. expect(() => {
  57. dialog.showSaveDialog({ message: {} as any });
  58. }).to.throw(/Message must be a string/);
  59. expect(() => {
  60. dialog.showSaveDialog({ nameFieldLabel: {} as any });
  61. }).to.throw(/Name field label must be a string/);
  62. });
  63. });
  64. describe('showMessageBox', () => {
  65. afterEach(closeAllWindows);
  66. // parentless message boxes are synchronous on macOS
  67. // dangling message boxes on windows cause a DCHECK: https://source.chromium.org/chromium/chromium/src/+/main:base/win/message_window.cc;drc=7faa4bf236a866d007dc5672c9ce42660e67a6a6;l=68
  68. ifit(process.platform !== 'darwin' && process.platform !== 'win32')('should not throw for a parentless message box', () => {
  69. expect(() => {
  70. dialog.showMessageBox({ message: 'i am message' });
  71. }).to.not.throw();
  72. });
  73. ifit(process.platform !== 'win32')('should not throw for valid cases', () => {
  74. expect(() => {
  75. const w = new BrowserWindow();
  76. dialog.showMessageBox(w, { message: 'i am message' });
  77. }).to.not.throw();
  78. });
  79. it('throws errors when the options are invalid', () => {
  80. expect(() => {
  81. dialog.showMessageBox(undefined as any, { type: 'not-a-valid-type' as any, message: '' });
  82. }).to.throw(/Invalid message box type/);
  83. expect(() => {
  84. dialog.showMessageBox(null as any, { buttons: false as any, message: '' });
  85. }).to.throw(/Buttons must be an array/);
  86. expect(() => {
  87. dialog.showMessageBox({ title: 300 as any, message: '' });
  88. }).to.throw(/Title must be a string/);
  89. expect(() => {
  90. dialog.showMessageBox({ message: [] as any });
  91. }).to.throw(/Message must be a string/);
  92. expect(() => {
  93. dialog.showMessageBox({ detail: 3.14 as any, message: '' });
  94. }).to.throw(/Detail must be a string/);
  95. expect(() => {
  96. dialog.showMessageBox({ checkboxLabel: false as any, message: '' });
  97. }).to.throw(/checkboxLabel must be a string/);
  98. });
  99. });
  100. describe('showMessageBox with signal', () => {
  101. afterEach(closeAllWindows);
  102. it('closes message box immediately', async () => {
  103. const controller = new AbortController();
  104. const signal = controller.signal;
  105. const w = new BrowserWindow();
  106. const p = dialog.showMessageBox(w, { signal, message: 'i am message' });
  107. controller.abort();
  108. const result = await p;
  109. expect(result.response).to.equal(0);
  110. });
  111. it('closes message box after a while', async () => {
  112. const controller = new AbortController();
  113. const signal = controller.signal;
  114. const w = new BrowserWindow();
  115. const p = dialog.showMessageBox(w, { signal, message: 'i am message' });
  116. await delay(500);
  117. controller.abort();
  118. const result = await p;
  119. expect(result.response).to.equal(0);
  120. });
  121. it('cancels message box', async () => {
  122. const controller = new AbortController();
  123. const signal = controller.signal;
  124. const w = new BrowserWindow();
  125. const p = dialog.showMessageBox(w, {
  126. signal,
  127. message: 'i am message',
  128. buttons: ['OK', 'Cancel'],
  129. cancelId: 1
  130. });
  131. controller.abort();
  132. const result = await p;
  133. expect(result.response).to.equal(1);
  134. });
  135. it('cancels message box after a while', async () => {
  136. const controller = new AbortController();
  137. const signal = controller.signal;
  138. const w = new BrowserWindow();
  139. const p = dialog.showMessageBox(w, {
  140. signal,
  141. message: 'i am message',
  142. buttons: ['OK', 'Cancel'],
  143. cancelId: 1
  144. });
  145. await delay(500);
  146. controller.abort();
  147. const result = await p;
  148. expect(result.response).to.equal(1);
  149. });
  150. });
  151. describe('showErrorBox', () => {
  152. it('throws errors when the options are invalid', () => {
  153. expect(() => {
  154. (dialog.showErrorBox as any)();
  155. }).to.throw(/Insufficient number of arguments/);
  156. expect(() => {
  157. dialog.showErrorBox(3 as any, 'four');
  158. }).to.throw(/Error processing argument at index 0/);
  159. expect(() => {
  160. dialog.showErrorBox('three', 4 as any);
  161. }).to.throw(/Error processing argument at index 1/);
  162. });
  163. });
  164. describe('showCertificateTrustDialog', () => {
  165. it('throws errors when the options are invalid', () => {
  166. expect(() => {
  167. (dialog.showCertificateTrustDialog as any)();
  168. }).to.throw(/options must be an object/);
  169. expect(() => {
  170. dialog.showCertificateTrustDialog({} as any);
  171. }).to.throw(/certificate must be an object/);
  172. expect(() => {
  173. dialog.showCertificateTrustDialog({ certificate: {} as any, message: false as any });
  174. }).to.throw(/message must be a string/);
  175. });
  176. });
  177. });