2

I'm writing some auto-test code using qtestlib. In the example code below:

#include <QtTest/QtTest>

QWidget *win = new QWidget;
QLabel *label = new QLabel("&what");
QLineEdit *le = new QLineEdit;
label->setBuddy(le);

QHBoxLayout *layout = new QHBoxLayout;
layout->addWidget(label);
layout->addWidget(le);
win->setLayout(layout);
win->show();

QTestEventList events1, events2;
events1.addKeyClick(Qt::Key_W, Qt::AltModifier);
events1.addKeyClicks("hello");
events1.addDelay(1000);
events1.simulate(win); // didn't type 'hello'

events2.addKeyClicks("world");
events2.addDelay(1000);
events2.simulate(le); // did type 'world'

I can't let the le get focus and type 'hello' by sending events to its parent widget win. But I can do that by sending events to le directly.

Things is that normally there are many private widget members in a custom QWidget class. And I can't simulate key/mouse events by sending the events to the instance. And of cause I can't send events to its private widgets. So what can I do to perform a auto-test to such class?

environment: Gentoo Linux KDE Qt-4.8

Yang Liu
  • 173
  • 2
  • 13

2 Answers2

1

Found solution now for sure:

  QTest::keyClick(win, Qt::Key_F, Qt::AltModifier,500);
  QTest::keyClicks(win->focusWidget(),"blah");
  QTest::keyClick(win, Qt::Key_W, Qt::AltModifier,500);
  QTest::keyClicks(win->focusWidget(),"blah2");

enter image description here

Last parameter is delay and It is necessary (You should test it).

kajojeq
  • 886
  • 9
  • 27
  • In the real code I use `QTestEventList` to simulate. And I just add few `addDelay()` to check the result. – Yang Liu Nov 14 '13 at 12:53
  • That's it! And `dalay=1` seems also worked. But I'm afraid it's machine/platform-specified. So still wondering a *universal* practice for testing a widget like that. – Yang Liu Nov 14 '13 at 14:41
1

The best way is to set name for subobjects of tested widget and then fetch them by this name.

You should also try to give focus to this editor. There are many ways to do that. For example by calling nextInFocusChain until you reach desired editor and the calling setFocus(Qt::MouseFocusReason);

Other way to get private parts is sub classing and/or forked build for testing where you can freely access private parts (it is possible to prepare some macros for that).

Marek R
  • 32,568
  • 6
  • 55
  • 140
  • I think the "set name" method is great. But to fork test sub objects is what I wanna escape from. Because by doing so it's hard to sync between test code and production code. – Yang Liu Nov 15 '13 at 03:18
  • `setObjectName` is great since there are tools (I don't remember name) where you can write Ruby scripts to perform functional test on the whole application. You can also add friendship to forward declared test class to be able access private parts in test. – Marek R Nov 15 '13 at 09:05