Skip to content

Commit

Permalink
[Libsqlite tests] Resources for sqlite tests
Browse files Browse the repository at this point in the history
  • Loading branch information
RAJAGOPALAN-GANGADHARAN committed Nov 5, 2020
1 parent cf215db commit 2057c3f
Show file tree
Hide file tree
Showing 5 changed files with 175 additions and 0 deletions.
1 change: 1 addition & 0 deletions tests/webkitwidgets/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ set(QtWK1ApiTests
qwebpage
qwebsecurityorigin
qwebview
sqlite
)

set(tst_hybridPixmap_SOURCES hybridPixmap/widget.cpp)
Expand Down
41 changes: 41 additions & 0 deletions tests/webkitwidgets/sqlite/resources/test.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<html>
<script>
var db, res = "none";
var request = window.indexedDB.open("testDataBase", 3);
request.onerror = function (event) {
res = "Failed Database Creation";
};

request.onsuccess = function (event) {
res = "Database Success";
db = request.result;
};

request.onupgradeneeded = function (event) {
var db = event.target.result;
var objectStore = db.createObjectStore("testTable", { keyPath: "id" });
}

function read(id) {
var transaction = db.transaction(["testTable"]);
var objectStore = transaction.objectStore("testTable");
var request = objectStore.get(id);

request.onerror = function (event) {
res = `Unable to get ${id}`;
};

request.onsuccess = function (event) {
if (request.result) res = request.result.name;
else res = `Unable to get ${id}`;
};
}

function add(id, name) {
var request = db.transaction(["testTable"], "readwrite")
.objectStore("testTable")
.add({ id: id, name: name });
}

</script>
</html>
2 changes: 2 additions & 0 deletions tests/webkitwidgets/sqlite/sqlite.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
include(../tests.pri)
exists($${TARGET}.qrc):RESOURCES += $${TARGET}.qrc
126 changes: 126 additions & 0 deletions tests/webkitwidgets/sqlite/tst_sqlite.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
/*
* Copyright (C) 2020 Rajagopalan-Gangadharan <g.raju2000@gmail.com>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/

#include <QtTest/QtTest>
#include "../util.h"

#include <QDir>
#include <qwebpage.h>
#include <qwebframe.h>

static 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);
}


class tst_Sqlite : public QObject
{
Q_OBJECT

public:
tst_Sqlite();
~tst_Sqlite();

public Q_SLOTS:
void initTestCase();
void cleanupTestCase();
private Q_SLOTS:
void indexedDBTest();

private:
QString tmpDirPath() const
{
static QString tmpd = QDir::tempPath() + "/tst_sqlite-"
+ QDateTime::currentDateTime().toString(QLatin1String("yyyyMMddhhmmss"));
return tmpd;
}
};

tst_Sqlite::tst_Sqlite()
{
}

tst_Sqlite::~tst_Sqlite()
{
}

void tst_Sqlite::initTestCase()
{
}

void tst_Sqlite::cleanupTestCase()
{
removeRecursive(tmpDirPath());
}

void tst_Sqlite::indexedDBTest()
{
QString path = tmpDirPath();
QWebPage page;

page.settings()->setOfflineStoragePath(path);
QVERIFY(page.settings()->offlineStoragePath() == path);

QWebSettings::setOfflineStorageDefaultQuota(1024 * 1024);
QVERIFY(QWebSettings::offlineStorageDefaultQuota() == 1024 * 1024);

page.settings()->setAttribute(QWebSettings::OfflineStorageDatabaseEnabled, true);

QString dbFileName = path + "Databases.db";

if (QFile::exists(dbFileName))
QFile::remove(dbFileName);

QSignalSpy loadSpy(&page, SIGNAL(loadFinished(bool)));
page.mainFrame()->load(QUrl("qrc:///test.html"));

waitForSignal(&page, SIGNAL(loadFinished(bool)));
QCOMPARE(loadSpy.count(), 1);

waitForSignal(&page, SIGNAL(loadFinished(bool)), 2000);

QVariant res = page.mainFrame()->evaluateJavaScript("res");
QCOMPARE(res.toString(), QStringLiteral("Database Success"));

page.mainFrame()->evaluateJavaScript("add('1','Name1')");
waitForSignal(&page, SIGNAL(loadFinished(bool)), 1000);

page.mainFrame()->evaluateJavaScript("read('1')");
waitForSignal(&page, SIGNAL(loadFinished(bool)), 1000);

res = page.mainFrame()->evaluateJavaScript("res");
QCOMPARE(res.toString(), QStringLiteral("Name1"));
}

QTEST_MAIN(tst_Sqlite)
#include "tst_sqlite.moc"
5 changes: 5 additions & 0 deletions tests/webkitwidgets/sqlite/tst_sqlite.qrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<!DOCTYPE RCC><RCC version="1.0">
<qresource prefix="/">
<file alias="test.html">resources/test.html</file>
</qresource>
</RCC>

0 comments on commit 2057c3f

Please sign in to comment.