tests/auto/declarative/qdeclarativeconnection/tst_qdeclarativeconnection.cpp
branchGCC_SURGE
changeset 31 5daf16870df6
parent 30 5dc02b23752f
child 33 3e2da88830cd
equal deleted inserted replaced
27:93b982ccede2 31:5daf16870df6
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2010 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 #include <qtest.h>
       
    42 #include <QtDeclarative/qdeclarativeengine.h>
       
    43 #include <QtDeclarative/qdeclarativecomponent.h>
       
    44 #include <private/qdeclarativeconnections_p.h>
       
    45 #include <private/qdeclarativeitem_p.h>
       
    46 #include "../../../shared/util.h"
       
    47 #include <QtDeclarative/qdeclarativescriptstring.h>
       
    48 
       
    49 class tst_qdeclarativeconnection : public QObject
       
    50 
       
    51 {
       
    52     Q_OBJECT
       
    53 public:
       
    54     tst_qdeclarativeconnection();
       
    55 
       
    56 private slots:
       
    57     void defaultValues();
       
    58     void properties();
       
    59     void connection();
       
    60     void trimming();
       
    61     void targetChanged();
       
    62     void unknownSignals_data();
       
    63     void unknownSignals();
       
    64 
       
    65 private:
       
    66     QDeclarativeEngine engine;
       
    67 };
       
    68 
       
    69 tst_qdeclarativeconnection::tst_qdeclarativeconnection()
       
    70 {
       
    71 }
       
    72 
       
    73 void tst_qdeclarativeconnection::defaultValues()
       
    74 {
       
    75     QDeclarativeEngine engine;
       
    76     QDeclarativeComponent c(&engine, QUrl::fromLocalFile(SRCDIR "/data/test-connection3.qml"));
       
    77     QDeclarativeConnections *item = qobject_cast<QDeclarativeConnections*>(c.create());
       
    78 
       
    79     QVERIFY(item != 0);
       
    80     QVERIFY(item->target() == 0);
       
    81 
       
    82     delete item;
       
    83 }
       
    84 
       
    85 void tst_qdeclarativeconnection::properties()
       
    86 {
       
    87     QDeclarativeEngine engine;
       
    88     QDeclarativeComponent c(&engine, QUrl::fromLocalFile(SRCDIR "/data/test-connection2.qml"));
       
    89     QDeclarativeConnections *item = qobject_cast<QDeclarativeConnections*>(c.create());
       
    90 
       
    91     QVERIFY(item != 0);
       
    92 
       
    93     QVERIFY(item != 0);
       
    94     QVERIFY(item->target() == item);
       
    95 
       
    96     delete item;
       
    97 }
       
    98 
       
    99 void tst_qdeclarativeconnection::connection()
       
   100 {
       
   101     QDeclarativeEngine engine;
       
   102     QDeclarativeComponent c(&engine, QUrl::fromLocalFile(SRCDIR "/data/test-connection.qml"));
       
   103     QDeclarativeItem *item = qobject_cast<QDeclarativeItem*>(c.create());
       
   104 
       
   105     QVERIFY(item != 0);
       
   106 
       
   107     QCOMPARE(item->property("tested").toBool(), false);
       
   108     QCOMPARE(item->width(), 50.);
       
   109     emit item->setWidth(100.);
       
   110     QCOMPARE(item->width(), 100.);
       
   111     QCOMPARE(item->property("tested").toBool(), true);
       
   112 
       
   113     delete item;
       
   114 }
       
   115 
       
   116 void tst_qdeclarativeconnection::trimming()
       
   117 {
       
   118     QDeclarativeEngine engine;
       
   119     QDeclarativeComponent c(&engine, QUrl::fromLocalFile(SRCDIR "/data/trimming.qml"));
       
   120     QDeclarativeItem *item = qobject_cast<QDeclarativeItem*>(c.create());
       
   121 
       
   122     QVERIFY(item != 0);
       
   123 
       
   124     QCOMPARE(item->property("tested").toString(), QString(""));
       
   125     int index = item->metaObject()->indexOfSignal("testMe(int,QString)");
       
   126     QMetaMethod method = item->metaObject()->method(index);
       
   127     method.invoke(item,
       
   128                   Qt::DirectConnection,
       
   129                   Q_ARG(int, 5),
       
   130                   Q_ARG(QString, "worked"));
       
   131     QCOMPARE(item->property("tested").toString(), QString("worked5"));
       
   132 
       
   133     delete item;
       
   134 }
       
   135 
       
   136 // Confirm that target can be changed by one of our signal handlers
       
   137 void tst_qdeclarativeconnection::targetChanged()
       
   138 {
       
   139     QDeclarativeEngine engine;
       
   140     QDeclarativeComponent c(&engine, QUrl::fromLocalFile(SRCDIR "/data/connection-targetchange.qml"));
       
   141     QDeclarativeItem *item = qobject_cast<QDeclarativeItem*>(c.create());
       
   142     QVERIFY(item != 0);
       
   143 
       
   144     QDeclarativeConnections *connections = item->findChild<QDeclarativeConnections*>("connections");
       
   145     QVERIFY(connections);
       
   146 
       
   147     QDeclarativeItem *item1 = item->findChild<QDeclarativeItem*>("item1");
       
   148     QVERIFY(item1);
       
   149 
       
   150     item1->setWidth(200);
       
   151 
       
   152     QDeclarativeItem *item2 = item->findChild<QDeclarativeItem*>("item2");
       
   153     QVERIFY(item2);
       
   154     QVERIFY(connections->target() == item2);
       
   155 
       
   156     // If we don't crash then we're OK
       
   157 
       
   158     delete item;
       
   159 }
       
   160 
       
   161 void tst_qdeclarativeconnection::unknownSignals_data()
       
   162 {
       
   163     QTest::addColumn<QString>("file");
       
   164     QTest::addColumn<QString>("error");
       
   165 
       
   166     QTest::newRow("basic") << "connection-unknownsignals.qml" << ":6:5: QML Connections: Cannot assign to non-existent property \"onFooBar\"";
       
   167     QTest::newRow("parent") << "connection-unknownsignals-parent.qml" << ":6:5: QML Connections: Cannot assign to non-existent property \"onFooBar\"";
       
   168     QTest::newRow("ignored") << "connection-unknownsignals-ignored.qml" << ""; // should be NO error
       
   169     QTest::newRow("notarget") << "connection-unknownsignals-notarget.qml" << ""; // should be NO error
       
   170 }
       
   171 
       
   172 void tst_qdeclarativeconnection::unknownSignals()
       
   173 {
       
   174     QFETCH(QString, file);
       
   175     QFETCH(QString, error);
       
   176 
       
   177     QUrl url = QUrl::fromLocalFile(SRCDIR "/data/" + file);
       
   178     if (!error.isEmpty()) {
       
   179         QTest::ignoreMessage(QtWarningMsg, (url.toString() + error).toLatin1());
       
   180     } else {
       
   181         // QTest has no way to insist no message (i.e. fail)
       
   182     }
       
   183 
       
   184     QDeclarativeEngine engine;
       
   185     QDeclarativeComponent c(&engine, url);
       
   186     QDeclarativeItem *item = qobject_cast<QDeclarativeItem*>(c.create());
       
   187     QVERIFY(item != 0);
       
   188 
       
   189     // check that connection is created (they are all runtime errors)
       
   190     QDeclarativeConnections *connections = item->findChild<QDeclarativeConnections*>("connections");
       
   191     QVERIFY(connections);
       
   192 
       
   193     delete item;
       
   194 }
       
   195 
       
   196 QTEST_MAIN(tst_qdeclarativeconnection)
       
   197 
       
   198 #include "tst_qdeclarativeconnection.moc"