summaryrefslogtreecommitdiffstats
path: root/interpreter/builtins.js
blob: e8f6e10075a4ff7eb23f8e1916920c3e148615ee (plain)
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
/****************************************************************************
**
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of QtUiTest.
**
** $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$
**
****************************************************************************/

/*
    "Builtin" functions available to all tests.
    This file gets evaluated once at the beginning of every test.

    Documentation for these functions should be placed in qsystemtest.cpp.

    Note, if a function exists with the same name in both here and QSystemTest,
    the QtScript version takes precedence.  To force a call to the QSystemTest
    function, use ParentTestObject[signature], e.g. see expectMessageBox.

    Note that the exported function needs to be a public slot, AND have at least one
    parameter. See: runAsManualTest(void)
*/

/* called before every init() */
function init_global()
{
}

/* called after every cleanup() */
function cleanup_global()
{
}

/* called before every initTestCase() */
function initTestCase_global()
{
}

/* called after every cleanupTestCase() */
function cleanupTestCase_global()
{
    killApplication();
}

function verify() {
    if (arguments.length == 0) {
        fail( "You need to specify at least a boolean expression" );
        return false;
    }
    var msg = "";
    if (arguments.length > 1) { msg = arguments[1] };

    if (ParentTestObject['runAsManualTest(void)']()) {
        var description = "verify that MAGIC_DATA";
        if (msg.length > 0) description = "MAGIC_COMMAND:" + msg;
        ParentTestObject['manualTest(QString)'](description);
        return true;
    }

    return ParentTestObject['verify(bool,QString)'](arguments[0],msg);
}

function print() {
    var str = "";
    for (var i = 0; i < arguments.length; ++i) {
        if (arguments[i] != undefined) str = str + arguments[i];
    }
    ParentTestObject['print(QVariant)'](str);
}

/* Expect conditionFunction to return true after executing code, or fail with message */
function _expect(conditionFunction, message) {
    if (message == undefined) {
        message = "Expected condition did not become true";
    }
    this.__defineSetter__("code", function(code){
        try {
            code.apply(this);
        } catch (e) {
            if (this.onFail != undefined) {
                this.onFail.apply(this);
            }
            throw e;
        }
        if (!conditionFunction.call()) {
            fail(message);
        }
    });
}

function expect(a,b) {
    return new _expect(a,b);
}

function select( item, queryPath ) {
    if (item != undefined) {
        if (queryPath == undefined) queryPath = "";
        ParentTestObject['select(QString,QString)'](item, queryPath);
    }
    return true;
}

function selectIndex( idx, queryPath ) {
    if (idx instanceof Array) {
        ParentTestObject['selectIndex(QVariantList,QString)'](idx, queryPath);
    } else {
        ParentTestObject['selectIndex(QVariantList,QString)']([idx, 0], queryPath);
    }
}

function skip(text, mode) {
    ParentTestObject['skip(QString,QSystemTest::SkipMode)'](text, mode);
}

/*
    Wait for code to return true for up to timeout ms, with given number of intervals,
    and message on failure
*/
function _waitFor(timeout, intervals, message) {
    if (timeout == undefined) {
        timeout = 10000;
    }
    if (intervals == undefined) {
        intervals = 20;
    }
    if (message == undefined) {
        message = "Waited-for condition did not become true after " + timeout + " milliseconds";
    }
    this.__defineSetter__("code", function(code){
        ok = code.apply(this);
        i = 0;
        while (!ok && i < intervals) {
            wait(timeout/intervals);
            ok = code.apply(this);
            i = i + 1;
        }
        if (!ok) {
            fail(message);
        }
    });
}
function waitFor(a,b,c) {
    return new _waitFor(a,b,c);
}

Array.prototype.isEmpty = function()
{
    return (this.length <= 0);
}

Array.prototype.contains = function()
{
    obj = arguments[0];

    if (ParentTestObject['runAsManualTest(void)']()) {
        var description = " contains " + obj;
        ParentTestObject['manualTestData(QString)'](description);
        return true;
    }

    for (var i=0; i<this.length; i++) {
        if (this[i] == obj) {
            return true;
        }
    }
    return false;
}

// Ensure we don't get the 'contains' function when using for-in on arrays
setFlags(Array.prototype, "contains", QScriptValue.SkipInEnumeration);

Array.prototype.containsOne = function()
{
    obj = arguments[0];
    var count = 0;
    for (var i=0; i<this.length; i++) {
        if (this[i] == obj) {
            count++;
        }
    }
    return count == 1;
}
// Ensure we don't get the 'containsOne' function when using for-in on arrays
setFlags(Array.prototype, "containsOne", QScriptValue.SkipInEnumeration);

