QNetworkInformation: Give the manual test a GUI

For mobile platforms. Makes it quite a bit easier to follow on the
updates when I'm not tethered to my PC with ADB.

Change-Id: Icba03470e6082b6e47e31c9ead6df074407d3172
Reviewed-by: Alex Blasche <alexander.blasche@qt.io>
This commit is contained in:
Mårten Nordheim 2021-05-27 15:56:32 +02:00
parent 16db1d2165
commit 1edb51a315
2 changed files with 76 additions and 2 deletions

View File

@ -6,3 +6,9 @@ qt_internal_add_manual_test(qnetworkinformation
Qt::Network
Qt::Test
)
qt_internal_extend_target(qnetworkinformation CONDITION ANDROID OR UIKIT
PUBLIC_LIBRARIES
Qt::Widgets
DEFINES
MOBILE)

View File

@ -26,13 +26,71 @@
**
****************************************************************************/
#ifdef MOBILE
#include <QtWidgets/qapplication.h>
#include <QtWidgets/qmainwindow.h>
#include <QtWidgets/qlabel.h>
#include <QtCore/qmetaobject.h>
#else
#include <QtCore/qcoreapplication.h>
#endif
#include <QtCore/qdebug.h>
#include <QtNetwork/qnetworkinformation.h>
#ifdef MOBILE
template<typename QEnum>
QString enumToString (const QEnum value)
{
return QString::fromUtf8(QMetaEnum::fromType<QEnum>().valueToKey(int(value)));
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow() : QMainWindow(nullptr)
{
label = new QLabel(this);
label->setText("hello");
setCentralWidget(label);
}
void updateReachability(QNetworkInformation::Reachability newValue)
{
reachability = newValue;
updateText();
}
void updateCaptiveState(bool newValue)
{
captive = newValue;
updateText();
}
private:
void updateText()
{
QString str =
QLatin1String("Reachability: %1\nBehind captive portal: %2")
.arg(enumToString(reachability), QStringView(captive ? u"true" : u"false"));
label->setText(str);
}
QLabel *label;
QNetworkInformation::Reachability reachability;
bool captive;
};
#endif
int main(int argc, char **argv)
{
#ifdef MOBILE
QApplication app(argc, argv);
MainWindow window;
window.show();
#else
QCoreApplication app(argc, argv);
#endif
if (!QNetworkInformation::load(QNetworkInformation::Feature::Reachability)) {
qWarning("Failed to load any backend");
@ -44,13 +102,19 @@ int main(int argc, char **argv)
qDebug() << "Now you can make changes to the current network connection. Qt should see the "
"changes and notify about it.";
QObject::connect(info, &QNetworkInformation::reachabilityChanged,
[](QNetworkInformation::Reachability newStatus) {
[&](QNetworkInformation::Reachability newStatus) {
qDebug() << "Updated:" << newStatus;
#ifdef MOBILE
window.updateReachability(newStatus);
#endif
});
QObject::connect(info, &QNetworkInformation::isBehindCaptivePortalChanged,
[](bool status) {
[&](bool status) {
qDebug() << "Updated, behind captive portal:" << status;
#ifdef MOBILE
window.updateCaptiveState(status);
#endif
});
qDebug() << "Initial reachability:" << info->reachability();
@ -58,3 +122,7 @@ int main(int argc, char **argv)
return app.exec();
}
#ifdef MOBILE
#include "tst_qnetworkinformation.moc"
#endif