1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00

Switch integration tests to get application menus by IDs (#3912)

This commit is contained in:
Sebastian Malton 2021-10-07 15:30:52 -04:00 committed by GitHub
parent 724b9450a6
commit e10c13cdf4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 73 additions and 165 deletions

View File

@ -25,16 +25,24 @@
TEST_NAMESPACE namespace. This is done to minimize destructive impact of the cluster tests on an existing minikube TEST_NAMESPACE namespace. This is done to minimize destructive impact of the cluster tests on an existing minikube
cluster and vice versa. cluster and vice versa.
*/ */
import type { Page } from "playwright"; import type { ElectronApplication, Page } from "playwright";
import * as utils from "../helpers/utils"; import * as utils from "../helpers/utils";
describe("preferences page tests", () => { describe("preferences page tests", () => {
let window: Page, cleanup: () => Promise<void>; let window: Page, cleanup: () => Promise<void>;
beforeEach(async () => { beforeEach(async () => {
({ window, cleanup } = await utils.start()); let app: ElectronApplication;
({ window, cleanup, app } = await utils.start());
await utils.clickWelcomeButton(window); await utils.clickWelcomeButton(window);
await window.keyboard.press("Meta+,");
await app.evaluate(async ({ app }) => {
await app.applicationMenu
.getMenuItemById(process.platform === "darwin" ? "root" : "file")
.submenu.getMenuItemById("preferences")
.click();
});
}, 10*60*1000); }, 10*60*1000);
afterEach(async () => { afterEach(async () => {

View File

@ -19,14 +19,14 @@
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
import type { Page } from "playwright"; import type { ElectronApplication, Page } from "playwright";
import * as utils from "../helpers/utils"; import * as utils from "../helpers/utils";
describe("Lens command palette", () => { describe("Lens command palette", () => {
let window: Page, cleanup: () => Promise<void>; let window: Page, cleanup: () => Promise<void>, app: ElectronApplication;
beforeEach(async () => { beforeEach(async () => {
({ window, cleanup } = await utils.start()); ({ window, cleanup, app } = await utils.start());
await utils.clickWelcomeButton(window); await utils.clickWelcomeButton(window);
}, 10*60*1000); }, 10*60*1000);
@ -35,8 +35,13 @@ describe("Lens command palette", () => {
}, 10*60*1000); }, 10*60*1000);
describe("menu", () => { describe("menu", () => {
it("opens command dialog from keyboard shortcut", async () => { it("opens command dialog from menu", async () => {
await window.keyboard.press("Meta+Shift+p"); await app.evaluate(async ({ app }) => {
await app.applicationMenu
.getMenuItemById("view")
.submenu.getMenuItemById("command-palette")
.click();
});
await window.waitForSelector(".Select__option >> text=Hotbar: Switch"); await window.waitForSelector(".Select__option >> text=Hotbar: Switch");
}, 10*60*1000); }, 10*60*1000);
}); });

View File

@ -33,8 +33,9 @@ export function getBundledKubectlVersion(): string {
export async function getAppVersionFromProxyServer(proxyPort: number): Promise<string> { export async function getAppVersionFromProxyServer(proxyPort: number): Promise<string> {
const response = await requestPromise({ const response = await requestPromise({
method: "GET", method: "GET",
uri: `http://localhost:${proxyPort}/version`, uri: `http://127.0.0.1:${proxyPort}/version`,
resolveWithFullResponse: true resolveWithFullResponse: true,
proxy: undefined,
}); });
return JSON.parse(response.body).version; return JSON.parse(response.body).version;

View File

@ -19,19 +19,23 @@
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
import { app, BrowserWindow, dialog, IpcMainEvent, Menu, MenuItem, MenuItemConstructorOptions, webContents, shell } from "electron"; import { app, BrowserWindow, dialog, Menu, MenuItem, MenuItemConstructorOptions, webContents, shell } from "electron";
import { autorun } from "mobx"; import { autorun } from "mobx";
import type { WindowManager } from "./window-manager"; import type { WindowManager } from "./window-manager";
import { appName, isMac, isWindows, isTestEnv, docsUrl, supportUrl, productName } from "../common/vars"; import { appName, isMac, isWindows, docsUrl, supportUrl, productName } from "../common/vars";
import { MenuRegistry } from "../extensions/registries/menu-registry"; import { MenuRegistry } from "../extensions/registries/menu-registry";
import logger from "./logger"; import logger from "./logger";
import { exitApp } from "./exit-app"; import { exitApp } from "./exit-app";
import { broadcastMessage, ipcMainOn } from "../common/ipc"; import { broadcastMessage } from "../common/ipc";
import * as packageJson from "../../package.json"; import * as packageJson from "../../package.json";
import { preferencesURL, extensionsURL, addClusterURL, catalogURL, welcomeURL } from "../common/routes"; import { preferencesURL, extensionsURL, addClusterURL, catalogURL, welcomeURL } from "../common/routes";
export type MenuTopId = "mac" | "file" | "edit" | "view" | "help"; export type MenuTopId = "mac" | "file" | "edit" | "view" | "help";
interface MenuItemsOpts extends MenuItemConstructorOptions {
submenu?: MenuItemConstructorOptions[];
}
export function initMenu(windowManager: WindowManager) { export function initMenu(windowManager: WindowManager) {
return autorun(() => buildMenu(windowManager), { return autorun(() => buildMenu(windowManager), {
delay: 100 delay: 100
@ -68,11 +72,13 @@ export function buildMenu(windowManager: WindowManager) {
await windowManager.navigate(url); await windowManager.navigate(url);
} }
const macAppMenu: MenuItemConstructorOptions = { const macAppMenu: MenuItemsOpts = {
label: app.getName(), label: app.getName(),
id: "root",
submenu: [ submenu: [
{ {
label: `About ${productName}`, label: `About ${productName}`,
id: "about",
click(menuItem: MenuItem, browserWindow: BrowserWindow) { click(menuItem: MenuItem, browserWindow: BrowserWindow) {
showAbout(browserWindow); showAbout(browserWindow);
} }
@ -81,6 +87,7 @@ export function buildMenu(windowManager: WindowManager) {
{ {
label: "Preferences", label: "Preferences",
accelerator: "CmdOrCtrl+,", accelerator: "CmdOrCtrl+,",
id: "preferences",
click() { click() {
navigate(preferencesURL()); navigate(preferencesURL());
}, },
@ -88,6 +95,7 @@ export function buildMenu(windowManager: WindowManager) {
{ {
label: "Extensions", label: "Extensions",
accelerator: "CmdOrCtrl+Shift+E", accelerator: "CmdOrCtrl+Shift+E",
id: "extensions",
click() { click() {
navigate(extensionsURL()); navigate(extensionsURL());
} }
@ -102,18 +110,21 @@ export function buildMenu(windowManager: WindowManager) {
{ {
label: "Quit", label: "Quit",
accelerator: "Cmd+Q", accelerator: "Cmd+Q",
id: "quit",
click() { click() {
exitApp(); exitApp();
} }
} }
], ],
}; };
const fileMenu: MenuItemConstructorOptions = { const fileMenu: MenuItemsOpts = {
label: "File", label: "File",
id: "file",
submenu: [ submenu: [
{ {
label: "Add Cluster", label: "Add Cluster",
accelerator: "CmdOrCtrl+Shift+A", accelerator: "CmdOrCtrl+Shift+A",
id: "add-cluster",
click() { click() {
navigate(addClusterURL()); navigate(addClusterURL());
} }
@ -122,6 +133,7 @@ export function buildMenu(windowManager: WindowManager) {
{ type: "separator" }, { type: "separator" },
{ {
label: "Preferences", label: "Preferences",
id: "preferences",
accelerator: "Ctrl+,", accelerator: "Ctrl+,",
click() { click() {
navigate(preferencesURL()); navigate(preferencesURL());
@ -145,6 +157,7 @@ export function buildMenu(windowManager: WindowManager) {
{ {
label: "Exit", label: "Exit",
accelerator: "Alt+F4", accelerator: "Alt+F4",
id: "quit",
click() { click() {
exitApp(); exitApp();
} }
@ -152,8 +165,9 @@ export function buildMenu(windowManager: WindowManager) {
]) ])
], ],
}; };
const editMenu: MenuItemConstructorOptions = { const editMenu: MenuItemsOpts = {
label: "Edit", label: "Edit",
id: "edit",
submenu: [ submenu: [
{ role: "undo" }, { role: "undo" },
{ role: "redo" }, { role: "redo" },
@ -166,12 +180,14 @@ export function buildMenu(windowManager: WindowManager) {
{ role: "selectAll" }, { role: "selectAll" },
] ]
}; };
const viewMenu: MenuItemConstructorOptions = { const viewMenu: MenuItemsOpts = {
label: "View", label: "View",
id: "view",
submenu: [ submenu: [
{ {
label: "Catalog", label: "Catalog",
accelerator: "Shift+CmdOrCtrl+C", accelerator: "Shift+CmdOrCtrl+C",
id: "catalog",
click() { click() {
navigate(catalogURL()); navigate(catalogURL());
} }
@ -179,6 +195,7 @@ export function buildMenu(windowManager: WindowManager) {
{ {
label: "Command Palette...", label: "Command Palette...",
accelerator: "Shift+CmdOrCtrl+P", accelerator: "Shift+CmdOrCtrl+P",
id: "command-palette",
click() { click() {
broadcastMessage("command-palette:open"); broadcastMessage("command-palette:open");
} }
@ -187,6 +204,7 @@ export function buildMenu(windowManager: WindowManager) {
{ {
label: "Back", label: "Back",
accelerator: "CmdOrCtrl+[", accelerator: "CmdOrCtrl+[",
id: "go-back",
click() { click() {
webContents.getAllWebContents().filter(wc => wc.getType() === "window").forEach(wc => wc.goBack()); webContents.getAllWebContents().filter(wc => wc.getType() === "window").forEach(wc => wc.goBack());
} }
@ -194,6 +212,7 @@ export function buildMenu(windowManager: WindowManager) {
{ {
label: "Forward", label: "Forward",
accelerator: "CmdOrCtrl+]", accelerator: "CmdOrCtrl+]",
id: "go-forward",
click() { click() {
webContents.getAllWebContents().filter(wc => wc.getType() === "window").forEach(wc => wc.goForward()); webContents.getAllWebContents().filter(wc => wc.getType() === "window").forEach(wc => wc.goForward());
} }
@ -201,6 +220,7 @@ export function buildMenu(windowManager: WindowManager) {
{ {
label: "Reload", label: "Reload",
accelerator: "CmdOrCtrl+R", accelerator: "CmdOrCtrl+R",
id: "reload",
click() { click() {
windowManager.reload(); windowManager.reload();
} }
@ -214,23 +234,27 @@ export function buildMenu(windowManager: WindowManager) {
{ role: "togglefullscreen" } { role: "togglefullscreen" }
] ]
}; };
const helpMenu: MenuItemConstructorOptions = { const helpMenu: MenuItemsOpts = {
role: "help", role: "help",
id: "help",
submenu: [ submenu: [
{ {
label: "Welcome", label: "Welcome",
id: "welcome",
click() { click() {
navigate(welcomeURL()); navigate(welcomeURL());
}, },
}, },
{ {
label: "Documentation", label: "Documentation",
id: "documentation",
click: async () => { click: async () => {
shell.openExternal(docsUrl); shell.openExternal(docsUrl);
}, },
}, },
{ {
label: "Support", label: "Support",
id: "support",
click: async () => { click: async () => {
shell.openExternal(supportUrl); shell.openExternal(supportUrl);
}, },
@ -238,6 +262,7 @@ export function buildMenu(windowManager: WindowManager) {
...ignoreOnMac([ ...ignoreOnMac([
{ {
label: `About ${productName}`, label: `About ${productName}`,
id: "about",
click(menuItem: MenuItem, browserWindow: BrowserWindow) { click(menuItem: MenuItem, browserWindow: BrowserWindow) {
showAbout(browserWindow); showAbout(browserWindow);
} }
@ -246,69 +271,28 @@ export function buildMenu(windowManager: WindowManager) {
] ]
}; };
// Prepare menu items order // Prepare menu items order
const appMenu: Record<MenuTopId, MenuItemConstructorOptions> = { const appMenu = new Map([
mac: macAppMenu, ["mac", macAppMenu],
file: fileMenu, ["file", fileMenu],
edit: editMenu, ["edit", editMenu],
view: viewMenu, ["view", viewMenu],
help: helpMenu, ["help", helpMenu],
}; ]);
// Modify menu from extensions-api // Modify menu from extensions-api
MenuRegistry.getInstance().getItems().forEach(({ parentId, ...menuItem }) => { for (const { parentId, ...menuItem } of MenuRegistry.getInstance().getItems()) {
try { if (!appMenu.has(parentId)) {
const topMenu = appMenu[parentId as MenuTopId].submenu as MenuItemConstructorOptions[]; logger.error(`[MENU]: cannot register menu item for parentId=${parentId}, parent item doesn't exist`, { menuItem });
topMenu.push(menuItem); continue;
} catch (err) { }
logger.error(`[MENU]: can't register menu item, parentId=${parentId}`, { menuItem });
appMenu.get(parentId).submenu.push(menuItem);
} }
});
if (!isMac) { if (!isMac) {
delete appMenu.mac; appMenu.delete("mac");
} }
const menu = Menu.buildFromTemplate(Object.values(appMenu)); Menu.setApplicationMenu(Menu.buildFromTemplate([...appMenu.values()]));
Menu.setApplicationMenu(menu);
if (isTestEnv) {
// this is a workaround for the test environment (spectron) not being able to directly access
// the application menus (https://github.com/electron-userland/spectron/issues/21)
ipcMainOn("test-menu-item-click", (event: IpcMainEvent, ...names: string[]) => {
let menu: Menu = Menu.getApplicationMenu();
const parentLabels: string[] = [];
let menuItem: MenuItem;
for (const name of names) {
parentLabels.push(name);
menuItem = menu?.items?.find(item => item.label === name);
if (!menuItem) {
break;
}
menu = menuItem.submenu;
}
const menuPath: string = parentLabels.join(" -> ");
if (!menuItem) {
logger.info(`[MENU:test-menu-item-click] Cannot find menu item ${menuPath}`);
return;
}
const { enabled, visible, click } = menuItem;
if (enabled === false || visible === false || typeof click !== "function") {
logger.info(`[MENU:test-menu-item-click] Menu item ${menuPath} not clickable`);
return;
}
logger.info(`[MENU:test-menu-item-click] Menu item ${menuPath} click!`);
menuItem.click();
});
}
} }

View File

@ -1,88 +0,0 @@
/**
* Copyright (c) 2021 OpenLens Authors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
import { ipcRenderer } from "electron";
import { navigate } from "../navigation";
/**
* The definition of a keyboard shortcut
*/
interface Shortcut {
code?: string;
key?: string;
metaKey?: boolean;
altKey?: boolean;
shiftKey?: boolean;
ctrlKey?: boolean;
action: () => void;
}
const shortcuts: Shortcut[] = [
{
key: "p",
metaKey: true,
shiftKey: true,
action: () => ipcRenderer.emit("command-palette:open"),
},
{
code: "Comma",
metaKey: true,
action: () => navigate("/preferences"),
},
];
function shortcutMatches(shortcut: Shortcut, event: KeyboardEvent): boolean {
if (typeof shortcut.metaKey === "boolean" && shortcut.metaKey !== event.metaKey) {
return false;
}
if (typeof shortcut.altKey === "boolean" && shortcut.altKey !== event.altKey) {
return false;
}
if (typeof shortcut.shiftKey === "boolean" && shortcut.shiftKey !== event.shiftKey) {
return false;
}
if (typeof shortcut.ctrlKey === "boolean" && shortcut.ctrlKey !== event.ctrlKey) {
return false;
}
if (typeof shortcut.code === "string" && shortcut.code !== event.code) {
return false;
}
if (typeof shortcut.key === "string" && shortcut.key !== event.key) {
return false;
}
return true;
}
export function registerKeyboardShortcuts() {
window.addEventListener("keydown", event => {
for (const shortcut of shortcuts) {
if (shortcutMatches(shortcut, event)) {
shortcut.action();
}
}
});
}

View File

@ -36,7 +36,6 @@ import { registerIpcListeners } from "./ipc";
import { ipcRenderer } from "electron"; import { ipcRenderer } from "electron";
import { IpcRendererNavigationEvents } from "./navigation/events"; import { IpcRendererNavigationEvents } from "./navigation/events";
import { catalogEntityRegistry } from "./api/catalog-entity-registry"; import { catalogEntityRegistry } from "./api/catalog-entity-registry";
import { registerKeyboardShortcuts } from "./keyboard-shortcuts";
import logger from "../common/logger"; import logger from "../common/logger";
import { unmountComponentAtNode } from "react-dom"; import { unmountComponentAtNode } from "react-dom";
@ -51,7 +50,6 @@ export class LensApp extends React.Component {
window.addEventListener("offline", () => broadcastMessage("network:offline")); window.addEventListener("offline", () => broadcastMessage("network:offline"));
window.addEventListener("online", () => broadcastMessage("network:online")); window.addEventListener("online", () => broadcastMessage("network:online"));
registerKeyboardShortcuts();
registerIpcListeners(); registerIpcListeners();
window.onbeforeunload = () => { window.onbeforeunload = () => {