contentstorage/casrv/causifscanner/tsrc/t_causifscanner/src/casrvtestutils.cpp
changeset 117 c63ee96dbe5f
equal deleted inserted replaced
115:3ab5c078b490 117:c63ee96dbe5f
       
     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:
       
    15 *
       
    16 */
       
    17 
       
    18 #include <W32STD.H>
       
    19 #include <e32property.h>
       
    20 #include <cadef.h>
       
    21 #include <badesca.h>
       
    22 #include <apgtask.h>
       
    23 #include <AknTaskList.h>
       
    24 #include <apgcli.h>
       
    25 #include <APACMDLN.h>
       
    26 #include <EIKENV.h>
       
    27 
       
    28 
       
    29 #include "castorageproxy.h"
       
    30 #include "cainnerentry.h"
       
    31 #include "cainnerquery.h"
       
    32 #include "caarraycleanup.inl"
       
    33 #include "casrvplugin.h"
       
    34 
       
    35 
       
    36 #include "waitactive.h"
       
    37 #include "casrvtestutils.h"
       
    38 #include "testconsts.h"
       
    39 
       
    40 
       
    41 //-----------------------------------------------------------------------
       
    42 // CONSTRUCTION
       
    43 // ---------------------------------------------------------------------------
       
    44 //
       
    45 CTestUtils* CTestUtils::NewL()
       
    46     {
       
    47     CTestUtils* self = CTestUtils::NewLC();
       
    48     CleanupStack::Pop();
       
    49 
       
    50     return self;
       
    51     }
       
    52 
       
    53 // ---------------------------------------------------------------------------
       
    54 //
       
    55 // ---------------------------------------------------------------------------
       
    56 //
       
    57 CTestUtils* CTestUtils::NewLC()
       
    58     {
       
    59     CTestUtils* self = new( ELeave ) CTestUtils();
       
    60     CleanupStack::PushL( self );
       
    61 
       
    62     self->ConstructL();
       
    63 
       
    64     return self;
       
    65     }
       
    66 
       
    67 // ---------------------------------------------------------------------------
       
    68 // Destructor (virtual by CBase)
       
    69 // ---------------------------------------------------------------------------
       
    70 //
       
    71 CTestUtils::~CTestUtils()
       
    72     {
       
    73     iFileManager->Delete(KTestDbDest);
       
    74     delete iFileManager;    iFileManager = NULL;
       
    75     iFs.Close();
       
    76     }
       
    77 
       
    78 // ---------------------------------------------------------------------------
       
    79 // Default constructor
       
    80 // ---------------------------------------------------------------------------
       
    81 //
       
    82 CTestUtils::CTestUtils()
       
    83     {
       
    84 
       
    85     }
       
    86 
       
    87 // ---------------------------------------------------------------------------
       
    88 // Second phase construct
       
    89 // ---------------------------------------------------------------------------
       
    90 //
       
    91 void CTestUtils::ConstructL()
       
    92     {
       
    93     User::LeaveIfError( iFs.Connect() );
       
    94     iFileManager = CFileMan::NewL( iFs );
       
    95     }
       
    96 
       
    97 // ---------------------------------------------------------------------------
       
    98 //
       
    99 // ---------------------------------------------------------------------------
       
   100 //
       
   101 void CTestUtils::WaitL(TInt aMicroSec)
       
   102     {
       
   103     CWaitActive* wait = CWaitActive::NewL();
       
   104     wait->Wait(aMicroSec);
       
   105     delete wait;
       
   106     }
       
   107 
       
   108 // ---------------------------------------------------------------------------
       
   109 // CTestUtils::GetRProperty
       
   110 // ---------------------------------------------------------------------------
       
   111 //
       
   112 TInt CTestUtils::GetRProperty( TUid aCategory, TUint aKey, TDes& aValue )
       
   113     {
       
   114     return RProperty::Get( aCategory, aKey, aValue );
       
   115     }
       
   116 
       
   117 // ---------------------------------------------------------------------------
       
   118 // CTestUtils::GetRProperty
       
   119 // ---------------------------------------------------------------------------
       
   120 //
       
   121 TInt CTestUtils::GetRProperty( TUid aCategory, TUint aKey, TInt& aValue )
       
   122     {
       
   123     return RProperty::Get( aCategory, aKey, aValue );
       
   124     }
       
   125 
       
   126 // ---------------------------------------------------------------------------
       
   127 // CTestUtils::SetRProperty
       
   128 // ---------------------------------------------------------------------------
       
   129 //
       
   130 TInt CTestUtils::SetRProperty( TUid aCategory, TUint aKey, const TDesC& aValue )
       
   131     {
       
   132     return RProperty::Set( aCategory, aKey, aValue );
       
   133     }
       
   134 
       
   135 // ---------------------------------------------------------------------------
       
   136 // CTestUtils::SetRProperty
       
   137 // ---------------------------------------------------------------------------
       
   138 //
       
   139 TInt CTestUtils::SetRProperty( TUid aCategory, TUint aKey, TInt aValue )
       
   140     {
       
   141     return RProperty::Set( aCategory, aKey, aValue );
       
   142     }
       
   143 
       
   144 
       
   145 //-----------------------------------------------------------------------
       
   146 //
       
   147 //-----------------------------------------------------------------------
       
   148 TInt CTestUtils::Copy( const TDesC& aSource, const TDesC& aDest )
       
   149     {
       
   150     return iFileManager->Copy(aSource, aDest);
       
   151     }
       
   152 
       
   153 //-----------------------------------------------------------------------
       
   154 //
       
   155 //-----------------------------------------------------------------------
       
   156 TInt CTestUtils::CopyDb()
       
   157     {
       
   158     return Copy(KTestDbSource, KTestDbDest);
       
   159     }
       
   160 
       
   161 //-----------------------------------------------------------------------
       
   162 //
       
   163 //-----------------------------------------------------------------------
       
   164 TBool CTestUtils::AppExistsInStorageL( TInt aUid, CCaStorageProxy* aStorage)
       
   165     {
       
   166     TBool exists(EFalse);
       
   167     CCaInnerQuery* satAppQuery = CCaInnerQuery::NewLC();
       
   168     satAppQuery->SetUid( aUid );
       
   169     RPointerArray<CCaInnerEntry> resultArray;
       
   170     CleanupResetAndDestroyPushL( resultArray );
       
   171     aStorage->GetEntriesL( satAppQuery, resultArray );
       
   172 
       
   173     if ( resultArray.Count() )
       
   174         {
       
   175         CCaInnerEntry* dbg = resultArray[0];
       
   176         exists = ETrue;
       
   177         }
       
   178 
       
   179     CleanupStack::PopAndDestroy( &resultArray );
       
   180     CleanupStack::PopAndDestroy( satAppQuery );
       
   181 
       
   182     return exists;
       
   183     }
       
   184 
       
   185 //-----------------------------------------------------------------------
       
   186 //
       
   187 //-----------------------------------------------------------------------
       
   188 CCaInnerEntry* CTestUtils::GetAppEntryL( TInt aUid, CCaStorageProxy* aStorage)
       
   189     {
       
   190     CCaInnerEntry* entry = NULL;
       
   191     CCaInnerQuery* satAppQuery = CCaInnerQuery::NewLC();
       
   192     satAppQuery->SetUid( aUid );
       
   193     RPointerArray<CCaInnerEntry> resultArray;
       
   194     CleanupResetAndDestroyPushL( resultArray );
       
   195     aStorage->GetEntriesL( satAppQuery, resultArray );
       
   196 
       
   197     if ( resultArray.Count() )
       
   198         {
       
   199         entry = resultArray[0];
       
   200         resultArray.Remove(0);
       
   201         }
       
   202 
       
   203     CleanupStack::PopAndDestroy( &resultArray );
       
   204     CleanupStack::PopAndDestroy( satAppQuery );
       
   205 
       
   206     return entry;
       
   207     }
       
   208 
       
   209 //-----------------------------------------------------------------------
       
   210 //
       
   211 //-----------------------------------------------------------------------
       
   212 TInt CTestUtils::AppsWithFlagsOffL( TInt aFlags, CCaStorageProxy* aStorage)
       
   213     {
       
   214     CCaInnerQuery* satAppQuery = CCaInnerQuery::NewLC();
       
   215     CDesC16ArrayFlat* appType =
       
   216                         new (ELeave) CDesC16ArrayFlat( 1 );
       
   217     CleanupStack::PushL( appType );
       
   218     appType->AppendL( KCaTypeApp );
       
   219     satAppQuery->SetEntryTypeNames( appType );
       
   220 
       
   221     satAppQuery->SetFlagsOff( aFlags );
       
   222     RPointerArray<CCaInnerEntry> resultArray;
       
   223     CleanupResetAndDestroyPushL( resultArray );
       
   224     aStorage->GetEntriesL( satAppQuery, resultArray );
       
   225 
       
   226     TInt count = resultArray.Count();
       
   227     CleanupStack::PopAndDestroy( &resultArray );
       
   228     CleanupStack::Pop( appType );
       
   229     CleanupStack::PopAndDestroy( satAppQuery );
       
   230 
       
   231     return count;
       
   232     }
       
   233 
       
   234 
       
   235 //-----------------------------------------------------------------------
       
   236 //
       
   237 //-----------------------------------------------------------------------
       
   238 TInt CTestUtils::GetAppFlagsL( TInt aUid, CCaStorageProxy* aStorage)
       
   239     {
       
   240     TInt flags(0);
       
   241     CCaInnerQuery* satAppQuery = CCaInnerQuery::NewLC();
       
   242     satAppQuery->SetUid( aUid );
       
   243     RPointerArray<CCaInnerEntry> resultArray;
       
   244     CleanupResetAndDestroyPushL( resultArray );
       
   245     aStorage->GetEntriesL( satAppQuery, resultArray );
       
   246 
       
   247     if ( resultArray.Count() )
       
   248         {
       
   249         flags = resultArray[0]->GetFlags();
       
   250         }
       
   251 
       
   252     CleanupStack::PopAndDestroy( &resultArray );
       
   253     CleanupStack::PopAndDestroy( satAppQuery );
       
   254 
       
   255     return flags;
       
   256     }
       
   257 
       
   258 //-----------------------------------------------------------------------
       
   259 //
       
   260 //-----------------------------------------------------------------------
       
   261 TInt CTestUtils::GetPackageFlagsL(
       
   262         const TDesC& aSource, CCaStorageProxy* aStorage)
       
   263     {
       
   264     TInt flags(0);
       
   265 
       
   266     CCaInnerQuery* satAppQuery = CCaInnerQuery::NewLC();
       
   267     CDesC16ArrayFlat* appType = new ( ELeave ) CDesC16ArrayFlat( 1 );
       
   268     CleanupStack::PushL( appType );
       
   269     appType->AppendL( KCaTypePackage );
       
   270     satAppQuery->SetEntryTypeNames( appType );
       
   271     CleanupStack::Pop( appType );
       
   272 
       
   273     RPointerArray<CCaInnerEntry> resultArray;
       
   274     CleanupResetAndDestroyPushL( resultArray );
       
   275     aStorage->GetEntriesL( satAppQuery, resultArray );
       
   276 
       
   277     for( TInt i = 0; i < resultArray.Count(); i++ )
       
   278         {
       
   279         if ( resultArray[i]->GetText().Compare( aSource ) == KErrNone )
       
   280             {
       
   281             flags = resultArray[i]->GetFlags();
       
   282             }
       
   283         }
       
   284 
       
   285     CleanupStack::PopAndDestroy( &resultArray );
       
   286     CleanupStack::PopAndDestroy( satAppQuery );
       
   287 
       
   288     return flags;
       
   289     }
       
   290 
       
   291 
       
   292 //-----------------------------------------------------------------------
       
   293 //
       
   294 //-----------------------------------------------------------------------
       
   295 void CTestUtils::RemoveAppL( TInt aUid, CCaStorageProxy* aStorage)
       
   296     {
       
   297     CCaInnerQuery* appQuery = CCaInnerQuery::NewLC();
       
   298     appQuery->SetUid( aUid );
       
   299     RPointerArray<CCaInnerEntry> resultArray;
       
   300     CleanupResetAndDestroyPushL( resultArray );
       
   301     aStorage->GetEntriesL( appQuery, resultArray );
       
   302 
       
   303     if ( resultArray.Count() )
       
   304         {
       
   305         RArray<TInt> idsToRemove;
       
   306         CleanupClosePushL(idsToRemove);
       
   307         idsToRemove.AppendL( resultArray[0]->GetId() );
       
   308         aStorage->RemoveL(idsToRemove);
       
   309         CleanupStack::PopAndDestroy( &idsToRemove );
       
   310         }
       
   311 
       
   312     CleanupStack::PopAndDestroy( &resultArray );
       
   313     CleanupStack::PopAndDestroy( appQuery );
       
   314     }
       
   315 
       
   316 //-----------------------------------------------------------------------
       
   317 //
       
   318 //-----------------------------------------------------------------------
       
   319 
       
   320 void CTestUtils::CopyMmcHistory()
       
   321 {
       
   322 //    iFileManager->Copy( KTestMmcHistSource, KTestMmcHistDest );
       
   323 }
       
   324 
       
   325 
       
   326 //-----------------------------------------------------------------------
       
   327 
       
   328 
       
   329 //-----------------------------------------------------------------------
       
   330 //
       
   331 //-----------------------------------------------------------------------
       
   332 TUint CTestUtils::FindAppUidL( const TDesC& aName, CCaStorageProxy* aStorage )
       
   333     {
       
   334     CCaInnerQuery* satAppQuery = CCaInnerQuery::NewLC();
       
   335     CDesC16ArrayFlat* appType =
       
   336                         new (ELeave) CDesC16ArrayFlat( 1 );
       
   337     CleanupStack::PushL( appType );
       
   338     appType->AppendL( KCaTypeApp );
       
   339     satAppQuery->SetEntryTypeNames( appType );
       
   340 
       
   341     RPointerArray<CCaInnerEntry> resultArray;
       
   342     CleanupResetAndDestroyPushL( resultArray );
       
   343     aStorage->GetEntriesL( satAppQuery, resultArray );
       
   344 
       
   345     TUint appUid(0);
       
   346     for ( TInt i = 0; i < resultArray.Count(); i++ )
       
   347         {
       
   348         if ( resultArray[i]->GetText() == aName )
       
   349             {
       
   350             appUid = resultArray[i]->GetUid();
       
   351             }
       
   352         }
       
   353     CleanupStack::PopAndDestroy( &resultArray );
       
   354     CleanupStack::Pop( appType );
       
   355     CleanupStack::PopAndDestroy( satAppQuery );
       
   356 
       
   357     return appUid;
       
   358     }
       
   359 
       
   360 // -----------------------------------------------------------------------------
       
   361 //
       
   362 TInt CTestUtils::SimpleCloseTaskL(TInt aUid)
       
   363     {
       
   364     RWsSession session;
       
   365     TInt error = session.Connect();
       
   366     TUid uidApp( TUid::Uid( aUid )) ;
       
   367     TApaTaskList taskList( session );
       
   368     TApaTask task = taskList.FindApp( uidApp );
       
   369 
       
   370     if( task.Exists() )
       
   371         {
       
   372         task.EndTask();
       
   373         }
       
   374     else
       
   375         {
       
   376         error = KErrNotFound;
       
   377         }
       
   378 
       
   379     WaitL( 4000000 );
       
   380     session.Close();
       
   381     WaitL( 1000000  );
       
   382 
       
   383     return error;
       
   384     }
       
   385 
       
   386 // ----------------------------------------------------------------------------
       
   387 //
       
   388 void CTestUtils::InstallFinishedL()
       
   389     {
       
   390     iActiveWait->AsyncStop();
       
   391     }
       
   392 
       
   393 
       
   394 // ---------------------------------------------------------
       
   395 // CTestUtils::LaunchApplicationL
       
   396 // ---------------------------------------------------------
       
   397 
       
   398 CCaSrvPlugin* CTestUtils::LoadPluginL( TUid aImplUid, TPluginParams aPluginParams )
       
   399     {
       
   400     RImplInfoPtrArray infoArray;
       
   401 
       
   402     // Note that a special cleanup function is required to reset and destroy
       
   403     // all items in the array, and then close it.
       
   404     CleanupResetAndDestroyPushL( infoArray );
       
   405     CCaSrvPlugin::ListAllImplementationsL( infoArray );
       
   406 
       
   407     // Loop through each info for each implementation
       
   408     // and create and use each in turn
       
   409     CCaSrvPlugin* plug = NULL;
       
   410     for( TInt i = 0; i < infoArray.Count(); i++ )
       
   411         {
       
   412         // Slice off first sub-section in the data section
       
   413         TUid current_plugin = infoArray[i]->ImplementationUid();
       
   414         if ( current_plugin == aImplUid )
       
   415             {
       
   416             plug = CCaSrvPlugin::NewL( current_plugin, &aPluginParams );
       
   417             break;
       
   418             }
       
   419         }
       
   420     CleanupStack::PopAndDestroy( &infoArray );
       
   421     return plug;
       
   422     }
       
   423 
       
   424 
       
   425 
       
   426 
       
   427 // ---------------------------------------------------------
       
   428 // CTestUtils::LaunchApplicationL
       
   429 // ---------------------------------------------------------
       
   430 //
       
   431 TInt CTestUtils::LaunchApplicationL( const TUid aUid )
       
   432     {
       
   433     RWsSession wsSession;
       
   434     User::LeaveIfError( wsSession.Connect() );
       
   435     CleanupClosePushL<RWsSession>( wsSession );
       
   436 
       
   437     CAknTaskList* taskList = CAknTaskList::NewL( wsSession );
       
   438     TApaTask task = taskList->FindRootApp( aUid );
       
   439     delete taskList;
       
   440 
       
   441     if ( task.Exists() )
       
   442         {
       
   443         task.BringToForeground();
       
   444         }
       
   445     else
       
   446         {
       
   447         TApaAppInfo appInfo;
       
   448         TApaAppCapabilityBuf capabilityBuf;
       
   449         RApaLsSession appArcSession;
       
   450         User::LeaveIfError( appArcSession.Connect() );
       
   451         CleanupClosePushL<RApaLsSession>( appArcSession );
       
   452 
       
   453         User::LeaveIfError( appArcSession.GetAppInfo( appInfo, aUid ) );
       
   454         User::LeaveIfError( appArcSession.GetAppCapability( capabilityBuf, aUid ) );
       
   455 
       
   456         TApaAppCapability& caps = capabilityBuf();
       
   457         TFileName appName = appInfo.iFullName;
       
   458         CApaCommandLine* cmdLine = CApaCommandLine::NewLC();
       
   459         cmdLine->SetExecutableNameL( appName );
       
   460 
       
   461         if ( caps.iLaunchInBackground )
       
   462             {
       
   463             cmdLine->SetCommandL( EApaCommandBackground );
       
   464             }
       
   465         else
       
   466             {
       
   467             cmdLine->SetCommandL( EApaCommandRun );
       
   468             }
       
   469 
       
   470         User::LeaveIfError( appArcSession.StartApp( *cmdLine ) );
       
   471 
       
   472         CleanupStack::PopAndDestroy( cmdLine );
       
   473         CleanupStack::PopAndDestroy( &appArcSession );
       
   474         }
       
   475     CleanupStack::PopAndDestroy( &wsSession );
       
   476 
       
   477     //verification if app has really launched
       
   478     WaitL(20000000);
       
   479     RWsSession verWsSession;
       
   480     User::LeaveIfError( verWsSession.Connect() );
       
   481     CleanupClosePushL<RWsSession>( verWsSession );
       
   482 
       
   483     CAknTaskList* verTaskList = CAknTaskList::NewL( verWsSession );
       
   484     TApaTask verTask = verTaskList->FindRootApp( aUid );
       
   485     delete verTaskList;
       
   486 
       
   487     TInt result(KErrGeneral);
       
   488     if ( verTask.Exists() )
       
   489         {
       
   490         result = KErrNone;
       
   491         }
       
   492     CleanupStack::PopAndDestroy( &verWsSession );
       
   493     return result;
       
   494     }
       
   495 
       
   496