From 44f81a348c4a973383d2947d7aca70806494e800 Mon Sep 17 00:00:00 2001 From: Martin Fleck Date: Fri, 5 Jul 2024 16:16:24 +0200 Subject: [PATCH] Fix unit test case by adapting to new Theia behavior --- .../tests/crossmodel-explorer-view.spec.ts | 33 +++++++++++---- .../browser/form-client-frontend-module.ts | 8 ++-- .../src/browser/form-editor-open-handler.ts | 25 +++++++++--- .../mapping-diagram-manager.ts | 16 ++++++-- .../system-diagram-frontend-module.ts | 9 ++++- .../system-diagram/system-diagram-manager.ts | 40 +++++++++++++++++++ 6 files changed, 111 insertions(+), 20 deletions(-) create mode 100644 packages/glsp-client/src/browser/system-diagram/system-diagram-manager.ts diff --git a/e2e-tests/src/tests/crossmodel-explorer-view.spec.ts b/e2e-tests/src/tests/crossmodel-explorer-view.spec.ts index 9b932e39..7fc0f3e4 100644 --- a/e2e-tests/src/tests/crossmodel-explorer-view.spec.ts +++ b/e2e-tests/src/tests/crossmodel-explorer-view.spec.ts @@ -1,12 +1,28 @@ /******************************************************************************** * Copyright (c) 2023 CrossBreeze. ********************************************************************************/ -import { expect } from '@playwright/test'; +import { expect, Page } from '@playwright/test'; import test, { app } from '../fixtures/crossmodel-fixture'; import { CrossModelExplorerView } from '../page-objects/crossmodel-explorer-view'; let explorer: CrossModelExplorerView; +async function checkOpenWithItem(page: Page, text: string): Promise { + // Locate all elements matching the selector + const elements = await page.$$('.quick-input-list .monaco-highlighted-label'); + + // Iterate over elements to check for visibility and text content + for (const element of elements) { + if (await element.isVisible()) { + const textContent = await element.textContent(); + if (textContent?.includes(text)) { + return true; + } + } + } + return false; +} + test.describe('CrossModel Explorer View', () => { test.beforeAll(async ({ browser }) => { explorer = await app.openView(CrossModelExplorerView); @@ -20,8 +36,9 @@ test.describe('CrossModel Explorer View', () => { const menu = await file.openContextMenu(); expect(await menu.isOpen()).toBe(true); // Expect the Code and Form editor to be in the Open With menu option. - expect(await menu.menuItemByNamePath('Open With', 'Code Editor')).toBeDefined(); - expect(await menu.menuItemByNamePath('Open With', 'Form Editor')).toBeDefined(); + await menu.clickMenuItem('Open With...'); + expect(await checkOpenWithItem(explorer.page, 'Text Editor')).toBeTruthy(); + expect(await checkOpenWithItem(explorer.page, 'Form Editor')).toBeTruthy(); await menu.close(); }); @@ -32,8 +49,9 @@ test.describe('CrossModel Explorer View', () => { const menu = await file.openContextMenu(); expect(await menu.isOpen()).toBe(true); // Expect the Code and Form editor to be in the Open With menu option. - expect(await menu.menuItemByNamePath('Open With', 'Code Editor')).toBeDefined(); - expect(await menu.menuItemByNamePath('Open With', 'Form Editor')).toBeDefined(); + await menu.clickMenuItem('Open With...'); + expect(await checkOpenWithItem(explorer.page, 'Text Editor')).toBeTruthy(); + expect(await checkOpenWithItem(explorer.page, 'Form Editor')).toBeTruthy(); await menu.close(); }); @@ -44,8 +62,9 @@ test.describe('CrossModel Explorer View', () => { const menu = await file.openContextMenu(); expect(await menu.isOpen()).toBe(true); // Expect the Code and Form editor to be in the Open With menu option. - expect(await menu.menuItemByNamePath('Open With', 'Code Editor')).toBeDefined(); - expect(await menu.menuItemByNamePath('Open With', 'System Diagram')).toBeDefined(); + await menu.clickMenuItem('Open With...'); + expect(await checkOpenWithItem(explorer.page, 'Text Editor')).toBeTruthy(); + expect(await checkOpenWithItem(explorer.page, 'System Diagram')).toBeTruthy(); await menu.close(); }); }); diff --git a/packages/form-client/src/browser/form-client-frontend-module.ts b/packages/form-client/src/browser/form-client-frontend-module.ts index cc1de9e0..21dc5b2f 100644 --- a/packages/form-client/src/browser/form-client-frontend-module.ts +++ b/packages/form-client/src/browser/form-client-frontend-module.ts @@ -2,14 +2,16 @@ * Copyright (c) 2023 CrossBreeze. ********************************************************************************/ -import { NavigatableWidgetOptions, OpenHandler, WidgetFactory } from '@theia/core/lib/browser'; +import { CrossModelWidgetOptions } from '@crossbreeze/core/lib/browser'; +import { FrontendApplicationContribution, NavigatableWidgetOptions, OpenHandler, WidgetFactory } from '@theia/core/lib/browser'; import { ContainerModule } from '@theia/core/shared/inversify'; import { FormEditorOpenHandler, createFormEditorId } from './form-editor-open-handler'; import { FormEditorWidget, FormEditorWidgetOptions } from './form-editor-widget'; -import { CrossModelWidgetOptions } from '@crossbreeze/core/lib/browser'; export default new ContainerModule(bind => { - bind(OpenHandler).to(FormEditorOpenHandler).inSingletonScope(); + bind(FormEditorOpenHandler).toSelf().inSingletonScope(); + bind(OpenHandler).toService(FormEditorOpenHandler); + bind(FrontendApplicationContribution).toService(FormEditorOpenHandler); bind(WidgetFactory).toDynamicValue(context => ({ id: FormEditorOpenHandler.ID, // must match the id in the open handler createWidget: (navigatableOptions: NavigatableWidgetOptions) => { diff --git a/packages/form-client/src/browser/form-editor-open-handler.ts b/packages/form-client/src/browser/form-editor-open-handler.ts index cecc4e1e..4afb516a 100644 --- a/packages/form-client/src/browser/form-editor-open-handler.ts +++ b/packages/form-client/src/browser/form-editor-open-handler.ts @@ -2,20 +2,35 @@ * Copyright (c) 2023 CrossBreeze. ********************************************************************************/ -import { MaybePromise, nls } from '@theia/core'; -import { NavigatableWidgetOpenHandler } from '@theia/core/lib/browser'; +import { nls } from '@theia/core'; +import { FrontendApplicationContribution, NavigatableWidgetOpenHandler, OpenWithHandler, OpenWithService } from '@theia/core/lib/browser'; import URI from '@theia/core/lib/common/uri'; -import { injectable } from '@theia/core/shared/inversify'; +import { inject, injectable, postConstruct } from '@theia/core/shared/inversify'; import { FormEditorWidget } from './form-editor-widget'; @injectable() -export class FormEditorOpenHandler extends NavigatableWidgetOpenHandler { +export class FormEditorOpenHandler + extends NavigatableWidgetOpenHandler + implements OpenWithHandler, FrontendApplicationContribution +{ static ID = 'form-editor-opener'; readonly id = FormEditorOpenHandler.ID; // must match the id of the widget factory readonly label = nls.localize('form-client/form-editor', 'Form Editor'); - canHandle(uri: URI): MaybePromise { + @inject(OpenWithService) protected readonly openWithService: OpenWithService; + + initialize(): void { + // ensure this class is instantiated early + } + + @postConstruct() + protected override init(): void { + this.openWithService.registerHandler(this); + super.init(); + } + + canHandle(uri: URI): number { return uri.path.ext === '.cm' ? 1 : -1; } } diff --git a/packages/glsp-client/src/browser/mapping-diagram/mapping-diagram-manager.ts b/packages/glsp-client/src/browser/mapping-diagram/mapping-diagram-manager.ts index bfd53552..9b9f39fe 100644 --- a/packages/glsp-client/src/browser/mapping-diagram/mapping-diagram-manager.ts +++ b/packages/glsp-client/src/browser/mapping-diagram/mapping-diagram-manager.ts @@ -2,19 +2,27 @@ * Copyright (c) 2024 CrossBreeze. ********************************************************************************/ +import { codiconCSSString } from '@eclipse-glsp/client'; import { GLSPDiagramManager } from '@eclipse-glsp/theia-integration'; import { URI } from '@theia/core'; -import { WidgetOpenerOptions } from '@theia/core/lib/browser'; -import { injectable } from '@theia/core/shared/inversify'; +import { OpenWithHandler, OpenWithService, WidgetOpenerOptions } from '@theia/core/lib/browser'; +import { inject, injectable, postConstruct } from '@theia/core/shared/inversify'; import { MappingDiagramLanguage } from '../../common/crossmodel-diagram-language'; -import { codiconCSSString } from '@eclipse-glsp/client'; export interface ProblemMarkerOpenerOptions extends WidgetOpenerOptions { selection?: Range; } @injectable() -export class MappingDiagramManager extends GLSPDiagramManager { +export class MappingDiagramManager extends GLSPDiagramManager implements OpenWithHandler { + @inject(OpenWithService) protected readonly openWithService: OpenWithService; + + @postConstruct() + protected override init(): void { + this.openWithService.registerHandler(this); + super.init(); + } + get label(): string { return MappingDiagramLanguage.label; } diff --git a/packages/glsp-client/src/browser/system-diagram/system-diagram-frontend-module.ts b/packages/glsp-client/src/browser/system-diagram/system-diagram-frontend-module.ts index 0888acae..2f63fd0a 100644 --- a/packages/glsp-client/src/browser/system-diagram/system-diagram-frontend-module.ts +++ b/packages/glsp-client/src/browser/system-diagram/system-diagram-frontend-module.ts @@ -7,12 +7,14 @@ import { DiagramConfiguration, GLSPClientContribution, GLSPDiagramWidget, - GLSPTheiaFrontendModule + GLSPTheiaFrontendModule, + registerDiagramManager } from '@eclipse-glsp/theia-integration'; import { SystemDiagramLanguage } from '../../common/crossmodel-diagram-language'; import { CrossModelClientContribution } from '../crossmodel-client-contribution'; import { SystemDiagramConfiguration } from './system-diagram-configuration'; +import { SystemDiagramManager } from './system-diagram-manager'; import { SystemDiagramWidget } from './system-diagram-widget'; export class SystemDiagramModule extends GLSPTheiaFrontendModule { @@ -32,6 +34,11 @@ export class SystemDiagramModule extends GLSPTheiaFrontendModule { super.bindDiagramWidgetFactory(context); context.rebind(GLSPDiagramWidget).to(SystemDiagramWidget); } + + override configureDiagramManager(context: ContainerContext): void { + context.bind(SystemDiagramManager).toSelf().inSingletonScope(); + registerDiagramManager(context.bind, SystemDiagramManager, false); + } } export default new SystemDiagramModule(); diff --git a/packages/glsp-client/src/browser/system-diagram/system-diagram-manager.ts b/packages/glsp-client/src/browser/system-diagram/system-diagram-manager.ts new file mode 100644 index 00000000..782d8f64 --- /dev/null +++ b/packages/glsp-client/src/browser/system-diagram/system-diagram-manager.ts @@ -0,0 +1,40 @@ +/******************************************************************************** + * Copyright (c) 2024 CrossBreeze. + ********************************************************************************/ + +import { codiconCSSString } from '@eclipse-glsp/client'; +import { GLSPDiagramManager } from '@eclipse-glsp/theia-integration'; +import { OpenWithHandler, OpenWithService } from '@theia/core/lib/browser'; +import { inject, injectable, postConstruct } from '@theia/core/shared/inversify'; +import { SystemDiagramLanguage } from '../../common/crossmodel-diagram-language'; + +@injectable() +export class SystemDiagramManager extends GLSPDiagramManager implements OpenWithHandler { + @inject(OpenWithService) protected readonly openWithService: OpenWithService; + + @postConstruct() + protected override init(): void { + this.openWithService.registerHandler(this); + super.init(); + } + + get label(): string { + return SystemDiagramLanguage.label; + } + + override get iconClass(): string { + return SystemDiagramLanguage.iconClass ?? codiconCSSString('type-hierarchy-sub'); + } + + override get fileExtensions(): string[] { + return SystemDiagramLanguage.fileExtensions; + } + + override get diagramType(): string { + return SystemDiagramLanguage.diagramType; + } + + override get contributionId(): string { + return SystemDiagramLanguage.contributionId; + } +}