gui-unit-testing-with-qt-test-advanced/GuiUnitTestingAdv/TesterPanelConcat/TestPanelConcat.cpp
void TestPanelConcat::TestFocusUsage()
{
	// enables focus and widget events
	QApplication::setActiveWindow(&mPanel);

	// set initial focus
	mPanel.mInputA->setFocus();
	QVERIFY2(mPanel.mInputA->hasFocus(), "Input A doesn't have focus");

	// write STR1
	QTest::keyClicks(QApplication::focusWidget(), STR1);

	// move focus to next widget
	QTest::keyClick(&mPanel, Qt::Key_Tab);
	QVERIFY2(mPanel.mInputB->hasFocus(), "Input B doesn't have focus");

	// write STR2
	QTest::keyClicks(QApplication::focusWidget(), STR2);

	// move focus to next widget
	QTest::keyClick(&mPanel, Qt::Key_Tab);
	QVERIFY2(mPanel.mButtonConcat->hasFocus(), "Button CONCAT doesn't have focus");

	// press button CONCAT using space
	QTest::keyClick(QApplication::focusWidget(), Qt::Key_Space);
	QCOMPARE(mPanel.mLabelRes->text(), STR_RES);

	// move focus to next widget
	QTest::keyClick(&mPanel, Qt::Key_Tab);
	QVERIFY2(mPanel.mButtonCancel->hasFocus(), "Button CANCEL doesn't have focus");

	// press button CANCEL using space
	QTest::keyClick(QApplication::focusWidget(), Qt::Key_Space);
	QVERIFY2(mPanel.mInputA->text().isEmpty(), "Cancel didn't work on input A");
	QVERIFY2(mPanel.mInputB->text().isEmpty(), "Cancel didn't work on input B");
	QVERIFY2(mPanel.mLabelRes->text().isEmpty(), "Cancel didn't work on res label");
}
//In the real world, it would be better to split this test in two functions.
In the real world, it would be better to split this test in two functions.
In the real world it would be better to split this test in two functions .
void TestPanelConcat::TestFocusUsage()
{
	QApplication::setActiveWindow(&mPanel);
	mPanel.mInputA->setFocus();
	QVERIFY2(mPanel.mInputA->hasFocus(), "Input A doesn't have focus");
	QTest::keyClicks(QApplication::focusWidget(), STR1);
	QTest::keyClick(&mPanel, Qt::Key_Tab);
	QVERIFY2(mPanel.mInputB->hasFocus(), "Input B doesn't have focus");
	QTest::keyClicks(QApplication::focusWidget(), STR2);
	QTest::keyClick(&mPanel, Qt::Key_Tab);
	QVERIFY2(mPanel.mButtonConcat->hasFocus(), "Button CONCAT doesn't have focus");
	QTest::keyClick(QApplication::focusWidget(), Qt::Key_Space);
	QCOMPARE(mPanel.mLabelRes->text(), STR_RES);
	QTest::keyClick(&mPanel, Qt::Key_Tab);
	QVERIFY2(mPanel.mButtonCancel->hasFocus(), "Button CANCEL doesn't have focus");
	QTest::keyClick(QApplication::focusWidget(), Qt::Key_Space);
	QVERIFY2(mPanel.mInputA->text().isEmpty(), "Cancel didn't work on input A");
	QVERIFY2(mPanel.mInputB->text().isEmpty(), "Cancel didn't work on input B");
	QVERIFY2(mPanel.mLabelRes->text().isEmpty(), "Cancel didn't work on res label");
}
void TestPanelConcat :: TestFocusUsage ( ) { QApplication :: setActiveWindow ( & mPanel ) ; mPanel . mInputA -> setFocus ( ) ; QVERIFY2 ( mPanel . mInputA -> hasFocus ( ) , " " ) ; QTest :: keyClicks ( QApplication :: focusWidget ( ) , STR1 ) ; QTest :: keyClick ( & mPanel , Qt :: Key_Tab ) ; QVERIFY2 ( mPanel . mInputB -> hasFocus ( ) , " " ) ; QTest :: keyClicks ( QApplication :: focusWidget ( ) , STR2 ) ; QTest :: keyClick ( & mPanel , Qt :: Key_Tab ) ; QVERIFY2 ( mPanel . mButtonConcat -> hasFocus ( ) , " " ) ; QTest :: keyClick ( QApplication :: focusWidget ( ) , Qt :: Key_Space ) ; QCOMPARE ( mPanel . mLabelRes -> text ( ) , STR_RES ) ; QTest :: keyClick ( & mPanel , Qt :: Key_Tab ) ; QVERIFY2 ( mPanel . mButtonCancel -> hasFocus ( ) , " " ) ; QTest :: keyClick ( QApplication :: focusWidget ( ) , Qt :: Key_Space ) ; QVERIFY2 ( mPanel . mInputA -> text ( ) . isEmpty ( ) , " " ) ; QVERIFY2 ( mPanel . mInputB -> text ( ) . isEmpty ( ) , " " ) ; QVERIFY2 ( mPanel . mLabelRes -> text ( ) . isEmpty ( ) , " " ) ; }
In the real world, it would be better to split this test in two functions.
In the real world it would be better to split this test in two functions .
// enables focus and widget events // set initial focus // write STR1 // move focus to next widget // write STR2 // move focus to next widget // press button CONCAT using space // move focus to next widget // press button CANCEL using space