resourcemgmt/powerandmemorynotificationservice/tsrc/t_shutdownserver.cpp
changeset 0 4e1aa6a622a0
child 21 ccb4f6b3db21
equal deleted inserted replaced
-1:000000000000 0:4e1aa6a622a0
       
     1 // Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 #include <e32test.h>        //RTest
       
    17 #include <e32svr.h>         //RDebug
       
    18 #include <savenotf.h>       //RSaveSession, MSaveObserver, CSaveNotifier
       
    19 #ifndef SYMBIAN_ENABLE_SPLIT_HEADERS
       
    20 #include <shutdownsrv.h>    //CServShutdownServer
       
    21 #else //SYMBIAN_ENABLE_SPLIT_HEADERS
       
    22 #include "shutdownsess.h" //CServShutdownServer
       
    23 #endif //SYMBIAN_ENABLE_SPLIT_HEADERS
       
    24 #include <savepriv.h>
       
    25 
       
    26 static RTest TheTest(_L("T_ShutdownServer"));
       
    27 const TInt KOneSec = 1000000;
       
    28 
       
    29 
       
    30 // The timeout of 1 second which is shorter than the one defined at server side(1.5 seconds).
       
    31 // It will give a pause to close all requests in thread
       
    32 const TUint32 KShtdwnTimeoutShorter =1000000;
       
    33 
       
    34 //
       
    35 //
       
    36 //Test macroses and functions
       
    37 static void Check(TInt aValue, TInt aLine)
       
    38 	{
       
    39 	if(!aValue)
       
    40 		{
       
    41 		TheTest(EFalse, aLine);
       
    42 		}
       
    43 	}
       
    44 static void Check(TInt aValue, TInt aExpected, TInt aLine)
       
    45 	{
       
    46 	if(aValue != aExpected)
       
    47 		{
       
    48 		RDebug::Print(_L("*** Expected error: %d, got: %d\r\n"), aExpected, aValue);
       
    49 		TheTest(EFalse, aLine);
       
    50 		}
       
    51 	}
       
    52 #define TEST(arg) ::Check((arg), __LINE__)
       
    53 #define TEST2(aValue, aExpected) ::Check(aValue, aExpected, __LINE__)
       
    54 
       
    55 //
       
    56 
       
    57 static void Leave(TInt aLine, TInt aError)
       
    58 	{
       
    59 	RDebug::Print(_L("*** Leave. Error: %d, line: %d\r\n"), aError, aLine);
       
    60 	User::Leave(aError);
       
    61 	}
       
    62 
       
    63 static void LeaveIfError(TInt aLine, TInt aError)
       
    64 	{
       
    65 	if(aError < KErrNone)
       
    66 		{
       
    67 		::Leave(aLine, aError);
       
    68 		}
       
    69 	}
       
    70 
       
    71 static void LeaveIfNull(TInt aLine, TAny* aPtr)
       
    72 	{
       
    73 	if(!aPtr)
       
    74 		{
       
    75 		::Leave(aLine, KErrNoMemory);
       
    76 		}
       
    77 	}
       
    78 
       
    79 #define __LEAVE(err) ::Leave(__LINE__, err)
       
    80 #define __LEAVE_IF_ERROR(err) ::LeaveIfError(__LINE__, err)
       
    81 #define __LEAVE_IF_NULL(ptr) ::LeaveIfNull(__LINE__, ptr)
       
    82 
       
    83 //
       
    84 //
       
    85 //This test class is used to receive a powerdown notification from the shutdown server.
       
    86 class CPowerdownClient : public CActive, public MSaveObserver
       
    87     {
       
    88 public:
       
    89     static CPowerdownClient* NewLC();
       
    90     virtual ~CPowerdownClient();
       
    91     virtual void SaveL(MSaveObserver::TSaveType aSaveType);
       
    92 
       
    93 protected:
       
    94 	virtual void DoCancel();
       
    95 	virtual void RunL();
       
    96 
       
    97 private:
       
    98     CPowerdownClient();
       
    99     void ConstructL();
       
   100 
       
   101 private:
       
   102     RTimer iTimer;
       
   103     CSaveNotifier* iSaveNotifier;
       
   104     };
       
   105 
       
   106 CPowerdownClient* CPowerdownClient::NewLC()
       
   107     {
       
   108     CPowerdownClient* self = new CPowerdownClient;
       
   109     __LEAVE_IF_NULL(self);
       
   110     CleanupStack::PushL(self);
       
   111     self->ConstructL();
       
   112     return self;
       
   113     }
       
   114 
       
   115 CPowerdownClient::~CPowerdownClient()
       
   116     {
       
   117     Cancel();
       
   118     iTimer.Close();
       
   119     delete iSaveNotifier;
       
   120     }
       
   121 
       
   122 //MSaveNotifier::SaveL() implementation. Called when powerdown event occurs.
       
   123 void CPowerdownClient::SaveL(MSaveObserver::TSaveType)
       
   124     {
       
   125     iSaveNotifier->DelayRequeue();
       
   126     iTimer.After(iStatus, KOneSec);
       
   127     TEST2(iStatus.Int(), KRequestPending);
       
   128     SetActive();
       
   129     }
       
   130 
       
   131 void CPowerdownClient::DoCancel()
       
   132     {
       
   133     iTimer.Cancel();
       
   134     }
       
   135 
       
   136 //Processes timer events.
       
   137 void CPowerdownClient::RunL()
       
   138     {
       
   139     static TBool powerOffCalled = EFalse;
       
   140     if(!powerOffCalled)
       
   141         {
       
   142         TEST2(iSaveNotifier->SwitchOff(MSaveObserver::ESaveAll, ETrue), KErrNone);
       
   143         powerOffCalled = ETrue;
       
   144         }
       
   145     else
       
   146         {
       
   147         TBool powerOff = ETrue;
       
   148         iSaveNotifier->ServerPowerState(powerOff);
       
   149 		TEST(powerOff);
       
   150         iSaveNotifier->HandleError(KErrGeneral);
       
   151         User::After(KOneSec);
       
   152         CActiveScheduler::Stop();
       
   153         }
       
   154     }
       
   155 
       
   156 //Adds the CPowerdownClient object to the active scheduler.
       
   157 CPowerdownClient::CPowerdownClient() :
       
   158     CActive(CActive::EPriorityStandard)
       
   159     {
       
   160 	CActiveScheduler::Add(this);
       
   161     }
       
   162 
       
   163 //Constructs CPowerdownClient object and generates a timer event, processed 1 second later.
       
   164 void CPowerdownClient::ConstructL()
       
   165     {
       
   166     iSaveNotifier = CSaveNotifier::NewL(*this);
       
   167        __LEAVE_IF_ERROR(iTimer.CreateLocal());
       
   168     iTimer.After(iStatus, KOneSec);
       
   169     TEST2(iStatus.Int(), KRequestPending);
       
   170     SetActive();
       
   171     }
       
   172 
       
   173 /**
       
   174  This function is creating the test shutdown server and starting its scheduler.
       
   175  */
       
   176 static void RunAdaptionSrvL(CServShutdownServer* aServer)
       
   177 	{
       
   178 	CActiveScheduler* sched = new(ELeave) CActiveScheduler();
       
   179     CleanupStack::PushL( sched );
       
   180     CActiveScheduler::Install( sched );
       
   181    
       
   182     aServer->ConstructL();
       
   183     RThread::Rendezvous( KErrNone );
       
   184     
       
   185     CActiveScheduler::Start();
       
   186     delete aServer;
       
   187     CleanupStack::PopAndDestroy( sched );
       
   188 	}
       
   189 
       
   190 
       
   191 /**
       
   192  This function is called when the StartShutDownSrv's thread is resumed.
       
   193  */
       
   194 static TInt AdaptionSrvInitFunction( TAny* aServer )
       
   195 	{
       
   196     CTrapCleanup* cleanup=CTrapCleanup::New();
       
   197 	TInt err = KErrNoMemory;
       
   198 	if (cleanup)
       
   199 		{
       
   200 	    TRAP( err, RunAdaptionSrvL(static_cast<CServShutdownServer*>(aServer)));
       
   201 	    delete cleanup;
       
   202 		}
       
   203     return err;
       
   204 	}
       
   205 //
       
   206 // Functions to launch the servers (these will run in the main thread)
       
   207 //
       
   208 
       
   209 /**
       
   210 Function to launch the shutdown server in a new thread.
       
   211  */
       
   212 static TInt StartShutDownSrv(CServShutdownServer* aServer,RThread* aServerThread )
       
   213 	{
       
   214 	//RThread server;
       
   215 	TInt err = aServerThread->Create( __TEST_SHUTDOWN_SERVER_NAME, &AdaptionSrvInitFunction, 
       
   216 								KDefaultStackSize, NULL, &(*aServer ));
       
   217 
       
   218 	if( KErrNone == err )
       
   219 		{
       
   220 		TRequestStatus trs;
       
   221 		aServerThread->Rendezvous( trs );
       
   222 		aServerThread->Resume();
       
   223 		
       
   224 		User::WaitForRequest( trs );	
       
   225 		
       
   226 		//We can't use the 'exit reason' if the server panicked as this is the 
       
   227 		//panic 'reason' and may be '0' which cannot be distinguished from KErrNone
       
   228 		err = (aServerThread->ExitType()==EExitPanic) ? KErrGeneral : trs.Int();
       
   229 		}
       
   230 	return err;
       
   231 	}
       
   232 
       
   233 /**
       
   234 @SYMTestCaseID 			SYSLIB-PWRLCLI-UT-3693
       
   235 @SYMTestCaseDesc 		Test the ClientArrayLC functionality of shutdownserver.
       
   236 @SYMPREQ				PREQ1089
       
   237 @SYMREQ					REQ6883
       
   238 @SYMTestStatus			Implemented
       
   239 @SYMTestPriority		Critical
       
   240 @SYMTestPurpose			To test the existing default behaviour of shutdownserver 
       
   241 @SYMTestActions			1.Create a test shutdown server in a thread.
       
   242 						2.Test the power down state in shutdown server
       
   243 						3.Register with shutdown server for powerdown notification.
       
   244 						4.Test that registered clients with the shutdown server.
       
   245 						5.Test that registered clients does not have any pending requests.
       
   246 						
       
   247 @SYMTestExpectedResults	1.Registered clients with shutdown server is 1
       
   248 						2.Registered clients will not have any pending requests.
       
   249 */	
       
   250 void TestShutDownSrvObserverFunctions()
       
   251     {
       
   252  	CActiveScheduler* scheduler = new CActiveScheduler;
       
   253     __LEAVE_IF_NULL(scheduler);
       
   254     CleanupStack::PushL(scheduler);
       
   255 	CActiveScheduler::Install(scheduler);
       
   256 	//Create a test server in a different thread.
       
   257 	RThread serverThread;
       
   258     CServShutdownServer* server = CServShutdownServer::NewL();
       
   259     StartShutDownSrv(server,&serverThread);
       
   260 	//Power down state will be 0. By default power state is set to 0.
       
   261 	TBool allSessionsHavePendingRequest;
       
   262 	TBool powerOff;
       
   263 	server->GetShutdownState(powerOff,allSessionsHavePendingRequest);
       
   264 	TEST(!powerOff);
       
   265 	//Register to shutdown server for power down notification
       
   266     CPowerdownClient* powerdownClient = CPowerdownClient::NewLC();
       
   267 	//CPowerdownClient will test the power down states of shutdown server and completes the request.
       
   268 	CActiveScheduler::Start();
       
   269 	
       
   270 	//Test that shutdown server has only one registered client.
       
   271 	CArrayFix<TThreadId>* array = server->ClientArrayLC();
       
   272 	TEST(array->Count() == 1);
       
   273 	//Test that registered client has no pending requests.
       
   274 	TBool res = server->IsClientHung(array->At(0));	
       
   275 	TEST(!res);
       
   276 		
       
   277 	serverThread.Kill(KErrNone);
       
   278 	serverThread.Close();
       
   279 	User::After(KShtdwnTimeoutShorter);				
       
   280 	CleanupStack::PopAndDestroy(array);	
       
   281     CleanupStack::PopAndDestroy(powerdownClient);
       
   282     CleanupStack::PopAndDestroy(scheduler);
       
   283     }
       
   284 
       
   285 
       
   286 //
       
   287 //
       
   288 //Tests
       
   289 static void DoRunL()
       
   290     {
       
   291     TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-PWRLCLI-UT-3693 Test the ClientArrayLC functionality of shutdownserver."));
       
   292     ::TestShutDownSrvObserverFunctions();
       
   293     }
       
   294 
       
   295 TInt E32Main()
       
   296 	{
       
   297 	CTrapCleanup* tc = CTrapCleanup::New();
       
   298     TEST(tc != NULL);
       
   299 
       
   300 	TheTest.Start(_L("Power shutdown tests"));
       
   301 
       
   302     TRAPD(err, ::DoRunL());
       
   303 	TEST2(err, KErrNone);
       
   304 
       
   305 	TheTest.End();
       
   306 	TheTest.Close();
       
   307 	
       
   308 	delete tc;
       
   309 
       
   310 	User::Heap().Check();
       
   311 	return KErrNone;
       
   312 	}