Skip to content

Commit

Permalink
MM-49079 - Calls: process link clicks from Calls popout (#2558) (#2560)
Browse files Browse the repository at this point in the history
* process link clicks from Calls popout

* add a mock for windows tests

(cherry picked from commit ec8e0b9)

Co-authored-by: Christopher Poile <[email protected]>
  • Loading branch information
mattermost-build and cpoile authored Feb 17, 2023
1 parent 9c74e18 commit de62108
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 8 deletions.
1 change: 1 addition & 0 deletions src/common/communication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ export const CALLS_LEAVE_CALL = 'calls-leave-call';
export const CALLS_WIDGET_RESIZE = 'calls-widget-resize';
export const CALLS_WIDGET_SHARE_SCREEN = 'calls-widget-share-screen';
export const CALLS_WIDGET_CHANNEL_LINK_CLICK = 'calls-widget-channel-link-click';
export const CALLS_LINK_CLICK = 'calls-link-click';
export const CALLS_JOINED_CALL = 'calls-joined-call';
export const CALLS_POPOUT_FOCUS = 'calls-popout-focus';
export const CALLS_ERROR = 'calls-error';
Expand Down
2 changes: 2 additions & 0 deletions src/main/preload/callsWidget.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
DESKTOP_SOURCES_RESULT,
DESKTOP_SOURCES_MODAL_REQUEST,
DISPATCH_GET_DESKTOP_SOURCES,
CALLS_LINK_CLICK,
} from 'common/communication';

