Divec has uploaded a new change for review. https://gerrit.wikimedia.org/r/324028
Change subject: Hijack EventSequencer timeouts in unit testing ...................................................................... Hijack EventSequencer timeouts in unit testing Change-Id: I9839edf578ef7d9693500dbb36cf76810e1e1555 --- M tests/ce/ve.ce.Surface.test.js M tests/ce/ve.ce.TestRunner.js M tests/ve.test.utils.js 3 files changed, 45 insertions(+), 30 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/VisualEditor/VisualEditor refs/changes/28/324028/1 diff --git a/tests/ce/ve.ce.Surface.test.js b/tests/ce/ve.ce.Surface.test.js index 0743891..0ce15e8 100644 --- a/tests/ce/ve.ce.Surface.test.js +++ b/tests/ce/ve.ce.Surface.test.js @@ -16,6 +16,8 @@ model = view.getModel(), data = ve.copy( model.getDocument().getFullData() ); + ve.test.utils.hijackEventSequencerTimeouts( view.eventSequencer ); + model.setSelection( ve.test.utils.selectionFromRangeOrSelection( model.getDocument(), rangeOrSelection ) ); @@ -34,14 +36,12 @@ // TODO: Could probably switch to using this for every test, but it // would need the faked testing surface to be improved. view.eventSequencer.onEvent( 'keydown', $.Event( 'keydown', e ) ); + view.eventSequencer.onEvent( 'keypress', $.Event( 'keypress', e ) ); if ( forceSelection ) { view.showSelectionState( view.getSelectionState( forceSelection ) ); } - view.eventSequencer.runPendingCalls( 'keydown' ); - view.eventSequencer.onEvent( 'keypress', $.Event( 'keypress', e ) ); - view.eventSequencer.runPendingCalls( 'keypress' ); view.eventSequencer.onEvent( 'keyup', $.Event( 'keyup', e ) ); - view.eventSequencer.runPendingCalls( 'keyup' ); + view.eventSequencer.endLoop(); } else { if ( forceSelection ) { view.showSelectionState( view.getSelectionState( forceSelection ) ); @@ -60,7 +60,6 @@ { type: 'linear', range: expectedRangeOrSelection } : expectedRangeOrSelection ); - assert.equalHash( model.getSelection(), expectedSelection, msg + ': selection' ); view.destroy(); }; diff --git a/tests/ce/ve.ce.TestRunner.js b/tests/ce/ve.ce.TestRunner.js index 6308d6f..534d3f6 100644 --- a/tests/ce/ve.ce.TestRunner.js +++ b/tests/ce/ve.ce.TestRunner.js @@ -118,13 +118,10 @@ * @param {ve.ce.Surface} surface The UI Surface */ ve.ce.TestRunner = function VeCeTestRunner( surface ) { - var testRunner, - callId = 0; this.view = surface; this.model = surface.getModel(); this.doc = surface.getElementDocument(); this.nativeSelection = surface.nativeSelection; - this.postponedCalls = {}; // TODO: The code assumes that the document consists of exactly one paragraph this.lastText = this.getParagraph().textContent; @@ -133,14 +130,7 @@ surface.surfaceObserver.pollInterval = null; // Take control of eventSequencer 'setTimeouts' - testRunner = this; - this.view.eventSequencer.postpone = function ( f ) { - testRunner.postponedCalls[ ++callId ] = f; - return callId; - }; - this.view.eventSequencer.cancelPostponed = function ( callId ) { - delete testRunner.postponedCalls[ callId ]; - }; + ve.test.utils.hijackEventSequencerTimeouts( this.view.eventSequencer ); }; /* Methods */ @@ -166,22 +156,10 @@ /** * Run any pending postponed calls * - * Exceptions thrown may leave this.postponedCalls in an inconsistent state + * Exceptions thrown will leave postponed calls in an inconsistent state */ ve.ce.TestRunner.prototype.endLoop = function () { - var callId, postponedCalls, - check = true; - - // postponed calls may add more postponed calls - while ( check ) { - postponedCalls = this.postponedCalls; - this.postponedCalls = {}; - check = false; - for ( callId in postponedCalls ) { - check = true; - postponedCalls[ callId ](); - } - } + this.view.eventSequencer.endLoop(); }; /** diff --git a/tests/ve.test.utils.js b/tests/ve.test.utils.js index 0a5006c..e53993f 100644 --- a/tests/ve.test.utils.js +++ b/tests/ve.test.utils.js @@ -459,4 +459,42 @@ add( rootNode ); return html.join( '' ); }; + + /** + * Take control of EventSequencer timeouts + * + * Modifies an EventSequencer object in-place to allow setTimeout behaviour to be + * simulated by test code. Replaces `postpone` and `cancelPostponed` with methods that + * queue/unqueue to an array, and adds an `endLoop` method to unqueue and run every + * queued call. + * + * @param {ve.EventSequencer} eventSequencer The EventSequencer to hijack + */ + ve.test.utils.hijackEventSequencerTimeouts = function ( eventSequencer ) { + eventSequencer.postponedCalls = []; + + eventSequencer.postpone = function ( f ) { + this.postponedCalls.push( f ); + return this.postponedCalls.length - 1; + }; + + eventSequencer.cancelPostponed = function ( callId ) { + this.postponedCalls[ callId ] = null; + }; + + eventSequencer.endLoop = function () { + var i, f; + // Run every postponed call in order of postponement. Do not cache + // list length, because postponed calls may add more postponed calls + for ( i = 0; i < this.postponedCalls.length; i++ ) { + f = this.postponedCalls[ i ]; + if ( f ) { + // Exceptions thrown here will leave the postponed calls + // list in an inconsistent state + f(); + } + } + this.postponedCalls.length = 0; + }; + }; }() ); -- To view, visit https://gerrit.wikimedia.org/r/324028 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I9839edf578ef7d9693500dbb36cf76810e1e1555 Gerrit-PatchSet: 1 Gerrit-Project: VisualEditor/VisualEditor Gerrit-Branch: master Gerrit-Owner: Divec <da...@troi.org> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits