spellchecker-spec.ts 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. import { BrowserWindow, Session, session } from 'electron/main';
  2. import { expect } from 'chai';
  3. import * as path from 'path';
  4. import { closeWindow } from './window-helpers';
  5. import { emittedOnce } from './events-helpers';
  6. import { ifit, ifdescribe, delay } from './spec-helpers';
  7. const features = process._linkedBinding('electron_common_features');
  8. const v8Util = process._linkedBinding('electron_common_v8_util');
  9. ifdescribe(features.isBuiltinSpellCheckerEnabled())('spellchecker', () => {
  10. let w: BrowserWindow;
  11. async function rightClick () {
  12. const contextMenuPromise = emittedOnce(w.webContents, 'context-menu');
  13. w.webContents.sendInputEvent({
  14. type: 'mouseDown',
  15. button: 'right',
  16. x: 43,
  17. y: 42
  18. });
  19. return (await contextMenuPromise)[1] as Electron.ContextMenuParams;
  20. }
  21. // When the page is just loaded, the spellchecker might not be ready yet. Since
  22. // there is no event to know the state of spellchecker, the only reliable way
  23. // to detect spellchecker is to keep checking with a busy loop.
  24. async function rightClickUntil (fn: (params: Electron.ContextMenuParams) => boolean) {
  25. const now = Date.now();
  26. const timeout = 10 * 1000;
  27. let contextMenuParams = await rightClick();
  28. while (!fn(contextMenuParams) && (Date.now() - now < timeout)) {
  29. await delay(100);
  30. contextMenuParams = await rightClick();
  31. }
  32. return contextMenuParams;
  33. }
  34. const fixtures = path.resolve(__dirname, '../spec/fixtures');
  35. const preload = path.join(fixtures, 'module', 'preload-electron.js');
  36. const generateSpecs = (description: string, sandbox: boolean) => {
  37. describe(description, () => {
  38. beforeEach(async () => {
  39. w = new BrowserWindow({
  40. show: false,
  41. webPreferences: {
  42. partition: `unique-spell-${Date.now()}`,
  43. contextIsolation: false,
  44. preload,
  45. sandbox
  46. }
  47. });
  48. w.webContents.session.setSpellCheckerLanguages(['en-US']);
  49. await w.loadFile(path.resolve(__dirname, './fixtures/chromium/spellchecker.html'));
  50. });
  51. afterEach(async () => {
  52. await closeWindow(w);
  53. });
  54. // Context menu test can not run on Windows.
  55. const shouldRun = process.platform !== 'win32';
  56. ifit(shouldRun)('should detect correctly spelled words as correct', async () => {
  57. await w.webContents.executeJavaScript('document.body.querySelector("textarea").value = "Beautiful and lovely"');
  58. await w.webContents.executeJavaScript('document.body.querySelector("textarea").focus()');
  59. const contextMenuParams = await rightClickUntil((contextMenuParams) => contextMenuParams.selectionText.length > 0);
  60. expect(contextMenuParams.misspelledWord).to.eq('');
  61. expect(contextMenuParams.dictionarySuggestions).to.have.lengthOf(0);
  62. });
  63. ifit(shouldRun)('should detect incorrectly spelled words as incorrect', async () => {
  64. await w.webContents.executeJavaScript('document.body.querySelector("textarea").value = "Beautifulllll asd asd"');
  65. await w.webContents.executeJavaScript('document.body.querySelector("textarea").focus()');
  66. const contextMenuParams = await rightClickUntil((contextMenuParams) => contextMenuParams.misspelledWord.length > 0);
  67. expect(contextMenuParams.misspelledWord).to.eq('Beautifulllll');
  68. expect(contextMenuParams.dictionarySuggestions).to.have.length.of.at.least(1);
  69. });
  70. ifit(shouldRun)('should detect incorrectly spelled words as incorrect after disabling all languages and re-enabling', async () => {
  71. w.webContents.session.setSpellCheckerLanguages([]);
  72. await delay(500);
  73. w.webContents.session.setSpellCheckerLanguages(['en-US']);
  74. await w.webContents.executeJavaScript('document.body.querySelector("textarea").value = "Beautifulllll asd asd"');
  75. await w.webContents.executeJavaScript('document.body.querySelector("textarea").focus()');
  76. const contextMenuParams = await rightClickUntil((contextMenuParams) => contextMenuParams.misspelledWord.length > 0);
  77. expect(contextMenuParams.misspelledWord).to.eq('Beautifulllll');
  78. expect(contextMenuParams.dictionarySuggestions).to.have.length.of.at.least(1);
  79. });
  80. ifit(shouldRun)('should expose webFrame spellchecker correctly', async () => {
  81. await w.webContents.executeJavaScript('document.body.querySelector("textarea").value = "Beautifulllll asd asd"');
  82. await w.webContents.executeJavaScript('document.body.querySelector("textarea").focus()');
  83. await rightClickUntil((contextMenuParams) => contextMenuParams.misspelledWord.length > 0);
  84. const callWebFrameFn = (expr: string) => w.webContents.executeJavaScript(`electron.webFrame.${expr}`);
  85. expect(await callWebFrameFn('isWordMisspelled("test")')).to.equal(false);
  86. expect(await callWebFrameFn('isWordMisspelled("testt")')).to.equal(true);
  87. expect(await callWebFrameFn('getWordSuggestions("test")')).to.be.empty();
  88. expect(await callWebFrameFn('getWordSuggestions("testt")')).to.not.be.empty();
  89. });
  90. describe('spellCheckerEnabled', () => {
  91. it('is enabled by default', async () => {
  92. expect(w.webContents.session.spellCheckerEnabled).to.be.true();
  93. });
  94. ifit(shouldRun)('can be dynamically changed', async () => {
  95. await w.webContents.executeJavaScript('document.body.querySelector("textarea").value = "Beautifulllll asd asd"');
  96. await w.webContents.executeJavaScript('document.body.querySelector("textarea").focus()');
  97. await rightClickUntil((contextMenuParams) => contextMenuParams.misspelledWord.length > 0);
  98. const callWebFrameFn = (expr: string) => w.webContents.executeJavaScript(`electron.webFrame.${expr}`);
  99. w.webContents.session.spellCheckerEnabled = false;
  100. v8Util.runUntilIdle();
  101. expect(w.webContents.session.spellCheckerEnabled).to.be.false();
  102. expect(await callWebFrameFn('isWordMisspelled("testt")')).to.equal(false);
  103. w.webContents.session.spellCheckerEnabled = true;
  104. v8Util.runUntilIdle();
  105. expect(w.webContents.session.spellCheckerEnabled).to.be.true();
  106. expect(await callWebFrameFn('isWordMisspelled("testt")')).to.equal(true);
  107. });
  108. });
  109. describe('custom dictionary word list API', () => {
  110. let ses: Session;
  111. beforeEach(async () => {
  112. // ensure a new session runs on each test run
  113. ses = session.fromPartition(`persist:customdictionary-test-${Date.now()}`);
  114. });
  115. afterEach(async () => {
  116. if (ses) {
  117. await ses.clearStorageData();
  118. ses = null as any;
  119. }
  120. });
  121. describe('ses.listWordsFromSpellCheckerDictionary', () => {
  122. it('should successfully list words in custom dictionary', async () => {
  123. const words = ['foo', 'bar', 'baz'];
  124. const results = words.map(word => ses.addWordToSpellCheckerDictionary(word));
  125. expect(results).to.eql([true, true, true]);
  126. const wordList = await ses.listWordsInSpellCheckerDictionary();
  127. expect(wordList).to.have.deep.members(words);
  128. });
  129. it('should return an empty array if no words are added', async () => {
  130. const wordList = await ses.listWordsInSpellCheckerDictionary();
  131. expect(wordList).to.have.length(0);
  132. });
  133. });
  134. describe('ses.addWordToSpellCheckerDictionary', () => {
  135. it('should successfully add word to custom dictionary', async () => {
  136. const result = ses.addWordToSpellCheckerDictionary('foobar');
  137. expect(result).to.equal(true);
  138. const wordList = await ses.listWordsInSpellCheckerDictionary();
  139. expect(wordList).to.eql(['foobar']);
  140. });
  141. it('should fail for an empty string', async () => {
  142. const result = ses.addWordToSpellCheckerDictionary('');
  143. expect(result).to.equal(false);
  144. const wordList = await ses.listWordsInSpellCheckerDictionary;
  145. expect(wordList).to.have.length(0);
  146. });
  147. // remove API will always return false because we can't add words
  148. it('should fail for non-persistent sessions', async () => {
  149. const tempSes = session.fromPartition('temporary');
  150. const result = tempSes.addWordToSpellCheckerDictionary('foobar');
  151. expect(result).to.equal(false);
  152. });
  153. });
  154. describe('ses.removeWordFromSpellCheckerDictionary', () => {
  155. it('should successfully remove words to custom dictionary', async () => {
  156. const result1 = ses.addWordToSpellCheckerDictionary('foobar');
  157. expect(result1).to.equal(true);
  158. const wordList1 = await ses.listWordsInSpellCheckerDictionary();
  159. expect(wordList1).to.eql(['foobar']);
  160. const result2 = ses.removeWordFromSpellCheckerDictionary('foobar');
  161. expect(result2).to.equal(true);
  162. const wordList2 = await ses.listWordsInSpellCheckerDictionary();
  163. expect(wordList2).to.have.length(0);
  164. });
  165. it('should fail for words not in custom dictionary', () => {
  166. const result2 = ses.removeWordFromSpellCheckerDictionary('foobar');
  167. expect(result2).to.equal(false);
  168. });
  169. });
  170. });
  171. });
  172. };
  173. generateSpecs('without sandbox', false);
  174. generateSpecs('with sandbox', true);
  175. });