Results 1 to 8 of 8

Thread: unit test focus behavior with QTest

  1. #1
    Join Date
    Nov 2010
    Posts
    9
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default unit test focus behavior with QTest

    Hi

    I’ve just switched to Qt and have created a new widget derived from QLineEdit that I am trying to unit test. So far I’ve been able to test everything except some custom behavior on gain/lose focus. Basically the text of the control (potentially) changes when it gains/loses focus and I would like to test this behavior. I can’t find anything in QTest, the qtestlib tutorial, or searching this forum (I accept that my searching ability may be lacking but I tried most combinations of qtest, unit test, qtestlib, simulate, and focus) and am hoping someone can at least point me in the right direction or let me know if it isn't possible.

    In short, I want a test that does the following:

    Qt Code:
    1. //inside a test
    2. my_ctrl ctrl;
    3.  
    4. QTest::keyClicks(&ctrl, "1.23456789");
    5.  
    6. //how do I cause ctrl to think it has lost focus here?
    7.  
    8. QString desired_result = //desired result, something other than “1.23456789”
    9.  
    10. QCOMPARE(ctrl.text(), desired_result);
    To copy to clipboard, switch view to plain text mode 

    Thank you for your help

  2. #2
    Join Date
    Nov 2010
    Posts
    315
    Thanked 53 Times in 51 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: unit test focus behavior with QTest

    Why you don't just call QWidget API: QWidget::setFocus and QWidget::clearFocus?

  3. #3
    Join Date
    Nov 2010
    Posts
    9
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: unit test focus behavior with QTest

    Tried clearFocus but that didn't work. According to the docs

    If the widget has active focus, a focus out event is sent to this widget to tell it that it is about to lose the focus.
    Maybe "about to lose focus" is different from actually losing focus? I don't see how I can check setFocus without having a way to remove focus

    For the record, I've tested my control in a test app to make sure it actually works, which it does. All I wish to do now is enshrine this behavior in a unit test.

    Here's a more complete yet still simple example of what I'm after:

    my_ctrl header
    Qt Code:
    1. class my_ctrl : public QLineEdit
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. my_ctrl(QWidget * parent = 0);
    7. ~my_ctrl();
    8.  
    9. protected:
    10. virtual void focusInEvent(QFocusEvent * e);
    11. virtual void focusOutEvent(QFocusEvent * e);
    12. };
    To copy to clipboard, switch view to plain text mode 

    my_ctrl implementation
    Qt Code:
    1. my_ctrl::my_ctrl(QWidget * parent)
    2. : QLineEdit(parent)
    3. {}
    4.  
    5. my_ctrl::~my_ctrl()
    6. {}
    7.  
    8.  
    9. void my_ctrl::focusInEvent(QFocusEvent * e)
    10. {
    11. setText("Focus In");
    12. QLineEdit::focusInEvent(e);
    13. }
    14.  
    15. void my_ctrl::focusOutEvent(QFocusEvent * e)
    16. {
    17. setText("Focus Out");
    18. QLineEdit::focusOutEvent(e);
    19. }
    To copy to clipboard, switch view to plain text mode 

    test code
    Qt Code:
    1. class test_my_ctrl : public QObject
    2. {
    3. Q_OBJECT
    4. private slots:
    5. void test_lose_focus();
    6. };
    7.  
    8.  
    9.  
    10. void test_my_ctrl::test_lose_focus()
    11. {
    12. my_ctrl ctrl;
    13.  
    14. QTest::keyClicks(&ctrl, "1.23456789");
    15.  
    16. QCOMPARE(ctrl.text(), QString("1.23456789"));
    17.  
    18. ctrl.clearFocus(); //doesn't work
    19.  
    20. QCOMPARE(ctrl.text(), QString("Focus Out")); //fails with Actual (ctrl.text()): 1.23456789
    21.  
    22. }
    23.  
    24.  
    25. QTEST_MAIN(test_my_ctrl)
    26. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Nov 2010
    Posts
    315
    Thanked 53 Times in 51 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: unit test focus behavior with QTest

    This should look like this (simple version):
    Qt Code:
    1. void test_my_ctrl::test_lose_focus()
    2. {
    3. my_ctrl ctrl;
    4. ctrl.setFocus(Qt::TabFocusReason); // first you make sure that focus is in correct place
    5. ctrl.selectAll(); // just in case if it is not selected, so old content will be replaced
    6. QTest::keyClicks(&ctrl, "1.23456789", Qt::NoModifier, 10); // then you type
    7.  
    8. QCOMPARE(ctrl.text(), QString("1.23456789"));
    9. ctrl.clearFocus();
    10. QTest::qWait(100); // this may be needed to process some of posted events - it may be not needed
    11. QCOMPARE(ctrl.text(), QString("Focus Out"));
    12. }
    To copy to clipboard, switch view to plain text mode 
    Focus in event should be done in similar manner.
    Advanced version should be data driven.

  5. #5
    Join Date
    Nov 2010
    Posts
    9
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: unit test focus behavior with QTest

    Nope, gave it up to a second (QTest::qWait(1000)) and still no good. Did you actually try your suggestion and, if so, using what platform and version? I ask because I went through a long conversation about a problem in wxWidgets (the framework that I'm moving from) that ended up being just a difference in event propagation behavior on different platforms.

  6. #6
    Join Date
    Nov 2010
    Posts
    315
    Thanked 53 Times in 51 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: unit test focus behavior with QTest

    We both forgot about little detail. To have focus widget HAS TO BE VISIBLE and I'm sure that this is the problem.
    So just add ctrl.show() on front (or give parent which is visible) .

  7. The following user says thank you to MarekR22 for this useful post:

    bob_smith (17th November 2010)

  8. #7
    Join Date
    Nov 2010
    Posts
    9
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: unit test focus behavior with QTest

    Sure enough that works. The only downside is that now the control is visible when the tests are run, of course. Originally I was hoping to have something that works in the same way as QTest::keyClicks in that the control is never actually visible to whoever is running the test when the keystrokes are entered. However now at least I have something that works, I’ll just have to see if it acceptable for use in our build process.

    Thank you for your help.

  9. #8
    Join Date
    Aug 2015
    Posts
    1
    Qt products
    Qt4

    Default Re: unit test focus behavior with QTest

    Quote Originally Posted by bob_smith View Post
    Sure enough that works. The only downside is that now the control is visible when the tests are run, of course. Originally I was hoping to have something that works in the same way as QTest::keyClicks in that the control is never actually visible to whoever is running the test when the keystrokes are entered. However now at least I have something that works, I’ll just have to see if it acceptable for use in our build process.

    Thank you for your help.
    I know this is an old thread but I am posting this in case anyone else comes across it. I was able to get the focus events working during tests without having to show the widget. I called "QApplication::setActiveWindow(patient_);", where patient is my main widget which holds other widgets in it like line edits etc. After this focus events propagated properply.

Similar Threads

  1. Unit test coverage
    By leoalvesmachado in forum Newbie
    Replies: 3
    Last Post: 16th April 2010, 13:48
  2. how to test slot using QTest?
    By hashb in forum Qt Programming
    Replies: 6
    Last Post: 14th December 2009, 04:22
  3. Unit Test Organisation, and having qmake what I want
    By jrharshath in forum Qt Programming
    Replies: 1
    Last Post: 15th September 2009, 18:59
  4. QTest Unit Testing
    By bothapn in forum Qt Programming
    Replies: 1
    Last Post: 16th July 2006, 22:11
  5. How to unit test a Qt Gui
    By mitskits in forum Qt Programming
    Replies: 1
    Last Post: 20th January 2006, 07:36

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.