diff options
author | Warwick Allison <warwick.allison@nokia.com> | 2009-09-18 02:57:15 (GMT) |
---|---|---|
committer | Warwick Allison <warwick.allison@nokia.com> | 2009-09-18 02:57:15 (GMT) |
commit | 62615291477b6268263fad5c1f767d6291b541c4 (patch) | |
tree | bdd566135354472f6494b0cad264e324864da59e /tests/auto/declarative/sql | |
parent | f2d5e83afcc30de4d77b30f90df6f82e53796d59 (diff) | |
download | Qt-62615291477b6268263fad5c1f767d6291b541c4.zip Qt-62615291477b6268263fad5c1f767d6291b541c4.tar.gz Qt-62615291477b6268263fad5c1f767d6291b541c4.tar.bz2 |
Make SqlDatabase testable and add a basic test.
Diffstat (limited to 'tests/auto/declarative/sql')
-rw-r--r-- | tests/auto/declarative/sql/data/test1.js | 18 | ||||
-rw-r--r-- | tests/auto/declarative/sql/tst_sql.cpp | 108 |
2 files changed, 126 insertions, 0 deletions
diff --git a/tests/auto/declarative/sql/data/test1.js b/tests/auto/declarative/sql/data/test1.js new file mode 100644 index 0000000..2ae9988 --- /dev/null +++ b/tests/auto/declarative/sql/data/test1.js @@ -0,0 +1,18 @@ +var db; +db = openDatabase("QmlTestDB", "", "Test database from Qt autotests", 1000000); +var r="testerror"; + +// Asynchronous in WebKit, so must wait before calling test() +db.transaction(function(tx) { + r = "passed"; + tx.executeSql('CREATE TABLE IF NOT EXISTS Greeting(salutation TEXT, salutee TEXT)', [], + function(tx, rs) { }, function(tx, error) { r="FAILED: "+error.message }); + tx.executeSql('INSERT INTO Greeting VALUES(?, ?)', [ 'hello', 'world' ], + function(tx, rs) { }, function(tx, error) { r="FAILED: "+error.message }); +}, function(tx, error) { r="TXFAILED: "+error.message }, function(tx, result) { if (r=="testerror") r="passed" }); + + +function test() +{ + return r; +} diff --git a/tests/auto/declarative/sql/tst_sql.cpp b/tests/auto/declarative/sql/tst_sql.cpp new file mode 100644 index 0000000..3179c99 --- /dev/null +++ b/tests/auto/declarative/sql/tst_sql.cpp @@ -0,0 +1,108 @@ +#include <qtest.h> +#include "../../../shared/util.h" +#include <QtDeclarative/qmlengine.h> +#include <QtDeclarative/qmlcomponent.h> +#include <QtDeclarative/qfxtext.h> +#include <QtWebKit/qwebpage.h> +#include <QtWebKit/qwebframe.h> +#include <QtWebKit/qwebdatabase.h> +#include <QtWebKit/qwebsecurityorigin.h> +#include <QtCore/qdir.h> +#include <QtCore/qfile.h> + +class tst_sql : public QObject +{ + Q_OBJECT +public: + tst_sql() {} + +private slots: + void verifyAgainstWebKit_data(); + void verifyAgainstWebKit(); + +private: + QmlEngine engine; +}; + +class QWebPageWithJavaScriptConsoleMessages : public QWebPage { +public: + void javaScriptConsoleMessage(const QString& message, int lineNumber, const QString& sourceID) + { +qDebug() << sourceID << ":" << lineNumber << ":" << message; + } +}; + +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_sql::verifyAgainstWebKit_data() +{ + QTest::addColumn<QString>("jsfile"); // The input file + QTest::addColumn<QString>("result"); // The required output from the js test() function + QTest::addColumn<int>("databases"); // The number of databases that should have been created + + QTest::newRow("basic creation") << "data/test1.js" << "passed" << 1; +} + +void tst_sql::verifyAgainstWebKit() +{ + // Tests QML SQL Database support, and tests the same thing against + // WebKit (HTML5) support to validate the test. + // + // WebKit SQL is asynchronous, so tests are divided into code plus a test() + // function which is executed "later" (via QTRY_). + // + QFETCH(QString, jsfile); + QFETCH(QString, result); + QFETCH(int, databases); + + QString tmpdir = QString(SRCDIR)+"/output"; + + // QML... + QString qml= + "import Qt 4.6\n" + "Text { Script { source: \""+jsfile+"\" } text: test() }"; + + // Check default storage path (we can't use it since we don't want to mess with user's data) + QVERIFY(engine.offlineStoragePath().contains("Nokia")); + QVERIFY(engine.offlineStoragePath().contains("OfflineStorage")); + engine.setOfflineStoragePath(tmpdir); + QmlComponent component(&engine, qml.toUtf8(), QUrl::fromLocalFile(SRCDIR "/empty.qml")); // just a file for relative local imports + QFxText *text = qobject_cast<QFxText*>(component.create()); + QVERIFY(text != 0); + QCOMPARE(text->text(),result); + QCOMPARE(QDir(tmpdir+"/Databases").entryInfoList(QDir::Files|QDir::NoDotAndDotDot).count(), databases*2); // *2 = .ini file + .sqlite file + + // WebKit... + QFile f(jsfile); + QVERIFY(f.open(QIODevice::ReadOnly)); + QString js=f.readAll(); + + QWebPageWithJavaScriptConsoleMessages webpage; + QDir().mkpath(tmpdir); + webpage.settings()->setOfflineStoragePath(tmpdir); + webpage.mainFrame()->evaluateJavaScript(js); + QTest::qWait(200); // WebKit db access is asynchronous + QTRY_COMPARE(webpage.mainFrame()->evaluateJavaScript("test()").toString(),result); + + QWebSecurityOrigin origin = webpage.mainFrame()->securityOrigin(); + QList<QWebDatabase> dbs = origin.databases(); + QCOMPARE(dbs.count(), databases); + + + removeRecursive(tmpdir); +} + +QTEST_MAIN(tst_sql) + +#include "tst_sql.moc" |