@@ -0,0 +1,171 @@
1+/*
2+ * Copyright (C) 2015 Canonical, Ltd.
3+ *
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.
7+ *
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.
12+ *
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/>.
15+ */
16+
17+#include "homekeywatcher.h"
18+
19+#include <QTest>
20+#include <QSignalSpy>
21+
22+using namespace UnityUtil;
23+
24+class HomeKeyWatcherTest : public QObject {
25+ Q_OBJECT
26+private Q_SLOTS:
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
29+
30+ void touchTapTouch_data();
31+ void touchTapTouch();
32+
33+ void tapWhileTouching();
34+
35+private:
36+ void passTime(qint64 timeSpanMs);
37+
38+ FakeTimerFactory *m_fakeTimerFactory;
39+};
40+
41+void HomeKeyWatcherTest::init()
42+{
43+ m_fakeTimerFactory = new FakeTimerFactory;
44+}
45+
46+void HomeKeyWatcherTest::cleanup()
47+{
48+ delete m_fakeTimerFactory;
49+ m_fakeTimerFactory = nullptr;
50+}
51+
52+
53+void HomeKeyWatcherTest::passTime(qint64 timeSpanMs)
54+{
55+ qint64 finalTime = FakeElapsedTimer::msecsSinceEpoch + timeSpanMs;
56+ m_fakeTimerFactory->updateTime(finalTime);
57+}
58+
59+void HomeKeyWatcherTest::touchTapTouch_data()
60+{
61+ QTest::addColumn<int>("silenceBeforeTap");
62+ QTest::addColumn<int>("tapDuration");
63+ QTest::addColumn<int>("silenceAfterTap");
64+ QTest::addColumn<int>("expectedActivatedCount");
65+
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;
71+}
72+
73+void HomeKeyWatcherTest::touchTapTouch()
74+{
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());
82+
83+ {
84+ QTouchEvent touchEvent(QEvent::TouchBegin);
85+ homeKeyWatcher.update(&touchEvent);
86+ }
87+ passTime(100);
88+ {
89+ QTouchEvent touchEvent(QEvent::TouchUpdate);
90+ homeKeyWatcher.update(&touchEvent);
91+ }
92+ passTime(100);
93+ {
94+ QTouchEvent touchEvent(QEvent::TouchEnd);
95+ homeKeyWatcher.update(&touchEvent);
96+ }
97+
98+ passTime(silenceBeforeTap);
99+
100+ {
101+ QKeyEvent keyEvent(QEvent::KeyPress, Qt::Key_Home, Qt::NoModifier);
102+ homeKeyWatcher.update(&keyEvent);
103+ }
104+ int tapTime = 0;
105+ while (tapTime < tapDuration) {
106+ passTime(10);
107+ tapTime += 10;
108+ if (tapTime + 10 >= tapDuration) {
109+ QKeyEvent keyEvent(QEvent::KeyRelease, Qt::Key_Home, Qt::NoModifier);
110+ homeKeyWatcher.update(&keyEvent);
111+ } else {
112+ QKeyEvent keyEvent(QEvent::KeyPress, Qt::Key_Home, Qt::NoModifier, QString(), true /*autorepeat*/);
113+ homeKeyWatcher.update(&keyEvent);
114+ }
115+ }
116+
117+ passTime(silenceAfterTap);
118+
119+ {
120+ QTouchEvent touchEvent(QEvent::TouchBegin);
121+ homeKeyWatcher.update(&touchEvent);
122+ }
123+ passTime(100);
124+ {
125+ QTouchEvent touchEvent(QEvent::TouchUpdate);
126+ homeKeyWatcher.update(&touchEvent);
127+ }
128+ passTime(100);
129+ {
130+ QTouchEvent touchEvent(QEvent::TouchEnd);
131+ homeKeyWatcher.update(&touchEvent);
132+ }
133+ passTime(1000);
134+
135+ QCOMPARE(activatedSpy.count(), expectedActivatedCount);
136+}
137+
138+void HomeKeyWatcherTest::tapWhileTouching()
139+{
140+ HomeKeyWatcher homeKeyWatcher(m_fakeTimerFactory->create(), new FakeElapsedTimer);
141+ QSignalSpy activatedSpy(&homeKeyWatcher, SIGNAL(activated()));
142+ QVERIFY(activatedSpy.isValid());
143+
144+ {
145+ QTouchEvent touchEvent(QEvent::TouchBegin);
146+ homeKeyWatcher.update(&touchEvent);
147+ }
148+ passTime(1000);
149+ {
150+ QKeyEvent keyEvent(QEvent::KeyPress, Qt::Key_Home, Qt::NoModifier);
151+ homeKeyWatcher.update(&keyEvent);
152+ }
153+ passTime(100);
154+ {
155+ QKeyEvent keyEvent(QEvent::KeyRelease, Qt::Key_Home, Qt::NoModifier);
156+ homeKeyWatcher.update(&keyEvent);
157+ }
158+ passTime(1000);
159+ {
160+ QTouchEvent touchEvent(QEvent::TouchEnd);
161+ homeKeyWatcher.update(&touchEvent);
162+ }
163+ passTime(1000);
164+
165+
166+ QCOMPARE(activatedSpy.count(), 0);
167+}
168+
169+QTEST_GUILESS_MAIN(HomeKeyWatcherTest)
170+
171+#include "homekeywatchertest.moc"