Răsfoiți Sursa

Tests: fixed failed tests and 100% CC.

Szymon Cofalik 9 ani în urmă
părinte
comite
a03c46fa56

+ 1 - 1
packages/ckeditor5-engine/src/model/selection.js

@@ -125,7 +125,7 @@ export default class Selection {
 	}
 
 	/**
-	 * Checks whether, this selection is equal to given selection. Selections are equal if they have same directions,
+	 * Checks whether this selection is equal to given selection. Selections are equal if they have same directions,
 	 * same number of ranges and all ranges from one selection equal to a range from other selection.
 	 *
 	 * @param {engine.model.Selection} otherSelection Selection to compare with.

+ 16 - 0
packages/ckeditor5-engine/tests/conversion/view-selection-to-model-converters.js

@@ -106,4 +106,20 @@ describe( 'convertSelectionChange', () => {
 		expect( modelGetData( model ) ).to.equal( '<paragraph>f[o]o</paragraph><paragraph>b[a]r</paragraph>' );
 		expect( model.selection.isBackward ).to.true;
 	} );
+
+	it( 'should not enqueue changes if selection has not changed', () => {
+		const viewSelection = new ViewSelection();
+		viewSelection.addRange( ViewRange.createFromParentsAndOffsets(
+			viewRoot.getChild( 0 ).getChild( 0 ), 1, viewRoot.getChild( 0 ).getChild( 0 ), 1 ) );
+
+		convertSelection( null, { newSelection: viewSelection } );
+
+		const spy = sinon.spy();
+
+		model.on( 'changesDone', spy );
+
+		convertSelection( null, { newSelection: viewSelection } );
+
+		expect( spy.called ).to.be.false;
+	} );
 } );

+ 1 - 1
packages/ckeditor5-engine/tests/model/selection.js

@@ -671,7 +671,7 @@ describe( 'Selection', () => {
 			selection.addRange( range2 );
 
 			const otherSelection = new Selection();
-			otherSelection.addRange( range1 );
+			otherSelection.addRange( range2 );
 
 			expect( selection.isEqual( otherSelection ) ).to.be.false;
 		} );

+ 58 - 39
packages/ckeditor5-engine/tests/view/observer/selectionobserver.js

@@ -3,7 +3,7 @@
  * For licensing, see LICENSE.md.
  */
 
-/* globals setTimeout, Range, document */
+/* globals setTimeout, Range, document, KeyboardEvent, MouseEvent */
 /* bender-tags: view, browser-only */
 
 import ViewRange from '/ckeditor5/engine/view/range.js';
@@ -162,71 +162,90 @@ describe( 'SelectionObserver', () => {
 	} );
 
 	it( 'should not be treated as an infinite loop if the position is different', ( done ) => {
-		let counter = 30;
-
 		const viewFoo = viewDocument.getRoot().getChild( 0 ).getChild( 0 );
 		viewDocument.selection.addRange( ViewRange.createFromParentsAndOffsets( viewFoo, 0, viewFoo, 0 ) );
 
+		let counter = 0;
+
+		const spy = testUtils.sinon.spy( log, 'warn' );
+
 		listenter.listenTo( viewDocument, 'selectionChange', () => {
-			counter--;
+			counter++;
 
-			if ( counter > 0 ) {
-				setTimeout( () => changeCollapsedDomSelection( counter ) );
-			} else {
-				done();
+			if ( counter < 15 ) {
+				setTimeout( changeCollapsedDomSelection, 100 );
 			}
 		} );
 
-		changeCollapsedDomSelection( counter );
-	} );
+		changeCollapsedDomSelection();
 
-	it( 'should not be treated as an infinite loop if it is less then 3 times', ( done ) => {
-		let counter = 3;
+		setTimeout( () => {
+			expect( spy.called ).to.be.false;
+			done();
+		}, 1500 );
+	} );
 
+	it( 'should not be treated as an infinite loop if selection is changed only few times', ( done ) => {
 		const viewFoo = viewDocument.getRoot().getChild( 0 ).getChild( 0 );
 		viewDocument.selection.addRange( ViewRange.createFromParentsAndOffsets( viewFoo, 0, viewFoo, 0 ) );
 
-		listenter.listenTo( viewDocument, 'selectionChange', () => {
-			counter--;
+		const spy = testUtils.sinon.spy( log, 'warn' );
 
-			if ( counter > 0 ) {
-				setTimeout( () => changeDomSelection() );
-			} else {
-				done();
-			}
-		} );
+		for ( let i = 0; i < 4; i++ ) {
+			changeDomSelection();
+		}
 
-		changeDomSelection();
+		setTimeout( () => {
+			expect( spy.called ).to.be.false;
+			done();
+		}, 1500 );
 	} );
 
-	it( 'should call render after selection change which reset selection if it was not changed', ( done ) => {
-		const viewBar = viewDocument.getRoot().getChild( 1 ).getChild( 0 );
-		viewDocument.selection.addRange( ViewRange.createFromParentsAndOffsets( viewBar, 0, viewBar, 1 ) );
+	const events = {
+		keydown: KeyboardEvent,
+		mousedown: MouseEvent,
+		mousemove: MouseEvent
+	};
 
-		listenter.listenTo( viewDocument, 'selectionChange', () => {
-			setTimeout( () => {
-				const domSelection = document.getSelection();
+	for ( let event in events ) {
+		it( 'should not be treated as an infinite loop if change is triggered by ' + event + ' event', ( done ) => {
+			let counter = 0;
 
-				expect( domSelection.rangeCount ).to.equal( 1 );
+			const viewFoo = viewDocument.getRoot().getChild( 0 ).getChild( 0 );
+			viewDocument.selection.addRange( ViewRange.createFromParentsAndOffsets( viewFoo, 0, viewFoo, 0 ) );
 
-				const domRange = domSelection.getRangeAt( 0 );
-				const domBar = document.getElementById( 'main' ).childNodes[ 1 ].childNodes[ 0 ];
+			const spy = testUtils.sinon.spy( log, 'warn' );
 
-				expect( domRange.startContainer ).to.equal( domBar );
-				expect( domRange.startOffset ).to.equal( 0 );
-				expect( domRange.endContainer ).to.equal( domBar );
-				expect( domRange.endOffset ).to.equal( 1 );
+			listenter.listenTo( viewDocument, 'selectionChange', () => {
+				counter++;
 
-				done();
+				if ( counter < 15 ) {
+					setTimeout( () => {
+						document.dispatchEvent( new events[ event ]( event ) );
+						changeDomSelection();
+					}, 100 );
+				}
 			} );
-		} );
 
-		changeDomSelection();
-	} );
+			setTimeout( () => {
+				expect( spy.called ).to.be.false;
+				done();
+			}, 1000 );
+
+			document.dispatchEvent( new events[ event ]( event ) );
+			changeDomSelection();
+		} );
+	}
 } );
 
-function changeCollapsedDomSelection( pos = 1 ) {
+function changeCollapsedDomSelection() {
 	const domSelection = document.getSelection();
+	const pos = domSelection.anchorOffset + 1;
+
+	if ( pos > 20 ) {
+		return;
+	}
+
 	domSelection.removeAllRanges();
 	const domFoo = document.getElementById( 'main' ).childNodes[ 0 ].childNodes[ 0 ];
 	const domRange = new Range();