contentstorage/cahandler/app/tsrc/t_caapphandler/src/T_caapphandlerplugin.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:
*
*/
/*
 * T_caapphandlerplugin.cpp
 *
 *  Created on: 2009-09-16
 *      Author:
 */

//  CLASS HEADER

#include <w32std.h>
#include <apgtask.h>
#include <apgcli.h>
#include <mmf/common/mmfcontrollerpluginresolver.h>
#include <usif/scr/scr.h>
#include <usif/sif/sifcommon.h>
#include <usif/scr/screntries.h>
#include <qservicemanager.h>
#include <QProcess>
#include <qservicemanager.h>

#include <cadefs.h>

#include "T_caapphandlerplugin.h"
#include "waitactive.h"
#include "catasklist.h"
#include "caapphandler.h"
#include "caapphandlerplugin.h"
#include "caentry.h"
#include "caclient_defines.h"
#include "t_cainstaller.h"

template <typename RClass>
struct RClassDeleter
{
    static inline void cleanup(RClass *ptr)
    {
        ptr->Close();
    }
};


typedef RClassDeleter< RBuf > RBufDeleter;
typedef QScopedPointer< RBuf, RBufDeleter > 
    RBufPointer;


QTM_USE_NAMESPACE

static const char caTypeApp[] = "application";
static const char caTypePackage[] = "package";
static const char caTypeWidget[] = "widget";
static const char caAttrView[] = "view";
static const char caCmdClose[] = "close";
static const char caAttrWindowGroupId[] = "window_group_id";
static const char caAttrComponentId[] = "component_id";
static const char caUidAttribute[] = "application:uid";

T_CaHandler::T_CaHandler():
    iAppHandler(NULL)
{
}

void T_CaHandler::cleanup()
{
    delete iAppHandler;
    iAppHandler = NULL;
}

// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
//
void T_CaHandler::initTestCase()
{
    QStringList pluginPaths;
    QString xmlName("caapphandlerplugin.xml");

    pluginPaths.append("c:/resource/qt/plugins/commandhandler");
    pluginPaths.append("z:/resource/qt/plugins/commandhandler");

    QServiceManager serviceManager;
    foreach (QString path, pluginPaths) {
        QApplication::addLibraryPath(path);
        QDir dir(path);
        if (dir.exists(xmlName)) {
            serviceManager.addService(dir.absoluteFilePath(xmlName));
        }
    }

    Usif::RSoftwareInstall sif;
    CleanupClosePushL( sif );
    User::LeaveIfError( sif.Connect() );
    
    // Install the component
    _LIT16( KHelloJarPath, "c:\\testing\\data\\t_caapphandler\\installs\\hello.jar" );
    Usif::COpaqueNamedParams* arguments = NULL;
    Usif::COpaqueNamedParams* results = NULL;
    arguments = Usif::COpaqueNamedParams::NewLC();
    results = Usif::COpaqueNamedParams::NewLC();
    arguments->AddIntL( Usif::KSifInParam_InstallSilently, 1 );
    TRequestStatus status;
    sif.Install( KHelloJarPath, *arguments, *results, status, EFalse );
    User::WaitForRequest( status );
    User::LeaveIfError( status.Int() );
    
    CleanupStack::PopAndDestroy( results );
    CleanupStack::PopAndDestroy( arguments );
    
    // Disconnect from the SIF server
    CleanupStack::PopAndDestroy( &sif );
}

// ---------------------------------------------------------------------------
//
// ---------------------------------------------------------------------------
//
void T_CaHandler::appHandlerConstruction()
{
    iAppHandler = new CaAppHandler();
    QVERIFY(iAppHandler);
}


// ---------------------------------------------------------------------------
//
// ---------------------------------------------------------------------------
//
void T_CaHandler::appHandlerMultipleLoad()
{
    RPointerArray<CaAppHandler> handlerArray;

    bool foundNull(false);
    TRAPD(err,
          CleanupResetAndDestroyPushL(handlerArray);
    for (TInt i(0); i < 10; ++i) {
    handlerArray.AppendL(new CaAppHandler());
        if (handlerArray[i] == 0) {
            foundNull = true;
            break;
        }
    }
    CleanupStack::PopAndDestroy(&handlerArray);
         );
    QCOMPARE(err, KErrNone);
    QVERIFY(!foundNull);
}

