contentstorage/casrv/causifscanner/tsrc/t_causifscanner/src/casrvtestutils.cpp
author Jaakko Haukipuro (Nokia-MS/Oulu) <Jaakko.Haukipuro@nokia.com>
Thu, 16 Sep 2010 12:11:40 +0100
changeset 117 c63ee96dbe5f
permissions -rw-r--r--
Missing activityfw and taskswitcher components - fix for Bug 3670

/*
* Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
* All rights reserved.
* This component and the accompanying materials are made available
* under the terms of "Eclipse Public License v1.0"
* which accompanies this distribution, and is available
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
*
* Initial Contributors:
* Nokia Corporation - initial contribution.
*
* Contributors:
*
* Description:
*
*/

#include <W32STD.H>
#include <e32property.h>
#include <cadef.h>
#include <badesca.h>
#include <apgtask.h>
#include <AknTaskList.h>
#include <apgcli.h>
#include <APACMDLN.h>
#include <EIKENV.h>


#include "castorageproxy.h"
#include "cainnerentry.h"
#include "cainnerquery.h"
#include "caarraycleanup.inl"
#include "casrvplugin.h"


#include "waitactive.h"
#include "casrvtestutils.h"
#include "testconsts.h"


//-----------------------------------------------------------------------
// CONSTRUCTION
// ---------------------------------------------------------------------------
//
CTestUtils* CTestUtils::NewL()
    {
    CTestUtils* self = CTestUtils::NewLC();
    CleanupStack::Pop();

    return self;
    }

// ---------------------------------------------------------------------------
//
// ---------------------------------------------------------------------------
//
CTestUtils* CTestUtils::NewLC()
    {
    CTestUtils* self = new( ELeave ) CTestUtils();
    CleanupStack::PushL( self );

    self->ConstructL();

    return self;
    }

// ---------------------------------------------------------------------------
// Destructor (virtual by CBase)
// ---------------------------------------------------------------------------
//
CTestUtils::~CTestUtils()
    {
    iFileManager->Delete(KTestDbDest);
    delete iFileManager;    iFileManager = NULL;
    iFs.Close();
    }

// ---------------------------------------------------------------------------
// Default constructor
// ---------------------------------------------------------------------------
//
CTestUtils::CTestUtils()
    {

    }

// ---------------------------------------------------------------------------
// Second phase construct
// ---------------------------------------------------------------------------
//
void CTestUtils::ConstructL()
    {
    User::LeaveIfError( iFs.Connect() );
    iFileManager = CFileMan::NewL( iFs );
    }

// ---------------------------------------------------------------------------
//
// ---------------------------------------------------------------------------
//
void CTestUtils::WaitL(TInt aMicroSec)
    {
    CWaitActive* wait = CWaitActive::NewL();
    wait->Wait(aMicroSec);
    delete wait;
    }

// ---------------------------------------------------------------------------
// CTestUtils::GetRProperty
// ---------------------------------------------------------------------------
//
TInt CTestUtils::GetRProperty( TUid aCategory, TUint aKey, TDes& aValue )
    {
    return RProperty::Get( aCategory, aKey, aValue );
    }

// ---------------------------------------------------------------------------
// CTestUtils::GetRProperty
// ---------------------------------------------------------------------------
//
TInt CTestUtils::GetRProperty( TUid aCategory, TUint aKey, TInt& aValue )
    {
    return RProperty::Get( aCategory, aKey, aValue );
    }

// ---------------------------------------------------------------------------
// CTestUtils::SetRProperty
// ---------------------------------------------------------------------------
//
TInt CTestUtils::SetRProperty( TUid aCategory, TUint aKey, const TDesC& aValue )
    {
    return RProperty::Set( aCategory, aKey, aValue );
    }

// ---------------------------------------------------------------------------
// CTestUtils::SetRProperty
// ---------------------------------------------------------------------------
//
TInt CTestUtils::SetRProperty( TUid aCategory, TUint aKey, TInt aValue )
    {
    return RProperty::Set( aCategory, aKey, aValue );
    }


