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

QTest::mouseClick does not trigger context menu

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Reported
    • Priority: P3: Somewhat important
    • Resolution: Unresolved
    • Affects Version/s: 5.5.1
    • Fix Version/s: None
    • Component/s: Testing: qtestlib
    • Labels:
      None
    • Environment:
      Ubuntu 14.04 LTS

      Description

      I cannot use QTest::mouseClick to trigger a context menu.

      This code:

      #include <QString>
      #include <QTest>
      #include <QCoreApplication>
      #include <QMainWindow>
      #include <QStandardItemModel>
      #include <QTreeView>
      #include <QSignalSpy>
      
      class Test : public QObject
      {
          Q_OBJECT
      
      private Q_SLOTS:
          void testMouseClickContextMenu();
      };
      
      void Test::testMouseClickContextMenu()
      {
          QMainWindow w;
          QStandardItemModel *model = new QStandardItemModel();
          QStandardItem *item = new QStandardItem("Test");
          model->appendRow({item});
          QTreeView *tree = new QTreeView();
          tree->setModel(model);
          w.setCentralWidget(tree);
      
          tree->setContextMenuPolicy(Qt::CustomContextMenu);
      
          QSignalSpy spy(tree, SIGNAL(customContextMenuRequested(QPoint)));
          QTest::mouseClick(tree, Qt::RightButton, 0,
                            tree->visualRect(item->index()).center());
          QCOMPARE(spy.size(), 1); //Actual is 0
      }
      
      QTEST_MAIN(Test)
      
      #include "tst_test.moc"
      

      produces this result:

      Config: Using QtTest library 5.5.1, Qt 5.5.1 (x86_64-little_endian-lp64 shared (dynamic) release build; by GCC 4.9.1 20140922 (Red Hat 4.9.1-10))
      PASS   : Test::initTestCase()
      FAIL!  : Test::testMouseClickContextMenu() Compared values are not the same
         Actual   (spy.size()): 0
         Expected (1)         : 1
         Loc: [../qt-sandbox/tst_test.cpp(32)]
      PASS   : Test::cleanupTestCase()
      Totals: 2 passed, 1 failed, 0 skipped, 0 blacklisted
      

        Attachments

          Issue Links

          No reviews matched the request. Check your Options in the drop-down menu of this sections header.

            Activity

              People

              Assignee:
              paeglis Gatis Paeglis
              Reporter:
              milliehandshrimp Emily Zora
              Votes:
              1 Vote for this issue
              Watchers:
              5 Start watching this issue

                Dates

                Created:
                Updated:

                  Gerrit Reviews

                  There are no open Gerrit changes