testexecfw/stf/stffw/testsrv/src/TestServer.cpp
changeset 2 8bb370ba6d1d
equal deleted inserted replaced
1:bbd31066657e 2:8bb370ba6d1d
       
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). 
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 * 
       
    14 * Description: This module contains implementation of CTestServer 
       
    15 * class member functions.
       
    16 *
       
    17 */
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include <e32std.h>
       
    21 #include <e32svr.h>
       
    22 #include "TestEngineClient.h"
       
    23 #include <StifTestModule.h>
       
    24 #include <stifinternal/TestServerClient.h>
       
    25 #include "TestServer.h"
       
    26 #include "TestServerCommon.h"
       
    27 #include <stifinternal/TestThreadContainerRunnerFactory.h>
       
    28 
       
    29 // EXTERNAL DATA STRUCTURES
       
    30 
       
    31 // EXTERNAL FUNCTION PROTOTYPES  
       
    32 
       
    33 // CONSTANTS
       
    34 
       
    35 // MACROS
       
    36 
       
    37 // LOCAL CONSTANTS AND MACROS
       
    38 
       
    39 // MODULE DATA STRUCTURES
       
    40 
       
    41 // Struct to pass parameters to server thread
       
    42 struct TThreadStartTestServer
       
    43     {
       
    44     TFileName  iName;         // Server name
       
    45     RThread    iServerThread; // The server thread
       
    46     RSemaphore iStarted;      // Startup syncronisation semaphore   
       
    47     TBool      iInNewThread;  // Is thread running in new process?
       
    48     TInt       iStartupResult;// Start-up result
       
    49     TBool      iUiTesting;    // Is it testserver for UI testing
       
    50     CTestThreadContainerRunnerFactory* iTestThreadContainerRunnerFactory; // Pointer to CTestThreadContainerRunner. Defined when
       
    51 																		  // iUiTesting is true
       
    52     };
       
    53 
       
    54 // LOCAL FUNCTION PROTOTYPES
       
    55 
       
    56 // FORWARD DECLARATIONS
       
    57 
       
    58 // ==================== LOCAL FUNCTIONS =======================================
       
    59 
       
    60 // None
       
    61 
       
    62 // ================= MEMBER FUNCTIONS =========================================
       
    63 
       
    64 /*
       
    65 -------------------------------------------------------------------------------
       
    66 
       
    67     Class: CTestServer
       
    68 
       
    69     Method: PanicServer
       
    70 
       
    71     Description: Panics the server. 
       
    72 
       
    73     Parameters: const TTestServerPanic aPanic: in: Panic code
       
    74 
       
    75     Return Values: None
       
    76 
       
    77     Errors/Exceptions: None
       
    78 
       
    79     Status: Approved
       
    80 
       
    81 -------------------------------------------------------------------------------
       
    82 */
       
    83 void CTestServer::PanicServer( const TTestServerPanic aPanic )
       
    84     {
       
    85     
       
    86 #ifdef USE_LOGGER
       
    87     // Check if logger is available, if so, use it.
       
    88     CStifLogger* log = (CStifLogger*) Dll::Tls();
       
    89     if ( log )
       
    90         {
       
    91         log->Log( CStifLogger::ERed, _L("TestServer.DLL Panic %d"), aPanic);
       
    92         }
       
    93 #endif
       
    94 
       
    95     RDebug::Print( _L( "CTestServer::PanicServer" ) );
       
    96     _LIT( KTxtTestServer,"CTestServer" );
       
    97     User::Panic( KTxtTestServer,aPanic );
       
    98 
       
    99     }
       
   100 
       
   101 /*
       
   102 -------------------------------------------------------------------------------
       
   103 
       
   104     Class: CTestServer
       
   105 
       
   106     Method: NewL
       
   107 
       
   108     Description: Returns new CTestServer object
       
   109 
       
   110     Parameters: const TFileName& aName: in: Server name
       
   111 
       
   112     Return Values: None
       
   113 
       
   114     Errors/Exceptions: Leaves if memory allocation, ConstructL or StartL leaves.
       
   115 
       
   116     Status: Approved
       
   117 
       
   118 -------------------------------------------------------------------------------
       
   119 */
       
   120 CTestServer* CTestServer::NewL( const TFileName& aName )
       
   121     {
       
   122 
       
   123     CTestServer* self = new( ELeave ) CTestServer();
       
   124     CleanupStack::PushL( self );
       
   125 
       
   126     // Construct the server
       
   127     self->ConstructL( aName );
       
   128 
       
   129     // Start the server
       
   130     self->StartL( aName );
       
   131 
       
   132     CleanupStack::Pop( self );
       
   133 
       
   134     return self;
       
   135 
       
   136     }
       
   137 
       
   138 /*
       
   139 -------------------------------------------------------------------------------
       
   140 
       
   141     Class: CTestServer
       
   142 
       
   143     Method: ConstructL
       
   144 
       
   145     Description: Second level constructor. Obtains pointer to library
       
   146     entrypoint.
       
   147 
       
   148     Parameters: const TFileName& aName: in: Server name
       
   149 
       
   150     Return Values: None
       
   151 
       
   152     Errors/Exceptions: Leaves if entrypoint can't be obtained.
       
   153 
       
   154     Status: Approved
       
   155 
       
   156 -------------------------------------------------------------------------------
       
   157 */
       
   158 void CTestServer::ConstructL( const TFileName& aName )
       
   159     {
       
   160     // Construct heap buffer for configuration file
       
   161     iModuleNameBuffer = HBufC::NewL( aName.Length() );
       
   162     iModuleName.Set ( iModuleNameBuffer->Des() );
       
   163     iModuleName.Copy ( aName );
       
   164 
       
   165     iContainerIndex = CObjectConIx::NewL();
       
   166 
       
   167     }
       
   168 
       
   169 /*
       
   170 -------------------------------------------------------------------------------
       
   171 
       
   172     Class: CTestServer
       
   173 
       
   174     Method: CTestServer
       
   175 
       
   176     Description: Constructor.
       
   177 
       
   178     Initialises non-zero member variables and base class with correct
       
   179     priority.
       
   180 
       
   181     Parameters: None
       
   182 
       
   183     Return Values: None
       
   184 
       
   185     Errors/Exceptions: None
       
   186 
       
   187     Status: Approved
       
   188 
       
   189 -------------------------------------------------------------------------------
       
   190 */
       
   191 CTestServer::CTestServer() : CServer2( CTestServer::ETestServerPriority ),
       
   192                              iModuleName(0, 0),
       
   193                              iSessionCount( 0 )
       
   194     {
       
   195     iFirstTime = ETrue;
       
   196 
       
   197     }
       
   198 
       
   199 /*
       
   200 -------------------------------------------------------------------------------
       
   201 
       
   202     Class: CTestServer
       
   203 
       
   204     Method: ~CTestServer
       
   205 
       
   206     Description: Destructor
       
   207     Frees memory.
       
   208 
       
   209     Parameters: None
       
   210 
       
   211     Return Values: None
       
   212 
       
   213     Errors/Exceptions: None
       
   214 
       
   215     Status: Approved
       
   216 
       
   217 -------------------------------------------------------------------------------
       
   218 */
       
   219 CTestServer::~CTestServer()
       
   220     {
       
   221 
       
   222     delete iModuleNameBuffer;
       
   223     iModuleNameBuffer = NULL;
       
   224 
       
   225     delete iContainerIndex;
       
   226     iContainerIndex = NULL;
       
   227 
       
   228     }
       
   229 
       
   230 /*
       
   231 -------------------------------------------------------------------------------
       
   232 
       
   233     Class: CTestServer
       
   234 
       
   235     Method: NewContainerL
       
   236 
       
   237     Description: Returns new container. Used to store subsessions
       
   238 
       
   239     Parameters: None
       
   240 
       
   241     Return Values: CObjectCon* New object container
       
   242 
       
   243     Errors/Exceptions: None
       
   244 
       
   245     Status: Approved
       
   246 
       
   247 -------------------------------------------------------------------------------
       
   248 */
       
   249 CObjectCon* CTestServer::NewContainerL()
       
   250     {
       
   251      
       
   252     CObjectCon* container = iContainerIndex->CreateL();
       
   253 
       
   254     iSessionCount++;
       
   255 
       
   256     return container;
       
   257 
       
   258     }
       
   259 
       
   260 /*
       
   261 -------------------------------------------------------------------------------
       
   262 
       
   263     Class: CTestServer
       
   264 
       
   265     Method: DeleteContainer
       
   266 
       
   267     Description: Deletes a container.
       
   268 
       
   269     Parameters: CObjectCon* aContainer: in: Container to be removed
       
   270 
       
   271     Return Values: None
       
   272 
       
   273     Errors/Exceptions: None
       
   274 
       
   275     Status: Approved
       
   276 
       
   277 -------------------------------------------------------------------------------
       
   278 */
       
   279 void CTestServer::DeleteContainer( CObjectCon* aContainer )
       
   280     {
       
   281     iContainerIndex->Remove( aContainer );
       
   282 
       
   283     }
       
   284 
       
   285 /*
       
   286 -------------------------------------------------------------------------------
       
   287 
       
   288     Class: CTestServer
       
   289 
       
   290     Method: SessionClosed
       
   291 
       
   292     Description: Inform Server that session is closed.
       
   293 
       
   294     Parameters: None
       
   295 
       
   296     Return Values: None
       
   297 
       
   298     Errors/Exceptions: None
       
   299 
       
   300     Status: Approved
       
   301 
       
   302 -------------------------------------------------------------------------------
       
   303 */
       
   304 void CTestServer::SessionClosed()
       
   305     {
       
   306     // Decrease session count
       
   307     iSessionCount--;
       
   308 
       
   309     // Check if last session is closed
       
   310     if ( iSessionCount <= 0 )
       
   311         {
       
   312         // Stop the active scheduler
       
   313         // Execution will continue in ThreadFunction()
       
   314         CActiveScheduler::Stop();
       
   315         }
       
   316 
       
   317     }
       
   318 /*
       
   319 -------------------------------------------------------------------------------
       
   320 
       
   321     Class: CTestServer
       
   322 
       
   323     Method: NewSessionL
       
   324 
       
   325     Description: Returns new session.
       
   326     
       
   327     Parameters: const TVersion &aVersion: in: Version required
       
   328 
       
   329     Return Values: CSharableSession* New session
       
   330 
       
   331     Errors/Exceptions: Leaves if invalid version or CTestModule construction
       
   332                        leaves
       
   333 
       
   334     Status: Approved
       
   335 
       
   336 -------------------------------------------------------------------------------
       
   337 */
       
   338 CSession2* CTestServer::NewSessionL( const TVersion& aVersion,
       
   339                                         const RMessage2& /*aMessage*/ ) const
       
   340     {
       
   341     // check version is ok
       
   342     TVersion v( KTestServerMajorVersionNumber,
       
   343                 KTestServerMinorVersionNumber,
       
   344                 KTestServerBuildVersionNumber
       
   345                );
       
   346     if( !User::QueryVersionSupported( v,aVersion ) )
       
   347         {
       
   348         User::Leave( KErrNotSupported );
       
   349         }
       
   350 
       
   351     return CTestModule::NewL( ( CTestServer* ) this );
       
   352     }
       
   353 
       
   354 /*
       
   355 -------------------------------------------------------------------------------
       
   356 
       
   357     Class: CTestServer
       
   358 
       
   359     Method: ModuleName
       
   360 
       
   361     Description: Returns module name
       
   362 
       
   363     Parameters: None
       
   364 
       
   365     Return Values: const TDesC&* Module name
       
   366 
       
   367     Errors/Exceptions: None
       
   368 
       
   369     Status: Approved
       
   370 
       
   371 -------------------------------------------------------------------------------
       
   372 */
       
   373 const TDesC& CTestServer::ModuleName() const
       
   374     {
       
   375     return iModuleName;
       
   376 
       
   377     }
       
   378 
       
   379 /*
       
   380 -------------------------------------------------------------------------------
       
   381 
       
   382     Class: CTestServer
       
   383 
       
   384     Method: FirstTime
       
   385 
       
   386     Description: Is module already once initialised.
       
   387 
       
   388     Parameters: None
       
   389 
       
   390     Return Values: TBool Has module initialized?
       
   391 
       
   392     Errors/Exceptions: None
       
   393 
       
   394     Status: Approved
       
   395 
       
   396 -------------------------------------------------------------------------------
       
   397 */
       
   398 //@spe const TBool CTestServer::FirstTime() const
       
   399 TBool CTestServer::FirstTime() const
       
   400     {
       
   401     return iFirstTime;
       
   402 
       
   403     }
       
   404 
       
   405 /*
       
   406 -------------------------------------------------------------------------------
       
   407 
       
   408     Class: CTestServer
       
   409 
       
   410     Method: ClearFirstTime
       
   411 
       
   412     Description: Clear module first time flag. 
       
   413 
       
   414     Parameters: None
       
   415 
       
   416     Return Values: None
       
   417 
       
   418     Errors/Exceptions: None
       
   419 
       
   420     Status: Approved
       
   421 
       
   422 -------------------------------------------------------------------------------
       
   423 */
       
   424 void CTestServer::ClearFirstTime()
       
   425     {
       
   426     iFirstTime = EFalse;
       
   427 
       
   428     }
       
   429 
       
   430 /*
       
   431 -------------------------------------------------------------------------------
       
   432 
       
   433     Class: CTestServer
       
   434 
       
   435     Method: ThreadFunction
       
   436 
       
   437     Description: The thread function, where Test Server lives in
       
   438     
       
   439     Parameters: TAny* aStarted: in: Start-up information
       
   440     
       
   441     Return Values: TInt Result from test module
       
   442 
       
   443     Errors/Exceptions: Clean-up stack can't be created because cannot
       
   444                        leave, error checks are done locally.
       
   445                        Panics if:
       
   446                        invalid start-up information
       
   447                        Test Server can't be started
       
   448 
       
   449     Status: Approved
       
   450 
       
   451 -------------------------------------------------------------------------------
       
   452 */
       
   453 TInt CTestServer::ThreadFunction( TAny* aStarted )
       
   454     {
       
   455 
       
   456     __UHEAP_MARK;
       
   457 
       
   458     TInt error( KErrNone );
       
   459 
       
   460     // Get start-up information
       
   461     TThreadStartTestServer* startInfo = ( TThreadStartTestServer* ) aStarted;
       
   462     __ASSERT_ALWAYS( startInfo,PanicServer( ENoStartupInformation ) );
       
   463 
       
   464     // Create clean-up stack
       
   465     CTrapCleanup* tc = CTrapCleanup::New();
       
   466     __ASSERT_ALWAYS( tc, PanicServer(ECreateTrapCleanup));
       
   467 
       
   468     // Construct the logger
       
   469     TName path = _L("C:\\logs\\testframework\\testserver\\");
       
   470     TFileName name = _L("testserver_");
       
   471     name.Append ( startInfo->iName );
       
   472 
       
   473     // Create logger, in Wins use HTML in HW default logger
       
   474     TLoggerSettings loggerSettings;
       
   475 
       
   476     // Directory must create by hand if test server log wanted
       
   477     loggerSettings.iCreateLogDirectories = EFalse;
       
   478 
       
   479     loggerSettings.iOverwrite = ETrue;
       
   480     loggerSettings.iTimeStamp = ETrue;
       
   481     loggerSettings.iLineBreak = ETrue;
       
   482     loggerSettings.iEventRanking = EFalse;
       
   483     loggerSettings.iThreadId = EFalse;
       
   484     loggerSettings.iHardwareFormat = CStifLogger::ETxt;
       
   485 #ifndef FORCE_STIF_INTERNAL_LOGGING_TO_RDEBUG
       
   486     loggerSettings.iEmulatorFormat = CStifLogger::EHtml;
       
   487     loggerSettings.iHardwareOutput = CStifLogger::EFile;
       
   488     loggerSettings.iEmulatorOutput = CStifLogger::EFile;
       
   489 #else
       
   490     RDebug::Print( _L( "STIF Test Server logging forced to RDebug" ) );
       
   491     loggerSettings.iEmulatorFormat = CStifLogger::ETxt;
       
   492     loggerSettings.iHardwareOutput = CStifLogger::ERDebug;
       
   493     loggerSettings.iEmulatorOutput = CStifLogger::ERDebug;
       
   494 #endif
       
   495     loggerSettings.iUnicode = EFalse;
       
   496     loggerSettings.iAddTestCaseTitle = EFalse;
       
   497 
       
   498     CStifLogger* logger = NULL;
       
   499     TRAP ( error, logger = CStifLogger::NewL( path, name, loggerSettings ) );
       
   500 
       
   501     // Thread Local Storage is used for get pointer to logger.
       
   502     Dll::SetTls ( logger );
       
   503 
       
   504     __TRACE( KInit,( _L( "TestServer.DLL server starting" ) ) );
       
   505     __TRACE( KInit,( CStifLogger::EBold, _L( "Loading module: %S"), &startInfo->iName ) );
       
   506 
       
   507     RLibrary module;
       
   508     TInt ret = KErrNone;
       
   509 
       
   510     TFileName newNameBuffer;
       
   511     TInt check = CheckModuleName( startInfo->iName, newNameBuffer );
       
   512     if( check == KErrNone )
       
   513         {
       
   514         // Load the module(TestScripter)
       
   515         ret = module.Load( newNameBuffer );
       
   516         }
       
   517     else
       
   518         {
       
   519         RemoveOptionalIndex(startInfo->iName, newNameBuffer);
       
   520         __TRACE(KInit, (CStifLogger::EBold, _L( "Valid module name is [%S] (extracted from [%S])"), &newNameBuffer, &startInfo->iName));
       
   521         // Load the module(Others)
       
   522         ret = module.Load(newNameBuffer);
       
   523         }
       
   524 
       
   525     // If test module loading fails, do not start server
       
   526     if( ret != KErrNone )
       
   527         {
       
   528          __TRACE( KError,( CStifLogger::ERed, _L( "Test module loading failed, code = %d" ), ret ) );
       
   529          __TRACE( KError,( _L( "Check that module is compiled properly and stored to correct directory and all DLLs that it requires are available" ) ) );                  
       
   530 
       
   531          // Error will be handled in StartNewServer
       
   532         startInfo->iStartupResult = ret;
       
   533         startInfo->iStarted.Signal();
       
   534         if ( !startInfo->iInNewThread )
       
   535             {
       
   536             startInfo->iStarted.Close();
       
   537             }
       
   538         module.Close();
       
   539         Dll::FreeTls();
       
   540         // Delete logger
       
   541         delete logger;
       
   542         logger = NULL;
       
   543         // Delete clean-up stack
       
   544         delete tc;
       
   545         tc = NULL;
       
   546         __UHEAP_MARKEND;
       
   547         return ret;
       
   548         }
       
   549     else
       
   550         {
       
   551         __TRACE( KInit,( _L( "Test module loaded correctly" ) ) );
       
   552         }
       
   553 
       
   554     // Verify that there is function
       
   555     CTestInterfaceFactory libEntry = ( CTestInterfaceFactory ) module.Lookup( 1 );
       
   556     if( libEntry == NULL )
       
   557         {
       
   558          // Error will be handled in StartNewServer
       
   559         __TRACE( KError,( CStifLogger::ERed, _L( "Can't find entrypoint from test module" ) ) );
       
   560 
       
   561         startInfo->iStartupResult = KErrNotFound;
       
   562         startInfo->iStarted.Signal();
       
   563         if ( !startInfo->iInNewThread )
       
   564             {
       
   565             startInfo->iStarted.Close();
       
   566             }
       
   567         module.Close();
       
   568         Dll::FreeTls();
       
   569         // Delete logger
       
   570         delete logger;
       
   571         logger = NULL;
       
   572         // Delete clean-up stack
       
   573         delete tc;
       
   574         tc = NULL;
       
   575         __UHEAP_MARKEND;
       
   576         return KErrNotFound;
       
   577         }
       
   578 
       
   579     module.Close();
       
   580 
       
   581     // Construct and install active scheduler
       
   582     CActiveScheduler* scheduler = new CActiveScheduler;
       
   583     __ASSERT_ALWAYS( scheduler, PanicServer( EMainSchedulerError ) );
       
   584     CActiveScheduler::Install( scheduler );
       
   585 
       
   586     // Construct server
       
   587     CTestServer* server = NULL;
       
   588     TRAPD( err, server = CTestServer::NewL( startInfo->iName ) );
       
   589     __ASSERT_ALWAYS( !err, PanicServer( ESvrCreateServer ) );
       
   590 
       
   591     server->iUiTestingSupport = startInfo->iUiTesting;
       
   592     server->iTestThreadContainerRunnerFactory = startInfo->iTestThreadContainerRunnerFactory;
       
   593     
       
   594     // Inform that we are up and running
       
   595     startInfo->iStartupResult = KErrNone;
       
   596     startInfo->iStarted.Signal();
       
   597     if ( !startInfo->iInNewThread )
       
   598         {
       
   599         startInfo->iStarted.Close();
       
   600         }
       
   601 
       
   602     // Start handling requests
       
   603     CActiveScheduler::Start();
       
   604 
       
   605     // Execution continues from here after CActiveScheduler::Stop
       
   606 
       
   607     __TRACE( KVerbose,( _L( "TestServer.DLL active scheduler stopped" ) ) );
       
   608 
       
   609     // Delete the server
       
   610     delete server;
       
   611     server = NULL;
       
   612     __TRACE( KVerbose,( _L( "TestServer.DLL server object deleted" ) ) );
       
   613 
       
   614     delete scheduler;
       
   615     scheduler = NULL;
       
   616     __TRACE( KVerbose,( _L( "Active scheduler deleted" ) ) );
       
   617 
       
   618     __TRACE ( KInit, (_L("TestServer.DLL ThreadFunction exiting, server closing")) );
       
   619 
       
   620     Dll::FreeTls();
       
   621     // Delete logger
       
   622     delete logger;
       
   623     logger = NULL;
       
   624     // Delete clean-up stack
       
   625     delete tc;
       
   626     tc = NULL;
       
   627 
       
   628     __UHEAP_MARKEND;
       
   629 
       
   630     return KErrNone;
       
   631 
       
   632     }
       
   633 
       
   634 /*
       
   635 -------------------------------------------------------------------------------
       
   636 
       
   637     Class: CTestServer
       
   638 
       
   639     Method: GetServerThreadId
       
   640 
       
   641     Description: Returns server thread id
       
   642 
       
   643     Parameters: None
       
   644 
       
   645     Return Values: TInt : thread id
       
   646 
       
   647     Errors/Exceptions: None
       
   648 
       
   649     Status: Approved
       
   650 
       
   651 -------------------------------------------------------------------------------
       
   652 */
       
   653 TInt CTestServer::GetServerThreadId()
       
   654     {
       
   655     RThread thread; 
       
   656     return thread.Id();
       
   657 
       
   658     }
       
   659 
       
   660 /*
       
   661 -------------------------------------------------------------------------------
       
   662 
       
   663     Class: CTestServer
       
   664 
       
   665     Method: GetTestThreadContainerRunnerFactory
       
   666 
       
   667     Description: Returns server thread id
       
   668 
       
   669     Parameters: None
       
   670 
       
   671     Return Values: TInt : thread id
       
   672 
       
   673     Errors/Exceptions: None
       
   674 
       
   675     Status: Approved
       
   676 
       
   677 -------------------------------------------------------------------------------
       
   678 */
       
   679 CTestThreadContainerRunnerFactory* CTestServer::GetTestThreadContainerRunnerFactory()
       
   680 	{
       
   681 	
       
   682 	return iTestThreadContainerRunnerFactory;
       
   683 	}
       
   684 
       
   685 /*
       
   686 -------------------------------------------------------------------------------
       
   687 
       
   688     Class: CTestServer
       
   689 
       
   690     Method: UiTesting
       
   691 
       
   692     Description: Gets information if testserver supports UI testing
       
   693 
       
   694     Parameters: None
       
   695 
       
   696     Return Values: True if testserver supports UI testing, False if not.
       
   697 
       
   698     Errors/Exceptions: None
       
   699 
       
   700     Status: Approved
       
   701 
       
   702 -------------------------------------------------------------------------------
       
   703 */
       
   704 TBool CTestServer::IsUiTestingSupported()
       
   705 	{	
       
   706 	return iUiTestingSupport;
       
   707 	}
       
   708 
       
   709 /*
       
   710 -------------------------------------------------------------------------------
       
   711 
       
   712     Class: CTestServer
       
   713 
       
   714     Method: GetUiEnvProxy
       
   715 
       
   716     Description: Gets UIEnvProxy
       
   717 
       
   718     Parameters: None
       
   719 
       
   720     Return Values: Pointer to UIEnvProxy.
       
   721 
       
   722     Errors/Exceptions: None
       
   723 
       
   724     Status: Approved
       
   725 
       
   726 -------------------------------------------------------------------------------
       
   727 */
       
   728 CUiEnvProxy* CTestServer::GetUiEnvProxy()
       
   729 	{
       
   730 	
       
   731 	return iTestThreadContainerRunnerFactory->GetUiEnvProxy();
       
   732 	}
       
   733 
       
   734 
       
   735 // ================= OTHER EXPORTED FUNCTIONS =================================
       
   736 
       
   737 /*
       
   738 -------------------------------------------------------------------------------
       
   739 
       
   740     Class: -
       
   741 
       
   742     Method: StartNewServer
       
   743 
       
   744     Description: Starts a new server. Server will be running its own
       
   745     thread and this functions returns when server is up and running or
       
   746     server start-up fails.
       
   747 
       
   748     Parameters: const TFileName& aModuleFileName: in: Module name
       
   749                 TFileName& aServerName: in: Server name
       
   750                 const TBool aInNewThread: in: Is new thread
       
   751                 RSemaphore aSynchronisation: in: For synchronisation
       
   752                 TBool aUiTestingServer: in: Indicates if testserver should support UI testing
       
   753                 CTestThreadContainerRunnerFactory* aTestThreadContainerRunnerFactory: in: Pointer to runner factory
       
   754 
       
   755     Return Values: TInt: Symbian error code
       
   756 
       
   757     Errors/Exceptions: None
       
   758 
       
   759     Status: Approved
       
   760 
       
   761 -------------------------------------------------------------------------------
       
   762 */
       
   763 EXPORT_C TInt StartNewServer( const TFileName& aModuleFileName,
       
   764                               TFileName& aServerName,
       
   765                               const TBool aInNewThread,
       
   766                               RSemaphore aSynchronisation,
       
   767                               TBool aUiTestingServer,
       
   768                               CTestThreadContainerRunnerFactory* aTestThreadContainerRunnerFactory
       
   769                             )
       
   770     {
       
   771 
       
   772     __UHEAP_MARK;
       
   773 
       
   774     //Check server not already started
       
   775     TFindServer findServer( aModuleFileName );
       
   776     TFullName name;
       
   777     if( findServer.Next( name ) == KErrNone )
       
   778         {   
       
   779         // Server already started, nothing to do
       
   780         aServerName = aModuleFileName;
       
   781         __UHEAP_MARKEND;
       
   782         return KErrAlreadyExists;
       
   783         }
       
   784 
       
   785     // Construct start-up information object
       
   786     TThreadStartTestServer* startInfo = new TThreadStartTestServer();
       
   787     if( startInfo == NULL )
       
   788         {
       
   789         __UHEAP_MARKEND;
       
   790         return KErrNoMemory;
       
   791         }
       
   792 
       
   793     // Fill the start-up information
       
   794     startInfo->iName = aModuleFileName;
       
   795     startInfo->iStartupResult = KErrNone;
       
   796     startInfo->iStarted = aSynchronisation;
       
   797     startInfo->iInNewThread = aInNewThread;
       
   798 	startInfo->iUiTesting = aUiTestingServer;
       
   799 	startInfo->iTestThreadContainerRunnerFactory = aTestThreadContainerRunnerFactory;
       
   800 
       
   801     // EKA1
       
   802     if ( aInNewThread )
       
   803         {
       
   804         // Create thread    
       
   805         TInt res = startInfo->iServerThread.Create( 
       
   806             startInfo->iName ,                       // Name of thread
       
   807             CTestServer::ThreadFunction,             // Thread function
       
   808             KDefaultStackSize,                       // Stack size
       
   809             KDefaultHeapSize,                        // Heap initial size
       
   810             KMaxHeapSize,                            // Heap start max size
       
   811             startInfo                                // Parameter to thread function
       
   812             );
       
   813 
       
   814         // If thread creation failed
       
   815         if( res != KErrNone )
       
   816             {
       
   817             startInfo->iStarted.Close();             // Close semaphore
       
   818             delete startInfo;
       
   819             startInfo = NULL;
       
   820             __UHEAP_MARKEND;
       
   821             return res;
       
   822             }
       
   823 
       
   824          // Now start thread
       
   825         startInfo->iServerThread.SetPriority( EPriorityMuchMore ); 
       
   826         startInfo->iServerThread.Resume();
       
   827 
       
   828         // Wait until the thread is started
       
   829         startInfo->iStarted.Wait();
       
   830 
       
   831         // Server is started( or it has returned error )
       
   832     
       
   833 
       
   834         }
       
   835 
       
   836     // EKA2 and EKA1's HW
       
   837     else
       
   838         {
       
   839         // Call directly thread function, this starts server and
       
   840         // blocks this thread.
       
   841         // Priority is default, not known reason why should be greater
       
   842         // than default priority (Work over a year).
       
   843         CTestServer::ThreadFunction( startInfo );
       
   844         }
       
   845 
       
   846     // Set server name
       
   847     aServerName = aModuleFileName;
       
   848 
       
   849     // Free memory
       
   850     TInt r = startInfo->iStartupResult;
       
   851     startInfo->iServerThread.Close();
       
   852     startInfo->iStarted.Close();
       
   853     delete startInfo;
       
   854     startInfo = NULL;
       
   855 
       
   856     __UHEAP_MARKEND;
       
   857 
       
   858     // Return start-up result.
       
   859     return r;
       
   860 
       
   861     }
       
   862 
       
   863 /*
       
   864 -------------------------------------------------------------------------------
       
   865 
       
   866     Class: -
       
   867 
       
   868     Method: StartNewServer
       
   869 
       
   870     Description: Starts a new server. Server will be running its own
       
   871     thread and this functions returns when server is up and running or
       
   872     server start-up fails.
       
   873 
       
   874     Parameters: const TFileName& aModuleFileName: in: Module name
       
   875                 TFileName& aServerName: in: Server name
       
   876                 const TBool aInNewThread: in: Is new thread
       
   877                 RSemaphore aSynchronisation: in: For synchronisation
       
   878 
       
   879     Return Values: TInt: Symbian error code
       
   880 
       
   881     Errors/Exceptions: None
       
   882 
       
   883     Status: Approved
       
   884 
       
   885 -------------------------------------------------------------------------------
       
   886 */
       
   887 EXPORT_C TInt StartNewServer( const TFileName& aModuleFileName,
       
   888                               TFileName& aServerName,
       
   889                               const TBool aInNewThread,
       
   890                               RSemaphore aSynchronisation
       
   891                             )
       
   892 	{
       
   893 	
       
   894 	return StartNewServer( aModuleFileName, aServerName, aInNewThread, aSynchronisation, false, NULL );
       
   895 	}
       
   896 
       
   897 
       
   898 /*
       
   899 -------------------------------------------------------------------------------
       
   900 
       
   901     Class: -
       
   902 
       
   903     Method: StartNewServer
       
   904 
       
   905     Description: Starts a new server. Server will be running its own
       
   906     thread and this functions returns when server is up and running or
       
   907     server start-up fails.
       
   908 
       
   909     Parameters: const TFileName& aName: in: Module name to be used
       
   910                 TFileName& aServerName: out: The name of the server
       
   911 
       
   912     Return Values: TInt Error code / KErrNone
       
   913 
       
   914     Errors/Exceptions: None
       
   915 
       
   916     Status: Proposal
       
   917 
       
   918 -------------------------------------------------------------------------------
       
   919 */
       
   920 EXPORT_C TInt StartNewServer( const TFileName& aModuleFileName,
       
   921                               TFileName& aServerName
       
   922                             )
       
   923     {
       
   924 
       
   925     __UHEAP_MARK;
       
   926 
       
   927     RMutex startupMutex;
       
   928     TInt ret( KErrNone );
       
   929     // Global mutex already created(see CTestEngineServer::ThreadFunction).
       
   930     // Open global mutex.
       
   931     ret = startupMutex.OpenGlobal( KStifTestServerStartupMutex );
       
   932     if( ret != KErrNone )
       
   933         {
       
   934         // Not able to open mutex
       
   935         return ret;
       
   936         }
       
   937 
       
   938     startupMutex.Wait();
       
   939 
       
   940     //Check server not already started
       
   941     TFindServer findServer( aModuleFileName );
       
   942     TFullName name;
       
   943     if( findServer.Next( name ) == KErrNone )
       
   944         {
       
   945         // Server already started, nothing to do
       
   946         aServerName = aModuleFileName;
       
   947         
       
   948         // release startupmutex
       
   949         startupMutex.Signal();
       
   950         startupMutex.Close();
       
   951 
       
   952         __UHEAP_MARKEND;
       
   953         return KErrAlreadyExists;
       
   954         }
       
   955 
       
   956     RSemaphore startupSemaphore;
       
   957     startupSemaphore.CreateLocal( 0 );
       
   958 
       
   959     // Start server in new thread
       
   960     TInt r = StartNewServer ( aModuleFileName, aServerName, ETrue, startupSemaphore );
       
   961 
       
   962     // startupSemaphore will be closed inside StartNewServer when start-up is done.
       
   963 
       
   964     // release startupmutex
       
   965     startupMutex.Signal();
       
   966     startupMutex.Close();
       
   967 
       
   968     __UHEAP_MARKEND;
       
   969 
       
   970     // Return start-up result.
       
   971     return r;
       
   972 
       
   973     }
       
   974 
       
   975 
       
   976 //  End of File