summaryrefslogtreecommitdiffstats
blob: fa9b18402871603c632e2b05934eb11de2ba0ccc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*
 * Copyright (c) 2010-2016 Isode Limited.
 * All rights reserved.
 * See the COPYING file for more information.
 */

#include <memory>

#include <boost/bind.hpp>

#include <cppunit/extensions/HelperMacros.h>
#include <cppunit/extensions/TestFactoryRegistry.h>

#include <Swiften/Network/Timer.h>
#include <Swiften/Network/TimerFactory.h>

#include <SwifTools/Idle/ActualIdleDetector.h>
#include <SwifTools/Idle/IdleQuerier.h>

using namespace Swift;

class ActualIdleDetectorTest : public CppUnit::TestFixture {
        CPPUNIT_TEST_SUITE(ActualIdleDetectorTest);
        CPPUNIT_TEST(testDestructor);
        CPPUNIT_TEST(testHandleTick_Idle);
        CPPUNIT_TEST(testHandleTick_Idle_AlreadyIdle);
        CPPUNIT_TEST(testHandleTick_NotIdle);
        CPPUNIT_TEST(testHandleTick_NotIdle_AlreadyNotIdle);
        CPPUNIT_TEST_SUITE_END();

    public:
        void setUp() {
            querier = new MockIdleQuerier();
            timerFactory = new MockTimerFactory();
            idleEvents.clear();
        }

        void tearDown() {
            delete timerFactory;
            delete querier;
        }

        void testDestructor()  {
            ActualIdleDetector* testling = createDetector();
            testling->setIdleTimeSeconds(15);
            delete testling;

            querier->idleTime = 15;
            timerFactory->updateTime(15000);

            CPPUNIT_ASSERT_EQUAL(0, static_cast<int>(idleEvents.size()));
        }

        void testHandleTick_Idle() {
            std::unique_ptr<ActualIdleDetector> testling(createDetector());
            testling->setIdleTimeSeconds(15);
            querier->idleTime = 15;

            timerFactory->updateTime(15000);

            CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(idleEvents.size()));
            CPPUNIT_ASSERT(idleEvents[0]);
        }

        void testHandleTick_Idle_AlreadyIdle() {
            std::unique_ptr<ActualIdleDetector> testling(createDetector());
            testling->setIdleTimeSeconds(15);
            querier->idleTime = 15;
            timerFactory->updateTime(15000);

            querier->idleTime = 30;
            timerFactory->updateTime(30000);

            CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(idleEvents.size()));
            CPPUNIT_ASSERT(idleEvents[0]);
        }

        void testHandleTick_NotIdle() {
            std::unique_ptr<ActualIdleDetector> testling(createDetector());
            testling->setIdleTimeSeconds(15);
            querier->idleTime = 15;
            timerFactory->updateTime(15000);

            querier->idleTime = 5;
            timerFactory->updateTime(30000);

            CPPUNIT_ASSERT_EQUAL(2, static_cast<int>(idleEvents.size()));
            CPPUNIT_ASSERT(idleEvents[0]);
            CPPUNIT_ASSERT(!idleEvents[1]);
        }

        void testHandleTick_NotIdle_AlreadyNotIdle() {
            std::unique_ptr<ActualIdleDetector> testling(createDetector());
            testling->setIdleTimeSeconds(15);
            querier->idleTime = 5;

            timerFactory->updateTime(15000);

            CPPUNIT_ASSERT_EQUAL(0, static_cast<int>(idleEvents.size()));
        }

    private:
        ActualIdleDetector* createDetector() {
            ActualIdleDetector* detector = new ActualIdleDetector(querier, timerFactory, 10);
            detector->onIdleChanged.connect(boost::bind(&ActualIdleDetectorTest::handleIdle, this, _1));
            return detector;
        }

        void handleIdle(bool b) {
            idleEvents.push_back(b);
        }

    private:
        struct MockIdleQuerier : public IdleQuerier {
            MockIdleQuerier() : idleTime(0) {}
            virtual int getIdleTimeSeconds() { return idleTime; }
            int idleTime;
        };

        struct MockTimer : public Timer {
            MockTimer(int interval) : interval(interval), running(false), lastTime(0) {}

            virtual void start() {
                running = true;
            }

            virtual void stop() {
                running = false;
            }

            virtual void updateTime(int currentTime) {
                if (lastTime == currentTime) {
                    return;
                }
                if (running) {
                    int time = lastTime;
                    while (time <= currentTime) {
                        onTick();
                        time += interval;
                    }
                }
                lastTime = currentTime;
            }

            int interval;
            bool running;
            int lastTime;
        };

        struct MockTimerFactory : public TimerFactory {
            MockTimerFactory() {}

            void updateTime(int milliseconds) {
                for (std::shared_ptr<MockTimer> timer : timers) {
                    timer->updateTime(milliseconds);
                }
            }

            std::shared_ptr<Timer> createTimer(int milliseconds) {
                std::shared_ptr<MockTimer> timer(new MockTimer(milliseconds));
                timers.push_back(timer);
                return timer;
            }

            std::vector<std::shared_ptr<MockTimer> > timers;
        };

        MockIdleQuerier* querier;
        MockTimerFactory* timerFactory;
        std::vector<bool> idleEvents;
};

CPPUNIT_TEST_SUITE_REGISTRATION(ActualIdleDetectorTest);