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
|
// WARNING: drag and drop is only simulated on these tests, so manual testing might also be necessary
describe('drag and drop', function() {
before(function(done) {
helper.newPad(function() {
createScriptWithSeveralLines(done);
});
this.timeout(60000);
});
context('when user drags part of one line and drops it far form its original place', function() {
before(function(done) {
selectPartOfSourceLine();
dragSelectedTextAndDropItIntoMiddleOfLine(TARGET_LINE);
// make sure DnD was correctly simulated
helper.waitFor(function() {
var $targetLine = getLine(TARGET_LINE);
var sourceWasMovedToTarget = $targetLine.text() === 'Target line [line 1]';
return sourceWasMovedToTarget;
}).done(done);
});
context('and user triggers UNDO', function() {
before(function() {
var $undoButton = helper.padChrome$(".buttonicon-undo");
$undoButton.click();
});
it('moves text back to its original place', function(done) {
// test text was removed from drop target
var $targetLine = getLine(TARGET_LINE);
expect($targetLine.text()).to.be('Target line []');
// test text was added back to original place
var $firstSourceLine = getLine(FIRST_SOURCE_LINE);
var $lastSourceLine = getLine(FIRST_SOURCE_LINE + 1);
expect($firstSourceLine.text()).to.be('Source line 1.');
expect($lastSourceLine.text()).to.be('Source line 2.');
done();
});
});
});
context('when user drags some lines far form its original place', function() {
before(function(done) {
selectMultipleSourceLines();
dragSelectedTextAndDropItIntoMiddleOfLine(TARGET_LINE);
// make sure DnD was correctly simulated
helper.waitFor(function() {
var $lineAfterTarget = getLine(TARGET_LINE + 1);
var sourceWasMovedToTarget = $lineAfterTarget.text() !== '...';
return sourceWasMovedToTarget;
}).done(done);
});
context('and user triggers UNDO', function() {
before(function() {
var $undoButton = helper.padChrome$(".buttonicon-undo");
$undoButton.click();
});
it('moves text back to its original place', function(done) {
// test text was removed from drop target
var $targetLine = getLine(TARGET_LINE);
expect($targetLine.text()).to.be('Target line []');
// test text was added back to original place
var $firstSourceLine = getLine(FIRST_SOURCE_LINE);
var $lastSourceLine = getLine(FIRST_SOURCE_LINE + 1);
expect($firstSourceLine.text()).to.be('Source line 1.');
expect($lastSourceLine.text()).to.be('Source line 2.');
done();
});
});
});
/* ********************* Helper functions/constants ********************* */
var TARGET_LINE = 2;
var FIRST_SOURCE_LINE = 5;
var getLine = function(lineNumber) {
var $lines = helper.padInner$('div');
return $lines.slice(lineNumber, lineNumber + 1);
}
var createScriptWithSeveralLines = function(done) {
// create some lines to be used on the tests
var $firstLine = helper.padInner$('div').first();
$firstLine.html('...<br>...<br>Target line []<br>...<br>...<br>Source line 1.<br>Source line 2.<br>');
// wait for lines to be split
helper.waitFor(function(){
var $lastSourceLine = getLine(FIRST_SOURCE_LINE + 1);
return $lastSourceLine.text() === 'Source line 2.';
}).done(done);
}
var selectPartOfSourceLine = function() {
var $sourceLine = getLine(FIRST_SOURCE_LINE);
// select 'line 1' from 'Source line 1.'
var start = 'Source '.length;
var end = start + 'line 1'.length;
helper.selectLines($sourceLine, $sourceLine, start, end);
}
var selectMultipleSourceLines = function() {
var $firstSourceLine = getLine(FIRST_SOURCE_LINE);
var $lastSourceLine = getLine(FIRST_SOURCE_LINE + 1);
helper.selectLines($firstSourceLine, $lastSourceLine);
}
var dragSelectedTextAndDropItIntoMiddleOfLine = function(targetLineNumber) {
// dragstart: start dragging content
triggerEvent('dragstart');
// drop: get HTML data from selected text
var draggedHtml = getHtmlFromSelectedText();
triggerEvent('drop');
// dragend: remove original content + insert HTML data into target
moveSelectionIntoTarget(draggedHtml, targetLineNumber);
triggerEvent('dragend');
}
var getHtmlFromSelectedText = function() {
var innerDocument = helper.padInner$.document;
var range = innerDocument.getSelection().getRangeAt(0);
var clonedSelection = range.cloneContents();
var span = innerDocument.createElement('span');
span.id = 'buffer';
span.appendChild(clonedSelection);
var draggedHtml = span.outerHTML;
return draggedHtml;
}
var triggerEvent = function(eventName) {
var event = helper.padInner$.Event(eventName);
helper.padInner$('#innerdocbody').trigger(event);
}
var moveSelectionIntoTarget = function(draggedHtml, targetLineNumber) {
var innerDocument = helper.padInner$.document;
// delete original content
innerDocument.execCommand('delete');
// set position to insert content on target line
var $target = getLine(targetLineNumber);
$target.sendkeys('{selectall}{rightarrow}{leftarrow}');
// Insert content.
// Based on http://stackoverflow.com/a/6691294, to be IE-compatible
var range = innerDocument.getSelection().getRangeAt(0);
var frag = innerDocument.createDocumentFragment();
var el = innerDocument.createElement('div');
el.innerHTML = draggedHtml;
while (el.firstChild) {
frag.appendChild(el.firstChild);
}
range.insertNode(frag);
}
});
|