|
| 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 <QtTest/QtTest> |
| 18 | + |
| 19 | +#include "qgsfocuswatcher.h" |
| 20 | +#include <QApplication> |
| 21 | +#include <QLineEdit> |
| 22 | + |
| 23 | +class TestQgsFocusWatcher: public QObject |
| 24 | +{ |
| 25 | + Q_OBJECT |
| 26 | + private slots: |
| 27 | + void initTestCase(); // will be called before the first testfunction is executed. |
| 28 | + void cleanupTestCase(); // will be called after the last testfunction was executed. |
| 29 | + void init(); // will be called before each testfunction is executed. |
| 30 | + void cleanup(); // will be called after every testfunction. |
| 31 | + |
| 32 | + void testSignals(); |
| 33 | + |
| 34 | + private: |
| 35 | + |
| 36 | +}; |
| 37 | + |
| 38 | +void TestQgsFocusWatcher::initTestCase() |
| 39 | +{ |
| 40 | +} |
| 41 | + |
| 42 | +void TestQgsFocusWatcher::cleanupTestCase() |
| 43 | +{ |
| 44 | +} |
| 45 | + |
| 46 | +void TestQgsFocusWatcher::init() |
| 47 | +{ |
| 48 | +} |
| 49 | + |
| 50 | +void TestQgsFocusWatcher::cleanup() |
| 51 | +{ |
| 52 | +} |
| 53 | + |
| 54 | +void TestQgsFocusWatcher::testSignals() |
| 55 | +{ |
| 56 | + QWidget* w = new QWidget(); |
| 57 | + QLineEdit* e1 = new QLineEdit( w ); |
| 58 | + QLineEdit* e2 = new QLineEdit( w ); |
| 59 | + QApplication::setActiveWindow( w ); //required for focus events |
| 60 | + e1->setFocus(); |
| 61 | + |
| 62 | + QgsFocusWatcher* watcher1 = new QgsFocusWatcher( e1 ); |
| 63 | + QgsFocusWatcher* watcher2 = new QgsFocusWatcher( e2 ); |
| 64 | + |
| 65 | + QSignalSpy spyFocusIn1( watcher1, SIGNAL( focusIn() ) ); |
| 66 | + QSignalSpy spyFocusOut1( watcher1, SIGNAL( focusOut() ) ); |
| 67 | + QSignalSpy spyFocusChanged1( watcher1, SIGNAL( focusChanged( bool ) ) ); |
| 68 | + QSignalSpy spyFocusIn2( watcher2, SIGNAL( focusIn() ) ); |
| 69 | + QSignalSpy spyFocusOut2( watcher2, SIGNAL( focusOut() ) ); |
| 70 | + QSignalSpy spyFocusChanged2( watcher2, SIGNAL( focusChanged( bool ) ) ); |
| 71 | + |
| 72 | + e2->setFocus(); |
| 73 | + QCOMPARE( spyFocusIn1.count(), 0 ); |
| 74 | + QCOMPARE( spyFocusOut1.count(), 1 ); |
| 75 | + QCOMPARE( spyFocusChanged1.count(), 1 ); |
| 76 | + QCOMPARE( spyFocusChanged1.last().at( 0 ).toBool(), false ); |
| 77 | + QCOMPARE( spyFocusIn2.count(), 1 ); |
| 78 | + QCOMPARE( spyFocusOut2.count(), 0 ); |
| 79 | + QCOMPARE( spyFocusChanged2.count(), 1 ); |
| 80 | + QCOMPARE( spyFocusChanged2.last().at( 0 ).toBool(), true ); |
| 81 | + |
| 82 | + e1->setFocus(); |
| 83 | + QCOMPARE( spyFocusIn1.count(), 1 ); |
| 84 | + QCOMPARE( spyFocusOut1.count(), 1 ); |
| 85 | + QCOMPARE( spyFocusChanged1.count(), 2 ); |
| 86 | + QCOMPARE( spyFocusChanged1.last().at( 0 ).toBool(), true ); |
| 87 | + QCOMPARE( spyFocusIn2.count(), 1 ); |
| 88 | + QCOMPARE( spyFocusOut2.count(), 1 ); |
| 89 | + QCOMPARE( spyFocusChanged2.count(), 2 ); |
| 90 | + QCOMPARE( spyFocusChanged2.last().at( 0 ).toBool(), false ); |
| 91 | + |
| 92 | + delete w; |
| 93 | +} |
| 94 | + |
| 95 | + |
| 96 | +QTEST_MAIN( TestQgsFocusWatcher ) |
| 97 | +#include "testqgsfocuswatcher.moc" |
0 commit comments