From 0875626e22ad4e709ddf505e701a8d699559f5b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A5rten=20Nordheim?= Date: Fri, 18 Dec 2020 15:35:20 +0100 Subject: Long live QNetworkInformation The plugins are meant to indicate what they do support, meaning users of QNetworkInformation can choose to not care about which plugin is used and rather just request what they want. Task-number: QTBUG-86966 Change-Id: Ie130e1791250ec2a4470e3ba7081d982654af06c Reviewed-by: Timur Pocheptsov Reviewed-by: Edward Welbourne --- tests/auto/network/kernel/CMakeLists.txt | 1 + .../kernel/qnetworkinformation/CMakeLists.txt | 6 + .../tst_qnetworkinformation.cpp | 156 +++++++++++++++++++++ 3 files changed, 163 insertions(+) create mode 100644 tests/auto/network/kernel/qnetworkinformation/CMakeLists.txt create mode 100644 tests/auto/network/kernel/qnetworkinformation/tst_qnetworkinformation.cpp (limited to 'tests/auto/network/kernel') diff --git a/tests/auto/network/kernel/CMakeLists.txt b/tests/auto/network/kernel/CMakeLists.txt index 8b8960d0ae..89526fa14c 100644 --- a/tests/auto/network/kernel/CMakeLists.txt +++ b/tests/auto/network/kernel/CMakeLists.txt @@ -13,4 +13,5 @@ if(QT_FEATURE_private_tests AND NOT MACOS) endif() if(QT_FEATURE_private_tests) add_subdirectory(qauthenticator) + add_subdirectory(qnetworkinformation) endif() diff --git a/tests/auto/network/kernel/qnetworkinformation/CMakeLists.txt b/tests/auto/network/kernel/qnetworkinformation/CMakeLists.txt new file mode 100644 index 0000000000..f0c37913a8 --- /dev/null +++ b/tests/auto/network/kernel/qnetworkinformation/CMakeLists.txt @@ -0,0 +1,6 @@ +qt_internal_add_test(tst_qnetworkinformation + SOURCES + tst_qnetworkinformation.cpp + PUBLIC_LIBRARIES + Qt::NetworkPrivate +) diff --git a/tests/auto/network/kernel/qnetworkinformation/tst_qnetworkinformation.cpp b/tests/auto/network/kernel/qnetworkinformation/tst_qnetworkinformation.cpp new file mode 100644 index 0000000000..09fa65273d --- /dev/null +++ b/tests/auto/network/kernel/qnetworkinformation/tst_qnetworkinformation.cpp @@ -0,0 +1,156 @@ +/**************************************************************************** +** +** Copyright (C) 2021 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 +#include +#include + +#include +#include + +class MockFactory; +class tst_QNetworkInformation : public QObject +{ + Q_OBJECT +private slots: + void initTestCase(); + void reachability(); + void cleanupTestCase(); + +private: + std::unique_ptr mockFactory; +}; + +static const QString mockName = QStringLiteral("mock"); +class MockBackend : public QNetworkInformationBackend +{ + Q_OBJECT +public: + MockBackend() + { + Q_ASSERT(!instance); + instance = this; + setReachability(QNetworkInformation::Reachability::Online); + } + ~MockBackend() { instance = nullptr; } + + QString name() const override { return mockName; } + + QNetworkInformation::Features featuresSupported() const override + { + return featuresSupportedStatic(); + } + + static void setNewReachability(QNetworkInformation::Reachability value) + { + Q_ASSERT(instance); + instance->setReachability(value); + } + + static QNetworkInformation::Features featuresSupportedStatic() + { + return { QNetworkInformation::Feature::Reachability }; + } + +private: + static inline MockBackend *instance = nullptr; +}; + +class MockFactory : public QNetworkInformationBackendFactory +{ + Q_OBJECT +public: + QString name() const override { return mockName; } + QNetworkInformationBackend * + create(QNetworkInformation::Features requiredFeatures) const override + { + if ((requiredFeatures & featuresSupported()) != requiredFeatures) + return nullptr; + return new MockBackend(); + } + QNetworkInformation::Features featuresSupported() const override + { + return MockBackend::featuresSupportedStatic(); + } +}; + +void tst_QNetworkInformation::initTestCase() +{ + auto prevBackends = QNetworkInformation::availableBackends(); + qDebug() << "available backends:" << prevBackends; + // Creating the factory registers it as a backend + mockFactory = std::make_unique(); + auto backends = QNetworkInformation::availableBackends(); + QVERIFY(backends.size() > prevBackends.size()); + QVERIFY(backends.contains(u"mock")); + QVERIFY(QNetworkInformation::load(u"mock")); + QVERIFY(QNetworkInformation::load(u"mock")); + QVERIFY(!QNetworkInformation::load(u"mocks")); +} + +void tst_QNetworkInformation::cleanupTestCase() +{ + // Make sure the factory gets unregistered on destruction: + mockFactory.reset(); + auto backends = QNetworkInformation::availableBackends(); + QVERIFY(!backends.contains(u"mock")); +} + +void tst_QNetworkInformation::reachability() +{ + auto info = QNetworkInformation::instance(); + QNetworkInformation::Reachability boundIsOnline = QNetworkInformation::Reachability::Unknown; + bool signalEmitted = false; + + connect(info, &QNetworkInformation::reachabilityChanged, this, [&, info]() { + signalEmitted = true; + boundIsOnline = info->reachability(); + }); + QCOMPARE(info->reachability(), QNetworkInformation::Reachability::Online); + MockBackend::setNewReachability(QNetworkInformation::Reachability::Disconnected); + QCoreApplication::processEvents(); + QVERIFY(signalEmitted); + QCOMPARE(info->reachability(), QNetworkInformation::Reachability::Disconnected); + QCOMPARE(boundIsOnline, QNetworkInformation::Reachability::Disconnected); + + // Set the same value again, signal should not be emitted again + signalEmitted = false; + MockBackend::setNewReachability(QNetworkInformation::Reachability::Disconnected); + QCoreApplication::processEvents(); + QVERIFY(!signalEmitted); + + MockBackend::setNewReachability(QNetworkInformation::Reachability::Local); + QCOMPARE(info->reachability(), QNetworkInformation::Reachability::Local); + QCOMPARE(boundIsOnline, QNetworkInformation::Reachability::Local); + MockBackend::setNewReachability(QNetworkInformation::Reachability::Site); + QCOMPARE(info->reachability(), QNetworkInformation::Reachability::Site); + QCOMPARE(boundIsOnline, QNetworkInformation::Reachability::Site); +} + +QTEST_MAIN(tst_QNetworkInformation); +#include "tst_qnetworkinformation.moc" -- cgit v1.2.3