1 /****************************************************************************
3 ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
6 ** This file is part of the test suite of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
40 ****************************************************************************/
42 #include <QtQml/qqmlengine.h>
43 #include <QtQml/qqmlcomponent.h>
44 #include <QtQuick/private/qquicktext_p.h>
45 #include <private/qqmlengine_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_qqmlsqldatabase : public QQmlDataTest
64 qApp->setApplicationName("tst_qqmlsqldatabase");
65 qApp->setOrganizationName("Nokia");
66 qApp->setOrganizationDomain("nokia.com");
67 engine = new QQmlEngine;
70 ~tst_qqmlsqldatabase()
78 void checkDatabasePath();
82 void testQml_cleanopen_data();
83 void testQml_cleanopen();
84 void totalDatabases();
86 void cleanupTestCase();
89 QString dbDir() const;
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_qqmlsqldatabase::initTestCase()
107 if (engine->offlineStoragePath().isEmpty())
108 QSKIP("offlineStoragePath is empty, skip this test.");
109 QQmlDataTest::initTestCase();
110 removeRecursive(dbDir());
111 QDir().mkpath(dbDir());
114 void tst_qqmlsqldatabase::cleanupTestCase()
116 if (engine->offlineStoragePath().isEmpty())
117 QSKIP("offlineStoragePath is empty, skip this test.");
118 removeRecursive(dbDir());
121 QString tst_qqmlsqldatabase::dbDir() const
123 static QString tmpd = QDir::tempPath()+"/tst_qqmlsqldatabase_output-"
124 + QDateTime::currentDateTime().toString(QLatin1String("yyyyMMddhhmmss"));
128 void tst_qqmlsqldatabase::checkDatabasePath()
130 if (engine->offlineStoragePath().isEmpty())
131 QSKIP("offlineStoragePath is empty, skip this test.");
133 // Check default storage path (we can't use it since we don't want to mess with user's data)
134 QVERIFY(engine->offlineStoragePath().contains("tst_qqmlsqldatabase"));
135 QVERIFY(engine->offlineStoragePath().contains("OfflineStorage"));
138 static const int total_databases_created_by_tests = 12;
139 void tst_qqmlsqldatabase::testQml_data()
141 QTest::addColumn<QString>("jsfile"); // The input file
143 // Each test should use a newly named DB to avoid inter-test dependencies
144 QTest::newRow("creation") << "creation.js";
145 QTest::newRow("creation-a") << "creation-a.js";
146 QTest::newRow("creation") << "creation.js";
147 QTest::newRow("error-creation") << "error-creation.js"; // re-uses above DB
148 QTest::newRow("changeversion") << "changeversion.js";
149 QTest::newRow("readonly") << "readonly.js";
150 QTest::newRow("readonly-error") << "readonly-error.js";
151 QTest::newRow("selection") << "selection.js";
152 QTest::newRow("selection-bindnames") << "selection-bindnames.js";
153 QTest::newRow("iteration") << "iteration.js";
154 QTest::newRow("iteration-forwardonly") << "iteration-forwardonly.js";
155 QTest::newRow("error-a") << "error-a.js";
156 QTest::newRow("error-notransaction") << "error-notransaction.js";
157 QTest::newRow("error-outsidetransaction") << "error-outsidetransaction.js"; // reuse above
158 QTest::newRow("reopen1") << "reopen1.js";
159 QTest::newRow("reopen2") << "reopen2.js"; // re-uses above DB
161 // If you add a test, you should usually use a new database in the
162 // test - in which case increment total_databases_created_by_tests above.
166 class QWebPageWithJavaScriptConsoleMessages : public QWebPage {
168 void javaScriptConsoleMessage(const QString& message, int lineNumber, const QString& sourceID)
170 qWarning() << sourceID << ":" << lineNumber << ":" << message;
174 void tst_qqmlsqldatabase::validateAgainstWebkit()
176 // Validates tests against WebKit (HTML5) support.
178 QFETCH(QString, jsfile);
179 QFETCH(QString, result);
180 QFETCH(int, databases);
183 QVERIFY(f.open(QIODevice::ReadOnly));
184 QString js=f.readAll();
186 QWebPageWithJavaScriptConsoleMessages webpage;
187 webpage.settings()->setOfflineStoragePath(dbDir());
188 webpage.settings()->setAttribute(QWebSettings::OfflineStorageDatabaseEnabled, true);
190 QEXPECT_FAIL("","WebKit doesn't support openDatabaseSync yet", Continue);
191 QCOMPARE(webpage.mainFrame()->evaluateJavaScript(js).toString(),result);
193 QTest::qWait(100); // WebKit crashes if you quit it too fast
195 QWebSecurityOrigin origin = webpage.mainFrame()->securityOrigin();
196 QList<QWebDatabase> dbs = origin.databases();
197 QCOMPARE(dbs.count(), databases);
201 void tst_qqmlsqldatabase::testQml()
203 if (engine->offlineStoragePath().isEmpty())
204 QSKIP("offlineStoragePath is empty, skip this test.");
206 // Tests QML SQL Database support with tests
207 // that have been validated against Webkit.
209 QFETCH(QString, jsfile);
212 "import QtQuick 2.0\n"
213 "import \""+jsfile+"\" as JS\n"
214 "Text { text: JS.test() }";
216 engine->setOfflineStoragePath(dbDir());
217 QQmlComponent component(engine);
218 component.setData(qml.toUtf8(), testFileUrl("empty.qml")); // just a file for relative local imports
219 QVERIFY(!component.isError());
220 QQuickText *text = qobject_cast<QQuickText*>(component.create());
222 QCOMPARE(text->text(),QString("passed"));
225 void tst_qqmlsqldatabase::testQml_cleanopen_data()
227 QTest::addColumn<QString>("jsfile"); // The input file
228 QTest::newRow("reopen1") << "reopen1.js";
229 QTest::newRow("reopen2") << "reopen2.js";
230 QTest::newRow("error-creation") << "error-creation.js"; // re-uses creation DB
233 void tst_qqmlsqldatabase::testQml_cleanopen()
235 if (engine->offlineStoragePath().isEmpty())
236 QSKIP("offlineStoragePath is empty, skip this test.");
238 // Same as testQml, but clean connections between tests,
239 // making it more like the tests are running in new processes.
242 engine->collectGarbage();
244 foreach (QString dbname, QSqlDatabase::connectionNames()) {
245 QSqlDatabase::removeDatabase(dbname);
249 void tst_qqmlsqldatabase::totalDatabases()
251 if (engine->offlineStoragePath().isEmpty())
252 QSKIP("offlineStoragePath is empty, skip this test.");
254 QCOMPARE(QDir(dbDir()+"/Databases").entryInfoList(QDir::Files|QDir::NoDotAndDotDot).count(), total_databases_created_by_tests*2);
257 QTEST_MAIN(tst_qqmlsqldatabase)
259 #include "tst_qqmlsqldatabase.moc"