OSDN Git Service

fe3ff4b46572cec8279dff97a19166235de0a29c
[qt-creator-jp/qt-creator-jp.git] / share / qtcreator / qml / qmlobserver / qdeclarativetester.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 tools applications 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 <qdeclarativetester.h>
43 #include <QtDeclarative/qdeclarativeview.h>
44 #include <QtDeclarative/QDeclarativeComponent>
45 #include <QtGui/QGraphicsObject>
46 #include <QtGui/QApplication>
47 #include <QtCore/QDebug>
48 #include <QtCore/QFile>
49 #include <QtCore/QDir>
50 #include <QtCore/QCryptographicHash>
51
52 #ifndef NO_PRIVATE_HEADERS
53 #include <private/qabstractanimation_p.h>
54 #include <private/qdeclarativeitem_p.h>
55 #endif
56
57 QT_BEGIN_NAMESPACE
58
59 extern Q_GUI_EXPORT bool qt_applefontsmoothing_enabled;
60
61 QDeclarativeTester::QDeclarativeTester(const QString &script, QDeclarativeViewer::ScriptOptions opts,
62                      QDeclarativeView *parent)
63 : QAbstractAnimation(parent), m_script(script), m_view(parent), filterEvents(true), options(opts),
64   testscript(0), hasCompleted(false), hasFailed(false)
65 {
66     parent->viewport()->installEventFilter(this);
67     parent->installEventFilter(this);
68 #ifndef NO_PRIVATE_HEADERS
69     QUnifiedTimer::instance()->setConsistentTiming(true);
70 #endif
71
72     //Font antialiasing makes tests system-specific, so disable it
73     QFont noAA = QApplication::font();
74     noAA.setStyleStrategy(QFont::NoAntialias);
75     QApplication::setFont(noAA);
76
77     if (options & QDeclarativeViewer::Play)
78         this->run();
79     start();
80 }
81
82 QDeclarativeTester::~QDeclarativeTester()
83 {
84     if (!hasFailed &&
85         options & QDeclarativeViewer::Record &&
86         options & QDeclarativeViewer::SaveOnExit)
87         save();
88 }
89
90 int QDeclarativeTester::duration() const
91 {
92     return -1;
93 }
94
95 void QDeclarativeTester::addMouseEvent(Destination dest, QMouseEvent *me)
96 {
97     MouseEvent e(me);
98     e.destination = dest;
99     m_mouseEvents << e;
100 }
101
102 void QDeclarativeTester::addKeyEvent(Destination dest, QKeyEvent *ke)
103 {
104     KeyEvent e(ke);
105     e.destination = dest;
106     m_keyEvents << e;
107 }
108
109 bool QDeclarativeTester::eventFilter(QObject *o, QEvent *e)
110 {
111     if (!filterEvents)
112         return false;
113
114     Destination destination;
115     if (o == m_view) {
116         destination = View;
117     } else if (o == m_view->viewport()) {
118         destination = ViewPort;
119     } else {
120         return false;
121     }
122
123     switch (e->type()) {
124         case QEvent::KeyPress:
125         case QEvent::KeyRelease:
126             addKeyEvent(destination, (QKeyEvent *)e);
127             return true;
128         case QEvent::MouseButtonPress:
129         case QEvent::MouseButtonRelease:
130         case QEvent::MouseMove:
131         case QEvent::MouseButtonDblClick:
132             addMouseEvent(destination, (QMouseEvent *)e);
133             return true;
134         default:
135             break;
136     }
137     return false;
138 }
139
140 void QDeclarativeTester::executefailure()
141 {
142     hasFailed = true;
143
144     if (options & QDeclarativeViewer::ExitOnFailure)
145         exit(-1);
146 }
147
148 void QDeclarativeTester::imagefailure()
149 {
150     hasFailed = true;
151
152     if (options & QDeclarativeViewer::ExitOnFailure){
153         testSkip();
154         exit(hasFailed?-1:0);
155     }
156 }
157
158 void QDeclarativeTester::testSkip()
159 {
160     if (options & QDeclarativeViewer::TestSkipProperty){
161         QString e = m_view->rootObject()->property("skip").toString();
162         if (!e.isEmpty()) {
163             if(hasFailed){
164                 qWarning() << "Test failed, but skipping it: " << e;
165             }else{
166                 qWarning() << "Test skipped: " << e;
167             }
168             hasFailed = 0;
169         }
170     }
171 }
172
173 void QDeclarativeTester::complete()
174 {
175     if ((options & QDeclarativeViewer::TestErrorProperty) && !hasFailed) {
176         QString e = m_view->rootObject()->property("error").toString();
177         if (!e.isEmpty()) {
178             qWarning() << "Test failed:" << e;
179             hasFailed = true;
180         }
181     }
182
183
184     testSkip();
185     if (options & QDeclarativeViewer::ExitOnComplete)
186         QApplication::exit(hasFailed?-1:0);
187
188     if (hasCompleted)
189         return;
190     hasCompleted = true;
191
192     if (options & QDeclarativeViewer::Play)
193         qWarning("Script playback complete");
194 }
195
196 void QDeclarativeTester::run()
197 {
198     QDeclarativeComponent c(m_view->engine(), m_script + QLatin1String(".qml"));
199
200     testscript = qobject_cast<QDeclarativeVisualTest *>(c.create());
201     if (testscript) testscript->setParent(this);
202     else { executefailure(); exit(-1); }
203     testscriptidx = 0;
204 }
205
206 void QDeclarativeTester::save()
207 {
208     QString filename = m_script + QLatin1String(".qml");
209     QFileInfo filenameInfo(filename);
210     QDir saveDir = filenameInfo.absoluteDir();
211     saveDir.mkpath(".");
212
213     QFile file(filename);
214     file.open(QIODevice::WriteOnly);
215     QTextStream ts(&file);
216
217     ts << "import Qt.VisualTest 4.7\n\n";
218     ts << "VisualTest {\n";
219
220     int imgCount = 0;
221     QList<KeyEvent> keyevents = m_savedKeyEvents;
222     QList<MouseEvent> mouseevents = m_savedMouseEvents;
223     for (int ii = 0; ii < m_savedFrameEvents.count(); ++ii) {
224         const FrameEvent &fe = m_savedFrameEvents.at(ii);
225         ts << "    Frame {\n";
226         ts << "        msec: " << fe.msec << "\n";
227         if (!fe.hash.isEmpty()) {
228             ts << "        hash: \"" << fe.hash.toHex() << "\"\n";
229         } else if (!fe.image.isNull()) {
230             QString filename = filenameInfo.baseName() + "." + QString::number(imgCount) + ".png";
231             fe.image.save(m_script + "." + QString::number(imgCount) + ".png");
232             imgCount++;
233             ts << "        image: \"" << filename << "\"\n";
234         }
235         ts << "    }\n";
236
237         while (!mouseevents.isEmpty() &&
238                mouseevents.first().msec == fe.msec) {
239             MouseEvent me = mouseevents.takeFirst();
240
241             ts << "    Mouse {\n";
242             ts << "        type: " << me.type << "\n";
243             ts << "        button: " << me.button << "\n";
244             ts << "        buttons: " << me.buttons << "\n";
245             ts << "        x: " << me.pos.x() << "; y: " << me.pos.y() << "\n";
246             ts << "        modifiers: " << me.modifiers << "\n";
247             if (me.destination == ViewPort)
248                 ts << "        sendToViewport: true\n";
249             ts << "    }\n";
250         }
251
252         while (!keyevents.isEmpty() &&
253                keyevents.first().msec == fe.msec) {
254             KeyEvent ke = keyevents.takeFirst();
255
256             ts << "    Key {\n";
257             ts << "        type: " << ke.type << "\n";
258             ts << "        key: " << ke.key << "\n";
259             ts << "        modifiers: " << ke.modifiers << "\n";
260             ts << "        text: \"" << ke.text.toUtf8().toHex() << "\"\n";
261             ts << "        autorep: " << (ke.autorep?"true":"false") << "\n";
262             ts << "        count: " << ke.count << "\n";
263             if (ke.destination == ViewPort)
264                 ts << "        sendToViewport: true\n";
265             ts << "    }\n";
266         }
267     }
268
269     ts << "}\n";
270     file.close();
271 }
272
273 void QDeclarativeTester::updateCurrentTime(int msec)
274 {
275 #ifndef NO_PRIVATE_HEADERS
276     QDeclarativeItemPrivate::setConsistentTime(msec);
277 #endif
278     if (!testscript && msec > 16 && options & QDeclarativeViewer::Snapshot)
279         return;
280
281     QImage img(m_view->width(), m_view->height(), QImage::Format_RGB32);
282
283     if (options & QDeclarativeViewer::TestImages) {
284         img.fill(qRgb(255,255,255));
285
286 #ifdef Q_WS_MAC
287         bool oldSmooth = qt_applefontsmoothing_enabled;
288         qt_applefontsmoothing_enabled = false;
289 #endif
290         QPainter p(&img);
291 #ifdef Q_WS_MAC
292         qt_applefontsmoothing_enabled = oldSmooth;
293 #endif
294
295         m_view->render(&p);
296     }
297
298     bool snapshot = msec == 16 && (options & QDeclarativeViewer::Snapshot
299                                    || (testscript && testscript->count() == 2));
300
301     FrameEvent fe;
302     fe.msec = msec;
303     if (msec == 0 || !(options & QDeclarativeViewer::TestImages)) {
304         // Skip first frame, skip if not doing images
305     } else if (0 == ((m_savedFrameEvents.count()-1) % 60) || snapshot) {
306         fe.image = img;
307     } else {
308         QCryptographicHash hash(QCryptographicHash::Md5);
309         hash.addData((const char *)img.bits(), img.bytesPerLine() * img.height());
310         fe.hash = hash.result();
311     }
312     m_savedFrameEvents.append(fe);
313
314     // Deliver mouse events
315     filterEvents = false;
316
317     if (!testscript) {
318         for (int ii = 0; ii < m_mouseEvents.count(); ++ii) {
319             MouseEvent &me = m_mouseEvents[ii];
320             me.msec = msec;
321             QMouseEvent event(me.type, me.pos, me.button, me.buttons, me.modifiers);
322
323             if (me.destination == View) {
324                 QCoreApplication::sendEvent(m_view, &event);
325             } else {
326                 QCoreApplication::sendEvent(m_view->viewport(), &event);
327             }
328         }
329
330         for (int ii = 0; ii < m_keyEvents.count(); ++ii) {
331             KeyEvent &ke = m_keyEvents[ii];
332             ke.msec = msec;
333             QKeyEvent event(ke.type, ke.key, ke.modifiers, ke.text, ke.autorep, ke.count);
334
335             if (ke.destination == View) {
336                 QCoreApplication::sendEvent(m_view, &event);
337             } else {
338                 QCoreApplication::sendEvent(m_view->viewport(), &event);
339             }
340         }
341         m_savedMouseEvents.append(m_mouseEvents);
342         m_savedKeyEvents.append(m_keyEvents);
343     }
344
345     m_mouseEvents.clear();
346     m_keyEvents.clear();
347
348     // Advance test script
349     while (testscript && testscript->count() > testscriptidx) {
350
351         QObject *event = testscript->event(testscriptidx);
352
353         if (QDeclarativeVisualTestFrame *frame = qobject_cast<QDeclarativeVisualTestFrame *>(event)) {
354             if (frame->msec() < msec) {
355                 if (options & QDeclarativeViewer::TestImages && !(options & QDeclarativeViewer::Record)) {
356                     qWarning() << "QDeclarativeTester(" << m_script << "): Extra frame.  Seen:"
357                                << msec << "Expected:" << frame->msec();
358                     imagefailure();
359                 }
360             } else if (frame->msec() == msec) {
361                 if (!frame->hash().isEmpty() && frame->hash().toUtf8() != fe.hash.toHex()) {
362                     if (options & QDeclarativeViewer::TestImages && !(options & QDeclarativeViewer::Record)) {
363                         qWarning() << "QDeclarativeTester(" << m_script << "): Mismatched frame hash at" << msec
364                                    << ".  Seen:" << fe.hash.toHex()
365                                    << "Expected:" << frame->hash().toUtf8();
366                         imagefailure();
367                     }
368                 }
369             } else if (frame->msec() > msec) {
370                 break;
371             }
372
373             if (options & QDeclarativeViewer::TestImages && !(options & QDeclarativeViewer::Record) && !frame->image().isEmpty()) {
374                 QImage goodImage(frame->image().toLocalFile());
375                 if (frame->msec() == 16 && goodImage.size() != img.size()){
376                     //Also an image mismatch, but this warning is more informative. Only checked at start though.
377                     qWarning() << "QDeclarativeTester(" << m_script << "): Size mismatch. This test must be run at " << goodImage.size();
378                     imagefailure();
379                 }
380                 if (goodImage != img) {
381                     QString reject(frame->image().toLocalFile() + ".reject.png");
382                     qWarning() << "QDeclarativeTester(" << m_script << "): Image mismatch.  Reject saved to:"
383                                << reject;
384                     img.save(reject);
385                     bool doDiff = (goodImage.size() == img.size());
386                     if (doDiff) {
387                         QImage diffimg(m_view->width(), m_view->height(), QImage::Format_RGB32);
388                         diffimg.fill(qRgb(255,255,255));
389                         QPainter p(&diffimg);
390                         int diffCount = 0;
391                         for (int x = 0; x < img.width(); ++x) {
392                             for (int y = 0; y < img.height(); ++y) {
393                                 if (goodImage.pixel(x,y) != img.pixel(x,y)) {
394                                     ++diffCount;
395                                     p.drawPoint(x,y);
396                                 }
397                             }
398                         }
399                         QString diff(frame->image().toLocalFile() + ".diff.png");
400                         diffimg.save(diff);
401                         qWarning().nospace() << "                    Diff (" << diffCount << " pixels differed) saved to: " << diff;
402                     }
403                     imagefailure();
404                 }
405             }
406         } else if (QDeclarativeVisualTestMouse *mouse = qobject_cast<QDeclarativeVisualTestMouse *>(event)) {
407             QPoint pos(mouse->x(), mouse->y());
408             QPoint globalPos = m_view->mapToGlobal(QPoint(0, 0)) + pos;
409             QMouseEvent event((QEvent::Type)mouse->type(), pos, globalPos, (Qt::MouseButton)mouse->button(), (Qt::MouseButtons)mouse->buttons(), (Qt::KeyboardModifiers)mouse->modifiers());
410
411             MouseEvent me(&event);
412             me.msec = msec;
413             if (!mouse->sendToViewport()) {
414                 QCoreApplication::sendEvent(m_view, &event);
415                 me.destination = View;
416             } else {
417                 QCoreApplication::sendEvent(m_view->viewport(), &event);
418                 me.destination = ViewPort;
419             }
420             m_savedMouseEvents.append(me);
421         } else if (QDeclarativeVisualTestKey *key = qobject_cast<QDeclarativeVisualTestKey *>(event)) {
422
423             QKeyEvent event((QEvent::Type)key->type(), key->key(), (Qt::KeyboardModifiers)key->modifiers(), QString::fromUtf8(QByteArray::fromHex(key->text().toUtf8())), key->autorep(), key->count());
424
425             KeyEvent ke(&event);
426             ke.msec = msec;
427             if (!key->sendToViewport()) {
428                 QCoreApplication::sendEvent(m_view, &event);
429                 ke.destination = View;
430             } else {
431                 QCoreApplication::sendEvent(m_view->viewport(), &event);
432                 ke.destination = ViewPort;
433             }
434             m_savedKeyEvents.append(ke);
435         }
436         testscriptidx++;
437     }
438
439     filterEvents = true;
440
441     if (testscript && testscript->count() <= testscriptidx) {
442         //if (msec == 16) //for a snapshot, leave it up long enough to see
443         //    (void)::sleep(1);
444         complete();
445     }
446 }
447
448 void QDeclarativeTester::registerTypes()
449 {
450     qmlRegisterType<QDeclarativeVisualTest>("Qt.VisualTest", 4,7, "VisualTest");
451     qmlRegisterType<QDeclarativeVisualTestFrame>("Qt.VisualTest", 4,7, "Frame");
452     qmlRegisterType<QDeclarativeVisualTestMouse>("Qt.VisualTest", 4,7, "Mouse");
453     qmlRegisterType<QDeclarativeVisualTestKey>("Qt.VisualTest", 4,7, "Key");
454 }
455
456 QT_END_NAMESPACE