From ccfd8882b8e61237a44b59e5e953dbfd538527c5 Mon Sep 17 00:00:00 2001 From: Beka Westberg Date: Tue, 1 Mar 2022 19:40:59 +0000 Subject: [PATCH] fix: split user input helpers into new file --- tests/deps.mocha.js | 11 ++-- tests/mocha/gesture_test.js | 3 +- tests/mocha/keydown_test.js | 3 +- tests/mocha/shortcut_registry_test.js | 3 +- tests/mocha/test_helpers/test_helpers.js | 60 +-------------------- tests/mocha/test_helpers/user_input.js | 68 ++++++++++++++++++++++++ tests/mocha/trashcan_test.js | 3 +- tests/mocha/zoom_controls_test.js | 3 +- 8 files changed, 85 insertions(+), 69 deletions(-) create mode 100644 tests/mocha/test_helpers/user_input.js diff --git a/tests/deps.mocha.js b/tests/deps.mocha.js index ea698a84b..710740359 100644 --- a/tests/deps.mocha.js +++ b/tests/deps.mocha.js @@ -28,13 +28,13 @@ goog.addDependency('../../tests/mocha/field_textinput_test.js', ['Blockly.test.f goog.addDependency('../../tests/mocha/field_variable_test.js', ['Blockly.test.fieldVariable'], ['Blockly.test.helpers', 'Blockly.test.helpers.blockDefinitions', 'Blockly.test.helpers.fields'], {'lang': 'es6', 'module': 'goog'}); goog.addDependency('../../tests/mocha/flyout_test.js', ['Blockly.test.flyout'], ['Blockly.test.helpers', 'Blockly.test.helpers.toolbox_definitions'], {'lang': 'es6', 'module': 'goog'}); goog.addDependency('../../tests/mocha/generator_test.js', ['Blockly.test.generator'], ['Blockly.Dart', 'Blockly.JavaScript', 'Blockly.Lua', 'Blockly.PHP', 'Blockly.Python', 'Blockly.test.helpers'], {'lang': 'es6', 'module': 'goog'}); -goog.addDependency('../../tests/mocha/gesture_test.js', ['Blockly.test.gesture'], ['Blockly.test.helpers', 'Blockly.test.helpers.blockDefinitions'], {'lang': 'es6', 'module': 'goog'}); +goog.addDependency('../../tests/mocha/gesture_test.js', ['Blockly.test.gesture'], ['Blockly.test.helpers', 'Blockly.test.helpers.blockDefinitions', 'Blockly.test.helpers.userInput'], {'lang': 'es6', 'module': 'goog'}); goog.addDependency('../../tests/mocha/input_test.js', ['Blockly.test.input'], ['Blockly.test.helpers'], {'lang': 'es6', 'module': 'goog'}); goog.addDependency('../../tests/mocha/insertion_marker_test.js', ['Blockly.test.insertionMarker'], ['Blockly.test.helpers'], {'lang': 'es6', 'module': 'goog'}); goog.addDependency('../../tests/mocha/jso_deserialization_test.js', ['Blockly.test.jsoDeserialization'], ['Blockly.Events.utils', 'Blockly.test.helpers'], {'lang': 'es6', 'module': 'goog'}); goog.addDependency('../../tests/mocha/jso_serialization_test.js', ['Blockly.test.jsoSerialization'], ['Blockly.test.helpers', 'Blockly.test.helpers.blockDefinitions'], {'lang': 'es8', 'module': 'goog'}); goog.addDependency('../../tests/mocha/json_test.js', ['Blockly.test.json'], ['Blockly.test.helpers', 'Blockly.test.helpers.warnings'], {'lang': 'es6', 'module': 'goog'}); -goog.addDependency('../../tests/mocha/keydown_test.js', ['Blockly.test.keydown'], ['Blockly.test.helpers', 'Blockly.test.helpers.blockDefinitions'], {'lang': 'es6', 'module': 'goog'}); +goog.addDependency('../../tests/mocha/keydown_test.js', ['Blockly.test.keydown'], ['Blockly.test.helpers', 'Blockly.test.helpers.blockDefinitions', 'Blockly.test.helpers.userInput'], {'lang': 'es6', 'module': 'goog'}); goog.addDependency('../../tests/mocha/logic_ternary_test.js', ['Blockly.test.logicTernary'], ['Blockly.Events.utils', 'Blockly.test.helpers', 'Blockly.test.helpers.serialization'], {'lang': 'es6', 'module': 'goog'}); goog.addDependency('../../tests/mocha/metrics_test.js', ['Blockly.test.metrics'], ['Blockly.test.helpers'], {'lang': 'es6', 'module': 'goog'}); goog.addDependency('../../tests/mocha/mutator_test.js', ['Blockly.test.mutator'], ['Blockly.test.helpers', 'Blockly.test.helpers.blockDefinitions'], {'lang': 'es6', 'module': 'goog'}); @@ -43,7 +43,7 @@ goog.addDependency('../../tests/mocha/procedures_test.js', ['Blockly.test.proced goog.addDependency('../../tests/mocha/registry_test.js', ['Blockly.test.registry'], ['Blockly.test.helpers', 'Blockly.test.helpers.warnings'], {'lang': 'es6', 'module': 'goog'}); goog.addDependency('../../tests/mocha/run_mocha_tests_in_browser.js', [], [], {'lang': 'es8'}); goog.addDependency('../../tests/mocha/serializer_test.js', ['Blockly.test.serialization'], ['Blockly.test.helpers', 'Blockly.test.helpers.common'], {'lang': 'es6', 'module': 'goog'}); -goog.addDependency('../../tests/mocha/shortcut_registry_test.js', ['Blockly.test.shortcutRegistry'], ['Blockly.test.helpers'], {'lang': 'es6', 'module': 'goog'}); +goog.addDependency('../../tests/mocha/shortcut_registry_test.js', ['Blockly.test.shortcutRegistry'], ['Blockly.test.helpers', 'Blockly.test.helpers.userInput'], {'lang': 'es6', 'module': 'goog'}); goog.addDependency('../../tests/mocha/test_helpers/block_definitions.js', ['Blockly.test.helpers.blockDefinitions'], [], {'lang': 'es6', 'module': 'goog'}); goog.addDependency('../../tests/mocha/test_helpers/code_generation.js', ['Blockly.test.helpers.codeGeneration'], ['Blockly.test.helpers.common'], {'lang': 'es6', 'module': 'goog'}); goog.addDependency('../../tests/mocha/test_helpers/common.js', ['Blockly.test.helpers.common'], [], {'lang': 'es6', 'module': 'goog'}); @@ -52,12 +52,13 @@ goog.addDependency('../../tests/mocha/test_helpers/procedures.js', ['Blockly.tes goog.addDependency('../../tests/mocha/test_helpers/serialization.js', ['Blockly.test.helpers.serialization'], ['Blockly.test.helpers.common'], {'lang': 'es6', 'module': 'goog'}); goog.addDependency('../../tests/mocha/test_helpers/test_helpers.js', ['Blockly.test.helpers'], ['Blockly.Events.utils', 'Blockly.blocks', 'Blockly.test.helpers.common', 'Blockly.utils.KeyCodes'], {'lang': 'es6', 'module': 'goog'}); goog.addDependency('../../tests/mocha/test_helpers/toolbox_definitions.js', ['Blockly.test.helpers.toolbox_definitions'], [], {'lang': 'es6', 'module': 'goog'}); +goog.addDependency('../../tests/mocha/test_helpers/user_input.js', ['Blockly.test.helpers.userInput'], ['Blockly.utils.KeyCodes'], {'lang': 'es6', 'module': 'goog'}); goog.addDependency('../../tests/mocha/test_helpers/warnings.js', ['Blockly.test.helpers.warnings'], ['Blockly.Events.utils', 'Blockly.blocks', 'Blockly.test.helpers.common', 'Blockly.utils.KeyCodes'], {'lang': 'es6', 'module': 'goog'}); goog.addDependency('../../tests/mocha/test_helpers/workspace.js', ['Blockly.test.helpers.workspace'], ['Blockly.Events.utils', 'Blockly.test.helpers', 'Blockly.test.helpers.warnings'], {'lang': 'es6', 'module': 'goog'}); goog.addDependency('../../tests/mocha/theme_test.js', ['Blockly.test.theme'], ['Blockly.Events.utils', 'Blockly.test.helpers'], {'lang': 'es6', 'module': 'goog'}); goog.addDependency('../../tests/mocha/toolbox_test.js', ['Blockly.test.toolbox'], ['Blockly.test.helpers', 'Blockly.test.helpers.blockDefinitions', 'Blockly.test.helpers.toolbox_definitions'], {'lang': 'es6', 'module': 'goog'}); goog.addDependency('../../tests/mocha/tooltip_test.js', ['Blockly.test.tooltip'], ['Blockly.test.helpers'], {'lang': 'es6', 'module': 'goog'}); -goog.addDependency('../../tests/mocha/trashcan_test.js', ['Blockly.test.trashcan'], ['Blockly.Events.utils', 'Blockly.test.helpers', 'Blockly.test.helpers.blockDefinitions'], {'lang': 'es6', 'module': 'goog'}); +goog.addDependency('../../tests/mocha/trashcan_test.js', ['Blockly.test.trashcan'], ['Blockly.Events.utils', 'Blockly.test.helpers', 'Blockly.test.helpers.blockDefinitions', 'Blockly.test.helpers.userInput'], {'lang': 'es6', 'module': 'goog'}); goog.addDependency('../../tests/mocha/utils_test.js', ['Blockly.test.utils'], ['Blockly.test.helpers'], {'lang': 'es6', 'module': 'goog'}); goog.addDependency('../../tests/mocha/variable_map_test.js', ['Blockly.test.variableMap'], ['Blockly.test.helpers'], {'lang': 'es6', 'module': 'goog'}); goog.addDependency('../../tests/mocha/variable_model_test.js', ['Blockly.test.variableModel'], ['Blockly.test.helpers'], {'lang': 'es6', 'module': 'goog'}); @@ -67,4 +68,4 @@ goog.addDependency('../../tests/mocha/workspace_comment_test.js', ['Blockly.test goog.addDependency('../../tests/mocha/workspace_svg_test.js', ['Blockly.test.workspaceSvg'], ['Blockly.test.helpers', 'Blockly.test.helpers.blockDefinitions', 'Blockly.test.helpers.workspace'], {'lang': 'es6', 'module': 'goog'}); goog.addDependency('../../tests/mocha/workspace_test.js', ['Blockly.test.workspace'], ['Blockly.test.helpers', 'Blockly.test.helpers.workspace'], {'lang': 'es6', 'module': 'goog'}); goog.addDependency('../../tests/mocha/xml_test.js', ['Blockly.test.xml'], ['Blockly.test.helpers'], {'lang': 'es6', 'module': 'goog'}); -goog.addDependency('../../tests/mocha/zoom_controls_test.js', ['Blockly.test.zoomControls'], ['Blockly.Events.utils', 'Blockly.test.helpers'], {'lang': 'es6', 'module': 'goog'}); +goog.addDependency('../../tests/mocha/zoom_controls_test.js', ['Blockly.test.zoomControls'], ['Blockly.Events.utils', 'Blockly.test.helpers', 'Blockly.test.helpers.userInput'], {'lang': 'es6', 'module': 'goog'}); diff --git a/tests/mocha/gesture_test.js b/tests/mocha/gesture_test.js index 56cf00c8b..12a36cfe6 100644 --- a/tests/mocha/gesture_test.js +++ b/tests/mocha/gesture_test.js @@ -6,8 +6,9 @@ goog.module('Blockly.test.gesture'); -const {assertEventFired, assertEventNotFired, dispatchPointerEvent, sharedTestSetup, sharedTestTeardown} = goog.require('Blockly.test.helpers'); +const {assertEventFired, assertEventNotFired, sharedTestSetup, sharedTestTeardown} = goog.require('Blockly.test.helpers'); const {defineBasicBlockWithField} = goog.require('Blockly.test.helpers.blockDefinitions'); +const {dispatchPointerEvent} = goog.require('Blockly.test.helpers.userInput'); suite('Gesture', function() { diff --git a/tests/mocha/keydown_test.js b/tests/mocha/keydown_test.js index b37a1b43d..142fa9939 100644 --- a/tests/mocha/keydown_test.js +++ b/tests/mocha/keydown_test.js @@ -6,8 +6,9 @@ goog.module('Blockly.test.keydown'); -const {createKeyDownEvent, sharedTestSetup, sharedTestTeardown} = goog.require('Blockly.test.helpers'); +const {createKeyDownEvent} = goog.require('Blockly.test.helpers.userInput'); const {defineStackBlock} = goog.require('Blockly.test.helpers.blockDefinitions'); +const {sharedTestSetup, sharedTestTeardown} = goog.require('Blockly.test.helpers'); suite('Key Down', function() { diff --git a/tests/mocha/shortcut_registry_test.js b/tests/mocha/shortcut_registry_test.js index dc8440f6a..ca79d002d 100644 --- a/tests/mocha/shortcut_registry_test.js +++ b/tests/mocha/shortcut_registry_test.js @@ -6,7 +6,8 @@ goog.module('Blockly.test.shortcutRegistry'); -const {createKeyDownEvent, sharedTestSetup, sharedTestTeardown} = goog.require('Blockly.test.helpers'); +const {createKeyDownEvent} = goog.require('Blockly.test.helpers.userInput'); +const {sharedTestSetup, sharedTestTeardown} = goog.require('Blockly.test.helpers'); suite('Keyboard Shortcut Registry Test', function() { diff --git a/tests/mocha/test_helpers/test_helpers.js b/tests/mocha/test_helpers/test_helpers.js index 494b080ff..c9b3c4a3b 100644 --- a/tests/mocha/test_helpers/test_helpers.js +++ b/tests/mocha/test_helpers/test_helpers.js @@ -419,62 +419,4 @@ function assertNthCallEventArgEquals(spy, n, instanceType, expectedProperties, const eventArg = nthCall.firstArg; assertXmlProperties_(eventArg, xmlProperties); } -exports.assertNthCallEventArgEquals = assertNthCallEventArgEquals; - -/** - * Triggers pointer event on target. - * @param {!EventTarget} target The object receiving the event. - * @param {string} type The type of mouse event (eg: mousedown, mouseup, - * click). - * @param {Object=} properties Properties to pass into event - * constructor. - */ -function dispatchPointerEvent(target, type, properties) { - const eventInitDict = { - cancelable: true, - bubbles: true, - isPrimary: true, - pressure: 0.5, - clientX: 10, - clientY: 10, - }; - if (properties) { - Object.assign(eventInitDict, properties); - } - const event = new PointerEvent(type, eventInitDict); - target.dispatchEvent(event); -} -exports.dispatchPointerEvent = dispatchPointerEvent; - -/** - * Creates a key down event used for testing. - * @param {number} keyCode The keycode for the event. Use Blockly.utils.KeyCodes enum. - * @param {!Array=} modifiers A list of modifiers. Use Blockly.utils.KeyCodes enum. - * @return {!KeyboardEvent} The mocked keydown event. - */ -function createKeyDownEvent(keyCode, modifiers) { - const event = { - keyCode: keyCode, - }; - if (modifiers && modifiers.length > 0) { - event.altKey = modifiers.indexOf(KeyCodes.ALT) > -1; - event.ctrlKey = modifiers.indexOf(KeyCodes.CTRL) > -1; - event.metaKey = modifiers.indexOf(KeyCodes.META) > -1; - event.shiftKey = modifiers.indexOf(KeyCodes.SHIFT) > -1; - } - return new KeyboardEvent('keydown', event); -} -exports.createKeyDownEvent = createKeyDownEvent; - -/** - * Simulates mouse click by triggering relevant mouse events. - * @param {!EventTarget} target The object receiving the event. - * @param {Object=} properties Properties to pass into event - * constructor. - */ -function simulateClick(target, properties) { - dispatchPointerEvent(target, 'pointerdown', properties); - dispatchPointerEvent(target, 'pointerup', properties); - dispatchPointerEvent(target, 'click', properties); -} -exports.simulateClick = simulateClick; +exports.assertNthCallEventArgEquals = assertNthCallEventArgEquals; \ No newline at end of file diff --git a/tests/mocha/test_helpers/user_input.js b/tests/mocha/test_helpers/user_input.js new file mode 100644 index 000000000..9482a7909 --- /dev/null +++ b/tests/mocha/test_helpers/user_input.js @@ -0,0 +1,68 @@ +/** + * @license + * Copyright 2019 Google LLC + * SPDX-License-Identifier: Apache-2.0 + */ + +goog.module('Blockly.test.helpers.userInput'); + +const {KeyCodes} = goog.require('Blockly.utils.KeyCodes'); + + +/** + * Triggers pointer event on target. + * @param {!EventTarget} target The object receiving the event. + * @param {string} type The type of mouse event (eg: mousedown, mouseup, + * click). + * @param {Object=} properties Properties to pass into event + * constructor. + */ +function dispatchPointerEvent(target, type, properties) { + const eventInitDict = { + cancelable: true, + bubbles: true, + isPrimary: true, + pressure: 0.5, + clientX: 10, + clientY: 10, + }; + if (properties) { + Object.assign(eventInitDict, properties); + } + const event = new PointerEvent(type, eventInitDict); + target.dispatchEvent(event); +} +exports.dispatchPointerEvent = dispatchPointerEvent; + +/** + * Creates a key down event used for testing. + * @param {number} keyCode The keycode for the event. Use Blockly.utils.KeyCodes enum. + * @param {!Array=} modifiers A list of modifiers. Use Blockly.utils.KeyCodes enum. + * @return {!KeyboardEvent} The mocked keydown event. + */ +function createKeyDownEvent(keyCode, modifiers) { + const event = { + keyCode: keyCode, + }; + if (modifiers && modifiers.length > 0) { + event.altKey = modifiers.indexOf(KeyCodes.ALT) > -1; + event.ctrlKey = modifiers.indexOf(KeyCodes.CTRL) > -1; + event.metaKey = modifiers.indexOf(KeyCodes.META) > -1; + event.shiftKey = modifiers.indexOf(KeyCodes.SHIFT) > -1; + } + return new KeyboardEvent('keydown', event); +} +exports.createKeyDownEvent = createKeyDownEvent; + +/** + * Simulates mouse click by triggering relevant mouse events. + * @param {!EventTarget} target The object receiving the event. + * @param {Object=} properties Properties to pass into event + * constructor. + */ +function simulateClick(target, properties) { + dispatchPointerEvent(target, 'pointerdown', properties); + dispatchPointerEvent(target, 'pointerup', properties); + dispatchPointerEvent(target, 'click', properties); +} +exports.simulateClick = simulateClick; diff --git a/tests/mocha/trashcan_test.js b/tests/mocha/trashcan_test.js index 6ea1dbe38..e5ac93f10 100644 --- a/tests/mocha/trashcan_test.js +++ b/tests/mocha/trashcan_test.js @@ -6,9 +6,10 @@ goog.module('Blockly.test.trashcan'); -const {assertEventFired, assertEventNotFired, sharedTestSetup, sharedTestTeardown, simulateClick} = goog.require('Blockly.test.helpers'); +const {assertEventFired, assertEventNotFired, sharedTestSetup, sharedTestTeardown} = goog.require('Blockly.test.helpers'); const {defineBasicBlockWithField, defineMutatorBlocks, defineRowBlock, defineStackBlock, defineStatementBlock} = goog.require('Blockly.test.helpers.blockDefinitions'); const eventUtils = goog.require('Blockly.Events.utils'); +const {simulateClick} = goog.require('Blockly.test.helpers.userInput'); suite("Trashcan", function() { diff --git a/tests/mocha/zoom_controls_test.js b/tests/mocha/zoom_controls_test.js index 3d9d1f2d4..45cf6ac72 100644 --- a/tests/mocha/zoom_controls_test.js +++ b/tests/mocha/zoom_controls_test.js @@ -6,8 +6,9 @@ goog.module('Blockly.test.zoomControls'); +const {assertEventFired, assertEventNotFired, sharedTestSetup, sharedTestTeardown} = goog.require('Blockly.test.helpers'); const eventUtils = goog.require('Blockly.Events.utils'); -const {assertEventFired, assertEventNotFired, sharedTestSetup, sharedTestTeardown, simulateClick} = goog.require('Blockly.test.helpers'); +const {simulateClick} = goog.require('Blockly.test.helpers.userInput'); suite("Zoom Controls", function() {