Path: blob/main/src/vs/base/test/browser/actionbar.test.ts
5241 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import assert from 'assert';6import { ActionBar, prepareActions } from '../../browser/ui/actionbar/actionbar.js';7import { Action, Separator } from '../../common/actions.js';8import { ensureNoDisposablesAreLeakedInTestSuite } from '../common/utils.js';9import { createToggleActionViewItemProvider, ToggleActionViewItem, unthemedToggleStyles } from '../../browser/ui/toggle/toggle.js';10import { ActionViewItem } from '../../browser/ui/actionbar/actionViewItems.js';1112suite('Actionbar', () => {1314const store = ensureNoDisposablesAreLeakedInTestSuite();1516test('prepareActions()', function () {17const a1 = new Separator();18const a2 = new Separator();19const a3 = store.add(new Action('a3'));20const a4 = new Separator();21const a5 = new Separator();22const a6 = store.add(new Action('a6'));23const a7 = new Separator();2425const actions = prepareActions([a1, a2, a3, a4, a5, a6, a7]);26assert.strictEqual(actions.length, 3); // duplicate separators get removed27assert(actions[0] === a3);28assert(actions[1] === a5);29assert(actions[2] === a6);30});3132test('hasAction()', function () {33const container = document.createElement('div');34const actionbar = store.add(new ActionBar(container));3536const a1 = store.add(new Action('a1'));37const a2 = store.add(new Action('a2'));3839actionbar.push(a1);40assert.strictEqual(actionbar.hasAction(a1), true);41assert.strictEqual(actionbar.hasAction(a2), false);4243actionbar.pull(0);44assert.strictEqual(actionbar.hasAction(a1), false);4546actionbar.push(a1, { index: 1 });47actionbar.push(a2, { index: 0 });48assert.strictEqual(actionbar.hasAction(a1), true);49assert.strictEqual(actionbar.hasAction(a2), true);5051actionbar.pull(0);52assert.strictEqual(actionbar.hasAction(a1), true);53assert.strictEqual(actionbar.hasAction(a2), false);5455actionbar.pull(0);56assert.strictEqual(actionbar.hasAction(a1), false);57assert.strictEqual(actionbar.hasAction(a2), false);5859actionbar.push(a1);60assert.strictEqual(actionbar.hasAction(a1), true);61actionbar.clear();62assert.strictEqual(actionbar.hasAction(a1), false);63});6465suite('ToggleActionViewItemProvider', () => {6667test('renders toggle for actions with checked state', function () {68const container = document.createElement('div');69const provider = createToggleActionViewItemProvider(unthemedToggleStyles);70const actionbar = store.add(new ActionBar(container, {71actionViewItemProvider: provider72}));7374const toggleAction = store.add(new Action('toggle', 'Toggle', undefined, true, undefined));75toggleAction.checked = true;7677actionbar.push(toggleAction);7879// Verify that the action was rendered as a toggle80assert.strictEqual(actionbar.viewItems.length, 1);81assert(actionbar.viewItems[0] instanceof ToggleActionViewItem, 'Action with checked state should render as ToggleActionViewItem');82});8384test('renders button for actions without checked state', function () {85const container = document.createElement('div');86const provider = createToggleActionViewItemProvider(unthemedToggleStyles);87const actionbar = store.add(new ActionBar(container, {88actionViewItemProvider: provider89}));9091const buttonAction = store.add(new Action('button', 'Button'));9293actionbar.push(buttonAction);9495// Verify that the action was rendered as a regular button (ActionViewItem)96assert.strictEqual(actionbar.viewItems.length, 1);97assert(actionbar.viewItems[0] instanceof ActionViewItem, 'Action without checked state should render as ActionViewItem');98assert(!(actionbar.viewItems[0] instanceof ToggleActionViewItem), 'Action without checked state should not render as ToggleActionViewItem');99});100101test('handles mixed actions (toggles and buttons)', function () {102const container = document.createElement('div');103const provider = createToggleActionViewItemProvider(unthemedToggleStyles);104const actionbar = store.add(new ActionBar(container, {105actionViewItemProvider: provider106}));107108const toggleAction = store.add(new Action('toggle', 'Toggle'));109toggleAction.checked = false;110const buttonAction = store.add(new Action('button', 'Button'));111112actionbar.push([toggleAction, buttonAction]);113114// Verify that we have both types of items115assert.strictEqual(actionbar.viewItems.length, 2);116assert(actionbar.viewItems[0] instanceof ToggleActionViewItem, 'First action should be a toggle');117assert(actionbar.viewItems[1] instanceof ActionViewItem, 'Second action should be a button');118assert(!(actionbar.viewItems[1] instanceof ToggleActionViewItem), 'Second action should not be a toggle');119});120121test('toggle state changes when action checked changes', function () {122const container = document.createElement('div');123const provider = createToggleActionViewItemProvider(unthemedToggleStyles);124const actionbar = store.add(new ActionBar(container, {125actionViewItemProvider: provider126}));127128const toggleAction = store.add(new Action('toggle', 'Toggle'));129toggleAction.checked = false;130131actionbar.push(toggleAction);132133// Verify the toggle view item was created134const toggleViewItem = actionbar.viewItems[0] as ToggleActionViewItem;135assert(toggleViewItem instanceof ToggleActionViewItem, 'Toggle view item should exist');136137// Change the action's checked state138toggleAction.checked = true;139// The view item should reflect the updated checked state140assert.strictEqual(toggleAction.checked, true, 'Toggle action should update checked state');141});142143test('quick input button with toggle property creates action with checked state', async function () {144const { quickInputButtonToAction } = await import('../../../platform/quickinput/browser/quickInputUtils.js');145146// Create a button with toggle property147const toggleButton = {148iconClass: 'test-icon',149tooltip: 'Toggle Button',150toggle: { checked: true }151};152153const action = quickInputButtonToAction(toggleButton, 'test-id', () => { });154155// Verify the action has checked property set156assert.strictEqual(action.checked, true, 'Action should have checked property set to true');157158// Create a button without toggle property159const regularButton = {160iconClass: 'test-icon',161tooltip: 'Regular Button'162};163164const regularAction = quickInputButtonToAction(regularButton, 'test-id-2', () => { });165166// Verify the action doesn't have checked property167assert.strictEqual(regularAction.checked, undefined, 'Regular action should not have checked property');168});169});170});171172173