|
| 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 | + |
| 6 | +import 'mocha'; |
| 7 | +import * as assert from 'assert'; |
| 8 | +import * as vscode from 'vscode'; |
| 9 | +import { join } from 'path'; |
| 10 | + |
| 11 | +function waitFor(ms: number): Promise<void> { |
| 12 | + let resolveFunc: () => void; |
| 13 | + |
| 14 | + const promise = new Promise<void>(resolve => { |
| 15 | + resolveFunc = resolve; |
| 16 | + }); |
| 17 | + setTimeout(() => { |
| 18 | + resolveFunc!(); |
| 19 | + }, ms); |
| 20 | + |
| 21 | + return promise; |
| 22 | +} |
| 23 | + |
| 24 | +suite('notebook workflow', () => { |
| 25 | + test('notebook open', async function () { |
| 26 | + const resource = vscode.Uri.parse(join(vscode.workspace.rootPath || '', './first.ipynb')); |
| 27 | + await vscode.commands.executeCommand('vscode.openWith', resource, 'notebookTest'); |
| 28 | + |
| 29 | + await waitFor(500); |
| 30 | + assert.equal(vscode.notebook.activeNotebookEditor !== undefined, true, 'notebook first'); |
| 31 | + assert.equal(vscode.notebook.activeNotebookEditor!.selection?.source, 'test'); |
| 32 | + assert.equal(vscode.notebook.activeNotebookEditor!.selection?.language, 'typescript'); |
| 33 | + |
| 34 | + await vscode.commands.executeCommand('workbench.notebook.code.insertCellBelow'); |
| 35 | + assert.equal(vscode.notebook.activeNotebookEditor!.selection?.source, ''); |
| 36 | + |
| 37 | + await vscode.commands.executeCommand('workbench.notebook.code.insertCellAbove'); |
| 38 | + const activeCell = vscode.notebook.activeNotebookEditor!.selection; |
| 39 | + assert.notEqual(vscode.notebook.activeNotebookEditor!.selection, undefined); |
| 40 | + assert.equal(activeCell!.source, ''); |
| 41 | + assert.equal(vscode.notebook.activeNotebookEditor!.document.cells.length, 3); |
| 42 | + assert.equal(vscode.notebook.activeNotebookEditor!.document.cells.indexOf(activeCell!), 1); |
| 43 | + |
| 44 | + await vscode.commands.executeCommand('workbench.action.files.save'); |
| 45 | + await vscode.commands.executeCommand('workbench.action.closeActiveEditor'); |
| 46 | + }); |
| 47 | + |
| 48 | + test('notebook cell actions', async function () { |
| 49 | + const resource = vscode.Uri.parse(join(vscode.workspace.rootPath || '', './second.ipynb')); |
| 50 | + await vscode.commands.executeCommand('vscode.openWith', resource, 'notebookTest'); |
| 51 | + |
| 52 | + await waitFor(500); |
| 53 | + assert.equal(vscode.notebook.activeNotebookEditor !== undefined, true, 'notebook first'); |
| 54 | + assert.equal(vscode.notebook.activeNotebookEditor!.selection?.source, 'test'); |
| 55 | + assert.equal(vscode.notebook.activeNotebookEditor!.selection?.language, 'typescript'); |
| 56 | + |
| 57 | + // ---- insert cell below and focus ---- // |
| 58 | + await vscode.commands.executeCommand('workbench.notebook.code.insertCellBelow'); |
| 59 | + assert.equal(vscode.notebook.activeNotebookEditor!.selection?.source, ''); |
| 60 | + |
| 61 | + // ---- insert cell above and focus ---- // |
| 62 | + await vscode.commands.executeCommand('workbench.notebook.code.insertCellAbove'); |
| 63 | + let activeCell = vscode.notebook.activeNotebookEditor!.selection; |
| 64 | + assert.notEqual(vscode.notebook.activeNotebookEditor!.selection, undefined); |
| 65 | + assert.equal(activeCell!.source, ''); |
| 66 | + assert.equal(vscode.notebook.activeNotebookEditor!.document.cells.length, 3); |
| 67 | + assert.equal(vscode.notebook.activeNotebookEditor!.document.cells.indexOf(activeCell!), 1); |
| 68 | + |
| 69 | + // ---- focus bottom ---- // |
| 70 | + await vscode.commands.executeCommand('workbench.action.notebook.focusBottom'); |
| 71 | + activeCell = vscode.notebook.activeNotebookEditor!.selection; |
| 72 | + assert.equal(vscode.notebook.activeNotebookEditor!.document.cells.indexOf(activeCell!), 2); |
| 73 | + |
| 74 | + // ---- focus top and then copy down ---- // |
| 75 | + await vscode.commands.executeCommand('workbench.action.notebook.focusTop'); |
| 76 | + activeCell = vscode.notebook.activeNotebookEditor!.selection; |
| 77 | + assert.equal(vscode.notebook.activeNotebookEditor!.document.cells.indexOf(activeCell!), 0); |
| 78 | + |
| 79 | + await vscode.commands.executeCommand('workbench.notebook.cell.copyDown'); |
| 80 | + activeCell = vscode.notebook.activeNotebookEditor!.selection; |
| 81 | + assert.equal(vscode.notebook.activeNotebookEditor!.document.cells.indexOf(activeCell!), 1); |
| 82 | + assert.equal(activeCell?.source, 'test'); |
| 83 | + |
| 84 | + await vscode.commands.executeCommand('workbench.notebook.cell.delete'); |
| 85 | + activeCell = vscode.notebook.activeNotebookEditor!.selection; |
| 86 | + assert.equal(vscode.notebook.activeNotebookEditor!.document.cells.indexOf(activeCell!), 1); |
| 87 | + assert.equal(activeCell?.source, ''); |
| 88 | + |
| 89 | + // ---- focus top and then copy up ---- // |
| 90 | + await vscode.commands.executeCommand('workbench.action.notebook.focusTop'); |
| 91 | + await vscode.commands.executeCommand('workbench.notebook.cell.copyUp'); |
| 92 | + assert.equal(vscode.notebook.activeNotebookEditor!.document.cells.length, 4); |
| 93 | + assert.equal(vscode.notebook.activeNotebookEditor!.document.cells[0].source, 'test'); |
| 94 | + assert.equal(vscode.notebook.activeNotebookEditor!.document.cells[1].source, 'test'); |
| 95 | + assert.equal(vscode.notebook.activeNotebookEditor!.document.cells[2].source, ''); |
| 96 | + assert.equal(vscode.notebook.activeNotebookEditor!.document.cells[3].source, ''); |
| 97 | + activeCell = vscode.notebook.activeNotebookEditor!.selection; |
| 98 | + assert.equal(vscode.notebook.activeNotebookEditor!.document.cells.indexOf(activeCell!), 0); |
| 99 | + |
| 100 | + |
| 101 | + // ---- move up and down ---- // |
| 102 | + |
| 103 | + // await vscode.commands.executeCommand('workbench.notebook.cell.moveDown'); |
| 104 | + // await vscode.commands.executeCommand('workbench.notebook.cell.moveDown'); |
| 105 | + // activeCell = vscode.notebook.activeNotebookEditor!.selection; |
| 106 | + |
| 107 | + // assert.equal(vscode.notebook.activeNotebookEditor!.document.cells.indexOf(activeCell!), 2); |
| 108 | + // assert.equal(vscode.notebook.activeNotebookEditor!.document.cells[0].source, 'test'); |
| 109 | + // assert.equal(vscode.notebook.activeNotebookEditor!.document.cells[1].source, ''); |
| 110 | + // assert.equal(vscode.notebook.activeNotebookEditor!.document.cells[2].source, 'test'); |
| 111 | + // assert.equal(vscode.notebook.activeNotebookEditor!.document.cells[3].source, ''); |
| 112 | + |
| 113 | + |
| 114 | + // ---- ---- // |
| 115 | + |
| 116 | + await vscode.commands.executeCommand('workbench.action.closeActiveEditor'); |
| 117 | + }); |
| 118 | +}); |
0 commit comments