From bae64052f49e00bd30da80720bfcf4e9609b1827 Mon Sep 17 00:00:00 2001 From: Johannes Rieken Date: Wed, 12 Aug 2020 09:05:18 +0200 Subject: [PATCH] unit test for document remove --- .../api/browser/mainThreadNotebook.ts | 2 +- .../workbench/api/common/extHost.protocol.ts | 2 +- .../workbench/api/common/extHostNotebook.ts | 2 +- .../test/browser/api/extHostNotebook.test.ts | 49 +++++++++++++++++-- .../api/extHostNotebookConcatDocument.test.ts | 4 +- 5 files changed, 50 insertions(+), 9 deletions(-) diff --git a/src/vs/workbench/api/browser/mainThreadNotebook.ts b/src/vs/workbench/api/browser/mainThreadNotebook.ts index ceed17dc7b2..d9a61aa4f42 100644 --- a/src/vs/workbench/api/browser/mainThreadNotebook.ts +++ b/src/vs/workbench/api/browser/mainThreadNotebook.ts @@ -203,7 +203,7 @@ export class MainThreadNotebooks extends Disposable implements MainThreadNoteboo async removeNotebookTextModel(uri: URI): Promise { // TODO@rebornix, remove cell should use emitDelta as well to ensure document/editor events are sent together - await this._proxy.$acceptDocumentAndEditorsDelta({ removedDocuments: [uri] }); + this._proxy.$acceptDocumentAndEditorsDelta({ removedDocuments: [uri] }); let textModelDisposableStore = this._editorEventListenersMapping.get(uri.toString()); textModelDisposableStore?.dispose(); this._editorEventListenersMapping.delete(URI.from(uri).toString()); diff --git a/src/vs/workbench/api/common/extHost.protocol.ts b/src/vs/workbench/api/common/extHost.protocol.ts index 0ad328d3549..1c7e49f5c97 100644 --- a/src/vs/workbench/api/common/extHost.protocol.ts +++ b/src/vs/workbench/api/common/extHost.protocol.ts @@ -1633,7 +1633,7 @@ export interface ExtHostNotebookShape { $acceptModelChanged(uriComponents: UriComponents, event: NotebookCellsChangedEvent): void; $acceptModelSaved(uriComponents: UriComponents): void; $acceptEditorPropertiesChanged(uriComponents: UriComponents, data: INotebookEditorPropertiesChangeData): void; - $acceptDocumentAndEditorsDelta(delta: INotebookDocumentsAndEditorsDelta): Promise; + $acceptDocumentAndEditorsDelta(delta: INotebookDocumentsAndEditorsDelta): void; $undoNotebook(viewType: string, uri: UriComponents, editId: number, isDirty: boolean): Promise; $redoNotebook(viewType: string, uri: UriComponents, editId: number, isDirty: boolean): Promise; diff --git a/src/vs/workbench/api/common/extHostNotebook.ts b/src/vs/workbench/api/common/extHostNotebook.ts index eeecd1409b8..ef20b264351 100644 --- a/src/vs/workbench/api/common/extHostNotebook.ts +++ b/src/vs/workbench/api/common/extHostNotebook.ts @@ -1513,7 +1513,7 @@ export class ExtHostNotebookController implements ExtHostNotebookShape, ExtHostN this._editors.set(editorId, { editor }); } - async $acceptDocumentAndEditorsDelta(delta: INotebookDocumentsAndEditorsDelta) { + $acceptDocumentAndEditorsDelta(delta: INotebookDocumentsAndEditorsDelta): void { let editorChanged = false; if (delta.removedDocuments) { diff --git a/src/vs/workbench/test/browser/api/extHostNotebook.test.ts b/src/vs/workbench/test/browser/api/extHostNotebook.test.ts index b1aaf836009..98908bbb0eb 100644 --- a/src/vs/workbench/test/browser/api/extHostNotebook.test.ts +++ b/src/vs/workbench/test/browser/api/extHostNotebook.test.ts @@ -10,7 +10,7 @@ import { TestRPCProtocol } from 'vs/workbench/test/browser/api/testRPCProtocol'; import { DisposableStore } from 'vs/base/common/lifecycle'; import { NullLogService } from 'vs/platform/log/common/log'; import { mock } from 'vs/base/test/common/mock'; -import { MainContext, MainThreadCommandsShape, MainThreadNotebookShape } from 'vs/workbench/api/common/extHost.protocol'; +import { IModelAddedData, MainContext, MainThreadCommandsShape, MainThreadNotebookShape } from 'vs/workbench/api/common/extHost.protocol'; import { ExtHostNotebookDocument, ExtHostNotebookController } from 'vs/workbench/api/common/extHostNotebook'; import { CellKind, CellUri, NotebookCellsChangeType } from 'vs/workbench/contrib/notebook/common/notebookCommon'; import { URI } from 'vs/base/common/uri'; @@ -47,7 +47,7 @@ suite('NotebookCell#Document', function () { let reg = extHostNotebooks.registerNotebookContentProvider(nullExtensionDescription, 'test', new class extends mock() { // async openNotebook() { } }); - await extHostNotebooks.$acceptDocumentAndEditorsDelta({ + extHostNotebooks.$acceptDocumentAndEditorsDelta({ addedDocuments: [{ handle: 0, uri: notebookUri, @@ -77,7 +77,7 @@ suite('NotebookCell#Document', function () { selections: [0] }] }); - await extHostNotebooks.$acceptDocumentAndEditorsDelta({ newActiveEditor: '_notebook_editor_0' }); + extHostNotebooks.$acceptDocumentAndEditorsDelta({ newActiveEditor: '_notebook_editor_0' }); notebook = extHostNotebooks.notebookDocuments[0]!; @@ -116,7 +116,7 @@ suite('NotebookCell#Document', function () { removedCellUris.push(doc.uri.toString()); }); - await extHostNotebooks.$acceptDocumentAndEditorsDelta({ removedDocuments: [notebook.uri] }); + extHostNotebooks.$acceptDocumentAndEditorsDelta({ removedDocuments: [notebook.uri] }); reg.dispose(); assert.strictEqual(removedCellUris.length, 2); @@ -174,4 +174,45 @@ suite('NotebookCell#Document', function () { await p; }); + + test('cell document stays open when notebook is still open', async function () { + + const docs: vscode.TextDocument[] = []; + const addData: IModelAddedData[] = []; + for (let cell of notebook.cells) { + const doc = extHostDocuments.getDocument(cell.uri); + assert.ok(doc); + assert.equal(extHostDocuments.getDocument(cell.uri).isClosed, false); + docs.push(doc); + addData.push({ + EOL: '\n', + isDirty: doc.isDirty, + lines: doc.getText().split('\n'), + modeId: doc.languageId, + uri: doc.uri, + versionId: doc.version + }); + } + + // this call happens when opening a document on the main side + extHostDocumentsAndEditors.$acceptDocumentsAndEditorsDelta({ addedDocuments: addData }); + + // this call happens when closing a document from the main side + extHostDocumentsAndEditors.$acceptDocumentsAndEditorsDelta({ removedDocuments: docs.map(d => d.uri) }); + + // notebook is still open -> cell documents stay open + for (let cell of notebook.cells) { + assert.ok(extHostDocuments.getDocument(cell.uri)); + assert.equal(extHostDocuments.getDocument(cell.uri).isClosed, false); + } + + // close notebook -> docs are closed + extHostNotebooks.$acceptDocumentAndEditorsDelta({ removedDocuments: [notebook.uri] }); + for (let cell of notebook.cells) { + assert.throws(() => extHostDocuments.getDocument(cell.uri)); + } + for (let doc of docs) { + assert.equal(doc.isClosed, true); + } + }); }); diff --git a/src/vs/workbench/test/browser/api/extHostNotebookConcatDocument.test.ts b/src/vs/workbench/test/browser/api/extHostNotebookConcatDocument.test.ts index 2349b4b6a99..237c1a30e4a 100644 --- a/src/vs/workbench/test/browser/api/extHostNotebookConcatDocument.test.ts +++ b/src/vs/workbench/test/browser/api/extHostNotebookConcatDocument.test.ts @@ -48,7 +48,7 @@ suite('NotebookConcatDocument', function () { let reg = extHostNotebooks.registerNotebookContentProvider(nullExtensionDescription, 'test', new class extends mock() { // async openNotebook() { } }); - await extHostNotebooks.$acceptDocumentAndEditorsDelta({ + extHostNotebooks.$acceptDocumentAndEditorsDelta({ addedDocuments: [{ handle: 0, uri: notebookUri, @@ -72,7 +72,7 @@ suite('NotebookConcatDocument', function () { } ] }); - await extHostNotebooks.$acceptDocumentAndEditorsDelta({ newActiveEditor: '_notebook_editor_0' }); + extHostNotebooks.$acceptDocumentAndEditorsDelta({ newActiveEditor: '_notebook_editor_0' }); notebook = extHostNotebooks.notebookDocuments[0]!; -- GitLab