Stabilize test.
[profile/ivi/qtdeclarative.git] / tests / auto / declarative / debugger / qdebugmessageservice / tst_qdebugmessageservice.cpp
index f138cb2..07e744d 100644 (file)
@@ -1,8 +1,7 @@
 /****************************************************************************
 **
-** Copyright (C) 2011 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$
 **
 ****************************************************************************/
@@ -59,14 +59,16 @@ class tst_QDebugMessageService : public QDeclarativeDataTest
 public:
     tst_QDebugMessageService();
 
+    void init(bool extendedOutput);
+
 private slots:
     void initTestCase();
     void cleanupTestCase();
 
-    void init();
     void cleanup();
 
     void retrieveDebugOutput();
+    void retrieveDebugOutputExtended();
 
 private:
     QDeclarativeDebugProcess *m_process;
@@ -84,6 +86,11 @@ struct LogEntry {
     QString toString() const { return QString::number(type) + ": " + message; }
 };
 
+bool operator==(const LogEntry &t1, const LogEntry &t2)
+{
+    return t1.type == t2.type && t1.message == t2.message;
+}
+
 class QDeclarativeDebugMsgClient : public QDeclarativeDebugClient
 {
     Q_OBJECT
@@ -97,7 +104,7 @@ public:
 
 protected:
     //inherited from QDeclarativeDebugClient
-    void statusChanged(Status status);
+    void stateChanged(State state);
     void messageReceived(const QByteArray &data);
 
 signals:
@@ -105,9 +112,9 @@ signals:
     void debugOutput();
 };
 
-void QDeclarativeDebugMsgClient::statusChanged(Status status)
+void QDeclarativeDebugMsgClient::stateChanged(State state)
 {
-    if (status == Enabled) {
+    if (state == Enabled) {
         emit enabled();
     }
 }
@@ -119,16 +126,10 @@ void QDeclarativeDebugMsgClient::messageReceived(const QByteArray &data)
     ds >> command;
 
     if (command == "MESSAGE") {
-        QByteArray container;
-        ds >> container;
-
-        QVERIFY(ds.atEnd());
-
-        QDataStream containerDs(container);
         int type;
         QByteArray message;
-        containerDs >> type >> message;
-        QVERIFY(containerDs.atEnd());
+        ds >> type >> message;
+        QVERIFY(ds.atEnd());
 
         QVERIFY(type >= QtDebugMsg);
         QVERIFY(type <= QtFatalMsg);
@@ -164,13 +165,14 @@ void tst_QDebugMessageService::cleanupTestCase()
         delete m_connection;
 }
 
-void tst_QDebugMessageService::init()
+void tst_QDebugMessageService::init(bool extendedOutput)
 {
     m_connection = new QDeclarativeDebugConnection();
     m_process = new QDeclarativeDebugProcess(QLibraryInfo::location(QLibraryInfo::BinariesPath) + "/qmlscene");
     m_client = new QDeclarativeDebugMsgClient(m_connection);
 
-    m_process->setEnvironment(QProcess::systemEnvironment() << "QML_CONSOLE_EXTENDED=1");
+    if (extendedOutput)
+        m_process->setEnvironment(QProcess::systemEnvironment() << "QML_CONSOLE_EXTENDED=1");
     m_process->start(QStringList() << QLatin1String(NORMALMODE) << QDeclarativeDataTest::instance()->testFile(QMLFILE));
     if (!m_process->waitForSessionStart()) {
         QFAIL(QString("Could not launch app. Application output: \n%1").arg(m_process->output()).toAscii());
@@ -202,15 +204,38 @@ void tst_QDebugMessageService::cleanup()
 
 void tst_QDebugMessageService::retrieveDebugOutput()
 {
-    if (m_client->logBuffer.isEmpty())
-        QDeclarativeDebugTest::waitForSignal(m_client, SIGNAL(debugOutput()));
-    QVERIFY(!m_client->logBuffer.isEmpty());
+    init(false);
+
+    int maxTries = 2;
+    while ((m_client->logBuffer.size() < 2)
+           || (maxTries-- > 0))
+        QDeclarativeDebugTest::waitForSignal(m_client, SIGNAL(debugOutput()), 1000);
+
+    QVERIFY(m_client->logBuffer.size() >= 2);
+
+    QVERIFY(m_client->logBuffer.contains(LogEntry(QtDebugMsg, QLatin1String("console.log"))));
+    QVERIFY(m_client->logBuffer.contains(LogEntry(QtDebugMsg, QLatin1String("console.count: 1"))));
+}
+
+void tst_QDebugMessageService::retrieveDebugOutputExtended()
+{
+    init(true);
+
+    int maxTries = 2;
+    while ((m_client->logBuffer.size() < 2)
+           || (maxTries-- > 0))
+        QDeclarativeDebugTest::waitForSignal(m_client, SIGNAL(debugOutput()), 1000);
+
+    QVERIFY(m_client->logBuffer.size() >= 2);
+
+    const QString path =
+            QUrl::fromLocalFile(QDeclarativeDataTest::instance()->testFile(QMLFILE)).toString();
 
+    QString logMsg = QString::fromLatin1("console.log (%1:%2)").arg(path).arg(48);
+    QString countMsg = QString::fromLatin1("console.count: 1 (%1:%2)").arg(path).arg(49);
 
-    QString msg = QString::fromLatin1("console.log (%1:%2)").arg(
-                QUrl::fromLocalFile(QDeclarativeDataTest::instance()->testFile(QMLFILE)).toString()).arg(48);
-    QCOMPARE(m_client->logBuffer.last().toString(),
-             LogEntry(QtDebugMsg, msg).toString());
+    QVERIFY(m_client->logBuffer.contains(LogEntry(QtDebugMsg, logMsg)));
+    QVERIFY(m_client->logBuffer.contains(LogEntry(QtDebugMsg, countMsg)));
 }
 
 QTEST_MAIN(tst_QDebugMessageService)