dimanche 10 juin 2018

How to grab pointer to popup dialog in Qt which blocks the UI thread in QTest

I'm writing an integration test using QTest in which as soon as I click into the viewport of the widget, a multiline popup QInputDialog appears which blocks the further execution of code and requires manually canceling the dialog. Here is the code:

void PartTest::testTypewriterAnnotTool()
{
    Okular::Part part(nullptr, nullptr, QVariantList()); // It is the main widget of PDF reader comprising of viewport where PDF page is shown

    part.openUrl(QUrl::fromLocalFile(QStringLiteral(KDESRCDIR "data/file1.pdf"))); // open file1.pdf

    part.widget()->show();
    QVERIFY(QTest::qWaitForWindowExposed(part.widget()));

    // Width and height of pageView widget, the child of Part widget which shows the PDF page

    const int width = part.m_pageView->horizontalScrollBar()->maximum() +
                      part.m_pageView->viewport()->width();
    const int height = part.m_pageView->verticalScrollBar()->maximum() +
                       part.m_pageView->viewport()->height();

    part.m_document->setViewportPage(0); // sets viewport page 0, i.e. page number 1

    QMetaObject::invokeMethod(part.m_pageView, "slotToggleAnnotator", Q_ARG( bool, true )); // toggles and shows the annotation toolbar with all tools avaialable

    QList<QToolButton *> toolbuttonList = part.m_pageView->findChildren<QToolButton *>(); // find a list of annotation toolbutton
    // Check if the tooltip of 10th button is "Typewriter"
    QToolButton* typewriterButton = toolbuttonList.at(9);
    QCOMPARE( typewriterButton->toolTip(), QStringLiteral("Typewriter") );

    typewriterButton->click(); // clicking and selecting typewriter annotation tool

    QTest::mouseMove(part.m_pageView->viewport(), QPoint(width * 0.5, height * 0.2)); // leading mouse pointer to a specific point in the viewport

    QTest::mouseClick(part.m_pageView->viewport(), Qt::LeftButton, Qt::NoModifier, QPoint(width * 0.5, height * 0.2)); // mouse left button click on the viewport and a popup dialog with 'add a new note' appears

}

I have to write a test case where I have to grab that popup dialog and programmatically close it. I'm trying to achieve so by using the QTimer where I will execute a function to grab the dialog after 1 second of clicking in the viewport and then trying to close it like this:

class PartTest
{
...
private:
    Okular::part *m_part;
}

void PartTest::testTypewriterAnnotTool()
{
...
    m_part = &part;
    QTimer* mTimer = new QTimer(this);
    mTimer->setSingleShot(true);
    connect(mTimer, SIGNAL(timeout()), SLOT(testDialogClosed()));
    mTimer->start(1000);
    QTest::mouseClick(part.m_pageView->viewport(), Qt::LeftButton, Qt::NoModifier, QPoint(width * 0.5, height * 0.2));
}

void PartTest::testDialogClosed()
{
    bool m_clicked = false;
    QDialog *dialog = m_part->widget()->findChild<QDialog*>();
    if (dialog)
    {
        QDialogButtonBox *buttonBox = dialog->findChild<QDialogButtonBox*>();
        buttonBox->button(QDialogButtonBox::Cancel)->click();
        m_clicked = true;
    }
    QVERIFY(m_clicked);
}

Here, QVERIFY(m_clicked) test case always "fail" that means the popup QInputDialog is not the child of Okular::Part and I'm not getting a way to grab the pointer to this dialog window and close it. Any help?

Aucun commentaire:

Enregistrer un commentaire