From 88c2c254909e73268f3cfd879741f4960d54de27 Mon Sep 17 00:00:00 2001 From: nowrep Date: Fri, 25 Jan 2013 16:59:42 +0100 Subject: [PATCH] [Tests] Add tests for PageFormCompleter. Also updated copyright on other tests. --- tests/autotests/autotests.pro | 6 +- tests/autotests/formcompletertest.cpp | 185 ++++++++++++++++++++++++++ tests/autotests/formcompletertest.h | 52 ++++++++ tests/autotests/main.cpp | 6 +- tests/autotests/qztoolstest.cpp | 2 +- tests/autotests/qztoolstest.h | 2 +- 6 files changed, 248 insertions(+), 5 deletions(-) create mode 100644 tests/autotests/formcompletertest.cpp create mode 100644 tests/autotests/formcompletertest.h diff --git a/tests/autotests/autotests.pro b/tests/autotests/autotests.pro index 5e510b06b..a7fdd0fd0 100644 --- a/tests/autotests/autotests.pro +++ b/tests/autotests/autotests.pro @@ -46,8 +46,10 @@ INCLUDEPATH += $$PWD/../../src/lib/3rdparty\ $$PWD/../../src/lib/popupwindow\ HEADERS += \ - qztoolstest.h + qztoolstest.h \ + formcompletertest.h SOURCES += \ qztoolstest.cpp \ - main.cpp + main.cpp \ + formcompletertest.cpp diff --git a/tests/autotests/formcompletertest.cpp b/tests/autotests/formcompletertest.cpp new file mode 100644 index 000000000..a31175f0a --- /dev/null +++ b/tests/autotests/formcompletertest.cpp @@ -0,0 +1,185 @@ +/* ============================================================ +* QupZilla - WebKit based browser +* Copyright (C) 2013 David Rosca +* +* 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 . +* ============================================================ */ +#include "formcompletertest.h" +#include "pageformcompleter.h" + +#include +#include +#include + +void FormCompleterTest::initTestCase() +{ + view = new QWebView(); +} + +void FormCompleterTest::cleanupTestCase() +{ + delete view; +} + +void FormCompleterTest::init() +{ + view->setHtml(QString()); +} + +void FormCompleterTest::completePageTest1() +{ + // Basic test + + QByteArray data = "username=tst_username&password=tst_password"; + + QString html = "
" + "" + "" + "" + "
"; + + completeWithData(html, data); + + QCOMPARE(getElementByIdValue("id1").toString(), QString("tst_username")); + QCOMPARE(getElementByIdValue("id2").toString(), QString("tst_password")); +} + +void FormCompleterTest::completePageTest2() +{ + // Real word test: GMail login + // uses input type=email for username + + QByteArray data = "dsh=-821344601702946291&GALX=pAkf3B9TdCo&timeStmp=&secTok=" + "&_utf8=%E2%98%83&bgresponse=%21A0IjdiuCmhg1wERY2iLkbgozxgIAAAAOUgAAA" + "AcqAMaxqx-r1kXOOzeHkuDrRiOJvacFeSxGJ-pg_KQbwMJk3mBUCoZO2g602Uq4upHsM" + "KVfjKRkuPCC0bVCaglP90VLBN8lqCQ5zLSrczVa-WpBFXlEZsKm5UXasE2ZZUQfDc1MI" + "VQbDUviv7Ap54jx6vlTinen6UlxWW_wAvtLkpSO1hqrWnDSDmvFtbJZX61BlMFoHTPYk" + "ijYnuCWzrHWsfKVI8uigtpClgwBTGovCWzuLrbFhG6txV5SokxdfNhbr3Vv-zO9xCw&E" + "mail=tst_mail%40google.com&&Passwd=pass+%CB%87+word1&signIn=P%+se&Per" + "sistentCookie=yes&rmShown=1"; + + QString html = "
" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "
" + "" + "" + "
" + "" + "" + "" + "
"; + + completeWithData(html, data); + + QCOMPARE(getElementByIdValue("Email").toString(), QString("tst_mail@google.com")); + QCOMPARE(getElementByIdValue("Passwd").toString(), QString::fromUtf8("pass ˇ word1")); +} + +void FormCompleterTest::completePageTest3() +{ + // This test is mainly to test properly decoding special characters + // in post data. + + QByteArray data = "user=%2B%C4%9B%C5%A1S+%CB%87+-+%2520+%2F+aa_&" + "pass=%C3%BD%C5%BE%C4%9B%C5%A1%2B%C3%AD%C3%A1+das+%2B%2F+%5C+%C4%91"; + + QString html = "
" + "" + "" + "" + "
"; + + completeWithData(html, data); + + QCOMPARE(getElementByIdValue("id1").toString(), QString::fromUtf8("+ěšS ˇ - %20 / aa_")); + QCOMPARE(getElementByIdValue("id2").toString(), QString::fromUtf8("ýžěš+íá das +/ \\ đ")); +} + +void FormCompleterTest::extractFormTest1() +{ + // Basic test + + QByteArray data = "username=tst_username&password=tst_password"; + + QString html = "
" + "" + "" + "" + "
"; + + PageFormData form = extractFormData(html, data); + + QVERIFY(form.found == true); + QCOMPARE(form.username, QString("tst_username")); + QCOMPARE(form.password, QString("tst_password")); +} + +void FormCompleterTest::extractFormTest2() +{ + // Test special characters + + QByteArray data = "username=tst_username&password=tst_password"; + + QString html = QString::fromUtf8("
" + "" + "" + "" + "
"); + + PageFormData form = extractFormData(html, data); + + QVERIFY(form.found == true); + QCOMPARE(form.username, QString::fromUtf8("+ě ++ éí§`]|~đ11 +!:")); + QCOMPARE(form.password, QString::fromUtf8("+ěš asn~đ°#&# |\€")); +} + +void FormCompleterTest::completeWithData(const QString &html, const QByteArray &data) +{ + view->setHtml(html); + + PageFormCompleter completer(view->page()); + completer.completePage(data); +} + +PageFormData FormCompleterTest::extractFormData(const QString &html, const QByteArray &data) +{ + view->setHtml(html); + + PageFormCompleter completer(view->page()); + return completer.extractFormData(data); +} + +QVariant FormCompleterTest::getElementByIdValue(const QString &id) +{ + QString source = QString("document.getElementById('%1').value").arg(id); + return view->page()->mainFrame()->evaluateJavaScript(source); +} diff --git a/tests/autotests/formcompletertest.h b/tests/autotests/formcompletertest.h new file mode 100644 index 000000000..8a2d824e9 --- /dev/null +++ b/tests/autotests/formcompletertest.h @@ -0,0 +1,52 @@ +/* ============================================================ +* QupZilla - WebKit based browser +* Copyright (C) 2013 David Rosca +* +* 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 . +* ============================================================ */ +#ifndef FORMPAGECOMPLETERTEST_H +#define FORMPAGECOMPLETERTEST_H + +#include +#include + +class QWebView; +struct PageFormData; + +class FormCompleterTest : public QObject +{ + Q_OBJECT + +private slots: + void initTestCase(); + void cleanupTestCase(); + + void init(); + + void completePageTest1(); + void completePageTest2(); + void completePageTest3(); + + void extractFormTest1(); + void extractFormTest2(); + +private: + void completeWithData(const QString &html, const QByteArray &data); + PageFormData extractFormData(const QString &html, const QByteArray &data); + QVariant getElementByIdValue(const QString &id); + QWebView *view; + +}; + +#endif // FORMPAGECOMPLETERTEST_H diff --git a/tests/autotests/main.cpp b/tests/autotests/main.cpp index 3d07968d0..29477d0c0 100644 --- a/tests/autotests/main.cpp +++ b/tests/autotests/main.cpp @@ -1,6 +1,6 @@ /* ============================================================ * QupZilla - WebKit based browser -* Copyright (C) 2010-2013 David Rosca +* Copyright (C) 2013 David Rosca * * 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 @@ -16,6 +16,7 @@ * along with this program. If not, see . * ============================================================ */ #include "qztoolstest.h" +#include "formcompletertest.h" #include @@ -27,5 +28,8 @@ int main(int argc, char *argv[]) QzToolsTest qzToolsTest; QTest::qExec(&qzToolsTest, argc, argv); + FormCompleterTest formCompleterTest; + QTest::qExec(&formCompleterTest, argc, argv); + return 0; } diff --git a/tests/autotests/qztoolstest.cpp b/tests/autotests/qztoolstest.cpp index 0465f3f22..4c1a2f51c 100644 --- a/tests/autotests/qztoolstest.cpp +++ b/tests/autotests/qztoolstest.cpp @@ -1,6 +1,6 @@ /* ============================================================ * QupZilla - WebKit based browser -* Copyright (C) 2010-2013 David Rosca +* Copyright (C) 2013 David Rosca * * 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 diff --git a/tests/autotests/qztoolstest.h b/tests/autotests/qztoolstest.h index 99ca34540..71725aee2 100644 --- a/tests/autotests/qztoolstest.h +++ b/tests/autotests/qztoolstest.h @@ -1,6 +1,6 @@ /* ============================================================ * QupZilla - WebKit based browser -* Copyright (C) 2010-2013 David Rosca +* Copyright (C) 2013 David Rosca * * 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