2012-07-19 00:11:26 +00:00
|
|
|
/**
|
|
|
|
* VisualEditor data model Surface tests.
|
2012-07-19 21:25:16 +00:00
|
|
|
*
|
2012-07-19 00:11:26 +00:00
|
|
|
* @copyright 2011-2012 VisualEditor Team and others; see AUTHORS.txt
|
|
|
|
* @license The MIT License (MIT); see LICENSE.txt
|
|
|
|
*/
|
|
|
|
|
2012-05-04 22:29:47 +00:00
|
|
|
module( 've.dm.Surface' );
|
|
|
|
|
2012-06-04 22:02:56 +00:00
|
|
|
ve.dm.SurfaceStub = function( data ) {
|
2012-05-04 22:29:47 +00:00
|
|
|
// Inheritance
|
2012-06-04 22:02:56 +00:00
|
|
|
|
|
|
|
if ( data !== undefined ) {
|
|
|
|
this.dm = new ve.dm.Document ( data );
|
|
|
|
} else {
|
|
|
|
this.dm = new ve.dm.Document ( [{ 'type': 'paragraph' }, 'h', 'i', { 'type': '/paragraph' }] );
|
|
|
|
}
|
2012-05-04 22:29:47 +00:00
|
|
|
ve.dm.Surface.call( this, this.dm );
|
|
|
|
};
|
|
|
|
|
|
|
|
// Inheritance
|
|
|
|
|
|
|
|
ve.extendClass( ve.dm.SurfaceStub, ve.dm.Surface );
|
|
|
|
|
|
|
|
// Tests
|
|
|
|
|
2012-07-10 19:46:08 +00:00
|
|
|
test( 'getDocument', 1, function( assert ) {
|
2012-05-04 22:29:47 +00:00
|
|
|
var surface = new ve.dm.SurfaceStub();
|
2012-07-10 19:46:08 +00:00
|
|
|
assert.strictEqual( surface.getDocument(), surface.documentModel );
|
2012-05-07 18:58:02 +00:00
|
|
|
} );
|
2012-05-04 22:29:47 +00:00
|
|
|
|
2012-07-10 19:46:08 +00:00
|
|
|
test( 'getSelection', 1, function( assert ) {
|
2012-05-04 22:29:47 +00:00
|
|
|
var surface = new ve.dm.SurfaceStub();
|
2012-07-10 19:46:08 +00:00
|
|
|
assert.strictEqual( surface.getSelection(), surface.selection );
|
2012-05-07 18:58:02 +00:00
|
|
|
} );
|
2012-05-04 22:29:47 +00:00
|
|
|
|
2012-07-10 19:46:08 +00:00
|
|
|
test( 'change', 3, function( assert ) {
|
2012-06-19 22:56:25 +00:00
|
|
|
var surface = new ve.dm.SurfaceStub(),
|
|
|
|
tx = new ve.dm.Transaction(),
|
|
|
|
events = { 'transact': 0, 'select': 0, 'change': 0 };
|
2012-05-07 18:58:02 +00:00
|
|
|
surface.on( 'transact', function() {
|
2012-06-19 22:56:25 +00:00
|
|
|
events.transact++;
|
|
|
|
} );
|
|
|
|
surface.on( 'select', function() {
|
|
|
|
events.select++;
|
2012-05-07 18:58:02 +00:00
|
|
|
} );
|
2012-06-19 05:05:05 +00:00
|
|
|
surface.on( 'change', function() {
|
2012-06-19 22:56:25 +00:00
|
|
|
events.change++;
|
2012-06-19 05:05:05 +00:00
|
|
|
} );
|
|
|
|
surface.change( tx );
|
2012-07-10 19:46:08 +00:00
|
|
|
assert.deepEqual( events, { 'transact': 1, 'select': 0, 'change': 1 } );
|
2012-06-19 22:56:25 +00:00
|
|
|
surface.change( null, new ve.Range( 1, 1 ) );
|
2012-07-10 19:46:08 +00:00
|
|
|
assert.deepEqual( events, { 'transact': 1, 'select': 1, 'change': 2 } );
|
2012-06-19 22:56:25 +00:00
|
|
|
surface.change( tx, new ve.Range( 2, 2 ) );
|
2012-07-10 19:46:08 +00:00
|
|
|
assert.deepEqual( events, { 'transact': 2, 'select': 2, 'change': 3 } );
|
2012-06-04 22:02:56 +00:00
|
|
|
} );
|
|
|
|
|
2012-07-10 19:46:08 +00:00
|
|
|
test( 'annotate', 1, function( assert ) {
|
2012-06-04 22:02:56 +00:00
|
|
|
var surface,
|
|
|
|
cases = [
|
|
|
|
{
|
|
|
|
'msg': 'Set Bold',
|
|
|
|
'data': [
|
|
|
|
'b', 'o', 'l', 'd'
|
|
|
|
],
|
|
|
|
'expected':
|
|
|
|
[
|
|
|
|
[
|
|
|
|
"b",
|
|
|
|
{
|
|
|
|
"{\"type\":\"textStyle/bold\"}": {
|
|
|
|
"type": "textStyle/bold"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
],
|
|
|
|
[
|
|
|
|
"o",
|
|
|
|
{
|
|
|
|
"{\"type\":\"textStyle/bold\"}": {
|
|
|
|
"type": "textStyle/bold"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
],
|
|
|
|
[
|
|
|
|
"l",
|
|
|
|
{
|
|
|
|
"{\"type\":\"textStyle/bold\"}": {
|
|
|
|
"type": "textStyle/bold"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
],
|
|
|
|
[
|
|
|
|
"d",
|
|
|
|
{
|
|
|
|
"{\"type\":\"textStyle/bold\"}": {
|
|
|
|
"type": "textStyle/bold"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
],
|
|
|
|
'annotate': {
|
|
|
|
'method': 'set',
|
|
|
|
'annotation': { 'type': 'textStyle/bold' }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
];
|
|
|
|
|
|
|
|
expect( cases.length );
|
|
|
|
for ( var i = 0; i < cases.length; i++ ) {
|
|
|
|
surface = new ve.dm.SurfaceStub( cases[i].data );
|
2012-06-19 22:56:25 +00:00
|
|
|
surface.change( null, new ve.Range( 0, surface.getDocument().getData().length ) );
|
2012-07-19 03:40:49 +00:00
|
|
|
surface.annotate( cases[i].annotate.method, cases[i].annotate.annotation );
|
2012-07-10 19:46:08 +00:00
|
|
|
assert.deepEqual( surface.getDocument().getData(), cases[i].expected, cases[i].msg );
|
2012-06-04 22:02:56 +00:00
|
|
|
}
|
2012-06-19 05:05:05 +00:00
|
|
|
} );
|