Path: blob/main/src/vs/workbench/test/browser/part.test.ts
4778 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';1617/* eslint-disable no-restricted-syntax */1819suite('Workbench parts', () => {2021const disposables = new DisposableStore();2223class SimplePart extends Part {2425minimumWidth: number = 50;26maximumWidth: number = 50;27minimumHeight: number = 50;28maximumHeight: number = 50;2930override layout(width: number, height: number): void {31throw new Error('Method not implemented.');32}3334toJSON(): object {35throw new Error('Method not implemented.');36}37}3839class MyPart extends SimplePart {4041constructor(private expectedParent: HTMLElement) {42super('myPart', { hasTitle: true }, new TestThemeService(), disposables.add(new TestStorageService()), new TestLayoutService());43}4445protected override createTitleArea(parent: HTMLElement): HTMLElement {46assert.strictEqual(parent, this.expectedParent);47return super.createTitleArea(parent)!;48}4950protected override createContentArea(parent: HTMLElement): HTMLElement {51assert.strictEqual(parent, this.expectedParent);52return super.createContentArea(parent)!;53}5455testGetMemento(scope: StorageScope, target: StorageTarget) {56return super.getMemento(scope, target);57}5859testSaveState(): void {60return super.saveState();61}62}6364class MyPart2 extends SimplePart {6566constructor() {67super('myPart2', { hasTitle: true }, new TestThemeService(), disposables.add(new TestStorageService()), new TestLayoutService());68}6970protected override createTitleArea(parent: HTMLElement): HTMLElement {71const titleContainer = append(parent, $('div'));72const titleLabel = append(titleContainer, $('span'));73titleLabel.id = 'myPart.title';74titleLabel.innerText = 'Title';7576return titleContainer;77}7879protected override createContentArea(parent: HTMLElement): HTMLElement {80const contentContainer = append(parent, $('div'));81const contentSpan = append(contentContainer, $('span'));82contentSpan.id = 'myPart.content';83contentSpan.innerText = 'Content';8485return contentContainer;86}87}8889class MyPart3 extends SimplePart {9091constructor() {92super('myPart2', { hasTitle: false }, new TestThemeService(), disposables.add(new TestStorageService()), new TestLayoutService());93}9495protected override createTitleArea(parent: HTMLElement): HTMLElement {96return null!;97}9899protected override createContentArea(parent: HTMLElement): HTMLElement {100const contentContainer = append(parent, $('div'));101const contentSpan = append(contentContainer, $('span'));102contentSpan.id = 'myPart.content';103contentSpan.innerText = 'Content';104105return contentContainer;106}107}108109let fixture: HTMLElement;110const fixtureId = 'workbench-part-fixture';111112setup(() => {113fixture = document.createElement('div');114fixture.id = fixtureId;115mainWindow.document.body.appendChild(fixture);116});117118teardown(() => {119fixture.remove();120disposables.clear();121});122123test('Creation', () => {124const b = document.createElement('div');125mainWindow.document.getElementById(fixtureId)!.appendChild(b);126hide(b);127128let part = disposables.add(new MyPart(b));129part.create(b);130131assert.strictEqual(part.getId(), 'myPart');132133// Memento134// eslint-disable-next-line local/code-no-any-casts135let memento = part.testGetMemento(StorageScope.PROFILE, StorageTarget.MACHINE) as any;136assert(memento);137memento.foo = 'bar';138memento.bar = [1, 2, 3];139140part.testSaveState();141142// Re-Create to assert memento contents143part = disposables.add(new MyPart(b));144145memento = part.testGetMemento(StorageScope.PROFILE, StorageTarget.MACHINE);146assert(memento);147assert.strictEqual(memento.foo, 'bar');148assert.strictEqual(memento.bar.length, 3);149150// Empty Memento stores empty object151delete memento.foo;152delete memento.bar;153154part.testSaveState();155part = disposables.add(new MyPart(b));156memento = part.testGetMemento(StorageScope.PROFILE, StorageTarget.MACHINE);157assert(memento);158assert.strictEqual(isEmptyObject(memento), true);159});160161test('Part Layout with Title and Content', function () {162const b = document.createElement('div');163mainWindow.document.getElementById(fixtureId)!.appendChild(b);164hide(b);165166const part = disposables.add(new MyPart2());167part.create(b);168169assert(mainWindow.document.getElementById('myPart.title'));170assert(mainWindow.document.getElementById('myPart.content'));171});172173test('Part Layout with Content only', function () {174const b = document.createElement('div');175mainWindow.document.getElementById(fixtureId)!.appendChild(b);176hide(b);177178const part = disposables.add(new MyPart3());179part.create(b);180181assert(!mainWindow.document.getElementById('myPart.title'));182assert(mainWindow.document.getElementById('myPart.content'));183});184185ensureNoDisposablesAreLeakedInTestSuite();186});187188189