Move snippets to separate test in the examples testcase
authorAlan Alpert <alan.alpert@nokia.com>
Wed, 7 Dec 2011 03:26:48 +0000 (13:26 +1000)
committerQt by Nokia <qt-info@nokia.com>
Wed, 7 Dec 2011 12:17:06 +0000 (13:17 +0100)
This test filters out warning messages, so as to not clutter the logs.

Also altered an example so that v8 doesn't spit out a warning message.

Change-Id: I89ab9f1c12bee6cea88e06ce5bf98f2f69a13559
Reviewed-by: Aaron Kennedy <aaron.kennedy@nokia.com>
doc/src/snippets/declarative/codingconventions/myscript.js
tests/auto/qtquick2/examples/tst_examples.cpp

index cfa6462..e7f83c2 100644 (file)
@@ -1,5 +1,8 @@
 function calculateWidth(parent)
 {
+    if (parent == null)
+        return 0
+
     var w = parent.width / 3
     // ...
     // more javascript code
index e56d6a3..3d9a8ad 100644 (file)
 #include <QDeclarativeEngine>
 #include <QDeclarativeError>
 
+static QtMsgHandler testlibMsgHandler = 0;
+void msgHandlerFilter(QtMsgType type, const char *msg)
+{
+    if (type == QtCriticalMsg || type == QtFatalMsg)
+        (*testlibMsgHandler)(type, msg);
+}
+
 class tst_examples : public QObject
 {
     Q_OBJECT
@@ -57,8 +64,13 @@ public:
     tst_examples();
 
 private slots:
+    void init();
+    void cleanup();
+
     void sgexamples_data();
     void sgexamples();
+    void sgsnippets_data();
+    void sgsnippets();
 
     void namingConvention();
 private:
@@ -105,6 +117,18 @@ tst_examples::tst_examples()
 #endif
 }
 
+void tst_examples::init()
+{
+    if (!qstrcmp(QTest::currentTestFunction(), "sgsnippets"))
+        testlibMsgHandler = qInstallMsgHandler(msgHandlerFilter);
+}
+
+void tst_examples::cleanup()
+{
+    if (!qstrcmp(QTest::currentTestFunction(), "sgsnippets"))
+        qInstallMsgHandler(testlibMsgHandler);
+}
+
 /*
 This tests that the examples follow the naming convention required
 to have them tested by the examples() test.
@@ -201,23 +225,16 @@ that they start and exit cleanly.
 Examples are any .qml files under the examples/ directory that start
 with a lower case letter.
 */
-static void silentErrorsMsgHandler(QtMsgType, const char *)
-{
-}
-
-
 void tst_examples::sgexamples_data()
 {
     QTest::addColumn<QString>("file");
 
     QString examples = QLatin1String(SRCDIR) + "/../../../../examples/declarative/";
     QString tutorials = QLatin1String(SRCDIR) + "/../../../../examples/tutorials/"; //Only declarative tutorials since modularization
-    QString snippets = QLatin1String(SRCDIR) + "/../../../../doc/src/snippets/declarative";
 
     QStringList files;
     files << findQmlFiles(QDir(examples));
     files << findQmlFiles(QDir(tutorials));
-    files << findQmlFiles(QDir(snippets));
 
     foreach (const QString &file, files)
         QTest::newRow(qPrintable(file)) << file;
@@ -227,8 +244,42 @@ void tst_examples::sgexamples()
 {
     QFETCH(QString, file);
 
-    QtMsgHandler old = qInstallMsgHandler(silentErrorsMsgHandler);
-    qInstallMsgHandler(old);
+    QDeclarativeComponent component(&engine, QUrl::fromLocalFile(file));
+    if (component.status() == QDeclarativeComponent::Error)
+        qWarning() << component.errors();
+    QCOMPARE(component.status(), QDeclarativeComponent::Ready);
+
+    QScopedPointer<QObject> object(component.beginCreate(engine.rootContext()));
+    QQuickItem *root = qobject_cast<QQuickItem *>(object.data());
+    if (!root)
+        component.completeCreate();
+    QVERIFY(root);
+
+    QQuickCanvas canvas;
+    root->setParentItem(canvas.rootItem());
+    component.completeCreate();
+    canvas.show();
+
+    QTest::qWaitForWindowShown(&canvas);
+
+}
+
+void tst_examples::sgsnippets_data()
+{
+    QTest::addColumn<QString>("file");
+
+    QString snippets = QLatin1String(SRCDIR) + "/../../../../doc/src/snippets/declarative";
+
+    QStringList files;
+    files << findQmlFiles(QDir(snippets));
+
+    foreach (const QString &file, files)
+        QTest::newRow(qPrintable(file)) << file;
+}
+
+void tst_examples::sgsnippets()
+{
+    QFETCH(QString, file);
 
     QDeclarativeComponent component(&engine, QUrl::fromLocalFile(file));
     if (component.status() == QDeclarativeComponent::Error)