@@ -0,0 +1,150 @@
1+/*
2+ * Copyright (C) 2013 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 "GestureTest.h"
18+
19+#include <PressedOutsideNotifier.h>
20+
21+#include <QQuickItem>
22+#include <QQuickView>
23+#include <QtTest>
24+
25+class tst_PressedOutsideNotifier: public GestureTest
26+{
27+ Q_OBJECT
28+public:
29+ tst_PressedOutsideNotifier();
30+private Q_SLOTS:
31+ void init(); // called right before each and every test function is executed
32+
33+ void touchOutsideAreaTriggersSignal();
34+ void touchInsideAreaHasNoEffect();
35+ void mousePressOutsideAreaTriggersSignal();
36+ void mousePresssInsideAreaHasNoEffect();
37+ void nothingHappensWhileDisabled();
38+
39+private:
40+ QQuickItem *m_blueRect;
41+ PressedOutsideNotifier *m_notifier;
42+};
43+
44+tst_PressedOutsideNotifier::tst_PressedOutsideNotifier()
45+ : GestureTest(QStringLiteral("tst_PressedOutsideNotifier.qml"))
46+{
47+}
48+
49+void tst_PressedOutsideNotifier::init()
50+{
51+ GestureTest::init();
52+
53+ m_blueRect = m_view->rootObject()->findChild<QQuickItem*>("blueRect");
54+ Q_ASSERT(m_blueRect != nullptr);
55+
56+ m_notifier =
57+ m_view->rootObject()->findChild<PressedOutsideNotifier*>("pressedOutsideNotifier");
58+ Q_ASSERT(m_notifier != nullptr);
59+}
60+
61+void tst_PressedOutsideNotifier::touchOutsideAreaTriggersSignal()
62+{
63+ // half-way towards the top-left corner of the blueRect.
64+ // Therefore, still outside it.
65+ QPoint touch0 = QPoint(m_blueRect->x() * 0.5, m_blueRect->y() * 0.5);
66+
67+ QSignalSpy pressedOutsideSpy(m_notifier, SIGNAL(pressedOutside()));
68+
69+ QTest::touchEvent(m_view, m_device)
70+ .press(0, touch0);
71+
72+ qApp->processEvents();
73+
74+ QCOMPARE(pressedOutsideSpy.count(), 1);
75+
76+ QTest::touchEvent(m_view, m_device)
77+ .release(0, touch0);
78+}
79+
80+void tst_PressedOutsideNotifier::touchInsideAreaHasNoEffect()
81+{
82+ QPoint touch0 = QPoint(m_blueRect->x() + m_blueRect->width()*0.5,
83+ m_blueRect->y() + m_blueRect->height()*0.5);
84+
85+ QSignalSpy pressedOutsideSpy(m_notifier, SIGNAL(pressedOutside()));
86+
87+ QTest::touchEvent(m_view, m_device)
88+ .press(0, touch0);
89+
90+ qApp->processEvents();
91+
92+ QCOMPARE(pressedOutsideSpy.count(), 0);
93+
94+ QTest::touchEvent(m_view, m_device)
95+ .release(0, touch0);
96+}
97+
98+void tst_PressedOutsideNotifier::mousePressOutsideAreaTriggersSignal()
99+{
100+ QPoint mousePos= QPoint(m_blueRect->x() * 0.5, m_blueRect->y() * 0.5);
101+
102+ QSignalSpy pressedOutsideSpy(m_notifier, SIGNAL(pressedOutside()));
103+
104+ QTest::mousePress(m_view, Qt::LeftButton, 0 /*modifiers*/, mousePos);
105+
106+ qApp->processEvents();
107+
108+ QCOMPARE(pressedOutsideSpy.count(), 1);
109+
110+ QTest::mouseRelease(m_view, Qt::LeftButton, 0 /*modifiers*/, mousePos);
111+}
112+
113+void tst_PressedOutsideNotifier::mousePresssInsideAreaHasNoEffect()
114+{
115+ QPoint mousePos = QPoint(m_blueRect->x() + m_blueRect->width()*0.5,
116+ m_blueRect->y() + m_blueRect->height()*0.5);
117+
118+ QSignalSpy pressedOutsideSpy(m_notifier, SIGNAL(pressedOutside()));
119+
120+ QTest::mousePress(m_view, Qt::LeftButton, 0 /*modifiers*/, mousePos);
121+
122+ qApp->processEvents();
123+
124+ QCOMPARE(pressedOutsideSpy.count(), 0);
125+
126+ QTest::mouseRelease(m_view, Qt::LeftButton, 0 /*modifiers*/, mousePos);
127+}
128+
129+void tst_PressedOutsideNotifier::nothingHappensWhileDisabled()
130+{
131+ QPoint touch0 = QPoint(m_blueRect->x() * 0.5, m_blueRect->y() * 0.5);
132+
133+ QSignalSpy pressedOutsideSpy(m_notifier, SIGNAL(pressedOutside()));
134+
135+ m_notifier->setEnabled(false);
136+
137+ QTest::touchEvent(m_view, m_device)
138+ .press(0, touch0);
139+
140+ qApp->processEvents();
141+
142+ QCOMPARE(pressedOutsideSpy.count(), 0);
143+
144+ QTest::touchEvent(m_view, m_device)
145+ .release(0, touch0);
146+}
147+
148+QTEST_MAIN(tst_PressedOutsideNotifier)
149+
150+#include "tst_PressedOutsideNotifier.moc"