//-----------------------------------------------------------------------
//
//-----------------------------------------------------------------------
TInt CTestUtils::Copy( const TDesC& aSource, const TDesC& aDest )
    {
    return iFileManager->Copy(aSource, aDest);
    }

//-----------------------------------------------------------------------
//
//-----------------------------------------------------------------------
TInt CTestUtils::CopyDb()
    {
    return Copy(KTestDbSource, KTestDbDest);
    }

//-----------------------------------------------------------------------
//
//-----------------------------------------------------------------------
TBool CTestUtils::AppExistsInStorageL( TInt aUid, CCaStorageProxy* aStorage)
    {
    TBool exists(EFalse);
    CCaInnerQuery* satAppQuery = CCaInnerQuery::NewLC();
    satAppQuery->SetUid( aUid );
    RPointerArray<CCaInnerEntry> resultArray;
    CleanupResetAndDestroyPushL( resultArray );
    aStorage->GetEntriesL( satAppQuery, resultArray );

    if ( resultArray.Count() )
        {
        CCaInnerEntry* dbg = resultArray[0];
        exists = ETrue;
        }

    CleanupStack::PopAndDestroy( &resultArray );
    CleanupStack::PopAndDestroy( satAppQuery );

    return exists;
    }

//-----------------------------------------------------------------------
//
//-----------------------------------------------------------------------
CCaInnerEntry* CTestUtils::GetAppEntryL( TInt aUid, CCaStorageProxy* aStorage)
    {
    CCaInnerEntry* entry = NULL;
    CCaInnerQuery* satAppQuery = CCaInnerQuery::NewLC();
    satAppQuery->SetUid( aUid );
    RPointerArray<CCaInnerEntry> resultArray;
    CleanupResetAndDestroyPushL( resultArray );
    aStorage->GetEntriesL( satAppQuery, resultArray );

    if ( resultArray.Count() )
        {
        entry = resultArray[0];
        resultArray.Remove(0);
        }

    CleanupStack::PopAndDestroy( &resultArray );
    CleanupStack::PopAndDestroy( satAppQuery );

    return entry;
    }

//-----------------------------------------------------------------------
//
//-----------------------------------------------------------------------
TInt CTestUtils::AppsWithFlagsOffL( TInt aFlags, CCaStorageProxy* aStorage)
    {
    CCaInnerQuery* satAppQuery = CCaInnerQuery::NewLC();
    CDesC16ArrayFlat* appType =
                        new (ELeave) CDesC16ArrayFlat( 1 );
    CleanupStack::PushL( appType );
    appType->AppendL( KCaTypeApp );
    satAppQuery->SetEntryTypeNames( appType );

    satAppQuery->SetFlagsOff( aFlags );
    RPointerArray<CCaInnerEntry> resultArray;
    CleanupResetAndDestroyPushL( resultArray );
    aStorage->GetEntriesL( satAppQuery, resultArray );

    TInt count = resultArray.Count();
    CleanupStack::PopAndDestroy( &resultArray );
    CleanupStack::Pop( appType );
    CleanupStack::PopAndDestroy( satAppQuery );

    return count;
    }


//-----------------------------------------------------------------------
//
//-----------------------------------------------------------------------
TInt CTestUtils::GetAppFlagsL( TInt aUid, CCaStorageProxy* aStorage)
    {
    TInt flags(0);
    CCaInnerQuery* satAppQuery = CCaInnerQuery::NewLC();
    satAppQuery->SetUid( aUid );
    RPointerArray<CCaInnerEntry> resultArray;
    CleanupResetAndDestroyPushL( resultArray );
    aStorage->GetEntriesL( satAppQuery, resultArray );

    if ( resultArray.Count() )
        {
        flags = resultArray[0]->GetFlags();
        }

    CleanupStack::PopAndDestroy( &resultArray );
    CleanupStack::PopAndDestroy( satAppQuery );

    return flags;
    }

