|
| 1 | +#include "dirconloopbacktestsuite.h" |
| 2 | + |
| 3 | +#include <QCoreApplication> |
| 4 | +#include <QDateTime> |
| 5 | +#include <QEventLoop> |
| 6 | +#include <QSettings> |
| 7 | +#include <functional> |
| 8 | + |
| 9 | +#include "Tools/testsettings.h" |
| 10 | +#include "devices/dircon/dirconmanager.h" |
| 11 | +#include "devices/dircon/wahoodirconbike.h" |
| 12 | +#include "devices/fakebike/fakebike.h" |
| 13 | +#include "qzsettings.h" |
| 14 | + |
| 15 | +namespace { |
| 16 | + |
| 17 | +bool waitForCondition(const std::function<bool()> &predicate, int timeoutMs) { |
| 18 | + const QDateTime endTime = QDateTime::currentDateTime().addMSecs(timeoutMs); |
| 19 | + while (QDateTime::currentDateTime() < endTime) { |
| 20 | + if (predicate()) { |
| 21 | + return true; |
| 22 | + } |
| 23 | + QCoreApplication::processEvents(QEventLoop::AllEvents, 50); |
| 24 | + } |
| 25 | + return predicate(); |
| 26 | +} |
| 27 | + |
| 28 | +} // namespace |
| 29 | + |
| 30 | +void DirconLoopbackTestSuite::test_fakebike_dircon_server_accepts_wahoodirconbike_client() { |
| 31 | + TestSettings testSettings(QStringLiteral("qz-test-org"), QStringLiteral("qz-test-dircon-loopback")); |
| 32 | + testSettings.activate(); |
| 33 | + |
| 34 | + constexpr int kServerBasePort = 46000; |
| 35 | + testSettings.qsettings.setValue(QZSettings::dircon_yes, true); |
| 36 | + testSettings.qsettings.setValue(QZSettings::dircon_server_base_port, kServerBasePort); |
| 37 | + testSettings.qsettings.setValue(QZSettings::dircon_id, 1); |
| 38 | + testSettings.qsettings.setValue(QZSettings::virtual_device_enabled, false); |
| 39 | + testSettings.qsettings.setValue(QZSettings::zwift_play_emulator, false); |
| 40 | + testSettings.qsettings.setValue(QZSettings::rouvy_compatibility, false); |
| 41 | + |
| 42 | + fakebike serverBike(true, true, true); |
| 43 | + DirconManager dirconServer(&serverBike, 4, 1.0); |
| 44 | + |
| 45 | + DirconDeviceInfo deviceInfo; |
| 46 | + deviceInfo.name = QStringLiteral("Wahoo KICKR 0001"); |
| 47 | + deviceInfo.displayName = QStringLiteral("Wahoo KICKR 0001 (DirCon)"); |
| 48 | + deviceInfo.address = QStringLiteral("127.0.0.1"); |
| 49 | + deviceInfo.port = static_cast<quint16>(kServerBasePort); |
| 50 | + |
| 51 | + wahoodirconbike clientBike(deviceInfo, true, true, 4, 1.0); |
| 52 | + |
| 53 | + const bool connected = waitForCondition([&clientBike]() { return clientBike.connected(); }, 5000); |
| 54 | + EXPECT_TRUE(connected) << "DirCon client failed to complete handshake against local fakebike DirCon server"; |
| 55 | + |
| 56 | + // Drive the fakebike with target power to force metric updates on the DirCon server side, |
| 57 | + // then verify the client receives non-zero telemetry through notifications. |
| 58 | + serverBike.changePower(180); |
| 59 | + |
| 60 | + const bool receivedTelemetry = waitForCondition( |
| 61 | + [&clientBike]() { |
| 62 | + return clientBike.wattsMetric().value() > 0 && |
| 63 | + clientBike.currentCadence().value() > 0 && |
| 64 | + clientBike.currentSpeed().value() > 0; |
| 65 | + }, |
| 66 | + 8000); |
| 67 | + EXPECT_TRUE(receivedTelemetry) |
| 68 | + << "DirCon client connected but did not receive expected bike telemetry (watt/cadence/speed)"; |
| 69 | + |
| 70 | + testSettings.deactivate(); |
| 71 | +} |
0 commit comments