tests/auto/declarative/qdeclarativesqldatabase/tst_qdeclarativesqldatabase.cpp
changeset 30 5dc02b23752f
child 33 3e2da88830cd
equal deleted inserted replaced
29:b72c6db6890b 30:5dc02b23752f
       
     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 "../../../shared/util.h"
       
    43 #include <QtDeclarative/qdeclarativeengine.h>
       
    44 #include <QtDeclarative/qdeclarativecomponent.h>
       
    45 #include <private/qdeclarativetext_p.h>
       
    46 #include <private/qdeclarativeengine_p.h>
       
    47 #include <QtCore/qcryptographichash.h>
       
    48 #include <QtWebKit/qwebpage.h>
       
    49 #include <QtWebKit/qwebframe.h>
       
    50 #include <QtWebKit/qwebdatabase.h>
       
    51 #include <QtWebKit/qwebsecurityorigin.h>
       
    52 #include <QtSql/qsqldatabase.h>
       
    53 #include <QtCore/qdir.h>
       
    54 #include <QtCore/qfile.h>
       
    55 
       
    56 class tst_qdeclarativesqldatabase : public QObject
       
    57 {
       
    58     Q_OBJECT
       
    59 public:
       
    60     tst_qdeclarativesqldatabase()
       
    61     {
       
    62         qApp->setApplicationName("tst_qdeclarativesqldatabase");
       
    63         qApp->setOrganizationName("Nokia");
       
    64         qApp->setOrganizationDomain("nokia.com");
       
    65         engine = new QDeclarativeEngine;
       
    66     }
       
    67 
       
    68     ~tst_qdeclarativesqldatabase()
       
    69     {
       
    70         delete engine;
       
    71     }
       
    72 
       
    73 private slots:
       
    74     void initTestCase();
       
    75 
       
    76     void checkDatabasePath();
       
    77 
       
    78     void testQml_data();
       
    79     void testQml();
       
    80     void testQml_cleanopen_data();
       
    81     void testQml_cleanopen();
       
    82     void totalDatabases();
       
    83 
       
    84     void cleanupTestCase();
       
    85 
       
    86 private:
       
    87     QString dbDir() const;
       
    88     QDeclarativeEngine *engine;
       
    89 };
       
    90 
       
    91 class QWebPageWithJavaScriptConsoleMessages : public QWebPage {
       
    92 public:
       
    93     void javaScriptConsoleMessage(const QString& message, int lineNumber, const QString& sourceID)
       
    94     {
       
    95         qWarning() << sourceID << ":" << lineNumber << ":" << message;
       
    96     }
       
    97 };
       
    98 
       
    99 void removeRecursive(const QString& dirname)
       
   100 {
       
   101     QDir dir(dirname);
       
   102     QFileInfoList entries(dir.entryInfoList(QDir::Dirs|QDir::Files|QDir::NoDotAndDotDot));
       
   103     for (int i = 0; i < entries.count(); ++i)
       
   104         if (entries[i].isDir())
       
   105             removeRecursive(entries[i].filePath());
       
   106         else
       
   107             dir.remove(entries[i].fileName());
       
   108     QDir().rmdir(dirname);
       
   109 }
       
   110 
       
   111 void tst_qdeclarativesqldatabase::initTestCase()
       
   112 {
       
   113     removeRecursive(dbDir());
       
   114     QDir().mkpath(dbDir());
       
   115 }
       
   116 
       
   117 void tst_qdeclarativesqldatabase::cleanupTestCase()
       
   118 {
       
   119     removeRecursive(dbDir());
       
   120 }
       
   121 
       
   122 QString tst_qdeclarativesqldatabase::dbDir() const
       
   123 {
       
   124     static QString tmpd = QDir::tempPath()+"/tst_qdeclarativesqldatabase_output-"
       
   125         + QDateTime::currentDateTime().toString(QLatin1String("yyyyMMddhhmmss"));
       
   126     return tmpd;
       
   127 }
       
   128 
       
   129 void tst_qdeclarativesqldatabase::checkDatabasePath()
       
   130 {
       
   131     // Check default storage path (we can't use it since we don't want to mess with user's data)
       
   132     QVERIFY(engine->offlineStoragePath().contains("tst_qdeclarativesqldatabase"));
       
   133     QVERIFY(engine->offlineStoragePath().contains("OfflineStorage"));
       
   134 }
       
   135 
       
   136 static const int total_databases_created_by_tests = 12;
       
   137 void tst_qdeclarativesqldatabase::testQml_data()
       
   138 {
       
   139     QTest::addColumn<QString>("jsfile"); // The input file
       
   140 
       
   141     // Each test should use a newly named DB to avoid inter-test dependencies
       
   142     QTest::newRow("creation") << "data/creation.js";
       
   143     QTest::newRow("creation-a") << "data/creation-a.js";
       
   144     QTest::newRow("creation") << "data/creation.js";
       
   145     QTest::newRow("error-creation") << "data/error-creation.js"; // re-uses above DB
       
   146     QTest::newRow("changeversion") << "data/changeversion.js";
       
   147     QTest::newRow("readonly") << "data/readonly.js";
       
   148     QTest::newRow("readonly-error") << "data/readonly-error.js";
       
   149     QTest::newRow("selection") << "data/selection.js";
       
   150     QTest::newRow("selection-bindnames") << "data/selection-bindnames.js";
       
   151     QTest::newRow("iteration") << "data/iteration.js";
       
   152     QTest::newRow("iteration-forwardonly") << "data/iteration-forwardonly.js";
       
   153     QTest::newRow("error-a") << "data/error-a.js";
       
   154     QTest::newRow("error-notransaction") << "data/error-notransaction.js";
       
   155     QTest::newRow("error-outsidetransaction") << "data/error-outsidetransaction.js"; // reuse above
       
   156     QTest::newRow("reopen1") << "data/reopen1.js";
       
   157     QTest::newRow("reopen2") << "data/reopen2.js"; // re-uses above DB
       
   158 
       
   159     // If you add a test, you should usually use a new database in the
       
   160     // test - in which case increment total_databases_created_by_tests above.
       
   161 }
       
   162 
       
   163 /*
       
   164 void tst_qdeclarativesqldatabase::validateAgainstWebkit()
       
   165 {
       
   166     // Validates tests against WebKit (HTML5) support.
       
   167     //
       
   168     QFETCH(QString, jsfile);
       
   169     QFETCH(QString, result);
       
   170     QFETCH(int, databases);
       
   171 
       
   172     QFile f(jsfile);
       
   173     QVERIFY(f.open(QIODevice::ReadOnly));
       
   174     QString js=f.readAll();
       
   175 
       
   176     QWebPageWithJavaScriptConsoleMessages webpage;
       
   177     webpage.settings()->setOfflineStoragePath(dbDir());
       
   178     webpage.settings()->setAttribute(QWebSettings::OfflineStorageDatabaseEnabled, true);
       
   179 
       
   180     QEXPECT_FAIL("","WebKit doesn't support openDatabaseSync yet", Continue);
       
   181     QCOMPARE(webpage.mainFrame()->evaluateJavaScript(js).toString(),result);
       
   182 
       
   183     QTest::qWait(100); // WebKit crashes if you quit it too fast
       
   184 
       
   185     QWebSecurityOrigin origin = webpage.mainFrame()->securityOrigin();
       
   186     QList<QWebDatabase> dbs = origin.databases();
       
   187     QCOMPARE(dbs.count(), databases);
       
   188 }
       
   189 */
       
   190 
       
   191 void tst_qdeclarativesqldatabase::testQml()
       
   192 {
       
   193     // Tests QML SQL Database support with tests
       
   194     // that have been validated against Webkit.
       
   195     //
       
   196     QFETCH(QString, jsfile);
       
   197 
       
   198     QString qml=
       
   199         "import Qt 4.7\n"
       
   200         "import \""+jsfile+"\" as JS\n"
       
   201         "Text { text: JS.test() }";
       
   202 
       
   203     engine->setOfflineStoragePath(dbDir());
       
   204     QDeclarativeComponent component(engine);
       
   205     component.setData(qml.toUtf8(), QUrl::fromLocalFile(SRCDIR "/empty.qml")); // just a file for relative local imports
       
   206     QVERIFY(!component.isError());
       
   207     QDeclarativeText *text = qobject_cast<QDeclarativeText*>(component.create());
       
   208     QVERIFY(text != 0);
       
   209     QCOMPARE(text->text(),QString("passed"));
       
   210 }
       
   211 
       
   212 void tst_qdeclarativesqldatabase::testQml_cleanopen_data()
       
   213 {
       
   214     QTest::addColumn<QString>("jsfile"); // The input file
       
   215     QTest::newRow("reopen1") << "data/reopen1.js";
       
   216     QTest::newRow("reopen2") << "data/reopen2.js";
       
   217     QTest::newRow("error-creation") << "data/error-creation.js"; // re-uses creation DB
       
   218 }
       
   219 
       
   220 void tst_qdeclarativesqldatabase::testQml_cleanopen()
       
   221 {
       
   222     // Same as testQml, but clean connections between tests,
       
   223     // making it more like the tests are running in new processes.
       
   224     testQml();
       
   225 
       
   226     QDeclarativeEnginePrivate::getScriptEngine(engine)->collectGarbage(); // close databases
       
   227     foreach (QString dbname, QSqlDatabase::connectionNames()) {
       
   228         QSqlDatabase::removeDatabase(dbname);
       
   229     }
       
   230 }
       
   231 
       
   232 void tst_qdeclarativesqldatabase::totalDatabases()
       
   233 {
       
   234     QCOMPARE(QDir(dbDir()+"/Databases").entryInfoList(QDir::Files|QDir::NoDotAndDotDot).count(), total_databases_created_by_tests*2);
       
   235 }
       
   236 
       
   237 QTEST_MAIN(tst_qdeclarativesqldatabase)
       
   238 
       
   239 #include "tst_qdeclarativesqldatabase.moc"