1 /***************************************************************************
2 testqgsfocuswatcher.cpp
3 ----------------------
4 Date : April 2016
5 Copyright : (C) 2016 Nyall Dawson
6 Email : nyall dot dawson at gmail dot com
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15
16
17 #include "qgstest.h"
18
19 #include "qgsfocuswatcher.h"
20 #include <QApplication>
21 #include <QLineEdit>
22 #include <QSignalSpy>
23
24 class TestQgsFocusWatcher: public QObject
25 {
26 Q_OBJECT
27 private slots:
28 void initTestCase(); // will be called before the first testfunction is executed.
29 void cleanupTestCase(); // will be called after the last testfunction was executed.
30 void init(); // will be called before each testfunction is executed.
31 void cleanup(); // will be called after every testfunction.
32
33 void testSignals();
34
35 private:
36
37 };
38
initTestCase()39 void TestQgsFocusWatcher::initTestCase()
40 {
41 }
42
cleanupTestCase()43 void TestQgsFocusWatcher::cleanupTestCase()
44 {
45 }
46
init()47 void TestQgsFocusWatcher::init()
48 {
49 }
50
cleanup()51 void TestQgsFocusWatcher::cleanup()
52 {
53 }
54
testSignals()55 void TestQgsFocusWatcher::testSignals()
56 {
57 QWidget *w = new QWidget();
58 QLineEdit *e1 = new QLineEdit( w );
59 QLineEdit *e2 = new QLineEdit( w );
60 QApplication::setActiveWindow( w ); //required for focus events
61 e1->setFocus();
62
63 QgsFocusWatcher *watcher1 = new QgsFocusWatcher( e1 );
64 QgsFocusWatcher *watcher2 = new QgsFocusWatcher( e2 );
65
66 const QSignalSpy spyFocusIn1( watcher1, SIGNAL( focusIn() ) );
67 const QSignalSpy spyFocusOut1( watcher1, SIGNAL( focusOut() ) );
68 QSignalSpy spyFocusChanged1( watcher1, SIGNAL( focusChanged( bool ) ) );
69 const QSignalSpy spyFocusIn2( watcher2, SIGNAL( focusIn() ) );
70 const QSignalSpy spyFocusOut2( watcher2, SIGNAL( focusOut() ) );
71 QSignalSpy spyFocusChanged2( watcher2, SIGNAL( focusChanged( bool ) ) );
72
73 e2->setFocus();
74 QCOMPARE( spyFocusIn1.count(), 0 );
75 QCOMPARE( spyFocusOut1.count(), 1 );
76 QCOMPARE( spyFocusChanged1.count(), 1 );
77 QCOMPARE( spyFocusChanged1.last().at( 0 ).toBool(), false );
78 QCOMPARE( spyFocusIn2.count(), 1 );
79 QCOMPARE( spyFocusOut2.count(), 0 );
80 QCOMPARE( spyFocusChanged2.count(), 1 );
81 QCOMPARE( spyFocusChanged2.last().at( 0 ).toBool(), true );
82
83 e1->setFocus();
84 QCOMPARE( spyFocusIn1.count(), 1 );
85 QCOMPARE( spyFocusOut1.count(), 1 );
86 QCOMPARE( spyFocusChanged1.count(), 2 );
87 QCOMPARE( spyFocusChanged1.last().at( 0 ).toBool(), true );
88 QCOMPARE( spyFocusIn2.count(), 1 );
89 QCOMPARE( spyFocusOut2.count(), 1 );
90 QCOMPARE( spyFocusChanged2.count(), 2 );
91 QCOMPARE( spyFocusChanged2.last().at( 0 ).toBool(), false );
92
93 delete w;
94 }
95
96
97 QGSTEST_MAIN( TestQgsFocusWatcher )
98 #include "testqgsfocuswatcher.moc"
99