2019-01-22 09:12:16 -06:00
|
|
|
import { EventEmitter } from 'events';
|
|
|
|
import * as path from 'path';
|
2019-03-11 09:25:34 -05:00
|
|
|
import { isWindowsOS } from '../../src/common/env';
|
2019-01-22 09:12:16 -06:00
|
|
|
const ipcEmitter = new EventEmitter();
|
|
|
|
|
2019-02-04 08:23:58 -06:00
|
|
|
const mockIdleTime: number = 15;
|
2019-01-22 09:12:16 -06:00
|
|
|
const appName: string = 'Symphony';
|
2019-02-04 08:23:58 -06:00
|
|
|
const executableName: string = '/Symphony.exe';
|
|
|
|
const isReady: boolean = true;
|
2019-02-11 21:54:46 -06:00
|
|
|
const version: string = '4.0.0';
|
2019-05-07 03:46:59 -05:00
|
|
|
|
2019-01-22 09:12:16 -06:00
|
|
|
interface IApp {
|
2021-01-29 04:23:22 -06:00
|
|
|
commandLine: any;
|
|
|
|
getAppPath(): string;
|
|
|
|
getPath(type: string): string;
|
|
|
|
getName(): string;
|
|
|
|
isReady(): boolean;
|
|
|
|
getVersion(): string;
|
|
|
|
on(eventName: any, cb: any): void;
|
|
|
|
once(eventName: any, cb: any): void;
|
|
|
|
setPath(value: string, path: string): void;
|
|
|
|
setLoginItemSettings(settings: { openAtLogin: boolean; path: string }): void;
|
|
|
|
getLoginItemSettings(options?: {
|
|
|
|
path: string;
|
|
|
|
args: string[];
|
|
|
|
}): ILoginItemSettings;
|
|
|
|
setAppLogsPath(): void;
|
2019-05-07 02:56:57 -05:00
|
|
|
}
|
2019-05-07 03:46:59 -05:00
|
|
|
interface ILoginItemSettings {
|
2021-01-29 04:23:22 -06:00
|
|
|
openAtLogin: boolean;
|
2019-01-22 09:12:16 -06:00
|
|
|
}
|
|
|
|
interface IIpcMain {
|
2021-01-29 04:23:22 -06:00
|
|
|
on(event: any, cb: any): void;
|
2021-10-20 02:40:58 -05:00
|
|
|
handle(event: any, cb: any): Promise<void>;
|
2021-01-29 04:23:22 -06:00
|
|
|
send(event: any, cb: any): void;
|
2019-01-22 09:12:16 -06:00
|
|
|
}
|
|
|
|
interface IIpcRenderer {
|
2021-01-29 04:23:22 -06:00
|
|
|
sendSync(event: any, cb: any): any;
|
|
|
|
on(eventName: any, cb: any): void;
|
|
|
|
send(event: any, ...cb: any[]): void;
|
|
|
|
removeListener(eventName: any, cb: any): void;
|
|
|
|
once(eventName: any, cb: any): void;
|
2019-01-22 09:12:16 -06:00
|
|
|
}
|
2021-10-20 02:40:58 -05:00
|
|
|
interface IWebContents {
|
|
|
|
setWindowOpenHandler(details: any): any;
|
|
|
|
sendSync(event: any, cb: any): any;
|
|
|
|
on(eventName: any, cb: any): void;
|
|
|
|
send(event: any, ...cb: any[]): void;
|
|
|
|
removeListener(eventName: any, cb: any): void;
|
|
|
|
once(eventName: any, cb: any): void;
|
|
|
|
}
|
2019-02-04 08:23:58 -06:00
|
|
|
interface IPowerMonitor {
|
2021-01-29 04:23:22 -06:00
|
|
|
getSystemIdleTime(): void;
|
2019-02-04 08:23:58 -06:00
|
|
|
}
|
2019-01-22 09:12:16 -06:00
|
|
|
|
|
|
|
const pathToConfigDir = (): string => {
|
2021-01-29 04:23:22 -06:00
|
|
|
if (isWindowsOS) {
|
|
|
|
return path.join(__dirname, '/../..') as string;
|
|
|
|
} else {
|
|
|
|
return path.join(__dirname, '/..') as string;
|
|
|
|
}
|
2019-01-22 09:12:16 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
// electron app mock...
|
2019-03-13 02:00:59 -05:00
|
|
|
export const app: IApp = {
|
2021-01-29 04:23:22 -06:00
|
|
|
getAppPath: pathToConfigDir,
|
|
|
|
getPath: (type) => {
|
|
|
|
if (type === 'exe') {
|
|
|
|
return path.join(pathToConfigDir(), executableName);
|
|
|
|
}
|
|
|
|
if (type === 'userData') {
|
|
|
|
return path.join(pathToConfigDir(), '/../config');
|
|
|
|
}
|
|
|
|
return pathToConfigDir();
|
|
|
|
},
|
|
|
|
getName: () => appName,
|
|
|
|
isReady: () => isReady,
|
|
|
|
getVersion: () => version,
|
|
|
|
on: (event, cb) => {
|
|
|
|
ipcEmitter.on(event, cb);
|
|
|
|
},
|
|
|
|
setPath: () => jest.fn(),
|
|
|
|
commandLine: {
|
|
|
|
appendSwitch: jest.fn(),
|
|
|
|
},
|
|
|
|
once: (eventName, cb) => {
|
|
|
|
ipcEmitter.on(eventName, cb);
|
|
|
|
},
|
|
|
|
setLoginItemSettings: () => jest.fn(),
|
|
|
|
getLoginItemSettings: (): ILoginItemSettings => {
|
|
|
|
return { openAtLogin: true };
|
|
|
|
},
|
|
|
|
setAppLogsPath: (): void => {
|
|
|
|
return;
|
|
|
|
},
|
2019-01-22 09:12:16 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
// simple ipc mocks for render and main process ipc using
|
|
|
|
// nodes' EventEmitter
|
2019-03-13 02:00:59 -05:00
|
|
|
export const ipcMain: IIpcMain = {
|
2021-01-29 04:23:22 -06:00
|
|
|
on: (event, cb) => {
|
|
|
|
ipcEmitter.on(event, cb);
|
|
|
|
},
|
2021-10-20 02:40:58 -05:00
|
|
|
handle: (event, cb) => {
|
|
|
|
ipcEmitter.on(event, cb);
|
|
|
|
return Promise.resolve();
|
|
|
|
},
|
2021-01-29 04:23:22 -06:00
|
|
|
send: (event, args) => {
|
|
|
|
const senderEvent = {
|
|
|
|
sender: {
|
|
|
|
send: (eventSend, arg) => {
|
|
|
|
ipcEmitter.emit(eventSend, arg);
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
ipcEmitter.emit(event, senderEvent, args);
|
|
|
|
},
|
2019-01-22 09:12:16 -06:00
|
|
|
};
|
|
|
|
|
2019-03-13 02:00:59 -05:00
|
|
|
export const powerMonitor: IPowerMonitor = {
|
2021-01-29 04:23:22 -06:00
|
|
|
getSystemIdleTime: jest.fn().mockReturnValue(mockIdleTime),
|
2019-02-04 08:23:58 -06:00
|
|
|
};
|
|
|
|
|
2019-03-13 02:00:59 -05:00
|
|
|
export const ipcRenderer: IIpcRenderer = {
|
2021-01-29 04:23:22 -06:00
|
|
|
sendSync: (event, args) => {
|
|
|
|
const listeners = ipcEmitter.listeners(event);
|
|
|
|
if (listeners.length > 0) {
|
|
|
|
const listener = listeners[0];
|
|
|
|
const eventArg = {};
|
|
|
|
listener(eventArg, args);
|
|
|
|
return eventArg;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
},
|
|
|
|
send: (event, ...args) => {
|
|
|
|
const senderEvent = {
|
|
|
|
sender: {
|
|
|
|
send: (eventSend, ...arg) => {
|
|
|
|
ipcEmitter.emit(eventSend, ...arg);
|
|
|
|
},
|
|
|
|
},
|
|
|
|
preventDefault: jest.fn(),
|
|
|
|
};
|
|
|
|
ipcEmitter.emit(event, senderEvent, ...args);
|
|
|
|
},
|
|
|
|
on: (eventName, cb) => {
|
|
|
|
ipcEmitter.on(eventName, cb);
|
|
|
|
},
|
|
|
|
removeListener: (eventName, cb) => {
|
|
|
|
ipcEmitter.removeListener(eventName, cb);
|
|
|
|
},
|
|
|
|
once: (eventName, cb) => {
|
|
|
|
ipcEmitter.on(eventName, cb);
|
|
|
|
},
|
2019-01-22 09:12:16 -06:00
|
|
|
};
|
|
|
|
|
2021-10-20 02:40:58 -05:00
|
|
|
export const webContents: IWebContents = {
|
|
|
|
setWindowOpenHandler: (_details: {}) => {
|
|
|
|
return { action: 'allow' };
|
|
|
|
},
|
|
|
|
sendSync: (event, args) => {
|
|
|
|
const listeners = ipcEmitter.listeners(event);
|
|
|
|
if (listeners.length > 0) {
|
|
|
|
const listener = listeners[0];
|
|
|
|
const eventArg = {};
|
|
|
|
listener(eventArg, args);
|
|
|
|
return eventArg;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
},
|
|
|
|
send: (event, ...args) => {
|
|
|
|
const senderEvent = {
|
|
|
|
sender: {
|
|
|
|
send: (eventSend, ...arg) => {
|
|
|
|
ipcEmitter.emit(eventSend, ...arg);
|
|
|
|
},
|
|
|
|
},
|
|
|
|
preventDefault: jest.fn(),
|
|
|
|
};
|
|
|
|
ipcEmitter.emit(event, senderEvent, ...args);
|
|
|
|
},
|
|
|
|
on: (eventName, cb) => {
|
|
|
|
ipcEmitter.on(eventName, cb);
|
|
|
|
},
|
|
|
|
removeListener: (eventName, cb) => {
|
|
|
|
ipcEmitter.removeListener(eventName, cb);
|
|
|
|
},
|
|
|
|
once: (eventName, cb) => {
|
|
|
|
ipcEmitter.on(eventName, cb);
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2019-03-22 06:15:23 -05:00
|
|
|
export const shell = {
|
2021-01-29 04:23:22 -06:00
|
|
|
openExternal: jest.fn(),
|
2019-03-22 06:15:23 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
// tslint:disable-next-line:variable-name
|
|
|
|
export const Menu = {
|
2021-01-29 04:23:22 -06:00
|
|
|
buildFromTemplate: jest.fn(),
|
|
|
|
setApplicationMenu: jest.fn(),
|
2019-03-22 06:15:23 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
export const crashReporter = {
|
2021-01-29 04:23:22 -06:00
|
|
|
start: jest.fn(),
|
2021-01-29 09:43:56 -06:00
|
|
|
getLastCrashReport: jest.fn(),
|
2019-03-22 06:15:23 -05:00
|
|
|
};
|
|
|
|
|
2019-03-20 00:16:01 -05:00
|
|
|
const getCurrentWindow = jest.fn(() => {
|
2021-01-29 04:23:22 -06:00
|
|
|
return {
|
|
|
|
isFullScreen: jest.fn(() => {
|
|
|
|
return false;
|
|
|
|
}),
|
|
|
|
isMaximized: jest.fn(() => {
|
|
|
|
return false;
|
|
|
|
}),
|
|
|
|
on: jest.fn(),
|
|
|
|
removeListener: jest.fn(),
|
|
|
|
isDestroyed: jest.fn(() => {
|
|
|
|
return false;
|
|
|
|
}),
|
|
|
|
close: jest.fn(),
|
|
|
|
maximize: jest.fn(),
|
|
|
|
minimize: jest.fn(),
|
|
|
|
unmaximize: jest.fn(),
|
|
|
|
setFullScreen: jest.fn(),
|
|
|
|
};
|
2019-03-20 00:16:01 -05:00
|
|
|
});
|
|
|
|
|
2020-03-31 06:58:51 -05:00
|
|
|
const clipboard = {
|
2021-01-29 04:23:22 -06:00
|
|
|
write: jest.fn(),
|
|
|
|
readTest: jest.fn(() => {
|
|
|
|
return '';
|
|
|
|
}),
|
2020-03-31 06:58:51 -05:00
|
|
|
};
|
|
|
|
|
2019-03-22 06:15:23 -05:00
|
|
|
export const dialog = {
|
2021-01-29 04:23:22 -06:00
|
|
|
showMessageBox: jest.fn(),
|
|
|
|
showErrorBox: jest.fn(),
|
2019-03-20 23:21:59 -05:00
|
|
|
};
|
|
|
|
|
2019-03-25 02:47:30 -05:00
|
|
|
// tslint:disable-next-line:variable-name
|
|
|
|
export const BrowserWindow = {
|
2021-01-29 04:23:22 -06:00
|
|
|
getFocusedWindow: jest.fn(() => {
|
|
|
|
return {
|
|
|
|
isDestroyed: jest.fn(() => false),
|
|
|
|
};
|
|
|
|
}),
|
|
|
|
fromWebContents: (arg) => arg,
|
|
|
|
getAllWindows: jest.fn(() => []),
|
2019-03-25 02:47:30 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
export const session = {
|
2021-01-29 04:23:22 -06:00
|
|
|
defaultSession: {
|
|
|
|
clearCache: jest.fn(),
|
|
|
|
},
|
2019-03-25 02:47:30 -05:00
|
|
|
};
|
|
|
|
|
2019-03-13 02:00:59 -05:00
|
|
|
export const remote = {
|
2021-01-29 04:23:22 -06:00
|
|
|
app,
|
|
|
|
getCurrentWindow,
|
|
|
|
clipboard,
|
2019-01-22 09:12:16 -06:00
|
|
|
};
|