Skip to content
This repository has been archived by the owner on Apr 15, 2019. It is now read-only.

Commit

Permalink
Merge pull request #970 from LiskHQ/900-electron-tests
Browse files Browse the repository at this point in the history
Write unit tests for electron code - Closes #900
  • Loading branch information
gina contrino authored Nov 15, 2017
2 parents 6f7632c + c2253b2 commit aa9dd5e
Show file tree
Hide file tree
Showing 8 changed files with 586 additions and 276 deletions.
158 changes: 16 additions & 142 deletions app/src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,129 +2,12 @@ import electron from 'electron'; // eslint-disable-line import/no-extraneous-dep
import electronLocalshortcut from 'electron-localshortcut'; // eslint-disable-line import/no-extraneous-dependencies
import path from 'path';
import storage from 'electron-json-storage'; // eslint-disable-line import/no-extraneous-dependencies
import i18n from './i18n';
import buildMenu from './menu';
import win from './modules/win';
import localeHandler from './modules/localeHandler';

const { app, ipcMain } = electron;

const { app, BrowserWindow, Menu, ipcMain } = electron;

let win;
let isUILoaded = false;
let eventStack = [];
// @todo change en with the detected lang
const defaultLng = 'en';
let lang;

const copyright = `Copyright © 2016 - ${new Date().getFullYear()} Lisk Foundation`;
const protocolName = 'lisk';

const sendUrlToRouter = (url) => {
if (isUILoaded && win && win.webContents) {
win.webContents.send('openUrl', url);
} else {
eventStack.push({ event: 'openUrl', value: url });
}
};

/**
* Sends an event to client application
* @param {String} locale - the 2 letter name of the local
*/
const sendDetectedLang = (locale) => {
if (isUILoaded && win && win.webContents) {
win.webContents.send('detectedLocale', locale);
} else {
eventStack.push({ event: 'detectedLocale', value: locale });
}
};

// read config data from JSON file
const getConfig = () => {
storage.get('config', (error, data) => {
if (!error) {
lang = data.lang;
sendDetectedLang(lang);
}
});
};

getConfig();

function createWindow() {
// set language of the react app
if (lang) {
sendDetectedLang(lang);
} else {
sendDetectedLang(defaultLng);
}

const { width, height } = electron.screen.getPrimaryDisplay().workAreaSize;
win = new BrowserWindow({
width: width > 2000 ? Math.floor(width * 0.5) : width - 250,
height: height > 1000 ? Math.floor(height * 0.7) : height - 150,
center: true,
webPreferences: {
// Avoid app throttling when Electron is in background
backgroundThrottling: false,
// Specifies a script that will be loaded before other scripts run in the page.
preload: path.resolve(__dirname, '../src/ipc.js'),
},
});
win.on('blur', () => win.webContents.send('blur'));
win.on('focus', () => win.webContents.send('focus'));

if (process.platform !== 'darwin') {
sendUrlToRouter(process.argv[1] || '/');
}

Menu.setApplicationMenu(buildMenu(app, copyright, i18n));
win.loadURL(`file://${__dirname}/index.html`);

// Enables DevTools
win.devtools = true;
electronLocalshortcut.register(win, 'CmdOrCtrl+Shift+I', () => {
win.webContents.toggleDevTools();
});

win.on('closed', () => { win = null; });

const selectionMenu = Menu.buildFromTemplate([
{ role: 'copy' },
{ type: 'separator' },
{ role: 'selectall' },
]);

const inputMenu = Menu.buildFromTemplate([
{ role: 'undo' },
{ role: 'redo' },
{ type: 'separator' },
{ role: 'cut' },
{ role: 'copy' },
{ role: 'paste' },
{ type: 'separator' },
{ role: 'selectall' },
]);

win.webContents.on('context-menu', (e, props) => {
const { selectionText, isEditable } = props;
if (isEditable) {
inputMenu.popup(win);
} else if (selectionText && selectionText.trim() !== '') {
selectionMenu.popup(win);
}
});

// Resolve all events from stack when dom is ready
win.webContents.on('did-finish-load', () => {
isUILoaded = true;
if (eventStack.length > 0) {
eventStack.forEach(({ event, value }) => win.webContents.send(event, value));
eventStack = [];
}
});
}

app.on('ready', createWindow);
app.on('ready', () => { win.create({ electron, path, electronLocalshortcut, storage }); });

app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
Expand All @@ -134,29 +17,27 @@ app.on('window-all-closed', () => {

// This will override the values defined in the app’s .plist file (macOS)
if (process.platform === 'darwin') {
app.setAboutPanelOptions({
applicationName: 'Lisk Nano',
copyright,
});
const copyright = `Copyright © 2016 - ${new Date().getFullYear()} Lisk Foundation`;
app.setAboutPanelOptions({ applicationName: 'Lisk Nano', copyright });
}

app.on('activate', () => {
if (win === null) {
createWindow();
if (win.browser === null) {
win.create({ electron, path, electronLocalshortcut, storage });
}
});

// Set app protocol
app.setAsDefaultProtocolClient(protocolName);
app.setAsDefaultProtocolClient('lisk');

// Force single instance application
const isSecondInstance = app.makeSingleInstance((argv) => {
if (process.platform !== 'darwin') {
sendUrlToRouter(argv[1] || '/');
win.send({ event: 'openUrl', value: argv[1] || '/' });
}
if (win) {
if (win.isMinimized()) win.restore();
win.focus();
if (win.browser) {
if (win.browser.isMinimized()) win.browser.restore();
win.browser.focus();
}
});

Expand All @@ -168,7 +49,7 @@ app.on('will-finish-launching', () => {
// Protocol handler for MacOS
app.on('open-url', (event, url) => {
event.preventDefault();
sendUrlToRouter(url);
win.send({ event: 'openUrl', value: url });
});
});

Expand All @@ -185,17 +66,10 @@ ipcMain.on('proxyCredentialsEntered', (event, username, password) => {
ipcMain.on('set-locale', (event, locale) => {
const langCode = locale.substr(0, 2);
if (langCode) {
lang = langCode;
i18n.changeLanguage(langCode);
// write selected lang on JSON file
storage.set('config', { lang: langCode }, (error) => {
if (error) throw error;
});
Menu.setApplicationMenu(buildMenu(app, copyright, i18n));
event.returnValue = 'Rebuilt electron menu.';
localeHandler.update({ langCode, electron, storage, event });
}
});

ipcMain.on('request-locale', () => {
getConfig();
localeHandler.send({ storage });
});
Loading

0 comments on commit aa9dd5e

Please sign in to comment.