#include #include #include #include "../src/touchvideowidget.h" class TouchVideoTest : public QObject { Q_OBJECT static void click(TouchVideoWidget &widget, const QPointF &position) { QMouseEvent event(QEvent::MouseButtonPress, position, Qt::LeftButton, Qt::LeftButton, Qt::NoModifier); QApplication::sendEvent(&widget, &event); } static VideoFrame frame(int width, int height) { VideoFrame result; result.width=width; result.height=height; result.format=VideoPixelFormat::RGBA; result.stride0=width*4; result.plane0=QByteArray(width*height*4, char(0)); return result; } private slots: void frozenImageShowsSelectionAbovePhoto() { TouchVideoWidget widget; widget.resize(400,300); QImage image(400,200,QImage::Format_RGB32); image.fill(Qt::white); QVERIFY(widget.lockImage(image)); widget.show(); QVERIFY(QTest::qWaitForWindowExposed(&widget)); QTest::qWait(80); QVERIFY2(widget.isValid(),"A working OpenGL context is required for this visual regression"); QTest::mouseClick(&widget,Qt::LeftButton,Qt::NoModifier,QPoint(100,100)); QCOMPARE(widget.selectedPoints().size(),1); QTest::qWait(30); // QWidget::grab does not reliably composite QOpenGLWidget on Qt 5.12. // Check both the actual framebuffer and any child obscuring it. QVERIFY2(!widget.childAt(QPoint(100,100)),"A child widget covers the target selection layer"); const QImage rendered=widget.grabFramebuffer(); const qreal ratio=widget.devicePixelRatioF(); bool markerVisible=false; for(int y=90;y<=110;++y) for(int x=90;x<=110;++x) { const QColor color=rendered.pixelColor(qRound(x*ratio),qRound(y*ratio)); if(color.green()>150 && color.red()<130 && color.blue()<190) markerVisible=true; } QVERIFY2(markerVisible,"The frozen photo obscures the selected target marker"); QVERIFY(rendered.pixelColor(qRound(300*ratio),qRound(150*ratio)).red()>240); } void layoutTracksActualFrameRatioAndFrozenFrame() { TouchVideoWidget widget; widget.setFrameAspectLayoutEnabled(true); QCOMPARE(widget.heightForWidth(800),600); widget.present(frame(1280,720)); QCOMPARE(widget.heightForWidth(800),450); QCOMPARE(widget.minimumHeight(),widget.heightForWidth(widget.width())); QImage native(640,480,QImage::Format_RGB888); native.fill(Qt::black); QVERIFY(widget.lockImage(native)); QCOMPARE(widget.heightForWidth(800),600); widget.present(frame(1280,720)); QCOMPARE(widget.heightForWidth(800),600); // Live frames cannot resize the locked selection. widget.unlock(); QCOMPARE(widget.heightForWidth(800),450); widget.clearFrame(); QCOMPARE(widget.heightForWidth(800),450); // No layout jump on stop. widget.setFrameAspectLayoutEnabled(false); QCOMPARE(widget.heightForWidth(800),-1); } void mapsPixelCentersAndRejectsLetterbox() { QPointF pixel; // A 400 x 200 image is drawn at (0, 100), size 800 x 400. QVERIFY(TouchVideoWidget::mapToImage({1,101}, {800,600}, {400,200}, &pixel)); QCOMPARE(pixel, QPointF(0,0)); QVERIFY(TouchVideoWidget::mapToImage({201,201}, {800,600}, {400,200}, &pixel)); QCOMPARE(pixel, QPointF(100,50)); QVERIFY(!TouchVideoWidget::mapToImage({100,99}, {800,600}, {400,200}, &pixel)); QVERIFY(!TouchVideoWidget::mapToImage({100,500}, {800,600}, {400,200}, &pixel)); QVERIFY(!TouchVideoWidget::mapToImage({800,200}, {800,600}, {400,200}, &pixel)); QVERIFY(TouchVideoWidget::mapToImage({799.9,499.9}, {800,600}, {400,200}, &pixel)); QCOMPARE(pixel, QPointF(399,199)); // Portrait image has bars at the left and right. QVERIFY(!TouchVideoWidget::mapToImage({249,200}, {800,600}, {200,400}, &pixel)); QVERIFY(TouchVideoWidget::mapToImage({250.75,0.75}, {800,600}, {200,400}, &pixel)); QCOMPARE(pixel, QPointF(0,0)); } void rejectsInvalidGeometry() { QPointF pixel; QVERIFY(!TouchVideoWidget::mapToImage({0,0}, {}, {400,200}, &pixel)); QVERIFY(!TouchVideoWidget::mapToImage({0,0}, {800,600}, {}, &pixel)); QVERIFY(!TouchVideoWidget::mapToImage({0,0}, {800,600}, {400,200}, nullptr)); QVERIFY(!TouchVideoWidget::mapToImage({std::numeric_limits::quiet_NaN(),0}, {800,600}, {400,200}, &pixel)); } void selectsOnlyFrozenImageAndRestartsAfterTwoPoints() { TouchVideoWidget widget; widget.resize(800,600); widget.present(frame(400,200)); QSignalSpy changed(&widget, &TouchVideoWidget::pointsChanged); click(widget,{201,201}); QVERIFY(widget.selectedPoints().isEmpty()); QImage image(400,200,QImage::Format_RGB32); image.fill(Qt::white); QVERIFY(widget.lockImage(image)); QVERIFY(widget.locked()); widget.setMaxPoints(2); click(widget,{201,201}); click(widget,{401,301}); QCOMPARE(widget.selectedPoints(), (QVector{{100,50},{200,100}})); QCOMPARE(changed.count(),2); click(widget,{100,99}); QCOMPARE(changed.count(),2); click(widget,{1,101}); QCOMPARE(widget.selectedPoints(), (QVector{{0,0}})); QCOMPARE(changed.count(),3); widget.setMaxPoints(1); QVERIFY(widget.selectedPoints().isEmpty()); click(widget,{201,201}); click(widget,{401,301}); QCOMPARE(widget.selectedPoints(), (QVector{{200,100}})); } void preservesFrozenGeometryAndRestoresLatestLiveFrame() { TouchVideoWidget widget; widget.resize(800,600); widget.present(frame(640,480)); QImage image(400,200,QImage::Format_RGB32); image.fill(Qt::white); QVERIFY(widget.lockImage(image)); click(widget,{201,201}); widget.present(frame(1280,720)); QCOMPARE(widget.imageSize(),QSize(400,200)); QVERIFY(!widget.lockImage(QImage())); QVERIFY(widget.locked()); QCOMPARE(widget.selectedPoints().size(),1); widget.unlock(); QVERIFY(!widget.locked()); QVERIFY(widget.selectedPoints().isEmpty()); QCOMPARE(widget.imageSize(),QSize(1280,720)); widget.clearFrame(); QVERIFY(!widget.hasFrame()); QVERIFY(widget.imageSize().isEmpty()); } void locksExactVideoFrameAndRetainsItsOwner() { TouchVideoWidget widget; widget.resize(801,601); widget.present(frame(640,480)); for (VideoPixelFormat format : {VideoPixelFormat::RGBA,VideoPixelFormat::I420,VideoPixelFormat::NV12}) { VideoFrame frozen=frame(400,200); frozen.format=format; if (format != VideoPixelFormat::RGBA) { frozen.stride0=400; frozen.plane0=QByteArray(400*200,char(128)); frozen.stride1=format==VideoPixelFormat::I420 ? 200 : 400; frozen.plane1=QByteArray(frozen.stride1*100,char(128)); if (format==VideoPixelFormat::I420) { frozen.stride2=200; frozen.plane2=QByteArray(200*100,char(128)); } } auto owner=std::make_shared(7); std::weak_ptr alive=owner; frozen.owner=owner; frozen.metadata={{"sourceSequence",qulonglong(123)},{"ptsMatched",true}}; frozen.presentationTimestamp=123; QVERIFY(widget.lockFrame(frozen)); owner.reset(); frozen={}; QVERIFY(!alive.expired()); QVERIFY(widget.locked()); QCOMPARE(widget.imageSize(),QSize(400,200)); // Match the original renderer's fractional GL letterbox for YUV. const double sx=801.0/400.0; const double sy=format==VideoPixelFormat::RGBA ? 400.0/200.0 : sx; const double top=format==VideoPixelFormat::RGBA ? 100.0 : (601.0-200.0*sy)/2; click(widget,{100.5*sx,top+50.5*sy}); QCOMPARE(widget.selectedPoints().size(),1); QVERIFY(std::abs(widget.selectedPoints()[0].x()-100)<1e-9); QVERIFY(std::abs(widget.selectedPoints()[0].y()-50)<1e-9); QVERIFY(!widget.lockFrame(VideoFrame())); QCOMPARE(widget.selectedPoints().size(),1); widget.present(frame(800,600)); QVERIFY(!alive.expired()); widget.unlock(); QVERIFY(alive.expired()); QCOMPARE(widget.imageSize(),QSize(800,600)); QVERIFY(widget.selectedPoints().isEmpty()); } } }; QTEST_MAIN(TouchVideoTest) #include "touchvideo_test.moc"