Branch libreoffice-5-0-4
[LibreOffice.git] / vcl / qa / cppunit / timer.cxx
blob144d626c5d906bde06a7c282bae737510a4ffcbe
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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/.
8 */
9 /*
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>
21 #include "svdata.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
29 sal_Int32 mnSeconds;
30 public:
31 WatchDog(sal_Int32 nSeconds) :
32 Thread(),
33 mnSeconds( nSeconds )
35 create();
37 virtual void SAL_CALL run() SAL_OVERRIDE
39 TimeValue aWait;
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
51 public:
52 TimerTest() : BootstrapFixture(true, false) {}
54 void testIdleMainloop();
55 void testIdle();
56 #ifdef TEST_WATCHDOG
57 void testWatchdog();
58 #endif
59 void testDurations();
60 void testAutoTimer();
61 void testRecursiveTimer();
62 void testSlowTimerCallback();
64 CPPUNIT_TEST_SUITE(TimerTest);
65 CPPUNIT_TEST(testIdle);
66 CPPUNIT_TEST(testIdleMainloop);
67 #ifdef TEST_WATCHDOG
68 CPPUNIT_TEST(testWatchdog);
69 #endif
70 CPPUNIT_TEST(testDurations);
71 CPPUNIT_TEST(testAutoTimer);
72 CPPUNIT_TEST(testRecursiveTimer);
73 CPPUNIT_TEST(testSlowTimerCallback);
75 CPPUNIT_TEST_SUITE_END();
78 #ifdef TEST_WATCHDOG
79 void TimerTest::testWatchdog()
81 // out-wait the watchdog.
82 TimeValue aWait;
83 aWait.Seconds = 12;
84 aWait.Nanosec = 0;
85 osl::Thread::wait( aWait );
87 #endif
89 // --------------------------------------------------------------------
91 class IdleBool : public Idle
93 bool &mrBool;
94 public:
95 IdleBool( bool &rBool ) :
96 Idle(), mrBool( rBool )
98 SetPriority( SchedulerPriority::LOWEST );
99 Start();
100 mrBool = false;
102 virtual void Invoke() SAL_OVERRIDE
104 mrBool = true;
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);
117 // tdf#91727
118 void TimerTest::testIdleMainloop()
120 bool bTriggered = false;
121 IdleBool aTest( bTriggered );
122 while (!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
139 bool &mrBool;
140 public:
141 TimerBool( sal_uLong nMS, bool &rBool ) :
142 Timer(), mrBool( rBool )
144 SetTimeout( nMS );
145 Start();
146 mrBool = false;
148 virtual void Invoke() SAL_OVERRIDE
150 mrBool = true;
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++)
160 bool bDone = false;
161 TimerBool aTimer( aDurations[i], bDone );
162 // coverity[loop_top] - Application::Yield allows the timer to fire and toggle bDone
163 while( !bDone )
165 Application::Yield();
170 // --------------------------------------------------------------------
172 class AutoTimerCount : public AutoTimer
174 sal_Int32 &mrCount;
175 public:
176 AutoTimerCount( sal_uLong nMS, sal_Int32 &rCount ) :
177 AutoTimer(), mrCount( rCount )
179 SetTimeout( nMS );
180 Start();
181 mrCount = 0;
183 virtual void Invoke() SAL_OVERRIDE
185 mrCount++;
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
202 public:
203 YieldTimer( sal_uLong nMS ) : Timer()
205 SetTimeout( nMS );
206 Start();
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
221 while (nCount < 20)
222 Application::Yield();
225 // --------------------------------------------------------------------
227 class SlowCallbackTimer : public Timer
229 bool &mbSlow;
230 public:
231 SlowCallbackTimer( sal_uLong nMS, bool &bBeenSlow ) :
232 Timer(), mbSlow( bBeenSlow )
234 SetTimeout( nMS );
235 Start();
236 mbSlow = false;
238 virtual void Invoke() SAL_OVERRIDE
240 TimeValue aWait;
241 aWait.Seconds = 1;
242 aWait.Nanosec = 0;
243 osl::Thread::wait( aWait );
244 mbSlow = true;
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
255 while (!bBeenSlow)
256 Application::Yield();
257 // coverity[loop_top] - Application::Yield allows the timer to fire and increment nCount
258 while (nCount < 200)
259 Application::Yield();
262 CPPUNIT_TEST_SUITE_REGISTRATION(TimerTest);
264 CPPUNIT_PLUGIN_IMPLEMENT();
266 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */