-
Notifications
You must be signed in to change notification settings - Fork 703
/
test-main.js
83 lines (66 loc) · 1.92 KB
/
test-main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// Electron main process for TESTS ONLY
// This should never be used outside of tests.
if (process.env.NODE_ENV !== 'test') {
throw new Error('Cannot run test-main.js outside of tests!');
}
const path = require('path');
const electron = require('electron');
// Inject fake context menu
(() => {
let currentMenu;
electron.Menu.prototype.popup = function popup() {
currentMenu = this;
};
function getItem(menu, label) {
const found = menu.items.find(item => {
return item.label.indexOf(label) !== -1;
});
return found.submenu ?? found;
}
electron.ipcMain.on('__WEBDRIVER_FAKE_CONTEXT_MENU', (e, label) => {
if (typeof label === 'string') {
getItem(currentMenu, label).click();
} else if (Array.isArray(label)) {
let current = currentMenu;
const path = [...label];
while (path.length > 0) {
current = getItem(current, path.shift());
}
current.click();
}
});
})();
// Inject fake dialog
(() => {
let currentCb;
let currentButtons;
electron.dialog.showMessageBox = function showMessageBox(win, opts) {
return new Promise(resolve => {
currentCb = resolve;
// Support alternate function signature where win is omitted
if (win.buttons) {
currentButtons = win.buttons;
} else {
currentButtons = opts.buttons;
}
});
};
electron.ipcMain.on('__WEBDRIVER_FAKE_MESSAGE_BOX', (e, buttonLabel) => {
currentButtons.forEach((button, index) => {
if (button === buttonLabel) currentCb({ response: index });
});
});
})();
(() => {
let currentCb;
electron.dialog.showSaveDialog = function showSaveDialog(win, opts) {
return new Promise(resolve => {
currentCb = resolve;
});
};
electron.ipcMain.on('__WEBDRIVER_FAKE_SAVE_DIALOG', (e, filePath) => {
currentCb({ filePath });
});
})();
// Run the real application main process
require(path.join(__dirname, 'main.js'));