// ---------------------------------------------------------------------------
//
// ---------------------------------------------------------------------------
//
void T_CaHandler::appHandlerMultipleLoadAndDelete()
{
    CaAppHandler *handlerPtr(NULL);
    for (TInt i(0); i < 10; ++i) {
        handlerPtr = new CaAppHandler();
        delete handlerPtr;
        handlerPtr = NULL;
    }
}

// ---------------------------------------------------------------------------
//
// ---------------------------------------------------------------------------
//
void T_CaHandler::appHandlerOpenFails()
{
    appHandlerConstruction();

    TInt unsuportedCommandResult(KErrNone);
    TInt unexistingEntryOpenResult(KErrNone);
    TInt corruptedEntryOpenResult(KErrNone);
    TInt unsupportedEntryTypeOpenResult(KErrNone);

    CaEntry *entry = new CaEntry();

    // valid entry type name
    entry->setEntryTypeName(caTypeApp);

    unsuportedCommandResult =
        iAppHandler->execute(*entry, "unsupported_command_name");
    // invalid UID
    entry->setAttribute(APPLICATION_UID_ATTRIBUTE_NAME, "11111");

    unexistingEntryOpenResult = iAppHandler->execute(*entry, caCmdOpen);

    // invalid view ID
    entry->setAttribute(caAttrView, "not_a_hex_value");

    corruptedEntryOpenResult = iAppHandler->execute(*entry, caCmdOpen);

    // invalid entry type name
    entry->setEntryTypeName("unsupported_entry_type_name");

    unsupportedEntryTypeOpenResult =
        iAppHandler->execute(*entry, caCmdOpen);

    delete entry;
    entry = NULL;
    QCOMPARE(unsuportedCommandResult, KErrNotSupported);
    QCOMPARE(unexistingEntryOpenResult, KErrNotFound);
    QCOMPARE(corruptedEntryOpenResult, KErrGeneral);
    QCOMPARE(unsupportedEntryTypeOpenResult, KErrNotSupported);
}

// ---------------------------------------------------------------------------
//
// ---------------------------------------------------------------------------
//
void T_CaHandler::appHandlerRemoveFails()
{
    appHandlerConstruction();
    TInt unpermittedRemovalResult(KErrNone);
    TInt unexistingEntryRemovalResult(KErrNone);

    CaEntry *entry = new CaEntry();
    entry->setEntryTypeName(caTypeApp);

    // invalid flags (should be removable)
    entry->setFlags(0);

    unpermittedRemovalResult =
        iAppHandler->execute(*entry, caCmdRemove);

    // set correct flags
    entry->setFlags(RemovableEntryFlag);

    // invalid UID
    entry->setAttribute(APPLICATION_UID_ATTRIBUTE_NAME, "0");

    unexistingEntryRemovalResult =
        iAppHandler->execute(*entry, caCmdRemove);
    delete entry;
    entry = NULL;

    QCOMPARE(unpermittedRemovalResult, KErrAccessDenied);
    QCOMPARE(unexistingEntryRemovalResult, KErrNotFound);
}

