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