|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +'use strict'; |
| 5 | + |
| 6 | +// tslint:disable: no-any |
| 7 | + |
| 8 | +import { assert } from 'chai'; |
| 9 | +import { instance, mock, verify, when } from 'ts-mockito'; |
| 10 | +import { Uri } from 'vscode'; |
| 11 | +import { ActiveResourceService } from '../../client/activation/activeResource'; |
| 12 | +import { DocumentManager } from '../../client/common/application/documentManager'; |
| 13 | +import { IDocumentManager, IWorkspaceService } from '../../client/common/application/types'; |
| 14 | +import { WorkspaceService } from '../../client/common/application/workspace'; |
| 15 | + |
| 16 | +// tslint:disable-next-line: max-func-body-length |
| 17 | +suite('Active resource service', () => { |
| 18 | + let documentManager: IDocumentManager; |
| 19 | + let workspaceService: IWorkspaceService; |
| 20 | + let activeResourceService: ActiveResourceService; |
| 21 | + setup(() => { |
| 22 | + documentManager = mock(DocumentManager); |
| 23 | + workspaceService = mock(WorkspaceService); |
| 24 | + activeResourceService = new ActiveResourceService(instance(documentManager), instance(workspaceService)); |
| 25 | + }); |
| 26 | + |
| 27 | + test('Return document uri if a saved document is currently opened', async () => { |
| 28 | + const activeTextEditor = { |
| 29 | + document: { |
| 30 | + isUntitled: false, |
| 31 | + uri: Uri.parse('a') |
| 32 | + } |
| 33 | + }; |
| 34 | + when(documentManager.activeTextEditor).thenReturn(activeTextEditor as any); |
| 35 | + when(workspaceService.workspaceFolders).thenReturn([]); |
| 36 | + const activeResource = activeResourceService.getActiveResource(); |
| 37 | + assert.deepEqual(activeResource, activeTextEditor.document.uri); |
| 38 | + verify(documentManager.activeTextEditor).atLeast(1); |
| 39 | + verify(workspaceService.workspaceFolders).never(); |
| 40 | + }); |
| 41 | + |
| 42 | + test('Don\'t return document uri if a unsaved document is currently opened', async () => { |
| 43 | + const activeTextEditor = { |
| 44 | + document: { |
| 45 | + isUntitled: true, |
| 46 | + uri: Uri.parse('a') |
| 47 | + } |
| 48 | + }; |
| 49 | + when(documentManager.activeTextEditor).thenReturn(activeTextEditor as any); |
| 50 | + when(workspaceService.workspaceFolders).thenReturn([]); |
| 51 | + const activeResource = activeResourceService.getActiveResource(); |
| 52 | + assert.notDeepEqual(activeResource, activeTextEditor.document.uri); |
| 53 | + verify(documentManager.activeTextEditor).atLeast(1); |
| 54 | + verify(workspaceService.workspaceFolders).atLeast(1); |
| 55 | + }); |
| 56 | + |
| 57 | + test('If no document is currently opened & the workspace opened contains workspace folders, return the uri of the first workspace folder', async () => { |
| 58 | + const workspaceFolders = [ |
| 59 | + { |
| 60 | + uri: Uri.parse('a') |
| 61 | + }, |
| 62 | + { |
| 63 | + uri: Uri.parse('b') |
| 64 | + }]; |
| 65 | + when(documentManager.activeTextEditor).thenReturn(undefined); |
| 66 | + when(workspaceService.workspaceFolders).thenReturn(workspaceFolders as any); |
| 67 | + const activeResource = activeResourceService.getActiveResource(); |
| 68 | + assert.deepEqual(activeResource, workspaceFolders[0].uri); |
| 69 | + verify(documentManager.activeTextEditor).atLeast(1); |
| 70 | + verify(workspaceService.workspaceFolders).atLeast(1); |
| 71 | + }); |
| 72 | + |
| 73 | + test('If no document is currently opened & no folder is opened, return undefined', async () => { |
| 74 | + when(documentManager.activeTextEditor).thenReturn(undefined); |
| 75 | + when(workspaceService.workspaceFolders).thenReturn(undefined); |
| 76 | + const activeResource = activeResourceService.getActiveResource(); |
| 77 | + assert.deepEqual(activeResource, undefined); |
| 78 | + verify(documentManager.activeTextEditor).atLeast(1); |
| 79 | + verify(workspaceService.workspaceFolders).atLeast(1); |
| 80 | + }); |
| 81 | + |
| 82 | + test('If no document is currently opened & workspace contains no workspace folders, return undefined', async () => { |
| 83 | + when(documentManager.activeTextEditor).thenReturn(undefined); |
| 84 | + when(workspaceService.workspaceFolders).thenReturn([]); |
| 85 | + const activeResource = activeResourceService.getActiveResource(); |
| 86 | + assert.deepEqual(activeResource, undefined); |
| 87 | + verify(documentManager.activeTextEditor).atLeast(1); |
| 88 | + verify(workspaceService.workspaceFolders).atLeast(1); |
| 89 | + }); |
| 90 | +}); |
0 commit comments