testexecfw/useremul/src/RandomTest.cpp
changeset 0 3e07fef1e154
equal deleted inserted replaced
-1:000000000000 0:3e07fef1e154
       
     1 /*------------------------------------------------------------------
       
     2  -
       
     3  * Software Name : UserEmulator
       
     4  * Version       : v4.2.1309
       
     5  * 
       
     6  * Copyright (c) 2009 France Telecom. All rights reserved.
       
     7  * This software is distributed under the License 
       
     8  * "Eclipse Public License - v 1.0" the text of which is available
       
     9  * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
    10  *
       
    11  * Initial Contributors:
       
    12  * France Telecom 
       
    13  *
       
    14  * Contributors:
       
    15  *------------------------------------------------------------------
       
    16  -
       
    17  * File Name: RandomTest.cpp
       
    18  * 
       
    19  * Created: 13/08/2009
       
    20  * Author(s): Marcell Kiss, Reshma Sandeep Das
       
    21  *   
       
    22  * Description:
       
    23  * Random test class
       
    24  *------------------------------------------------------------------
       
    25  -
       
    26  *
       
    27  */
       
    28 
       
    29 // System Includes
       
    30 #include <e32math.h>
       
    31 #include <APGTASK.H>
       
    32 #include <APGWGNAM.H>
       
    33 #include <aknnotewrappers.h>
       
    34 // User Includes
       
    35 #include "RandomTest.h"
       
    36 
       
    37 // Constants
       
    38 #define KOkButton 167
       
    39 #define KBackspace 1
       
    40 
       
    41 #define TESTSTEPSNUMBER 40
       
    42 #define COUNTERFOROK 200
       
    43 
       
    44 // Uid of the Aactive Idle framework (3.1 devices)
       
    45 const TUid KUidStandBy1 = { 0x101fd64c };
       
    46 // Uid of the Aactive Idle framework (3.2 devices)
       
    47 const TUid KUidStandBy = { 0x102750f0 };
       
    48 // Uid of the Active Idle framework (5.0 devices)
       
    49 const TUid KUidStandby50 = {0x20017CD9};
       
    50 
       
    51 enum {ETap=0,EDoubleTap,EFlick,EDrag,EKeys,EKeys1,EKeys2,EKeys3};
       
    52 const TInt KMaxChance = 200;
       
    53 const TInt KShortFlickDist = 40;
       
    54 const TInt KEventTypeMax = 8;
       
    55 const TInt KScancodeMax = 65535;
       
    56 const TInt KLogAppAtStep = 100;
       
    57 
       
    58 const TInt KLSK = 164;
       
    59 const TInt KRSK = 165;
       
    60 
       
    61 // ======== MEMBER FUNCTIONS ========
       
    62 
       
    63 CRandomTest::CRandomTest(MKeyObserver& aObserver,RWsSession& aRWsSession,CSettings& aSettings,RApaLsSession& aSession,CLogger& aLogger)
       
    64 :CActive(EPriorityNormal),iObserver(aObserver),iRWsSession(&aRWsSession),iSettings(aSettings),iSession(aSession),iLogger(aLogger)
       
    65 {
       
    66 }
       
    67  
       
    68 CRandomTest::~CRandomTest()
       
    69 {	
       
    70     iSlotUIDs.Close();
       
    71 	Cancel();
       
    72 	iTimer.Close();
       
    73 }
       
    74  
       
    75 CRandomTest* CRandomTest::NewL(MKeyObserver& aObserver,RWsSession& aRWsSession,CSettings& aSettings,RApaLsSession& aSession,CLogger& aLogger)
       
    76 {
       
    77 	CRandomTest* me = CRandomTest::NewLC(aObserver,aRWsSession,aSettings,aSession,aLogger);
       
    78 	CleanupStack::Pop(me);
       
    79     return me;
       
    80 }
       
    81 
       
    82 CRandomTest* CRandomTest::NewLC(MKeyObserver& aObserver,RWsSession& aRWsSession,CSettings& aSettings,RApaLsSession& aSession,CLogger& aLogger)
       
    83 	{
       
    84 	CRandomTest* me = new (ELeave) CRandomTest(aObserver,aRWsSession,aSettings,aSession,aLogger);
       
    85 	CleanupStack::PushL(me);
       
    86 	me->ConstructL();
       
    87 	return me;
       
    88 	}
       
    89 // ----------------------------------------------------------
       
    90 // Second phase constructor
       
    91 // ----------------------------------------------------------
       
    92 // 
       
    93 void CRandomTest::ConstructL(void)
       
    94 {
       
    95 	CActiveScheduler::Add(this);
       
    96 	iTimer.CreateLocal();
       
    97 }
       
    98  
       
    99 // ----------------------------------------------------------
       
   100 // Starts random test
       
   101 // ----------------------------------------------------------
       
   102 //
       
   103 void CRandomTest::StartRandomTestL()
       
   104 {
       
   105 	Cancel();
       
   106 	iAppTestCounter = 0;
       
   107 	iAppNameLoggingCounter = 0;
       
   108 	iCounterForOkButton = 0;
       
   109 	iCantStartAppToTest = ETrue;
       
   110 	
       
   111 	if(iSettings.iRandomTestAppIdStatus)
       
   112 		{
       
   113 		_LIT(KCantStartApp,"Can't start app %d.\n");
       
   114 		TUid uid;
       
   115 		
       
   116 		TThreadId threadId;
       
   117 		TBuf<KBuffer128> buf;
       
   118 		TBuf<KBuffer12> CurrentAppUid;
       
   119 		TBuf<KBuffer256> RandomTestAppIDs;
       
   120 		RandomTestAppIDs.Copy(iSettings.iRandomTestAppID);
       
   121 		TUint32 k;
       
   122 		TInt res;
       
   123 		TInt pos=0;
       
   124 		TInt i=1;
       
   125 		_LIT(KCommaZero,",0");
       
   126 		
       
   127 		// 'Close' string by these chars
       
   128 		RandomTestAppIDs.Append(KCommaZero);
       
   129 	
       
   130 	    while((pos=RandomTestAppIDs.Locate(','))!=KErrNotFound)
       
   131 	       {
       
   132 	       k=0;
       
   133 	       if(RandomTestAppIDs.Left(pos).Length()<=10)
       
   134 	           CurrentAppUid = RandomTestAppIDs.Left(pos);
       
   135 	       else
       
   136 	           CurrentAppUid.Copy(KEMPTYTEXT);
       
   137 	           
       
   138 	       CurrentAppUid.Trim();
       
   139 	       RandomTestAppIDs.Copy(RandomTestAppIDs.Right(RandomTestAppIDs.Length()-pos-1));
       
   140 	       
       
   141 		
       
   142 		    // Converts decimal or hexa uid to integer
       
   143             if(CurrentAppUid.Left(2).Compare(KHEX)==0)
       
   144                 {
       
   145                 TLex id1(CurrentAppUid.Right(CurrentAppUid.Length()-2));
       
   146                 res = id1.Val(k,EHex);
       
   147                 }
       
   148             else
       
   149                 {
       
   150                 TLex id1(CurrentAppUid);
       
   151                 res = id1.Val(k,EDecimal);
       
   152                 }
       
   153             
       
   154             TInt doc_res=0;
       
   155             // Starts test application
       
   156             if(res==KErrNone)
       
   157                 {
       
   158                 uid.iUid = k;
       
   159                 doc_res=iSession.StartDocument(KN, uid ,threadId);
       
   160                 User::After(KWait01);
       
   161                 }
       
   162             if(doc_res==KErrNone && res==KErrNone)
       
   163                 {
       
   164                 iSlotUIDs.Append(uid);
       
   165                 }
       
   166             else
       
   167                 {
       
   168                 buf.Format(KCantStartApp, i);
       
   169                 iLogger.WriteLogL(buf,EFalse);
       
   170                 }
       
   171                 
       
   172            i++;
       
   173            }
       
   174                 
       
   175 		User::After(KWait02);
       
   176 		}		
       
   177 	// Starts timer
       
   178 	iTimer.After(iStatus,KWait02);
       
   179 	SetActive();
       
   180 }
       
   181 // --------------------------------------------
       
   182 // Stops random test (The active object)
       
   183 // --------------------------------------------
       
   184 //
       
   185 void CRandomTest::CancelRandomTest()
       
   186     {
       
   187     Cancel();
       
   188     }
       
   189 
       
   190 // --------------------------------------------
       
   191 // Restarts random test (The active object)
       
   192 // --------------------------------------------
       
   193 //
       
   194 void CRandomTest::RestartRandomTest()
       
   195     {
       
   196     iTimer.After(iStatus,KWait02);
       
   197     SetActive();
       
   198     }
       
   199     
       
   200 // --------------------------------------------
       
   201 // Stops random test
       
   202 // --------------------------------------------
       
   203 //
       
   204 void CRandomTest::StopRandomTest()
       
   205 	{
       
   206 	if(iSettings.iRandomTestAppIdStatus)
       
   207 		{
       
   208 		// Stops test apps if uids were valid and apps are running
       
   209 		for(TInt t=0;t<iSlotUIDs.Count();t++)
       
   210 		    {
       
   211             if(iSlotUIDs[t].iUid!=0)
       
   212                 {
       
   213                 TApaTaskList taskList1(*iRWsSession);
       
   214                 TApaTask task1 = taskList1.FindApp(iSlotUIDs[t]);
       
   215                 if (task1.Exists())
       
   216                     {
       
   217                     task1.EndTask();
       
   218                     User::After(KWait01);
       
   219                     }
       
   220                 }
       
   221 		    }
       
   222 		iSlotUIDs.Reset();
       
   223 		}
       
   224 	Cancel();
       
   225     }
       
   226 // ----------------------------------------------------------
       
   227 // CActive object cancel function
       
   228 // ----------------------------------------------------------
       
   229 //
       
   230 void CRandomTest::DoCancel()
       
   231 {
       
   232 	iTimer.Cancel();
       
   233 }
       
   234 
       
   235 // -----------------------------------------
       
   236 // Checks if given Uid is in test list
       
   237 // -----------------------------------------
       
   238 //
       
   239 TBool CRandomTest::IsUidInTestList(TUid aUid)
       
   240     {
       
   241     TInt res=EFalse;
       
   242     for(TInt t=0;t<iSlotUIDs.Count();t++)
       
   243             if(iSlotUIDs[t].iUid==aUid.iUid)
       
   244                 {
       
   245                 res=ETrue;
       
   246                 break;
       
   247                 }
       
   248                 
       
   249     return res;
       
   250     }
       
   251 
       
   252 // ----------------------------------------------------------
       
   253 // CActive object RunL function
       
   254 // Runs test on test apps
       
   255 // ----------------------------------------------------------
       
   256 //
       
   257 void CRandomTest::RunL()
       
   258 {
       
   259 	// For screen light
       
   260 	User::ResetInactivityTime();
       
   261 	// Gets screen width and height
       
   262 	TInt MaxX = iSettings.iAppRect.Width();
       
   263 	TInt MaxY = iSettings.iAppRect.Height();
       
   264 	// Gets random coordinates for random pointer events
       
   265 	TInt x = Math::Random()%MaxX*2;
       
   266 	TInt y = Math::Random()%MaxY*2;
       
   267 	// Repets value for long key press
       
   268 	TInt repeats = Math::Random()%2;
       
   269 	// Decides which key press event will be sent
       
   270 	TInt chance = Math::Random()%KMaxChance;
       
   271 	// Decides which event will be  sent (pointer/key press)
       
   272 	TInt randomevent = Math::Random()%KEventTypeMax;
       
   273 	
       
   274 	// Sends Ok button press event according to counter 
       
   275 	if(iCounterForOkButton++ > COUNTERFOROK)
       
   276 		{// Ok button press event
       
   277 		iCounterForOkButton = 0;
       
   278 		repeats = 0;
       
   279 		chance = KMaxChance-20;
       
   280 		randomevent = EKeys;
       
   281 		}
       
   282 	
       
   283 	TWsEvent wsEvent;
       
   284 	TKeyEvent& keyEvent(*wsEvent.Key());
       
   285 	
       
   286 	TRawEvent event;
       
   287 	TRawEvent event1;
       
   288 	TRawEvent event2;
       
   289 	TRawEvent event3;
       
   290 	
       
   291 	// Log foreground app's name
       
   292 	CApaWindowGroupName* gn = CApaWindowGroupName::NewLC(*iRWsSession, iRWsSession->GetFocusWindowGroup());
       
   293 	TApaAppInfo appInfo;
       
   294 	RApaLsSession ApaLs;
       
   295 	User::LeaveIfError(ApaLs.Connect());
       
   296 	ApaLs.GetAppInfo(appInfo, gn->AppUid());
       
   297 	
       
   298 	
       
   299 	// If one of our apps is in foreground then don't close it just send it to background
       
   300 	if(appInfo.iUid == KUidPanicCapturer )
       
   301 	{ 
       
   302 		TApaTaskList taskList(*iRWsSession);
       
   303 		TApaTask task = taskList.FindApp(appInfo.iUid);
       
   304 
       
   305 		if (task.Exists())
       
   306 			task.SendToBackground();
       
   307 		CleanupStack::PopAndDestroy(gn);
       
   308 		ApaLs.Close();
       
   309 		
       
   310 		iTimer.After(iStatus,KWait01);
       
   311 		SetActive();
       
   312 						
       
   313 		return;
       
   314 	}
       
   315 	// If app in foreground is different now or it's there for a long time then log it's name
       
   316 	else if(iLastAppName.Compare(appInfo.iCaption)!=0 || iAppNameLoggingCounter++>KLogAppAtStep )
       
   317 		{
       
   318 		iAppNameLoggingCounter = 0;
       
   319 		iLastAppName.Zero();
       
   320 		iLastAppName.Copy(appInfo.iCaption);
       
   321 		TBuf<KBuffer540> buf;
       
   322 		_LIT(KText,"Launched app.: ");
       
   323 		_LIT(KNL,"\n");
       
   324 		buf.Copy(KText);
       
   325 		buf.Append(iLastAppName);
       
   326 		buf.Append(KNL);
       
   327 		iLogger.WriteLogL(buf,EFalse);
       
   328 		}
       
   329 	CleanupStack::PopAndDestroy(gn);
       
   330 	ApaLs.Close();
       
   331 	
       
   332 	// If random test run is on in Settings dialog...
       
   333 	if(iSettings.iRandomTestAppIdStatus)
       
   334 		{
       
   335 		TThreadId threadId; // not used
       
   336 		// Bring given ID's app to foreground
       
   337 		TUid uid;
       
   338 		uid.iUid = 0;
       
   339 		
       
   340 		// Decides which test app should be in foreground
       
   341 		TInt count = iAppTestCounter/TESTSTEPSNUMBER;
       
   342 		
       
   343 		if(iSlotUIDs.Count()>count)
       
   344 		      uid = iSlotUIDs[count];
       
   345 		else
       
   346 		    {
       
   347 		    iAppTestCounter=0;
       
   348 		    if(iSlotUIDs.Count()>0)
       
   349 		      uid = iSlotUIDs[0];
       
   350 		    }
       
   351 		
       
   352 		iAppTestCounter++;
       
   353 		
       
   354 		
       
   355 		// If app in foreground is not one of our test apps then close it.
       
   356 		if( iSlotUIDs.Count()>0 )
       
   357 			{
       
   358 			TApaTaskList taskList(*iRWsSession);
       
   359 			TApaTask task = taskList.FindApp(uid);
       
   360 			
       
   361 			if( !IsUidInTestList(appInfo.iUid) && appInfo.iUid.iUid!=0x100058b3 && 
       
   362 			                 appInfo.iUid.iUid!=_UID3 ) // Don't close telephony app. and User Emulator (exceptions)
       
   363 				{
       
   364 				TApaTask task1 = taskList.FindApp(appInfo.iUid);
       
   365 				if (task1.Exists())
       
   366 					{
       
   367 					task1.EndTask();
       
   368 					User::After(KWait01);
       
   369 					}
       
   370 				}
       
   371 			// If our test app is not in foreground then bring it to foreground
       
   372 			if (task.Exists())
       
   373 				task.BringToForeground();
       
   374 			else
       
   375 			    // If it's not started then start it
       
   376 				iSession.StartDocument(KN, uid ,threadId);
       
   377 			}
       
   378 		else
       
   379 			{
       
   380 			// If there's no valid app uid display error message
       
   381 			if(iCantStartAppToTest)
       
   382 				{
       
   383 				_LIT(KNoValid,"No valid application to start! Random test ends.");
       
   384 				iCantStartAppToTest = EFalse;
       
   385 				CAknErrorNote* errorNote = new (ELeave) CAknErrorNote(ETrue);
       
   386 				errorNote->ExecuteLD(KNoValid);
       
   387 				iObserver.RandomTestEndsL();
       
   388 				return;
       
   389 				}
       
   390 			
       
   391 			iTimer.After(iStatus,KWait05);
       
   392 			SetActive();
       
   393 			
       
   394 			return;
       
   395 			}
       
   396 		}
       
   397 	
       
   398 	// Run pointer test if device enables it otherwise key events only
       
   399 	if(!AknLayoutUtils::PenEnabled())
       
   400 		randomevent = EKeys;
       
   401 	
       
   402 	// Events
       
   403 	switch(randomevent)
       
   404 		{
       
   405 		case ETap:
       
   406 			 event.Set(TRawEvent::EButton1Down, x, y);
       
   407 			 iRWsSession->SimulateRawEvent(event);
       
   408 			 User::After(KWait01);
       
   409 			 event1.Set(TRawEvent::EButton1Up, x, y);
       
   410 			 iRWsSession->SimulateRawEvent(event1);
       
   411 		break;
       
   412 		case EDoubleTap:			 
       
   413 			 event.Set(TRawEvent::EButton1Down, x, y);
       
   414 			 iRWsSession->SimulateRawEvent(event);
       
   415 			 User::After(KWait01);
       
   416 			 event1.Set(TRawEvent::EButton1Up, x, y);
       
   417 			 iRWsSession->SimulateRawEvent(event1);
       
   418 			 User::After(KWait01);
       
   419 			 event2.Set(TRawEvent::EButton1Down, x, y);
       
   420 			 iRWsSession->SimulateRawEvent(event2);
       
   421 			 User::After(KWait01);
       
   422 			 event3.Set(TRawEvent::EButton1Up, x, y);
       
   423 			 iRWsSession->SimulateRawEvent(event3);
       
   424 		break;
       
   425 		case EFlick:
       
   426 			event.Set(TRawEvent::EButton1Down, x, y);
       
   427 			iRWsSession->SimulateRawEvent(event);
       
   428 			User::After(KWait01);
       
   429 		
       
   430 			event1.Set(TRawEvent::EPointerMove, x+KShortFlickDist, y+KShortFlickDist);
       
   431 			iRWsSession->SimulateRawEvent(event1);
       
   432 			User::After(KWait01);
       
   433 			
       
   434 			event2.Set(TRawEvent::EButton1Up, x+KShortFlickDist, y+KShortFlickDist);
       
   435 			iRWsSession->SimulateRawEvent(event2);
       
   436 		break;
       
   437 		case EDrag:
       
   438 			event.Set(TRawEvent::EButton1Down, x, y);
       
   439 			iRWsSession->SimulateRawEvent(event);
       
   440 			User::After(KWait01);
       
   441 			
       
   442 			event1.Set(TRawEvent::EPointerMove, x+KShortFlickDist, y);
       
   443 			iRWsSession->SimulateRawEvent(event1);
       
   444 			User::After(KWait01);
       
   445 			
       
   446 			event2.Set(TRawEvent::EPointerMove, x+KShortFlickDist,y+KShortFlickDist);
       
   447 			iRWsSession->SimulateRawEvent(event2);
       
   448 			User::After(KWait01);
       
   449 			
       
   450 			event3.Set(TRawEvent::EButton1Up, x+KShortFlickDist, y+KShortFlickDist);
       
   451 			iRWsSession->SimulateRawEvent(event3);
       
   452 		break;
       
   453 		case EKeys:
       
   454 		case EKeys1:
       
   455 		case EKeys2:
       
   456 		case EKeys3:
       
   457 		default:
       
   458 			CApaWindowGroupName* gn = CApaWindowGroupName::NewLC(*iRWsSession, iRWsSession->GetFocusWindowGroup());
       
   459 			TUid foreTaskUid = gn->AppUid();
       
   460 			CleanupStack::PopAndDestroy(gn);
       
   461 			
       
   462 			iRWsSession->GetFocusWindowGroup();
       
   463 			keyEvent.iModifiers=0;
       
   464 			
       
   465 			// Increasing the chance of menu, ok, arrow ,etc. keys
       
   466 			if( chance > KMaxChance-120)
       
   467 				{
       
   468 				if(chance<KMaxChance-100)
       
   469 					keyEvent.iScanCode = EStdKeyDevice0;
       
   470 				else if(chance<KMaxChance-80)
       
   471 					keyEvent.iScanCode = EStdKeyDevice1;
       
   472 				else if(chance<KMaxChance-70)
       
   473 					keyEvent.iScanCode = EStdKeyUpArrow;
       
   474 				else if(chance<KMaxChance-60)
       
   475 					keyEvent.iScanCode = EStdKeyDownArrow;
       
   476 				else if(chance<KMaxChance-50)
       
   477 					keyEvent.iScanCode = EStdKeyLeftArrow;
       
   478 				else if(chance<KMaxChance-40)
       
   479 					keyEvent.iScanCode = EStdKeyRightArrow;
       
   480 				else if(chance<KMaxChance-10)
       
   481 					keyEvent.iScanCode = EStdKeyApplication0;
       
   482 				else if(chance<=KMaxChance)
       
   483 					keyEvent.iScanCode = EStdKeyBackspace;
       
   484 				}
       
   485 			else
       
   486 				keyEvent.iScanCode = Math::Random()%KScancodeMax;
       
   487 			
       
   488 			if(iSettings.iRandomTestAppIdStatus)
       
   489 				{
       
   490 				// If there is valid uid application -> no menu button push
       
   491 				// It's for random tests without predefined test apps
       
   492 				while(keyEvent.iScanCode ==  EStdKeyApplication0 || keyEvent.iCode == EKeyDevice2)
       
   493 					keyEvent.iScanCode = Math::Random()%KScancodeMax;
       
   494 					 
       
   495 				}
       
   496 			
       
   497 			// Sends the key event
       
   498 		  keyEvent.iCode = keyEvent.iScanCode;
       
   499 			
       
   500 			
       
   501 		  TRawEvent lEventDown;
       
   502 		  lEventDown.Set(TRawEvent::EKeyDown, keyEvent.iScanCode);
       
   503 		  UserSvr::AddEvent(lEventDown);
       
   504 
       
   505 		  User::After(KWait01);
       
   506 											
       
   507 		  // long key press
       
   508 		  if(repeats>0)
       
   509 			 User::After((KWait01*12)*repeats); // 1.2 sec * repeats
       
   510 											
       
   511 		  TRawEvent lEventUp;
       
   512 		  lEventUp.Set(TRawEvent::EKeyUp, keyEvent.iScanCode);
       
   513 		  UserSvr::AddEvent(lEventUp);
       
   514 			
       
   515 		break;
       
   516 		}
       
   517 	// Restart timer
       
   518 	iTimer.After(iStatus,KWait01);
       
   519 	SetActive();
       
   520 }