1 /****************************************************************************
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the test suite of the Qt Toolkit.
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
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.
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.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
40 ****************************************************************************/
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>
57 // In Symbian OS test data is located in applications private dir
61 class tst_qdeclarativesqldatabase : public QObject
65 tst_qdeclarativesqldatabase()
67 qApp->setApplicationName("tst_qdeclarativesqldatabase");
68 qApp->setOrganizationName("Nokia");
69 qApp->setOrganizationDomain("nokia.com");
70 engine = new QDeclarativeEngine;
73 ~tst_qdeclarativesqldatabase()
81 void checkDatabasePath();
85 void testQml_cleanopen_data();
86 void testQml_cleanopen();
87 void totalDatabases();
89 void cleanupTestCase();
92 QString dbDir() const;
93 QDeclarativeEngine *engine;
96 class QWebPageWithJavaScriptConsoleMessages : public QWebPage {
98 void javaScriptConsoleMessage(const QString& message, int lineNumber, const QString& sourceID)
100 qWarning() << sourceID << ":" << lineNumber << ":" << message;
104 void removeRecursive(const QString& dirname)
107 QFileInfoList entries(dir.entryInfoList(QDir::Dirs|QDir::Files|QDir::NoDotAndDotDot));
108 for (int i = 0; i < entries.count(); ++i)
109 if (entries[i].isDir())
110 removeRecursive(entries[i].filePath());
112 dir.remove(entries[i].fileName());
113 QDir().rmdir(dirname);
116 void tst_qdeclarativesqldatabase::initTestCase()
118 removeRecursive(dbDir());
119 QDir().mkpath(dbDir());
122 void tst_qdeclarativesqldatabase::cleanupTestCase()
124 removeRecursive(dbDir());
127 QString tst_qdeclarativesqldatabase::dbDir() const
129 static QString tmpd = QDir::tempPath()+"/tst_qdeclarativesqldatabase_output-"
130 + QDateTime::currentDateTime().toString(QLatin1String("yyyyMMddhhmmss"));
134 void tst_qdeclarativesqldatabase::checkDatabasePath()
136 // Check default storage path (we can't use it since we don't want to mess with user's data)
137 QVERIFY(engine->offlineStoragePath().contains("tst_qdeclarativesqldatabase"));
138 QVERIFY(engine->offlineStoragePath().contains("OfflineStorage"));
141 static const int total_databases_created_by_tests = 12;
142 void tst_qdeclarativesqldatabase::testQml_data()
144 QTest::addColumn<QString>("jsfile"); // The input file
146 // Each test should use a newly named DB to avoid inter-test dependencies
147 QTest::newRow("creation") << "data/creation.js";
148 QTest::newRow("creation-a") << "data/creation-a.js";
149 QTest::newRow("creation") << "data/creation.js";
150 QTest::newRow("error-creation") << "data/error-creation.js"; // re-uses above DB
151 QTest::newRow("changeversion") << "data/changeversion.js";
152 QTest::newRow("readonly") << "data/readonly.js";
153 QTest::newRow("readonly-error") << "data/readonly-error.js";
154 QTest::newRow("selection") << "data/selection.js";
155 QTest::newRow("selection-bindnames") << "data/selection-bindnames.js";
156 QTest::newRow("iteration") << "data/iteration.js";
157 QTest::newRow("iteration-forwardonly") << "data/iteration-forwardonly.js";
158 QTest::newRow("error-a") << "data/error-a.js";
159 QTest::newRow("error-notransaction") << "data/error-notransaction.js";
160 QTest::newRow("error-outsidetransaction") << "data/error-outsidetransaction.js"; // reuse above
161 QTest::newRow("reopen1") << "data/reopen1.js";
162 QTest::newRow("reopen2") << "data/reopen2.js"; // re-uses above DB
164 // If you add a test, you should usually use a new database in the
165 // test - in which case increment total_databases_created_by_tests above.
169 void tst_qdeclarativesqldatabase::validateAgainstWebkit()
171 // Validates tests against WebKit (HTML5) support.
173 QFETCH(QString, jsfile);
174 QFETCH(QString, result);
175 QFETCH(int, databases);
178 QVERIFY(f.open(QIODevice::ReadOnly));
179 QString js=f.readAll();
181 QWebPageWithJavaScriptConsoleMessages webpage;
182 webpage.settings()->setOfflineStoragePath(dbDir());
183 webpage.settings()->setAttribute(QWebSettings::OfflineStorageDatabaseEnabled, true);
185 QEXPECT_FAIL("","WebKit doesn't support openDatabaseSync yet", Continue);
186 QCOMPARE(webpage.mainFrame()->evaluateJavaScript(js).toString(),result);
188 QTest::qWait(100); // WebKit crashes if you quit it too fast
190 QWebSecurityOrigin origin = webpage.mainFrame()->securityOrigin();
191 QList<QWebDatabase> dbs = origin.databases();
192 QCOMPARE(dbs.count(), databases);
196 void tst_qdeclarativesqldatabase::testQml()
198 // Tests QML SQL Database support with tests
199 // that have been validated against Webkit.
201 QFETCH(QString, jsfile);
204 "import QtQuick 1.0\n"
205 "import \""+jsfile+"\" as JS\n"
206 "Text { text: JS.test() }";
208 engine->setOfflineStoragePath(dbDir());
209 QDeclarativeComponent component(engine);
210 component.setData(qml.toUtf8(), QUrl::fromLocalFile(SRCDIR "/empty.qml")); // just a file for relative local imports
211 QVERIFY(!component.isError());
212 QDeclarativeText *text = qobject_cast<QDeclarativeText*>(component.create());
214 QCOMPARE(text->text(),QString("passed"));
217 void tst_qdeclarativesqldatabase::testQml_cleanopen_data()
219 QTest::addColumn<QString>("jsfile"); // The input file
220 QTest::newRow("reopen1") << "data/reopen1.js";
221 QTest::newRow("reopen2") << "data/reopen2.js";
222 QTest::newRow("error-creation") << "data/error-creation.js"; // re-uses creation DB
225 void tst_qdeclarativesqldatabase::testQml_cleanopen()
227 // Same as testQml, but clean connections between tests,
228 // making it more like the tests are running in new processes.
231 QDeclarativeEnginePrivate::getScriptEngine(engine)->collectGarbage(); // close databases
232 foreach (QString dbname, QSqlDatabase::connectionNames()) {
233 QSqlDatabase::removeDatabase(dbname);
237 void tst_qdeclarativesqldatabase::totalDatabases()
239 QCOMPARE(QDir(dbDir()+"/Databases").entryInfoList(QDir::Files|QDir::NoDotAndDotDot).count(), total_databases_created_by_tests*2);
242 QTEST_MAIN(tst_qdeclarativesqldatabase)
244 #include "tst_qdeclarativesqldatabase.moc"