String.prototype.contains = function()
{
    var obj = arguments[0];
    if (arguments[1] != undefined) {
        obj = arguments[1];
    }

    if (obj instanceof Date) {
        var strs = [];
        strs.push(obj.toString("ddd, M/d/yy"));
        strs.push(obj.toString("dddd, MMMM d, yyyy"));
        strs.push(obj.toString("ddd, d MMM"));
        strs.push(obj.toString("dd/MM/yyyy"));

        if (ParentTestObject['runAsManualTest(void)']()) {
            var description = " contains date " + strs[i];
            ParentTestObject['manualTestData(QString)'](description);
            return true;
        }

        for (var i = 0; i < strs.length; ++i) {
            if (this.toString().contains(strs[i])) return true;
        }
        return false;
    } else if (obj instanceof RegExp) {
        if (ParentTestObject['runAsManualTest(void)']()) {
            var description = " contains " + obj;
            ParentTestObject['manualTestData(QString)'](description);
            return true;
        }

        return obj.test(this.toString());
    } else {
        if (ParentTestObject['runAsManualTest(void)']()) {
            var description = " contains " + obj;
            ParentTestObject['manualTestData(QString)'](description);
            return true;
        }

        var ref = this.toString();
        return (-1 != ref.indexOf(obj));
    }
}

String.prototype.toDate = function()
{
    var obj = this.toString();
    return toDate(obj);
}

String.prototype.startsWith = function()
{
    var obj = arguments[0];
    var ref = this.toString();
    return ref.indexOf( obj ) == 0;
}

String.prototype.left = function()
{
    var start = 0;
    var end = arguments[0];
    var ref = this.toString();
    return ref.slice( start, end );
}

String.prototype.right = function()
{
    var ref = this.toString();
    var start = ref.length - arguments[0];
    return ref.slice( start );
}

String.prototype.mid = function()
{
    var start = arguments[0];
    var end = arguments[1]+1;
    var ref = this.toString();
    return ref.slice( start, end );
}

String.prototype.toValue = function()
{
    var ret = 0;
    var ref = this.toString();
    for (var i=0; i<ref.length; i++) {
        var ch = ref.charAt(i);
        if (ch == '-') {
            ret = ret * -1;
        } else {
            ret = ret * 10;
            if (ch == '0') {
            } else if (ch == '1') {
                ret = ret + 1;
            } else if (ch == '2') {
                ret = ret + 2;
            } else if (ch == '3') {
                ret = ret + 3;
            } else if (ch == '4') {
                ret = ret + 4;
            } else if (ch == '5') {
                ret = ret + 5;
            } else if (ch == '6') {
                ret = ret + 6;
            } else if (ch == '7') {
                ret = ret + 7;
            } else if (ch == '8') {
                ret = ret + 8;
            } else if (ch == '9') {
                ret = ret + 9;
            } else {
                fail( "ERROR: Couldn't convert '" + ref + "' into a value." );
                return undefined;
            }
        }
    }
    return ret;
}

function anyDefined()
{
    for (var i = 0; i < arguments.length; ++i) {
        if (arguments[i] != undefined) return true;
    }
    return false;
}

function tabBar(index)
{
    if (index == undefined) {
        index = 0;
    }

    if (ParentTestObject['runAsManualTest(void)']()) {
        if (index == 0) {
            // In 99% of the cases there will be only one tabbar in an app
            // so we can get something like:
            // select 'foobar' from 'tabBar'
            return "tabBar";
        } else {
            // In the remaining 1% we get something like:
            // select 'foobar' from '2nd tabBar'
            return (index+1) + "nd tabBar";
        }
    }

    var tabBars = findByProperty( { inherits: "QTabBar" } );
    if (tabBars[index] == undefined) {
        fail( "ERROR: Tab widget not found." );
        return undefined;
    }
    return tabBars[index];
}

function menuBar(index)
{
    if (index == undefined) {
        index = 0;
    }

    if (ParentTestObject['runAsManualTest(void)']()) {
        if (index == 0) {
            // In 99% of the cases there will be only one menubar in an app
            // so we can get something like:
            // select 'foobar' from 'menuBar'
            return "menuBar";
        } else {
            // In the remaining 1% we get something like:
            // select 'foobar' from '2nd menuBar'
            return (index+1) + "nd menuBar";
        }
    }


    var menuBars = findByProperty( { inherits: "QMenuBar" } );
    if (menuBars[index] == undefined) {
        fail( "ERROR: MenuBar widget not found." );
        return undefined;
    }
    return menuBars[index];
}

