// SegmentBarsWidget unit tests. Lane GUI. // // Red when: a row stops being reused across ticks (rebuilt instead of patched), a // zero/unknown-length segment stops rendering as indeterminate, or a shrinking segment // set leaves stale rows visible instead of hiding them. #include #include #include "widgets/SegmentBarsWidget.hpp" using velox::gui::SegmentBarsWidget; using velox::gui::SegmentInfo; class TstSegmentBarsWidget : public QObject { Q_OBJECT private slots: void determinateShowsPercent(); void unknownLengthIsIndeterminate(); void shrinkingSetHidesStaleRows(); void rowsAreReusedNotRebuilt(); }; void TstSegmentBarsWidget::determinateShowsPercent() { SegmentBarsWidget w; SegmentInfo s; s.index = 0; s.downloadedBytes = 50; s.totalBytes = 200; s.state = QStringLiteral("downloading"); w.setSegments({s}); auto *bar = w.findChild(); QVERIFY(bar != nullptr); QCOMPARE(bar->minimum(), 0); QCOMPARE(bar->maximum(), 10000); QCOMPARE(bar->value(), 2500); // 50/200 == 25% } void TstSegmentBarsWidget::unknownLengthIsIndeterminate() { SegmentBarsWidget w; SegmentInfo s; s.index = 0; s.totalBytes = -1; // range not known yet w.setSegments({s}); auto *bar = w.findChild(); QVERIFY(bar != nullptr); QCOMPARE(bar->minimum(), 0); QCOMPARE(bar->maximum(), 0); // Qt's indeterminate-range convention } void TstSegmentBarsWidget::shrinkingSetHidesStaleRows() { SegmentBarsWidget w; SegmentInfo a; a.index = 0; a.totalBytes = 100; SegmentInfo b; b.index = 1; b.totalBytes = 100; w.setSegments({a, b}); const auto barsBefore = w.findChildren(); QCOMPARE(barsBefore.size(), 2); for (auto *bar : barsBefore) { QVERIFY(!bar->parentWidget()->isHidden()); } w.setSegments({a}); // segment 1 dropped (re-segmented retry with fewer connections) const auto barsAfter = w.findChildren(); QCOMPARE(barsAfter.size(), 2); // row not destroyed... QVERIFY(!barsAfter[0]->parentWidget()->isHidden()); QVERIFY(barsAfter[1]->parentWidget()->isHidden()); // ...just hidden } void TstSegmentBarsWidget::rowsAreReusedNotRebuilt() { SegmentBarsWidget w; SegmentInfo s; s.index = 0; s.totalBytes = 100; w.setSegments({s}); auto *barFirst = w.findChild(); QVERIFY(barFirst != nullptr); s.downloadedBytes = 40; w.setSegments({s}); auto *barSecond = w.findChild(); QCOMPARE(barFirst, barSecond); // same widget instance, just repainted } QTEST_MAIN(TstSegmentBarsWidget) #include "tst_segmentbarswidget.moc"