// ---------------------------------------------------------------------------
//
// ---------------------------------------------------------------------------
//
void T_CaHandler::appHandlerClose()
{
    TInt errorClose1(KErrNone);
    TInt errorClose2(KErrNone);
    TInt errorOpen(KErrNone);
    bool wasRunning(false);
    bool closedRunning(false);

    const TInt KPhonebookUid(0x20022EF9);

    RWsSession wsSession;
    TRAPD(err,
        CleanupClosePushL(wsSession);
        User::LeaveIfError(wsSession.Connect());

        TApaTaskList taskList(wsSession);
        TApaTask taskBefore = taskList.FindApp(TUid::Uid(KPhonebookUid));
        if (taskBefore.Exists()) {
            taskBefore.EndTask();
        }

        appHandlerConstruction();

        CaEntry *entry = new CaEntry();
        entry->setEntryTypeName(caTypeApp);
        entry->setAttribute(APPLICATION_UID_ATTRIBUTE_NAME,
            QString::number(KPhonebookUid, 10));

        errorClose1 = iAppHandler->execute(*entry, caCmdClose);
        QTest::qWait(10000);
        errorOpen = iAppHandler->execute(*entry, caCmdOpen);

        QTest::qWait(10000);
        TApaTask taskAfter = taskList.FindApp(TUid::Uid(KPhonebookUid));
        TInt id = taskAfter.WgId();
        QString numBuf;
        numBuf = QString::number(id);
        entry->setFlags(entry->flags() | RunningEntryFlag);
        entry->setAttribute(caAttrWindowGroupId, numBuf);

        wasRunning = taskAfter.Exists();

        errorClose2 = iAppHandler->execute(*entry, caCmdClose);

        QTest::qWait(10000);
        closedRunning = !taskList.FindApp(TUid::Uid(KPhonebookUid)).Exists();
        delete entry;
        entry = NULL;
        CleanupStack::PopAndDestroy(&wsSession);
         );
    QCOMPARE(err, KErrNone);
    QCOMPARE(errorClose1, KErrGeneral);
    QCOMPARE(errorOpen, KErrNone);
    QCOMPARE(errorClose2, KErrNone);
    QVERIFY(wasRunning);
    QVERIFY(closedRunning);
}

// ---------------------------------------------------------------------------
//
// ---------------------------------------------------------------------------
//
void T_CaHandler::appHandlerRunApplication()
{

    TInt error(KErrNone);
    bool started(false);
    // we will run calculator app
    const TInt KPhonebookUid(0x20022EF9);

    RWsSession wsSession;

    TRAPD(err,
        CleanupClosePushL(wsSession);
        User::LeaveIfError(wsSession.Connect());

        TApaTaskList taskList(wsSession);
        TApaTask taskBefore = taskList.FindApp(TUid::Uid(KPhonebookUid));
        if (taskBefore.Exists()) {
            taskBefore.EndTask();
        }

        appHandlerConstruction();

        CaEntry *entry = new CaEntry();
        entry->setEntryTypeName(caTypeApp);
        entry->setAttribute(APPLICATION_UID_ATTRIBUTE_NAME,
            QString::number(KPhonebookUid));

        error = iAppHandler->execute(*entry, caCmdOpen);

        QTest::qWait(1000);
        TApaTask taskAfter = taskList.FindApp(TUid::Uid(KPhonebookUid));
        started = taskAfter.Exists();
        taskAfter.EndTask();

        delete entry;
        entry = NULL;
        CleanupStack::PopAndDestroy(&wsSession);
         );
    QCOMPARE(err, KErrNone);
    QCOMPARE(error, KErrNone);
    QVERIFY(started);
}

// ---------------------------------------------------------------------------
//
// ---------------------------------------------------------------------------
//
void T_CaHandler::appHandlerRemoveApplication()
{
    bool leaveOnRemove(true);
    bool removed(false);

    _LIT(KSapiPackageFileName, 
        "c:\\testing\\data\\t_apphandler\\installs\\sapiapp_S60_3_X_v_1_0_0.sisx");
    _LIT(KSapiAppFileName, "c:\\sys\\bin\\sapiapp.exe");
    const TUid KSapiAppUid(TUid::Uid(0xe6bc1167));

    T_CaInstaller *installer = NULL;
    installer = T_CaInstaller::NewL();
    CleanupStack::PushL(installer);
    TRAPD(err, installer->InstallL(KSapiPackageFileName));
    QCOMPARE(err, KErrNone);
    QTest::qWait(20000);
    
    Usif::TComponentId componentId(0);
    TRAP(err, componentId = installer->GetComponentIdL(KSapiAppFileName));
    QCOMPARE(err, KErrNone);
    
    appHandlerConstruction();
    
    CaEntry *entry = new CaEntry();
    
    entry->setEntryTypeName(caTypeApp);
    entry->setFlags(RemovableEntryFlag);
    entry->setAttribute(APPLICATION_UID_ATTRIBUTE_NAME,
        QString::number(KSapiAppUid.iUid));
    
    entry->setAttribute(caAttrComponentId,
        QString::number(componentId));
    
    TRAP(err, iAppHandler->execute(*entry, caCmdRemove));
    QCOMPARE(err, KErrNone);
    QTest::qWait(20000);
    delete entry;
    entry = NULL;
    
    // check again if installed, should be gone
    TRAP(err, componentId = installer->GetComponentIdL(KSapiAppFileName))
    QCOMPARE(err, KErrNone);
    QCOMPARE(componentId, 0);
    CleanupStack::PopAndDestroy(installer);

}

