Initial import from the monolithic Qt.
[profile/ivi/qtdeclarative.git] / tests / auto / declarative / qdeclarativeinstruction / tst_qdeclarativeinstruction.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
6 **
7 ** This file is part of the test suite of the Qt Toolkit.
8 **
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** No Commercial Usage
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
14 ** this package.
15 **
16 ** GNU Lesser General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU Lesser
18 ** General Public License version 2.1 as published by the Free Software
19 ** Foundation and appearing in the file LICENSE.LGPL included in the
20 ** packaging of this file.  Please review the following information to
21 ** ensure the GNU Lesser General Public License version 2.1 requirements
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23 **
24 ** In addition, as a special exception, Nokia gives you certain additional
25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
27 **
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
30 **
31 **
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include <qtest.h>
43 #include <private/qdeclarativecompiler_p.h>
44
45 #ifdef Q_OS_SYMBIAN
46 // In Symbian OS test data is located in applications private dir
47 #define SRCDIR "."
48 #endif
49
50 class tst_qdeclarativeinstruction : public QObject
51 {
52     Q_OBJECT
53 public:
54     tst_qdeclarativeinstruction() {}
55
56 private slots:
57     void dump();
58 };
59
60 static QStringList messages;
61 static void msgHandler(QtMsgType, const char *msg)
62 {
63     messages << QLatin1String(msg);
64 }
65
66 void tst_qdeclarativeinstruction::dump()
67 {
68     QDeclarativeCompiledData *data = new QDeclarativeCompiledData(0);
69     {
70         QDeclarativeInstruction i;
71         i.line = 0;
72         i.type = QDeclarativeInstruction::Init;
73         i.init.bindingsSize = 0;
74         i.init.parserStatusSize = 3;
75         i.init.contextCache = -1;
76         i.init.compiledBinding = -1;
77         data->bytecode << i;
78     }
79
80     {
81         QDeclarativeCompiledData::TypeReference ref;
82         ref.className = "Test";
83         data->types << ref;
84
85         QDeclarativeInstruction i;
86         i.line = 1;
87         i.type = QDeclarativeInstruction::CreateObject;
88         i.create.type = 0;
89         i.create.data = -1;
90         i.create.bindingBits = -1;
91         i.create.column = 10;
92         data->bytecode << i;
93     }
94
95     {
96         data->primitives << "testId";
97
98         QDeclarativeInstruction i;
99         i.line = 2;
100         i.type = QDeclarativeInstruction::SetId;
101         i.setId.value = data->primitives.count() - 1;
102         i.setId.index = 0;
103         data->bytecode << i;
104     }
105
106     {
107         QDeclarativeInstruction i;
108         i.line = 3;
109         i.type = QDeclarativeInstruction::SetDefault;
110         data->bytecode << i;
111     }
112
113     {
114         QDeclarativeInstruction i;
115         i.line = 4;
116         i.type = QDeclarativeInstruction::CreateComponent;
117         i.createComponent.count = 3;
118         i.createComponent.column = 4;
119         i.createComponent.endLine = 14;
120         i.createComponent.metaObject = 0;
121
122         data->bytecode << i;
123     }
124
125     {
126         QDeclarativeInstruction i;
127         i.line = 5;
128         i.type = QDeclarativeInstruction::StoreMetaObject;
129         i.storeMeta.data = 3;
130         i.storeMeta.aliasData = 6;
131         i.storeMeta.propertyCache = 7;
132
133         data->bytecode << i;
134     }
135
136     {
137         QDeclarativeInstruction i;
138         i.line = 6;
139         i.type = QDeclarativeInstruction::StoreFloat;
140         i.storeFloat.propertyIndex = 3;
141         i.storeFloat.value = 11.3;
142         data->bytecode << i;
143     }
144
145     {
146         QDeclarativeInstruction i;
147         i.line = 7;
148         i.type = QDeclarativeInstruction::StoreDouble;
149         i.storeDouble.propertyIndex = 4;
150         i.storeDouble.value = 14.8;
151         data->bytecode << i;
152     }
153
154     {
155         QDeclarativeInstruction i;
156         i.line = 8;
157         i.type = QDeclarativeInstruction::StoreInteger;
158         i.storeInteger.propertyIndex = 5;
159         i.storeInteger.value = 9;
160         data->bytecode << i;
161     }
162
163     {
164         QDeclarativeInstruction i;
165         i.line = 9;
166         i.type = QDeclarativeInstruction::StoreBool;
167         i.storeBool.propertyIndex = 6;
168         i.storeBool.value = true;
169
170         data->bytecode << i;
171     }
172
173     {
174         data->primitives << "Test String";
175         QDeclarativeInstruction i;
176         i.line = 10;
177         i.type = QDeclarativeInstruction::StoreString;
178         i.storeString.propertyIndex = 7;
179         i.storeString.value = data->primitives.count() - 1;
180         data->bytecode << i;
181     }
182
183     {
184         data->urls << QUrl("http://www.nokia.com");
185         QDeclarativeInstruction i;
186         i.line = 11;
187         i.type = QDeclarativeInstruction::StoreUrl;
188         i.storeUrl.propertyIndex = 8;
189         i.storeUrl.value = data->urls.count() - 1;
190         data->bytecode << i;
191     }
192
193     {
194         QDeclarativeInstruction i;
195         i.line = 12;
196         i.type = QDeclarativeInstruction::StoreColor;
197         i.storeColor.propertyIndex = 9;
198         i.storeColor.value = 0xFF00FF00;
199         data->bytecode << i;
200     }
201
202     {
203         QDeclarativeInstruction i;
204         i.line = 13;
205         i.type = QDeclarativeInstruction::StoreDate;
206         i.storeDate.propertyIndex = 10;
207         i.storeDate.value = 9;
208         data->bytecode << i;
209     }
210
211     {
212         QDeclarativeInstruction i;
213         i.line = 14;
214         i.type = QDeclarativeInstruction::StoreTime;
215         i.storeTime.propertyIndex = 11;
216         i.storeTime.valueIndex = 33;
217         data->bytecode << i;
218     }
219
220     {
221         QDeclarativeInstruction i;
222         i.line = 15;
223         i.type = QDeclarativeInstruction::StoreDateTime;
224         i.storeDateTime.propertyIndex = 12;
225         i.storeDateTime.valueIndex = 44;
226         data->bytecode << i;
227     }
228
229     {
230         QDeclarativeInstruction i;
231         i.line = 16;
232         i.type = QDeclarativeInstruction::StorePoint;
233         i.storeRealPair.propertyIndex = 13;
234         i.storeRealPair.valueIndex = 3;
235         data->bytecode << i;
236     }
237
238     {
239         QDeclarativeInstruction i;
240         i.line = 17;
241         i.type = QDeclarativeInstruction::StorePointF;
242         i.storeRealPair.propertyIndex = 14;
243         i.storeRealPair.valueIndex = 9;
244         data->bytecode << i;
245     }
246
247     {
248         QDeclarativeInstruction i;
249         i.line = 18;
250         i.type = QDeclarativeInstruction::StoreSize;
251         i.storeRealPair.propertyIndex = 15;
252         i.storeRealPair.valueIndex = 8;
253         data->bytecode << i;
254     }
255
256     {
257         QDeclarativeInstruction i;
258         i.line = 19;
259         i.type = QDeclarativeInstruction::StoreSizeF;
260         i.storeRealPair.propertyIndex = 16;
261         i.storeRealPair.valueIndex = 99;
262         data->bytecode << i;
263     }
264
265     {
266         QDeclarativeInstruction i;
267         i.line = 20;
268         i.type = QDeclarativeInstruction::StoreRect;
269         i.storeRect.propertyIndex = 17;
270         i.storeRect.valueIndex = 2;
271         data->bytecode << i;
272     }
273
274     {
275         QDeclarativeInstruction i;
276         i.line = 21;
277         i.type = QDeclarativeInstruction::StoreRectF;
278         i.storeRect.propertyIndex = 18;
279         i.storeRect.valueIndex = 19;
280         data->bytecode << i;
281     }
282
283     {
284         QDeclarativeInstruction i;
285         i.line = 22;
286         i.type = QDeclarativeInstruction::StoreVector3D;
287         i.storeVector3D.propertyIndex = 19;
288         i.storeVector3D.valueIndex = 9;
289         data->bytecode << i;
290     }
291
292     {
293         data->primitives << "color(1, 1, 1, 1)";
294         QDeclarativeInstruction i;
295         i.line = 23;
296         i.type = QDeclarativeInstruction::StoreVariant;
297         i.storeString.propertyIndex = 20;
298         i.storeString.value = data->primitives.count() - 1;
299
300         data->bytecode << i;
301     }
302
303     {
304         QDeclarativeInstruction i;
305         i.line = 24;
306         i.type = QDeclarativeInstruction::StoreObject;
307         i.storeObject.propertyIndex = 21;
308         data->bytecode << i;
309     }
310
311     {
312         QDeclarativeInstruction i;
313         i.line = 25;
314         i.type = QDeclarativeInstruction::StoreVariantObject;
315         i.storeObject.propertyIndex = 22;
316         data->bytecode << i;
317     }
318
319     {
320         QDeclarativeInstruction i;
321         i.line = 26;
322         i.type = QDeclarativeInstruction::StoreInterface;
323         i.storeObject.propertyIndex = 23;
324         data->bytecode << i;
325     }
326
327     {
328         data->primitives << "console.log(1921)";
329
330         QDeclarativeInstruction i;
331         i.line = 27;
332         i.type = QDeclarativeInstruction::StoreSignal;
333         i.storeSignal.signalIndex = 2;
334         i.storeSignal.value = data->primitives.count() - 1;
335         data->bytecode << i;
336     }
337
338     {
339         QDeclarativeInstruction i;
340         i.line = 29;
341         i.type = QDeclarativeInstruction::StoreScriptString;
342         i.storeScriptString.propertyIndex = 24;
343         i.storeScriptString.value = 3;
344         i.storeScriptString.scope = 1;
345         data->bytecode << i;
346     }
347
348     {
349         data->datas << "mySignal";
350
351         QDeclarativeInstruction i;
352         i.line = 30;
353         i.type = QDeclarativeInstruction::AssignSignalObject;
354         i.assignSignalObject.signal = 0;
355         data->bytecode << i;
356     }
357
358     {
359         QDeclarativeInstruction i;
360         i.line = 31;
361         i.type = QDeclarativeInstruction::AssignCustomType;
362         i.assignCustomType.propertyIndex = 25;
363         i.assignCustomType.valueIndex = 4;
364         data->bytecode << i;
365     }
366
367     {
368         QDeclarativeInstruction i;
369         i.line = 32;
370         i.type = QDeclarativeInstruction::StoreBinding;
371         i.assignBinding.property = 26;
372         i.assignBinding.value = 3;
373         i.assignBinding.context = 2;
374         i.assignBinding.owner = 0;
375         data->bytecode << i;
376     }
377
378     {
379         QDeclarativeInstruction i;
380         i.line = 33;
381         i.type = QDeclarativeInstruction::StoreCompiledBinding;
382         i.assignBinding.property = 27;
383         i.assignBinding.value = 2;
384         i.assignBinding.context = 4;
385         i.assignBinding.owner = 0;
386         data->bytecode << i;
387     }
388
389     {
390         QDeclarativeInstruction i;
391         i.line = 34;
392         i.type = QDeclarativeInstruction::StoreValueSource;
393         i.assignValueSource.property = 29;
394         i.assignValueSource.owner = 1;
395         i.assignValueSource.castValue = 4;
396         data->bytecode << i;
397     }
398
399     {
400         QDeclarativeInstruction i;
401         i.line = 35;
402         i.type = QDeclarativeInstruction::StoreValueInterceptor;
403         i.assignValueInterceptor.property = 30;
404         i.assignValueInterceptor.owner = 2;
405         i.assignValueInterceptor.castValue = -4;
406         data->bytecode << i;
407     }
408
409     {
410         QDeclarativeInstruction i;
411         i.line = 36;
412         i.type = QDeclarativeInstruction::BeginObject;
413         i.begin.castValue = 4;
414         data->bytecode << i;
415     }
416
417     {
418         QDeclarativeInstruction i;
419         i.line = 38;
420         i.type = QDeclarativeInstruction::StoreObjectQList;
421         data->bytecode << i;
422     }
423
424     {
425         QDeclarativeInstruction i;
426         i.line = 39;
427         i.type = QDeclarativeInstruction::AssignObjectList;
428         data->bytecode << i;
429     }
430
431     {
432         QDeclarativeInstruction i;
433         i.line = 40;
434         i.type = QDeclarativeInstruction::FetchAttached;
435         i.fetchAttached.id = 23;
436         data->bytecode << i;
437     }
438
439     {
440         QDeclarativeInstruction i;
441         i.line = 42;
442         i.type = QDeclarativeInstruction::FetchQList;
443         i.fetch.property = 32;
444         data->bytecode << i;
445     }
446
447     {
448         QDeclarativeInstruction i;
449         i.line = 43;
450         i.type = QDeclarativeInstruction::FetchObject;
451         i.fetch.property = 33;
452         data->bytecode << i;
453     }
454
455     {
456         QDeclarativeInstruction i;
457         i.line = 44;
458         i.type = QDeclarativeInstruction::FetchValueType;
459         i.fetchValue.property = 34;
460         i.fetchValue.type = 6;
461         i.fetchValue.bindingSkipList = 7;
462         data->bytecode << i;
463     }
464
465     {
466         QDeclarativeInstruction i;
467         i.line = 45;
468         i.type = QDeclarativeInstruction::PopFetchedObject;
469         data->bytecode << i;
470     }
471
472     {
473         QDeclarativeInstruction i;
474         i.line = 46;
475         i.type = QDeclarativeInstruction::PopQList;
476         data->bytecode << i;
477     }
478
479     {
480         QDeclarativeInstruction i;
481         i.line = 47;
482         i.type = QDeclarativeInstruction::PopValueType;
483         i.fetchValue.property = 35;
484         i.fetchValue.type = 8;
485         data->bytecode << i;
486     }
487
488     {
489         QDeclarativeInstruction i;
490         i.line = 48;
491         i.type = QDeclarativeInstruction::Defer;
492         i.defer.deferCount = 7;
493         data->bytecode << i;
494     }
495
496     {
497         QDeclarativeInstruction i;
498         i.line = -1;
499         i.type = QDeclarativeInstruction::Defer;
500         i.defer.deferCount = 7;
501         data->bytecode << i;
502     }
503
504     {
505         QDeclarativeInstruction i;
506         i.line = 48;
507         i.type = QDeclarativeInstruction::StoreImportedScript;
508         i.storeScript.value = 2;
509         data->bytecode << i;
510     }
511
512     {
513         QDeclarativeInstruction i;
514         i.line = 50;
515         i.type = (QDeclarativeInstruction::Type)(1234); // Non-existent
516         data->bytecode << i;
517     }
518
519     {
520         QDeclarativeInstruction i;
521         i.line = 51;
522         i.type = QDeclarativeInstruction::StoreVariantInteger;
523         i.storeInteger.value = 11;
524         i.storeInteger.propertyIndex = 32;
525         data->bytecode << i;
526     }
527
528     {
529         QDeclarativeInstruction i;
530         i.line = 52;
531         i.type = QDeclarativeInstruction::StoreVariantDouble;
532         i.storeDouble.value = 33.7;
533         i.storeDouble.propertyIndex = 19;
534         data->bytecode << i;
535     }
536
537     QStringList expect;
538     expect 
539         << "Index\tLine\tOperation\t\tData1\tData2\tData3\tComments"
540         << "-------------------------------------------------------------------------------"
541         << "0\t\t0\tINIT\t\t\t0\t3\t-1\t-1"
542         << "1\t\t1\tCREATE\t\t\t0\t-1\t\t\"Test\""
543         << "2\t\t2\tSETID\t\t\t0\t\t\t\"testId\""
544         << "3\t\t3\tSET_DEFAULT"
545         << "4\t\t4\tCREATE_COMPONENT\t3"
546         << "5\t\t5\tSTORE_META\t\t3"
547         << "6\t\t6\tSTORE_FLOAT\t\t3\t11.3"
548         << "7\t\t7\tSTORE_DOUBLE\t\t4\t14.8"
549         << "8\t\t8\tSTORE_INTEGER\t\t5\t9"
550         << "9\t\t9\tSTORE_BOOL\t\t6\ttrue"
551         << "10\t\t10\tSTORE_STRING\t\t7\t1\t\t\"Test String\""
552         << "11\t\t11\tSTORE_URL\t\t8\t0\t\tQUrl(\"http://www.nokia.com\") "
553         << "12\t\t12\tSTORE_COLOR\t\t9\t\t\t\"ff00ff00\""
554         << "13\t\t13\tSTORE_DATE\t\t10\t9"
555         << "14\t\t14\tSTORE_TIME\t\t11\t33"
556         << "15\t\t15\tSTORE_DATETIME\t\t12\t44"
557         << "16\t\t16\tSTORE_POINT\t\t13\t3"
558         << "17\t\t17\tSTORE_POINTF\t\t14\t9"
559         << "18\t\t18\tSTORE_SIZE\t\t15\t8"
560         << "19\t\t19\tSTORE_SIZEF\t\t16\t99"
561         << "20\t\t20\tSTORE_RECT\t\t17\t2"
562         << "21\t\t21\tSTORE_RECTF\t\t18\t19"
563         << "22\t\t22\tSTORE_VECTOR3D\t\t19\t9"
564         << "23\t\t23\tSTORE_VARIANT\t\t20\t2\t\t\"color(1, 1, 1, 1)\""
565         << "24\t\t24\tSTORE_OBJECT\t\t21"
566         << "25\t\t25\tSTORE_VARIANT_OBJECT\t22"
567         << "26\t\t26\tSTORE_INTERFACE\t\t23"
568         << "27\t\t27\tSTORE_SIGNAL\t\t2\t3\t\t\"console.log(1921)\""
569         << "28\t\t29\tSTORE_SCRIPT_STRING\t24\t3\t1"
570         << "29\t\t30\tASSIGN_SIGNAL_OBJECT\t0\t\t\t\"mySignal\""
571         << "30\t\t31\tASSIGN_CUSTOMTYPE\t25\t4"
572         << "31\t\t32\tSTORE_BINDING\t26\t3\t2"
573         << "32\t\t33\tSTORE_COMPILED_BINDING\t27\t2\t4"
574         << "33\t\t34\tSTORE_VALUE_SOURCE\t29\t4"
575         << "34\t\t35\tSTORE_VALUE_INTERCEPTOR\t30\t-4"
576         << "35\t\t36\tBEGIN\t\t\t4"
577         << "36\t\t38\tSTORE_OBJECT_QLIST"
578         << "37\t\t39\tASSIGN_OBJECT_LIST"
579         << "38\t\t40\tFETCH_ATTACHED\t\t23"
580         << "39\t\t42\tFETCH_QLIST\t\t32"
581         << "40\t\t43\tFETCH\t\t\t33"
582         << "41\t\t44\tFETCH_VALUE\t\t34\t6\t7"
583         << "42\t\t45\tPOP"
584         << "43\t\t46\tPOP_QLIST"
585         << "44\t\t47\tPOP_VALUE\t\t35\t8"
586         << "45\t\t48\tDEFER\t\t\t7"
587         << "46\t\tNA\tDEFER\t\t\t7"
588         << "47\t\t48\tSTORE_IMPORTED_SCRIPT\t2"
589         << "48\t\t50\tXXX UNKNOWN INSTRUCTION\t1234"
590         << "49\t\t51\tSTORE_VARIANT_INTEGER\t\t32\t11"
591         << "50\t\t52\tSTORE_VARIANT_DOUBLE\t\t19\t33.7"
592         << "-------------------------------------------------------------------------------";
593
594     messages = QStringList();
595     QtMsgHandler old = qInstallMsgHandler(msgHandler);
596     data->dumpInstructions();
597     qInstallMsgHandler(old);
598
599     QCOMPARE(messages.count(), expect.count());
600     for (int ii = 0; ii < messages.count(); ++ii) {
601         QCOMPARE(messages.at(ii), expect.at(ii));
602     }
603
604     data->release();
605 }
606
607 QTEST_MAIN(tst_qdeclarativeinstruction)
608
609 #include "tst_qdeclarativeinstruction.moc"