summaryrefslogtreecommitdiff
path: root/vcl/qa/cppunit/timer.cxx
blob: 0732baaf5a96352901860f7e5fae7fa427765081 (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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */
/*
 * Timers are evil beasties across platforms ...
 */

#include <test/bootstrapfixture.hxx>

#include <osl/thread.hxx>
#include <salhelper/thread.hxx>

#include <vcl/timer.hxx>
#include <vcl/svapp.hxx>

/// Avoid our timer tests just wedging the build if they fail.
class WatchDog : public osl::Thread
{
    sal_Int32 mnSeconds;
public:
    WatchDog(sal_Int32 nSeconds) :
        Thread(),
        mnSeconds( nSeconds )
    {
        create();
    }
    virtual void SAL_CALL run() SAL_OVERRIDE
    {
        TimeValue aWait;
        aWait.Seconds = mnSeconds;
        aWait.Nanosec = 1000000; // +1ms
        osl::Thread::wait( aWait );
        CPPUNIT_ASSERT_MESSAGE("watchdog triggered", false);
    }
};

static WatchDog aWatchDog( 10 /* 10 secs should be enough */);

class TimerTest : public test::BootstrapFixture
{
public:
    TimerTest() : BootstrapFixture(true, false) {}

#ifdef TEST_WATCHDOG
    void testWatchdog();
#endif
    void testDurations();
    void testAutoTimer();
    void testRecursiveTimer();
    void testSlowTimerCallback();

    CPPUNIT_TEST_SUITE(TimerTest);
#ifdef TEST_WATCHDOG
    CPPUNIT_TEST(testWatchdog);
#endif
    CPPUNIT_TEST(testDurations);
    CPPUNIT_TEST(testAutoTimer);
    CPPUNIT_TEST(testRecursiveTimer);
    CPPUNIT_TEST(testSlowTimerCallback);

    CPPUNIT_TEST_SUITE_END();
};

#ifdef TEST_WATCHDOG
void TimerTest::testWatchdog()
{
    // out-wait the watchdog.
    TimeValue aWait;
    aWait.Seconds = 12;
    aWait.Nanosec = 0;
    osl::Thread::wait( aWait );
}
#endif

// --------------------------------------------------------------------

class TimerBool : public Timer
{
    bool &mrBool;
public:
    TimerBool( sal_uLong nMS, bool &rBool ) :
        Timer(), mrBool( rBool )
    {
        SetTimeout( nMS );
        Start();
        mrBool = false;
    }
    virtual void Timeout() SAL_OVERRIDE
    {
        mrBool = true;
        Application::EndYield();
    }
};

void TimerTest::testDurations()
{
    static const sal_uLong aDurations[] = { 0, 1, 500, 1000 };
    for (size_t i = 0; i < SAL_N_ELEMENTS( aDurations ); i++)
    {
        bool bDone = false;
        TimerBool aTimer( aDurations[i], bDone );
        // coverity[infinite_loop]
        while( !bDone )
        {
            Application::Yield();
        }
    }
}

// --------------------------------------------------------------------

class AutoTimerCount : public AutoTimer
{
    sal_Int32 &mrCount;
public:
    AutoTimerCount( sal_uLong nMS, sal_Int32 &rCount ) :
        AutoTimer(), mrCount( rCount )
    {
        SetTimeout( nMS );
        Start();
        mrCount = 0;
    }
    virtual void Timeout() SAL_OVERRIDE
    {
        mrCount++;
    }
};

void TimerTest::testAutoTimer()
{
    sal_Int32 nCount = 0;
    AutoTimerCount aCount(1, nCount);
    // coverity[infinite_loop]
    while (nCount < 100) {
        Application::Yield();
    }
}

// --------------------------------------------------------------------

class YieldTimer : public Timer
{
public:
    YieldTimer( sal_uLong nMS ) : Timer()
    {
        SetTimeout( nMS );
        Start();
    }
    virtual void Timeout() SAL_OVERRIDE
    {
        for (int i = 0; i < 100; i++)
            Application::Yield();
    }
};

void TimerTest::testRecursiveTimer()
{
    sal_Int32 nCount = 0;
    YieldTimer aCount(5);
    AutoTimerCount aCountUp( 3, nCount );
    // coverity[infinite_loop]
    while (nCount < 20)
        Application::Yield();
}

// --------------------------------------------------------------------

class SlowCallbackTimer : public Timer
{
    bool &mbSlow;
public:
    SlowCallbackTimer( sal_uLong nMS, bool &bBeenSlow ) :
        Timer(), mbSlow( bBeenSlow )
    {
        SetTimeout( nMS );
        Start();
        mbSlow = false;
    }
    virtual void Timeout() SAL_OVERRIDE
    {
        TimeValue aWait;
        aWait.Seconds = 1;
        aWait.Nanosec = 0;
        osl::Thread::wait( aWait );
        mbSlow = true;
    }
};

void TimerTest::testSlowTimerCallback()
{
    bool bBeenSlow = false;
    sal_Int32 nCount = 0;
    AutoTimerCount aHighFreq(1, nCount);
    SlowCallbackTimer aSlow(250, bBeenSlow);
    // coverity[infinite_loop]
    while (!bBeenSlow)
        Application::Yield();
    // coverity[infinite_loop]
    while (nCount < 200)
        Application::Yield();
}

CPPUNIT_TEST_SUITE_REGISTRATION(TimerTest);

CPPUNIT_PLUGIN_IMPLEMENT();

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */