From 5d8bd237564650c7257b65b6839597b7150ffe1a Mon Sep 17 00:00:00 2001 From: Rich Chiodo Date: Mon, 23 May 2022 17:25:00 -0700 Subject: Add a test --- .../singlefolder-tests/interactiveWindow.test.ts | 65 ++++++++++++++++++++++ .../src/singlefolder-tests/notebook.test.ts | 4 +- 2 files changed, 67 insertions(+), 2 deletions(-) create mode 100644 extensions/vscode-api-tests/src/singlefolder-tests/interactiveWindow.test.ts (limited to 'extensions') diff --git a/extensions/vscode-api-tests/src/singlefolder-tests/interactiveWindow.test.ts b/extensions/vscode-api-tests/src/singlefolder-tests/interactiveWindow.test.ts new file mode 100644 index 00000000000..eafc18804fd --- /dev/null +++ b/extensions/vscode-api-tests/src/singlefolder-tests/interactiveWindow.test.ts @@ -0,0 +1,65 @@ +/*--------------------------------------------------------------------------------------------- + * Copyright (c) Microsoft Corporation. All rights reserved. + * Licensed under the MIT License. See License.txt in the project root for license information. + *--------------------------------------------------------------------------------------------*/ + +import * as assert from 'assert'; +import 'mocha'; +import * as vscode from 'vscode'; +import { disposeAll } from '../utils'; +import { Kernel, saveAllFilesAndCloseAll } from './notebook.test'; + +export type INativeInteractiveWindow = { notebookUri: vscode.Uri; inputUri: vscode.Uri; notebookEditor: vscode.NotebookEditor }; + +async function createInteractiveWindow(kernel: Kernel) { + const { notebookEditor } = (await vscode.commands.executeCommand( + 'interactive.open', + // Keep focus on the owning file if there is one + { viewColumn: vscode.ViewColumn.Beside, preserveFocus: false }, + undefined, + kernel.controller.id, + undefined + )) as unknown as INativeInteractiveWindow; + + return notebookEditor; +} + + +(vscode.env.uiKind === vscode.UIKind.Web ? suite.skip : suite)('Interactive Window', function () { + + const testDisposables: vscode.Disposable[] = []; + let defaultKernel: Kernel; + + setup(async function () { + // there should be ONE default kernel in this suite + defaultKernel = new Kernel('mainKernel', 'Notebook Default Kernel'); + testDisposables.push(defaultKernel.controller); + await saveAllFilesAndCloseAll(); + }); + + teardown(async function () { + disposeAll(testDisposables); + testDisposables.length = 0; + await saveAllFilesAndCloseAll(); + }); + + test('Can open an interactive window', async () => { + assert.ok(vscode.workspace.workspaceFolders); + const notebookEditor = await createInteractiveWindow(defaultKernel); + assert.ok(notebookEditor); + + // Try adding a cell and running it. + const cell = new vscode.NotebookCellData(vscode.NotebookCellKind.Code, 'print foo', 'typescript'); + const edit = vscode.NotebookEdit.insertCells(0, [cell]); + const workspaceEdit = new vscode.WorkspaceEdit(); + workspaceEdit.set(notebookEditor.notebook.uri, [edit]); + await vscode.workspace.applyEdit(workspaceEdit); + + assert.strictEqual(notebookEditor.notebook.cellCount, 1); + assert.strictEqual(notebookEditor.notebook.cellAt(0).kind, vscode.NotebookCellKind.Code); + + await vscode.commands.executeCommand('notebook.execute'); + assert.strictEqual(notebookEditor.notebook.cellAt(0).outputs.length, 1, 'should execute'); + + }); +}); diff --git a/extensions/vscode-api-tests/src/singlefolder-tests/notebook.test.ts b/extensions/vscode-api-tests/src/singlefolder-tests/notebook.test.ts index 462e3113698..b68eb587922 100644 --- a/extensions/vscode-api-tests/src/singlefolder-tests/notebook.test.ts +++ b/extensions/vscode-api-tests/src/singlefolder-tests/notebook.test.ts @@ -18,7 +18,7 @@ async function openRandomNotebookDocument() { return vscode.workspace.openNotebookDocument(uri); } -async function saveAllFilesAndCloseAll() { +export async function saveAllFilesAndCloseAll() { await saveAllEditors(); await closeAllEditors(); } @@ -29,7 +29,7 @@ async function withEvent(event: vscode.Event, callback: (e: Promise) => } -class Kernel { +export class Kernel { readonly controller: vscode.NotebookController; -- cgit v1.2.3 From 9322fd543d401efa136d24656c8be455edd7c010 Mon Sep 17 00:00:00 2001 From: Rich Chiodo Date: Tue, 24 May 2022 10:13:04 -0700 Subject: Fix test to pass --- .../vscode-api-tests/src/singlefolder-tests/interactiveWindow.test.ts | 2 +- extensions/vscode-api-tests/src/singlefolder-tests/notebook.test.ts | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) (limited to 'extensions') diff --git a/extensions/vscode-api-tests/src/singlefolder-tests/interactiveWindow.test.ts b/extensions/vscode-api-tests/src/singlefolder-tests/interactiveWindow.test.ts index eafc18804fd..0890418484c 100644 --- a/extensions/vscode-api-tests/src/singlefolder-tests/interactiveWindow.test.ts +++ b/extensions/vscode-api-tests/src/singlefolder-tests/interactiveWindow.test.ts @@ -32,7 +32,7 @@ async function createInteractiveWindow(kernel: Kernel) { setup(async function () { // there should be ONE default kernel in this suite - defaultKernel = new Kernel('mainKernel', 'Notebook Default Kernel'); + defaultKernel = new Kernel('mainKernel', 'Notebook Default Kernel', 'interactive'); testDisposables.push(defaultKernel.controller); await saveAllFilesAndCloseAll(); }); diff --git a/extensions/vscode-api-tests/src/singlefolder-tests/notebook.test.ts b/extensions/vscode-api-tests/src/singlefolder-tests/notebook.test.ts index b68eb587922..8c413aaf9fb 100644 --- a/extensions/vscode-api-tests/src/singlefolder-tests/notebook.test.ts +++ b/extensions/vscode-api-tests/src/singlefolder-tests/notebook.test.ts @@ -35,8 +35,8 @@ export class Kernel { readonly associatedNotebooks = new Set(); - constructor(id: string, label: string) { - this.controller = vscode.notebooks.createNotebookController(id, 'notebookCoreTest', label); + constructor(id: string, label: string, viewType: string = 'notebookCoreTest') { + this.controller = vscode.notebooks.createNotebookController(id, viewType, label); this.controller.executeHandler = this._execute.bind(this); this.controller.supportsExecutionOrder = true; this.controller.supportedLanguages = ['typescript', 'javascript']; -- cgit v1.2.3 From b79d02db5c11960188a43d8bfa12001ab84bb3b8 Mon Sep 17 00:00:00 2001 From: Rich Chiodo Date: Tue, 24 May 2022 11:01:51 -0700 Subject: Add test for scrolling --- .../singlefolder-tests/interactiveWindow.test.ts | 37 ++++++++++++++++++---- .../src/singlefolder-tests/notebook.test.ts | 9 +++++- 2 files changed, 38 insertions(+), 8 deletions(-) (limited to 'extensions') diff --git a/extensions/vscode-api-tests/src/singlefolder-tests/interactiveWindow.test.ts b/extensions/vscode-api-tests/src/singlefolder-tests/interactiveWindow.test.ts index 0890418484c..6880607ca7d 100644 --- a/extensions/vscode-api-tests/src/singlefolder-tests/interactiveWindow.test.ts +++ b/extensions/vscode-api-tests/src/singlefolder-tests/interactiveWindow.test.ts @@ -24,6 +24,22 @@ async function createInteractiveWindow(kernel: Kernel) { return notebookEditor; } +async function addCell(code: string, notebook: vscode.NotebookDocument) { + const cell = new vscode.NotebookCellData(vscode.NotebookCellKind.Code, code, 'typescript'); + const edit = vscode.NotebookEdit.insertCells(notebook.cellCount, [cell]); + const workspaceEdit = new vscode.WorkspaceEdit(); + workspaceEdit.set(notebook.uri, [edit]); + await vscode.workspace.applyEdit(workspaceEdit); + return notebook.cellAt(notebook.cellCount - 1); +} + +async function addCellAndRun(code: string, notebook: vscode.NotebookDocument) { + const cell = await addCell(code, notebook); + await vscode.commands.executeCommand('notebook.execute'); + assert.strictEqual(cell.outputs.length, 1, 'execute failed'); + return cell; +} + (vscode.env.uiKind === vscode.UIKind.Web ? suite.skip : suite)('Interactive Window', function () { @@ -49,17 +65,24 @@ async function createInteractiveWindow(kernel: Kernel) { assert.ok(notebookEditor); // Try adding a cell and running it. - const cell = new vscode.NotebookCellData(vscode.NotebookCellKind.Code, 'print foo', 'typescript'); - const edit = vscode.NotebookEdit.insertCells(0, [cell]); - const workspaceEdit = new vscode.WorkspaceEdit(); - workspaceEdit.set(notebookEditor.notebook.uri, [edit]); - await vscode.workspace.applyEdit(workspaceEdit); + await addCell('print foo', notebookEditor.notebook); assert.strictEqual(notebookEditor.notebook.cellCount, 1); assert.strictEqual(notebookEditor.notebook.cellAt(0).kind, vscode.NotebookCellKind.Code); + }); + + test('Interactive window scrolls after execute', async () => { + assert.ok(vscode.workspace.workspaceFolders); + const notebookEditor = await createInteractiveWindow(defaultKernel); + assert.ok(notebookEditor); + + // Run and add a bunch of cells + for (let i = 0; i < 20; i++) { + await addCellAndRun(`print ${i}`, notebookEditor.notebook); + } - await vscode.commands.executeCommand('notebook.execute'); - assert.strictEqual(notebookEditor.notebook.cellAt(0).outputs.length, 1, 'should execute'); + // Verify visible range has the last cell + assert.strictEqual(notebookEditor.visibleRanges[notebookEditor.visibleRanges.length - 1].end, notebookEditor.notebook.cellCount, `Last cell is not visible`); }); }); diff --git a/extensions/vscode-api-tests/src/singlefolder-tests/notebook.test.ts b/extensions/vscode-api-tests/src/singlefolder-tests/notebook.test.ts index 8c413aaf9fb..33cad6edb97 100644 --- a/extensions/vscode-api-tests/src/singlefolder-tests/notebook.test.ts +++ b/extensions/vscode-api-tests/src/singlefolder-tests/notebook.test.ts @@ -29,6 +29,12 @@ async function withEvent(event: vscode.Event, callback: (e: Promise) => } +function sleep(ms: number): Promise { + return new Promise(resolve => { + setTimeout(resolve, ms); + }); +} + export class Kernel { readonly controller: vscode.NotebookController; @@ -59,8 +65,9 @@ export class Kernel { // create a single output with exec order 1 and output is plain/text // of either the cell itself or (iff empty) the cell's document's uri const task = this.controller.createNotebookCellExecution(cell); - task.start(); + task.start(Date.now()); task.executionOrder = 1; + await sleep(10); // Force to be take some time await task.replaceOutput([new vscode.NotebookCellOutput([ vscode.NotebookCellOutputItem.text(cell.document.getText() || cell.document.uri.toString(), 'text/plain') ])]); -- cgit v1.2.3