usbuis/usbui/tsrc/USBUiApiTest/src/USBUiApiTestBlocks.cpp
changeset 0 1e05558e2206
equal deleted inserted replaced
-1:000000000000 0:1e05558e2206
       
     1 /*
       
     2 * Copyright (c) 2002 - 2007 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:  ?Description
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 // [INCLUDE FILES] - do not remove
       
    21 #include <e32svr.h>
       
    22 #include <StifParser.h>
       
    23 #include <Stiftestinterface.h>
       
    24 #include "USBUiApiTest.h"
       
    25 
       
    26 // EXTERNAL DATA STRUCTURES
       
    27 //extern  ?external_data;
       
    28 
       
    29 // EXTERNAL FUNCTION PROTOTYPES  
       
    30 //extern ?external_function( ?arg_type,?arg_type );
       
    31 
       
    32 // CONSTANTS
       
    33 //const ?type ?constant_var = ?constant;
       
    34 
       
    35 // MACROS
       
    36 //#define ?macro ?macro_def
       
    37 
       
    38 // LOCAL CONSTANTS AND MACROS
       
    39 //const ?type ?constant_var = ?constant;
       
    40 //#define ?macro_name ?macro_def
       
    41 
       
    42 // MODULE DATA STRUCTURES
       
    43 //enum ?declaration
       
    44 //typedef ?declaration
       
    45 
       
    46 // LOCAL FUNCTION PROTOTYPES
       
    47 //?type ?function_name( ?arg_type, ?arg_type );
       
    48 
       
    49 // FORWARD DECLARATIONS
       
    50 //class ?FORWARD_CLASSNAME;
       
    51 
       
    52 // ============================= LOCAL FUNCTIONS ===============================
       
    53 
       
    54 // -----------------------------------------------------------------------------
       
    55 // ?function_name ?description.
       
    56 // ?description
       
    57 // Returns: ?value_1: ?description
       
    58 //          ?value_n: ?description_line1
       
    59 //                    ?description_line2
       
    60 // -----------------------------------------------------------------------------
       
    61 //
       
    62 /*
       
    63 ?type ?function_name(
       
    64     ?arg_type arg,  // ?description
       
    65     ?arg_type arg)  // ?description
       
    66     {
       
    67 
       
    68     ?code  // ?comment
       
    69 
       
    70     // ?comment
       
    71     ?code
       
    72     }
       
    73 */
       
    74 
       
    75 // ============================ MEMBER FUNCTIONS ===============================
       
    76 
       
    77 // -----------------------------------------------------------------------------
       
    78 // CUSBUiApiTest::Delete
       
    79 // Delete here all resources allocated and opened from test methods. 
       
    80 // Called from destructor. 
       
    81 // -----------------------------------------------------------------------------
       
    82 //
       
    83 void CUSBUiApiTest::Delete() 
       
    84     {
       
    85     iApaLsSession.Close();
       
    86     delete iCommandLine;
       
    87     iCommandLine = NULL;
       
    88     delete iRepository;
       
    89     iRepository = NULL;
       
    90     }
       
    91 
       
    92 // -----------------------------------------------------------------------------
       
    93 // CUSBUiApiTest::RunMethodL
       
    94 // Run specified method. Contains also table of test mothods and their names.
       
    95 // -----------------------------------------------------------------------------
       
    96 //
       
    97 TInt CUSBUiApiTest::RunMethodL( 
       
    98     CStifItemParser& aItem ) 
       
    99     {
       
   100 
       
   101     static TStifFunctionInfo const KFunctions[] =
       
   102         {  
       
   103         // Copy this line for every implemented function.
       
   104         // First string is the function name used in TestScripter script file.
       
   105         // Second is the actual implementation member function. 
       
   106         ENTRY( "LaunchUsbUiApp", CUSBUiApiTest::LaunchUsbUiApp ),
       
   107         ENTRY( "FinishTest", CUSBUiApiTest::FinishTest ),
       
   108         ENTRY( "TurnLightsOn", CUSBUiApiTest::TurnLightsOn ),
       
   109         //ADD NEW ENTRY HERE
       
   110         // [test cases entries] - Do not remove
       
   111 
       
   112         };
       
   113 
       
   114     const TInt count = sizeof( KFunctions ) / 
       
   115                         sizeof( TStifFunctionInfo );
       
   116 
       
   117     return RunInternalL( KFunctions, count, aItem );
       
   118 
       
   119     }
       
   120 
       
   121 // -----------------------------------------------------------------------------
       
   122 // CUSBUiApiTest::LaunchUsbUiApp
       
   123 // -----------------------------------------------------------------------------
       
   124 
       
   125 TInt CUSBUiApiTest::LaunchUsbUiApp( CStifItemParser& aItem )
       
   126     {
       
   127     TInt res;
       
   128     TPtrC testAskOnConnectionSettingName( KNullDesC );
       
   129     TPtrC testUsbPersonalityName( KNullDesC );
       
   130     TInt testAskOnConnectionSetting;
       
   131     TInt testUsbPersonalityId;
       
   132     
       
   133     res = aItem.GetString( _L( "LaunchUsbUiApp" ), testAskOnConnectionSettingName );   
       
   134     if ( res != KErrNone )
       
   135         {
       
   136         iLog -> Log( _L("GetString failed with value: %d"), res );
       
   137         iApaLsSession.Close();
       
   138         return res;
       
   139         }
       
   140     //STIF_LOG("aItem.GetString testAskOnConnectionSettingName OK");
       
   141     
       
   142     res = GetAskOnConnectionOptionFromString( testAskOnConnectionSettingName, testAskOnConnectionSetting );   
       
   143     if ( res != KErrNone )
       
   144         {
       
   145         iLog -> Log( _L("GetAskOnConnectionOptionFromString failed with value: %d"), res );
       
   146         iApaLsSession.Close();
       
   147         return res;
       
   148         }
       
   149     //STIF_LOG1("GetAskOnConnectionOptionFromString OK:  %d", testAskOnConnectionSetting);
       
   150     
       
   151     res = aItem.GetNextString( testUsbPersonalityName );   
       
   152     if ( res != KErrNone )
       
   153         {
       
   154         iLog -> Log( _L("GetNextString failed with value: %d"), res );
       
   155         iApaLsSession.Close();
       
   156         return res;
       
   157         }
       
   158     //STIF_LOG("aItem.GetNextString testUsbPersonalityName OK");
       
   159     
       
   160     res = GetUsbPersonalityOptionFromString( testUsbPersonalityName, testUsbPersonalityId );   
       
   161     if ( res != KErrNone )
       
   162         {
       
   163         iLog -> Log( _L("GetUsbPersonalityOptionFromString failed with value: %d"), res );
       
   164         iApaLsSession.Close();
       
   165         return res;
       
   166         }
       
   167     //STIF_LOG1("GetUsbPersonalityOptionFromString OK: %d",testUsbPersonalityId);
       
   168     
       
   169     res = GetUsbSettings( iUsbPersonalityBackup, iAskOnConnectionSettingBackup );
       
   170     if ( res != KErrNone )
       
   171         {
       
   172         iLog -> Log( _L("Failed to backup usb settings with value: %d"), res );
       
   173         return res;
       
   174         }
       
   175     STIF_LOG2("GetUsbSettings OK: %d, %d", iUsbPersonalityBackup, iAskOnConnectionSettingBackup);
       
   176     
       
   177     res = SetUsbSettings( testUsbPersonalityId, testAskOnConnectionSetting );
       
   178     if ( res != KErrNone )
       
   179         {
       
   180         iLog -> Log( _L("Changing usb settings failed with value (cenrep): %d"), res );
       
   181         return res;
       
   182         }
       
   183     STIF_LOG2("SetUsbSettings OK: %d, %d", testUsbPersonalityId, testAskOnConnectionSetting);
       
   184   
       
   185     iCommandLine->SetCommandL( EApaCommandRun );
       
   186     iCommandLine->SetExecutableNameL( KUsbAppFileName );
       
   187 
       
   188     res = iApaLsSession.Connect();
       
   189     if ( res != KErrNone )
       
   190         {
       
   191         iLog->Log( _L("Connecting to application server failed with value: %d"), res );
       
   192         return res;
       
   193         }
       
   194     STIF_LOG("iApaLsSession.Connect OK");
       
   195 
       
   196     res = iApaLsSession.StartApp( *iCommandLine );
       
   197     if ( res != KErrNone )
       
   198         {
       
   199         iLog->Log( _L("Starting application failed with value: %d"), res );
       
   200         iApaLsSession.Close();
       
   201         return res;
       
   202         }
       
   203     STIF_LOG("iApaLsSession.StartApp OK");
       
   204     
       
   205     if ( !IsUsbAppRunning() )
       
   206         {
       
   207         iLog->Log( _L("Usb application launch error") );
       
   208         iApaLsSession.Close();
       
   209         return KErrGeneral;
       
   210         }
       
   211     STIF_LOG("IsUsbAppRunning OK");
       
   212 
       
   213     iLog->Log( _L("Usb application started") );
       
   214     return KErrNone;
       
   215     }
       
   216 
       
   217 // -----------------------------------------------------------------------------
       
   218 // CUSBUiApiTest::FinishTest
       
   219 // -----------------------------------------------------------------------------
       
   220 
       
   221 TInt CUSBUiApiTest::FinishTest( CStifItemParser& aItem )
       
   222     {
       
   223     TInt res;
       
   224     TPtrC optionName( KNullDesC );
       
   225     TUSBAppTestOption option;
       
   226     TBool isAppRunning;
       
   227     TPtrC expectedAskOnConnectionSettingName( KNullDesC );
       
   228     TPtrC expectedPersonalityIdSettingName( KNullDesC );
       
   229     TInt actualAskOnConnectionSetting;
       
   230     TInt expectedAskOnConnectionSetting;
       
   231     TInt actualPersonalityIdSetting;
       
   232     TInt expectedPersonalityIdSetting;
       
   233     
       
   234     res = aItem.GetString( _L( "FinishTest" ), optionName );   
       
   235     if ( res != KErrNone )
       
   236         {
       
   237         iLog -> Log( _L("GetString failed with value: %d"), res );
       
   238         iApaLsSession.Close();
       
   239         return res;
       
   240         }
       
   241     STIF_LOG("aItem.GetString optionName OK");
       
   242     
       
   243     res = GetTestOption( optionName, option );
       
   244     if ( res != KErrNone )
       
   245         {
       
   246         iLog -> Log( _L("GetTestOption failed with value: %d"), res );
       
   247         iApaLsSession.Close();
       
   248         return res;
       
   249         }
       
   250     STIF_LOG1("GetTestOption OK: %d",option);
       
   251     
       
   252     if ( option == EAskOnConnectionSettingChanged || option == EUsbConnectionModeSettingChanged )
       
   253         {
       
   254         res = aItem.GetNextString( expectedAskOnConnectionSettingName );   
       
   255         if ( res != KErrNone )
       
   256             {
       
   257             iLog -> Log( _L("GetNextString failed with value: %d"), res );
       
   258             iApaLsSession.Close();
       
   259             return res;
       
   260             }
       
   261         STIF_LOG("aItem.GetNextString expectedAskOnConnectionSettingName OK");
       
   262         
       
   263         res = GetAskOnConnectionOptionFromString( expectedAskOnConnectionSettingName, expectedAskOnConnectionSetting );   
       
   264         if ( res != KErrNone )
       
   265             {
       
   266             iLog -> Log( _L("GetAskOnConnectionOptionFromString failed with value: %d"), res );
       
   267             iApaLsSession.Close();
       
   268             return res;
       
   269             }
       
   270         STIF_LOG1("GetAskOnConnectionOptionFromString OK: %d", expectedAskOnConnectionSetting);
       
   271         
       
   272         res = aItem.GetNextString( expectedPersonalityIdSettingName );   
       
   273         if ( res != KErrNone )
       
   274             {
       
   275             iLog -> Log( _L("GetNextString failed with value: %d"), res );
       
   276             iApaLsSession.Close();
       
   277             return res;
       
   278             }
       
   279         STIF_LOG("aItem.GetNextString expectedPersonalityIdSettingName OK");
       
   280         
       
   281         res = GetUsbPersonalityOptionFromString( expectedPersonalityIdSettingName, expectedPersonalityIdSetting );   
       
   282         if ( res != KErrNone )
       
   283             {
       
   284             iLog -> Log( _L("GetUsbPersonalityOptionFromString failed with value: %d"), res );
       
   285             iApaLsSession.Close();
       
   286             return res;
       
   287             }
       
   288         STIF_LOG1("GetUsbPersonalityOptionFromString OK %d", expectedPersonalityIdSetting);
       
   289         }
       
   290     
       
   291     isAppRunning = IsUsbAppRunning();
       
   292 
       
   293     res = GetUsbSettings( actualPersonalityIdSetting, actualAskOnConnectionSetting );
       
   294     if ( res != KErrNone )
       
   295         {
       
   296         iLog -> Log( _L("Getting usb settings failed with value (cenrep): %d"), res );
       
   297         iApaLsSession.Close();
       
   298         return res;
       
   299         }
       
   300     STIF_LOG2("GetUsbSettings OK: %d, %d", actualPersonalityIdSetting, actualAskOnConnectionSetting);
       
   301     
       
   302     res = SetUsbSettings( iUsbPersonalityBackup, iAskOnConnectionSettingBackup );
       
   303     if ( res != KErrNone )
       
   304         iLog -> Log( _L("Failed to restore original usb settings!") );
       
   305     STIF_LOG2("SetUsbSettings OK: %d, %d", iUsbPersonalityBackup, iAskOnConnectionSettingBackup);
       
   306     
       
   307     iApaLsSession.Close();
       
   308     
       
   309     switch( option )
       
   310         {
       
   311         case EAskOnConnectionSettingChanged:       
       
   312         case EUsbConnectionModeSettingChanged:
       
   313             iLog -> Log( _L("Actual USB ask on connection setting value: %d, expected: %d"), actualAskOnConnectionSetting, expectedAskOnConnectionSetting );
       
   314             STIF_ASSERT_EQUALS( expectedAskOnConnectionSetting, actualAskOnConnectionSetting );
       
   315             iLog -> Log( _L("Actual USB personality setting value: %d, expected: %d"), actualPersonalityIdSetting, expectedPersonalityIdSetting );
       
   316             STIF_ASSERT_EQUALS( expectedPersonalityIdSetting, actualPersonalityIdSetting );   
       
   317             iLog -> Log( _L("USB app is running: %d, expected: %d"), isAppRunning, EFalse );
       
   318             STIF_ASSERT_FALSE( isAppRunning );
       
   319             break;
       
   320         case EAppCancelled:
       
   321             iLog -> Log( _L("USB app is running: %d, expected: %d"), isAppRunning, EFalse );
       
   322             STIF_ASSERT_FALSE( isAppRunning );
       
   323             break;
       
   324         default:
       
   325             return KErrNotFound;
       
   326         }
       
   327       
       
   328     iLog -> Log( _L("Test case passed!") );
       
   329     return KErrNone;
       
   330     }
       
   331 
       
   332 // -----------------------------------------------------------------------------
       
   333 // CUSBUiApiTest::FinishTest
       
   334 // -----------------------------------------------------------------------------
       
   335 
       
   336 TInt CUSBUiApiTest::GetTestOption( TPtrC aOptionString, TUSBAppTestOption& aOption )
       
   337     {    
       
   338     if ( !aOptionString.Compare( _L( "USB_CONN_MODE" ) ) )
       
   339         {
       
   340         aOption = EUsbConnectionModeSettingChanged;
       
   341         iLog -> Log( _L("Test type: EUsbConnectionModeSettingChanged") );
       
   342         }
       
   343     else if ( !aOptionString.Compare( _L( "ASK_ON_CONN" ) ) )
       
   344         {
       
   345         aOption = EAskOnConnectionSettingChanged;
       
   346         iLog -> Log( _L("Test type: EAskOnConnectionSettingChanged") );
       
   347         }
       
   348     else if ( !aOptionString.Compare( _L( "CANCELLED" ) ) )
       
   349         {
       
   350         aOption = EAppCancelled;
       
   351         iLog -> Log( _L("Test type: EAppCancelled") );
       
   352         }
       
   353     else
       
   354         {
       
   355         iLog -> Log( _L("Test type: not supported") );
       
   356         return KErrNotFound;
       
   357         }
       
   358  
       
   359     return KErrNone;
       
   360     }
       
   361 
       
   362 // -----------------------------------------------------------------------------
       
   363 // CUSBUiApiTest::GetAskOnConnectionOptionFromString
       
   364 // -----------------------------------------------------------------------------
       
   365 
       
   366 TInt CUSBUiApiTest::GetAskOnConnectionOptionFromString( TPtrC aOptionString, TInt& aOption )
       
   367     {    
       
   368     if ( !aOptionString.Compare( _L( "ON" ) ) )
       
   369         {
       
   370         aOption = KUsbWatcherChangeOnConnectionOn;
       
   371         iLog -> Log( _L("Ask on connection option: ON") );
       
   372         }
       
   373     else if ( !aOptionString.Compare( _L( "OFF" ) ) )
       
   374         {
       
   375         aOption = KUsbWatcherChangeOnConnectionOff;
       
   376         iLog -> Log( _L("Ask on connection option: OFF") );
       
   377         }
       
   378     else
       
   379         {
       
   380         iLog -> Log( _L("Ask on connection option: not supported") );
       
   381         return KErrNotFound;
       
   382         }
       
   383  
       
   384     return KErrNone;
       
   385     }
       
   386 
       
   387 // -----------------------------------------------------------------------------
       
   388 // CUSBUiApiTest::GetUsbPersonalityOptionFromString
       
   389 // -----------------------------------------------------------------------------
       
   390 
       
   391 TInt CUSBUiApiTest::GetUsbPersonalityOptionFromString( TPtrC aOptionString, TInt& aOption )
       
   392     {    
       
   393     if ( !aOptionString.Compare( _L( "PCSUITE" ) ) )
       
   394         {
       
   395         aOption = KUsbPersonalityIdPCSuiteMTP;
       
   396         iLog -> Log( _L("Usb personality option: KUsbPersonalityIdPCSuiteMTP") );
       
   397         }
       
   398     else if ( !aOptionString.Compare( _L( "MS" ) ) )
       
   399         {
       
   400         aOption = KUsbPersonalityIdMS;
       
   401         iLog -> Log( _L("Usb personality option: KUsbPersonalityIdMS") );
       
   402         }
       
   403     else if ( !aOptionString.Compare( _L( "PTP" ) ) )
       
   404         {
       
   405         aOption = KUsbPersonalityIdPTP;
       
   406         iLog -> Log( _L("Usb personality option: KUsbPersonalityIdPTP") );
       
   407         }
       
   408     else if ( !aOptionString.Compare( _L( "MTP" ) ) )
       
   409         {
       
   410         aOption = KUsbPersonalityIdMTP;
       
   411         iLog -> Log( _L("Usb personality option: KUsbPersonalityIdMTP") );
       
   412         }
       
   413     else
       
   414         {
       
   415         iLog -> Log( _L("Usb personality option: not supported") );
       
   416         return KErrNotFound;
       
   417         }
       
   418  
       
   419     return KErrNone;
       
   420     }
       
   421 
       
   422 // -----------------------------------------------------------------------------
       
   423 // CUSBUiApiTest::IsUsbAppRunning
       
   424 // -----------------------------------------------------------------------------
       
   425 
       
   426 TBool CUSBUiApiTest::IsUsbAppRunning()
       
   427     {
       
   428     TFindProcess processFinder;
       
   429     TFullName processName;
       
   430     TBool isAppRunning = EFalse;
       
   431     RProcess matchedProcess;
       
   432     
       
   433     while ( processFinder.Next( processName ) == KErrNone )
       
   434             {
       
   435             matchedProcess.Open( processFinder );
       
   436             if ( matchedProcess.SecureId() == iUsbAppSecureId )
       
   437                 isAppRunning = ETrue;
       
   438             }
       
   439     
       
   440     matchedProcess.Close();    
       
   441     return isAppRunning;
       
   442     }
       
   443 
       
   444 // -----------------------------------------------------------------------------
       
   445 // CUSBUiApiTest::GetUsbSettings
       
   446 // -----------------------------------------------------------------------------
       
   447 
       
   448 TInt CUSBUiApiTest::GetUsbSettings( TInt& aUsbPersonalityId, TInt& aAskOnConnectionSetting )
       
   449     {
       
   450     TInt res;   
       
   451     res = iRepository -> Get( KUsbWatcherChangeOnConnectionSetting, aAskOnConnectionSetting );
       
   452     if ( res != KErrNone )
       
   453         return res;
       
   454     
       
   455     res = iRepository -> Get( KUsbWatcherPersonality, aUsbPersonalityId );
       
   456     if ( res != KErrNone )
       
   457         return res;
       
   458     
       
   459     return KErrNone;
       
   460     }
       
   461 
       
   462 // -----------------------------------------------------------------------------
       
   463 // CUSBUiApiTest::SetUsbSettings
       
   464 // -----------------------------------------------------------------------------
       
   465 
       
   466 TInt CUSBUiApiTest::SetUsbSettings( TInt aUsbPersonalityId, TInt aAskOnConnectionSetting )
       
   467     {
       
   468     TInt res;   
       
   469     res = iRepository -> Set( KUsbWatcherChangeOnConnectionSetting, aAskOnConnectionSetting );
       
   470     if ( res != KErrNone )
       
   471         return res;
       
   472     
       
   473     res = iRepository -> Set( KUsbWatcherPersonality, aUsbPersonalityId );
       
   474     if ( res != KErrNone )
       
   475         return res;
       
   476     
       
   477     return KErrNone;
       
   478     }
       
   479 
       
   480 // -----------------------------------------------------------------------------
       
   481 // CUSBUiApiTest::TurnLightsOn
       
   482 // -----------------------------------------------------------------------------
       
   483 
       
   484 TInt CUSBUiApiTest::TurnLightsOn( CStifItemParser& aItem )
       
   485     {
       
   486     TInt res;
       
   487     res = RProperty::Set(KPSUidCoreApplicationUIs, KLightsControl, ELightsOn);
       
   488     return res;
       
   489     }
       
   490 // ========================== OTHER EXPORTED FUNCTIONS =========================
       
   491 // None
       
   492 
       
   493 //  [End of File] - Do not remove