// Convert twiki markup to HTML for display in prompts
function twiki(text) {
    var lines = text.split("\n");
    var result = [];
    var listType = [];
    var currLevel = 0;
    var rowNumber = 0;
    var inListItem = false;
    var inTable = false;
    var inVerbatim = false;
    var liRegExp = /((?:   )+)(\*|(?:[1AaIi]).)\s*(.*)/;
    var liContRegExp = /(?:   )\s*(.*)/;
    var tableRegExp = /\s*\|(\s*)(.*)(\s*)\|\s*$/;

    for (var i=0; i < lines.length; i++) {
        var line = lines[i];

        // Verbatim
        if (inVerbatim) {
            if (line.match(/\<\s*\/\s*verbatim\s*\>/)) {
                inVerbatim = false;
                result.push("</pre>");
            } else {
                result.push(line);
            }
            continue;
        } else if (line.match(/\<\s*verbatim\s*\>/)) {
            inVerbatim = true;
            result.push("<pre>");
            continue;
        }

        // Simple replacements
        // Headings
        line = line.replace(/---\+(?:!!)? +(.*)/, "<h1>$1</h1>");
        line = line.replace(/---\+\+(?:!!)? +(.*)/, "<h2>$1</h2>");
        line = line.replace(/---\+\+\+(?:!!)? +(.*)/, "<h3>$1</h3>");
        line = line.replace(/---\+\+\+\+(?:!!)? +(.*)/, "<h4>$1</h4>");
        line = line.replace(/---\+\+\+\+\+(?:!!)? +(.*)/, "<h5>$1</h5>");
        line = line.replace(/---\+\+\+\+\+\+(?:!!)? +(.*)/, "<h6>$1</h6>");

        // Misc
        line = line.replace(/---[-]*/g, "<hr />");
        line = line.replace(/^$/, "<p />");
        line = line.replace(/!([A-Z]\w*)/g, "$1");
        line = line.replace(/%TOC%/g, "");
        line = line.replace(/%BB%/g, "<br />&#8226;");

        // Links and anchors
        line = line.replace(/^\#([A-Z]\w*)/, "<a name=\"$1\"></a><p />");
        line = line.replace(/\[\[\#([A-Z]\w*)\]\[(.*)\]\]/g, "<a href=\"#$1\">$2</a>");
        line = line.replace(/\[\[\#([A-Z]\w*)\]\]/g, "<a href=\"#$1\">#$1</a>");

//        line = line.replace(/\[\[(.*?)\]\[(.*?)\]\]/g, "<a href=\"$1\">$2</a>");
//        line = line.replace(/\[\[(.*?)\]\]/g, "<a href=\"$1\">$1</a>");
        line = line.replace(/\[\[(.*?)\]\[(.*?)\]\]/g, "$2");
        line = line.replace(/\[\[(.*?)\]\]/g, "$1");

        // Text style
        line = line.replace(/^\*(\S.*?\S)\*(?!\w)/, "<b>$1</b>");
        line = line.replace(/([^\w])\*(\S.*?\S)\*(?!\w)/g, "$1<b>$2</b>");
        line = line.replace(/^__(.*?\S)__(?!\w)/g, "<b><i>$2</i></b>");
        line = line.replace(/([^\w])__(.*?\S)__(?!\w)/g, "$1<b><i>$2</i></b>");
        line = line.replace(/^_([^_]*?\S)_(?!\w)/, "<i>$1</i>");
        line = line.replace(/([^\w])_([^_]*?\S)_(?!\w)/g, "$1<i>$2</i>");
        line = line.replace(/^==(.*?\S)==(?!\w)/g, "<b><code>$1</code></b>");
        line = line.replace(/([^\w])==(.*?\S)==(?!\w)/g, "$1<b><code>$2</code></b>");
        line = line.replace(/^=(.*?\S)=(?!\w)/, "<code>$1</code>");
        line = line.replace(/([^\w])=(.*?\S)=(?!\w)/g, "$1<code>$2</code>");
        line = line.replace(/%(.*)%(.*)%ENDCOLOR%/g, "<font color=\"$1\">$2<\/font>");

        // Tables
        tableMatch = tableRegExp.exec(line);
        if (tableMatch) {
            if (inTable) {
                rowNumber++;
                if (rowNumber%2) {
                    result.push("</tr>\n<tr bgcolor=\"#f2f2f2\">");
                } else {
                    result.push("</tr>\n<tr bgcolor=\"#e0e0e0\">");
                }
            } else {
                inTable = true;
                rowNumber = 0;
                result.push("<table>\n<tr bgcolor=\"#e0e0e0\">");
            }

            var cellRegExp = /\|(\s*)(\<b\>)?(.*?)(\<\/b\>)?(\s*)(?=\|)/g;
            var trLine = "";
            while (cell = cellRegExp.exec(line)) {
                var left = cell[1].length;
                var right = cell[5].length;
                var align = "left";
                if (left > 1 && left == right) {
                    align = "center";
                } else if (left > right) {
                    align = "right";
                }
                if (cell[2] && cell[4]) {
                    trLine += "<th bgcolor=\"#404040\"><font color=\"white\">" + cell[3] + "</font></th>";
                } else {
                    trLine += "<td align=\"" + align + "\">" + cell[3] + "</td>";
                }
            }
            line = trLine;
            result.push(line);
            continue;
        } else {
            if (inTable) {
                result.push("</tr></table>");
                inTable = false;
            }
        }

        // List Items
        liMatch = liRegExp.exec(line);
        level = (liMatch ? liMatch[1].length/3 : 0);
        liType = (level ? (liMatch[2] == "*" ? "ul" : "ol") : null);

        // List item continuation
        if (inListItem) {
            if (!liMatch) {
                liContMatch = liContRegExp.exec(line);
                if (!liContMatch) {
                    inListItem = false;
                    result.push("</li>");
                } else {
                    line = liContMatch[1];
                    level = currLevel;
                }
            } else {
                inListItem = false;
                result.push("</li>");
            }
        }

        // Change of list indentation level
        if (level > currLevel) {
            for (var j = currLevel; j < level; j++) {
                result.push("<" + liType + (liType == "ol" ? " type=\"" + liMatch[2][0] + "\"" : "") + ">");
                listType.push(liType);
            }
        } else {
            for (var j = level; j < currLevel; j++) {
                prevLiType = listType.pop();
                result.push("</" + prevLiType + ">");
            }
            if (liMatch && listType.length && listType[listType.length - 1] != liType) {
                // Change of list item type (from unordered to ordered, or vice-versa)
                result.push("</" + listType.pop() + ">");
                result.push("<" + liType + (liType == "ol" ? " type=\"" + liMatch[2][0] + "\"" : "") + ">");
                listType.push(liType);
            }
        }
        currLevel = level;

        if (liMatch) {
            line = line.replace(liRegExp, "<li" + (liType == "ol" ? " type=\"" + liMatch[2][0] + "\"" : "") + ">" + liMatch[3]);
            inListItem = true;
        }

        result.push(line);
    }

    return(result.join("\n"));
}

// Support object-oriented syntax for widget interaction
String.prototype.focusWidget = function() { return focusWidget(this); }
String.prototype.getSelectedText = function() { return getSelectedText(this); }
String.prototype.getText = function() { return getText(this); }
String.prototype.getSelectedValue = function() { return getSelectedValue(this); }
String.prototype.getValue = function() { return getValue(this); }
String.prototype.getList = function() { return getList(this); }
String.prototype.getLabels = function() { return getLabels(this); }
String.prototype.currentTitle = function() { return currentTitle(this); }
String.prototype.getGeometry = function() { return getGeometry(this); }
String.prototype.setProperty = function(name, value) { setProperty(this, name, value); }
String.prototype.getProperty = function(name) { return getProperty(this, name); }
String.prototype.keyPress = function(key) { keyPress(key, this); }
String.prototype.keyRelease = function(key) { keyRelease(key, this); }
String.prototype.keyClick = function(key) { keyClick(key, this); }
String.prototype.keyClickHold = function(key, duration) { keyClickHold(key, duration, this); }
String.prototype.mouseClick = function() { mouseClick(this); }
String.prototype.mouseClickHold = function(duration) { mouseClickHold(this, duration); }
String.prototype.mousePress = function() { mousePress(this); }
String.prototype.mouseRelease = function() { mouseRelease(this); }
String.prototype.enter = function(value, mode) { enter(value, this, mode) }
String.prototype.select = function(item) { select(item, this); }
String.prototype.selectIndex = function(idx) { selectIndex(idx, this); }
String.prototype.activate = function() { activate(this); }
String.prototype.isVisible = function() { return isVisible(this); }
String.prototype.isEnabled = function() { return isEnabled(this); }
String.prototype.isChecked = function() { return isChecked(this); }
String.prototype.setChecked = function(doCheck) { setChecked(doCheck, this); }
String.prototype.checkState = function() { return checkState(this); }
String.prototype.setCheckState = function(state) { setCheckState(state, this); }
String.prototype.saveScreen = function(name) { saveScreen(name, this); }