Path: blob/main/src/vs/workbench/test/browser/part.test.ts
5237 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import assert from 'assert';6import { Part } from '../../browser/part.js';7import { isEmptyObject } from '../../../base/common/types.js';8import { TestThemeService } from '../../../platform/theme/test/common/testThemeService.js';9import { append, $, hide } from '../../../base/browser/dom.js';10import { TestLayoutService } from './workbenchTestServices.js';11import { StorageScope, StorageTarget } from '../../../platform/storage/common/storage.js';12import { TestStorageService } from '../common/workbenchTestServices.js';13import { ensureNoDisposablesAreLeakedInTestSuite } from '../../../base/test/common/utils.js';14import { DisposableStore } from '../../../base/common/lifecycle.js';15import { mainWindow } from '../../../base/browser/window.js';1617suite('Workbench parts', () => {1819const disposables = new DisposableStore();2021class SimplePart extends Part {2223minimumWidth: number = 50;24maximumWidth: number = 50;25minimumHeight: number = 50;26maximumHeight: number = 50;2728override layout(width: number, height: number): void {29throw new Error('Method not implemented.');30}3132toJSON(): object {33throw new Error('Method not implemented.');34}35}3637class MyPart extends SimplePart {3839constructor(private expectedParent: HTMLElement) {40super('myPart', { hasTitle: true }, new TestThemeService(), disposables.add(new TestStorageService()), new TestLayoutService());41}4243protected override createTitleArea(parent: HTMLElement): HTMLElement {44assert.strictEqual(parent, this.expectedParent);45return super.createTitleArea(parent)!;46}4748protected override createContentArea(parent: HTMLElement): HTMLElement {49assert.strictEqual(parent, this.expectedParent);50return super.createContentArea(parent)!;51}5253testGetMemento(scope: StorageScope, target: StorageTarget) {54return super.getMemento(scope, target);55}5657testSaveState(): void {58return super.saveState();59}60}6162class MyPart2 extends SimplePart {6364constructor() {65super('myPart2', { hasTitle: true }, new TestThemeService(), disposables.add(new TestStorageService()), new TestLayoutService());66}6768protected override createTitleArea(parent: HTMLElement): HTMLElement {69const titleContainer = append(parent, $('div'));70const titleLabel = append(titleContainer, $('span'));71titleLabel.id = 'myPart.title';72titleLabel.innerText = 'Title';7374return titleContainer;75}7677protected override createContentArea(parent: HTMLElement): HTMLElement {78const contentContainer = append(parent, $('div'));79const contentSpan = append(contentContainer, $('span'));80contentSpan.id = 'myPart.content';81contentSpan.innerText = 'Content';8283return contentContainer;84}85}8687class MyPart3 extends SimplePart {8889constructor() {90super('myPart2', { hasTitle: false }, new TestThemeService(), disposables.add(new TestStorageService()), new TestLayoutService());91}9293protected override createTitleArea(parent: HTMLElement): HTMLElement {94return null!;95}9697protected override createContentArea(parent: HTMLElement): HTMLElement {98const contentContainer = append(parent, $('div'));99const contentSpan = append(contentContainer, $('span'));100contentSpan.id = 'myPart.content';101contentSpan.innerText = 'Content';102103return contentContainer;104}105}106107let fixture: HTMLElement;108const fixtureId = 'workbench-part-fixture';109110setup(() => {111fixture = document.createElement('div');112fixture.id = fixtureId;113mainWindow.document.body.appendChild(fixture);114});115116teardown(() => {117fixture.remove();118disposables.clear();119});120121test('Creation', () => {122const b = document.createElement('div');123mainWindow.document.getElementById(fixtureId)!.appendChild(b);124hide(b);125126let part = disposables.add(new MyPart(b));127part.create(b);128129assert.strictEqual(part.getId(), 'myPart');130131// Memento132// eslint-disable-next-line local/code-no-any-casts133let memento = part.testGetMemento(StorageScope.PROFILE, StorageTarget.MACHINE) as any;134assert(memento);135memento.foo = 'bar';136memento.bar = [1, 2, 3];137138part.testSaveState();139140// Re-Create to assert memento contents141part = disposables.add(new MyPart(b));142143memento = part.testGetMemento(StorageScope.PROFILE, StorageTarget.MACHINE);144assert(memento);145assert.strictEqual(memento.foo, 'bar');146assert.strictEqual(memento.bar.length, 3);147148// Empty Memento stores empty object149delete memento.foo;150delete memento.bar;151152part.testSaveState();153part = disposables.add(new MyPart(b));154memento = part.testGetMemento(StorageScope.PROFILE, StorageTarget.MACHINE);155assert(memento);156assert.strictEqual(isEmptyObject(memento), true);157});158159test('Part Layout with Title and Content', function () {160const b = document.createElement('div');161mainWindow.document.getElementById(fixtureId)!.appendChild(b);162hide(b);163164const part = disposables.add(new MyPart2());165part.create(b);166167assert(mainWindow.document.getElementById('myPart.title'));168assert(mainWindow.document.getElementById('myPart.content'));169});170171test('Part Layout with Content only', function () {172const b = document.createElement('div');173mainWindow.document.getElementById(fixtureId)!.appendChild(b);174hide(b);175176const part = disposables.add(new MyPart3());177part.create(b);178179assert(!mainWindow.document.getElementById('myPart.title'));180assert(mainWindow.document.getElementById('myPart.content'));181});182183ensureNoDisposablesAreLeakedInTestSuite();184});185186187