Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/base/test/browser/actionbar.test.ts
5241 views
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 assert from 'assert';
7
import { ActionBar, prepareActions } from '../../browser/ui/actionbar/actionbar.js';
8
import { Action, Separator } from '../../common/actions.js';
9
import { ensureNoDisposablesAreLeakedInTestSuite } from '../common/utils.js';
10
import { createToggleActionViewItemProvider, ToggleActionViewItem, unthemedToggleStyles } from '../../browser/ui/toggle/toggle.js';
11
import { ActionViewItem } from '../../browser/ui/actionbar/actionViewItems.js';
12
13
suite('Actionbar', () => {
14
15
const store = ensureNoDisposablesAreLeakedInTestSuite();
16
17
test('prepareActions()', function () {
18
const a1 = new Separator();
19
const a2 = new Separator();
20
const a3 = store.add(new Action('a3'));
21
const a4 = new Separator();
22
const a5 = new Separator();
23
const a6 = store.add(new Action('a6'));
24
const a7 = new Separator();
25
26
const actions = prepareActions([a1, a2, a3, a4, a5, a6, a7]);
27
assert.strictEqual(actions.length, 3); // duplicate separators get removed
28
assert(actions[0] === a3);
29
assert(actions[1] === a5);
30
assert(actions[2] === a6);
31
});
32
33
test('hasAction()', function () {
34
const container = document.createElement('div');
35
const actionbar = store.add(new ActionBar(container));
36
37
const a1 = store.add(new Action('a1'));
38
const a2 = store.add(new Action('a2'));
39
40
actionbar.push(a1);
41
assert.strictEqual(actionbar.hasAction(a1), true);
42
assert.strictEqual(actionbar.hasAction(a2), false);
43
44
actionbar.pull(0);
45
assert.strictEqual(actionbar.hasAction(a1), false);
46
47
actionbar.push(a1, { index: 1 });
48
actionbar.push(a2, { index: 0 });
49
assert.strictEqual(actionbar.hasAction(a1), true);
50
assert.strictEqual(actionbar.hasAction(a2), true);
51
52
actionbar.pull(0);
53
assert.strictEqual(actionbar.hasAction(a1), true);
54
assert.strictEqual(actionbar.hasAction(a2), false);
55
56
actionbar.pull(0);
57
assert.strictEqual(actionbar.hasAction(a1), false);
58
assert.strictEqual(actionbar.hasAction(a2), false);
59
60
actionbar.push(a1);
61
assert.strictEqual(actionbar.hasAction(a1), true);
62
actionbar.clear();
63
assert.strictEqual(actionbar.hasAction(a1), false);
64
});
65
66
suite('ToggleActionViewItemProvider', () => {
67
68
test('renders toggle for actions with checked state', function () {
69
const container = document.createElement('div');
70
const provider = createToggleActionViewItemProvider(unthemedToggleStyles);
71
const actionbar = store.add(new ActionBar(container, {
72
actionViewItemProvider: provider
73
}));
74
75
const toggleAction = store.add(new Action('toggle', 'Toggle', undefined, true, undefined));
76
toggleAction.checked = true;
77
78
actionbar.push(toggleAction);
79
80
// Verify that the action was rendered as a toggle
81
assert.strictEqual(actionbar.viewItems.length, 1);
82
assert(actionbar.viewItems[0] instanceof ToggleActionViewItem, 'Action with checked state should render as ToggleActionViewItem');
83
});
84
85
test('renders button for actions without checked state', function () {
86
const container = document.createElement('div');
87
const provider = createToggleActionViewItemProvider(unthemedToggleStyles);
88
const actionbar = store.add(new ActionBar(container, {
89
actionViewItemProvider: provider
90
}));
91
92
const buttonAction = store.add(new Action('button', 'Button'));
93
94
actionbar.push(buttonAction);
95
96
// Verify that the action was rendered as a regular button (ActionViewItem)
97
assert.strictEqual(actionbar.viewItems.length, 1);
98
assert(actionbar.viewItems[0] instanceof ActionViewItem, 'Action without checked state should render as ActionViewItem');
99
assert(!(actionbar.viewItems[0] instanceof ToggleActionViewItem), 'Action without checked state should not render as ToggleActionViewItem');
100
});
101
102
test('handles mixed actions (toggles and buttons)', function () {
103
const container = document.createElement('div');
104
const provider = createToggleActionViewItemProvider(unthemedToggleStyles);
105
const actionbar = store.add(new ActionBar(container, {
106
actionViewItemProvider: provider
107
}));
108
109
const toggleAction = store.add(new Action('toggle', 'Toggle'));
110
toggleAction.checked = false;
111
const buttonAction = store.add(new Action('button', 'Button'));
112
113
actionbar.push([toggleAction, buttonAction]);
114
115
// Verify that we have both types of items
116
assert.strictEqual(actionbar.viewItems.length, 2);
117
assert(actionbar.viewItems[0] instanceof ToggleActionViewItem, 'First action should be a toggle');
118
assert(actionbar.viewItems[1] instanceof ActionViewItem, 'Second action should be a button');
119
assert(!(actionbar.viewItems[1] instanceof ToggleActionViewItem), 'Second action should not be a toggle');
120
});
121
122
test('toggle state changes when action checked changes', function () {
123
const container = document.createElement('div');
124
const provider = createToggleActionViewItemProvider(unthemedToggleStyles);
125
const actionbar = store.add(new ActionBar(container, {
126
actionViewItemProvider: provider
127
}));
128
129
const toggleAction = store.add(new Action('toggle', 'Toggle'));
130
toggleAction.checked = false;
131
132
actionbar.push(toggleAction);
133
134
// Verify the toggle view item was created
135
const toggleViewItem = actionbar.viewItems[0] as ToggleActionViewItem;
136
assert(toggleViewItem instanceof ToggleActionViewItem, 'Toggle view item should exist');
137
138
// Change the action's checked state
139
toggleAction.checked = true;
140
// The view item should reflect the updated checked state
141
assert.strictEqual(toggleAction.checked, true, 'Toggle action should update checked state');
142
});
143
144
test('quick input button with toggle property creates action with checked state', async function () {
145
const { quickInputButtonToAction } = await import('../../../platform/quickinput/browser/quickInputUtils.js');
146
147
// Create a button with toggle property
148
const toggleButton = {
149
iconClass: 'test-icon',
150
tooltip: 'Toggle Button',
151
toggle: { checked: true }
152
};
153
154
const action = quickInputButtonToAction(toggleButton, 'test-id', () => { });
155
156
// Verify the action has checked property set
157
assert.strictEqual(action.checked, true, 'Action should have checked property set to true');
158
159
// Create a button without toggle property
160
const regularButton = {
161
iconClass: 'test-icon',
162
tooltip: 'Regular Button'
163
};
164
165
const regularAction = quickInputButtonToAction(regularButton, 'test-id-2', () => { });
166
167
// Verify the action doesn't have checked property
168
assert.strictEqual(regularAction.checked, undefined, 'Regular action should not have checked property');
169
});
170
});
171
});
172
173