-
Notifications
You must be signed in to change notification settings - Fork 105
Expand file tree
/
Copy pathframed_embed.js
More file actions
398 lines (384 loc) · 12.5 KB
/
Copy pathframed_embed.js
File metadata and controls
398 lines (384 loc) · 12.5 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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
var phantom = require('node-phantom-simple'),
phantomjs = require('phantomjs'),
assert = require('assert'),
testutil = require('./lib/testutil'),
one_step_timeout = 8000,
extended_timeout = 30000,
refreshThen = testutil.refreshThen,
asyncTest = testutil.asyncTest;
describe('framed embed', 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 serve the embed script', function(done) {
// Navigate to see the editor for the program named "first".
_page.set('content',
'<div id="embed" style="width:550px;height:350px"></div>' +
'<script src="http://pencilcode.net.dev/lib/pencilcodeembed.js"><' +
'/script>');
asyncTest(_page, one_step_timeout, null, function() {
window.resetTest = function() {
// this object will hold various checks we do along the way
window.test = {
code0: null,
code1: null,
code2: null,
updatedCode: null,
executedCode: null,
loaded: 0,
updated: 0,
executed: 0,
error: false,
loadedThis: false,
updatedThis: false,
executedThis: false,
response: null
};
};
window.resetTest();
}, function() {
// wait until PencilCodeEmbed is ready
if (!window.PencilCodeEmbed) return;
window.test.pce = typeof(window.PencilCodeEmbed);
return window.test;
}, function(err, result){
assert.ifError(err);
assert.equal(0, result.loaded);
assert.equal('function', result.pce);
done();
});
});
it('should set up the load event without firing it', function(done) {
asyncTest(_page, one_step_timeout, null, function() {
window.resetTest();
// create pencil code
var pco = window.pco =
new PencilCodeEmbed(document.getElementById("embed"));
// Set up load handler.
pco.on('load', function() {
window.test.loadedThis = this == pco;
window.test.loaded += 1;
// watch editor changes
pco.on('update', function(code) {
window.test.updatedThis = this == pco;
window.test.updated += 1;
window.test.updatedCode = code;
});
// watch execution
pco.on('execute', function () {
window.test.executedThis = this == pco;
window.test.executed += 1;
window.test.executedCode = pco.getCode();
});
// trap errors
pco.on('error', function (error) {
window.test.error = true;
});
});
}, function() {
// wait until PencilCodeEmbed is fully loaded and code has executed
if (window.pco && window.test) {
return window.test;
}
}, function(err, result){
assert.ifError(err);
assert.ok(!result.loaded);
assert.ok(!result.updated);
assert.ok(!result.executed);
assert.ok(!result.error);
done();
});
});
it('should load code via beginLoad and get update event', function(done) {
asyncTest(_page, extended_timeout, null, function() {
// prepare long code, to exercise URL length
var code = "speed 100\npen red\n";
for (var j = 0; j < 100; ++j) {
code += "fd 50; rt " + j + ", 50\n";
}
window.test.code0 = code;
pco.beginLoad(window.test.code0);
window.test.code1 = pco.getCode();
}, function() {
// wait until PencilCodeEmbed is fully loaded and update event fires.
if (window.test.updated) {
window.test.code2 = pco.getCode();
return window.test;
}
return false;
}, function(err, result){
assert.ifError(err);
assert.ok(result.code0.length > 500);
assert.equal(result.code0, result.code1);
assert.equal(result.code0, result.code2);
assert.equal(1, result.loaded);
assert.equal(1, result.updated);
assert.ok(!result.executed);
assert.ok(!result.error);
assert.ok(result.loadedThis);
assert.ok(result.updatedThis);
assert.ok(!result.executedCode);
assert.equal(result.code0, result.updatedCode);
done();
});
});
it('should load code via setCode and get update event', function(done) {
asyncTest(_page, one_step_timeout, null, function() {
window.resetTest();
// Prepare code without trailing \n, to force change.
// (the editor will guarantee a trailing \n)
window.test.code0 = 'k = 887\nspeed 150\npen blue\nrt 90\nfd 100';
pco.setCode(window.test.code0);
window.test.code1 = pco.getCode();
}, function() {
// wait until PencilCodeEmbed is fully loaded and code has executed
if (window.test.updated) {
window.test.code2 = pco.getCode();
return window.test;
}
return false;
}, function(err, result){
assert.ifError(err);
assert.equal(result.code0, result.code1);
assert.equal(result.code0 + '\n', result.code2);
// Load event should not fire again.
assert.ok(!result.loaded);
// Update should have happened exactly once.
assert.equal(1, result.updated);
assert.ok(!result.executed);
assert.ok(!result.error);
assert.ok(result.updatedThis);
assert.equal(result.code0 + '\n', result.updatedCode);
done();
});
});
it('should be able change visibility of controls', function(done) {
asyncTest(_page, one_step_timeout, null, function() {
window.resetTest();
// change visibility of controls
pco.hideEditor();
pco.showEditor();
pco.hideMiddleButton();
pco.showMiddleButton();
pco.hideToggleButton();
pco.showToggleButton();
pco.setReadOnly();
pco.setEditable();
pco.showNotification('Welcome!');
pco.hideNotification();
// Let the test go for 200ms
setTimeout(function() { window.test.finished = true; }, 200);
}, function() {
// wait until run is completed.
if (window.test.finished) {
window.test.code0 = pco.getCode();
return window.test;
}
return false;
}, function(err, result){
// No errors
assert.ifError(err);
// Code should be intact.
assert.ok(/fd 100/.test(result.code0));
// And no events should have fired.
assert.ok(!result.loaded);
assert.ok(!result.updated);
assert.ok(!result.executed);
assert.ok(!result.error);
done();
});
});
it('should run code via beginRun and get execute event', function(done) {
asyncTest(_page, extended_timeout, null, function() {
window.resetTest();
window.test.code0 = pco.getCode();
pco.beginRun();
}, function() {
// wait until run is completed.
if (window.test.executed) {
window.test.code1 = pco.getCode();
return window.test;
}
return false;
}, function(err, result){
assert.ifError(err);
assert.ok(/fd 100/.test(result.code0));
assert.equal(result.code0, result.code1);
// Load event should not fire again.
assert.ok(!result.loaded);
// Update should not have fired.
assert.ok(!result.updated);
// Run should happen once.
assert.equal(1, result.executed);
assert.ok(!result.error);
assert.ok(result.executedThis);
assert.equal(result.code0, result.executedCode);
done();
});
});
it('should be able to eval within the frame using eval', function(done) {
asyncTest(_page, one_step_timeout, null, function() {
window.resetTest();
pco.eval('k', function(v, e) {
window.test.response = { value: v, error: e };
});
}, function() {
// wait until run is completed.
if (window.test.response) {
return window.test;
}
return false;
}, function(err, result){
assert.ifError(err);
assert.equal(result.response.value, 887);
assert.ok(!result.response.error);
done();
});
});
it('should be able to get an error using eval', function(done) {
asyncTest(_page, one_step_timeout, null, function() {
window.resetTest();
pco.eval('u', function(v, e) {
window.test.response = { value: v, error: e };
});
}, function() {
// wait until run is completed.
if (window.test.response) {
return window.test;
}
return false;
}, function(err, result){
assert.ifError(err);
assert.ok(!result.response.value);
// Expect some sort of error message here.
assert.ok(result.response.error);
done();
});
});
it('should run code via beginRun with setupScript', function(done) {
asyncTest(_page, extended_timeout, null, function() {
window.resetTest();
window.test.code0 = pco.getCode();
pco.setupScript([
{code: 'window.u = "hello";'}
]);
pco.beginRun();
}, function() {
// wait until run is completed.
if (window.test.executed) {
window.test.code1 = pco.getCode();
return window.test;
}
return false;
}, function(err, result){
assert.ifError(err);
// Preview script is not visible to the user.
assert.ok(/fd 100/.test(result.code0));
assert.ok(!/hello/.test(result.code0));
assert.equal(result.code0, result.code1);
// Load event should not fire again.
assert.ok(!result.loaded);
// Update should not have fired.
assert.ok(!result.updated);
// Run should happen once.
assert.equal(1, result.executed);
assert.ok(!result.error);
assert.ok(result.executedThis);
assert.equal(result.code0, result.executedCode);
done();
});
});
it('should be able see a variable set with setupScript', function(done) {
asyncTest(_page, one_step_timeout, null, function() {
window.resetTest();
pco.eval('u', function(v, e) {
window.test.response = { value: v, error: e };
});
}, function() {
// wait until run is completed.
if (window.test.response) {
return window.test;
}
return false;
}, function(err, result){
assert.ifError(err);
assert.ok(!result.response.error);
assert.equal(result.response.value, 'hello');
done();
});
});
it('should be able to interrupt a script with stopRun', function(done) {
asyncTest(_page, one_step_timeout, null, function() {
window.resetTest();
pco.setCode('speed 1\nfor [1..100]\n rt 360\n', function(){});
pco.beginRun();
}, function() {
pco.eval('interrupt("test")', function(v, e) {
window.test.response = { value: v, error: e };
});
// wait until run starts.
if (window.test.response && window.test.response.value) {
return window.test;
}
return false;
}, function(err, result){
assert.ifError(err);
assert.ok(!result.response.error);
assert.equal(result.response.value, true);
next();
});
function next() { asyncTest(_page, one_step_timeout, null, function() {
window.resetTest();
pco.stopRun();
}, function() {
pco.stopRun();
pco.eval('interrupt("test")', function(v, e) {
window.test.response = { value: v, error: e };
});
// wait until run stops.
if (window.test.response && !window.test.response.value) {
return window.test;
}
return false;
}, function(err, result){
assert.ifError(err);
assert.ok(!result.response.error);
assert.equal(result.response.value, false);
done();
}); }
});
});