Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-129794

QTest::mouseClick() and mouseDClick() don't actually apply the delay to each event in the sequence

    XMLWordPrintable

Details

    • Bug
    • Resolution: Fixed
    • P3: Somewhat important
    • 6.9.0 FF
    • 6.8.0
    • Testing: qtestlib
    • None
    • 069858008 (dev)

    Description

      https://doc.qt.io/qt-6/qtest.html#mouseDClick says

      If delay is specified, the test will wait for the specified amount of milliseconds before each press and release.

      And the docs for mouseClick() say

      If delay is specified, the test will wait for the specified amount of milliseconds before pressing and before releasing the button.

      Now look at the code:

          static void mouseEvent(MouseAction action, QWindow *window, Qt::MouseButton button,
                                 Qt::KeyboardModifiers stateKey, QPoint pos, int delay=-1)
          {
              QTEST_ASSERT(window);
              extern int Q_TESTLIB_EXPORT defaultMouseDelay();
      
              // pos is in window local coordinates
              const QSize windowSize = window->geometry().size();
              if (windowSize.width() <= pos.x() || windowSize.height() <= pos.y()) {
                  qWarning("Mouse event at %d, %d occurs outside target window (%dx%d).",
                           pos.x(), pos.y(), windowSize.width(), windowSize.height());
              }
      
              int actualDelay = (delay == -1 || delay < defaultMouseDelay()) ? defaultMouseDelay() : delay;
              lastMouseTimestamp += qMax(1, actualDelay);
      
              if (pos.isNull())
                  pos = QPoint(window->width() / 2, window->height() / 2);
      
              QTEST_ASSERT(!stateKey || stateKey & Qt::KeyboardModifierMask);
      
              stateKey &= Qt::KeyboardModifierMask;
      
              QPointF global = window->mapToGlobal(pos);
              QPointer<QWindow> w(window);
      
              using namespace QTestPrivate;
              switch (action)
              {
              case MouseDClick:
                  qtestMouseButtons.setFlag(button, true);
                  qt_handleMouseEvent(w, pos, global, qtestMouseButtons, button, QEvent::MouseButtonPress,
                                      stateKey, lastMouseTimestamp);
                  qtestMouseButtons.setFlag(button, false);
                  qt_handleMouseEvent(w, pos, global, qtestMouseButtons, button, QEvent::MouseButtonRelease,
                                      stateKey, lastMouseTimestamp);
                  Q_FALLTHROUGH();
              case MousePress:
              case MouseClick:
                  qtestMouseButtons.setFlag(button, true);
                  qt_handleMouseEvent(w, pos, global, qtestMouseButtons, button, QEvent::MouseButtonPress,
                                      stateKey, lastMouseTimestamp);
                  if (action == MousePress)
                      break;
                  Q_FALLTHROUGH();
              case MouseRelease:
                  qtestMouseButtons.setFlag(button, false);
                  qt_handleMouseEvent(w, pos, global, qtestMouseButtons, button, QEvent::MouseButtonRelease,
                                      stateKey, lastMouseTimestamp);
                  if (delay == -1)
                      lastMouseTimestamp += mouseDoubleClickInterval; // avoid double clicks being generated
                  break;
      

      The delay argument is only added once, at the beginning. It does not get added during each call to qt_handleMouseEvent(). We just add it once and then keep using lastMouseTimestamp for each event in the sequence. This does not realistically simulate how users interact with the mouse.

      The docs are very old. So it's always been this way in Qt 5, and probably long before.

      When we change it, we might expect some timestamp-sensitive tests to fail. OTOH it has sometimes been hard to write realistic tests because of this.

      Attachments

        Issue Links

          For Gerrit Dashboard: QTBUG-129794
          # Subject Branch Project Status CR V

          Activity

            People

              srutledg Shawn Rutledge
              srutledg Shawn Rutledge
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:

                Gerrit Reviews

                  There are no open Gerrit changes