|
4 | 4 | * SPDX-License-Identifier: Apache-2.0 |
5 | 5 | */ |
6 | 6 |
|
| 7 | +import * as Blockly from '../../build/src/core/blockly.js'; |
7 | 8 | import {assert} from '../../node_modules/chai/index.js'; |
8 | 9 | import {defineEmptyBlock} from './test_helpers/block_definitions.js'; |
9 | 10 | import {MockIcon, MockSerializableIcon} from './test_helpers/icon_mocks.js'; |
10 | 11 | import { |
11 | 12 | sharedTestSetup, |
12 | 13 | sharedTestTeardown, |
13 | 14 | } from './test_helpers/setup_teardown.js'; |
| 15 | +import {simulateClick} from './test_helpers/user_input.js'; |
| 16 | + |
| 17 | +class TestIcon extends Blockly.icons.Icon { |
| 18 | + showContextMenu(e) { |
| 19 | + const menuItems = [ |
| 20 | + {text: 'Test icon menu item', enabled: true, callback: () => {}}, |
| 21 | + ]; |
| 22 | + Blockly.ContextMenu.show( |
| 23 | + e, |
| 24 | + menuItems, |
| 25 | + false, |
| 26 | + this.getSourceBlock().workspace, |
| 27 | + this.workspaceLocation, |
| 28 | + ); |
| 29 | + } |
| 30 | + |
| 31 | + getType() { |
| 32 | + new Blockly.icons.IconType() < TestIcon > 'test'; |
| 33 | + } |
| 34 | +} |
14 | 35 |
|
15 | 36 | suite('Icon', function () { |
16 | 37 | setup(function () { |
@@ -366,4 +387,45 @@ suite('Icon', function () { |
366 | 387 | ); |
367 | 388 | }); |
368 | 389 | }); |
| 390 | + |
| 391 | + suite('Contextual menus', function () { |
| 392 | + setup(function () { |
| 393 | + this.workspace = Blockly.inject('blocklyDiv', {}); |
| 394 | + Blockly.icons.registry.register( |
| 395 | + new Blockly.icons.IconType('test'), |
| 396 | + TestIcon, |
| 397 | + ); |
| 398 | + |
| 399 | + this.block = this.workspace.newBlock('empty_block'); |
| 400 | + this.block.initSvg(); |
| 401 | + }); |
| 402 | + |
| 403 | + test('are shown when icons are right clicked', function () { |
| 404 | + const icon = new TestIcon(this.block); |
| 405 | + this.block.addIcon(icon); |
| 406 | + simulateClick(icon.getFocusableElement(), {button: 2}); |
| 407 | + |
| 408 | + const menu = document.querySelector('.blocklyContextMenu'); |
| 409 | + assert.isNotNull(menu); |
| 410 | + assert.isTrue(menu.innerText.includes('Test icon menu item')); |
| 411 | + }); |
| 412 | + |
| 413 | + test('default to the contextual menu of the parent block', function () { |
| 414 | + this.block.setCommentText('hello there'); |
| 415 | + const icon = this.block.getIcon(Blockly.icons.IconType.COMMENT); |
| 416 | + simulateClick(icon.getFocusableElement(), {button: 2}); |
| 417 | + |
| 418 | + const expectedItems = |
| 419 | + Blockly.ContextMenuRegistry.registry.getContextMenuOptions({ |
| 420 | + block: this.block, |
| 421 | + }); |
| 422 | + |
| 423 | + assert.isNotEmpty(expectedItems); |
| 424 | + const menu = document.querySelector('.blocklyContextMenu'); |
| 425 | + for (const item of expectedItems) { |
| 426 | + if (!item.text) continue; |
| 427 | + assert.isTrue(menu.innerText.includes(item.text)); |
| 428 | + } |
| 429 | + }); |
| 430 | + }); |
369 | 431 | }); |
0 commit comments