1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
10 * Timers are evil beasties across platforms ...
13 #include <test/bootstrapfixture.hxx>
15 #include <osl/thread.hxx>
16 #include <salhelper/thread.hxx>
18 #include <vcl/timer.hxx>
19 #include <vcl/idle.hxx>
20 #include <vcl/svapp.hxx>
22 #include "salinst.hxx"
24 // #define TEST_WATCHDOG
26 /// Avoid our timer tests just wedging the build if they fail.
27 class WatchDog
: public osl::Thread
31 WatchDog(sal_Int32 nSeconds
) :
37 virtual void SAL_CALL
run() SAL_OVERRIDE
40 aWait
.Seconds
= mnSeconds
;
41 aWait
.Nanosec
= 1000000; // +1ms
42 osl::Thread::wait( aWait
);
43 CPPUNIT_ASSERT_MESSAGE("watchdog triggered", false);
47 static WatchDog
aWatchDog( 10 /* 10 secs should be enough */);
49 class TimerTest
: public test::BootstrapFixture
52 TimerTest() : BootstrapFixture(true, false) {}
54 void testIdleMainloop();
61 void testRecursiveTimer();
62 void testSlowTimerCallback();
64 CPPUNIT_TEST_SUITE(TimerTest
);
65 CPPUNIT_TEST(testIdle
);
66 CPPUNIT_TEST(testIdleMainloop
);
68 CPPUNIT_TEST(testWatchdog
);
70 CPPUNIT_TEST(testDurations
);
71 CPPUNIT_TEST(testAutoTimer
);
72 CPPUNIT_TEST(testRecursiveTimer
);
73 CPPUNIT_TEST(testSlowTimerCallback
);
75 CPPUNIT_TEST_SUITE_END();
79 void TimerTest::testWatchdog()
81 // out-wait the watchdog.
85 osl::Thread::wait( aWait
);
89 // --------------------------------------------------------------------
91 class IdleBool
: public Idle
95 IdleBool( bool &rBool
) :
96 Idle(), mrBool( rBool
)
98 SetPriority( SchedulerPriority::LOWEST
);
102 virtual void Invoke() SAL_OVERRIDE
105 Application::EndYield();
109 void TimerTest::testIdle()
111 bool bTriggered
= false;
112 IdleBool
aTest( bTriggered
);
113 Scheduler::ProcessTaskScheduling(false);
114 CPPUNIT_ASSERT_MESSAGE("idle triggered", bTriggered
);
118 void TimerTest::testIdleMainloop()
120 bool bTriggered
= false;
121 IdleBool
aTest( bTriggered
);
124 ImplSVData
* pSVData
= ImplGetSVData();
126 // can't test this via Application::Yield since this
127 // also processes all tasks directly via the scheduler.
128 pSVData
->maAppData
.mnDispatchLevel
++;
129 pSVData
->mpDefInst
->Yield( true, false );
130 pSVData
->maAppData
.mnDispatchLevel
--;
132 CPPUNIT_ASSERT_MESSAGE("mainloop idle triggered", bTriggered
);
135 // --------------------------------------------------------------------
137 class TimerBool
: public Timer
141 TimerBool( sal_uLong nMS
, bool &rBool
) :
142 Timer(), mrBool( rBool
)
148 virtual void Invoke() SAL_OVERRIDE
151 Application::EndYield();
155 void TimerTest::testDurations()
157 static const sal_uLong aDurations
[] = { 0, 1, 500, 1000 };
158 for (size_t i
= 0; i
< SAL_N_ELEMENTS( aDurations
); i
++)
161 TimerBool
aTimer( aDurations
[i
], bDone
);
162 // coverity[loop_top] - Application::Yield allows the timer to fire and toggle bDone
165 Application::Yield();
170 // --------------------------------------------------------------------
172 class AutoTimerCount
: public AutoTimer
176 AutoTimerCount( sal_uLong nMS
, sal_Int32
&rCount
) :
177 AutoTimer(), mrCount( rCount
)
183 virtual void Invoke() SAL_OVERRIDE
189 void TimerTest::testAutoTimer()
191 sal_Int32 nCount
= 0;
192 AutoTimerCount
aCount(1, nCount
);
193 while (nCount
< 100) {
194 Application::Yield();
198 // --------------------------------------------------------------------
200 class YieldTimer
: public Timer
203 YieldTimer( sal_uLong nMS
) : Timer()
208 virtual void Invoke() SAL_OVERRIDE
210 for (int i
= 0; i
< 100; i
++)
211 Application::Yield();
215 void TimerTest::testRecursiveTimer()
217 sal_Int32 nCount
= 0;
218 YieldTimer
aCount(5);
219 AutoTimerCount
aCountUp( 3, nCount
);
220 // coverity[loop_top] - Application::Yield allows the timer to fire and increment nCount
222 Application::Yield();
225 // --------------------------------------------------------------------
227 class SlowCallbackTimer
: public Timer
231 SlowCallbackTimer( sal_uLong nMS
, bool &bBeenSlow
) :
232 Timer(), mbSlow( bBeenSlow
)
238 virtual void Invoke() SAL_OVERRIDE
243 osl::Thread::wait( aWait
);
248 void TimerTest::testSlowTimerCallback()
250 bool bBeenSlow
= false;
251 sal_Int32 nCount
= 0;
252 AutoTimerCount
aHighFreq(1, nCount
);
253 SlowCallbackTimer
aSlow(250, bBeenSlow
);
254 // coverity[loop_top] - Application::Yield allows the timer to fire and toggle bBeenSlow
256 Application::Yield();
257 // coverity[loop_top] - Application::Yield allows the timer to fire and increment nCount
259 Application::Yield();
262 CPPUNIT_TEST_SUITE_REGISTRATION(TimerTest
);
264 CPPUNIT_PLUGIN_IMPLEMENT();
266 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */