Remove copy of tests/shared/util.h.
[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 <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>
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 QObject
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     removeRecursive(dbDir());
108     QDir().mkpath(dbDir());
109 }
110
111 void tst_qdeclarativesqldatabase::cleanupTestCase()
112 {
113     removeRecursive(dbDir());
114 }
115
116 QString tst_qdeclarativesqldatabase::dbDir() const
117 {
118     static QString tmpd = QDir::tempPath()+"/tst_qdeclarativesqldatabase_output-"
119         + QDateTime::currentDateTime().toString(QLatin1String("yyyyMMddhhmmss"));
120     return tmpd;
121 }
122
123 void tst_qdeclarativesqldatabase::checkDatabasePath()
124 {
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"));
128 }
129
130 static const int total_databases_created_by_tests = 12;
131 void tst_qdeclarativesqldatabase::testQml_data()
132 {
133     QTest::addColumn<QString>("jsfile"); // The input file
134
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
152
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.
155 }
156
157 /*
158 class QWebPageWithJavaScriptConsoleMessages : public QWebPage {
159 public:
160     void javaScriptConsoleMessage(const QString& message, int lineNumber, const QString& sourceID)
161     {
162         qWarning() << sourceID << ":" << lineNumber << ":" << message;
163     }
164 };
165
166 void tst_qdeclarativesqldatabase::validateAgainstWebkit()
167 {
168     // Validates tests against WebKit (HTML5) support.
169     //
170     QFETCH(QString, jsfile);
171     QFETCH(QString, result);
172     QFETCH(int, databases);
173
174     QFile f(jsfile);
175     QVERIFY(f.open(QIODevice::ReadOnly));
176     QString js=f.readAll();
177
178     QWebPageWithJavaScriptConsoleMessages webpage;
179     webpage.settings()->setOfflineStoragePath(dbDir());
180     webpage.settings()->setAttribute(QWebSettings::OfflineStorageDatabaseEnabled, true);
181
182     QEXPECT_FAIL("","WebKit doesn't support openDatabaseSync yet", Continue);
183     QCOMPARE(webpage.mainFrame()->evaluateJavaScript(js).toString(),result);
184
185     QTest::qWait(100); // WebKit crashes if you quit it too fast
186
187     QWebSecurityOrigin origin = webpage.mainFrame()->securityOrigin();
188     QList<QWebDatabase> dbs = origin.databases();
189     QCOMPARE(dbs.count(), databases);
190 }
191 */
192
193 void tst_qdeclarativesqldatabase::testQml()
194 {
195     // Tests QML SQL Database support with tests
196     // that have been validated against Webkit.
197     //
198     QFETCH(QString, jsfile);
199
200     QString qml=
201         "import QtQuick 2.0\n"
202         "import \""+jsfile+"\" as JS\n"
203         "Text { text: JS.test() }";
204
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());
210     QVERIFY(text != 0);
211     QCOMPARE(text->text(),QString("passed"));
212 }
213
214 void tst_qdeclarativesqldatabase::testQml_cleanopen_data()
215 {
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
220 }
221
222 void tst_qdeclarativesqldatabase::testQml_cleanopen()
223 {
224     // Same as testQml, but clean connections between tests,
225     // making it more like the tests are running in new processes.
226     testQml();
227
228     engine->collectGarbage();
229
230     foreach (QString dbname, QSqlDatabase::connectionNames()) {
231         QSqlDatabase::removeDatabase(dbname);
232     }
233 }
234
235 void tst_qdeclarativesqldatabase::totalDatabases()
236 {
237     QCOMPARE(QDir(dbDir()+"/Databases").entryInfoList(QDir::Files|QDir::NoDotAndDotDot).count(), total_databases_created_by_tests*2);
238 }
239
240 QTEST_MAIN(tst_qdeclarativesqldatabase)
241
242 #include "tst_qdeclarativesqldatabase.moc"