spellchecker-spec.ts 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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.electronBinding('features');
  8. ifdescribe(features.isBuiltinSpellCheckerEnabled())('spellchecker', () => {
  9. let w: BrowserWindow;
  10. beforeEach(async () => {
  11. w = new BrowserWindow({
  12. show: false
  13. });
  14. await w.loadFile(path.resolve(__dirname, './fixtures/chromium/spellchecker.html'));
  15. });
  16. afterEach(async () => {
  17. await closeWindow(w);
  18. });
  19. // Context menu test can not run on Windows, and it is not reliable on ARM
  20. // CI machines.
  21. const shouldRun = process.platform !== 'win32' &&
  22. process.arch !== 'arm' &&
  23. process.arch !== 'arm64';
  24. ifit(shouldRun)('should detect correctly spelled words as correct', async () => {
  25. await w.webContents.executeJavaScript('document.body.querySelector("textarea").value = "Beautiful and lovely"');
  26. await w.webContents.executeJavaScript('document.body.querySelector("textarea").focus()');
  27. const contextMenuPromise = emittedOnce(w.webContents, 'context-menu');
  28. // Wait for spellchecker to load
  29. await delay(500);
  30. w.webContents.sendInputEvent({
  31. type: 'mouseDown',
  32. button: 'right',
  33. x: 43,
  34. y: 42
  35. });
  36. const contextMenuParams: Electron.ContextMenuParams = (await contextMenuPromise)[1];
  37. expect(contextMenuParams.misspelledWord).to.eq('');
  38. expect(contextMenuParams.dictionarySuggestions).to.have.lengthOf(0);
  39. });
  40. ifit(shouldRun)('should detect incorrectly spelled words as incorrect', async () => {
  41. await w.webContents.executeJavaScript('document.body.querySelector("textarea").value = "Beautifulllll asd asd"');
  42. await w.webContents.executeJavaScript('document.body.querySelector("textarea").focus()');
  43. const contextMenuPromise = emittedOnce(w.webContents, 'context-menu');
  44. // Wait for spellchecker to load
  45. await delay(500);
  46. w.webContents.sendInputEvent({
  47. type: 'mouseDown',
  48. button: 'right',
  49. x: 43,
  50. y: 42
  51. });
  52. const contextMenuParams: Electron.ContextMenuParams = (await contextMenuPromise)[1];
  53. expect(contextMenuParams.misspelledWord).to.eq('Beautifulllll');
  54. expect(contextMenuParams.dictionarySuggestions).to.have.length.of.at.least(1);
  55. });
  56. describe('custom dictionary word list API', () => {
  57. let ses: Session;
  58. beforeEach(async () => {
  59. // ensure a new session runs on each test run
  60. ses = session.fromPartition(`persist:customdictionary-test-${Date.now()}`);
  61. });
  62. afterEach(async () => {
  63. if (ses) {
  64. await ses.clearStorageData();
  65. ses = null as any;
  66. }
  67. });
  68. describe('ses.listWordsFromSpellCheckerDictionary', () => {
  69. it('should successfully list words in custom dictionary', async () => {
  70. const words = ['foo', 'bar', 'baz'];
  71. const results = words.map(word => ses.addWordToSpellCheckerDictionary(word));
  72. expect(results).to.eql([true, true, true]);
  73. const wordList = await ses.listWordsInSpellCheckerDictionary();
  74. expect(wordList).to.have.deep.members(words);
  75. });
  76. it('should return an empty array if no words are added', async () => {
  77. const wordList = await ses.listWordsInSpellCheckerDictionary();
  78. expect(wordList).to.have.length(0);
  79. });
  80. });
  81. describe('ses.addWordToSpellCheckerDictionary', () => {
  82. it('should successfully add word to custom dictionary', async () => {
  83. const result = ses.addWordToSpellCheckerDictionary('foobar');
  84. expect(result).to.equal(true);
  85. const wordList = await ses.listWordsInSpellCheckerDictionary();
  86. expect(wordList).to.eql(['foobar']);
  87. });
  88. it('should fail for an empty string', async () => {
  89. const result = ses.addWordToSpellCheckerDictionary('');
  90. expect(result).to.equal(false);
  91. const wordList = await ses.listWordsInSpellCheckerDictionary;
  92. expect(wordList).to.have.length(0);
  93. });
  94. // remove API will always return false because we can't add words
  95. it('should fail for non-persistent sessions', async () => {
  96. const tempSes = session.fromPartition('temporary');
  97. const result = tempSes.addWordToSpellCheckerDictionary('foobar');
  98. expect(result).to.equal(false);
  99. });
  100. });
  101. describe('ses.removeWordFromSpellCheckerDictionary', () => {
  102. it('should successfully remove words to custom dictionary', async () => {
  103. const result1 = ses.addWordToSpellCheckerDictionary('foobar');
  104. expect(result1).to.equal(true);
  105. const wordList1 = await ses.listWordsInSpellCheckerDictionary();
  106. expect(wordList1).to.eql(['foobar']);
  107. const result2 = ses.removeWordFromSpellCheckerDictionary('foobar');
  108. expect(result2).to.equal(true);
  109. const wordList2 = await ses.listWordsInSpellCheckerDictionary();
  110. expect(wordList2).to.have.length(0);
  111. });
  112. it('should fail for words not in custom dictionary', () => {
  113. const result2 = ses.removeWordFromSpellCheckerDictionary('foobar');
  114. expect(result2).to.equal(false);
  115. });
  116. });
  117. });
  118. });