QQuickCanvas renames
[profile/ivi/qtdeclarative.git] / src / qmltest / quicktestresult.cpp
index 80c3f8b..ff2cf05 100644 (file)
@@ -1,8 +1,7 @@
 /****************************************************************************
 **
-** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
-** All rights reserved.
-** Contact: Nokia Corporation (qt-info@nokia.com)
+** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
+** Contact: http://www.qt-project.org/
 **
 ** This file is part of the test suite of the Qt Toolkit.
 **
@@ -35,6 +34,7 @@
 **
 **
 **
+**
 ** $QT_END_LICENSE$
 **
 ****************************************************************************/
@@ -53,6 +53,9 @@
 #include <QtCore/qbytearray.h>
 #include <QtCore/qcoreapplication.h>
 #include <QtCore/qdebug.h>
+#include <QtCore/QUrl>
+#include <QtCore/QDir>
+#include <QtQuick/qquickwindow.h>
 
 QT_BEGIN_NAMESPACE
 
@@ -60,6 +63,57 @@ static const char *globalProgramName = 0;
 static bool loggingStarted = false;
 static QBenchmarkGlobalData globalBenchmarkData;
 
+class Q_QUICK_TEST_EXPORT QuickTestImageObject : public QObject
+{
+    Q_OBJECT
+public:
+    QuickTestImageObject(const QImage& img, QObject *parent = 0)
+        : QObject(parent)
+        , m_image(img)
+    {
+    }
+
+    ~QuickTestImageObject() {}
+
+public Q_SLOTS:
+    int red(int x, int y) const
+    {
+        return pixel(x, y).value<QColor>().red();
+    }
+
+    int green(int x, int y) const
+    {
+        return pixel(x, y).value<QColor>().green();
+    }
+
+    int blue(int x, int y) const
+    {
+        return pixel(x, y).value<QColor>().blue();
+    }
+
+    int alpha(int x, int y) const
+    {
+        return pixel(x, y).value<QColor>().alpha();
+    }
+
+    QVariant pixel(int x, int y) const
+    {
+        if (m_image.isNull()
+         || x >= m_image.width()
+         || y >= m_image.height()
+         || x < 0
+         || y < 0
+         || x * y >= m_image.width() * m_image.height())
+            return QVariant();
+
+        const QRgb* pixel = reinterpret_cast<const QRgb*>(m_image.constScanLine(y));
+        pixel += x;
+        return QColor::fromRgba(*pixel);
+    }
+private:
+    QImage m_image;
+};
+
 class QuickTestResultPrivate
 {
 public:
@@ -78,7 +132,6 @@ public:
     }
 
     QByteArray intern(const QString &str);
-    void updateTestObjectName();
 
     QString testCaseName;
     QString functionName;
@@ -96,25 +149,6 @@ QByteArray QuickTestResultPrivate::intern(const QString &str)
     return *(internedStrings.insert(bstr));
 }
 