//-----------------------------------------------------------------------
//
//-----------------------------------------------------------------------
TInt CTestUtils::GetPackageFlagsL(
        const TDesC& aSource, CCaStorageProxy* aStorage)
    {
    TInt flags(0);

    CCaInnerQuery* satAppQuery = CCaInnerQuery::NewLC();
    CDesC16ArrayFlat* appType = new ( ELeave ) CDesC16ArrayFlat( 1 );
    CleanupStack::PushL( appType );
    appType->AppendL( KCaTypePackage );
    satAppQuery->SetEntryTypeNames( appType );
    CleanupStack::Pop( appType );

    RPointerArray<CCaInnerEntry> resultArray;
    CleanupResetAndDestroyPushL( resultArray );
    aStorage->GetEntriesL( satAppQuery, resultArray );

    for( TInt i = 0; i < resultArray.Count(); i++ )
        {
        if ( resultArray[i]->GetText().Compare( aSource ) == KErrNone )
            {
            flags = resultArray[i]->GetFlags();
            }
        }

    CleanupStack::PopAndDestroy( &resultArray );
    CleanupStack::PopAndDestroy( satAppQuery );

    return flags;
    }


//-----------------------------------------------------------------------
//
//-----------------------------------------------------------------------
void CTestUtils::RemoveAppL( TInt aUid, CCaStorageProxy* aStorage)
    {
    CCaInnerQuery* appQuery = CCaInnerQuery::NewLC();
    appQuery->SetUid( aUid );
    RPointerArray<CCaInnerEntry> resultArray;
    CleanupResetAndDestroyPushL( resultArray );
    aStorage->GetEntriesL( appQuery, resultArray );

    if ( resultArray.Count() )
        {
        RArray<TInt> idsToRemove;
        CleanupClosePushL(idsToRemove);
        idsToRemove.AppendL( resultArray[0]->GetId() );
        aStorage->RemoveL(idsToRemove);
        CleanupStack::PopAndDestroy( &idsToRemove );
        }

    CleanupStack::PopAndDestroy( &resultArray );
    CleanupStack::PopAndDestroy( appQuery );
    }

//-----------------------------------------------------------------------
//
//-----------------------------------------------------------------------

void CTestUtils::CopyMmcHistory()
{
//    iFileManager->Copy( KTestMmcHistSource, KTestMmcHistDest );
}


//-----------------------------------------------------------------------


//-----------------------------------------------------------------------
//
//-----------------------------------------------------------------------
TUint CTestUtils::FindAppUidL( const TDesC& aName, CCaStorageProxy* aStorage )
    {
    CCaInnerQuery* satAppQuery = CCaInnerQuery::NewLC();
    CDesC16ArrayFlat* appType =
                        new (ELeave) CDesC16ArrayFlat( 1 );
    CleanupStack::PushL( appType );
    appType->AppendL( KCaTypeApp );
    satAppQuery->SetEntryTypeNames( appType );

    RPointerArray<CCaInnerEntry> resultArray;
    CleanupResetAndDestroyPushL( resultArray );
    aStorage->GetEntriesL( satAppQuery, resultArray );

    TUint appUid(0);
    for ( TInt i = 0; i < resultArray.Count(); i++ )
        {
        if ( resultArray[i]->GetText() == aName )
            {
            appUid = resultArray[i]->GetUid();
            }
        }
    CleanupStack::PopAndDestroy( &resultArray );
    CleanupStack::Pop( appType );
    CleanupStack::PopAndDestroy( satAppQuery );

    return appUid;
    }

