mirror of
https://invent.kde.org/network/falkon.git
synced 2024-12-20 02:36:34 +01:00
Added autotests for QzTools helper functions.
This commit is contained in:
parent
25cf1eedc4
commit
21d2c57dd8
1
.gitignore
vendored
1
.gitignore
vendored
@ -30,3 +30,4 @@ tests/modeltest
|
|||||||
*.swp
|
*.swp
|
||||||
*_manifest.*
|
*_manifest.*
|
||||||
*.embed.manifest
|
*.embed.manifest
|
||||||
|
bin/autotests
|
||||||
|
9
scripts/run_tests.sh
Executable file
9
scripts/run_tests.sh
Executable file
@ -0,0 +1,9 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
# run_tests.sh
|
||||||
|
|
||||||
|
cd ../tests/autotests
|
||||||
|
qmake DEFINES+=NO_SYSTEM_DATAPATH && make
|
||||||
|
cd ../../bin
|
||||||
|
|
||||||
|
clear
|
||||||
|
./autotests
|
@ -1,6 +1,6 @@
|
|||||||
/* ============================================================
|
/* ============================================================
|
||||||
* QupZilla - WebKit based browser
|
* QupZilla - WebKit based browser
|
||||||
* Copyright (C) 2010-2012 David Rosca <nowrep@gmail.com>
|
* Copyright (C) 2010-2013 David Rosca <nowrep@gmail.com>
|
||||||
*
|
*
|
||||||
* This program is free software: you can redistribute it and/or modify
|
* This program is free software: you can redistribute it and/or modify
|
||||||
* it under the terms of the GNU General Public License as published by
|
* it under the terms of the GNU General Public License as published by
|
||||||
@ -69,7 +69,7 @@ QString QzTools::readAllFileContents(const QString &filename)
|
|||||||
return a;
|
return a;
|
||||||
}
|
}
|
||||||
|
|
||||||
return QByteArray();
|
return QString();
|
||||||
}
|
}
|
||||||
|
|
||||||
void QzTools::centerWidgetOnScreen(QWidget* w)
|
void QzTools::centerWidgetOnScreen(QWidget* w)
|
||||||
@ -126,10 +126,15 @@ void QzTools::removeDir(const QString &d)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Finds same part of @one in @other from the beginning */
|
||||||
QString QzTools::samePartOfStrings(const QString &one, const QString &other)
|
QString QzTools::samePartOfStrings(const QString &one, const QString &other)
|
||||||
{
|
{
|
||||||
int i = 0;
|
|
||||||
int maxSize = qMin(one.size(), other.size());
|
int maxSize = qMin(one.size(), other.size());
|
||||||
|
if (maxSize <= 0) {
|
||||||
|
return QString();
|
||||||
|
}
|
||||||
|
|
||||||
|
int i = 0;
|
||||||
while (one.at(i) == other.at(i)) {
|
while (one.at(i) == other.at(i)) {
|
||||||
i++;
|
i++;
|
||||||
if (i == maxSize) {
|
if (i == maxSize) {
|
||||||
@ -139,38 +144,6 @@ QString QzTools::samePartOfStrings(const QString &one, const QString &other)
|
|||||||
return one.left(i);
|
return one.left(i);
|
||||||
}
|
}
|
||||||
|
|
||||||
QUrl QzTools::makeRelativeUrl(const QUrl &baseUrl, const QUrl &rUrl)
|
|
||||||
{
|
|
||||||
QString baseUrlPath = baseUrl.path();
|
|
||||||
QString rUrlPath = rUrl.path();
|
|
||||||
|
|
||||||
QString samePart = QzTools::samePartOfStrings(baseUrlPath, rUrlPath);
|
|
||||||
|
|
||||||
QUrl returnUrl;
|
|
||||||
if (samePart.isEmpty()) {
|
|
||||||
returnUrl = rUrl;
|
|
||||||
}
|
|
||||||
else if (samePart == QLatin1String("/")) {
|
|
||||||
returnUrl = QUrl(rUrl.path());
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
samePart = samePart.left(samePart.lastIndexOf(QLatin1Char('/')) + 1);
|
|
||||||
int slashCount = samePart.count(QLatin1Char('/')) + 1;
|
|
||||||
if (samePart.startsWith(QLatin1Char('/'))) {
|
|
||||||
slashCount--;
|
|
||||||
}
|
|
||||||
if (samePart.endsWith(QLatin1Char('/'))) {
|
|
||||||
slashCount--;
|
|
||||||
}
|
|
||||||
|
|
||||||
rUrlPath.remove(samePart);
|
|
||||||
rUrlPath.prepend(QString("..""/").repeated(slashCount));
|
|
||||||
returnUrl = QUrl(rUrlPath);
|
|
||||||
}
|
|
||||||
|
|
||||||
return returnUrl;
|
|
||||||
}
|
|
||||||
|
|
||||||
QString QzTools::urlEncodeQueryString(const QUrl &url)
|
QString QzTools::urlEncodeQueryString(const QUrl &url)
|
||||||
{
|
{
|
||||||
QString returnString = url.toString(QUrl::RemoveQuery | QUrl::RemoveFragment);
|
QString returnString = url.toString(QUrl::RemoveQuery | QUrl::RemoveFragment);
|
||||||
@ -223,6 +196,11 @@ QString QzTools::ensureUniqueFilename(const QString &name, const QString &append
|
|||||||
QString QzTools::getFileNameFromUrl(const QUrl &url)
|
QString QzTools::getFileNameFromUrl(const QUrl &url)
|
||||||
{
|
{
|
||||||
QString fileName = url.toString(QUrl::RemoveFragment | QUrl::RemoveQuery | QUrl::RemoveScheme | QUrl::RemovePort);
|
QString fileName = url.toString(QUrl::RemoveFragment | QUrl::RemoveQuery | QUrl::RemoveScheme | QUrl::RemovePort);
|
||||||
|
|
||||||
|
if (fileName.endsWith(QLatin1Char('/'))) {
|
||||||
|
fileName = fileName.mid(0, fileName.length() - 1);
|
||||||
|
}
|
||||||
|
|
||||||
if (fileName.indexOf(QLatin1Char('/')) != -1) {
|
if (fileName.indexOf(QLatin1Char('/')) != -1) {
|
||||||
int pos = fileName.lastIndexOf(QLatin1Char('/'));
|
int pos = fileName.lastIndexOf(QLatin1Char('/'));
|
||||||
fileName = fileName.mid(pos);
|
fileName = fileName.mid(pos);
|
||||||
@ -232,7 +210,7 @@ QString QzTools::getFileNameFromUrl(const QUrl &url)
|
|||||||
fileName = filterCharsFromFilename(fileName);
|
fileName = filterCharsFromFilename(fileName);
|
||||||
|
|
||||||
if (fileName.isEmpty()) {
|
if (fileName.isEmpty()) {
|
||||||
fileName = filterCharsFromFilename(url.host().replace(QLatin1Char('.'), QLatin1Char('-')));
|
fileName = filterCharsFromFilename(url.host());
|
||||||
}
|
}
|
||||||
|
|
||||||
return fileName;
|
return fileName;
|
||||||
@ -289,8 +267,7 @@ QString QzTools::fileSizeToString(qint64 size)
|
|||||||
return QObject::tr("Unknown size");
|
return QObject::tr("Unknown size");
|
||||||
}
|
}
|
||||||
|
|
||||||
double _size = (double)size;
|
double _size = size / 1024.0; // KB
|
||||||
_size /= 1024; //kB
|
|
||||||
if (_size < 1000) {
|
if (_size < 1000) {
|
||||||
return QString::number(_size > 1 ? _size : 1, 'f', 0) + " KB";
|
return QString::number(_size > 1 ? _size : 1, 'f', 0) + " KB";
|
||||||
}
|
}
|
||||||
|
@ -44,7 +44,6 @@ bool QT_QUPZILLA_EXPORT removeFile(const QString &fullFileName);
|
|||||||
void QT_QUPZILLA_EXPORT removeDir(const QString &d);
|
void QT_QUPZILLA_EXPORT removeDir(const QString &d);
|
||||||
|
|
||||||
QString QT_QUPZILLA_EXPORT samePartOfStrings(const QString &one, const QString &other);
|
QString QT_QUPZILLA_EXPORT samePartOfStrings(const QString &one, const QString &other);
|
||||||
QUrl QT_QUPZILLA_EXPORT makeRelativeUrl(const QUrl &baseUrl, const QUrl &rUrl);
|
|
||||||
QString QT_QUPZILLA_EXPORT urlEncodeQueryString(const QUrl &url);
|
QString QT_QUPZILLA_EXPORT urlEncodeQueryString(const QUrl &url);
|
||||||
|
|
||||||
QString QT_QUPZILLA_EXPORT ensureUniqueFilename(const QString &name, const QString &appendFormat = QString("(%1)"));
|
QString QT_QUPZILLA_EXPORT ensureUniqueFilename(const QString &name, const QString &appendFormat = QString("(%1)"));
|
||||||
|
53
tests/autotests/autotests.pro
Normal file
53
tests/autotests/autotests.pro
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
isEqual(QT_MAJOR_VERSION, 5) {
|
||||||
|
QT += webkitwidgets network widgets printsupport sql script gui-private
|
||||||
|
} else {
|
||||||
|
QT += core gui webkit sql network script
|
||||||
|
}
|
||||||
|
|
||||||
|
TARGET = autotests
|
||||||
|
CONFIG += qtestlib
|
||||||
|
|
||||||
|
!unix|mac: LIBS += -L$$PWD/../../bin -lQupZilla
|
||||||
|
!mac:unix: LIBS += $$PWD/../../bin/libQupZilla.so
|
||||||
|
|
||||||
|
unix:contains(DEFINES, "NO_SYSTEM_DATAPATH"): QMAKE_LFLAGS+=$${QMAKE_LFLAGS_RPATH}\\$\$ORIGIN
|
||||||
|
|
||||||
|
include($$PWD/../../src/defines.pri)
|
||||||
|
|
||||||
|
DESTDIR = $$PWD/../../bin
|
||||||
|
OBJECTS_DIR = build
|
||||||
|
MOC_DIR = build
|
||||||
|
RCC_DIR = build
|
||||||
|
UI_DIR = build
|
||||||
|
|
||||||
|
INCLUDEPATH += $$PWD/../../src/lib/3rdparty\
|
||||||
|
$$PWD/../../src/lib/app\
|
||||||
|
$$PWD/../../src/lib/autofill\
|
||||||
|
$$PWD/../../src/lib/bookmarks\
|
||||||
|
$$PWD/../../src/lib/cookies\
|
||||||
|
$$PWD/../../src/lib/session\
|
||||||
|
$$PWD/../../src/lib/downloads\
|
||||||
|
$$PWD/../../src/lib/history\
|
||||||
|
$$PWD/../../src/lib/navigation\
|
||||||
|
$$PWD/../../src/lib/network\
|
||||||
|
$$PWD/../../src/lib/other\
|
||||||
|
$$PWD/../../src/lib/preferences\
|
||||||
|
$$PWD/../../src/lib/rss\
|
||||||
|
$$PWD/../../src/lib/tools\
|
||||||
|
$$PWD/../../src/lib/utils\
|
||||||
|
$$PWD/../../src/lib/webview\
|
||||||
|
$$PWD/../../src/lib/plugins\
|
||||||
|
$$PWD/../../src/lib/sidebar\
|
||||||
|
$$PWD/../../src/lib/data\
|
||||||
|
$$PWD/../../src/lib/adblock\
|
||||||
|
$$PWD/../../src/lib/desktopnotifications\
|
||||||
|
$$PWD/../../src/lib/opensearch\
|
||||||
|
$$PWD/../../src/lib/bookmarksimport\
|
||||||
|
$$PWD/../../src/lib/popupwindow\
|
||||||
|
|
||||||
|
HEADERS += \
|
||||||
|
qztoolstest.h
|
||||||
|
|
||||||
|
SOURCES += \
|
||||||
|
qztoolstest.cpp \
|
||||||
|
main.cpp
|
31
tests/autotests/main.cpp
Normal file
31
tests/autotests/main.cpp
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
/* ============================================================
|
||||||
|
* QupZilla - WebKit based browser
|
||||||
|
* Copyright (C) 2010-2013 David Rosca <nowrep@gmail.com>
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
* ============================================================ */
|
||||||
|
#include "qztoolstest.h"
|
||||||
|
|
||||||
|
#include <QtTest/QtTest>
|
||||||
|
|
||||||
|
int main(int argc, char *argv[])
|
||||||
|
{
|
||||||
|
QApplication app(argc, argv);
|
||||||
|
QTEST_DISABLE_KEYPAD_NAVIGATION;
|
||||||
|
|
||||||
|
QzToolsTest qzToolsTest;
|
||||||
|
QTest::qExec(&qzToolsTest, argc, argv);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
69
tests/autotests/qztoolstest.cpp
Normal file
69
tests/autotests/qztoolstest.cpp
Normal file
@ -0,0 +1,69 @@
|
|||||||
|
/* ============================================================
|
||||||
|
* QupZilla - WebKit based browser
|
||||||
|
* Copyright (C) 2010-2013 David Rosca <nowrep@gmail.com>
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
* ============================================================ */
|
||||||
|
#include "qztoolstest.h"
|
||||||
|
#include "qztools.h"
|
||||||
|
|
||||||
|
#include <QtTest/QtTest>
|
||||||
|
|
||||||
|
void QzToolsTest::samePartOfStrings_data()
|
||||||
|
{
|
||||||
|
QTest::addColumn<QString>("string1");
|
||||||
|
QTest::addColumn<QString>("string2");
|
||||||
|
QTest::addColumn<QString>("result");
|
||||||
|
|
||||||
|
// Lorem ipsum dolor sit amet, consectetur adipiscing elit.
|
||||||
|
QTest::newRow("General") << "Lorem ipsum dolor" << "Lorem ipsum dolor Test_1" << "Lorem ipsum dolor";
|
||||||
|
QTest::newRow("OneChar") << "L" << "LTest_1" << "L";
|
||||||
|
QTest::newRow("EmptyReturn") << "Lorem ipsum dolor" << "orem ipsum dolor Test_1" << "";
|
||||||
|
QTest::newRow("EmptyString1") << "" << "orem ipsum dolor Test_1" << "";
|
||||||
|
QTest::newRow("EmptyString2") << "Lorem ipsum dolor" << "" << "";
|
||||||
|
QTest::newRow("EmptyBoth") << "" << "" << "";
|
||||||
|
}
|
||||||
|
|
||||||
|
void QzToolsTest::samePartOfStrings()
|
||||||
|
{
|
||||||
|
QFETCH(QString, string1);
|
||||||
|
QFETCH(QString, string2);
|
||||||
|
QFETCH(QString, result);
|
||||||
|
|
||||||
|
QCOMPARE(QzTools::samePartOfStrings(string1, string2), result);
|
||||||
|
}
|
||||||
|
|
||||||
|
void QzToolsTest::getFileNameFromUrl_data()
|
||||||
|
{
|
||||||
|
QTest::addColumn<QUrl>("url");
|
||||||
|
QTest::addColumn<QString>("result");
|
||||||
|
|
||||||
|
QTest::newRow("Basic") << QUrl("http://www.google.com/filename.html") << "filename.html";
|
||||||
|
QTest::newRow("OnlyHost") << QUrl("http://www.google.com/") << "www.google.com";
|
||||||
|
QTest::newRow("OnlyHostWithoutSlash") << QUrl("http://www.google.com") << "www.google.com";
|
||||||
|
QTest::newRow("EndingDirectory") << QUrl("http://www.google.com/filename/") << "filename";
|
||||||
|
QTest::newRow("EmptyUrl") << QUrl("") << "";
|
||||||
|
QTest::newRow("OnlyScheme") << QUrl("http:") << "";
|
||||||
|
QTest::newRow("FileSchemeUrl") << QUrl("file:///usr/share/test/file.tx") << "file.tx";
|
||||||
|
QTest::newRow("FileSchemeUrlDirectory") << QUrl("file:///usr/share/test/") << "test";
|
||||||
|
QTest::newRow("FileSchemeUrlRoot") << QUrl("file:///") << "";
|
||||||
|
}
|
||||||
|
|
||||||
|
void QzToolsTest::getFileNameFromUrl()
|
||||||
|
{
|
||||||
|
QFETCH(QUrl, url);
|
||||||
|
QFETCH(QString, result);
|
||||||
|
|
||||||
|
QCOMPARE(QzTools::getFileNameFromUrl(url), result);
|
||||||
|
}
|
36
tests/autotests/qztoolstest.h
Normal file
36
tests/autotests/qztoolstest.h
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
/* ============================================================
|
||||||
|
* QupZilla - WebKit based browser
|
||||||
|
* Copyright (C) 2010-2013 David Rosca <nowrep@gmail.com>
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
* ============================================================ */
|
||||||
|
#ifndef QZTOOLSTEST_H
|
||||||
|
#define QZTOOLSTEST_H
|
||||||
|
|
||||||
|
#include <QObject>
|
||||||
|
|
||||||
|
class QzToolsTest : public QObject
|
||||||
|
{
|
||||||
|
Q_OBJECT
|
||||||
|
|
||||||
|
private slots:
|
||||||
|
void samePartOfStrings_data();
|
||||||
|
void samePartOfStrings();
|
||||||
|
|
||||||
|
void getFileNameFromUrl_data();
|
||||||
|
void getFileNameFromUrl();
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // QZTOOLSTEST_H
|
Loading…
Reference in New Issue
Block a user