sl@0: // Copyright (c) 1995-2009 Nokia Corporation and/or its subsidiary(-ies). sl@0: // All rights reserved. sl@0: // This component and the accompanying materials are made available sl@0: // under the terms of the License "Eclipse Public License v1.0" sl@0: // which accompanies this distribution, and is available sl@0: // at the URL "http://www.eclipse.org/legal/epl-v10.html". sl@0: // sl@0: // Initial Contributors: sl@0: // Nokia Corporation - initial contribution. sl@0: // sl@0: // Contributors: sl@0: // sl@0: // Description: sl@0: // e32test\system\t_inact.cpp sl@0: // Test inactivity timers sl@0: // sl@0: // sl@0: sl@0: #include sl@0: sl@0: RTest test(_L("T_INACT")); sl@0: sl@0: class CTimer1 : public CTimer sl@0: { sl@0: public: sl@0: static CTimer1* NewL(); sl@0: CTimer1(); sl@0: void Start(); sl@0: virtual void RunL(); sl@0: }; sl@0: sl@0: class CTimer2 : public CTimer sl@0: { sl@0: public: sl@0: static CTimer2* NewL(); sl@0: CTimer2(); sl@0: void Start(); sl@0: virtual void RunL(); sl@0: }; sl@0: sl@0: class CTimer3 : public CTimer sl@0: { sl@0: public: sl@0: static CTimer3* NewL(); sl@0: CTimer3(); sl@0: void Start(); sl@0: virtual void RunL(); sl@0: public: sl@0: TInt iInactive; sl@0: }; sl@0: sl@0: class CRepeatedTimer : public CTimer sl@0: { sl@0: public: sl@0: static CRepeatedTimer* NewL(TInt aCount, TInt aPeriod); sl@0: CRepeatedTimer(); sl@0: void Start(); sl@0: virtual void RunL(); sl@0: public: sl@0: TInt iCount; sl@0: TInt iPeriod; sl@0: }; sl@0: sl@0: CActiveScheduler* ActiveSched; sl@0: CTimer1* Timer1; sl@0: CTimer2* Timer2; sl@0: CTimer3* Timer3; sl@0: CRepeatedTimer* RepTimer; sl@0: sl@0: CTimer1::CTimer1() sl@0: : CTimer(EPriorityStandard) sl@0: { sl@0: } sl@0: sl@0: CTimer1* CTimer1::NewL() sl@0: { sl@0: CTimer1* pT=new (ELeave) CTimer1; sl@0: CleanupStack::PushL(pT); sl@0: pT->ConstructL(); sl@0: CleanupStack::Pop(); sl@0: return pT; sl@0: } sl@0: sl@0: void CTimer1::Start() sl@0: { sl@0: Inactivity(7); sl@0: } sl@0: sl@0: void CTimer1::RunL() sl@0: { sl@0: test.Printf(_L("CTimer1 expired\n")); sl@0: Start(); sl@0: } sl@0: sl@0: CTimer2::CTimer2() sl@0: : CTimer(EPriorityStandard) sl@0: { sl@0: } sl@0: sl@0: CTimer2* CTimer2::NewL() sl@0: { sl@0: CTimer2* pT=new (ELeave) CTimer2; sl@0: CleanupStack::PushL(pT); sl@0: pT->ConstructL(); sl@0: CleanupStack::Pop(); sl@0: return pT; sl@0: } sl@0: sl@0: void CTimer2::Start() sl@0: { sl@0: Inactivity(13); sl@0: } sl@0: sl@0: void CTimer2::RunL() sl@0: { sl@0: test.Printf(_L("CTimer2 expired\n")); sl@0: Start(); sl@0: } sl@0: sl@0: CTimer3::CTimer3() sl@0: : CTimer(EPriorityStandard) sl@0: { sl@0: } sl@0: sl@0: CTimer3* CTimer3::NewL() sl@0: { sl@0: CTimer3* pT=new (ELeave) CTimer3; sl@0: CleanupStack::PushL(pT); sl@0: pT->ConstructL(); sl@0: CleanupStack::Pop(); sl@0: return pT; sl@0: } sl@0: sl@0: void CTimer3::Start() sl@0: { sl@0: iInactive=User::InactivityTime().Int(); sl@0: After(500000); sl@0: } sl@0: sl@0: void CTimer3::RunL() sl@0: { sl@0: TInt inactive=User::InactivityTime().Int(); sl@0: if (inactive!=iInactive) sl@0: { sl@0: iInactive=inactive; sl@0: test.Printf(_L("%d\n"),inactive); sl@0: } sl@0: After(500000); sl@0: } sl@0: sl@0: CRepeatedTimer::CRepeatedTimer() sl@0: : CTimer(EPriorityStandard) sl@0: { sl@0: } sl@0: sl@0: CRepeatedTimer* CRepeatedTimer::NewL(TInt aCount, TInt aPeriod) sl@0: { sl@0: CRepeatedTimer* pT=new (ELeave) CRepeatedTimer; sl@0: pT->iCount=aCount; sl@0: pT->iPeriod=aPeriod; sl@0: CleanupStack::PushL(pT); sl@0: pT->ConstructL(); sl@0: CleanupStack::Pop(); sl@0: return pT; sl@0: } sl@0: sl@0: void CRepeatedTimer::Start() sl@0: { sl@0: Inactivity(iPeriod); sl@0: } sl@0: sl@0: void CRepeatedTimer::RunL() sl@0: { sl@0: test.Printf(_L("RepeatTimer expired %d\n"),iCount); sl@0: if (--iCount) sl@0: Start(); sl@0: else sl@0: CActiveScheduler::Stop(); sl@0: } sl@0: sl@0: void InitialiseL() sl@0: { sl@0: ActiveSched=new (ELeave) CActiveScheduler; sl@0: Timer1=CTimer1::NewL(); sl@0: Timer2=CTimer2::NewL(); sl@0: Timer3=CTimer3::NewL(); sl@0: RepTimer=CRepeatedTimer::NewL(5,10); sl@0: CActiveScheduler::Install(ActiveSched); sl@0: CActiveScheduler::Add(Timer1); sl@0: CActiveScheduler::Add(Timer2); sl@0: CActiveScheduler::Add(Timer3); sl@0: CActiveScheduler::Add(RepTimer); sl@0: Timer1->Start(); sl@0: Timer2->Start(); sl@0: Timer3->Start(); sl@0: RepTimer->Start(); sl@0: } sl@0: sl@0: void TestNonPositiveTimeout() sl@0: { sl@0: TInt ret; sl@0: TRequestStatus x1,x2; sl@0: RTimer rt1,rt2; sl@0: sl@0: test.Next(_L("Test RTimer::Inactivity() with zero timeout")); sl@0: ret=rt1.CreateLocal(); sl@0: ret=rt2.CreateLocal(); sl@0: sl@0: rt1.Inactivity(x1, 2); sl@0: User::After(500000); sl@0: rt2.Inactivity(x2, 0); sl@0: User::ResetInactivityTime(); sl@0: sl@0: User::WaitForRequest(x1); sl@0: test(x1==KErrNone); sl@0: User::WaitForRequest(x2); sl@0: test(x2==KErrNone); sl@0: sl@0: test.Next(_L("Test RTimer::Inactivity() with negative timeout")); sl@0: rt1.Inactivity(x1, -1); sl@0: User::WaitForRequest(x1); sl@0: test(x1==KErrArgument); sl@0: } sl@0: sl@0: GLDEF_C TInt E32Main() sl@0: { sl@0: test.Title(); sl@0: __UHEAP_MARK; sl@0: test.Start(_L("Test RTimer::Inactivity")); sl@0: sl@0: TestNonPositiveTimeout(); sl@0: sl@0: RTimer t1; sl@0: RTimer t2; sl@0: TInt r=t1.CreateLocal(); sl@0: test(r==KErrNone); sl@0: r=t2.CreateLocal(); sl@0: test(r==KErrNone); sl@0: test.Printf(_L("\nPress a key...\n")); sl@0: test.Getch(); sl@0: TRequestStatus s; sl@0: t1.Inactivity(s,5); sl@0: TTime before; sl@0: before.UniversalTime(); sl@0: test.Printf(_L("Wait... ")); sl@0: User::WaitForRequest(s); sl@0: TTime after; sl@0: after.UniversalTime(); sl@0: TTimeIntervalMicroSeconds interval=after.MicroSecondsFrom(before); sl@0: TInt interval_int=I64INT(interval.Int64()); sl@0: test.Printf(_L("Timer expired after %d us\n"),interval_int); sl@0: test.Printf(_L("Press a key...\n")); sl@0: test.Getch(); sl@0: t1.Inactivity(s,5); sl@0: before.UniversalTime(); sl@0: test.Printf(_L("Test changing time")); sl@0: r=User::SetUTCTime(before+TTimeIntervalDays(1)); sl@0: test(r==KErrNone); sl@0: test(s==KRequestPending); // make sure time change doesn't trigger inactivity timers sl@0: r=User::SetUTCTime(before-TTimeIntervalDays(1)); sl@0: test(r==KErrNone); sl@0: test(s==KRequestPending); // make sure time change doesn't trigger inactivity timers sl@0: r=User::SetUTCTime(before); sl@0: test(r==KErrNone); sl@0: test(s==KRequestPending); // make sure time change doesn't trigger inactivity timers sl@0: sl@0: TTime secure; sl@0: if ((r = secure.UniversalTimeSecure()) == KErrNone) sl@0: r = User::SetUTCTimeSecure(secure-TTimeIntervalDays(1)); sl@0: if (r != KErrNone) sl@0: { sl@0: RDebug::Printf("WARNING: Secure clock change test skipped because secure time could not be changed!"); sl@0: } sl@0: else sl@0: { sl@0: User::SetUTCTimeSecure(secure+TTimeIntervalDays(1)); sl@0: User::SetUTCTimeSecure(secure); sl@0: test(s == KRequestPending); // make sure secure time change doesn't trigger inactivity timers sl@0: } sl@0: sl@0: User::WaitForRequest(s); sl@0: after.UniversalTime(); sl@0: interval=after.MicroSecondsFrom(before); sl@0: interval_int=I64INT(interval.Int64()); sl@0: test.Printf(_L("Timer expired after %d us\n"),interval_int); sl@0: test.Printf(_L("Press a key...\n")); sl@0: test.Getch(); sl@0: sl@0: TInt inactive=User::InactivityTime().Int(); sl@0: TRequestStatus s1; sl@0: TRequestStatus s2; sl@0: t1.Inactivity(s1, 10); sl@0: t2.Inactivity(s2, 15); sl@0: FOREVER sl@0: { sl@0: TInt new_inact=User::InactivityTime().Int(); sl@0: if (new_inact!=inactive) sl@0: { sl@0: inactive=new_inact; sl@0: test.Printf(_L("%d\n"),inactive); sl@0: } sl@0: if (s2!=KRequestPending) sl@0: { sl@0: User::WaitForRequest(s2); sl@0: test(s2.Int()==KErrNone); sl@0: test.Printf(_L("Timer 2 expired\n")); sl@0: break; sl@0: } sl@0: if (s1!=KRequestPending) sl@0: { sl@0: User::WaitForRequest(s1); sl@0: test(s1.Int()==KErrNone); sl@0: test.Printf(_L("Timer 1 expired\n")); sl@0: s1=KRequestPending; sl@0: } sl@0: } sl@0: sl@0: test.Next(_L("Test RTimer::Cancel()")); sl@0: test.Printf(_L("Press a key...\n")); sl@0: test.Getch(); sl@0: t1.Inactivity(s1, 300); sl@0: t1.Cancel(); sl@0: User::WaitForRequest(s1); sl@0: test(s1==KErrCancel); sl@0: sl@0: test.Next(_L("Test CTimer::Inactivity()")); sl@0: test.Printf(_L("Press a key...\n")); sl@0: test.Getch(); sl@0: sl@0: CTrapCleanup* pC=CTrapCleanup::New(); sl@0: test(pC!=NULL); sl@0: TRAP(r,InitialiseL()); sl@0: test(r==KErrNone); sl@0: sl@0: CActiveScheduler::Start(); sl@0: sl@0: Timer1->Cancel(); sl@0: Timer2->Cancel(); sl@0: Timer3->Cancel(); sl@0: RepTimer->Cancel(); sl@0: sl@0: delete Timer1; sl@0: delete Timer2; sl@0: delete Timer3; sl@0: delete RepTimer; sl@0: delete ActiveSched; sl@0: delete pC; sl@0: sl@0: test.Printf(_L("Press a key...\n")); sl@0: test.Getch(); sl@0: sl@0: test.End(); sl@0: __UHEAP_MARKEND; sl@0: return KErrNone; sl@0: }