summaryrefslogtreecommitdiffstats
path: root/tests/auto/network/kernel/qnetworkinformation/tst_qnetworkinformation.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'tests/auto/network/kernel/qnetworkinformation/tst_qnetworkinformation.cpp')
-rw-r--r--tests/auto/network/kernel/qnetworkinformation/tst_qnetworkinformation.cpp128
1 files changed, 96 insertions, 32 deletions
diff --git a/tests/auto/network/kernel/qnetworkinformation/tst_qnetworkinformation.cpp b/tests/auto/network/kernel/qnetworkinformation/tst_qnetworkinformation.cpp
index 92dba1e7c1..daf81823e8 100644
--- a/tests/auto/network/kernel/qnetworkinformation/tst_qnetworkinformation.cpp
+++ b/tests/auto/network/kernel/qnetworkinformation/tst_qnetworkinformation.cpp
@@ -1,34 +1,10 @@
-/****************************************************************************
-**
-** 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$
-**
-****************************************************************************/
+// Copyright (C) 2021 The Qt Company Ltd.
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
#include <QtNetwork/private/qnetworkinformation_p.h>
#include <QtNetwork/qnetworkinformation.h>
#include <QtTest/qtest.h>
+#include <QtTest/qsignalspy.h>
#include <limits>
#include <memory>
@@ -39,8 +15,11 @@ class tst_QNetworkInformation : public QObject
Q_OBJECT
private slots:
void initTestCase();
+ void supportedFeatures();
void reachability();
void behindCaptivePortal();
+ void transportMedium();
+ void isMetered();
void cleanupTestCase();
private:
@@ -80,10 +59,24 @@ public:
instance->setBehindCaptivePortal(value);
}
+ static void setNewTransportMedium(QNetworkInformation::TransportMedium medium)
+ {
+ Q_ASSERT(instance);
+ instance->setTransportMedium(medium);
+ }
+
+ static void setNewMetered(bool metered)
+ {
+ Q_ASSERT(instance);
+ instance->setMetered(metered);
+ }
+
static QNetworkInformation::Features featuresSupportedStatic()
{
- return { QNetworkInformation::Feature::Reachability,
- QNetworkInformation::Feature::CaptivePortal };
+ return { QNetworkInformation::Feature::Reachability
+ | QNetworkInformation::Feature::CaptivePortal
+ | QNetworkInformation::Feature::TransportMedium
+ | QNetworkInformation::Feature::Metered };
}
private:
@@ -117,9 +110,10 @@ void tst_QNetworkInformation::initTestCase()
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"));
+ QVERIFY(QNetworkInformation::loadBackendByName(u"mock"));
+ QVERIFY(QNetworkInformation::loadBackendByName(u"mock"));
+ QVERIFY(QNetworkInformation::loadBackendByName(u"mOcK"));
+ QVERIFY(!QNetworkInformation::loadBackendByName(u"mocks"));
}
void tst_QNetworkInformation::cleanupTestCase()
@@ -130,6 +124,24 @@ void tst_QNetworkInformation::cleanupTestCase()
QVERIFY(!backends.contains(u"mock"));
}
+void tst_QNetworkInformation::supportedFeatures()
+{
+ auto info = QNetworkInformation::instance();
+
+ auto allFeatures = QNetworkInformation::Features(QNetworkInformation::Feature::CaptivePortal
+ | QNetworkInformation::Feature::Reachability
+ | QNetworkInformation::Feature::TransportMedium
+ | QNetworkInformation::Feature::Metered);
+
+ QCOMPARE(info->supportedFeatures(), allFeatures);
+
+ QVERIFY(info->supports(allFeatures));
+ QVERIFY(info->supports(QNetworkInformation::Feature::CaptivePortal));
+ QVERIFY(info->supports(QNetworkInformation::Feature::Reachability));
+ QVERIFY(info->supports(QNetworkInformation::Feature::TransportMedium));
+ QVERIFY(info->supports(QNetworkInformation::Feature::Metered));
+}
+
void tst_QNetworkInformation::reachability()
{
auto info = QNetworkInformation::instance();
@@ -187,5 +199,57 @@ void tst_QNetworkInformation::behindCaptivePortal()
QVERIFY(!signalEmitted);
}
+void tst_QNetworkInformation::transportMedium()
+{
+ auto info = QNetworkInformation::instance();
+ using TransportMedium = QNetworkInformation::TransportMedium;
+ TransportMedium medium = TransportMedium::Unknown;
+ bool signalEmitted = false;
+
+ connect(info, &QNetworkInformation::transportMediumChanged, this, [&, info](TransportMedium tm) {
+ signalEmitted = true;
+ QCOMPARE(tm, info->transportMedium());
+ medium = info->transportMedium();
+ });
+ QCOMPARE(info->transportMedium(), TransportMedium::Unknown); // Default is unknown
+
+ auto transportMediumEnum = QMetaEnum::fromType<TransportMedium>();
+ auto mediumCount = transportMediumEnum.keyCount();
+ // Verify index 0 is Unknown and skip it in the loop, it's the default.
+ QCOMPARE(TransportMedium(transportMediumEnum.value(0)), TransportMedium::Unknown);
+ for (int i = 1; i < mediumCount; ++i) {
+ signalEmitted = false;
+ TransportMedium m = TransportMedium(transportMediumEnum.value(i));
+ MockBackend::setNewTransportMedium(m);
+ QCoreApplication::processEvents();
+ QVERIFY(signalEmitted);
+ QCOMPARE(info->transportMedium(), m);
+ QCOMPARE(medium, m);
+ }
+
+ // Set the current value again, signal should not be emitted again
+ signalEmitted = false;
+ MockBackend::setNewTransportMedium(medium);
+ QCoreApplication::processEvents();
+ QVERIFY(!signalEmitted);
+}
+
+void tst_QNetworkInformation::isMetered()
+{
+ auto info = QNetworkInformation::instance();
+
+ QSignalSpy spy(info, &QNetworkInformation::isMeteredChanged);
+ QVERIFY(!info->isMetered());
+ MockBackend::setNewMetered(true);
+ QCOMPARE(spy.size(), 1);
+ QVERIFY(info->isMetered());
+ QVERIFY(spy[0][0].toBool());
+ spy.clear();
+
+ // Set the same value again, signal should not be emitted again
+ MockBackend::setNewMetered(true);
+ QCOMPARE(spy.size(), 0);
+}
+
QTEST_MAIN(tst_QNetworkInformation);
#include "tst_qnetworkinformation.moc"