spellchecker-spec.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. import { BrowserWindow, Session, session } from 'electron/main';
  2. import { expect } from 'chai';
  3. import * as path from 'node:path';
  4. import * as fs from 'node:fs/promises';
  5. import * as http from 'node:http';
  6. import { closeWindow } from './lib/window-helpers';
  7. import { ifit, ifdescribe, listen } from './lib/spec-helpers';
  8. import { once } from 'node:events';
  9. import { setTimeout } from 'node:timers/promises';
  10. const features = process._linkedBinding('electron_common_features');
  11. const v8Util = process._linkedBinding('electron_common_v8_util');
  12. ifdescribe(features.isBuiltinSpellCheckerEnabled())('spellchecker', function () {
  13. this.timeout((process.env.IS_ASAN ? 200 : 20) * 1000);
  14. let w: BrowserWindow;
  15. async function rightClick () {
  16. const contextMenuPromise = once(w.webContents, 'context-menu');
  17. w.webContents.sendInputEvent({
  18. type: 'mouseDown',
  19. button: 'right',
  20. x: 43,
  21. y: 42
  22. });
  23. return (await contextMenuPromise)[1] as Electron.ContextMenuParams;
  24. }
  25. // When the page is just loaded, the spellchecker might not be ready yet. Since
  26. // there is no event to know the state of spellchecker, the only reliable way
  27. // to detect spellchecker is to keep checking with a busy loop.
  28. async function rightClickUntil (fn: (params: Electron.ContextMenuParams) => boolean) {
  29. const now = Date.now();
  30. const timeout = (process.env.IS_ASAN ? 180 : 10) * 1000;
  31. let contextMenuParams = await rightClick();
  32. while (!fn(contextMenuParams) && (Date.now() - now < timeout)) {
  33. await setTimeout(100);
  34. contextMenuParams = await rightClick();
  35. }
  36. return contextMenuParams;
  37. }
  38. // Setup a server to download hunspell dictionary.
  39. const server = http.createServer(async (req, res) => {
  40. // The provided is minimal dict for testing only, full list of words can
  41. // be found at src/third_party/hunspell_dictionaries/xx_XX.dic.
  42. try {
  43. const data = await fs.readFile(path.join(__dirname, '/../../third_party/hunspell_dictionaries/xx-XX-3-0.bdic'));
  44. res.writeHead(200);
  45. res.end(data);
  46. } catch (err) {
  47. console.error('Failed to read dictionary file');
  48. res.writeHead(404);
  49. res.end(JSON.stringify(err));
  50. }
  51. });
  52. let serverUrl: string;
  53. before(async () => {
  54. serverUrl = (await listen(server)).url;
  55. });
  56. after(() => server.close());
  57. const fixtures = path.resolve(__dirname, 'fixtures');
  58. const preload = path.join(fixtures, 'module', 'preload-electron.js');
  59. const generateSpecs = (description: string, sandbox: boolean) => {
  60. describe(description, () => {
  61. beforeEach(async () => {
  62. w = new BrowserWindow({
  63. show: false,
  64. webPreferences: {
  65. partition: `unique-spell-${Date.now()}`,
  66. contextIsolation: false,
  67. preload,
  68. sandbox
  69. }
  70. });
  71. w.webContents.session.setSpellCheckerDictionaryDownloadURL(serverUrl);
  72. w.webContents.session.setSpellCheckerLanguages(['en-US']);
  73. await w.loadFile(path.resolve(__dirname, './fixtures/chromium/spellchecker.html'));
  74. });
  75. afterEach(async () => {
  76. await closeWindow(w);
  77. });
  78. // Context menu test can not run on Windows.
  79. const shouldRun = process.platform !== 'win32';
  80. ifit(shouldRun)('should detect correctly spelled words as correct', async () => {
  81. await w.webContents.executeJavaScript('document.body.querySelector("textarea").value = "typography"');
  82. await w.webContents.executeJavaScript('document.body.querySelector("textarea").focus()');
  83. const contextMenuParams = await rightClickUntil((contextMenuParams) => contextMenuParams.selectionText.length > 0);
  84. expect(contextMenuParams.misspelledWord).to.eq('');
  85. expect(contextMenuParams.dictionarySuggestions).to.have.lengthOf(0);
  86. });
  87. ifit(shouldRun)('should detect incorrectly spelled words as incorrect', async () => {
  88. await w.webContents.executeJavaScript('document.body.querySelector("textarea").value = "typograpy"');
  89. await w.webContents.executeJavaScript('document.body.querySelector("textarea").focus()');
  90. const contextMenuParams = await rightClickUntil((contextMenuParams) => contextMenuParams.misspelledWord.length > 0);
  91. expect(contextMenuParams.misspelledWord).to.eq('typograpy');
  92. expect(contextMenuParams.dictionarySuggestions).to.have.length.of.at.least(1);
  93. });
  94. ifit(shouldRun)('should detect incorrectly spelled words as incorrect after disabling all languages and re-enabling', async () => {
  95. w.webContents.session.setSpellCheckerLanguages([]);
  96. await setTimeout(500);
  97. w.webContents.session.setSpellCheckerLanguages(['en-US']);
  98. await w.webContents.executeJavaScript('document.body.querySelector("textarea").value = "typograpy"');
  99. await w.webContents.executeJavaScript('document.body.querySelector("textarea").focus()');
  100. const contextMenuParams = await rightClickUntil((contextMenuParams) => contextMenuParams.misspelledWord.length > 0);
  101. expect(contextMenuParams.misspelledWord).to.eq('typograpy');
  102. expect(contextMenuParams.dictionarySuggestions).to.have.length.of.at.least(1);
  103. });
  104. ifit(shouldRun)('should expose webFrame spellchecker correctly', async () => {
  105. await w.webContents.executeJavaScript('document.body.querySelector("textarea").value = "typograpy"');
  106. await w.webContents.executeJavaScript('document.body.querySelector("textarea").focus()');
  107. await rightClickUntil((contextMenuParams) => contextMenuParams.misspelledWord.length > 0);
  108. const callWebFrameFn = (expr: string) => w.webContents.executeJavaScript(`electron.webFrame.${expr}`);
  109. expect(await callWebFrameFn('isWordMisspelled("typography")')).to.equal(false);
  110. expect(await callWebFrameFn('isWordMisspelled("typograpy")')).to.equal(true);
  111. expect(await callWebFrameFn('getWordSuggestions("typography")')).to.be.empty();
  112. expect(await callWebFrameFn('getWordSuggestions("typograpy")')).to.not.be.empty();
  113. });
  114. describe('spellCheckerEnabled', () => {
  115. it('is enabled by default', async () => {
  116. expect(w.webContents.session.spellCheckerEnabled).to.be.true();
  117. });
  118. ifit(shouldRun)('can be dynamically changed', async () => {
  119. await w.webContents.executeJavaScript('document.body.querySelector("textarea").value = "typograpy"');
  120. await w.webContents.executeJavaScript('document.body.querySelector("textarea").focus()');
  121. await rightClickUntil((contextMenuParams) => contextMenuParams.misspelledWord.length > 0);
  122. const callWebFrameFn = (expr: string) => w.webContents.executeJavaScript(`electron.webFrame.${expr}`);
  123. w.webContents.session.spellCheckerEnabled = false;
  124. v8Util.runUntilIdle();
  125. expect(w.webContents.session.spellCheckerEnabled).to.be.false();
  126. // spellCheckerEnabled is sent to renderer asynchronously and there is
  127. // no event notifying when it is finished, so wait a little while to
  128. // ensure the setting has been changed in renderer.
  129. await setTimeout(500);
  130. expect(await callWebFrameFn('isWordMisspelled("typograpy")')).to.equal(false);
  131. w.webContents.session.spellCheckerEnabled = true;
  132. v8Util.runUntilIdle();
  133. expect(w.webContents.session.spellCheckerEnabled).to.be.true();
  134. await setTimeout(500);
  135. expect(await callWebFrameFn('isWordMisspelled("typograpy")')).to.equal(true);
  136. });
  137. });
  138. describe('custom dictionary word list API', () => {
  139. let ses: Session;
  140. beforeEach(async () => {
  141. // ensure a new session runs on each test run
  142. ses = session.fromPartition(`persist:customdictionary-test-${Date.now()}`);
  143. });
  144. afterEach(async () => {
  145. if (ses) {
  146. await ses.clearStorageData();
  147. ses = null as any;
  148. }
  149. });
  150. describe('ses.listWordsFromSpellCheckerDictionary', () => {
  151. it('should successfully list words in custom dictionary', async () => {
  152. const words = ['foo', 'bar', 'baz'];
  153. const results = words.map(word => ses.addWordToSpellCheckerDictionary(word));
  154. expect(results).to.eql([true, true, true]);
  155. const wordList = await ses.listWordsInSpellCheckerDictionary();
  156. expect(wordList).to.have.deep.members(words);
  157. });
  158. it('should return an empty array if no words are added', async () => {
  159. const wordList = await ses.listWordsInSpellCheckerDictionary();
  160. expect(wordList).to.have.length(0);
  161. });
  162. });
  163. describe('ses.addWordToSpellCheckerDictionary', () => {
  164. it('should successfully add word to custom dictionary', async () => {
  165. const result = ses.addWordToSpellCheckerDictionary('foobar');
  166. expect(result).to.equal(true);
  167. const wordList = await ses.listWordsInSpellCheckerDictionary();
  168. expect(wordList).to.eql(['foobar']);
  169. });
  170. it('should fail for an empty string', async () => {
  171. const result = ses.addWordToSpellCheckerDictionary('');
  172. expect(result).to.equal(false);
  173. const wordList = await ses.listWordsInSpellCheckerDictionary;
  174. expect(wordList).to.have.length(0);
  175. });
  176. // remove API will always return false because we can't add words
  177. it('should fail for non-persistent sessions', async () => {
  178. const tempSes = session.fromPartition('temporary');
  179. const result = tempSes.addWordToSpellCheckerDictionary('foobar');
  180. expect(result).to.equal(false);
  181. });
  182. });
  183. describe('ses.setSpellCheckerLanguages', () => {
  184. const isMac = process.platform === 'darwin';
  185. ifit(isMac)('should be a no-op when setSpellCheckerLanguages is called on macOS', () => {
  186. expect(() => {
  187. w.webContents.session.setSpellCheckerLanguages(['i-am-a-nonexistent-language']);
  188. }).to.not.throw();
  189. });
  190. ifit(!isMac)('should throw when a bad language is passed', () => {
  191. expect(() => {
  192. w.webContents.session.setSpellCheckerLanguages(['i-am-a-nonexistent-language']);
  193. }).to.throw(/Invalid language code provided: "i-am-a-nonexistent-language" is not a valid language code/);
  194. });
  195. ifit(!isMac)('should not throw when a recognized language is passed', () => {
  196. expect(() => {
  197. w.webContents.session.setSpellCheckerLanguages(['es']);
  198. }).to.not.throw();
  199. });
  200. });
  201. describe('SetSpellCheckerDictionaryDownloadURL', () => {
  202. const isMac = process.platform === 'darwin';
  203. ifit(isMac)('should be a no-op when a bad url is passed on macOS', () => {
  204. expect(() => {
  205. w.webContents.session.setSpellCheckerDictionaryDownloadURL('i-am-not-a-valid-url');
  206. }).to.not.throw();
  207. });
  208. ifit(!isMac)('should throw when a bad url is passed', () => {
  209. expect(() => {
  210. w.webContents.session.setSpellCheckerDictionaryDownloadURL('i-am-not-a-valid-url');
  211. }).to.throw(/The URL you provided to setSpellCheckerDictionaryDownloadURL is not a valid URL/);
  212. });
  213. });
  214. describe('ses.removeWordFromSpellCheckerDictionary', () => {
  215. it('should successfully remove words to custom dictionary', async () => {
  216. const result1 = ses.addWordToSpellCheckerDictionary('foobar');
  217. expect(result1).to.equal(true);
  218. const wordList1 = await ses.listWordsInSpellCheckerDictionary();
  219. expect(wordList1).to.eql(['foobar']);
  220. const result2 = ses.removeWordFromSpellCheckerDictionary('foobar');
  221. expect(result2).to.equal(true);
  222. const wordList2 = await ses.listWordsInSpellCheckerDictionary();
  223. expect(wordList2).to.have.length(0);
  224. });
  225. it('should fail for words not in custom dictionary', () => {
  226. const result2 = ses.removeWordFromSpellCheckerDictionary('foobar');
  227. expect(result2).to.equal(false);
  228. });
  229. });
  230. });
  231. });
  232. };
  233. generateSpecs('without sandbox', false);
  234. generateSpecs('with sandbox', true);
  235. });