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 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
42 #include <QtDeclarative/qdeclarativeengine.h>
43 #include <QtDeclarative/qdeclarativecomponent.h>
44 #include <private/qsgtext_p.h>
45 #include <private/qdeclarativeengine_p.h>
46 #include <QtCore/qcryptographichash.h>
48 #include <QtWebKit/qwebpage.h>
49 #include <QtWebKit/qwebframe.h>
50 #include <QtWebKit/qwebdatabase.h>
51 #include <QtWebKit/qwebsecurityorigin.h>
53 #include <QtSql/qsqldatabase.h>
54 #include <QtCore/qdir.h>
55 #include <QtCore/qfile.h>
56 #include "../shared/util.h"
58 class tst_qdeclarativesqldatabase : public QObject
62 tst_qdeclarativesqldatabase()
64 qApp->setApplicationName("tst_qdeclarativesqldatabase");
65 qApp->setOrganizationName("Nokia");
66 qApp->setOrganizationDomain("nokia.com");
67 engine = new QDeclarativeEngine;
70 ~tst_qdeclarativesqldatabase()
78 void checkDatabasePath();
82 void testQml_cleanopen_data();
83 void testQml_cleanopen();
84 void totalDatabases();
86 void cleanupTestCase();
89 QString dbDir() const;
90 QDeclarativeEngine *engine;
93 void removeRecursive(const QString& dirname)
96 QFileInfoList entries(dir.entryInfoList(QDir::Dirs|QDir::Files|QDir::NoDotAndDotDot));
97 for (int i = 0; i < entries.count(); ++i)
98 if (entries[i].isDir())
99 removeRecursive(entries[i].filePath());
101 dir.remove(entries[i].fileName());
102 QDir().rmdir(dirname);
105 void tst_qdeclarativesqldatabase::initTestCase()
107 removeRecursive(dbDir());
108 QDir().mkpath(dbDir());
111 void tst_qdeclarativesqldatabase::cleanupTestCase()
113 removeRecursive(dbDir());
116 QString tst_qdeclarativesqldatabase::dbDir() const
118 static QString tmpd = QDir::tempPath()+"/tst_qdeclarativesqldatabase_output-"
119 + QDateTime::currentDateTime().toString(QLatin1String("yyyyMMddhhmmss"));
123 void tst_qdeclarativesqldatabase::checkDatabasePath()
125 // Check default storage path (we can't use it since we don't want to mess with user's data)
126 QVERIFY(engine->offlineStoragePath().contains("tst_qdeclarativesqldatabase"));
127 QVERIFY(engine->offlineStoragePath().contains("OfflineStorage"));
130 static const int total_databases_created_by_tests = 12;
131 void tst_qdeclarativesqldatabase::testQml_data()
133 QTest::addColumn<QString>("jsfile"); // The input file
135 // Each test should use a newly named DB to avoid inter-test dependencies
136 QTest::newRow("creation") << "creation.js";
137 QTest::newRow("creation-a") << "creation-a.js";
138 QTest::newRow("creation") << "creation.js";
139 QTest::newRow("error-creation") << "error-creation.js"; // re-uses above DB
140 QTest::newRow("changeversion") << "changeversion.js";
141 QTest::newRow("readonly") << "readonly.js";
142 QTest::newRow("readonly-error") << "readonly-error.js";
143 QTest::newRow("selection") << "selection.js";
144 QTest::newRow("selection-bindnames") << "selection-bindnames.js";
145 QTest::newRow("iteration") << "iteration.js";
146 QTest::newRow("iteration-forwardonly") << "iteration-forwardonly.js";
147 QTest::newRow("error-a") << "error-a.js";
148 QTest::newRow("error-notransaction") << "error-notransaction.js";
149 QTest::newRow("error-outsidetransaction") << "error-outsidetransaction.js"; // reuse above
150 QTest::newRow("reopen1") << "reopen1.js";
151 QTest::newRow("reopen2") << "reopen2.js"; // re-uses above DB
153 // If you add a test, you should usually use a new database in the
154 // test - in which case increment total_databases_created_by_tests above.
158 class QWebPageWithJavaScriptConsoleMessages : public QWebPage {
160 void javaScriptConsoleMessage(const QString& message, int lineNumber, const QString& sourceID)
162 qWarning() << sourceID << ":" << lineNumber << ":" << message;
166 void tst_qdeclarativesqldatabase::validateAgainstWebkit()
168 // Validates tests against WebKit (HTML5) support.
170 QFETCH(QString, jsfile);
171 QFETCH(QString, result);
172 QFETCH(int, databases);
175 QVERIFY(f.open(QIODevice::ReadOnly));
176 QString js=f.readAll();
178 QWebPageWithJavaScriptConsoleMessages webpage;
179 webpage.settings()->setOfflineStoragePath(dbDir());
180 webpage.settings()->setAttribute(QWebSettings::OfflineStorageDatabaseEnabled, true);
182 QEXPECT_FAIL("","WebKit doesn't support openDatabaseSync yet", Continue);
183 QCOMPARE(webpage.mainFrame()->evaluateJavaScript(js).toString(),result);
185 QTest::qWait(100); // WebKit crashes if you quit it too fast
187 QWebSecurityOrigin origin = webpage.mainFrame()->securityOrigin();
188 QList<QWebDatabase> dbs = origin.databases();
189 QCOMPARE(dbs.count(), databases);
193 void tst_qdeclarativesqldatabase::testQml()
195 // Tests QML SQL Database support with tests
196 // that have been validated against Webkit.
198 QFETCH(QString, jsfile);
201 "import QtQuick 2.0\n"
202 "import \""+jsfile+"\" as JS\n"
203 "Text { text: JS.test() }";
205 engine->setOfflineStoragePath(dbDir());
206 QDeclarativeComponent component(engine);
207 component.setData(qml.toUtf8(), QUrl::fromLocalFile(TESTDATA("empty.qml"))); // just a file for relative local imports
208 QVERIFY(!component.isError());
209 QSGText *text = qobject_cast<QSGText*>(component.create());
211 QCOMPARE(text->text(),QString("passed"));
214 void tst_qdeclarativesqldatabase::testQml_cleanopen_data()
216 QTest::addColumn<QString>("jsfile"); // The input file
217 QTest::newRow("reopen1") << "reopen1.js";
218 QTest::newRow("reopen2") << "reopen2.js";
219 QTest::newRow("error-creation") << "error-creation.js"; // re-uses creation DB
222 void tst_qdeclarativesqldatabase::testQml_cleanopen()
224 // Same as testQml, but clean connections between tests,
225 // making it more like the tests are running in new processes.
228 engine->collectGarbage();
230 foreach (QString dbname, QSqlDatabase::connectionNames()) {
231 QSqlDatabase::removeDatabase(dbname);
235 void tst_qdeclarativesqldatabase::totalDatabases()
237 QCOMPARE(QDir(dbDir()+"/Databases").entryInfoList(QDir::Files|QDir::NoDotAndDotDot).count(), total_databases_created_by_tests*2);
240 QTEST_MAIN(tst_qdeclarativesqldatabase)
242 #include "tst_qdeclarativesqldatabase.moc"