From 1ef82d220402b656364de7f6d9e8e41c7329dde5 Mon Sep 17 00:00:00 2001 From: Kiran Niranjan Date: Wed, 26 Jul 2017 15:34:26 +0530 Subject: [PATCH] Electron-47 1. Handled spellchecker issue for pop outs 2. Wrote spectron test for spellchecker --- js/preload/preloadMain.js | 20 +++- js/spellChecker/spellChecker.js | 26 ++++- tests/spectron/spellChecker.spectron.js | 120 ++++++++++++++++++++++++ 3 files changed, 159 insertions(+), 7 deletions(-) create mode 100644 tests/spectron/spellChecker.spectron.js diff --git a/js/preload/preloadMain.js b/js/preload/preloadMain.js index 6ff38854..ffc340ee 100644 --- a/js/preload/preloadMain.js +++ b/js/preload/preloadMain.js @@ -18,7 +18,21 @@ const apiEnums = require('../enums/api.js'); const apiCmds = apiEnums.cmds; const apiName = apiEnums.apiName; const getMediaSources = require('../desktopCapturer/getSources'); -const SpellCheckerHelper = require('../spellChecker/spellChecker').SpellCheckHelper; + +// bug in electron preventing us from using spellchecker in pop outs +// https://github.com/electron/electron/issues/4025 +// so loading the spellchecker in try catch so that we don't +// block other method from loading +try { + const SpellCheckerHelper = require('../spellChecker/spellChecker').SpellCheckHelper; + // Method to initialize spell checker + const spellChecker = new SpellCheckerHelper(); + spellChecker.initializeSpellChecker(); +} catch (err){ + /* eslint-disable no-console */ + console.error('requiring spellchecker module: ' + err); + /* eslint-enable no-console */ +} require('../downloadManager/downloadManager'); @@ -329,8 +343,4 @@ function createAPI() { window.addEventListener('online', updateOnlineStatus, false); updateOnlineStatus(); - - // Method to initialize spell checker - const spellChecker = new SpellCheckerHelper(); - spellChecker.initializeSpellChecker(); } diff --git a/js/spellChecker/spellChecker.js b/js/spellChecker/spellChecker.js index 56d91fdd..2a8a2092 100644 --- a/js/spellChecker/spellChecker.js +++ b/js/spellChecker/spellChecker.js @@ -1,3 +1,5 @@ +const { remote } = require('electron'); +const { MenuItem } = remote; const { SpellCheckHandler, ContextMenuListener, ContextMenuBuilder } = require('electron-spellchecker'); class SpellCheckHelper { @@ -7,17 +9,37 @@ class SpellCheckHelper { } /** - * To initialize for a specific window + * Method to initialize spell checker */ initializeSpellChecker() { this.spellCheckHandler.attachToInput(); - const contextMenuBuilder = new ContextMenuBuilder(this.spellCheckHandler); + const contextMenuBuilder = new ContextMenuBuilder(this.spellCheckHandler, null, false, SpellCheckHelper.processMenu); this.contextMenuListener = new ContextMenuListener((info) => { contextMenuBuilder.showPopupMenu(info); }, null, null); } + /** + * Method to add default menu items to the + * menu that was generated by ContextMenuBuilder + * + * This method will be invoked by electron-spellchecker + * before showing the context menu + * + * @param menu + * @returns menu + */ + static processMenu(menu) { + menu.append(new MenuItem({ type: 'separator' })); + menu.append(new MenuItem({ + role: 'reload', + accelerator: 'CmdOrCtrl+R', + label: 'Reload' + })); + return menu; + } + } module.exports = { diff --git a/tests/spectron/spellChecker.spectron.js b/tests/spectron/spellChecker.spectron.js new file mode 100644 index 00000000..204c8753 --- /dev/null +++ b/tests/spectron/spellChecker.spectron.js @@ -0,0 +1,120 @@ +const Application = require('./spectronSetup'); +const path = require('path'); +const {isMac} = require('../../js/utils/misc.js'); +const childProcess = require('child_process'); +let app = new Application({}); +let robot; + +describe('Tests for spellChecker', () => { + + let originalTimeout = jasmine.DEFAULT_TIMEOUT_INTERVAL; + jasmine.DEFAULT_TIMEOUT_INTERVAL = Application.getTimeOut(); + + beforeAll((done) => { + childProcess.exec(`npm rebuild robotjs --target=${process.version} --build-from-source`, function () { + robot = require('robotjs'); + app.startApplication().then((startedApp) => { + app = startedApp; + done(); + }); + }); + }); + + afterAll((done) => { + if (app && app.isRunning()) { + jasmine.DEFAULT_TIMEOUT_INTERVAL = originalTimeout; + app.stop().then(() => { + done(); + }).catch((err) => { + console.log(err); + done(); + }); + } + }); + + it('should launch the app', (done) => { + return app.client.waitUntilWindowLoaded().then(() => { + return app.client.getWindowCount().then((count) => { + expect(count === 1).toBeTruthy(); + done(); + }).catch((err) => { + expect(err).toBeFalsy(); + }); + }).catch((err) => { + expect(err).toBeFalsy(); + }); + }); + + it('should check window count', () => { + return app.client.url('file:///' + path.join(__dirname, '..', '..', 'demo/index.html')); + }); + + it('should set the misspelled word', () => { + return app.client + .windowByIndex(0) + .setValue('#tag', 'comming ') + .getValue('#tag').then((value) => { + expect(value === 'comming ').toBeTruthy(); + }); + }); + + it('should bring the app to front in windows', (done) => { + if (!isMac) { + app.browserWindow.focus(); + app.browserWindow.restore(); + app.browserWindow.getBounds().then((bounds) => { + robot.setMouseDelay(100); + let x = bounds.x + 200; + let y = bounds.y + 200; + robot.moveMouseSmooth(x, y); + robot.mouseClick(); + done(); + }); + } else { + done(); + } + }); + + it('should invoke context menu ', (done) => { + if (isMac) { + app.browserWindow.getBounds().then((bounds) => { + let x = bounds.x + 45; + let y = bounds.y + 398; + + robot.moveMouseSmooth(x, y); + robot.setMouseDelay(200); + robot.mouseClick('left', true); + robot.mouseClick('right'); + robot.setKeyboardDelay(500); + robot.keyTap('down'); + robot.keyTap('down'); + robot.keyTap('enter'); + done(); + }); + } else { + app.browserWindow.getBounds().then((bounds) => { + let x = bounds.x + 55; + let y = bounds.y + 430; + + robot.moveMouseSmooth(x, y); + robot.setMouseDelay(200); + robot.mouseClick('left', true); + robot.mouseClick('right'); + robot.setKeyboardDelay(500); + robot.keyTap('down'); + robot.keyTap('down'); + robot.keyTap('enter'); + done(); + }); + } + }); + + it('should verify the text field', () => { + return app.client + .windowByIndex(0) + .getValue('#tag').then((value) => { + expect(value).toBe('coming '); + }); + }); + +}); \ No newline at end of file