2012-07-19 00:11:26 +00:00
|
|
|
/**
|
|
|
|
* VisualEditor data model TransactionProcessor 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-01 02:06:20 +00:00
|
|
|
module( 've.dm.TransactionProcessor' );
|
|
|
|
|
|
|
|
/* Tests */
|
|
|
|
|
2012-05-14 21:31:32 +00:00
|
|
|
test( 'commit/rollback', function() {
|
|
|
|
var cases = {
|
|
|
|
'no operations': {
|
|
|
|
'calls': [],
|
2012-05-15 00:16:18 +00:00
|
|
|
'expected': function( data ) {}
|
2012-05-14 21:31:32 +00:00
|
|
|
},
|
|
|
|
'retaining': {
|
|
|
|
'calls': [['pushRetain', 38]],
|
2012-05-15 00:16:18 +00:00
|
|
|
'expected': function( data ) {}
|
2012-05-14 21:31:32 +00:00
|
|
|
},
|
|
|
|
'annotating content': {
|
|
|
|
'calls': [
|
2012-05-15 00:16:18 +00:00
|
|
|
['pushRetain', 1],
|
2012-05-14 21:31:32 +00:00
|
|
|
['pushStartAnnotating', 'set', { 'type': 'textStyle/bold' }],
|
2012-05-15 00:16:18 +00:00
|
|
|
['pushRetain', 1],
|
|
|
|
['pushStopAnnotating', 'set', { 'type': 'textStyle/bold' }],
|
|
|
|
['pushRetain', 1],
|
2012-05-14 21:31:32 +00:00
|
|
|
['pushStartAnnotating', 'clear', { 'type': 'textStyle/italic' }],
|
2012-05-15 00:16:18 +00:00
|
|
|
['pushStartAnnotating', 'set', { 'type': 'textStyle/bold' }],
|
|
|
|
['pushStartAnnotating', 'set', { 'type': 'textStyle/underline' }],
|
|
|
|
['pushRetain', 1],
|
2012-05-14 21:31:32 +00:00
|
|
|
['pushStopAnnotating', 'clear', { 'type': 'textStyle/italic' }],
|
|
|
|
['pushStopAnnotating', 'set', { 'type': 'textStyle/bold' }],
|
2012-05-15 00:16:18 +00:00
|
|
|
['pushStopAnnotating', 'set', { 'type': 'textStyle/underline' }]
|
2012-05-14 21:31:32 +00:00
|
|
|
],
|
2012-05-15 00:16:18 +00:00
|
|
|
'expected': function( data ) {
|
2012-05-14 21:31:32 +00:00
|
|
|
var b = { '{"type":"textStyle/bold"}': { 'type': 'textStyle/bold' } },
|
2012-05-15 00:16:18 +00:00
|
|
|
u = { '{"type":"textStyle/underline"}': { 'type': 'textStyle/underline' } };
|
2012-05-14 21:31:32 +00:00
|
|
|
data[1] = ['a', b];
|
|
|
|
data[2] = ['b', b];
|
2012-05-15 00:16:18 +00:00
|
|
|
data[3] = ['c', ve.extendObject( {}, b, u )];
|
2012-05-14 21:31:32 +00:00
|
|
|
}
|
|
|
|
},
|
2012-05-15 00:16:18 +00:00
|
|
|
'annotating content and leaf elements': {
|
|
|
|
'calls': [
|
2012-06-08 05:08:49 +00:00
|
|
|
['pushRetain', 38],
|
2012-05-15 00:16:18 +00:00
|
|
|
['pushStartAnnotating', 'set', { 'type': 'textStyle/bold' }],
|
|
|
|
['pushRetain', 2],
|
|
|
|
['pushStopAnnotating', 'set', { 'type': 'textStyle/bold' }]
|
|
|
|
],
|
|
|
|
'expected': function( data ) {
|
|
|
|
var b = { '{"type":"textStyle/bold"}': { 'type': 'textStyle/bold' } };
|
2012-06-08 05:08:49 +00:00
|
|
|
data[38] = ['h', b];
|
|
|
|
data[39].annotations = b;
|
2012-05-15 00:16:18 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
'using an annotation method other than set or clear throws an exception': {
|
|
|
|
'calls': [
|
|
|
|
['pushStartAnnotating', 'invalid-method', { 'type': 'textStyle/bold' }],
|
|
|
|
['pushRetain', 1],
|
|
|
|
['pushStopAnnotating', 'invalid-method', { 'type': 'textStyle/bold' }]
|
|
|
|
],
|
|
|
|
'exception': /^Invalid annotation method/
|
|
|
|
},
|
2012-06-08 05:08:49 +00:00
|
|
|
'annotating branch opening element throws an exception': {
|
2012-05-15 00:16:18 +00:00
|
|
|
'calls': [
|
|
|
|
['pushStartAnnotating', 'set', { 'type': 'textStyle/bold' }],
|
|
|
|
['pushRetain', 1],
|
|
|
|
['pushStopAnnotating', 'set', { 'type': 'textStyle/bold' }]
|
|
|
|
],
|
2012-06-08 05:08:49 +00:00
|
|
|
'exception': /^Invalid transaction, cannot annotate a branch opening element$/
|
|
|
|
},
|
|
|
|
'annotating branch closing element throws an exception': {
|
|
|
|
'calls': [
|
|
|
|
['pushRetain', 4],
|
|
|
|
['pushStartAnnotating', 'set', { 'type': 'textStyle/bold' }],
|
|
|
|
['pushRetain', 1],
|
|
|
|
['pushStopAnnotating', 'set', { 'type': 'textStyle/bold' }]
|
|
|
|
],
|
|
|
|
'exception': /^Invalid transaction, cannot annotate a branch closing element$/
|
2012-05-15 00:16:18 +00:00
|
|
|
},
|
|
|
|
'setting duplicate annotations throws an exception': {
|
|
|
|
'calls': [
|
|
|
|
['pushRetain', 2],
|
|
|
|
['pushStartAnnotating', 'set', { 'type': 'textStyle/bold' }],
|
|
|
|
['pushRetain', 1],
|
|
|
|
['pushStopAnnotating', 'set', { 'type': 'textStyle/bold' }]
|
|
|
|
],
|
|
|
|
'exception': /^Invalid transaction, annotation to be set is already set$/
|
|
|
|
},
|
|
|
|
'removing non-existent annotations throws an exception': {
|
|
|
|
'calls': [
|
|
|
|
['pushRetain', 1],
|
|
|
|
['pushStartAnnotating', 'clear', { 'type': 'textStyle/bold' }],
|
|
|
|
['pushRetain', 1],
|
|
|
|
['pushStopAnnotating', 'clear', { 'type': 'textStyle/bold' }]
|
|
|
|
],
|
|
|
|
'exception': /^Invalid transaction, annotation to be cleared is not set$/
|
|
|
|
},
|
2012-05-14 21:31:32 +00:00
|
|
|
'changing, removing and adding attributes': {
|
|
|
|
'calls': [
|
|
|
|
['pushReplaceElementAttribute', 'level', 1, 2],
|
2012-06-08 05:08:49 +00:00
|
|
|
['pushRetain', 12],
|
2012-05-14 21:31:32 +00:00
|
|
|
['pushReplaceElementAttribute', 'style', 'bullet', 'number'],
|
|
|
|
['pushReplaceElementAttribute', 'test', undefined, 'abcd'],
|
2012-06-08 05:08:49 +00:00
|
|
|
['pushRetain', 27],
|
2012-05-14 21:31:32 +00:00
|
|
|
['pushReplaceElementAttribute', 'html/src', 'image.png', undefined]
|
|
|
|
],
|
2012-05-15 00:16:18 +00:00
|
|
|
'expected': function( data ) {
|
2012-05-14 21:31:32 +00:00
|
|
|
data[0].attributes.level = 2;
|
2012-06-08 05:08:49 +00:00
|
|
|
data[12].attributes.style = 'number';
|
|
|
|
data[12].attributes.test = 'abcd';
|
|
|
|
delete data[39].attributes['html/src'];
|
2012-05-14 21:31:32 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
'changing attributes on non-element data throws an exception': {
|
|
|
|
'calls': [
|
|
|
|
['pushRetain', 1],
|
|
|
|
['pushReplaceElementAttribute', 'foo', 23, 42]
|
|
|
|
],
|
2012-05-15 00:16:18 +00:00
|
|
|
'exception': /^Invalid element error, can not set attributes on non-element data$/
|
2012-05-14 21:31:32 +00:00
|
|
|
},
|
2012-05-31 13:23:44 +00:00
|
|
|
'inserting text': {
|
2012-05-16 20:33:27 +00:00
|
|
|
'calls': [
|
|
|
|
['pushRetain', 1],
|
|
|
|
['pushReplace', [], ['F', 'O', 'O']]
|
|
|
|
],
|
|
|
|
'expected': function( data ) {
|
|
|
|
data.splice( 1, 0, 'F', 'O', 'O' );
|
|
|
|
}
|
|
|
|
},
|
2012-05-31 13:23:44 +00:00
|
|
|
'removing text': {
|
2012-05-16 20:33:27 +00:00
|
|
|
'calls': [
|
|
|
|
['pushRetain', 1],
|
|
|
|
['pushReplace', ['a'], []]
|
|
|
|
],
|
|
|
|
'expected': function( data ) {
|
|
|
|
data.splice( 1, 1 );
|
|
|
|
}
|
|
|
|
},
|
2012-05-31 13:23:44 +00:00
|
|
|
'replacing text': {
|
2012-05-14 21:31:32 +00:00
|
|
|
'calls': [
|
|
|
|
['pushRetain', 1],
|
|
|
|
['pushReplace', ['a'], ['F', 'O', 'O']]
|
|
|
|
],
|
2012-05-15 00:16:18 +00:00
|
|
|
'expected': function( data ) {
|
2012-05-14 21:31:32 +00:00
|
|
|
data.splice( 1, 1, 'F', 'O', 'O' );
|
|
|
|
}
|
2012-05-15 00:16:18 +00:00
|
|
|
},
|
2012-05-31 13:23:44 +00:00
|
|
|
'inserting mixed content': {
|
|
|
|
'calls': [
|
|
|
|
['pushRetain', 1],
|
|
|
|
['pushReplace', ['a'], ['F', 'O', 'O', {'type':'image'}, {'type':'/image'}, 'B', 'A', 'R']]
|
|
|
|
],
|
|
|
|
'expected': function( data ) {
|
|
|
|
data.splice( 1, 1, 'F', 'O', 'O', {'type':'image'}, {'type':'/image'}, 'B', 'A', 'R' );
|
|
|
|
}
|
|
|
|
},
|
2012-05-15 00:16:18 +00:00
|
|
|
'converting an element': {
|
|
|
|
'calls': [
|
|
|
|
[
|
|
|
|
'pushReplace',
|
|
|
|
[{ 'type': 'heading', 'attributes': { 'level': 1 } }],
|
|
|
|
[{ 'type': 'paragraph' }]
|
|
|
|
],
|
|
|
|
['pushRetain', 3],
|
|
|
|
['pushReplace', [{ 'type': '/heading' }], [{ 'type': '/paragraph' }]]
|
|
|
|
],
|
|
|
|
'expected': function( data ) {
|
|
|
|
data[0].type = 'paragraph';
|
|
|
|
delete data[0].attributes;
|
|
|
|
data[4].type = '/paragraph';
|
|
|
|
}
|
2012-05-15 04:50:33 +00:00
|
|
|
},
|
|
|
|
'splitting an element': {
|
|
|
|
'calls': [
|
|
|
|
['pushRetain', 2],
|
|
|
|
[
|
|
|
|
'pushReplace',
|
|
|
|
[],
|
|
|
|
[{ 'type': '/heading' }, { 'type': 'heading', 'attributes': { 'level': 1 } }]
|
|
|
|
]
|
|
|
|
],
|
|
|
|
'expected': function( data ) {
|
|
|
|
data.splice(
|
|
|
|
2,
|
|
|
|
0,
|
|
|
|
{ 'type': '/heading' },
|
|
|
|
{ 'type': 'heading', 'attributes': { 'level': 1 } }
|
|
|
|
);
|
|
|
|
}
|
2012-05-16 20:33:27 +00:00
|
|
|
},
|
|
|
|
'merging an element': {
|
|
|
|
'calls': [
|
2012-06-08 05:08:49 +00:00
|
|
|
['pushRetain', 57],
|
2012-05-16 20:33:27 +00:00
|
|
|
[
|
|
|
|
'pushReplace',
|
|
|
|
[{ 'type': '/paragraph' }, { 'type': 'paragraph' }],
|
|
|
|
[]
|
|
|
|
]
|
|
|
|
],
|
|
|
|
'expected': function( data ) {
|
2012-06-08 05:08:49 +00:00
|
|
|
data.splice( 57, 2 );
|
2012-05-16 20:33:27 +00:00
|
|
|
}
|
2012-06-06 22:34:55 +00:00
|
|
|
},
|
|
|
|
'stripping elements': {
|
|
|
|
'calls': [
|
|
|
|
['pushRetain', 3],
|
|
|
|
[
|
|
|
|
'pushReplace',
|
|
|
|
[['c', { '{"type":"textStyle/italic"}': { 'type': 'textStyle/italic' } }]],
|
|
|
|
[]
|
|
|
|
],
|
2012-06-08 05:08:49 +00:00
|
|
|
['pushRetain', 6],
|
2012-06-06 22:34:55 +00:00
|
|
|
[
|
|
|
|
'pushReplace',
|
|
|
|
['d'],
|
|
|
|
[]
|
|
|
|
]
|
|
|
|
],
|
|
|
|
'expected': function( data ) {
|
2012-06-08 05:08:49 +00:00
|
|
|
data.splice( 10, 1 );
|
2012-06-06 22:34:55 +00:00
|
|
|
data.splice( 3, 1 );
|
|
|
|
}
|
2012-05-14 21:31:32 +00:00
|
|
|
}
|
|
|
|
};
|
2012-05-15 07:25:27 +00:00
|
|
|
// Generate original document
|
|
|
|
var originalData = ve.dm.example.data,
|
|
|
|
originalDoc = new ve.dm.Document( originalData );
|
2012-05-14 21:31:32 +00:00
|
|
|
// Run tests
|
|
|
|
for ( var msg in cases ) {
|
2012-05-15 07:25:27 +00:00
|
|
|
var testDocument = new ve.dm.Document( ve.copyArray( originalData ) ),
|
2012-05-14 21:31:32 +00:00
|
|
|
tx = new ve.dm.Transaction();
|
|
|
|
for ( var i = 0; i < cases[msg].calls.length; i++ ) {
|
|
|
|
tx[cases[msg].calls[i][0]].apply( tx, cases[msg].calls[i].slice( 1 ) );
|
|
|
|
}
|
2012-05-15 00:16:18 +00:00
|
|
|
if ( 'expected' in cases[msg] ) {
|
2012-05-15 07:25:27 +00:00
|
|
|
// Generate expected document
|
|
|
|
var expectedData = ve.copyArray( originalData );
|
|
|
|
cases[msg].expected( expectedData );
|
|
|
|
var expectedDocument = new ve.dm.Document( expectedData );
|
|
|
|
// Commit
|
|
|
|
ve.dm.TransactionProcessor.commit( testDocument, tx );
|
|
|
|
deepEqual( testDocument.getData(), expectedData, 'commit (data): ' + msg );
|
|
|
|
deepEqual(
|
|
|
|
ve.example.getNodeTreeSummary( testDocument.getDocumentNode() ),
|
|
|
|
ve.example.getNodeTreeSummary( expectedDocument.getDocumentNode() ),
|
2012-05-15 05:36:06 +00:00
|
|
|
'commit (tree): ' + msg
|
|
|
|
);
|
2012-05-15 07:25:27 +00:00
|
|
|
// Rollback
|
|
|
|
ve.dm.TransactionProcessor.rollback( testDocument, tx );
|
|
|
|
deepEqual( testDocument.getData(), ve.dm.example.data, 'rollback (data): ' + msg );
|
|
|
|
deepEqual(
|
|
|
|
ve.example.getNodeTreeSummary( testDocument.getDocumentNode() ),
|
|
|
|
ve.example.getNodeTreeSummary( originalDoc.getDocumentNode() ),
|
2012-05-15 05:36:06 +00:00
|
|
|
'rollback (tree): ' + msg
|
|
|
|
);
|
2012-05-14 21:31:32 +00:00
|
|
|
} else if ( 'exception' in cases[msg] ) {
|
|
|
|
/*jshint loopfunc:true */
|
|
|
|
raises(
|
|
|
|
function() {
|
2012-05-15 07:25:27 +00:00
|
|
|
ve.dm.TransactionProcessor.commit( testDocument, tx );
|
2012-05-14 21:31:32 +00:00
|
|
|
},
|
|
|
|
cases[msg].exception,
|
|
|
|
'commit: ' + msg
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2012-05-04 22:50:50 +00:00
|
|
|
} );
|