From 7cbb3f565dc5414d226fe399767cb4375449b32a Mon Sep 17 00:00:00 2001 From: Richard Hansen Date: Thu, 1 Apr 2021 15:52:39 -0400 Subject: [PATCH] tests: Speed up `helper.edit()` and `helper.clearPad()` --- src/static/js/collab_client.js | 5 ++++- src/tests/frontend/helper.js | 18 ++++++++++++++++++ src/tests/frontend/helper/methods.js | 16 +++++++++++----- 3 files changed, 33 insertions(+), 6 deletions(-) diff --git a/src/static/js/collab_client.js b/src/static/js/collab_client.js index 4b6c3ffcf..e507d6b06 100644 --- a/src/static/js/collab_client.js +++ b/src/static/js/collab_client.js @@ -44,6 +44,7 @@ const getCollabClient = (ace2editor, serverVars, initialUserInfo, options, _pad) let channelState = 'CONNECTING'; let lastCommitTime = 0; let initialStartConnectTime = 0; + let commitDelay = 500; const userId = initialUserInfo.userId; // var socket; @@ -102,7 +103,7 @@ const getCollabClient = (ace2editor, serverVars, initialUserInfo, options, _pad) return; } - const earliestCommit = lastCommitTime + 500; + const earliestCommit = lastCommitTime + commitDelay; if (now < earliestCommit) { setTimeout(handleUserChanges, earliestCommit - now); return; @@ -488,6 +489,8 @@ const getCollabClient = (ace2editor, serverVars, initialUserInfo, options, _pad) setChannelState, setStateIdle, setIsPendingRevision, + set commitDelay(ms) { commitDelay = ms; }, + get commitDelay() { return commitDelay; }, }; tellAceAboutHistoricalAuthors(serverVars.historicalAuthorData); diff --git a/src/tests/frontend/helper.js b/src/tests/frontend/helper.js index 05c5f3347..86af1ae58 100644 --- a/src/tests/frontend/helper.js +++ b/src/tests/frontend/helper.js @@ -127,6 +127,8 @@ const helper = {}; $('#iframe-container').append($iframe); await new Promise((resolve) => $iframe.one('load', resolve)); helper.padChrome$ = getFrameJQuery($('#iframe-container iframe')); + helper.padChrome$.padeditor = + helper.padChrome$.window.require('ep_etherpad-lite/static/js/pad_editor').padeditor; if (opts.clearCookies) { helper.clearPadPrefCookie(); } @@ -260,6 +262,22 @@ const helper = {}; selection.addRange(range); }; + // Temporarily reduces minimum time between commits and calls the provided function with a single + // argument: a function that immediately incorporates all pad edits (as opposed to waiting for the + // idle timer to fire). + helper.withFastCommit = async (fn) => { + const incorp = () => helper.padChrome$.padeditor.ace.callWithAce( + (ace) => ace.ace_inCallStackIfNecessary('helper.edit', () => ace.ace_fastIncorp())); + const cc = helper.padChrome$.window.pad.collabClient; + const {commitDelay} = cc; + cc.commitDelay = 0; + try { + return await fn(incorp); + } finally { + cc.commitDelay = commitDelay; + } + }; + const getTextNodeAndOffsetOf = ($targetLine, targetOffsetAtLine) => { const $textNodes = $targetLine.find('*').contents().filter(function () { return this.nodeType === Node.TEXT_NODE; diff --git a/src/tests/frontend/helper/methods.js b/src/tests/frontend/helper/methods.js index 0dd251a0c..253bfbc0d 100644 --- a/src/tests/frontend/helper/methods.js +++ b/src/tests/frontend/helper/methods.js @@ -32,8 +32,11 @@ helper.spyOnSocketIO = () => { helper.edit = async (message, line) => { const editsNum = helper.commits.length; line = line ? line - 1 : 0; - helper.linesDiv()[line].sendkeys(message); - return helper.waitForPromise(() => editsNum + 1 === helper.commits.length); + await helper.withFastCommit(async (incorp) => { + helper.linesDiv()[line].sendkeys(message); + incorp(); + await helper.waitForPromise(() => editsNum + 1 === helper.commits.length); + }); }; /** @@ -216,7 +219,10 @@ helper.clearPad = async () => { await helper.waitForPromise(() => !helper.padInner$.document.getSelection().isCollapsed); const e = new helper.padInner$.Event(helper.evtType); e.keyCode = 8; // delete key - helper.padInner$('#innerdocbody').trigger(e); - await helper.waitForPromise(helper.padIsEmpty); - await helper.waitForPromise(() => helper.commits.length > commitsBefore); + await helper.withFastCommit(async (incorp) => { + helper.padInner$('#innerdocbody').trigger(e); + incorp(); + await helper.waitForPromise(helper.padIsEmpty); + await helper.waitForPromise(() => helper.commits.length > commitsBefore); + }); };