// -----------------------------------------------------------------------------
//
TInt CTestUtils::SimpleCloseTaskL(TInt aUid)
    {
    RWsSession session;
    TInt error = session.Connect();
    TUid uidApp( TUid::Uid( aUid )) ;
    TApaTaskList taskList( session );
    TApaTask task = taskList.FindApp( uidApp );

    if( task.Exists() )
        {
        task.EndTask();
        }
    else
        {
        error = KErrNotFound;
        }

    WaitL( 4000000 );
    session.Close();
    WaitL( 1000000  );

    return error;
    }

// ----------------------------------------------------------------------------
//
void CTestUtils::InstallFinishedL()
    {
    iActiveWait->AsyncStop();
    }


// ---------------------------------------------------------
// CTestUtils::LaunchApplicationL
// ---------------------------------------------------------

CCaSrvPlugin* CTestUtils::LoadPluginL( TUid aImplUid, TPluginParams aPluginParams )
    {
    RImplInfoPtrArray infoArray;

    // Note that a special cleanup function is required to reset and destroy
    // all items in the array, and then close it.
    CleanupResetAndDestroyPushL( infoArray );
    CCaSrvPlugin::ListAllImplementationsL( infoArray );

    // Loop through each info for each implementation
    // and create and use each in turn
    CCaSrvPlugin* plug = NULL;
    for( TInt i = 0; i < infoArray.Count(); i++ )
        {
        // Slice off first sub-section in the data section
        TUid current_plugin = infoArray[i]->ImplementationUid();
        if ( current_plugin == aImplUid )
            {
            plug = CCaSrvPlugin::NewL( current_plugin, &aPluginParams );
            break;
            }
        }
    CleanupStack::PopAndDestroy( &infoArray );
    return plug;
    }




// ---------------------------------------------------------
// CTestUtils::LaunchApplicationL
// ---------------------------------------------------------
//
TInt CTestUtils::LaunchApplicationL( const TUid aUid )
    {
    RWsSession wsSession;
    User::LeaveIfError( wsSession.Connect() );
    CleanupClosePushL<RWsSession>( wsSession );

    CAknTaskList* taskList = CAknTaskList::NewL( wsSession );
    TApaTask task = taskList->FindRootApp( aUid );
    delete taskList;

    if ( task.Exists() )
        {
        task.BringToForeground();
        }
    else
        {
        TApaAppInfo appInfo;
        TApaAppCapabilityBuf capabilityBuf;
        RApaLsSession appArcSession;
        User::LeaveIfError( appArcSession.Connect() );
        CleanupClosePushL<RApaLsSession>( appArcSession );

        User::LeaveIfError( appArcSession.GetAppInfo( appInfo, aUid ) );
        User::LeaveIfError( appArcSession.GetAppCapability( capabilityBuf, aUid ) );

        TApaAppCapability& caps = capabilityBuf();
        TFileName appName = appInfo.iFullName;
        CApaCommandLine* cmdLine = CApaCommandLine::NewLC();
        cmdLine->SetExecutableNameL( appName );

        if ( caps.iLaunchInBackground )
            {
            cmdLine->SetCommandL( EApaCommandBackground );
            }
        else
            {
            cmdLine->SetCommandL( EApaCommandRun );
            }

        User::LeaveIfError( appArcSession.StartApp( *cmdLine ) );

        CleanupStack::PopAndDestroy( cmdLine );
        CleanupStack::PopAndDestroy( &appArcSession );
        }
    CleanupStack::PopAndDestroy( &wsSession );

    //verification if app has really launched
    WaitL(20000000);
    RWsSession verWsSession;
    User::LeaveIfError( verWsSession.Connect() );
    CleanupClosePushL<RWsSession>( verWsSession );

    CAknTaskList* verTaskList = CAknTaskList::NewL( verWsSession );
    TApaTask verTask = verTaskList->FindRootApp( aUid );
    delete verTaskList;

    TInt result(KErrGeneral);
    if ( verTask.Exists() )
        {
        result = KErrNone;
        }
    CleanupStack::PopAndDestroy( &verWsSession );
    return result;
    }