on xenial help sign in
Modifications according to review feedback
commit5ac6c11fa4f4ff495bf88d459bc1d21963e414fe authorDaniel d'Andrada <daniel.dandrada@canonical.com> dateWed 22 Apr 2015, 07:40 (11 years ago) parent6c9ed82
9 files changed+164−183
plugins/Utils/CMakeLists.txt+1−2 plugins/Utils/ElapsedTimer.cpp−21 plugins/Utils/ElapsedTimer.h+4−18 plugins/Utils/homekeywatcher.cpp → plugins/Utils/HomeKeyWatcher.cpp+4−4 plugins/Utils/homekeywatcher.h → plugins/Utils/HomeKeyWatcher.h plugins/Utils/Timer.cpp−101 plugins/Utils/Timer.h+2−35 plugins/Utils/plugin.cpp+1−1 tests/plugins/Utils/homekeywatchertest.cpp+152−1
plugins/Utils/CMakeLists.txt+1−2
@@ -9,14 +9,13 @@ include_directories(
99
1010set(QMLPLUGIN_SRC
1111 constants.cpp
12− homekeywatcher.cpp
12+ HomeKeyWatcher.cpp
1313 inputwatcher.cpp
1414 qlimitproxymodelqml.cpp
1515 unitysortfilterproxymodelqml.cpp
1616 relativetimeformatter.cpp
1717 timeformatter.cpp
1818 Timer.cpp
19− ElapsedTimer.cpp
2019 unitymenumodelpaths.cpp
2120 windowkeysfilter.cpp
2221 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 @@
2323
2424namespace UnityUtil {
2525
26+/**
27+ Interface for ElapsedTimer implementations
28+ */
2629class AbstractElapsedTimer {
2730public:
2831 virtual ~AbstractElapsedTimer() {}
@@ -31,7 +34,7 @@ public:
3134 virtual qint64 elapsed() const = 0;
3235};
3336
34−/*
37+/**
3538 A QElapsedTimer wrapper
3639 */
3740class ElapsedTimer : public AbstractElapsedTimer {
@@ -43,23 +46,6 @@ private:
4346 QElapsedTimer m_timer;
4447};
4548
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−
6349} // namespace UnityUtil
6450
6551#endif // UNITYUTIL_ELAPSEDTIMER_H
plugins/Utils/homekeywatcher.cpp → plugins/Utils/HomeKeyWatcher.cpprenamed+4−4
@@ -14,7 +14,7 @@
1414 * along with this program. If not, see <http://www.gnu.org/licenses/>.
1515 */
1616
17−#include "homekeywatcher.h"
17+#include "HomeKeyWatcher.h"
1818
1919#include <QQuickWindow>
2020
@@ -33,6 +33,8 @@ HomeKeyWatcher::HomeKeyWatcher(UnityUtil::AbstractTimer *timer,
3333 , m_windowLastTouchedTimer(elapsedTimer)
3434 , m_emitActivatedIfNoTouchesAroundTimer(timer)
3535{
36+ m_windowLastTouchedTimer->start();
37+
3638 connect(this, &QQuickItem::windowChanged,
3739 this, &HomeKeyWatcher::setupFilterOnWindow);
3840
@@ -64,7 +66,7 @@ void HomeKeyWatcher::update(QEvent *event)
6466 if (event->type() == QEvent::KeyPress) {
6567 QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
6668
67− if (keyEvent->key() == Qt::Key_Home && !keyEvent->isAutoRepeat()
69+ if (keyEvent->key() == Qt::Key_Super_L && !keyEvent->isAutoRepeat()
6870 && !m_windowBeingTouched
6971 && m_windowLastTouchedTimer->elapsed() >= msecsWithoutTouches) {
7072 m_emitActivatedIfNoTouchesAroundTimer->start();
@@ -101,5 +103,3 @@ void HomeKeyWatcher::emitActivatedIfNoTouchesAround()
101103 Q_EMIT activated();
102104 }
103105}
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)
5656 m_timer.setSingleShot(value);
5757}
5858
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−
16059} // namespace UnityUtil
plugins/Utils/Timer.h+2−35
@@ -25,7 +25,7 @@
2525
2626namespace UnityUtil {
2727
28−/* Defines an interface for a Timer. Useful for tests. */
28+/** Defines an interface for a Timer. Useful for tests. */
2929class AbstractTimer : public QObject
3030{
3131 Q_OBJECT
@@ -44,7 +44,7 @@ private:
4444 bool m_isRunning;
4545};
4646
47−/* Essentially a QTimer wrapper */
47+/** A QTimer wrapper */
4848class Timer : public AbstractTimer
4949{
5050 Q_OBJECT
@@ -61,27 +61,6 @@ private:
6161 QTimer m_timer;
6262};
6363
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−
8564class AbstractTimerFactory
8665{
8766public:
@@ -95,18 +74,6 @@ public:
9574 AbstractTimer *create(QObject *parent = nullptr) override { return new Timer(parent); }
9675};
9776
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−
11077} // namespace UnityUtil
11178
11279#endif // UNITYUTIL_TIMER_H
plugins/Utils/plugin.cpp+1−1
@@ -23,7 +23,7 @@
2323#include "plugin.h"
2424
2525// local
26−#include "homekeywatcher.h"
26+#include "HomeKeyWatcher.h"
2727#include "inputwatcher.h"
2828#include "qlimitproxymodelqml.h"
2929#include "unitysortfilterproxymodelqml.h"
tests/plugins/Utils/homekeywatchertest.cpp+152−1
@@ -14,11 +14,61 @@
1414 * along with this program. If not, see <http://www.gnu.org/licenses/>.
1515 */
1616
17−#include "homekeywatcher.h"
17+#include "HomeKeyWatcher.h"
1818
1919#include <QTest>
2020#include <QSignalSpy>
2121
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+
2272using namespace UnityUtil;
2373
2474class HomeKeyWatcherTest : public QObject {
@@ -166,6 +216,107 @@ void HomeKeyWatcherTest::tapWhileTouching()
166216 QCOMPARE(activatedSpy.count(), 0);
167217}
168218
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+
169320QTEST_GUILESS_MAIN(HomeKeyWatcherTest)
170321
171322#include "homekeywatchertest.moc"