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