summaryrefslogtreecommitdiffstats
path: root/tests/auto/network/kernel
diff options
context:
space:
mode:
authorMårten Nordheim <marten.nordheim@qt.io>2021-10-25 20:23:24 +0200
committerMårten Nordheim <marten.nordheim@qt.io>2021-11-04 12:02:25 +0100
commitbd52c1bba6f5a0b7e820596e566146b5cf8a4ee7 (patch)
tree728aa2010288daee21336992428d57e306f22282 /tests/auto/network/kernel
parent2148e1f0e6026ad7a87c7eec0362d1250c1f09e9 (diff)
QNI: Add API to check if connection is metered
This may be a useful factor in deciding whether or not you should perform communications over the network which are not purely essential. For example, if you have a logging mechanism you can delay uploading them until you are no longer on a metered network. Task-number: QTBUG-91024 Change-Id: I19d32f031a3893512dc440914133678004987fb1 Reviewed-by: Paul Wicking <paul.wicking@qt.io> Reviewed-by: Marc Mutz <marc.mutz@qt.io> Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
Diffstat (limited to 'tests/auto/network/kernel')
-rw-r--r--tests/auto/network/kernel/qnetworkinformation/tst_qnetworkinformation.cpp36
1 files changed, 32 insertions, 4 deletions
diff --git a/tests/auto/network/kernel/qnetworkinformation/tst_qnetworkinformation.cpp b/tests/auto/network/kernel/qnetworkinformation/tst_qnetworkinformation.cpp
index b78983ae40..e37db514e3 100644
--- a/tests/auto/network/kernel/qnetworkinformation/tst_qnetworkinformation.cpp
+++ b/tests/auto/network/kernel/qnetworkinformation/tst_qnetworkinformation.cpp
@@ -29,6 +29,7 @@
#include <QtNetwork/private/qnetworkinformation_p.h>
#include <QtNetwork/qnetworkinformation.h>
#include <QtTest/qtest.h>
+#include <QtTest/qsignalspy.h>
#include <limits>
#include <memory>
@@ -43,6 +44,7 @@ private slots:
void reachability();
void behindCaptivePortal();
void transportMedium();
+ void isMetered();
void cleanupTestCase();
private:
@@ -88,11 +90,18 @@ public:
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
- | QNetworkInformation::Feature::TransportMedium };
+ | QNetworkInformation::Feature::TransportMedium
+ | QNetworkInformation::Feature::Metered };
}
private:
@@ -144,9 +153,10 @@ void tst_QNetworkInformation::supportedFeatures()
{
auto info = QNetworkInformation::instance();
- auto allFeatures = QNetworkInformation::Features(
- QNetworkInformation::Feature::CaptivePortal | QNetworkInformation::Feature::Reachability
- | QNetworkInformation::Feature::TransportMedium);
+ auto allFeatures = QNetworkInformation::Features(QNetworkInformation::Feature::CaptivePortal
+ | QNetworkInformation::Feature::Reachability
+ | QNetworkInformation::Feature::TransportMedium
+ | QNetworkInformation::Feature::Metered);
QCOMPARE(info->supportedFeatures(), allFeatures);
@@ -154,6 +164,7 @@ void tst_QNetworkInformation::supportedFeatures()
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()
@@ -248,5 +259,22 @@ void tst_QNetworkInformation::transportMedium()
QVERIFY(!signalEmitted);
}
+void tst_QNetworkInformation::isMetered()
+{
+ auto info = QNetworkInformation::instance();
+
+ QSignalSpy spy(info, &QNetworkInformation::isMeteredChanged);
+ QVERIFY(!info->isMetered());
+ MockBackend::setNewMetered(true);
+ QCOMPARE(spy.count(), 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.count(), 0);
+}
+
QTEST_MAIN(tst_QNetworkInformation);
#include "tst_qnetworkinformation.moc"