Initial import from the monolithic Qt.
[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 ** 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 #ifdef Q_OS_SYMBIAN
57 // In Symbian OS test data is located in applications private dir
58 #define SRCDIR "."
59 #endif
60
61 class tst_qdeclarativesqldatabase : public QObject
62 {
63     Q_OBJECT
64 public:
65     tst_qdeclarativesqldatabase()
66     {
67         qApp->setApplicationName("tst_qdeclarativesqldatabase");
68         qApp->setOrganizationName("Nokia");
69         qApp->setOrganizationDomain("nokia.com");
70         engine = new QDeclarativeEngine;
71     }
72
73     ~tst_qdeclarativesqldatabase()
74     {
75         delete engine;
76     }
77
78 private slots:
79     void initTestCase();
80
81     void checkDatabasePath();
82
83     void testQml_data();
84     void testQml();
85     void testQml_cleanopen_data();
86     void testQml_cleanopen();
87     void totalDatabases();
88
89     void cleanupTestCase();
90
91 private:
92     QString dbDir() const;
93     QDeclarativeEngine *engine;
94 };
95
96 class QWebPageWithJavaScriptConsoleMessages : public QWebPage {
97 public:
98     void javaScriptConsoleMessage(const QString& message, int lineNumber, const QString& sourceID)
99     {
100         qWarning() << sourceID << ":" << lineNumber << ":" << message;
101     }
102 };
103
104 void removeRecursive(const QString& dirname)
105 {
106     QDir dir(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());
111         else
112             dir.remove(entries[i].fileName());
113     QDir().rmdir(dirname);
114 }
115
116 void tst_qdeclarativesqldatabase::initTestCase()
117 {
118     removeRecursive(dbDir());
119     QDir().mkpath(dbDir());
120 }
121
122 void tst_qdeclarativesqldatabase::cleanupTestCase()
123 {
124     removeRecursive(dbDir());
125 }
126
127 QString tst_qdeclarativesqldatabase::dbDir() const
128 {
129     static QString tmpd = QDir::tempPath()+"/tst_qdeclarativesqldatabase_output-"
130         + QDateTime::currentDateTime().toString(QLatin1String("yyyyMMddhhmmss"));
131     return tmpd;
132 }
133
134 void tst_qdeclarativesqldatabase::checkDatabasePath()
135 {
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"));
139 }
140
141 static const int total_databases_created_by_tests = 12;
142 void tst_qdeclarativesqldatabase::testQml_data()
143 {
144     QTest::addColumn<QString>("jsfile"); // The input file
145
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
163
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.
166 }
167
168 /*
169 void tst_qdeclarativesqldatabase::validateAgainstWebkit()
170 {
171     // Validates tests against WebKit (HTML5) support.
172     //
173     QFETCH(QString, jsfile);
174     QFETCH(QString, result);
175     QFETCH(int, databases);
176
177     QFile f(jsfile);
178     QVERIFY(f.open(QIODevice::ReadOnly));
179     QString js=f.readAll();
180
181     QWebPageWithJavaScriptConsoleMessages webpage;
182     webpage.settings()->setOfflineStoragePath(dbDir());
183     webpage.settings()->setAttribute(QWebSettings::OfflineStorageDatabaseEnabled, true);
184
185     QEXPECT_FAIL("","WebKit doesn't support openDatabaseSync yet", Continue);
186     QCOMPARE(webpage.mainFrame()->evaluateJavaScript(js).toString(),result);
187
188     QTest::qWait(100); // WebKit crashes if you quit it too fast
189
190     QWebSecurityOrigin origin = webpage.mainFrame()->securityOrigin();
191     QList<QWebDatabase> dbs = origin.databases();
192     QCOMPARE(dbs.count(), databases);
193 }
194 */
195
196 void tst_qdeclarativesqldatabase::testQml()
197 {
198     // Tests QML SQL Database support with tests
199     // that have been validated against Webkit.
200     //
201     QFETCH(QString, jsfile);
202
203     QString qml=
204         "import QtQuick 1.0\n"
205         "import \""+jsfile+"\" as JS\n"
206         "Text { text: JS.test() }";
207
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());
213     QVERIFY(text != 0);
214     QCOMPARE(text->text(),QString("passed"));
215 }
216
217 void tst_qdeclarativesqldatabase::testQml_cleanopen_data()
218 {
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
223 }
224
225 void tst_qdeclarativesqldatabase::testQml_cleanopen()
226 {
227     // Same as testQml, but clean connections between tests,
228     // making it more like the tests are running in new processes.
229     testQml();
230
231     QDeclarativeEnginePrivate::getScriptEngine(engine)->collectGarbage(); // close databases
232     foreach (QString dbname, QSqlDatabase::connectionNames()) {
233         QSqlDatabase::removeDatabase(dbname);
234     }
235 }
236
237 void tst_qdeclarativesqldatabase::totalDatabases()
238 {
239     QCOMPARE(QDir(dbDir()+"/Databases").entryInfoList(QDir::Files|QDir::NoDotAndDotDot).count(), total_databases_created_by_tests*2);
240 }
241
242 QTEST_MAIN(tst_qdeclarativesqldatabase)
243
244 #include "tst_qdeclarativesqldatabase.moc"