diff options
author | Jesus Fernandez <jesus.fernandez@qt.io> | 2016-08-24 16:39:04 +0200 |
---|---|---|
committer | Jesus Fernandez <jesus.fernandez@qt.io> | 2016-09-06 16:07:52 +0000 |
commit | a073103a06a08fd053c379ca504d2ceabd413522 (patch) | |
tree | 48422a62a865857a080b760019e9c196f2fd07b4 /tests | |
parent | d25e4dd1b04d66137bb75ca3775b66704a6ad34c (diff) |
Added property tests
New tests to check the signal emission from the setters.
Register the signature method as a metatype.
Change-Id: I4f550df59d5eeddef7db1dd9264ad9e8bd92971f
Reviewed-by: Timur Pocheptsov <timur.pocheptsov@theqtcompany.com>
Diffstat (limited to 'tests')
-rw-r--r-- | tests/auto/abstractoauth/abstractoauth.pro | 6 | ||||
-rw-r--r-- | tests/auto/abstractoauth/tst_abstractoauth.cpp | 79 | ||||
-rw-r--r-- | tests/auto/auto.pro | 4 | ||||
-rw-r--r-- | tests/auto/oauth1/tst_oauth1.cpp | 148 |
4 files changed, 237 insertions, 0 deletions
diff --git a/tests/auto/abstractoauth/abstractoauth.pro b/tests/auto/abstractoauth/abstractoauth.pro new file mode 100644 index 0000000..f82032c --- /dev/null +++ b/tests/auto/abstractoauth/abstractoauth.pro @@ -0,0 +1,6 @@ +TEMPLATE = app +CONFIG += testcase +TARGET = tst_abstractoauth +SOURCES += tst_abstractoauth.cpp + +QT = core core-private network networkauth networkauth-private testlib diff --git a/tests/auto/abstractoauth/tst_abstractoauth.cpp b/tests/auto/abstractoauth/tst_abstractoauth.cpp new file mode 100644 index 0000000..0faf06e --- /dev/null +++ b/tests/auto/abstractoauth/tst_abstractoauth.cpp @@ -0,0 +1,79 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtCore> +#include <QtTest> +#include <QtNetwork> + +#include <QtNetworkAuth/qabstractoauth.h> + +#include <private/qabstractoauth_p.h> + +class tst_AbstractOAuth : public QObject +{ + Q_OBJECT + +private: + struct AbstractOAuth : QAbstractOAuth { + AbstractOAuth() : QAbstractOAuth(*new QAbstractOAuthPrivate(QUrl(), nullptr), nullptr) {} + + virtual QString clientIdentifier() const override { return QString(); } + virtual void setClientIdentifier(const QString &) override {} + virtual QString token() const override { return QString(); } + virtual void setToken(const QString &) override {} + virtual QNetworkReply *head(const QUrl &, const QVariantMap &) override { return nullptr; } + virtual QNetworkReply *get(const QUrl &, const QVariantMap &) override { return nullptr; } + virtual QNetworkReply *post(const QUrl &, const QVariantMap &) override { return nullptr; } + virtual QNetworkReply *deleteResource(const QUrl &, const QVariantMap &) override + { + return nullptr; + } + virtual void grant() override {} + }; + +private Q_SLOTS: + void authorizationUrlSignal(); +}; + +void tst_AbstractOAuth::authorizationUrlSignal() +{ + AbstractOAuth obj; + QUrl expectedValue = QUrl("http://example.net/"); + const QUrl defaultValue = obj.authorizationUrl(); + QVERIFY(expectedValue != defaultValue); + bool emitted = false; + connect(&obj, &QAbstractOAuth::authorizationUrlChanged, [&](const QUrl &value) { + QCOMPARE(expectedValue, value); + emitted = true; + }); + obj.setAuthorizationUrl(expectedValue); + QVERIFY(emitted); +} + +QTEST_MAIN(tst_AbstractOAuth) +#include "tst_abstractoauth.moc" diff --git a/tests/auto/auto.pro b/tests/auto/auto.pro index 4bee3ee..41f3293 100644 --- a/tests/auto/auto.pro +++ b/tests/auto/auto.pro @@ -5,3 +5,7 @@ SUBDIRS += \ oauth1 \ oauth1signature \ oauthhttpserverreplyhandler + +qtConfig(private_tests) { + SUBDIRS += abstractoauth +} diff --git a/tests/auto/oauth1/tst_oauth1.cpp b/tests/auto/oauth1/tst_oauth1.cpp index 0983bf3..1f604ce 100644 --- a/tests/auto/oauth1/tst_oauth1.cpp +++ b/tests/auto/oauth1/tst_oauth1.cpp @@ -74,11 +74,62 @@ public: int waitForFinish(QNetworkReplyPtr &reply); void fillParameters(QVariantMap *parameters, const QUrlQuery &query); + template<class Type> + struct PropertyTester + { + typedef Type InnerType; + typedef void(QOAuth1::*ConstSignalType)(const Type &); + typedef void(QOAuth1::*SignalType)(Type); + typedef QVector<std::function<void(Type *, QOAuth1 *object)>> SetterFunctions; + + private: + // Each entry in setters should set its first parameter to an expected value + // and act on its second, a QOAuth1 object, to trigger signal; this + // function shall check that signal is passed the value the setter previously + // told us to expect. + template<class SignalType> + static void runImpl(SignalType signal, const SetterFunctions &setters) + { + QOAuth1 obj; + Type expectedValue; + QSignalSpy spy(&obj, signal); + connect(&obj, signal, [&](const Type &value) { + QCOMPARE(expectedValue, value); + }); + for (const auto &setter : setters) { + const auto previous = expectedValue; + setter(&expectedValue, &obj); + QVERIFY(previous != expectedValue); // To check if the value was modified + } + QCOMPARE(spy.count(), setters.size()); // The signal should be emitted + } + + public: + + static void run(ConstSignalType signal, const SetterFunctions &setters) + { + runImpl(signal, setters); + } + + static void run(SignalType signal, const SetterFunctions &setters) + { + runImpl(signal, setters); + } + }; + public Q_SLOTS: void finished(); void gotError(); private Q_SLOTS: + void clientIdentifierSignal(); + void clientSharedSecretSignal(); + void tokenSecretSignal(); + void temporaryCredentialsUrlSignal(); + void temporaryTokenCredentialsUrlSignal(); + void tokenCredentialsUrlSignal(); + void signatureMethodSignal(); + void getToken_data(); void getToken(); @@ -132,6 +183,103 @@ void tst_OAuth1::gotError() disconnect(QObject::sender(), SIGNAL(finished()), this, 0); } +void tst_OAuth1::clientIdentifierSignal() +{ + using PropertyTester = PropertyTester<QString>; + PropertyTester::SetterFunctions setters { + [](QString *expectedValue, QOAuth1 *object) { + *expectedValue = "setClientIdentifier"; + object->setClientIdentifier(*expectedValue); + }, + [](QString *expectedValue, QOAuth1 *object) { + *expectedValue = "setClientCredentials"; + object->setClientCredentials(qMakePair(*expectedValue, QString())); + } + }; + PropertyTester::run(&QOAuth1::clientIdentifierChanged, setters); +} + +void tst_OAuth1::clientSharedSecretSignal() +{ + using PropertyTester = PropertyTester<QString>; + PropertyTester::SetterFunctions setters { + [](QString *expectedValue, QOAuth1 *object) { + *expectedValue = "setClientSharedSecret"; + object->setClientSharedSecret(*expectedValue); + }, + [](QString *expectedValue, QOAuth1 *object) { + *expectedValue = "setClientCredentials"; + object->setClientCredentials(qMakePair(QString(), *expectedValue)); + } + }; + PropertyTester::run(&QOAuth1::clientSharedSecretChanged, setters); +} + +void tst_OAuth1::tokenSecretSignal() +{ + using PropertyTester = PropertyTester<QString>; + PropertyTester::SetterFunctions setters { + [](QString *expectedValue, QOAuth1 *object) { + *expectedValue = "setToken"; + object->setToken(*expectedValue); + }, + [](QString *expectedValue, QOAuth1 *object) { + *expectedValue = "setTokenCredentials"; + object->setTokenCredentials(qMakePair(*expectedValue, QString())); + } + }; + PropertyTester::run(&QOAuth1::tokenChanged, setters); +} + +void tst_OAuth1::temporaryCredentialsUrlSignal() +{ + using PropertyTester = PropertyTester<QUrl>; + PropertyTester::SetterFunctions setters { + [](QUrl *expectedValue, QOAuth1 *object) { + *expectedValue = QUrl("http://example.net/"); + object->setTemporaryCredentialsUrl(*expectedValue); + } + }; + PropertyTester::run(&QOAuth1::temporaryCredentialsUrlChanged, setters); +} + +void tst_OAuth1::temporaryTokenCredentialsUrlSignal() +{ + using PropertyTester = PropertyTester<QUrl>; + PropertyTester::SetterFunctions setters { + [](QUrl *expectedValue, QOAuth1 *object) { + *expectedValue = QUrl("http://example.net/"); + object->setTemporaryCredentialsUrl(*expectedValue); + } + }; + PropertyTester::run(&QOAuth1::temporaryCredentialsUrlChanged, setters); +} + +void tst_OAuth1::tokenCredentialsUrlSignal() +{ + using PropertyTester = PropertyTester<QUrl>; + PropertyTester::SetterFunctions setters { + [](QUrl *expectedValue, QOAuth1 *object) { + *expectedValue = QUrl("http://example.net/"); + object->setTokenCredentialsUrl(*expectedValue); + } + }; + PropertyTester::run(&QOAuth1::tokenCredentialsUrlChanged, setters); +} + +void tst_OAuth1::signatureMethodSignal() +{ + using PropertyTester = PropertyTester<QOAuth1::SignatureMethod>; + PropertyTester::SetterFunctions setters { + [](PropertyTester::InnerType *expectedValue, QOAuth1 *object) { + QVERIFY(object->signatureMethod() != QOAuth1::SignatureMethod::PlainText); + *expectedValue = QOAuth1::SignatureMethod::PlainText; + object->setSignatureMethod(*expectedValue); + } + }; + PropertyTester::run(&QOAuth1::signatureMethodChanged, setters); +} + void tst_OAuth1::getToken_data() { QTest::addColumn<StringPair>("clientCredentials"); |