window.addEventListener('message', ({origin, data = {}} = {}) => {
Expand Down Expand Up @@ -47,6 +48,7 @@ window.addEventListener('message', ({origin, data = {}} = {}) => {
}
case DESKTOP_SOURCES_MODAL_REQUEST:
case CALLS_WIDGET_CHANNEL_LINK_CLICK:
case CALLS_LINK_CLICK:
case CALLS_WIDGET_RESIZE:
case CALLS_JOINED_CALL:
case CALLS_POPOUT_FOCUS:
Expand Down
26 changes: 22 additions & 4 deletions src/main/windows/callsWidgetWindow.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,26 @@ import {
MINIMUM_CALLS_WIDGET_HEIGHT,
CALLS_PLUGIN_ID,
} from 'common/utils/constants';
import WebContentsEventManager from '../views/webContentEvents';

import CallsWidgetWindow from './callsWidgetWindow';

jest.mock('electron', () => ({
app: {
getAppPath: () => '/path/to/app',
},
BrowserWindow: jest.fn(),
ipcMain: {
on: jest.fn(),
off: jest.fn(),
handle: jest.fn(),
},
}));

jest.mock('../views/webContentEvents', () => ({
generateNewWindowListener: jest.fn(),
}));

describe('main/windows/callsWidgetWindow', () => {
describe('create CallsWidgetWindow', () => {
const widgetConfig = {
Expand Down Expand Up @@ -330,9 +339,7 @@ describe('main/windows/callsWidgetWindow', () => {
send: jest.fn(),
};

let isMinimized = false;
baseWindow.restore = jest.fn();
baseWindow.isMinimized = jest.fn(() => isMinimized);

const widgetWindow = new CallsWidgetWindow(mainWindow, mainView, widgetConfig);

Expand All @@ -341,19 +348,30 @@ describe('main/windows/callsWidgetWindow', () => {

expect(widgetWindow.popOut).toBeNull();

const popOut = new BrowserWindow();
const popOut = new EventEmitter();
popOut.webContents = {
setWindowOpenHandler: jest.fn(),
on: jest.fn(),
id: 'webContentsId',
};
popOut.focus = jest.fn();
popOut.restore = jest.fn();
popOut.isMinimized = jest.fn().mockReturnValue(false);

widgetWindow.onPopOutFocus();
expect(popOut.focus).not.toHaveBeenCalled();
expect(popOut.restore).not.toHaveBeenCalled();

widgetWindow.onPopOutCreate(popOut);
expect(widgetWindow.popOut).toBe(popOut);
expect(popOut.webContents.setWindowOpenHandler).toHaveBeenCalled();
expect(WebContentsEventManager.generateNewWindowListener).toHaveBeenCalledWith('webContentsId', true);

widgetWindow.onPopOutFocus();
expect(popOut.focus).toHaveBeenCalled();
expect(popOut.restore).not.toHaveBeenCalled();

isMinimized = true;
popOut.isMinimized = jest.fn().mockReturnValue(true);
widgetWindow.onPopOutFocus();
expect(popOut.focus).toHaveBeenCalled();
expect(popOut.restore).toHaveBeenCalled();
Expand Down
8 changes: 7 additions & 1 deletion src/main/windows/callsWidgetWindow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// See LICENSE.txt for license information.

import url from 'url';

import {EventEmitter} from 'events';
import {BrowserWindow, Rectangle, ipcMain, IpcMainEvent} from 'electron';
import log from 'electron-log';
Expand Down Expand Up @@ -31,6 +30,8 @@ import {
CALLS_WIDGET_RESIZE,
CALLS_WIDGET_SHARE_SCREEN,
} from 'common/communication';
import webContentsEventManager from 'main/views/webContentEvents';
import Config from 'common/config';

type LoadURLOpts = {
extraHeaders: string;
Expand Down Expand Up @@ -200,6 +201,11 @@ export default class CallsWidgetWindow extends EventEmitter {

private onPopOutCreate = (win: BrowserWindow) => {
this.popOut = win;

// Let the webContentsEventManager handle links that try to open a new window
const spellcheck = Config.useSpellChecker;
const newWindow = webContentsEventManager.generateNewWindowListener(this.popOut.webContents.id, spellcheck);
this.popOut.webContents.setWindowOpenHandler(newWindow);
}

private onPopOutFocus = () => {
Expand Down
24 changes: 24 additions & 0 deletions src/main/windows/windowManager.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ jest.mock('../downloadsManager', () => ({
}));

jest.mock('./callsWidgetWindow');
jest.mock('main/views/webContentEvents', () => ({}));

describe('main/windows/windowManager', () => {
describe('handleUpdateConfig', () => {
Expand Down Expand Up @@ -1266,6 +1267,29 @@ describe('main/windows/windowManager', () => {
});
});

describe('handleCallsLinkClick', () => {
const windowManager = new WindowManager();
const view1 = {
view: {
webContents: {
send: jest.fn(),
},
},
};
windowManager.viewManager = {
views: new Map([
['server-1_tab-messaging', view1],
]),
getCurrentView: jest.fn(),
};

it('should pass through the click link to browser history push', () => {
windowManager.viewManager.getCurrentView.mockReturnValue(view1);
windowManager.handleCallsLinkClick(null, {link: '/other/subpath'});
expect(view1.view.webContents.send).toBeCalledWith('browser-history-push', '/other/subpath');
});
});

describe('getServerURLFromWebContentsId', () => {
const view = {
name: 'server-1_tab-messaging',
Expand Down
13 changes: 10 additions & 3 deletions src/main/windows/windowManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ import path from 'path';
import {app, BrowserWindow, nativeImage, systemPreferences, ipcMain, IpcMainEvent, IpcMainInvokeEvent, desktopCapturer} from 'electron';
import log from 'electron-log';

import {
CallsJoinCallMessage,
} from 'types/calls';
import {CallsJoinCallMessage, CallsLinkClickMessage} from 'types/calls';

import {
MAXIMIZE_CHANGE,
Expand All @@ -35,6 +33,7 @@ import {
DESKTOP_SOURCES_MODAL_REQUEST,
CALLS_WIDGET_CHANNEL_LINK_CLICK,
CALLS_ERROR,
CALLS_LINK_CLICK,
} from 'common/communication';
import urlUtils from 'common/utils/url';
import {SECOND} from 'common/utils/constants';
Expand Down Expand Up @@ -95,6 +94,7 @@ export class WindowManager {
ipcMain.on(CALLS_LEAVE_CALL, () => this.callsWidgetWindow?.close());
ipcMain.on(DESKTOP_SOURCES_MODAL_REQUEST, this.handleDesktopSourcesModalRequest);
ipcMain.on(CALLS_WIDGET_CHANNEL_LINK_CLICK, this.handleCallsWidgetChannelLinkClick);
ipcMain.on(CALLS_LINK_CLICK, this.handleCallsLinkClick);
}

handleUpdateConfig = () => {
Expand Down Expand Up @@ -150,6 +150,13 @@ export class WindowManager {
}
}

handleCallsLinkClick = (_: IpcMainEvent, msg: CallsLinkClickMessage) => {
log.debug('WindowManager.handleCallsLinkClick with linkURL', msg.link);
this.mainWindow?.focus();
const currentView = this.viewManager?.getCurrentView();
currentView?.view.webContents.send(BROWSER_HISTORY_PUSH, msg.link);
}

showSettingsWindow = () => {
log.debug('WindowManager.showSettingsWindow');

Expand Down
4 changes: 4 additions & 0 deletions src/types/calls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,7 @@ export type CallsWidgetShareScreenMessage = {
export type CallsJoinedCallMessage = {
callID: string;
}

export type CallsLinkClickMessage = {
link: string | URL;
}

0 comments on commit de62108

Please sign in to comment.