mediawiki-extensions-Visual.../modules/ve/test/dm/ve.dm.Surface.test.js
Catrope 5e7c14c868 Manage annotations in ve.AnnotationSet object
Introduced the ve.AnnotationSet class to manage sets of annotations. This
is a generalization of ve.OrderedHashSet, a class that manages a set
using an array and an object keyed by hash.

Converted everything that stores, tracks or passes around annotations to
use ve.AnnotationSet. In particular, this means the linear model now
contains AnnotationSets instead of hash-keyed objects.

This allows us to maintain the order of annotations in the linear model,
and will help fix bugs with annotation ordering and splitting.

Change-Id: I50975b0a95f4cc33017a0b59fdede9ed1eff0124
2012-09-06 14:39:38 -07:00

124 lines
2.7 KiB
JavaScript

/**
* VisualEditor data model Surface tests.
*
* @copyright 2011-2012 VisualEditor Team and others; see AUTHORS.txt
* @license The MIT License (MIT); see LICENSE.txt
*/
QUnit.module( 've.dm.Surface' );
ve.dm.SurfaceStub = function ( data ) {
// Inheritance
if ( data !== undefined ) {
this.dm = new ve.dm.Document ( data );
} else {
this.dm = new ve.dm.Document ( [{ 'type': 'paragraph' }, 'h', 'i', { 'type': '/paragraph' }] );
}
ve.dm.Surface.call( this, this.dm );
};
// Inheritance
ve.extendClass( ve.dm.SurfaceStub, ve.dm.Surface );
// Tests
QUnit.test( 'getDocument', 1, function ( assert ) {
var surface = new ve.dm.SurfaceStub();
assert.strictEqual( surface.getDocument(), surface.documentModel );
} );
QUnit.test( 'getSelection', 1, function ( assert ) {
var surface = new ve.dm.SurfaceStub();
assert.strictEqual( surface.getSelection(), surface.selection );
} );
QUnit.test( 'change', 3, function ( assert ) {
var surface = new ve.dm.SurfaceStub(),
tx = new ve.dm.Transaction(),
events = {
'transact': 0,
'select': 0,
'change': 0
};
surface.on( 'transact', function () {
events.transact++;
} );
surface.on( 'select', function () {
events.select++;
} );
surface.on( 'change', function () {
events.change++;
} );
surface.change( tx );
assert.deepEqual( events, { 'transact': 1, 'select': 0, 'change': 1 } );
surface.change( null, new ve.Range( 1, 1 ) );
assert.deepEqual( events, { 'transact': 1, 'select': 1, 'change': 2 } );
surface.change( tx, new ve.Range( 2, 2 ) );
assert.deepEqual( events, { 'transact': 2, 'select': 2, 'change': 3 } );
} );
QUnit.test( 'annotate', 1, function ( assert ) {
var i,
surface,
cases = [
{
'msg': 'Set Bold',
'data': [
'b', 'o', 'l', 'd'
],
'expected':
[
[
'b',
[
{
'type': 'textStyle/bold'
}
]
],
[
'o',
[
{
'type': 'textStyle/bold'
}
]
],
[
'l',
[
{
'type': 'textStyle/bold'
}
]
],
[
'd',
[
{
'type': 'textStyle/bold'
}
]
]
],
'annotate': {
'method': 'set',
'annotation': { 'type': 'textStyle/bold' }
}
}
];
QUnit.expect( cases.length );
for ( i = 0; i < cases.length; i++ ) {
ve.dm.example.preprocessAnnotations( cases[i].data );
ve.dm.example.preprocessAnnotations( cases[i].expected );
surface = new ve.dm.SurfaceStub( cases[i].data );
surface.change( null, new ve.Range( 0, surface.getDocument().getData().length ) );
surface.annotate( cases[i].annotate.method, cases[i].annotate.annotation );
assert.deepEqual( surface.getDocument().getData(), cases[i].expected, cases[i].msg );
}
} );