-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathstatus-bar.test.ts
More file actions
139 lines (116 loc) · 6.41 KB
/
Copy pathstatus-bar.test.ts
File metadata and controls
139 lines (116 loc) · 6.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/**
* Copyright 2022-2026 Arm Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import 'jest';
import * as vscode from 'vscode';
import { ExtensionContext } from 'vscode';
import { cmsisToolboxManagerFactory } from './solutions/cmsis-toolbox.factories';
import { MockSolutionManager, solutionManagerFactory } from './solutions/solution-manager.factories';
import { StatusBar } from './status-bar';
import { ThemeProvider } from './vscode-api/theme-provider';
import { csolutionFactory, CSolutionMock } from './solutions/csolution.factory';
import path from 'path';
const mockCreateStatusBarItem = vscode.window.createStatusBarItem as jest.Mock;
describe('StatusBar', () => {
const extensionContext = { subscriptions: [] } as unknown as ExtensionContext;
const themeProvider: ThemeProvider = { getThemeColor: (color) => ({ id: color }) };
let solutionManager: MockSolutionManager;
let csolution: CSolutionMock;
beforeEach(() => {
solutionManager = solutionManagerFactory();
csolution = csolutionFactory({
solutionPath: path.join(solutionManager.workspaceFolder!.fsPath, 'solution', 'test.csolution.yml')
});
solutionManager.getCsolution.mockReturnValue(csolution);
});
it('does not create a status bar item on construction', () => {
const cmsisToolboxManager = cmsisToolboxManagerFactory();
new StatusBar(solutionManager, cmsisToolboxManager, themeProvider);
expect(mockCreateStatusBarItem).not.toHaveBeenCalled();
});
describe('after activation', () => {
it('creates a status bar item on activation', async () => {
const cmsisToolboxManager = cmsisToolboxManagerFactory();
const statusBar = new StatusBar(solutionManager, cmsisToolboxManager, themeProvider);
await statusBar.activate(extensionContext);
expect(mockCreateStatusBarItem).toHaveBeenCalledTimes(1);
expect(mockCreateStatusBarItem.mock.results[0].value.command).toBe(StatusBar.commandType);
expect(mockCreateStatusBarItem.mock.results[0].value.name).toBe('CMSIS Context');
});
it('shows and hides the status bar item according to load state', async () => {
const cmsisToolboxManager = cmsisToolboxManagerFactory();
const statusBar = new StatusBar(solutionManager, cmsisToolboxManager, themeProvider);
await statusBar.activate(extensionContext);
const statusBarItem = mockCreateStatusBarItem.mock.results[0].value;
solutionManager.fireOnDidChangeLoadState(
{ solutionPath: 'sol' },
{ solutionPath: '' }
);
expect(statusBarItem.show).toHaveBeenCalledTimes(1);
expect(statusBarItem.hide).toHaveBeenCalledTimes(0);
solutionManager.fireOnDidChangeLoadState(
{ solutionPath: '' },
{ solutionPath: 'sol' }
);
expect(statusBarItem.show).toHaveBeenCalledTimes(1);
expect(statusBarItem.hide).toHaveBeenCalledTimes(1);
});
it('shows cmsis tool status bar', async () => {
csolution.getActiveTargetSetName = jest.fn().mockReturnValue('target@set');
csolution.getContextDescriptors = jest.fn().mockReturnValue([{
displayName: 'project.build+target',
projectName: 'project',
buildType: 'build',
targetType: 'target'
}]);
const cmsisToolboxManager = cmsisToolboxManagerFactory();
const statusBar = new StatusBar(solutionManager, cmsisToolboxManager, themeProvider);
await statusBar.activate(extensionContext);
const statusBarItem = mockCreateStatusBarItem.mock.results[0].value;
// start cmsis tool
solutionManager.fireOnDidChangeLoadState(
{ solutionPath: 'sol' },
{ solutionPath: '' }
);
solutionManager.onLoadedBuildFilesEmitter.fire(['success', false]);
cmsisToolboxManager.mockTriggerOnRunCmsisTool(true, false);
expect(statusBarItem.show).toHaveBeenCalledTimes(3);
expect(statusBarItem.text).toBe('$(sync~spin) target@set');
// end cmsis tool
cmsisToolboxManager.mockTriggerOnRunCmsisTool(false, false);
expect(statusBarItem.show).toHaveBeenCalledTimes(4);
expect(statusBarItem.text).toBe('$(target) target@set');
expect(statusBarItem.tooltip.valueOf()).toEqual('**solution/test**\n - project.build\n');
// cbuild setup completed and files loaded successfully
solutionManager.onLoadedBuildFilesEmitter.fire(['success', false]);
expect(statusBarItem.show).toHaveBeenCalledTimes(5);
expect(statusBarItem.backgroundColor).toStrictEqual(themeProvider.getThemeColor('statusBarItem.background'));
// Run status bar command
statusBar.runOnClick();
expect(vscode.commands.executeCommand).toHaveBeenCalledTimes(1);
expect(vscode.commands.executeCommand).toHaveBeenCalledWith('cmsis-csolution.manageSolution');
(vscode.commands.executeCommand as jest.Mock).mockClear();
// cbuild setup completed with error
solutionManager.onLoadedBuildFilesEmitter.fire(['error', false]);
expect(statusBarItem.show).toHaveBeenCalledTimes(6);
expect(statusBarItem.backgroundColor).toStrictEqual(themeProvider.getThemeColor('statusBarItem.errorBackground'));
// Run status bar command
statusBar.runOnClick();
expect(vscode.commands.executeCommand).toHaveBeenCalledTimes(2);
expect(vscode.commands.executeCommand).toHaveBeenCalledWith('cmsis-csolution.manageSolution');
expect(vscode.commands.executeCommand).toHaveBeenCalledWith(expect.stringContaining('workbench.action.output.show.extension-output'));
});
});
});