/
undo.js
206 lines (166 loc) · 6.62 KB
/
undo.js
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
/* bender-ckeditor-plugins: embedbase,embed,toolbar,htmlwriter,entities,undo */
/* bender-include: ../widget/_helpers/tools.js, _helpers/tools.js */
/* global widgetTestsTools, embedTools */
'use strict';
bender.editors = {
inline: {
name: 'editor_inline',
creator: 'inline'
}
};
function echoJsonpCallback( urlTemplate, urlParams, callback ) {
callback( {
type: 'rich',
html: '<p>url:' + urlParams.url + '</p>'
} );
}
function assertCommands( editor, expectActiveUndo, expectActiveRedo, msg ) {
msg += ' - ';
assert.areSame( expectActiveUndo ? CKEDITOR.TRISTATE_OFF : CKEDITOR.TRISTATE_DISABLED, editor.getCommand( 'undo' ).state, msg + 'undo' );
assert.areSame( expectActiveRedo ? CKEDITOR.TRISTATE_OFF : CKEDITOR.TRISTATE_DISABLED, editor.getCommand( 'redo' ).state, msg + 'redo' );
}
function setUpEditor( editor, htmlWithSel ) {
editor.focus();
bender.tools.selection.setWithHtml( editor, htmlWithSel );
editor.widgets.checkWidgets();
editor.resetUndo();
}
var obj2Array = widgetTestsTools.obj2Array,
jsonpCallback = echoJsonpCallback;
embedTools.mockJsonp( function() {
jsonpCallback.apply( this, arguments );
} );
bender.test( {
_should: {
ignore: {
'test undo and redo after creation': CKEDITOR.env.webkit && !CKEDITOR.env.chrome,
'test undo and redo after creation and edition': CKEDITOR.env.webkit && !CKEDITOR.env.chrome,
'test undo and redo after edition': CKEDITOR.env.webkit && !CKEDITOR.env.chrome
}
},
spies: [],
listeners: [],
init: function() {
this.editors.inline.dataProcessor.writer.sortAttributes = true;
},
tearDown: function() {
var spy;
while ( spy = this.spies.pop() ) {
spy.restore();
}
var listener;
while ( listener = this.listeners.pop() ) {
listener.removeListener();
}
},
'test undo and redo after creation': function() {
var bot = this.editorBots.inline,
editor = bot.editor;
editor.once( 'dialogShow', function( evt ) {
var dialog = evt.data;
setTimeout( function() {
dialog.setValueOf( 'info', 'url', '//after/creation' );
dialog.getButton( 'ok' ).click();
}, 50 );
} );
editor.once( 'dialogHide', function() {
resume( function() {
assert.areSame( 1, obj2Array( editor.widgets.instances ).length, '1 widget after insert' );
assertCommands( editor, true, false, 'after insert' );
editor.execCommand( 'undo' );
assert.areSame( 0, obj2Array( editor.widgets.instances ).length, '0 widgets after undo' );
assertCommands( editor, false, true, 'after undo' );
assert.areSame( '<p>foo</p>', editor.getData(), 'data after undo' );
editor.execCommand( 'redo' );
assert.areSame( 1, obj2Array( editor.widgets.instances ).length, '1 widget after redo' );
assertCommands( editor, true, false, 'after redo' );
assert.areSame( '<p>foo</p><div data-oembed-url="//after/creation"><p>url:%2F%2Fafter%2Fcreation</p></div><p> </p>',
editor.getData(), 'data after redo' );
} );
} );
setUpEditor( editor, '<p>foo[]</p>' );
wait( function() {
editor.execCommand( 'embed' );
} );
},
'test undo and redo after edition': function() {
var bot = this.editorBots.inline,
editor = bot.editor;
editor.once( 'dialogShow', function( evt ) {
var dialog = evt.data;
setTimeout( function() {
dialog.setValueOf( 'info', 'url', '//after/edition' );
dialog.getButton( 'ok' ).click();
}, 50 );
} );
editor.once( 'dialogHide', function() {
resume( function() {
assert.areSame( '//after/edition', obj2Array( editor.widgets.instances )[ 0 ].data.url, 'widget url after edition' );
assertCommands( editor, true, false, 'after edition' );
editor.execCommand( 'undo' );
assertCommands( editor, false, true, 'after undo' );
assert.areSame( '<p>foo</p><div data-oembed-url="//before/edition"><p>url:%2F%2Fbefore%2Fedition</p></div><p> </p>',
editor.getData(), 'data after undo' );
editor.execCommand( 'redo' );
assertCommands( editor, true, false, 'after redo' );
assert.areSame( '<p>foo</p><div data-oembed-url="//after/edition"><p>url:%2F%2Fafter%2Fedition</p></div><p> </p>',
editor.getData(), 'data after redo' );
} );
} );
bot.setData( '<p>foo</p><div data-oembed-url="//before/edition"><p>url:%2F%2Fbefore%2Fedition</p></div><p> </p>', function() {
obj2Array( editor.widgets.instances )[ 0 ].focus();
editor.resetUndo();
wait( function() {
editor.execCommand( 'embed' );
} );
} );
},
'test undo and redo after creation and edition': function() {
var bot = this.editorBots.inline,
editor = bot.editor,
urls = [ '//creation/edition/1', '//creation/edition/2' ],
stage = 0;
this.listeners.push( editor.on( 'dialogShow', function( evt ) {
var dialog = evt.data;
setTimeout( function() {
dialog.setValueOf( 'info', 'url', urls[ stage ] );
stage += 1;
dialog.getButton( 'ok' ).click();
}, 50 );
} ) );
this.listeners.push( editor.on( 'dialogHide', function() {
// Edit freshly created widget.
if ( stage == 1 ) {
setTimeout( function() {
editor.execCommand( 'embed' );
}, 50 );
return;
}
resume( function() {
assert.areSame( 1, obj2Array( editor.widgets.instances ).length, '1 widget after creation and edition' );
assert.areSame( urls[ 1 ], obj2Array( editor.widgets.instances )[ 0 ].data.url, 'widget url after edition' );
assertCommands( editor, true, false, 'after edition' );
editor.execCommand( 'undo' );
assert.areSame( 1, obj2Array( editor.widgets.instances ).length, '1 widget after 1st undo' );
assert.areSame( urls[ 0 ], obj2Array( editor.widgets.instances )[ 0 ].data.url, 'widget url after 1st undo' );
assertCommands( editor, true, true, 'after 1st undo' );
editor.execCommand( 'undo' );
assert.areSame( 0, obj2Array( editor.widgets.instances ).length, '0 widgets after 2nd undo' );
assertCommands( editor, false, true, 'after 2nd undo' );
assert.areSame( '<p>foo</p>', editor.getData(), 'data after 2nd undo' );
editor.execCommand( 'redo' );
assert.areSame( 1, obj2Array( editor.widgets.instances ).length, '1 widget after 1st redo' );
assert.areSame( urls[ 0 ], obj2Array( editor.widgets.instances )[ 0 ].data.url, 'widget url after 1st redo' );
assertCommands( editor, true, true, 'after 1st redo' );
editor.execCommand( 'redo' );
assert.areSame( 1, obj2Array( editor.widgets.instances ).length, '1 widget after 2nd redo' );
assert.areSame( urls[ 1 ], obj2Array( editor.widgets.instances )[ 0 ].data.url, 'widget url after 2nd redo' );
assertCommands( editor, true, false, 'after 2nd redo' );
} );
} ) );
setUpEditor( editor, '<p>foo[]</p>' );
wait( function() {
editor.execCommand( 'embed' );
} );
}
} );