Path: blob/main/src/vs/workbench/contrib/notebook/test/browser/notebookCellLayoutManager.test.ts
5251 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*--------------------------------------------------------------------------------------------*/4import * as assert from 'assert';5import { ensureNoDisposablesAreLeakedInTestSuite } from '../../../../../base/test/common/utils.js';6import { ICellViewModel } from '../../browser/notebookBrowser.js';7import { NotebookCellLayoutManager } from '../../browser/notebookCellLayoutManager.js';8import { INotebookCellList } from '../../browser/view/notebookRenderingCommon.js';9import { INotebookLoggingService } from '../../common/notebookLoggingService.js';10import { NotebookEditorWidget } from '../../browser/notebookEditorWidget.js';11import { NotebookViewModel } from '../../browser/viewModel/notebookViewModelImpl.js';12import { ICellRange } from '../../common/notebookRange.js';1314suite('NotebookCellLayoutManager', () => {1516const store = ensureNoDisposablesAreLeakedInTestSuite();1718const mockCellViewModel = () => {19return { handle: 'cell1' } as unknown as ICellViewModel;20};2122class MockList implements Pick<INotebookCellList, 'getViewIndex' | 'elementHeight' | 'inRenderingTransaction' | 'updateElementHeight2'> {23private _height = new Map();24getViewIndex(cell: ICellViewModel) { return this.cells.indexOf(cell) < 0 ? undefined : this.cells.indexOf(cell); }25elementHeight(cell: ICellViewModel) { return this._height.get(cell) ?? 100; }26inRenderingTransaction = false;27updateElementHeight2(cell: ICellViewModel, height: number) { this._height.set(cell, height); }28getViewIndexCalled = false;29cells: ICellViewModel[] = [];30}31class MockLoggingService implements INotebookLoggingService {32readonly _serviceBrand: undefined;33debug() { }34info() { }35warn() { }36error() { }37trace() { }38}39class MockNotebookWidget implements Pick<NotebookEditorWidget, 'viewModel' | 'hasEditorFocus' | 'getAbsoluteTopOfElement' | 'getLength' | 'visibleRanges' | 'getDomNode'> {40viewModel: NotebookViewModel | undefined = {41hasCell: (cell: ICellViewModel) => true,42getCellIndex: () => 043} as unknown as NotebookViewModel;44hasEditorFocus() { return true; }45getAbsoluteTopOfElement() { return 0; }46getLength() { return 1; }47visibleRanges: ICellRange[] = [{ start: 0, end: 0 }];48getDomNode(): HTMLElement {49return {50style: {51height: '100px'52}53} as HTMLElement;54}55}5657test('should update cell height', async () => {58const cell = mockCellViewModel();59const cell2 = mockCellViewModel();60const list = new MockList();61list.cells.push(cell);62list.cells.push(cell2);63const widget = new MockNotebookWidget();64const mgr = store.add(new NotebookCellLayoutManager(widget as unknown as NotebookEditorWidget, list as unknown as INotebookCellList, new MockLoggingService()));65mgr.layoutNotebookCell(cell, 200);66mgr.layoutNotebookCell(cell2, 200);67assert.strictEqual(list.elementHeight(cell), 200);68assert.strictEqual(list.elementHeight(cell2), 200);69});7071test('should schedule updates if already in a rendering transaction', async () => {72const cell = mockCellViewModel();73const cell2 = mockCellViewModel();74const list = new MockList();75list.inRenderingTransaction = true;76list.cells.push(cell);77list.cells.push(cell2);78const widget = new MockNotebookWidget();79const mgr = store.add(new NotebookCellLayoutManager(widget as unknown as NotebookEditorWidget, list as unknown as INotebookCellList, new MockLoggingService()));8081const promise = mgr.layoutNotebookCell(cell, 200);82mgr.layoutNotebookCell(cell2, 200);83assert.strictEqual(list.elementHeight(cell), 100);84assert.strictEqual(list.elementHeight(cell2), 100);85list.inRenderingTransaction = false;8687await promise;8889assert.strictEqual(list.elementHeight(cell), 200);90assert.strictEqual(list.elementHeight(cell2), 200);91});9293test('should not update if cell is hidden', async () => {94const cell = mockCellViewModel();95const list = new MockList();96const widget = new MockNotebookWidget();97const mgr = store.add(new NotebookCellLayoutManager(widget as unknown as NotebookEditorWidget, list as unknown as INotebookCellList, new MockLoggingService()));98await mgr.layoutNotebookCell(cell, 200);99assert.strictEqual(list.elementHeight(cell), 100);100});101102test('should not update if height is unchanged', async () => {103const cell = mockCellViewModel();104const list = new MockList();105list.cells.push(cell);106const widget = new MockNotebookWidget();107const mgr = store.add(new NotebookCellLayoutManager(widget as unknown as NotebookEditorWidget, list as unknown as INotebookCellList, new MockLoggingService()));108await mgr.layoutNotebookCell(cell, 100);109assert.strictEqual(list.elementHeight(cell), 100);110});111});112113114