-
Notifications
You must be signed in to change notification settings - Fork 105
Expand file tree
/
Copy pathdebug_code.js
More file actions
330 lines (328 loc) · 12.1 KB
/
Copy pathdebug_code.js
File metadata and controls
330 lines (328 loc) · 12.1 KB
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
var phantom = require('node-phantom-simple'),
phantomjs = require('phantomjs'),
assert = require('assert'),
testutil = require('./lib/testutil'),
one_step_timeout = 8000,
refreshThen = testutil.refreshThen,
asyncTest = testutil.asyncTest;
describe('code debugger', function() {
var _ph, _page;
before(function(done) {
// Create the headless webkit browser.
phantom.create({
path: phantomjs.path,
parameters: {
// Use the test server as a proxy server, so that all requests
// go to this server (instead of trying real DNS lookups).
proxy: '127.0.0.1:8193',
// Set the disk storage to zero to avoid persisting localStorage
// between test runs.
'local-storage-quota': 0
}
}, function(error, ph) {
assert.ifError(error);
// Open a page for browsing.
_ph = ph;
_ph.createPage(function(err, page) {
_page = page;
// Set the size to a modern laptop size.
page.set('viewportSize', { width: 1200, height: 900 }, function(err) {
assert.ifError(err);
// Point it to a blank page to start
page.open('about:blank', function(err, status){
assert.ifError(err);
assert.equal(status, 'success');
done();
});
});
});
});
});
after(function() {
// Be sure to kill the browser when the test is done, or else
// we can leave orphan processes.
_ph.exit();
});
it('should show run button when loading code', function(done) {
// Navigate to see the editor for the program named "first".
_page.open('http://livetest.pencilcode.net.dev/edit/hi',
function(err, status) {
assert.ifError(err);
assert.equal(status, 'success');
asyncTest(_page, one_step_timeout, null, null, function() {
// Poll until the element with class="editor" appears on the page.
if (!$('.editor').length) return;
// Reach in and return the text that is shown within the editor.
var ace_editor = ace.edit($('.droplet-ace')[0]);
return {
text: ace_editor.getSession().getValue(),
runbuttoncount: $('#run').length
};
}, function(err, result) {
assert.ifError(err);
// The editor text should contain this line of code.
assert.ok(/pen blue/.test(result.text));
assert.equal(result.runbuttoncount, 1);
done();
});
});
});
it('should be able to run the program', function(done) {
asyncTest(_page, one_step_timeout, null, function() {
// Click on the triangle run button.
$('#run').mousedown();
$('#run').click();
}, function() {
try {
// Wait for the preview frame to show.
if (!$('.preview iframe').length) return;
if (!$('.preview iframe')[0].contentWindow.see) return;
// Evaluate some expression in the coffeescript evaluation window.
var seval = $('.preview iframe')[0].contentWindow.see.eval;
// And also wait for the turtle to start moving.
if (seval('getxy()')[1] < 10) return;
return {
direction: seval('direction()'),
getxy: seval('getxy()'),
touchesred: seval('touches red'),
touchesblue: seval('touches blue'),
queuelen: seval('turtle.queue().length'),
stopcount: $('#stop').length
};
}
catch(e) {
return {poll: true, error: e};
}
}, function(err, result) {
assert.ifError(err);
// The first move is forward....
assert.equal(0, result.direction);
assert.ok(Math.abs(result.getxy[0] - 0) < 1e-6);
// The turtle should not be touching any red pixels.
assert.equal(false, result.touchesred);
// The turtle should be touching blue pixels that it drew.
assert.equal(true, result.touchesblue);
// The turtle should still have lots of queued actions.
assert.ok(result.queuelen > 0);
// The stop button should be showing
assert.equal(1, result.stopcount);
done();
});
});
/*
* CURRENTLY THE DEBUGGER IS BROKEN - tests commented for now.
* Much of the debugging functionality was disabled in order to
* improve execution speed, so that students can work with recursion
* without timing out.
* TODO(davidbau): ressurect the debugger.
*
it('should be able to stop the program', function(done) {
asyncTest(_page, one_step_timeout, null, function() {
// Click on the square stop button.
$('#stop').mousedown();
$('#stop').click();
}, function() {
try {
if (!$('.preview iframe').length) return;
if (!$('.preview iframe')[0].contentWindow.see) return;
// Evaluate some expression in the coffeescript evaluation window.
var seval = $('.preview iframe')[0].contentWindow.see.eval;
// Reset interrupts so that we can evaluate some expressions.
seval('interrupt("reset")');
// And also wait for the turtle to start moving.
return {
touchesred: seval('touches red'),
touchesblue: seval('touches blue'),
queuelen: seval('turtle.queue().length'),
debugtracecount: $('.debugtrace').length,
debugtracetop: $('.debugtrace').css('top')
};
}
catch(e) {
return {poll: true, error: e};
}
}, function(err, result) {
assert.ifError(err);
// The turtle should not be touching any red pixels.
assert.equal(false, result.touchesred);
// The turtle should be touching blue pixels that it drew.
assert.equal(true, result.touchesblue);
// The turtle should not be moving any more.
assert.equal(0, result.queuelen);
// A line of code should be traced.
assert.equal(1, result.debugtracecount);
// The traced code should be around line 4 or beyond.
assert.ok(parseInt(result.debugtracetop) > 80);
done();
});
});
it('should be able to highlight lines when hovered', function(done) {
asyncTest(_page, one_step_timeout, null, function() {
// Click on the triangle run button.
$('#run').mousedown();
$('#run').click();
// Create function to simulate mouse movements.
window._simulate = function simulate(type, target, options) {
if ('string' == typeof(target)) {
target = $(target).get(0);
}
options = options || {};
var pageX = pageY = clientX = clientY = dx = dy = 0;
var location = options.location || target;
if (location) {
if ('string' == typeof(location)) {
location = $(location).get(0);
}
var gbcr = location.getBoundingClientRect();
clientX = gbcr.left,
clientY = gbcr.top,
pageX = clientX + window.pageXOffset;
pageY = clientY + window.pageYOffset;
dx = Math.floor((gbcr.right - gbcr.left) / 2);
dy = Math.floor((gbcr.bottom - gbcr.top) / 2);
}
if ('dx' in options) dx = options.dx;
if ('dy' in options) dy = options.dy;
pageX = (options.pageX == null ? pageX : options.pageX) + dx;
pageY = (options.pageY == null ? pageY : options.pageY) + dy;
clientX = pageX - window.pageXOffset;
clientY = pageY - window.pageYOffset;
var opts = {
bubbles: options.bubbles || true,
cancelable: options.cancelable || true,
view: options.view || target.ownerDocument.defaultView,
detail: options.detail || 1,
pageX: pageX,
pageY: pageY,
clientX: clientX,
clientY: clientY,
screenX: clientX + window.screenLeft,
screenY: clientY + window.screenTop,
ctrlKey: options.ctrlKey || false,
altKey: options.altKey || false,
shiftKey: options.shiftKey || false,
metaKey: options.metaKey || false,
button: options.button || 0,
which: options.which || 1,
relatedTarget: options.relatedTarget || null,
}
var evt;
try {
// Modern API supported by IE9+
evt = new MouseEvent(type, opts);
}
catch (e) {
// Old API still required by PhantomJS.
evt = target.ownerDocument.createEvent('MouseEvents');
evt.initMouseEvent(type, opts.bubbles, opts.cancelable, opts.view,
opts.detail, opts.screenX, opts.screenY, opts.clientX, opts.clientY,
opts.ctrlKey, opts.altKey, opts.shiftKey, opts.metaKey, opts.button,
opts.relatedTarget);
}
target.dispatchEvent(evt);
}
}, function() {
try {
if (!$('.preview iframe').length) return;
if (!$('.preview iframe')[0].contentWindow.see) return;
if (!$('.ace_gutter-cell').length) return;
// Simulate hovering over a program line.
window._simulate('mouseover', $('.ace_gutter-cell')[0]);
// Wait until hovering occurs.
if ($('.debugfocus').length == 0) {
return;
}
return {
debugfocus: $('.debugfocus').length
};
}
catch(e) {
return {poll: true, error: e};
}
}, function(err, result) {
assert.ifError(err);
// A line of code should be highlighted.
assert.equal(1, result.debugfocus);
done();
});
});
it('should be able to unhighlight lines when unhovered', function(done) {
asyncTest(_page, one_step_timeout, null, function() {
// Click on the triangle run button.
$('#run').mousedown();
$('#run').click();
}, function() {
try {
if (!$('.preview iframe').length) return;
if (!$('.preview iframe')[0].contentWindow.see) return;
if (!$('.ace_gutter-cell').length) return;
// Have the mouse hover over the program line.
window._simulate('mouseover', $('.ace_gutter-cell')[0]);
// Have the mouse move away from the program line.
window._simulate('mouseout', $('.ace_gutter-cell')[0]);
// Wait until mouse moves away from the program line.
if ($('.debugfocus').length != 0) {
return;
}
return {
debugfocus: $('.debugfocus').length,
};
}
catch(e) {
return {poll: true, error: e};
}
}, function(err, result) {
assert.ifError(err);
// A line of code should not be highlighted.
assert.equal(0, result.debugfocus);
done();
});
});
it('should not trace commands in the test panel', function(done) {
asyncTest(_page, one_step_timeout, null, function() {
// Click on the square stop button.
$('#stop').mousedown();
$('#stop').click();
}, function() {
try {
if (!$('.preview iframe').length) return;
if (!$('.preview iframe')[0].contentWindow.see) return;
// Keep track of the currently traced line.
var curr_trace = $('.debugtrace').css('top');
// Evaluate some expression in the coffeescript window.
var seval = $('.preview iframe')[0].contentWindow.see.eval;
seval('interrupt("reset")');
seval('fd 100');
return {
originaltrace: curr_trace,
debugtracecount: $('.debugtrace').length,
debugtracetop: $('.debugtrace').css('top')
};
}
catch(e) {
return {poll: true, error: e};
}
}, function(err, result) {
assert.ifError(err);
// The same line should still be traced.
assert.equal(parseInt(result.originaltrace), parseInt(result.debugtracetop));
done();
});
});
*/
it('is done', function(done) {
asyncTest(_page, one_step_timeout, null, function() {
// Final cleanup: delete local storage and the cookie.
localStorage.clear();
document.cookie='login=; expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/';
}, function() {
return {
cookie: document.cookie
};
}, function(err, result) {
assert.ifError(err);
assert.ok(!/login=/.test(result.cookie));
done();
});
});
});