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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
|
/****************************************************************************
**
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the test suite of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** No Commercial Usage
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the Technology Preview License Agreement accompanying
** this package.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
**
**
**
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include <QtTest/QtTest>
#include <qscriptengine.h>
#include <qscriptenginedebugger.h>
#include <qaction.h>
#include <qlineedit.h>
#include <qmainwindow.h>
#include <qmenu.h>
#include <qplaintextedit.h>
#include <qtoolbar.h>
//TESTED_CLASS=
//TESTED_FILES=
// Will try to wait for the condition while allowing event processing
#define QTRY_COMPARE(__expr, __expected) \
do { \
const int __step = 50; \
const int __timeout = 5000; \
if ((__expr) != (__expected)) { \
QTest::qWait(0); \
} \
for (int __i = 0; __i < __timeout && ((__expr) != (__expected)); __i+=__step) { \
QTest::qWait(__step); \
} \
QCOMPARE(__expr, __expected); \
} while(0)
// Can't use QTest::qWait() because it causes event loop to hang on some platforms
static void qsWait(int ms)
{
QTimer timer;
timer.setSingleShot(true);
timer.setInterval(ms);
timer.start();
QEventLoop loop;
QObject::connect(&timer, SIGNAL(timeout()), &loop, SLOT(quit()));
loop.exec();
QCoreApplication::processEvents();
}
class tst_QScriptEngineDebugger : public QObject
{
Q_OBJECT
public:
tst_QScriptEngineDebugger();
virtual ~tst_QScriptEngineDebugger();
private slots:
void attachAndDetach();
void action();
void widget();
void standardObjects();
void debuggerSignals();
void consoleCommands();
void multithreadedDebugging();
};
tst_QScriptEngineDebugger::tst_QScriptEngineDebugger()
{
}
tst_QScriptEngineDebugger::~tst_QScriptEngineDebugger()
{
}
void tst_QScriptEngineDebugger::attachAndDetach()
{
{
QScriptEngineDebugger debugger;
QCOMPARE(debugger.state(), QScriptEngineDebugger::SuspendedState);
debugger.attachTo(0);
QScriptEngine engine;
debugger.attachTo(&engine);
QCOMPARE(debugger.state(), QScriptEngineDebugger::SuspendedState);
}
{
QScriptEngineDebugger debugger;
QScriptEngine engine;
QScriptValue oldPrint = engine.globalObject().property("print");
QVERIFY(oldPrint.isFunction());
QVERIFY(!engine.globalObject().property("__FILE__").isValid());
QVERIFY(!engine.globalObject().property("__LINE__").isValid());
debugger.attachTo(&engine);
QVERIFY(engine.globalObject().property("__FILE__").isUndefined());
QVERIFY(engine.globalObject().property("__LINE__").isNumber());
QVERIFY(!engine.globalObject().property("print").strictlyEquals(oldPrint));
debugger.detach();
QVERIFY(engine.globalObject().property("print").strictlyEquals(oldPrint));
QVERIFY(!engine.globalObject().property("__FILE__").isValid());
QVERIFY(!engine.globalObject().property("__LINE__").isValid());
}
{
QScriptEngineDebugger debugger;
QScriptEngine engine;
debugger.attachTo(&engine);
debugger.detach();
QScriptEngine engine2;
debugger.attachTo(&engine2);
}
{
QScriptEngineDebugger debugger;
QScriptEngine engine;
debugger.attachTo(&engine);
debugger.detach();
QScriptEngine engine2;
debugger.attachTo(&engine2);
debugger.detach();
}
#ifndef Q_OS_WINCE // demands too much memory for WinCE
{
QScriptEngineDebugger debugger;
QScriptEngine engine;
debugger.attachTo(&engine);
QScriptEngineDebugger debugger2;
debugger2.attachTo(&engine);
}
#endif
{
QScriptEngine *engine = new QScriptEngine;
QScriptEngineDebugger debugger;
debugger.attachTo(engine);
delete engine;
QScriptEngine engine2;
debugger.attachTo(&engine2);
}
}
void tst_QScriptEngineDebugger::action()
{
QScriptEngine engine;
QScriptEngineDebugger debugger;
debugger.attachTo(&engine);
QList<QScriptEngineDebugger::DebuggerAction> actions;
actions
<< QScriptEngineDebugger::InterruptAction
<< QScriptEngineDebugger::ContinueAction
<< QScriptEngineDebugger::StepIntoAction
<< QScriptEngineDebugger::StepOverAction
<< QScriptEngineDebugger::StepOutAction
<< QScriptEngineDebugger::RunToCursorAction
<< QScriptEngineDebugger::RunToNewScriptAction
<< QScriptEngineDebugger::ToggleBreakpointAction
<< QScriptEngineDebugger::ClearDebugOutputAction
<< QScriptEngineDebugger::ClearErrorLogAction
<< QScriptEngineDebugger::ClearConsoleAction
<< QScriptEngineDebugger::FindInScriptAction
<< QScriptEngineDebugger::FindNextInScriptAction
<< QScriptEngineDebugger::FindPreviousInScriptAction
<< QScriptEngineDebugger::GoToLineAction;
QList<QAction*> lst;
for (int i = 0; i < actions.size(); ++i) {
QScriptEngineDebugger::DebuggerAction da = actions.at(i);
QAction *act = debugger.action(da);
QVERIFY(act != 0);
QCOMPARE(act, debugger.action(da));
QCOMPARE(act->parent(), (QObject*)&debugger);
QVERIFY(lst.indexOf(act) == -1);
lst.append(act);
}
}
void tst_QScriptEngineDebugger::widget()
{
QScriptEngine engine;
QScriptEngineDebugger debugger;
debugger.attachTo(&engine);
QList<QScriptEngineDebugger::DebuggerWidget> widgets;
widgets
<< QScriptEngineDebugger::ConsoleWidget
<< QScriptEngineDebugger::StackWidget
<< QScriptEngineDebugger::ScriptsWidget
<< QScriptEngineDebugger::LocalsWidget
<< QScriptEngineDebugger::CodeWidget
<< QScriptEngineDebugger::CodeFinderWidget
<< QScriptEngineDebugger::BreakpointsWidget
<< QScriptEngineDebugger::DebugOutputWidget
<< QScriptEngineDebugger::ErrorLogWidget;
QList<QWidget*> lst;
for (int i = 0; i < widgets.size(); ++i) {
QScriptEngineDebugger::DebuggerWidget dw = widgets.at(i);
QWidget *wid = debugger.widget(dw);
QVERIFY(wid != 0);
QCOMPARE(wid, debugger.widget(dw));
QVERIFY(lst.indexOf(wid) == -1);
lst.append(wid);
QCOMPARE(static_cast<QWidget *>(wid->parent()), (QWidget*)0);
}
}
void tst_QScriptEngineDebugger::standardObjects()
{
QScriptEngine engine;
QScriptEngineDebugger debugger;
debugger.attachTo(&engine);
QMainWindow *win = debugger.standardWindow();
QCOMPARE(static_cast<QWidget *>(win->parent()), (QWidget*)0);
QMenu *menu = debugger.createStandardMenu();
QCOMPARE(static_cast<QWidget *>(menu->parent()), (QWidget*)0);
QToolBar *toolBar = debugger.createStandardToolBar();
QCOMPARE(static_cast<QWidget *>(toolBar->parent()), (QWidget*)0);
QMenu *menu2 = debugger.createStandardMenu(win);
QCOMPARE(static_cast<QWidget *>(menu2->parent()), (QWidget*)win);
QVERIFY(menu2 != menu);
QToolBar *toolBar2 = debugger.createStandardToolBar(win);
QCOMPARE(static_cast<QWidget *>(toolBar2->parent()), (QWidget*)win);
QVERIFY(toolBar2 != toolBar);
delete menu;
delete toolBar;
}
void tst_QScriptEngineDebugger::debuggerSignals()
{
QScriptEngine engine;
QScriptEngineDebugger debugger;
debugger.attachTo(&engine);
debugger.setAutoShowStandardWindow(false);
QSignalSpy evaluationSuspendedSpy(&debugger, SIGNAL(evaluationSuspended()));
QSignalSpy evaluationResumedSpy(&debugger, SIGNAL(evaluationResumed()));
QObject::connect(&debugger, SIGNAL(evaluationSuspended()),
debugger.action(QScriptEngineDebugger::ContinueAction),
SLOT(trigger()));
engine.evaluate("123");
QCOMPARE(evaluationSuspendedSpy.count(), 0);
QCOMPARE(evaluationResumedSpy.count(), 0);
engine.evaluate("debugger");
QCoreApplication::processEvents();
QCOMPARE(evaluationSuspendedSpy.count(), 1);
QCOMPARE(evaluationResumedSpy.count(), 1);
}
static void executeConsoleCommand(QLineEdit *inputEdit, QPlainTextEdit *outputEdit,
const QString &text)
{
QString before = outputEdit->toPlainText();
inputEdit->setText(text);
QTest::keyPress(inputEdit, Qt::Key_Enter);
const int delay = 100;
qsWait(delay);
QString after = outputEdit->toPlainText();
int retryCount = 10;
LAgain:
while ((before == after) && (retryCount != 0)) {
qsWait(delay);
after = outputEdit->toPlainText();
--retryCount;
}
if (before != after) {
before = after;
qsWait(delay);
after = outputEdit->toPlainText();
if (before != after) {
retryCount = 10;
goto LAgain;
}
}
}
class DebuggerCommandExecutor : public QObject
{
Q_OBJECT
public:
DebuggerCommandExecutor(QLineEdit *inputEdit,
QPlainTextEdit *outputEdit,
const QString &text,
QObject *parent = 0)
: QObject(parent), m_inputEdit(inputEdit),
m_outputEdit(outputEdit), m_commands(text) {}
DebuggerCommandExecutor(QLineEdit *inputEdit,
QPlainTextEdit *outputEdit,
const QStringList &commands,
QObject *parent = 0)
: QObject(parent), m_inputEdit(inputEdit),
m_outputEdit(outputEdit), m_commands(commands) {}
public Q_SLOTS:
void execute() {
for (int i = 0; i < m_commands.size(); ++i)
executeConsoleCommand(m_inputEdit, m_outputEdit, m_commands.at(i));
}
private:
QLineEdit *m_inputEdit;
QPlainTextEdit *m_outputEdit;
QStringList m_commands;
};
void tst_QScriptEngineDebugger::consoleCommands()
{
QSKIP("This test can hang / misbehave because of timing/event loop issues (task 241300)", SkipAll);
QScriptEngine engine;
QScriptEngineDebugger debugger;
debugger.setAutoShowStandardWindow(false);
debugger.attachTo(&engine);
QWidget *consoleWidget = debugger.widget(QScriptEngineDebugger::ConsoleWidget);
QLineEdit *inputEdit = qFindChild<QLineEdit*>(consoleWidget);
QVERIFY(inputEdit != 0);
QPlainTextEdit *outputEdit = qFindChild<QPlainTextEdit*>(consoleWidget);
QVERIFY(outputEdit != 0);
QVERIFY(outputEdit->toPlainText().startsWith("Welcome to the Qt Script debugger."));
outputEdit->clear();
// print()
{
QWidget *debugOutputWidget = debugger.widget(QScriptEngineDebugger::DebugOutputWidget);
QPlainTextEdit *debugOutputEdit = qFindChild<QPlainTextEdit*>(debugOutputWidget);
QVERIFY(debugOutputEdit != 0);
QVERIFY(debugOutputEdit->toPlainText().isEmpty());
executeConsoleCommand(inputEdit, outputEdit, "print('Test of debug output')");
QCOMPARE(debugOutputEdit->toPlainText(), QString::fromLatin1("Test of debug output"));
QCOMPARE(outputEdit->toPlainText(), QString::fromLatin1("qsdb> print('Test of debug output')"));
}
outputEdit->clear();
executeConsoleCommand(inputEdit, outputEdit, ".info scripts");
QCOMPARE(outputEdit->toPlainText(), QString::fromLatin1("qsdb> .info scripts\nNo scripts loaded."));
outputEdit->clear();
executeConsoleCommand(inputEdit, outputEdit, ".break foo.qs:123");
QCOMPARE(outputEdit->toPlainText(), QString::fromLatin1("qsdb> .break foo.qs:123\nBreakpoint 1: foo.qs, line 123."));
outputEdit->clear();
executeConsoleCommand(inputEdit, outputEdit, ".break 123");
QCOMPARE(outputEdit->toPlainText(), QString::fromLatin1("qsdb> .break 123\nNo script."));
outputEdit->clear();
executeConsoleCommand(inputEdit, outputEdit, ".info breakpoints");
QCOMPARE(outputEdit->toPlainText(), QString::fromLatin1("qsdb> .info breakpoints\nId\tEnabled\tWhere\n1\tyes\tfoo.qs:123"));
outputEdit->clear();
executeConsoleCommand(inputEdit, outputEdit, ".disable 1");
QCOMPARE(outputEdit->toPlainText(), QString::fromLatin1("qsdb> .disable 1"));
outputEdit->clear();
executeConsoleCommand(inputEdit, outputEdit, ".info breakpoints");
QCOMPARE(outputEdit->toPlainText(), QString::fromLatin1("qsdb> .info breakpoints\nId\tEnabled\tWhere\n1\tno\tfoo.qs:123"));
outputEdit->clear();
executeConsoleCommand(inputEdit, outputEdit, ".disable 1");
QCOMPARE(outputEdit->toPlainText(), QString::fromLatin1("qsdb> .disable 1"));
outputEdit->clear();
executeConsoleCommand(inputEdit, outputEdit, ".disable 123");
QCOMPARE(outputEdit->toPlainText(), QString::fromLatin1("qsdb> .disable 123\nNo breakpoint number 123."));
outputEdit->clear();
executeConsoleCommand(inputEdit, outputEdit, ".enable 1");
QCOMPARE(outputEdit->toPlainText(), QString::fromLatin1("qsdb> .enable 1"));
outputEdit->clear();
executeConsoleCommand(inputEdit, outputEdit, ".info breakpoints");
QCOMPARE(outputEdit->toPlainText(), QString::fromLatin1("qsdb> .info breakpoints\nId\tEnabled\tWhere\n1\tyes\tfoo.qs:123"));
outputEdit->clear();
executeConsoleCommand(inputEdit, outputEdit, ".enable 123");
QCOMPARE(outputEdit->toPlainText(), QString::fromLatin1("qsdb> .enable 123\nNo breakpoint number 123."));
outputEdit->clear();
executeConsoleCommand(inputEdit, outputEdit, ".condition 1 i > 456");
QCOMPARE(outputEdit->toPlainText(), QString::fromLatin1("qsdb> .condition 1 i > 456"));
outputEdit->clear();
executeConsoleCommand(inputEdit, outputEdit, ".info breakpoints");
QCOMPARE(outputEdit->toPlainText(), QString::fromLatin1("qsdb> .info breakpoints\nId\tEnabled\tWhere\n1\tyes\tfoo.qs:123\n\tstop only if i > 456"));
outputEdit->clear();
executeConsoleCommand(inputEdit, outputEdit, ".condition 1");
QCOMPARE(outputEdit->toPlainText(), QString::fromLatin1("qsdb> .condition 1\nBreakpoint 1 now unconditional."));
outputEdit->clear();
executeConsoleCommand(inputEdit, outputEdit, ".info breakpoints");
QCOMPARE(outputEdit->toPlainText(), QString::fromLatin1("qsdb> .info breakpoints\nId\tEnabled\tWhere\n1\tyes\tfoo.qs:123"));
outputEdit->clear();
executeConsoleCommand(inputEdit, outputEdit, ".condition 123");
QCOMPARE(outputEdit->toPlainText(), QString::fromLatin1("qsdb> .condition 123\nNo breakpoint number 123."));
outputEdit->clear();
executeConsoleCommand(inputEdit, outputEdit, ".ignore 1");
QCOMPARE(outputEdit->toPlainText(), QString::fromLatin1("qsdb> .ignore 1\nMissing argument (ignore-count)."));
outputEdit->clear();
executeConsoleCommand(inputEdit, outputEdit, ".ignore 1 10");
QCOMPARE(outputEdit->toPlainText(), QString::fromLatin1("qsdb> .ignore 1 10\nBreakpoint 1 will be ignored the next 10 time(s)."));
outputEdit->clear();
executeConsoleCommand(inputEdit, outputEdit, ".delete 1");
QCOMPARE(outputEdit->toPlainText(), QString::fromLatin1("qsdb> .delete 1"));
outputEdit->clear();
executeConsoleCommand(inputEdit, outputEdit, ".info breakpoints");
QCOMPARE(outputEdit->toPlainText(), QString::fromLatin1("qsdb> .info breakpoints\nNo breakpoints set."));
outputEdit->clear();
executeConsoleCommand(inputEdit, outputEdit, ".tbreak bar.qs:456");
QCOMPARE(outputEdit->toPlainText(), QString::fromLatin1("qsdb> .tbreak bar.qs:456\nBreakpoint 2: bar.qs, line 456."));
{
QString script;
script.append("function foo(i) {\n");
for (int i = 0; i < 100; ++i)
script.append(QString::fromLatin1(" i = i + %0;\n").arg(i));
script.append(" return i;\n}");
engine.evaluate(script, "foo.qs");
}
outputEdit->clear();
executeConsoleCommand(inputEdit, outputEdit, ".info scripts");
QCOMPARE(outputEdit->toPlainText(), QString::fromLatin1("qsdb> .info scripts\n\tfoo.qs"));
outputEdit->clear();
executeConsoleCommand(inputEdit, outputEdit, ".list foo.qs");
QCOMPARE(outputEdit->toPlainText(), QString::fromLatin1("qsdb> .list foo.qs\n"
"1\tfunction foo(i) {\n"
"2\t i = i + 0;\n"
"3\t i = i + 1;\n"
"4\t i = i + 2;\n"
"5\t i = i + 3;\n"
"6\t i = i + 4;\n"
"7\t i = i + 5;\n"
"8\t i = i + 6;\n"
"9\t i = i + 7;\n"
"10\t i = i + 8;"));
outputEdit->clear();
executeConsoleCommand(inputEdit, outputEdit, ".list");
QCOMPARE(outputEdit->toPlainText(), QString::fromLatin1("qsdb> .list\n"
"No script."));
outputEdit->clear();
executeConsoleCommand(inputEdit, outputEdit, ".backtrace");
QCOMPARE(outputEdit->toPlainText(), QString::fromLatin1("qsdb> .backtrace\n#0 <global>() at -1"));
outputEdit->clear();
executeConsoleCommand(inputEdit, outputEdit, ".down");
QCOMPARE(outputEdit->toPlainText(), QString::fromLatin1("qsdb> .down\nAlready at bottom (innermost) frame."));
outputEdit->clear();
executeConsoleCommand(inputEdit, outputEdit, ".up");
QCOMPARE(outputEdit->toPlainText(), QString::fromLatin1("qsdb> .up\nAlready at top (outermost) frame."));
outputEdit->clear();
executeConsoleCommand(inputEdit, outputEdit, ".frame");
QCOMPARE(outputEdit->toPlainText(), QString::fromLatin1("qsdb> .frame\n#0 <global>() at -1"));
outputEdit->clear();
executeConsoleCommand(inputEdit, outputEdit, ".break foo.qs:789");
QCOMPARE(outputEdit->toPlainText(), QString::fromLatin1("qsdb> .break foo.qs:789\nBreakpoint 3: foo.qs, line 789."));
outputEdit->clear();
executeConsoleCommand(inputEdit, outputEdit, ".clear foo.qs:789");
QCOMPARE(outputEdit->toPlainText(), QString::fromLatin1("qsdb> .clear foo.qs:789\nDeleted breakpoint 3."));
outputEdit->clear();
executeConsoleCommand(inputEdit, outputEdit, ".info breakpoints");
QCOMPARE(outputEdit->toPlainText(), QString::fromLatin1("qsdb> .info breakpoints\nId\tEnabled\tWhere\n2\tyes\tbar.qs:456"));
outputEdit->clear();
executeConsoleCommand(inputEdit, outputEdit, ".info locals");
QVERIFY(outputEdit->toPlainText().startsWith("qsdb> .info locals\n"
"NaN : NaN\n"
"Infinity : Infinity\n"
"undefined : undefined\n"
"print : function () { [native] }\n"
"parseInt : function () { [native] }\n"
"parseFloat : function () { [native] }\n"
"isNaN : function () { [native] }\n"
"isFinite : function () { [native] }\n"
"decodeURI : function () { [native] }\n"
"decodeURIComponent : function () { [native] }\n"
"encodeURI : function () { [native] }\n"
"encodeURIComponent : function () { [native] }\n"
"escape : function () { [native] }\n"
"unescape : function () { [native] }\n"
"version : function () { [native] }\n"
"gc : function () { [native] }\n"
"Object : function () { [native] }\n"
"Function : function () { [native] }\n"
"Number : function () { [native] }\n"
"Boolean : function () { [native] }"));
outputEdit->clear();
QVERIFY(!engine.globalObject().property("a").isValid());
executeConsoleCommand(inputEdit, outputEdit, ".eval a = 123");
QVERIFY(engine.globalObject().property("a").isNumber());
QCOMPARE(engine.globalObject().property("a").toInt32(), 123);
outputEdit->clear();
QVERIFY(!engine.globalObject().property("b").isValid());
executeConsoleCommand(inputEdit, outputEdit, "b = 456");
QVERIFY(engine.globalObject().property("b").isNumber());
QCOMPARE(engine.globalObject().property("b").toInt32(), 456);
outputEdit->clear();
executeConsoleCommand(inputEdit, outputEdit, ".break myscript.qs:1");
QCOMPARE(outputEdit->toPlainText(), QString::fromLatin1("qsdb> .break myscript.qs:1\nBreakpoint 4: myscript.qs, line 1."));
{
DebuggerCommandExecutor executor(inputEdit, outputEdit, ".continue");
QObject::connect(&debugger, SIGNAL(evaluationSuspended()), &executor, SLOT(execute()), Qt::QueuedConnection);
outputEdit->clear();
engine.evaluate("void(123);", "myscript.qs");
QCOMPARE(outputEdit->toPlainText(), QString::fromLatin1("Breakpoint 4 at myscript.qs, line 1.\n1\tvoid(123);\nqsdb> .continue"));
}
{
DebuggerCommandExecutor executor(inputEdit, outputEdit, ".step");
QObject::connect(&debugger, SIGNAL(evaluationSuspended()), &executor, SLOT(execute()), Qt::QueuedConnection);
outputEdit->clear();
engine.evaluate("void(123);\nvoid(456);", "myscript.qs");
QCOMPARE(outputEdit->toPlainText(), QString::fromLatin1("Breakpoint 4 at myscript.qs, line 1.\n"
"1\tvoid(123);\n"
"qsdb> .step\n"
"2\tvoid(456);\n"
"qsdb> .step"));
}
{
DebuggerCommandExecutor executor(inputEdit, outputEdit, ".step 2");
QObject::connect(&debugger, SIGNAL(evaluationSuspended()), &executor, SLOT(execute()), Qt::QueuedConnection);
outputEdit->clear();
engine.evaluate("void(123);\nvoid(456);", "myscript.qs");
QCOMPARE(outputEdit->toPlainText(), QString::fromLatin1("Breakpoint 4 at myscript.qs, line 1.\n1\tvoid(123);\nqsdb> .step 2"));
}
{
DebuggerCommandExecutor executor(inputEdit, outputEdit, ".next");
QObject::connect(&debugger, SIGNAL(evaluationSuspended()), &executor, SLOT(execute()), Qt::QueuedConnection);
outputEdit->clear();
engine.evaluate("void(123);\nvoid(456);", "myscript.qs");
QCOMPARE(outputEdit->toPlainText(), QString::fromLatin1("Breakpoint 4 at myscript.qs, line 1.\n"
"1\tvoid(123);\n"
"qsdb> .next\n"
"2\tvoid(456);\n"
"qsdb> .next"));
}
{
DebuggerCommandExecutor executor(inputEdit, outputEdit, ".next 2");
QObject::connect(&debugger, SIGNAL(evaluationSuspended()), &executor, SLOT(execute()), Qt::QueuedConnection);
outputEdit->clear();
engine.evaluate("void(123);\nvoid(456);", "myscript.qs");
QCOMPARE(outputEdit->toPlainText(), QString::fromLatin1("Breakpoint 4 at myscript.qs, line 1.\n"
"1\tvoid(123);\n"
"qsdb> .next 2"));
}
{
DebuggerCommandExecutor executor(inputEdit, outputEdit, ".finish");
QObject::connect(&debugger, SIGNAL(evaluationSuspended()), &executor, SLOT(execute()), Qt::QueuedConnection);
outputEdit->clear();
engine.evaluate("void(123);\nvoid(456);", "myscript.qs");
QCOMPARE(outputEdit->toPlainText(), QString::fromLatin1("Breakpoint 4 at myscript.qs, line 1.\n1\tvoid(123);\nqsdb> .finish"));
}
{
DebuggerCommandExecutor executor(inputEdit, outputEdit, ".return");
QObject::connect(&debugger, SIGNAL(evaluationSuspended()), &executor, SLOT(execute()), Qt::QueuedConnection);
outputEdit->clear();
engine.evaluate("void(123);\nvoid(456);\n789;", "myscript.qs");
QCOMPARE(outputEdit->toPlainText(), QString::fromLatin1("Breakpoint 4 at myscript.qs, line 1.\n1\tvoid(123);\nqsdb> .return"));
}
{
DebuggerCommandExecutor executor(inputEdit, outputEdit, QStringList() << ".list" << ".continue");
QObject::connect(&debugger, SIGNAL(evaluationSuspended()), &executor, SLOT(execute()), Qt::QueuedConnection);
outputEdit->clear();
engine.evaluate("void(123);\nvoid(456);\n789;", "myscript.qs");
QCOMPARE(outputEdit->toPlainText(), QString::fromLatin1("Breakpoint 4 at myscript.qs, line 1.\n"
"1\tvoid(123);\n"
"qsdb> .list\n"
"1\tvoid(123);\n"
"2\tvoid(456);\n"
"3\t789;\n"
"4\n"
"5\n"
"6\n"
"7\n"
"8\n"
"9\n"
"10\n"
"qsdb> .continue"));
}
{
QString script;
script.append("function bar(i) {\n");
for (int i = 0; i < 10; ++i)
script.append(QString::fromLatin1(" i = i + %0;\n").arg(i));
script.append(" return i;\n}");
engine.evaluate(script, "bar.qs");
}
outputEdit->clear();
executeConsoleCommand(inputEdit, outputEdit, ".break bar.qs:7");
{
DebuggerCommandExecutor executor(inputEdit, outputEdit, QStringList()
<< ".list"
<< ".up"
<< ".list"
<< ".frame"
<< ".down"
<< ".list"
<< ".continue");
QObject::connect(&debugger, SIGNAL(evaluationSuspended()), &executor, SLOT(execute()), Qt::QueuedConnection);
outputEdit->clear();
engine.evaluate("bar(123);", "testscript.qs");
QCOMPARE(outputEdit->toPlainText(), QString::fromLatin1("Breakpoint 5 at bar.qs, line 7.\n"
"7\t i = i + 5;\n"
"qsdb> .list\n"
"2\t i = i + 0;\n"
"3\t i = i + 1;\n"
"4\t i = i + 2;\n"
"5\t i = i + 3;\n"
"6\t i = i + 4;\n"
"7\t i = i + 5;\n"
"8\t i = i + 6;\n"
"9\t i = i + 7;\n"
"10\t i = i + 8;\n"
"11\t i = i + 9;\n"
"qsdb> .up\n"
"#1 <global>()@testscript.qs:1\n"
"qsdb> .list\n"
"1\tbar(123);\n"
"2\n"
"3\n"
"4\n"
"5\n"
"6\n"
"7\n"
"8\n"
"9\n"
"10\n"
"qsdb> .frame\n"
"#1 <global>()@testscript.qs:1\n"
"qsdb> .down\n"
"#0 bar(123)@bar.qs:7\n"
"qsdb> .list\n"
"2\t i = i + 0;\n"
"3\t i = i + 1;\n"
"4\t i = i + 2;\n"
"5\t i = i + 3;\n"
"6\t i = i + 4;\n"
"7\t i = i + 5;\n"
"8\t i = i + 6;\n"
"9\t i = i + 7;\n"
"10\t i = i + 8;\n"
"11\t i = i + 9;\n"
"qsdb> .continue"));
}
{
DebuggerCommandExecutor executor(inputEdit, outputEdit, QStringList()
<< ".list 9"
<< ".continue");
QObject::connect(&debugger, SIGNAL(evaluationSuspended()), &executor, SLOT(execute()), Qt::QueuedConnection);
outputEdit->clear();
engine.evaluate("bar(123);", "testscript.qs");
QCOMPARE(outputEdit->toPlainText(), QString::fromLatin1("Breakpoint 5 at bar.qs, line 7.\n"
"7\t i = i + 5;\n"
"qsdb> .list 9\n"
"4\t i = i + 2;\n"
"5\t i = i + 3;\n"
"6\t i = i + 4;\n"
"7\t i = i + 5;\n"
"8\t i = i + 6;\n"
"9\t i = i + 7;\n"
"10\t i = i + 8;\n"
"11\t i = i + 9;\n"
"12\t return i;\n"
"13\t}\n"
"qsdb> .continue"));
}
}
class ScriptEvaluator : public QObject
{
Q_OBJECT
public:
ScriptEvaluator(QObject *parent = 0)
: QObject(parent) {
m_engine = new QScriptEngine(this);
}
QScriptEngine *engine() const {
return m_engine;
}
public Q_SLOTS:
QScriptValue evaluate(const QString &program) {
return m_engine->evaluate(program);
}
private:
QScriptEngine *m_engine;
};
void tst_QScriptEngineDebugger::multithreadedDebugging()
{
#ifdef Q_OS_WINCE
QSKIP("This tests uses too much memory for Windows CE", SkipAll);
#endif
ScriptEvaluator eval;
QThread thread;
eval.moveToThread(&thread);
eval.engine()->moveToThread(&thread);
QScriptEngineDebugger debugger;
QSignalSpy evaluationSuspendedSpy(&debugger, SIGNAL(evaluationSuspended()));
QSignalSpy evaluationResumedSpy(&debugger, SIGNAL(evaluationResumed()));
debugger.attachTo(eval.engine());
QMetaObject::invokeMethod(&eval, "evaluate", Qt::QueuedConnection, Q_ARG(QString, "debugger"));
QSignalSpy threadFinishedSpy(&thread, SIGNAL(finished()));
thread.start();
QTRY_COMPARE(evaluationSuspendedSpy.count(), 1);
QTRY_COMPARE(evaluationResumedSpy.count(), 0);
thread.quit();
QTRY_COMPARE(threadFinishedSpy.count(), 1);
}
QTEST_MAIN(tst_QScriptEngineDebugger)
#include "tst_qscriptenginedebugger.moc"
|