-void QuickTestResultPrivate::updateTestObjectName()
-{
-    // In plain logging mode we use the TestCase name as the
-    // class name so that multiple TestCase elements will report
-    // results with "testCase::function".  In XML logging mode,
-    // we use the program name as the class name and report test
-    // functions as "testCase__function".
-    if (QTestLog::logMode() == QTestLog::Plain) {
-        if (testCaseName.isEmpty()) {
-            QTestResult::setCurrentTestObject(globalProgramName);
-        } else if (QTestLog::logMode() == QTestLog::Plain) {
-            QTestResult::setCurrentTestObject
-                (intern(testCaseName).constData());
-        }
-    } else {
-        QTestResult::setCurrentTestObject(globalProgramName);
-    }
-}
-
 QuickTestResult::QuickTestResult(QObject *parent)
     : QObject(parent), d_ptr(new QuickTestResultPrivate)
 {
@@ -144,7 +178,6 @@ void QuickTestResult::setTestCaseName(const QString &name)
 {
     Q_D(QuickTestResult);
     d->testCaseName = name;
-    d->updateTestObjectName();
     emit testCaseNameChanged();
 }
 
@@ -167,15 +200,11 @@ void QuickTestResult::setFunctionName(const QString &name)
 {
     Q_D(QuickTestResult);
     if (!name.isEmpty()) {
-        // In plain logging mode, we use the function name directly.
-        // In XML logging mode, we use "testCase__functionName" as the
-        // program name is acting as the class name.
-        if (QTestLog::logMode() == QTestLog::Plain ||
-                d->testCaseName.isEmpty()) {
+        if (d->testCaseName.isEmpty()) {
             QTestResult::setCurrentTestFunction
                 (d->intern(name).constData());
         } else {
-            QString fullName = d->testCaseName + QLatin1String("__") + name;
+            QString fullName = d->testCaseName + QLatin1String("::") + name;
             QTestResult::setCurrentTestFunction
                 (d->intern(fullName).constData());
         }
@@ -186,17 +215,6 @@ void QuickTestResult::setFunctionName(const QString &name)
     emit functionNameChanged();
 }
 
-QuickTestResult::FunctionType QuickTestResult::functionType() const
-{
-    return FunctionType(QTestResult::currentTestLocation());
-}
-
-void QuickTestResult::setFunctionType(FunctionType type)
-{
-    QTestResult::setCurrentTestLocation(QTestResult::TestLocation(type));
-    emit functionTypeChanged();
-}
-
 /*!
     \qmlproperty string TestResult::dataTag
 
@@ -226,28 +244,15 @@ void QuickTestResult::setDataTag(const QString &tag)
 /*!
     \qmlproperty bool TestResult::failed
 
-    This property returns true if the current test function has
-    failed; false otherwise.  The fail state is reset when
-    functionName is changed or finishTestFunction() is called.
+    This property returns true if the current test function (or
+    current test data row for a data-driven test) has failed;
+    false otherwise.  The fail state is reset when functionName
+    is changed or finishTestDataCleanup() is called.
 
-    \sa skipped, dataFailed
+    \sa skipped
 */
 bool QuickTestResult::isFailed() const
 {
-    return QTestResult::testFailed();
-}
-
-/*!
-    \qmlproperty bool TestResult::dataFailed
-
-    This property returns true if the current data function has
-    failed; false otherwise.  The fail state is reset when
-    functionName is changed or finishTestFunction() is called.
-
-    \sa failed
-*/
-bool QuickTestResult::isDataFailed() const
-{
     return QTestResult::currentTestFailed();
 }
 
@@ -279,7 +284,7 @@ void QuickTestResult::setSkipped(bool skip)
 */
 int QuickTestResult::passCount() const
 {
-    return QTestResult::passCount();
+    return QTestLog::passCount();
 }
 
 /*!
@@ -291,7 +296,7 @@ int QuickTestResult::passCount() const
 */
 int QuickTestResult::failCount() const
 {
-    return QTestResult::failCount();
+    return QTestLog::failCount();
 }
 
 /*!
@@ -303,7 +308,7 @@ int QuickTestResult::failCount() const
 */
 int QuickTestResult::skipCount() const
 {
-    return QTestResult::skipCount();
+    return QTestLog::skipCount();
 }
 
 /*!
@@ -339,18 +344,9 @@ void QuickTestResult::startLogging()
 {
     // The program name is used for logging headers and footers if it
     // is set.  Otherwise the test case name is used.
-    Q_D(QuickTestResult);
     if (loggingStarted)
         return;
-    const char *saved = QTestResult::currentTestObjectName();
-    if (globalProgramName) {
-        QTestResult::setCurrentTestObject(globalProgramName);
-    } else {
-        QTestResult::setCurrentTestObject
-            (d->intern(d->testCaseName).constData());
-    }
     QTestLog::startLogging();
-    QTestResult::setCurrentTestObject(saved);
     loggingStarted = true;
 }
 
@@ -366,10 +362,8 @@ void QuickTestResult::stopLogging()
     Q_D(QuickTestResult);
     if (globalProgramName)
         return;     // Logging will be stopped by setProgramName(0).
-    const char *saved = QTestResult::currentTestObjectName();
     QTestResult::setCurrentTestObject(d->intern(d->testCaseName).constData());
     QTestLog::stopLogging();
-    QTestResult::setCurrentTestObject(saved);
 }
 
 void QuickTestResult::initTestTable()
@@ -377,6 +371,9 @@ void QuickTestResult::initTestTable()
     Q_D(QuickTestResult);
     delete d->table;
     d->table = new QTestTable;
+    //qmltest does not really need the column for data driven test
+    //add this to avoid warnings.
+    d->table->addColumn(qMetaTypeId<QString>(), "qmltest_dummy_data_column");
 }
 
 void QuickTestResult::clearTestTable()
@@ -386,100 +383,96 @@ void QuickTestResult::clearTestTable()
     d->table = 0;
 }
 
+void QuickTestResult::finishTestData()
+{
+    QTestResult::finishedCurrentTestData();
+}
+
+void QuickTestResult::finishTestDataCleanup()
+{
+    QTestResult::finishedCurrentTestDataCleanup();
+}
+
 void QuickTestResult::finishTestFunction()
 {
     QTestResult::finishedCurrentTestFunction();
 }
 
-static QString qtest_fixFile(const QString &file)
+static QString qtestFixUrl(const QUrl &location)
 {
-    if (file.startsWith(QLatin1String("file://")))
-        return file.mid(7);
-    else
-        return file;
+    if (location.isLocalFile()) // Use QUrl's logic for Windows drive letters.
+        return QDir::toNativeSeparators(location.toLocalFile());
+    return location.toString();
 }
 
 void QuickTestResult::fail
-    (const QString &message, const QString &file, int line)
+    (const QString &message, const QUrl &location, int line)
 {
     QTestResult::addFailure(message.toLatin1().constData(),
-                            qtest_fixFile(file).toLatin1().constData(), line);
+                            qtestFixUrl(location).toLatin1().constData(), line);
 }
 
 bool QuickTestResult::verify
-    (bool success, const QString &message, const QString &file, int line)
+    (bool success, const QString &message, const QUrl &location, int line)
 {
     if (!success && message.isEmpty()) {
         return QTestResult::verify
             (success, "verify()", "",
-             qtest_fixFile(file).toLatin1().constData(), line);
+             qtestFixUrl(location).toLatin1().constData(), line);
     } else {
         return QTestResult::verify
             (success, message.toLatin1().constData(), "",
-             qtest_fixFile(file).toLatin1().constData(), line);
+             qtestFixUrl(location).toLatin1().constData(), line);
     }
 }
 
 bool QuickTestResult::compare
     (bool success, const QString &message,
      const QString &val1, const QString &val2,
-     const QString &file, int line)
+     const QUrl &location, int line)
 {
-    if (success) {
-        return QTestResult::compare
-            (success, message.toLocal8Bit().constData(),
-             qtest_fixFile(file).toLatin1().constData(), line);
-    } else {
-        return QTestResult::compare
-            (success, message.toLocal8Bit().constData(),
-             QTest::toString(val1.toLatin1().constData()),
-             QTest::toString(val2.toLatin1().constData()),
-             "", "",
-             qtest_fixFile(file).toLatin1().constData(), line);
-    }
-}
-
-void QuickTestResult::skipSingle
-    (const QString &message, const QString &file, int line)
-{
-    QTestResult::addSkip(message.toLatin1().constData(),
-                         qtest_fixFile(file).toLatin1().constData(), line);
+    return QTestResult::compare
+        (success, message.toLocal8Bit().constData(),
+         QTest::toString(val1.toLatin1().constData()),
+         QTest::toString(val2.toLatin1().constData()),
+         "", "",
+         qtestFixUrl(location).toLatin1().constData(), line);
 }
 
-void QuickTestResult::skipAll
-    (const QString &message, const QString &file, int line)
+void QuickTestResult::skip
+    (const QString &message, const QUrl &location, int line)
 {
     QTestResult::addSkip(message.toLatin1().constData(),
-                         qtest_fixFile(file).toLatin1().constData(), line);
+                         qtestFixUrl(location).toLatin1().constData(), line);
     QTestResult::setSkipCurrentTest(true);
 }
 
 bool QuickTestResult::expectFail
-    (const QString &tag, const QString &comment, const QString &file, int line)
+    (const QString &tag, const QString &comment, const QUrl &location, int line)
 {
     return QTestResult::expectFail
         (tag.toLatin1().constData(),
          QTest::toString(comment.toLatin1().constData()),
-         QTest::Abort, qtest_fixFile(file).toLatin1().constData(), line);
+         QTest::Abort, qtestFixUrl(location).toLatin1().constData(), line);
 }
 
 bool QuickTestResult::expectFailContinue
-    (const QString &tag, const QString &comment, const QString &file, int line)
+    (const QString &tag, const QString &comment, const QUrl &location, int line)
 {
     return QTestResult::expectFail
         (tag.toLatin1().constData(),
          QTest::toString(comment.toLatin1().constData()),
-         QTest::Continue, qtest_fixFile(file).toLatin1().constData(), line);
+         QTest::Continue, qtestFixUrl(location).toLatin1().constData(), line);
 }
 
-void QuickTestResult::warn(const QString &message)
+void QuickTestResult::warn(const QString &message, const QUrl &location, int line)
 {
-    QTestLog::warn(message.toLatin1().constData());
+    QTestLog::warn(message.toLatin1().constData(), qtestFixUrl(location).toLatin1().constData(), line);
 }
 
 void QuickTestResult::ignoreWarning(const QString &message)
 {
-    QTestResult::ignoreMessage(QtWarningMsg, message.toLatin1().constData());
+    QTestLog::ignoreMessage(QtWarningMsg, message.toLatin1().constData());
 }
 
 void QuickTestResult::wait(int ms)
@@ -593,6 +586,17 @@ void QuickTestResult::stopBenchmark()
     d->benchmarkIter = 0;
 }
 
+QObject *QuickTestResult::grabImage(QQuickItem *item)
+{
+    if (item) {
+        QQuickWindow *window = item->window();
+        QImage grabbed = window->grabWindow();
+        QRectF rf(item->x(), item->y(), item->width(), item->height());
+        rf = rf.intersected(QRectF(0, 0, grabbed.width(), grabbed.height()));
+        return new QuickTestImageObject(grabbed.copy(rf.toAlignedRect()));
+    }
+    return 0;
+}
 namespace QTest {
     void qtest_qParseArgs(int argc, char *argv[], bool qml);
 };
@@ -614,6 +618,12 @@ void QuickTestResult::setProgramName(const char *name)
         QTestResult::setCurrentTestObject(0);
     }
     globalProgramName = name;
+    QTestResult::setCurrentTestObject(globalProgramName);
+}
+
+void QuickTestResult::setCurrentAppname(const char *appname)
+{
+    QTestResult::setCurrentAppname(appname);
 }
 
 int QuickTestResult::exitCode()
@@ -623,8 +633,10 @@ int QuickTestResult::exitCode()
 #else
     // make sure our exit code is never going above 127
     // since that could wrap and indicate 0 test fails
-    return qMin(QTestResult::failCount(), 127);
+    return qMin(QTestLog::failCount(), 127);
 #endif
 }
 
+#include "quicktestresult.moc"
+
 QT_END_NAMESPACE