on xenial help sign in
Modifications according to review feedback
5ac6c11Daniel d'Andrada
plugins/Utils/CMakeLists.txt+1−2
@@ -9,14 +9,13 @@ include_directories(
9
9
10set(QMLPLUGIN_SRC
10set(QMLPLUGIN_SRC
11 constants.cpp
11 constants.cpp
12− homekeywatcher.cpp
12+ HomeKeyWatcher.cpp
13 inputwatcher.cpp
13 inputwatcher.cpp
14 qlimitproxymodelqml.cpp
14 qlimitproxymodelqml.cpp
15 unitysortfilterproxymodelqml.cpp
15 unitysortfilterproxymodelqml.cpp
16 relativetimeformatter.cpp
16 relativetimeformatter.cpp
17 timeformatter.cpp
17 timeformatter.cpp
18 Timer.cpp
18 Timer.cpp
19− ElapsedTimer.cpp
20 unitymenumodelpaths.cpp
19 unitymenumodelpaths.cpp
21 windowkeysfilter.cpp
20 windowkeysfilter.cpp
22 easingcurve.cpp
21 easingcurve.cpp
plugins/Utils/ElapsedTimer.cppdeleted−21
@@ -1,21 +0,0 @@
1−/*
2− * Copyright (C) 2015 - Canonical Ltd.
3− *
4− * This program is free software: you can redistribute it and/or modify it
5− * under the terms of the GNU Lesser General Public License, as
6− * published by the Free Software Foundation; either version 2.1 or 3.0
7− * of the License.
8− *
9− * This program is distributed in the hope that it will be useful, but
10− * WITHOUT ANY WARRANTY; without even the implied warranties of
11− * MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR
12− * PURPOSE. See the applicable version of the GNU Lesser General Public
13− * License for more details.
14− *
15− * You should have received a copy of both the GNU Lesser General Public
16− * License along with this program. If not, see <http://www.gnu.org/licenses/>
17− */
18−
19−#include "ElapsedTimer.h"
20−
21−qint64 UnityUtil::FakeElapsedTimer::msecsSinceEpoch = 0;
plugins/Utils/ElapsedTimer.h+4−18
@@ -23,6 +23,9 @@
23
23
24namespace UnityUtil {
24namespace UnityUtil {
25
25
26+/**
27+ Interface for ElapsedTimer implementations
28+ */
26class AbstractElapsedTimer {
29class AbstractElapsedTimer {
27public:
30public:
28 virtual ~AbstractElapsedTimer() {}
31 virtual ~AbstractElapsedTimer() {}
@@ -31,7 +34,7 @@ public:
31 virtual qint64 elapsed() const = 0;
34 virtual qint64 elapsed() const = 0;
32};
35};
33
36
34−/*
37+/**
35 A QElapsedTimer wrapper
38 A QElapsedTimer wrapper
36 */
39 */
37class ElapsedTimer : public AbstractElapsedTimer {
40class ElapsedTimer : public AbstractElapsedTimer {
@@ -43,23 +46,6 @@ private:
43 QElapsedTimer m_timer;
46 QElapsedTimer m_timer;
44};
47};
45
48
46−/*
47− A fake ElapsedTimer, useful for tests
48− */
49−class FakeElapsedTimer : public AbstractElapsedTimer {
50−public:
51− static qint64 msecsSinceEpoch;
52−
53− FakeElapsedTimer() { start(); }
54−
55− void start() override { m_msecsSinceReference = msecsSinceEpoch; }
56− qint64 msecsSinceReference() const override { return m_msecsSinceReference; }
57− qint64 elapsed() const override { return msecsSinceEpoch - m_msecsSinceReference; }
58−
59−private:
60− qint64 m_msecsSinceReference;
61−};
62−
63} // namespace UnityUtil
49} // namespace UnityUtil
64
50
65#endif // UNITYUTIL_ELAPSEDTIMER_H
51#endif // UNITYUTIL_ELAPSEDTIMER_H
plugins/Utils/homekeywatcher.cpp → plugins/Utils/HomeKeyWatcher.cpprenamed+4−4
@@ -14,7 +14,7 @@
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
15 */
16
16
17−#include "homekeywatcher.h"
17+#include "HomeKeyWatcher.h"
18
18
19#include <QQuickWindow>
19#include <QQuickWindow>
20
20
@@ -33,6 +33,8 @@ HomeKeyWatcher::HomeKeyWatcher(UnityUtil::AbstractTimer *timer,
33 , m_windowLastTouchedTimer(elapsedTimer)
33 , m_windowLastTouchedTimer(elapsedTimer)
34 , m_emitActivatedIfNoTouchesAroundTimer(timer)
34 , m_emitActivatedIfNoTouchesAroundTimer(timer)
35{
35{
36+ m_windowLastTouchedTimer->start();
37+
36 connect(this, &QQuickItem::windowChanged,
38 connect(this, &QQuickItem::windowChanged,
37 this, &HomeKeyWatcher::setupFilterOnWindow);
39 this, &HomeKeyWatcher::setupFilterOnWindow);
38
40
@@ -64,7 +66,7 @@ void HomeKeyWatcher::update(QEvent *event)
64 if (event->type() == QEvent::KeyPress) {
66 if (event->type() == QEvent::KeyPress) {
65 QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
67 QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
66
68
67− if (keyEvent->key() == Qt::Key_Home && !keyEvent->isAutoRepeat()
69+ if (keyEvent->key() == Qt::Key_Super_L && !keyEvent->isAutoRepeat()
68 && !m_windowBeingTouched
70 && !m_windowBeingTouched
69 && m_windowLastTouchedTimer->elapsed() >= msecsWithoutTouches) {
71 && m_windowLastTouchedTimer->elapsed() >= msecsWithoutTouches) {
70 m_emitActivatedIfNoTouchesAroundTimer->start();
72 m_emitActivatedIfNoTouchesAroundTimer->start();
@@ -101,5 +103,3 @@ void HomeKeyWatcher::emitActivatedIfNoTouchesAround()
101 Q_EMIT activated();
103 Q_EMIT activated();
102 }
104 }
103}
105}
104−
105−#include "homekeywatcher.moc"
plugins/Utils/homekeywatcher.h → plugins/Utils/HomeKeyWatcher.hrenamed

The content is the same.

plugins/Utils/Timer.cpp−101
@@ -56,105 +56,4 @@ void Timer::setSingleShot(bool value)
56 m_timer.setSingleShot(value);
56 m_timer.setSingleShot(value);
57}
57}
58
58
59−/////////////////////////////////// FakeTimer //////////////////////////////////
60−
61−FakeTimer::FakeTimer(QObject *parent)
62− : UnityUtil::AbstractTimer(parent)
63− , m_interval(0)
64− , m_singleShot(true)
65−{
66−}
67−
68−void FakeTimer::update()
69−{
70− if (!isRunning()) {
71− return;
72− }
73−
74− if (m_nextTimeoutTime <= FakeElapsedTimer::msecsSinceEpoch) {
75− if (isSingleShot()) {
76− stop();
77− } else {
78− m_nextTimeoutTime += interval();
79− }
80− Q_EMIT timeout();
81− }
82−}
83−
84−void FakeTimer::start()
85−{
86− AbstractTimer::start();
87− m_nextTimeoutTime = FakeElapsedTimer::msecsSinceEpoch + (qint64)interval();
88−}
89−
90−int FakeTimer::interval() const
91−{
92− return m_interval;
93−}
94−
95−void FakeTimer::setInterval(int msecs)
96−{
97− m_interval = msecs;
98−}
99−
100−bool FakeTimer::isSingleShot() const
101−{
102− return m_singleShot;
103−}
104−
105−void FakeTimer::setSingleShot(bool value)
106−{
107− m_singleShot = value;
108−}
109−
110−/////////////////////////////////// FakeTimerFactory //////////////////////////////////
111−
112−FakeTimerFactory::FakeTimerFactory()
113−{
114−}
115−
116−FakeTimerFactory::~FakeTimerFactory()
117−{
118− for (int i = 0; i < timers.count(); ++i) {
119− FakeTimer *timer = timers[i].data();
120− if (timer) {
121− delete timer;
122− }
123− }
124−}
125−
126−void FakeTimerFactory::updateTime(qint64 targetTime)
127−{
128− qint64 minTimeoutTime = targetTime;
129−
130− for (int i = 0; i < timers.count(); ++i) {
131− FakeTimer *timer = timers[i].data();
132− if (timer && timer->isRunning() && timer->nextTimeoutTime() < minTimeoutTime) {
133− minTimeoutTime = timer->nextTimeoutTime();
134− }
135− }
136−
137− FakeElapsedTimer::msecsSinceEpoch = minTimeoutTime;
138−
139− for (int i = 0; i < timers.count(); ++i) {
140− FakeTimer *timer = timers[i].data();
141− if (timer) {
142− timer->update();
143− }
144− }
145−
146− if (FakeElapsedTimer::msecsSinceEpoch < targetTime) {
147− updateTime(targetTime);
148− }
149−}
150−
151−AbstractTimer *FakeTimerFactory::create(QObject *parent)
152−{
153− FakeTimer *fakeTimer = new FakeTimer(parent);
154−
155− timers.append(fakeTimer);
156−
157− return fakeTimer;
158−}
159−
160} // namespace UnityUtil
59} // namespace UnityUtil
plugins/Utils/Timer.h+2−35
@@ -25,7 +25,7 @@
25
25
26namespace UnityUtil {
26namespace UnityUtil {
27
27
28−/* Defines an interface for a Timer. Useful for tests. */
28+/** Defines an interface for a Timer. Useful for tests. */
29class AbstractTimer : public QObject
29class AbstractTimer : public QObject
30{
30{
31 Q_OBJECT
31 Q_OBJECT
@@ -44,7 +44,7 @@ private:
44 bool m_isRunning;
44 bool m_isRunning;
45};
45};
46
46
47−/* Essentially a QTimer wrapper */
47+/** A QTimer wrapper */
48class Timer : public AbstractTimer
48class Timer : public AbstractTimer
49{
49{
50 Q_OBJECT
50 Q_OBJECT
@@ -61,27 +61,6 @@ private:
61 QTimer m_timer;
61 QTimer m_timer;
62};
62};
63
63
64−/* For tests */
65−class FakeTimer : public AbstractTimer
66−{
67− Q_OBJECT
68−public:
69− FakeTimer(QObject *parent = nullptr);
70−
71− void update();
72− qint64 nextTimeoutTime() const { return m_nextTimeoutTime; }
73−
74− int interval() const override;
75− void setInterval(int msecs) override;
76− void start() override;
77− bool isSingleShot() const override;
78− void setSingleShot(bool value) override;
79−private:
80− int m_interval;
81− bool m_singleShot;
82− qint64 m_nextTimeoutTime;
83−};
84−
85class AbstractTimerFactory
64class AbstractTimerFactory
86{
65{
87public:
66public:
@@ -95,18 +74,6 @@ public:
95 AbstractTimer *create(QObject *parent = nullptr) override { return new Timer(parent); }
74 AbstractTimer *create(QObject *parent = nullptr) override { return new Timer(parent); }
96};
75};
97
76
98−class FakeTimerFactory : public AbstractTimerFactory
99−{
100−public:
101− FakeTimerFactory();
102− virtual ~FakeTimerFactory();
103−
104− void updateTime(qint64 targetTime);
105−
106− AbstractTimer *create(QObject *parent = nullptr) override;
107− QList<QPointer<FakeTimer>> timers;
108−};
109−
110} // namespace UnityUtil
77} // namespace UnityUtil
111
78
112#endif // UNITYUTIL_TIMER_H
79#endif // UNITYUTIL_TIMER_H
plugins/Utils/plugin.cpp+1−1
@@ -23,7 +23,7 @@
23#include "plugin.h"
23#include "plugin.h"
24
24
25// local
25// local
26−#include "homekeywatcher.h"
26+#include "HomeKeyWatcher.h"
27#include "inputwatcher.h"
27#include "inputwatcher.h"
28#include "qlimitproxymodelqml.h"
28#include "qlimitproxymodelqml.h"
29#include "unitysortfilterproxymodelqml.h"
29#include "unitysortfilterproxymodelqml.h"
tests/plugins/Utils/homekeywatchertest.cpp+152−1
@@ -14,11 +14,61 @@
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
15 */
16
16
17−#include "homekeywatcher.h"
17+#include "HomeKeyWatcher.h"
18
18
19#include <QTest>
19#include <QTest>
20#include <QSignalSpy>
20#include <QSignalSpy>
21
21
22+
23+namespace UnityUtil {
24+class FakeElapsedTimer : public AbstractElapsedTimer {
25+public:
26+ static qint64 msecsSinceEpoch;
27+
28+ FakeElapsedTimer() { start(); }
29+
30+ void start() override { m_msecsSinceReference = msecsSinceEpoch; }
31+ qint64 msecsSinceReference() const override { return m_msecsSinceReference; }
32+ qint64 elapsed() const override { return msecsSinceEpoch - m_msecsSinceReference; }
33+
34+private:
35+ qint64 m_msecsSinceReference;
36+};
37+qint64 FakeElapsedTimer::msecsSinceEpoch = 0;
38+
39+class FakeTimer : public AbstractTimer
40+{
41+ Q_OBJECT
42+public:
43+ FakeTimer(QObject *parent = nullptr);
44+
45+ void update();
46+ qint64 nextTimeoutTime() const { return m_nextTimeoutTime; }
47+
48+ int interval() const override;
49+ void setInterval(int msecs) override;
50+ void start() override;
51+ bool isSingleShot() const override;
52+ void setSingleShot(bool value) override;
53+private:
54+ int m_interval;
55+ bool m_singleShot;
56+ qint64 m_nextTimeoutTime;
57+};
58+
59+class FakeTimerFactory : public AbstractTimerFactory
60+{
61+public:
62+ FakeTimerFactory();
63+ virtual ~FakeTimerFactory();
64+
65+ void updateTime(qint64 targetTime);
66+
67+ AbstractTimer *create(QObject *parent = nullptr) override;
68+ QList<QPointer<FakeTimer>> timers;
69+};
70+} // namespace UnityUtil
71+
22using namespace UnityUtil;
72using namespace UnityUtil;
23
73
24class HomeKeyWatcherTest : public QObject {
74class HomeKeyWatcherTest : public QObject {
@@ -166,6 +216,107 @@ void HomeKeyWatcherTest::tapWhileTouching()
166 QCOMPARE(activatedSpy.count(), 0);
216 QCOMPARE(activatedSpy.count(), 0);
167}
217}
168
218
219+/////////////////////////////////// FakeTimer //////////////////////////////////
220+
221+FakeTimer::FakeTimer(QObject *parent)
222+ : UnityUtil::AbstractTimer(parent)
223+ , m_interval(0)
224+ , m_singleShot(true)
225+{
226+}
227+
228+void FakeTimer::update()
229+{
230+ if (!isRunning()) {
231+ return;
232+ }
233+
234+ if (m_nextTimeoutTime <= FakeElapsedTimer::msecsSinceEpoch) {
235+ if (isSingleShot()) {
236+ stop();
237+ } else {
238+ m_nextTimeoutTime += interval();
239+ }
240+ Q_EMIT timeout();
241+ }
242+}
243+
244+void FakeTimer::start()
245+{
246+ AbstractTimer::start();
247+ m_nextTimeoutTime = FakeElapsedTimer::msecsSinceEpoch + (qint64)interval();
248+}
249+
250+int FakeTimer::interval() const
251+{
252+ return m_interval;
253+}
254+
255+void FakeTimer::setInterval(int msecs)
256+{
257+ m_interval = msecs;
258+}
259+
260+bool FakeTimer::isSingleShot() const
261+{
262+ return m_singleShot;
263+}
264+
265+void FakeTimer::setSingleShot(bool value)
266+{
267+ m_singleShot = value;
268+}
269+
270+/////////////////////////////////// FakeTimerFactory //////////////////////////////////
271+
272+FakeTimerFactory::FakeTimerFactory()
273+{
274+}
275+
276+FakeTimerFactory::~FakeTimerFactory()
277+{
278+ for (int i = 0; i < timers.count(); ++i) {
279+ FakeTimer *timer = timers[i].data();
280+ if (timer) {
281+ delete timer;
282+ }
283+ }
284+}
285+
286+void FakeTimerFactory::updateTime(qint64 targetTime)
287+{
288+ qint64 minTimeoutTime = targetTime;
289+
290+ for (int i = 0; i < timers.count(); ++i) {
291+ FakeTimer *timer = timers[i].data();
292+ if (timer && timer->isRunning() && timer->nextTimeoutTime() < minTimeoutTime) {
293+ minTimeoutTime = timer->nextTimeoutTime();
294+ }
295+ }
296+
297+ FakeElapsedTimer::msecsSinceEpoch = minTimeoutTime;
298+
299+ for (int i = 0; i < timers.count(); ++i) {
300+ FakeTimer *timer = timers[i].data();
301+ if (timer) {
302+ timer->update();
303+ }
304+ }
305+
306+ if (FakeElapsedTimer::msecsSinceEpoch < targetTime) {
307+ updateTime(targetTime);
308+ }
309+}
310+
311+AbstractTimer *FakeTimerFactory::create(QObject *parent)
312+{
313+ FakeTimer *fakeTimer = new FakeTimer(parent);
314+
315+ timers.append(fakeTimer);
316+
317+ return fakeTimer;
318+}
319+
169QTEST_GUILESS_MAIN(HomeKeyWatcherTest)
320QTEST_GUILESS_MAIN(HomeKeyWatcherTest)
170
321
171#include "homekeywatchertest.moc"
322#include "homekeywatchertest.moc"