// ---------------------------------------------------------------------------
//
// ---------------------------------------------------------------------------
//
void T_CaHandler::appHandlerRemoveJavaApplication()
{
    const int javaAppUid = 0x101251BB;//
    bool removed(false);
    int error(KErrNone);

    TRAPD(err,
          QVERIFY(javaAppInstalled(javaAppUid));
          
          appHandlerConstruction();

          CaEntry *entry = new CaEntry();
          entry->setEntryTypeName(caTypeApp);
          entry->setFlags(RemovableEntryFlag);
          entry->setAttribute(APPLICATION_UID_ATTRIBUTE_NAME,
                  QString::number(javaAppUid));
          
          Usif::RSoftwareComponentRegistry scr;
          CleanupClosePushL(scr);
          User::LeaveIfError(scr.Connect());
          TUid uid;
          Usif::TComponentId componentId =
              scr.GetComponentIdForAppL( uid.Uid( javaAppUid ) );
            
          if (componentId > 0) {
              entry->setAttribute(caAttrComponentId, QString::number(componentId));
          }

          CleanupStack::PopAndDestroy(&scr);
          
          error = iAppHandler->execute(*entry, caCmdRemove);
          QTest::qWait(20000);
          delete entry;
          entry = NULL;
          )
    QVERIFY(!javaAppInstalled(javaAppUid));
    QCOMPARE(err, KErrNone);
    QCOMPARE(error, KErrNone);
}

// ---------------------------------------------------------------------------
//
// ---------------------------------------------------------------------------
//
TBool T_CaHandler::javaAppInstalled(TInt32 aUid)
    {
    TBool result = EFalse;
    Usif::RSoftwareComponentRegistry scr;
    CleanupClosePushL(scr);
    scr.Connect();
    TUid uid;
    TInt err;
    Usif::TComponentId componentId( 0 );
    TRAP(err, componentId = 
            scr.GetComponentIdForAppL(
            uid.Uid( aUid ) ) )
    if (componentId > 0 && err == KErrNone)
        {
        result = ETrue;
        }
    CleanupStack::PopAndDestroy(&scr);
    return result;
    }


// ---------------------------------------------------------------------------
//
// ---------------------------------------------------------------------------
//
void T_CaHandler::testCCaTaskList()
{
    RWsSession wsSession;
    User::LeaveIfError( wsSession.Connect() );
    CleanupClosePushL<RWsSession>( wsSession );

	CCaTaskList* taskList = CCaTaskList::NewL( wsSession );
	taskList->UpdateListL();
	QVERIFY(taskList->IsRootWindowGroup(-1) == EFalse);
	delete taskList;
    CleanupStack::PopAndDestroy( &wsSession );
}

// ---------------------------------------------------------------------------
//
// ---------------------------------------------------------------------------
//
void T_CaHandler::appHandlerCreateInstance()
{
    QServiceManager serviceManager;
    CaAppHandlerPlugin plugin;
    QServiceFilter serviceFilter("com.nokia.homescreen.ICommandHandler");
    serviceFilter.setCustomAttribute("entryTypeName", "application");
    QList<QServiceInterfaceDescriptor> serviceInterfaceDescriptorList =
        serviceManager.findInterfaces(serviceFilter);
    int len = serviceInterfaceDescriptorList.size();
    QVERIFY(len>0);
    QServiceInterfaceDescriptor serviceInterfaceDescriptor =
        serviceInterfaceDescriptorList[0];
    QObject *handler = plugin.createInstance(
        serviceInterfaceDescriptor, NULL, NULL);
    QVERIFY(handler);
    delete handler;
}

QTEST_MAIN(T_CaHandler)