@@ -0,0 +1,171 @@
2+ * Copyright (C) 2015 Canonical, Ltd.
4+ * This program is free software; you can redistribute it and/or modify
5+ * it under the terms of the GNU General Public License as published by
6+ * the Free Software Foundation; version 3.
8+ * This program is distributed in the hope that it will be useful,
9+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
10+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+ * GNU General Public License for more details.
13+ * You should have received a copy of the GNU General Public License
14+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
17+#include "homekeywatcher.h"
22+using namespace UnityUtil;
24+class HomeKeyWatcherTest : public QObject {
27+ void init(); // called right before each and every test function is executed
28+ void cleanup(); // called right after each and every test function is executed
30+ void touchTapTouch_data();
31+ void touchTapTouch();
33+ void tapWhileTouching();
36+ void passTime(qint64 timeSpanMs);
38+ FakeTimerFactory *m_fakeTimerFactory;
41+void HomeKeyWatcherTest::init()
43+ m_fakeTimerFactory = new FakeTimerFactory;
46+void HomeKeyWatcherTest::cleanup()
48+ delete m_fakeTimerFactory;
49+ m_fakeTimerFactory = nullptr;
53+void HomeKeyWatcherTest::passTime(qint64 timeSpanMs)
55+ qint64 finalTime = FakeElapsedTimer::msecsSinceEpoch + timeSpanMs;
56+ m_fakeTimerFactory->updateTime(finalTime);
59+void HomeKeyWatcherTest::touchTapTouch_data()
61+ QTest::addColumn<int>("silenceBeforeTap");
62+ QTest::addColumn<int>("tapDuration");
63+ QTest::addColumn<int>("silenceAfterTap");
64+ QTest::addColumn<int>("expectedActivatedCount");
66+ QTest::newRow("tap followed by touch") << 2000 << 50 << 10 << 0;
67+ QTest::newRow("touch, tap and touch") << 10 << 50 << 10 << 0;
68+ QTest::newRow("touch followed by tap") << 10 << 50 << 2000 << 0;
69+ QTest::newRow("touch followed by long tap") << 10 << 500 << 2000 << 0;
70+ QTest::newRow("isolated tap") << 1000 << 50 << 1000 << 1;
73+void HomeKeyWatcherTest::touchTapTouch()
75+ QFETCH(int, silenceBeforeTap);
76+ QFETCH(int, tapDuration);
77+ QFETCH(int, silenceAfterTap);
78+ QFETCH(int, expectedActivatedCount);
79+ HomeKeyWatcher homeKeyWatcher(m_fakeTimerFactory->create(), new FakeElapsedTimer);
80+ QSignalSpy activatedSpy(&homeKeyWatcher, SIGNAL(activated()));
81+ QVERIFY(activatedSpy.isValid());
84+ QTouchEvent touchEvent(QEvent::TouchBegin);
85+ homeKeyWatcher.update(&touchEvent);
89+ QTouchEvent touchEvent(QEvent::TouchUpdate);
90+ homeKeyWatcher.update(&touchEvent);
94+ QTouchEvent touchEvent(QEvent::TouchEnd);
95+ homeKeyWatcher.update(&touchEvent);
98+ passTime(silenceBeforeTap);
101+ QKeyEvent keyEvent(QEvent::KeyPress, Qt::Key_Home, Qt::NoModifier);
102+ homeKeyWatcher.update(&keyEvent);
105+ while (tapTime < tapDuration) {
108+ if (tapTime + 10 >= tapDuration) {
109+ QKeyEvent keyEvent(QEvent::KeyRelease, Qt::Key_Home, Qt::NoModifier);
110+ homeKeyWatcher.update(&keyEvent);
112+ QKeyEvent keyEvent(QEvent::KeyPress, Qt::Key_Home, Qt::NoModifier, QString(), true /*autorepeat*/);
113+ homeKeyWatcher.update(&keyEvent);
117+ passTime(silenceAfterTap);
120+ QTouchEvent touchEvent(QEvent::TouchBegin);
121+ homeKeyWatcher.update(&touchEvent);
125+ QTouchEvent touchEvent(QEvent::TouchUpdate);
126+ homeKeyWatcher.update(&touchEvent);
130+ QTouchEvent touchEvent(QEvent::TouchEnd);
131+ homeKeyWatcher.update(&touchEvent);
135+ QCOMPARE(activatedSpy.count(), expectedActivatedCount);
138+void HomeKeyWatcherTest::tapWhileTouching()
140+ HomeKeyWatcher homeKeyWatcher(m_fakeTimerFactory->create(), new FakeElapsedTimer);
141+ QSignalSpy activatedSpy(&homeKeyWatcher, SIGNAL(activated()));
142+ QVERIFY(activatedSpy.isValid());
145+ QTouchEvent touchEvent(QEvent::TouchBegin);
146+ homeKeyWatcher.update(&touchEvent);
150+ QKeyEvent keyEvent(QEvent::KeyPress, Qt::Key_Home, Qt::NoModifier);
151+ homeKeyWatcher.update(&keyEvent);
155+ QKeyEvent keyEvent(QEvent::KeyRelease, Qt::Key_Home, Qt::NoModifier);
156+ homeKeyWatcher.update(&keyEvent);
160+ QTouchEvent touchEvent(QEvent::TouchEnd);
161+ homeKeyWatcher.update(&touchEvent);
166+ QCOMPARE(activatedSpy.count(), 0);
169+QTEST_GUILESS_MAIN(HomeKeyWatcherTest)
171+#include "homekeywatchertest.moc"