1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
|
/*!
* VisualEditor UserInterface FragmentInspector tests.
*
* @copyright See AUTHORS.txt
*/
QUnit.module( 've.ui.FragmentInspector' );
/* Tests */
ve.test.utils.runFragmentInspectorTests = function ( surface, assert, cases ) {
let promise = Promise.resolve();
surface.getView().showSelectionState = function () {};
cases.forEach( ( caseItem ) => {
promise = promise.then( () => surface.context.inspectors.getWindow( caseItem.name ).then( ( inspector ) => {
const surfaceModel = surface.getModel(),
linearData = ve.copy( surfaceModel.getDocument().getFullData() );
surfaceModel.setLinearSelection( caseItem.range );
const setupData = ve.extendObject( { surface: surface, fragment: surfaceModel.getFragment() }, caseItem.setupData );
return inspector.setup( setupData ).then( () => inspector.ready( setupData ).then( () => {
if ( caseItem.input ) {
caseItem.input.call( inspector );
}
// TODO: Skips ActionProcess
return inspector.teardown( caseItem.actionData || { action: 'done' } ).then( () => {
assert.equalRange( surfaceModel.getSelection().getRange(), caseItem.expectedRange, caseItem.msg + ': range' );
if ( caseItem.expectedData ) {
caseItem.expectedData( linearData );
assert.equalLinearData(
surfaceModel.getDocument().getFullData(),
linearData,
caseItem.msg + ': data'
);
}
if ( caseItem.expectedInsertionAnnotations ) {
assert.deepEqual(
surfaceModel.getInsertionAnnotations().getHashes(),
caseItem.expectedInsertionAnnotations,
caseItem.msg + ': insertion annotations'
);
}
while ( surfaceModel.canUndo() ) {
surfaceModel.undo();
}
// Insertion annotations are not cleared by undo
surfaceModel.setInsertionAnnotations( null );
} );
} ) );
} ) );
} );
return promise;
};
QUnit.test( 'Different selections and inputs', ( assert ) => {
const done = assert.async(),
surface = ve.test.utils.createSurfaceFromHtml( ve.dm.example.singleLine`
<p>Foo <a href="bar">bar</a> baz x</p>
<p><!-- comment --> comment</p>
<p>Fo<a href="bar">o bar</a></p>
` ),
fooHash = 'hd5a13e54366d44db',
barHash = 'h071cb84c069d07a4',
quuxHash = 'hb085ebec56a162a4',
cases = [
{
msg: 'Collapsed selection expands to word',
name: 'link',
range: new ve.Range( 2 ),
expectedRange: new ve.Range( 1, 4 ),
expectedData: function ( data ) {
data.splice(
1, 3,
[ 'F', [ fooHash ] ],
[ 'o', [ fooHash ] ],
[ 'o', [ fooHash ] ]
);
}
},
{
msg: 'Collapsed selection in word (noExpand)',
name: 'link',
range: new ve.Range( 2 ),
setupData: { noExpand: true },
expectedRange: new ve.Range( 2 ),
expectedData: function () {}
},
{
msg: 'Cancel restores original data & selection',
name: 'link',
range: new ve.Range( 2 ),
expectedRange: new ve.Range( 2 ),
expectedData: function () {},
actionData: {}
},
{
msg: 'Collapsed selection inside existing link',
name: 'link',
range: new ve.Range( 6 ),
expectedRange: new ve.Range( 5, 8 ),
expectedData: function () {}
},
{
msg: 'Selection inside existing link',
name: 'link',
range: new ve.Range( 6, 7 ),
expectedRange: new ve.Range( 5, 8 ),
expectedData: function () {}
},
{
msg: 'Selection spanning existing link',
name: 'link',
range: new ve.Range( 6, 10 ),
expectedRange: new ve.Range( 5, 10 ),
expectedData: function ( data ) {
data.splice(
8, 2,
[ ' ', [ barHash ] ],
[ 'b', [ barHash ] ]
);
}
},
{
msg: 'Collapsed selection inside a word that is partially linked',
name: 'link',
range: new ve.Range( 30 ),
expectedRange: new ve.Range( 29, 36 ),
expectedData: function ( data ) {
data.splice(
29, 2,
[ 'F', [ barHash ] ],
[ 'o', [ barHash ] ]
);
}
},
{
msg: 'Selection with whitespace is trimmed',
name: 'link',
range: new ve.Range( 1, 5 ),
expectedRange: new ve.Range( 1, 4 )
},
{
msg: 'Link insertion',
name: 'link',
range: new ve.Range( 13 ),
input: function () {
this.annotationInput.getTextInputWidget().setValue( 'quux' );
},
expectedRange: new ve.Range( 17 ),
expectedData: function ( data ) {
data.splice(
13, 0,
[ 'q', [ quuxHash ] ],
[ 'u', [ quuxHash ] ],
[ 'u', [ quuxHash ] ],
[ 'x', [ quuxHash ] ]
);
}
},
{
msg: 'Link insertion with no input is no-op',
name: 'link',
range: new ve.Range( 13 ),
expectedRange: new ve.Range( 13 ),
expectedData: function () {}
},
{
msg: 'Link modified',
name: 'link',
range: new ve.Range( 5, 8 ),
input: function () {
this.annotationInput.getTextInputWidget().setValue( 'quux' );
},
expectedRange: new ve.Range( 5, 8 ),
expectedData: function ( data ) {
data.splice(
5, 3,
[ 'b', [ quuxHash ] ],
[ 'a', [ quuxHash ] ],
[ 'r', [ quuxHash ] ]
);
}
},
{
msg: 'Link removed (clear input)',
name: 'link',
range: new ve.Range( 5, 8 ),
input: function () {
this.annotationInput.getTextInputWidget().setValue( '' );
},
expectedRange: new ve.Range( 5, 8 ),
expectedData: function ( data ) {
data.splice(
5, 3,
...'bar'
);
}
},
{
msg: 'Clear input & cancel is still a no-op',
name: 'link',
range: new ve.Range( 5, 8 ),
input: function () {
this.annotationInput.getTextInputWidget().setValue( '' );
},
expectedRange: new ve.Range( 5, 8 ),
expectedData: function () {},
actionData: {}
},
{
msg: 'Comment change',
name: 'comment',
range: new ve.Range( 17, 19 ),
input: function () {
this.textWidget.setValue( 'new' );
},
expectedRange: new ve.Range( 17, 19 ),
expectedData: function ( data ) {
data.splice(
17, 2,
{
type: 'comment',
attributes: { text: ' new ' }
},
{ type: '/comment' }
);
}
},
{
msg: 'Comment cancel',
name: 'comment',
range: new ve.Range( 17, 19 ),
input: function () {
this.textWidget.setValue( 'new' );
},
actionData: {},
expectedRange: new ve.Range( 17, 19 ),
expectedData: function () {}
},
{
msg: 'Comment clear (empty input)',
name: 'comment',
range: new ve.Range( 17, 19 ),
input: function () {
this.textWidget.setValue( '' );
},
expectedRange: new ve.Range( 17 ),
expectedData: function ( data ) {
data.splice( 17, 2 );
}
},
{
msg: 'Comment delete (action button)',
name: 'comment',
range: new ve.Range( 17, 19 ),
actionData: { action: 'remove' },
expectedRange: new ve.Range( 17 ),
expectedData: function ( data ) {
data.splice( 17, 2 );
}
},
{
msg: 'Language annotation doesn\'t expand',
name: 'language',
range: new ve.Range( 2, 3 ),
expectedRange: new ve.Range( 2, 3 ),
expectedData: function ( data ) {
data.splice(
2, 1,
[ 'o', [ 'h785e2045ecc398c1' ] ]
);
}
},
{
msg: 'Collapsed language annotation becomes insertion annotation',
name: 'language',
range: new ve.Range( 13 ),
expectedRange: new ve.Range( 13 ),
expectedData: function () {},
expectedInsertionAnnotations: [ 'h785e2045ecc398c1' ]
}
];
ve.test.utils.runFragmentInspectorTests( surface, assert, cases ).finally( () => done() );
} );
|