diff options
author | Matthew Vogt <matthew.vogt@nokia.com> | 2012-02-16 14:43:03 +1000 |
---|---|---|
committer | Qt by Nokia <qt-info@nokia.com> | 2012-02-24 04:51:31 +0100 |
commit | b855240b782395f94315f43ea3e7e182299fac48 (patch) | |
tree | bc594c04449be8cd14cd0ab0bb72dafc2be0ffb2 /tests/auto/qml/qqmlsqldatabase | |
parent | 6a42a6e0a9a1abdda0d07a5a20b4ac7e45348684 (diff) |
Rename QDeclarative symbols to QQuick and QQml
Symbols beginning with QDeclarative are already exported
by the quick1 module.
Users can apply the bin/rename-qtdeclarative-symbols.sh
script to modify client code using the previous names of the
renamed symbols.
Task-number: QTBUG-23737
Change-Id: Ifaa482663767634931e8711a8e9bf6e404859e66
Reviewed-by: Martin Jones <martin.jones@nokia.com>
Diffstat (limited to 'tests/auto/qml/qqmlsqldatabase')
19 files changed, 664 insertions, 0 deletions
diff --git a/tests/auto/qml/qqmlsqldatabase/data/README b/tests/auto/qml/qqmlsqldatabase/data/README new file mode 100644 index 0000000000..7efca3a972 --- /dev/null +++ b/tests/auto/qml/qqmlsqldatabase/data/README @@ -0,0 +1,3 @@ +These tests are executed in sequence - the database persist until the end of the +testing. This is done to better exercise the persistence of the database, since +that is how it is used. diff --git a/tests/auto/qml/qqmlsqldatabase/data/changeversion.js b/tests/auto/qml/qqmlsqldatabase/data/changeversion.js new file mode 100644 index 0000000000..178ff7c4af --- /dev/null +++ b/tests/auto/qml/qqmlsqldatabase/data/changeversion.js @@ -0,0 +1,55 @@ +.import QtQuick.LocalStorage 2.0 as Sql + +function test() { + var r="transaction_not_finished"; + + var db = Sql.openDatabaseSync("QmlTestDB-changeversion", "", "Test database from Qt autotests", 1000000, + function(db) { + db.changeVersion("","1.0") + db.transaction(function(tx){ + tx.executeSql('CREATE TABLE Greeting(salutation TEXT, salutee TEXT)'); + }) + }); + + db.transaction(function(tx){ + tx.executeSql('INSERT INTO Greeting VALUES ("Hello", "world")'); + tx.executeSql('INSERT INTO Greeting VALUES ("Goodbye", "cruel world")'); + }); + + + db = Sql.openDatabaseSync("QmlTestDB-changeversion", "", "Test database from Qt autotests", 1000000); + + if (db.version == "1.0") + db.changeVersion("1.0","2.0",function(tx) + { + tx.executeSql('CREATE TABLE Utterance(type TEXT, phrase TEXT)') + var rs = tx.executeSql('SELECT * FROM Greeting'); + for (var i=0; i<rs.rows.length; ++i) { + var type = "Greeting"; + var phrase = rs.rows.item(i).salutation + ", " + rs.rows.item(i).salutee; + if (rs.rows.item(i).salutation == "Goodbye" + || rs.rows.item(i).salutation == "Farewell" + || rs.rows.item(i).salutation == "Good-bye") type = "Valediction"; + var ins = tx.executeSql('INSERT INTO Utterance VALUES(?,?)',[type,phrase]); + } + tx.executeSql('DROP TABLE Greeting'); + }); + else + return "db.version should be 1.0, but is " + db.version; + + var db = Sql.openDatabaseSync("QmlTestDB-changeversion", "2.0", "Test database from Qt autotests", 1000000); + + db.transaction(function(tx){ + var rs = tx.executeSql('SELECT * FROM Utterance'); + r = "" + for (var i=0; i<rs.rows.length; ++i) { + r += "(" + rs.rows.item(i).type + ": " + rs.rows.item(i).phrase + ")"; + } + if (r == "(Greeting: Hello, world)(Valediction: Goodbye, cruel world)") + r = "passed" + else + r = "WRONG DATA: " + r; + }) + + return r; +} diff --git a/tests/auto/qml/qqmlsqldatabase/data/creation-a.js b/tests/auto/qml/qqmlsqldatabase/data/creation-a.js new file mode 100644 index 0000000000..25cba05451 --- /dev/null +++ b/tests/auto/qml/qqmlsqldatabase/data/creation-a.js @@ -0,0 +1,20 @@ +.import QtQuick.LocalStorage 2.0 as Sql + +function test() { + var r="transaction_not_finished"; + + var db = Sql.openDatabaseSync("QmlTestDB-creation-a", "1.0", "Test database from Qt autotests", 1000000, + function(db) { + db.transaction(function(tx){ + tx.executeSql('CREATE TABLE Greeting(salutation TEXT, salutee TEXT)'); + r = "passed"; + }) + }); + + var db = Sql.openDatabaseSync("QmlTestDB-creation-a", "1.0", "Test database from Qt autotests", 1000000, + function(db) { + r = "FAILED: should have already been created"; + }); + + return r; +} diff --git a/tests/auto/qml/qqmlsqldatabase/data/creation.js b/tests/auto/qml/qqmlsqldatabase/data/creation.js new file mode 100644 index 0000000000..64eb4c7b92 --- /dev/null +++ b/tests/auto/qml/qqmlsqldatabase/data/creation.js @@ -0,0 +1,15 @@ +.import QtQuick.LocalStorage 2.0 as Sql + +function test() { + var r="transaction_not_finished"; + var db = Sql.openDatabaseSync("QmlTestDB-creation", "1.0", "Test database from Qt autotests", 1000000); + db.transaction( + function(tx) { + tx.executeSql('CREATE TABLE IF NOT EXISTS Greeting(salutation TEXT, salutee TEXT)'); + tx.executeSql('INSERT INTO Greeting VALUES(?, ?)', [ 'hello', 'world' ]); + r = "passed"; + } + ); + + return r; +} diff --git a/tests/auto/qml/qqmlsqldatabase/data/error-a.js b/tests/auto/qml/qqmlsqldatabase/data/error-a.js new file mode 100644 index 0000000000..d7e9b4fdff --- /dev/null +++ b/tests/auto/qml/qqmlsqldatabase/data/error-a.js @@ -0,0 +1,22 @@ +.import QtQuick.LocalStorage 2.0 as Sql + +function test() { + var db = Sql.openDatabaseSync("QmlTestDB-error-a", "1.0", "Test database from Qt autotests", 1000000); + var r="transaction_not_finished"; + + try { + db.transaction( + function(tx) { + var rs = tx.executeSql('SELECT * FROM NotExists'); + r = "SHOULD NOT SUCCEED"; + } + ); + } catch (err) { + if (err.message == "no such table: NotExists Unable to execute statement") + r = "passed"; + else + r = "WRONG ERROR="+err.message; + } + + return r; +} diff --git a/tests/auto/qml/qqmlsqldatabase/data/error-b.js b/tests/auto/qml/qqmlsqldatabase/data/error-b.js new file mode 100644 index 0000000000..17e34c989e --- /dev/null +++ b/tests/auto/qml/qqmlsqldatabase/data/error-b.js @@ -0,0 +1,15 @@ +.import QtQuick.LocalStorage 2.0 as Sql + +function test() { + var db = Sql.openDatabaseSync("QmlTestDB-error-b", "1.0", "Test database from Qt autotests", 1000000); + var r="transaction_not_finished"; + + db.transaction( + function(tx) { + tx.executeSql('INSERT INTO Greeting VALUES("junk","junk")'); + notexist[123] = "oops" + } + ); + + return r; +} diff --git a/tests/auto/qml/qqmlsqldatabase/data/error-creation.js b/tests/auto/qml/qqmlsqldatabase/data/error-creation.js new file mode 100644 index 0000000000..d66977d1e2 --- /dev/null +++ b/tests/auto/qml/qqmlsqldatabase/data/error-creation.js @@ -0,0 +1,16 @@ +.import QtQuick.LocalStorage 2.0 as Sql + +function test() { + var r="transaction_not_finished"; + try { + var db = Sql.openDatabaseSync("QmlTestDB-creation", "2.0", "Test database from Qt autotests", 1000000); + } catch (err) { + if (err.code != SQLException.VERSION_ERR) + r = "WRONG ERROR CODE="+err.code; + else if (err.message != "SQL: database version mismatch") + r = "WRONG ERROR="+err.message; + else + r = "passed"; + } + return r; +} diff --git a/tests/auto/qml/qqmlsqldatabase/data/error-notransaction.js b/tests/auto/qml/qqmlsqldatabase/data/error-notransaction.js new file mode 100644 index 0000000000..2cce3f613c --- /dev/null +++ b/tests/auto/qml/qqmlsqldatabase/data/error-notransaction.js @@ -0,0 +1,17 @@ +.import QtQuick.LocalStorage 2.0 as Sql + +function test() { + var db = Sql.openDatabaseSync("QmlTestDB-data/error-notransaction", "1.0", "Test database from Qt autotests", 1000000); + var r="transaction_not_finished"; + + try { + db.transaction(); + } catch (err) { + if (err.message == "transaction: missing callback") + r = "passed"; + else + r = "WRONG ERROR="+err.message; + } + + return r; +} diff --git a/tests/auto/qml/qqmlsqldatabase/data/error-outsidetransaction.js b/tests/auto/qml/qqmlsqldatabase/data/error-outsidetransaction.js new file mode 100644 index 0000000000..7af7c1c6a2 --- /dev/null +++ b/tests/auto/qml/qqmlsqldatabase/data/error-outsidetransaction.js @@ -0,0 +1,19 @@ +.import QtQuick.LocalStorage 2.0 as Sql + +function test() { + var db = Sql.openDatabaseSync("QmlTestDB-data/error-notransaction", "1.0", "Test database from Qt autotests", 1000000); + var r="transaction_not_finished"; + var v; + + try { + db.transaction(function(tx) { v = tx }); + v.executeSql("SELECT 'bad'") + } catch (err) { + if (err.message == "executeSql called outside transaction()") + r = "passed"; + else + r = "WRONG ERROR="+err.message; + } + + return r; +} diff --git a/tests/auto/qml/qqmlsqldatabase/data/iteration-forwardonly.js b/tests/auto/qml/qqmlsqldatabase/data/iteration-forwardonly.js new file mode 100644 index 0000000000..b3df1be113 --- /dev/null +++ b/tests/auto/qml/qqmlsqldatabase/data/iteration-forwardonly.js @@ -0,0 +1,31 @@ +.import QtQuick.LocalStorage 2.0 as Sql + +function test() { + var db = Sql.openDatabaseSync("QmlTestDB-iteration-forwardonly", "", "Test database from Qt autotests", 1000000); + var r="transaction_not_finished"; + + db.transaction( + function(tx) { + tx.executeSql('CREATE TABLE Greeting(salutation TEXT, salutee TEXT)'); + tx.executeSql('INSERT INTO Greeting VALUES ("Hello", "world")'); + tx.executeSql('INSERT INTO Greeting VALUES ("Goodbye", "cruel world")'); + } + ) + + db.transaction( + function(tx) { + var rs = tx.executeSql('SELECT * FROM Greeting'); + rs.forwardOnly = !rs.forwardOnly + var r1="" + for(var i = 0; i < rs.rows.length; i++) + r1 += rs.rows.item(i).salutation + ", " + rs.rows.item(i).salutee + ";" + if (r1 != "hello, world;hello, world;hello, world;hello, world;") + if (r1 != "Hello, world;Goodbye, cruel world;") + r = "SELECTED DATA WRONG: "+r1; + else + r = "passed"; + } + ); + + return r; +} diff --git a/tests/auto/qml/qqmlsqldatabase/data/iteration.js b/tests/auto/qml/qqmlsqldatabase/data/iteration.js new file mode 100644 index 0000000000..8d96dc2154 --- /dev/null +++ b/tests/auto/qml/qqmlsqldatabase/data/iteration.js @@ -0,0 +1,30 @@ +.import QtQuick.LocalStorage 2.0 as Sql + +function test() { + var db = Sql.openDatabaseSync("QmlTestDB-iteration", "", "Test database from Qt autotests", 1000000); + var r="transaction_not_finished"; + + db.transaction( + function(tx) { + tx.executeSql('CREATE TABLE Greeting(salutation TEXT, salutee TEXT)'); + tx.executeSql('INSERT INTO Greeting VALUES ("Hello", "world")'); + tx.executeSql('INSERT INTO Greeting VALUES ("Goodbye", "cruel world")'); + } + ) + + db.transaction( + function(tx) { + var rs = tx.executeSql('SELECT * FROM Greeting'); + var r1="" + for(var i = 0; i < rs.rows.length; i++) + r1 += rs.rows.item(i).salutation + ", " + rs.rows.item(i).salutee + ";" + if (r1 != "hello, world;hello, world;hello, world;hello, world;") + if (r1 != "Hello, world;Goodbye, cruel world;") + r = "SELECTED DATA WRONG: "+r1; + else + r = "passed"; + } + ); + + return r; +} diff --git a/tests/auto/qml/qqmlsqldatabase/data/readonly-error.js b/tests/auto/qml/qqmlsqldatabase/data/readonly-error.js new file mode 100644 index 0000000000..39eb398004 --- /dev/null +++ b/tests/auto/qml/qqmlsqldatabase/data/readonly-error.js @@ -0,0 +1,29 @@ +.import QtQuick.LocalStorage 2.0 as Sql + +function test() { + var r="transaction_not_finished"; + var db = Sql.openDatabaseSync("QmlTestDB-readonly-error", "1.0", "Test database from Qt autotests", 1000000); + + db.transaction( + function(tx) { + tx.executeSql('CREATE TABLE IF NOT EXISTS Greeting(salutation TEXT, salutee TEXT)'); + tx.executeSql('INSERT INTO Greeting VALUES(?, ?)', [ 'hello', 'world' ]); + } + ); + + try { + db.readTransaction( + function(tx) { + tx.executeSql('INSERT INTO Greeting VALUES(?, ?)', [ 'hello', 'world' ]); + r = "FAILED"; + } + ); + } catch (err) { + if (err.message == "Read-only Transaction") + r = "passed"; + else + r = "WRONG ERROR="+err.message; + } + + return r; +} diff --git a/tests/auto/qml/qqmlsqldatabase/data/readonly.js b/tests/auto/qml/qqmlsqldatabase/data/readonly.js new file mode 100644 index 0000000000..985c9ecab5 --- /dev/null +++ b/tests/auto/qml/qqmlsqldatabase/data/readonly.js @@ -0,0 +1,26 @@ +.import QtQuick.LocalStorage 2.0 as Sql + +function test() { + var r="transaction_not_finished"; + var db = Sql.openDatabaseSync("QmlTestDB-readonly", "1.0", "Test database from Qt autotests", 1000000); + + db.transaction( + function(tx) { + tx.executeSql('CREATE TABLE IF NOT EXISTS Greeting(salutation TEXT, salutee TEXT)'); + tx.executeSql('INSERT INTO Greeting VALUES(?, ?)', [ 'hello', 'world' ]); + r = "passed"; + } + ); + + db.readTransaction( + function(tx) { + var rs = tx.executeSql('SELECT * FROM Greeting'); + if (rs.rows.item(0).salutation == 'hello') + r = "passed"; + else + r = "FAILED"; + } + ); + + return r; +} diff --git a/tests/auto/qml/qqmlsqldatabase/data/reopen1.js b/tests/auto/qml/qqmlsqldatabase/data/reopen1.js new file mode 100644 index 0000000000..2171fb028e --- /dev/null +++ b/tests/auto/qml/qqmlsqldatabase/data/reopen1.js @@ -0,0 +1,16 @@ +.import QtQuick.LocalStorage 2.0 as Sql + +function test() { + var r="transaction_not_finished"; + var db = Sql.openDatabaseSync("QmlTestDB-reopen", "1.0", "Test database from Qt autotests", 1000000); + + db.transaction( + function(tx) { + tx.executeSql('CREATE TABLE IF NOT EXISTS Greeting(salutation TEXT, salutee TEXT)'); + tx.executeSql('INSERT INTO Greeting VALUES(?, ?)', [ 'hello', 'world' ]); + r = "passed"; + } + ); + + return r; +} diff --git a/tests/auto/qml/qqmlsqldatabase/data/reopen2.js b/tests/auto/qml/qqmlsqldatabase/data/reopen2.js new file mode 100644 index 0000000000..12c6135145 --- /dev/null +++ b/tests/auto/qml/qqmlsqldatabase/data/reopen2.js @@ -0,0 +1,18 @@ +.import QtQuick.LocalStorage 2.0 as Sql + +function test() { + var r="transaction_not_finished"; + var db = Sql.openDatabaseSync("QmlTestDB-reopen", "1.0", "Test database from Qt autotests", 1000000); + + db.transaction( + function(tx) { + var rs = tx.executeSql('SELECT * FROM Greeting'); + if (rs.rows.item(0).salutation == 'hello') + r = "passed"; + else + r = "FAILED"; + } + ); + + return r; +} diff --git a/tests/auto/qml/qqmlsqldatabase/data/selection-bindnames.js b/tests/auto/qml/qqmlsqldatabase/data/selection-bindnames.js new file mode 100644 index 0000000000..9333ccfacb --- /dev/null +++ b/tests/auto/qml/qqmlsqldatabase/data/selection-bindnames.js @@ -0,0 +1,28 @@ +.import QtQuick.LocalStorage 2.0 as Sql + +function test() { + var db = Sql.openDatabaseSync("QmlTestDB-bindnames", "", "Test database from Qt autotests", 1000000); + var r="transaction_not_finished"; + + db.transaction( + function(tx) { + tx.executeSql('CREATE TABLE IF NOT EXISTS Greeting(salutation TEXT, salutee TEXT)'); + tx.executeSql('INSERT INTO Greeting VALUES(?, ?)', [ 'hello', 'world' ]); + tx.executeSql('INSERT INTO Greeting VALUES(?, ?)', [ 'goodbye', 'world' ]); + tx.executeSql('INSERT INTO Greeting VALUES(?, ?)', [ 'hello', 'world' ]); + tx.executeSql('INSERT INTO Greeting VALUES(?, ?)', [ 'hello', 'there' ]); + } + ); + + db.transaction( + function(tx) { + var rs = tx.executeSql('SELECT * FROM Greeting WHERE salutation=:p2 AND salutee=:p1', {':p1':'world', ':p2':'hello'}); + if ( rs.rows.length != 2 ) + r = "SELECT RETURNED WRONG VALUE "+rs.rows.length+rs.rows.item(0)+rs.rows.item(1) + else + r = "passed"; + } + ); + + return r; +} diff --git a/tests/auto/qml/qqmlsqldatabase/data/selection.js b/tests/auto/qml/qqmlsqldatabase/data/selection.js new file mode 100644 index 0000000000..18d4dce80f --- /dev/null +++ b/tests/auto/qml/qqmlsqldatabase/data/selection.js @@ -0,0 +1,46 @@ +.import QtQuick.LocalStorage 2.0 as Sql + +function test() { + var db = Sql.openDatabaseSync("QmlTestDB-selection", "", "Test database from Qt autotests", 1000000); + var r="transaction_not_finished"; + + db.transaction( + function(tx) { + tx.executeSql('CREATE TABLE IF NOT EXISTS Greeting(salutation TEXT, salutee TEXT)'); + tx.executeSql('INSERT INTO Greeting VALUES(?, ?)', [ 'hello', 'world' ]); + tx.executeSql('INSERT INTO Greeting VALUES(?, ?)', [ 'hello', 'world' ]); + tx.executeSql('CREATE TABLE IF NOT EXISTS TypeTest(num INTEGER, txt1 TEXT, txt2 TEXT)'); + tx.executeSql("INSERT INTO TypeTest VALUES(1, null, 'hello')"); + } + ); + + db.transaction( + function(tx) { + tx.executeSql('INSERT INTO Greeting VALUES(?, ?)', [ 'hello', 'world' ]); + tx.executeSql('INSERT INTO Greeting VALUES(?, ?)', [ 'hello', 'world' ]); + var rs = tx.executeSql('SELECT * FROM Greeting'); + if ( rs.rows.length != 4 ) + r = "SELECT RETURNED WRONG VALUE "+rs.rows.length+rs.rows[0]+rs.rows[1] + else + r = "passed"; + } + ); + if (r == "passed") { + db.transaction(function (tx) { + r = ""; + var firstRow = tx.executeSql("SELECT * FROM TypeTest").rows.item(0); + if (typeof(firstRow.num) != "number") + r += " num:" + firstRow.num+ "type:" + typeof(firstRow.num); + if (typeof(firstRow.txt1) != "object" || firstRow.txt1 != null) + r += " txt1:" + firstRow.txt1 + " type:" + typeof(firstRow.txt1); + if (typeof(firstRow.txt2) != "string" || firstRow.txt2 != "hello") + r += " txt2:" + firstRow.txt2 + " type:" + typeof(firstRow.txt2); + if (r == "") + r = "passed"; + else + r = "SELECT RETURNED VALUES WITH WRONG TYPES " + r; + }); + } + + return r; +} diff --git a/tests/auto/qml/qqmlsqldatabase/qqmlsqldatabase.pro b/tests/auto/qml/qqmlsqldatabase/qqmlsqldatabase.pro new file mode 100644 index 0000000000..c17e16bfdd --- /dev/null +++ b/tests/auto/qml/qqmlsqldatabase/qqmlsqldatabase.pro @@ -0,0 +1,15 @@ +CONFIG += testcase +TARGET = tst_qqmlsqldatabase +macx:CONFIG -= app_bundle + +SOURCES += tst_qqmlsqldatabase.cpp + +include (../../shared/util.pri) + +CONFIG += parallel_test + +testDataFiles.files = data +testDataFiles.path = . +DEPLOYMENT += testDataFiles + +QT += core-private gui-private v8-private qml-private quick-private sql testlib diff --git a/tests/auto/qml/qqmlsqldatabase/tst_qqmlsqldatabase.cpp b/tests/auto/qml/qqmlsqldatabase/tst_qqmlsqldatabase.cpp new file mode 100644 index 0000000000..c1a8a63a4d --- /dev/null +++ b/tests/auto/qml/qqmlsqldatabase/tst_qqmlsqldatabase.cpp @@ -0,0 +1,243 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/ +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** GNU Lesser General Public License Usage +** This file may be used under the terms of the GNU Lesser General Public +** License version 2.1 as published by the Free Software Foundation and +** appearing in the file LICENSE.LGPL included in the packaging of this +** file. Please review the following information to ensure the GNU Lesser +** General Public License version 2.1 requirements will be met: +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU General +** Public License version 3.0 as published by the Free Software Foundation +** and appearing in the file LICENSE.GPL included in the packaging of this +** file. Please review the following information to ensure the GNU General +** Public License version 3.0 requirements will be met: +** http://www.gnu.org/copyleft/gpl.html. +** +** Other Usage +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ +#include <qtest.h> +#include <QtQml/qqmlengine.h> +#include <QtQml/qqmlcomponent.h> +#include <QtQuick/private/qquicktext_p.h> +#include <private/qqmlengine_p.h> +#include <QtCore/qcryptographichash.h> +/* +#include <QtWebKit/qwebpage.h> +#include <QtWebKit/qwebframe.h> +#include <QtWebKit/qwebdatabase.h> +#include <QtWebKit/qwebsecurityorigin.h> +*/ +#include <QtSql/qsqldatabase.h> +#include <QtCore/qdir.h> +#include <QtCore/qfile.h> +#include "../../shared/util.h" + +class tst_qqmlsqldatabase : public QQmlDataTest +{ + Q_OBJECT +public: + tst_qqmlsqldatabase() + { + qApp->setApplicationName("tst_qqmlsqldatabase"); + qApp->setOrganizationName("Nokia"); + qApp->setOrganizationDomain("nokia.com"); + engine = new QQmlEngine; + } + + ~tst_qqmlsqldatabase() + { + delete engine; + } + +private slots: + void initTestCase(); + + void checkDatabasePath(); + + void testQml_data(); + void testQml(); + void testQml_cleanopen_data(); + void testQml_cleanopen(); + void totalDatabases(); + + void cleanupTestCase(); + +private: + QString dbDir() const; + QQmlEngine *engine; +}; + +void removeRecursive(const QString& dirname) +{ + QDir dir(dirname); + QFileInfoList entries(dir.entryInfoList(QDir::Dirs|QDir::Files|QDir::NoDotAndDotDot)); + for (int i = 0; i < entries.count(); ++i) + if (entries[i].isDir()) + removeRecursive(entries[i].filePath()); + else + dir.remove(entries[i].fileName()); + QDir().rmdir(dirname); +} + +void tst_qqmlsqldatabase::initTestCase() +{ + QQmlDataTest::initTestCase(); + removeRecursive(dbDir()); + QDir().mkpath(dbDir()); +} + +void tst_qqmlsqldatabase::cleanupTestCase() +{ + removeRecursive(dbDir()); +} + +QString tst_qqmlsqldatabase::dbDir() const +{ + static QString tmpd = QDir::tempPath()+"/tst_qqmlsqldatabase_output-" + + QDateTime::currentDateTime().toString(QLatin1String("yyyyMMddhhmmss")); + return tmpd; +} + +void tst_qqmlsqldatabase::checkDatabasePath() +{ + // Check default storage path (we can't use it since we don't want to mess with user's data) + QVERIFY(engine->offlineStoragePath().contains("tst_qqmlsqldatabase")); + QVERIFY(engine->offlineStoragePath().contains("OfflineStorage")); +} + +static const int total_databases_created_by_tests = 12; +void tst_qqmlsqldatabase::testQml_data() +{ + QTest::addColumn<QString>("jsfile"); // The input file + + // Each test should use a newly named DB to avoid inter-test dependencies + QTest::newRow("creation") << "creation.js"; + QTest::newRow("creation-a") << "creation-a.js"; + QTest::newRow("creation") << "creation.js"; + QTest::newRow("error-creation") << "error-creation.js"; // re-uses above DB + QTest::newRow("changeversion") << "changeversion.js"; + QTest::newRow("readonly") << "readonly.js"; + QTest::newRow("readonly-error") << "readonly-error.js"; + QTest::newRow("selection") << "selection.js"; + QTest::newRow("selection-bindnames") << "selection-bindnames.js"; + QTest::newRow("iteration") << "iteration.js"; + QTest::newRow("iteration-forwardonly") << "iteration-forwardonly.js"; + QTest::newRow("error-a") << "error-a.js"; + QTest::newRow("error-notransaction") << "error-notransaction.js"; + QTest::newRow("error-outsidetransaction") << "error-outsidetransaction.js"; // reuse above + QTest::newRow("reopen1") << "reopen1.js"; + QTest::newRow("reopen2") << "reopen2.js"; // re-uses above DB + + // If you add a test, you should usually use a new database in the + // test - in which case increment total_databases_created_by_tests above. +} + +/* +class QWebPageWithJavaScriptConsoleMessages : public QWebPage { +public: + void javaScriptConsoleMessage(const QString& message, int lineNumber, const QString& sourceID) + { + qWarning() << sourceID << ":" << lineNumber << ":" << message; + } +}; + +void tst_qqmlsqldatabase::validateAgainstWebkit() +{ + // Validates tests against WebKit (HTML5) support. + // + QFETCH(QString, jsfile); + QFETCH(QString, result); + QFETCH(int, databases); + + QFile f(jsfile); + QVERIFY(f.open(QIODevice::ReadOnly)); + QString js=f.readAll(); + + QWebPageWithJavaScriptConsoleMessages webpage; + webpage.settings()->setOfflineStoragePath(dbDir()); + webpage.settings()->setAttribute(QWebSettings::OfflineStorageDatabaseEnabled, true); + + QEXPECT_FAIL("","WebKit doesn't support openDatabaseSync yet", Continue); + QCOMPARE(webpage.mainFrame()->evaluateJavaScript(js).toString(),result); + + QTest::qWait(100); // WebKit crashes if you quit it too fast + + QWebSecurityOrigin origin = webpage.mainFrame()->securityOrigin(); + QList<QWebDatabase> dbs = origin.databases(); + QCOMPARE(dbs.count(), databases); +} +*/ + +void tst_qqmlsqldatabase::testQml() +{ + // Tests QML SQL Database support with tests + // that have been validated against Webkit. + // + QFETCH(QString, jsfile); + + QString qml= + "import QtQuick 2.0\n" + "import \""+jsfile+"\" as JS\n" + "Text { text: JS.test() }"; + + engine->setOfflineStoragePath(dbDir()); + QQmlComponent component(engine); + component.setData(qml.toUtf8(), testFileUrl("empty.qml")); // just a file for relative local imports + QVERIFY(!component.isError()); + QQuickText *text = qobject_cast<QQuickText*>(component.create()); + QVERIFY(text != 0); + QCOMPARE(text->text(),QString("passed")); +} + +void tst_qqmlsqldatabase::testQml_cleanopen_data() +{ + QTest::addColumn<QString>("jsfile"); // The input file + QTest::newRow("reopen1") << "reopen1.js"; + QTest::newRow("reopen2") << "reopen2.js"; + QTest::newRow("error-creation") << "error-creation.js"; // re-uses creation DB +} + +void tst_qqmlsqldatabase::testQml_cleanopen() +{ + // Same as testQml, but clean connections between tests, + // making it more like the tests are running in new processes. + testQml(); + + engine->collectGarbage(); + + foreach (QString dbname, QSqlDatabase::connectionNames()) { + QSqlDatabase::removeDatabase(dbname); + } +} + +void tst_qqmlsqldatabase::totalDatabases() +{ + QCOMPARE(QDir(dbDir()+"/Databases").entryInfoList(QDir::Files|QDir::NoDotAndDotDot).count(), total_databases_created_by_tests*2); +} + +QTEST_MAIN(tst_qqmlsqldatabase) + +#include "tst_qqmlsqldatabase.moc" |