sysstatemgmt/systemstateplugins/test/tintgsapolicy/src/tgsastatetrantoshutdown.cpp
branchRCL_3
changeset 22 8cb079868133
parent 0 4e1aa6a622a0
equal deleted inserted replaced
21:ccb4f6b3db21 22:8cb079868133
       
     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 /**
       
    17  @file
       
    18  @test
       
    19  @internalComponent - Internal Symbian test code
       
    20 */
       
    21 
       
    22 #include "gsatestapps.h"
       
    23 
       
    24 #include <ssm/ssmstates.hrh>
       
    25 #include <ssm/ssmsubstates.hrh>
       
    26 #include <ssm/ssmstate.h>
       
    27 #include <ssm/ssmstatetransition.h>
       
    28 #include <ssm/ssmstatemanager.h>
       
    29 #include <ssm/ssmstateawaresession.h>
       
    30 #include <ssm/ssmdomaindefs.h>
       
    31 
       
    32 #include "tgsastatetran_base.h"
       
    33 
       
    34 class CStateTranToShutdown : public CTGsaStateTransition
       
    35 	{
       
    36 public:
       
    37 	static CStateTranToShutdown* NewL();
       
    38 	~CStateTranToShutdown();
       
    39 	void StartTestL();
       
    40 
       
    41 private:
       
    42 	void ConstructL();
       
    43 	CStateTranToShutdown();
       
    44 
       
    45 public:
       
    46 	void DoTestStateTranToShutdownL();
       
    47 	};
       
    48 
       
    49 static TInt StopScheduler(TAny* aStateTranToShutdown)
       
    50  	{
       
    51  	CStateTranToShutdown* stateTran = reinterpret_cast<CStateTranToShutdown*>(aStateTranToShutdown);
       
    52  	TRAPD(err,stateTran->CallStopSchedulerL());
       
    53  	return err;
       
    54  	}
       
    55 
       
    56 static TInt StartTest(TAny* aStateTranToShutdown)
       
    57  	{
       
    58  	TInt testCompletionReason = KErrNone;
       
    59  	CStateTranToShutdown* stateTran = reinterpret_cast<CStateTranToShutdown*>(aStateTranToShutdown);
       
    60  	TBool stateTest = stateTran->DoStartStateTranTest();
       
    61  	if (stateTest)
       
    62 		TRAP(testCompletionReason, stateTran->DoTestStateTranToShutdownL());
       
    63 
       
    64  	return testCompletionReason;
       
    65  	}
       
    66 
       
    67 CStateTranToShutdown* CStateTranToShutdown::NewL()
       
    68 	{
       
    69 	CStateTranToShutdown* self = new(ELeave) CStateTranToShutdown();
       
    70 	self->ConstructL();
       
    71 	return self;
       
    72 	}
       
    73 
       
    74 void CStateTranToShutdown::ConstructL()
       
    75 	{
       
    76 	CTGsaStateTransition::ConstructL(KGsaTestStateTranResultFilePath);
       
    77 	}
       
    78 
       
    79 CStateTranToShutdown::~CStateTranToShutdown()
       
    80 	{
       
    81 	}
       
    82 
       
    83 CStateTranToShutdown::CStateTranToShutdown()
       
    84 	{
       
    85 	}
       
    86 
       
    87 // Tests the Shutdown state transitions under different scenarios
       
    88 void CStateTranToShutdown::DoTestStateTranToShutdownL()
       
    89 	{
       
    90 	// This async callback is used to stop the scheduler once the system goes into fail state.
       
    91 	iAsyncCallbackForStopScheduler =  new(ELeave) CAsyncCallBack(CActive::EPriorityIdle);
       
    92 	TCallBack stopSchedulerCallback(StopScheduler, this);
       
    93 	iAsyncCallbackForStopScheduler->Set(stopSchedulerCallback);
       
    94 
       
    95 	// call the callback function which keeps looping until the system state gets into fail state.
       
    96 	iAsyncCallbackForStopScheduler->CallBack();
       
    97 
       
    98 	RSsmStateManager stateMan;
       
    99 	TInt err = stateMan.Connect();
       
   100 	if (err == KErrNone)
       
   101 		RDebug::Print(_L("\nStateTran Test: RSsmStateManager sess Connect %d\n"), err);
       
   102 	else
       
   103 		{
       
   104 		RDebug::Print(_L("\nStateTran Test: Unable to connect to RSsmStateManager sess\n"));	
       
   105 		User::Leave(KTestAppFailure);
       
   106 		}
       
   107 
       
   108 	// Creates and open the result file for streaming
       
   109 	err = iFileWriteStream.Replace(iFs, KGsaTestStateTranToShutdownResult, EFileWrite);
       
   110 	RDebug::Print(_L("\nStateTran Test: iFileWriteStream open with %d\n"), err);
       
   111 
       
   112 
       
   113 	// Gets the Current system state before starting the test
       
   114 	CurrentSystemStateL();
       
   115 
       
   116 	//Scenario 1 - Request for state transition to shutdown state - invalid substate
       
   117 	// request to transit to the shutdown state will be completed with KErrNotSupported
       
   118 	RDebug::Print(_L("\nScenario 1 \n"));
       
   119 
       
   120 	TRequestStatus status_invalidsubstate;
       
   121 	TSsmState shutdownState_Invalid(ESsmShutdown, 150);
       
   122 	TSsmStateTransition stateTrans_Invalid(shutdownState_Invalid, 1);
       
   123 
       
   124 	stateMan.RequestStateTransition(stateTrans_Invalid, status_invalidsubstate);
       
   125 	RDebug::Print(_L("\n Start RequestStateTransition from Normal to shutdown state with invalid substate %d\n "), status_invalidsubstate.Int());
       
   126 	
       
   127 	User::WaitForRequest(status_invalidsubstate);
       
   128 	iFileWriteStream.WriteInt32L(status_invalidsubstate.Int());
       
   129 	if (status_invalidsubstate.Int() == KErrNotSupported)
       
   130 		{
       
   131 		CurrentSystemStateL();
       
   132 		RDebug::Print(_L("\n RequestStateTransition from Normal to shutdown state with invalid substate completes with %d Expected -5\n "), status_invalidsubstate.Int());
       
   133 		}
       
   134 	else
       
   135 		User::Leave(KTestAppFailure);
       
   136 
       
   137 	//Scenario 2 - Request for state transition to shutdown state - critical substate(Restart)
       
   138 	// Request for state transition to shutdown state - critical substate(Standby)
       
   139 	// first request to transit to the shutdown state critical substate(Restart)will be completed with KErrCancel
       
   140 	// second request to transit to the shutdown state critical substate(Standby)will be completed with KErrNone
       
   141 	RDebug::Print(_L("\nScenario 2 \n"));
       
   142 	TRequestStatus status_shutdowncritsubstate;
       
   143 	TSsmState shutdownState_Critical(ESsmShutdown, ESsmShutdownSubStateCritical);
       
   144 	TSsmStateTransition stateTrans_Shutdowncritical_Restart(shutdownState_Critical, 3);
       
   145 
       
   146 	stateMan.RequestStateTransition(stateTrans_Shutdowncritical_Restart, status_shutdowncritsubstate);
       
   147 	RDebug::Print(_L("\n RequestStateTransition from Normal to shutdown state critical substate Restart%d\n "), status_shutdowncritsubstate.Int());
       
   148 
       
   149 	TRequestStatus status_shutdowncritsubstate1;
       
   150 	TSsmStateTransition stateTrans_Shutdowncritical_Standby(shutdownState_Critical, 1);
       
   151 
       
   152 	stateMan.RequestStateTransition(stateTrans_Shutdowncritical_Standby, status_shutdowncritsubstate1);
       
   153 	RDebug::Print(_L("\n RequestStateTransition from Normal to shutdown state critical substate Standby %d\n "), status_shutdowncritsubstate.Int());
       
   154 
       
   155 	User::WaitForRequest(status_shutdowncritsubstate1);
       
   156 	iFileWriteStream.WriteInt32L(status_shutdowncritsubstate1.Int());
       
   157 	if (status_shutdowncritsubstate1.Int() == KErrNone)
       
   158 		{
       
   159 		RDebug::Print(_L("\n RequestStateTransition from Normal to shutdown state critical substate completed Standby %d Expected 0\n "), status_shutdowncritsubstate1.Int());
       
   160 		CurrentSystemStateL();
       
   161 		}
       
   162 	else
       
   163 		User::Leave(KTestAppFailure);
       
   164 
       
   165 	User::WaitForRequest(status_shutdowncritsubstate);
       
   166 	iFileWriteStream.WriteInt32L(status_shutdowncritsubstate.Int());
       
   167 	if (status_shutdowncritsubstate.Int() == KErrCancel)
       
   168 		{
       
   169 		CurrentSystemStateL();
       
   170 		RDebug::Print(_L("\n RequestStateTransition from Normal to shutdown state critical substate completed Restart %d Expected -3\n "), status_shutdowncritsubstate.Int());
       
   171 		}
       
   172 	else
       
   173 		User::Leave(KTestAppFailure);
       
   174 
       
   175 	}
       
   176 
       
   177 void CStateTranToShutdown::StartTestL()
       
   178 	{
       
   179 	CActiveScheduler* sched=new(ELeave) CActiveScheduler;
       
   180 	CleanupStack::PushL(sched);
       
   181 	CActiveScheduler::Install(sched);
       
   182 
       
   183 	// Notifier used to get the notification when system gets to fail state
       
   184 	CSsmStateAwareSession* notif_for_failstate = CSsmStateAwareSession::NewLC(KSM2UiServicesDomain3);
       
   185 	notif_for_failstate->AddSubscriberL(*this);
       
   186 
       
   187 	// This async callback is used to start the state transition test once system enters to normal state.
       
   188 	iAsyncCallbackForStartTest =  new(ELeave) CAsyncCallBack(CActive::EPriorityIdle);
       
   189 	TCallBack startTestCallback(StartTest, this);
       
   190 	iAsyncCallbackForStartTest->Set(startTestCallback);
       
   191 
       
   192 	// call the callback function which keeps looping until the system state gets into normal state.
       
   193 	iAsyncCallbackForStartTest->CallBack();
       
   194 	
       
   195 	RProcess::Rendezvous(KErrNone);
       
   196 	sched->Start();
       
   197 
       
   198 	CleanupStack::PopAndDestroy(2);
       
   199 	}
       
   200 
       
   201 TInt E32Main()
       
   202 	{
       
   203 	__UHEAP_MARK;
       
   204 
       
   205 	CTrapCleanup* cleanup=CTrapCleanup::New();
       
   206 	TInt r=KErrNoMemory;
       
   207 	TInt testCompletionReason = KErrNone;
       
   208 	if (cleanup)
       
   209 		{
       
   210 		__UHEAP_MARK;
       
   211 		CStateTranToShutdown* stateTran = NULL;
       
   212 		TRAP(r, stateTran = CStateTranToShutdown::NewL());
       
   213 		if (r == KErrNone)
       
   214 			{
       
   215 			TRAP(testCompletionReason, stateTran->StartTestL());
       
   216 			// testCompletionReason is the reason with which the test case completes.
       
   217 			TRAP(r, stateTran->CommitTestResultsL(testCompletionReason));
       
   218 			delete stateTran;
       
   219 			}
       
   220 		delete cleanup;
       
   221 		__UHEAP_MARKEND;
       
   222 
       
   223 		if (r != KErrNone)
       
   224 			{
       
   225 			User::Panic(_L("tgsastatetrantoshutdownpanic"), r);
       
   226 			}
       
   227 		}
       
   228 
       
   229 	__UHEAP_MARKEND;
       
   230 	return r;
       
   231 	}
       
   232 
       
   233 
       
   234