Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/workbench/contrib/notebook/test/browser/notebookCellLayoutManager.test.ts
5251 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
import * as assert from 'assert';
6
import { ensureNoDisposablesAreLeakedInTestSuite } from '../../../../../base/test/common/utils.js';
7
import { ICellViewModel } from '../../browser/notebookBrowser.js';
8
import { NotebookCellLayoutManager } from '../../browser/notebookCellLayoutManager.js';
9
import { INotebookCellList } from '../../browser/view/notebookRenderingCommon.js';
10
import { INotebookLoggingService } from '../../common/notebookLoggingService.js';
11
import { NotebookEditorWidget } from '../../browser/notebookEditorWidget.js';
12
import { NotebookViewModel } from '../../browser/viewModel/notebookViewModelImpl.js';
13
import { ICellRange } from '../../common/notebookRange.js';
14
15
suite('NotebookCellLayoutManager', () => {
16
17
const store = ensureNoDisposablesAreLeakedInTestSuite();
18
19
const mockCellViewModel = () => {
20
return { handle: 'cell1' } as unknown as ICellViewModel;
21
};
22
23
class MockList implements Pick<INotebookCellList, 'getViewIndex' | 'elementHeight' | 'inRenderingTransaction' | 'updateElementHeight2'> {
24
private _height = new Map();
25
getViewIndex(cell: ICellViewModel) { return this.cells.indexOf(cell) < 0 ? undefined : this.cells.indexOf(cell); }
26
elementHeight(cell: ICellViewModel) { return this._height.get(cell) ?? 100; }
27
inRenderingTransaction = false;
28
updateElementHeight2(cell: ICellViewModel, height: number) { this._height.set(cell, height); }
29
getViewIndexCalled = false;
30
cells: ICellViewModel[] = [];
31
}
32
class MockLoggingService implements INotebookLoggingService {
33
readonly _serviceBrand: undefined;
34
debug() { }
35
info() { }
36
warn() { }
37
error() { }
38
trace() { }
39
}
40
class MockNotebookWidget implements Pick<NotebookEditorWidget, 'viewModel' | 'hasEditorFocus' | 'getAbsoluteTopOfElement' | 'getLength' | 'visibleRanges' | 'getDomNode'> {
41
viewModel: NotebookViewModel | undefined = {
42
hasCell: (cell: ICellViewModel) => true,
43
getCellIndex: () => 0
44
} as unknown as NotebookViewModel;
45
hasEditorFocus() { return true; }
46
getAbsoluteTopOfElement() { return 0; }
47
getLength() { return 1; }
48
visibleRanges: ICellRange[] = [{ start: 0, end: 0 }];
49
getDomNode(): HTMLElement {
50
return {
51
style: {
52
height: '100px'
53
}
54
} as HTMLElement;
55
}
56
}
57
58
test('should update cell height', async () => {
59
const cell = mockCellViewModel();
60
const cell2 = mockCellViewModel();
61
const list = new MockList();
62
list.cells.push(cell);
63
list.cells.push(cell2);
64
const widget = new MockNotebookWidget();
65
const mgr = store.add(new NotebookCellLayoutManager(widget as unknown as NotebookEditorWidget, list as unknown as INotebookCellList, new MockLoggingService()));
66
mgr.layoutNotebookCell(cell, 200);
67
mgr.layoutNotebookCell(cell2, 200);
68
assert.strictEqual(list.elementHeight(cell), 200);
69
assert.strictEqual(list.elementHeight(cell2), 200);
70
});
71
72
test('should schedule updates if already in a rendering transaction', async () => {
73
const cell = mockCellViewModel();
74
const cell2 = mockCellViewModel();
75
const list = new MockList();
76
list.inRenderingTransaction = true;
77
list.cells.push(cell);
78
list.cells.push(cell2);
79
const widget = new MockNotebookWidget();
80
const mgr = store.add(new NotebookCellLayoutManager(widget as unknown as NotebookEditorWidget, list as unknown as INotebookCellList, new MockLoggingService()));
81
82
const promise = mgr.layoutNotebookCell(cell, 200);
83
mgr.layoutNotebookCell(cell2, 200);
84
assert.strictEqual(list.elementHeight(cell), 100);
85
assert.strictEqual(list.elementHeight(cell2), 100);
86
list.inRenderingTransaction = false;
87
88
await promise;
89
90
assert.strictEqual(list.elementHeight(cell), 200);
91
assert.strictEqual(list.elementHeight(cell2), 200);
92
});
93
94
test('should not update if cell is hidden', async () => {
95
const cell = mockCellViewModel();
96
const list = new MockList();
97
const widget = new MockNotebookWidget();
98
const mgr = store.add(new NotebookCellLayoutManager(widget as unknown as NotebookEditorWidget, list as unknown as INotebookCellList, new MockLoggingService()));
99
await mgr.layoutNotebookCell(cell, 200);
100
assert.strictEqual(list.elementHeight(cell), 100);
101
});
102
103
test('should not update if height is unchanged', async () => {
104
const cell = mockCellViewModel();
105
const list = new MockList();
106
list.cells.push(cell);
107
const widget = new MockNotebookWidget();
108
const mgr = store.add(new NotebookCellLayoutManager(widget as unknown as NotebookEditorWidget, list as unknown as INotebookCellList, new MockLoggingService()));
109
await mgr.layoutNotebookCell(cell, 100);
110
assert.strictEqual(list.elementHeight(cell), 100);
111
});
112
});
113
114