| 1 | /* |
| 2 | * Copyright (C) 2013 Canonical Ltd. |
| 3 | * |
| 4 | * Contact: Alberto Mardegan <[email protected]> |
| 5 | * |
| 6 | * This file is part of lomiri-online-accounts-service |
| 7 | * |
| 8 | * This program is free software: you can redistribute it and/or modify it |
| 9 | * under the terms of the GNU General Public License version 3, as published |
| 10 | * by the Free Software Foundation. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, but |
| 13 | * WITHOUT ANY WARRANTY; without even the implied warranties of |
| 14 | * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR |
| 15 | * PURPOSE. See the GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License along |
| 18 | * with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 | */ |
| 20 | |
| 21 | #include "inactivity-timer.h" |
| 22 | |
| 23 | #include <QDebug> |
| 24 | #include <QSignalSpy> |
| 25 | #include <QTest> |
| 26 | |
| 27 | using namespace OnlineAccountsUi; |
| 28 | |
| 29 | class Service: public QObject |
| 30 | { |
| 31 | Q_OBJECT |
| 32 | Q_PROPERTY(bool isIdle READ isIdle NOTIFY isIdleChanged) |
| 33 | |
| 34 | public: |
| 35 | Service(): QObject(), m_isIdle(true) {} |
| 36 | |
| 37 | bool isIdle() const { return m_isIdle; } |
| 38 | |
| 39 | void setIdle(bool idle) { |
| 40 | if (idle == m_isIdle) return; |
| 41 | m_isIdle = idle; |
| 42 | Q_EMIT isIdleChanged(); |
| 43 | } |
| 44 | |
| 45 | Q_SIGNALS: |
| 46 | void isIdleChanged(); |
| 47 | |
| 48 | private: |
| 49 | bool m_isIdle; |
| 50 | }; |
| 51 | |
| 52 | class InactivityTimerTest: public QObject |
| 53 | { |
| 54 | Q_OBJECT |
| 55 | |
| 56 | public: |
| 57 | InactivityTimerTest(); |
| 58 | |
| 59 | private Q_SLOTS: |
| 60 | void testAlwaysIdle(); |
| 61 | void testBecomeIdle(); |
| 62 | void testManyServices(); |
| 63 | }; |
| 64 | |
| 65 | InactivityTimerTest::InactivityTimerTest(): |
| 66 | QObject(0) |
| 67 | { |
| 68 | } |
| 69 | |
| 70 | void InactivityTimerTest::testAlwaysIdle() |
| 71 | { |
| 72 | InactivityTimer timer(10); |
| 73 | QSignalSpy timeout(&timer, SIGNAL(timeout())); |
| 74 | |
| 75 | Service service; |
| 76 | timer.watchObject(&service); |
| 77 | |
| 78 | QVERIFY(timeout.wait(100)); |
| 79 | } |
| 80 | |
| 81 | void InactivityTimerTest::testBecomeIdle() |
| 82 | { |
| 83 | InactivityTimer timer(10); |
| 84 | QSignalSpy timeout(&timer, SIGNAL(timeout())); |
| 85 | |
| 86 | Service service; |
| 87 | service.setIdle(false); |
| 88 | timer.watchObject(&service); |
| 89 | |
| 90 | /* No signal should be emitted, as the service is not idle */ |
| 91 | QVERIFY(!timeout.wait(100)); |
| 92 | |
| 93 | service.setIdle(true); |
| 94 | QVERIFY(timeout.wait(100)); |
| 95 | } |
| 96 | |
| 97 | void InactivityTimerTest::testManyServices() |
| 98 | { |
| 99 | InactivityTimer timer(50); |
| 100 | QSignalSpy timeout(&timer, SIGNAL(timeout())); |
| 101 | |
| 102 | Service service1; |
| 103 | timer.watchObject(&service1); |
| 104 | |
| 105 | Service service2; |
| 106 | timer.watchObject(&service2); |
| 107 | |
| 108 | Service service3; |
| 109 | service3.setIdle(false); |
| 110 | timer.watchObject(&service3); |
| 111 | |
| 112 | /* No signal should be emitted, as service3 is not idle */ |
| 113 | QVERIFY(!timeout.wait(100)); |
| 114 | |
| 115 | /* Now set it is as idle, but soon afterwards set service1 as busy */ |
| 116 | service3.setIdle(true); |
| 117 | QTest::qWait(10); |
| 118 | service1.setIdle(false); |
| 119 | QVERIFY(!timeout.wait(100)); |
| 120 | |
| 121 | service1.setIdle(true); |
| 122 | QVERIFY(timeout.wait(100)); |
| 123 | } |
| 124 | |
| 125 | QTEST_MAIN(InactivityTimerTest); |
| 126 | |
| 127 | #include "tst_inactivity_timer.moc" |