contentstorage/castorage/tsrc/t_castorage/src/castoragetestorganize.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:
*
*/
/*
 * castoragetestorganize.cpp
 *
 *  Created on: 2009-08-14
 *      Author: maciej.walczynski
 */

#include "castoragetest.h"
#include "caoperationparams.h"
#include "castorage.h"
#include "caarraycleanup.inl"
#include "castoragetestsqlcommands.h"
#include "casqlcommands.h"
#include "cainnerentry.h"
#include "casqlitestorage.h"
#include "casqlquery.h"

// ---------------------------------------------------------------------------
//
// ---------------------------------------------------------------------------
//
void CCAStorageTest::testOrganizeRemove()
{
    TInt err(KErrNone);

    __UHEAP_MARK;
    TRAP(err,
         RArray<TInt> entryIds;
         CleanupClosePushL(entryIds);
         entryIds.AppendL(15);
         entryIds.AppendL(16);

         TCaOperationParams organizeParams;
         organizeParams.iGroupId = 12;
         organizeParams.iOperationType = TCaOperationParams::ERemove;
         mStorage->OrganizeL(entryIds, organizeParams);

         CCaInnerQuery *innerQuery = CCaInnerQuery::NewLC();
         innerQuery->SetParentId(12);
         RPointerArray<CCaInnerEntry> resultArray;
         CleanupResetAndDestroyPushL(resultArray);
         mStorage->GetEntriesL(innerQuery, resultArray);

         //    Check that entries with ids 15 and 16 are not in group 12
    for (TInt j = 0; j < entryIds.Count(); j++) {
    for (TInt i = 0; i < resultArray.Count(); i++) {
            if (resultArray[i]->GetId() == entryIds[j]) {
                err = KErrWrongData;
                break;
            }
        }
    }

    CleanupStack::PopAndDestroy(&resultArray);
    CleanupStack::PopAndDestroy(innerQuery);
    CleanupStack::PopAndDestroy(&entryIds);
        );
    __UHEAP_MARKEND;

    QCOMPARE(err, KErrNone);
}


// ---------------------------------------------------------------------------
//
// ---------------------------------------------------------------------------
//
void CCAStorageTest::testOrganizeAppend()
{
    TInt err(KErrNone);

    __UHEAP_MARK;
    TRAP(err,
         RArray<TInt> entryIds;
         CleanupClosePushL(entryIds);
         entryIds.AppendL(8);
         entryIds.AppendL(7);

         TCaOperationParams organizeParams;
         organizeParams.iGroupId = 3;
         organizeParams.iOperationType = TCaOperationParams::EAppend;
         mStorage->OrganizeL(entryIds,organizeParams);

         CCaInnerQuery *innerQuery = CCaInnerQuery::NewLC();
         innerQuery->SetParentId(3);
         innerQuery->SetSort(CCaInnerQuery::Default);
         RPointerArray<CCaInnerEntry> resultArray;
         CleanupResetAndDestroyPushL(resultArray);
         mStorage->GetEntriesL(innerQuery, resultArray);

         TInt j = resultArray.Count() - 1;
    for (TInt i = entryIds.Count() - 1; i >= 0; i--, j--) {
    if (resultArray[j]->GetId() != entryIds[i]) {
            err = KErrWrongData;
        }
    }

    CleanupStack::PopAndDestroy(&resultArray);
    CleanupStack::PopAndDestroy(innerQuery);
    CleanupStack::PopAndDestroy(&entryIds);
        );

    __UHEAP_MARKEND;

    QCOMPARE(err, KErrNone);
}


// ---------------------------------------------------------------------------
//
// ---------------------------------------------------------------------------
//
void CCAStorageTest::testOrganizePrepend()
{
    TInt err(KErrNone);

    __UHEAP_MARK;
    TRAP(err,
         RArray<TInt> entryIds;
         CleanupClosePushL(entryIds);
         entryIds.AppendL(8);
         entryIds.AppendL(7);

         TCaOperationParams organizeParams;
         organizeParams.iGroupId = 3;
         organizeParams.iOperationType = TCaOperationParams::EPrepend;
         mStorage->OrganizeL(entryIds,organizeParams);

         CCaInnerQuery *innerQuery = CCaInnerQuery::NewLC();
         innerQuery->SetParentId(3);
         innerQuery->SetSort(CCaInnerQuery::Default);
         RPointerArray<CCaInnerEntry> resultArray;
         CleanupResetAndDestroyPushL(resultArray);
         mStorage->GetEntriesL(innerQuery, resultArray);

         TInt j = 0;
    for (TInt i = 0; i < entryIds.Count(); i++, j++) {
    if (resultArray[j]->GetId() != entryIds[i]) {
            err = KErrWrongData;
        }
    }

    CleanupStack::PopAndDestroy(&resultArray);
    CleanupStack::PopAndDestroy(innerQuery);
    CleanupStack::PopAndDestroy(&entryIds);
        );
    __UHEAP_MARKEND;

    QCOMPARE(err, KErrNone);
}



// ---------------------------------------------------------------------------
//
// ---------------------------------------------------------------------------
//
void CCAStorageTest::testOrganizeInsertNewIntoGroup()
{
    TInt err(KErrNone);

    __UHEAP_MARK;
    TRAP(err,
         //insert somewhere in the middle
         RArray<TInt> entryIds;
         CleanupClosePushL(entryIds);
         TInt newInsertEntryId = 7;

         entryIds.AppendL(newInsertEntryId);

         CCaInnerQuery *innerQuery = CCaInnerQuery::NewLC();
         innerQuery->SetParentId(2);
         innerQuery->SetSort(CCaInnerQuery::Default);

         RPointerArray<CCaInnerEntry> resultArray1;
         CleanupResetAndDestroyPushL(resultArray1);
         mStorage->GetEntriesL(innerQuery, resultArray1);

         TInt count = resultArray1.Count();
         TInt insertPosition = count / 2;
         TInt beforeId = resultArray1[insertPosition - 1]->GetId();

         TCaOperationParams organizeParams;
         organizeParams.iGroupId = 2;
         organizeParams.iBeforeEntryId = beforeId;
         organizeParams.iOperationType = TCaOperationParams::EInsert;
         mStorage->OrganizeL(entryIds,organizeParams);

         RPointerArray<CCaInnerEntry> resultArray2;
         CleanupResetAndDestroyPushL(resultArray2);
         mStorage->GetEntriesL(innerQuery, resultArray2);

         TInt idNew = resultArray2[ insertPosition - 1 ]->GetId();
         err = (resultArray2[ insertPosition - 1 ]->GetId() == newInsertEntryId) ? err : KErrWrongData;
         err = (resultArray2[ insertPosition ]->GetId() == beforeId) ? err : KErrWrongData;

         //insert into 1st position (prepend)

         CCaSqLiteStorage *storage = STATIC_CAST(CCaSqLiteStorage *,mStorage);
         CCaSqlQuery *query = CCaSqlQuery::NewLC(storage->iSqlDb);
         query->SetQueryL(KSQLGetUnexistingIdFromGroup);
         query->PrepareL();
         query->BindIntL(query->iStatement.ParameterIndex(KSQLGEIdGroup), KAllCollectionsGroupId);
         TInt unexistingIdInGroup;
         query->ExecuteL(unexistingIdInGroup);
         query->CloseStatement();
         entryIds.Reset();
         entryIds.AppendL(unexistingIdInGroup);
         TInt idFirst = resultArray2[ 0 ]->GetId();
         organizeParams.iBeforeEntryId = idFirst;
         mStorage->OrganizeL(entryIds,organizeParams);

         RPointerArray<CCaInnerEntry> resultArray3;
         CleanupResetAndDestroyPushL(resultArray3);
         mStorage->GetEntriesL(innerQuery, resultArray3);

         err = (resultArray3[ 0 ]->GetId() == unexistingIdInGroup) ? err : KErrWrongData;

         //insert before unexisting item
         TInt unexistingIdInGroup1;
         TInt unexistingIdInGroup2;
         query->SetQueryL(KSQLGetUnexistingIdFromGroup);
         query->PrepareL();
         query->BindIntL(query->iStatement.ParameterIndex(KSQLGEIdGroup), KAllCollectionsGroupId);
         query->ExecuteL(unexistingIdInGroup1);
         unexistingIdInGroup2 = unexistingIdInGroup1 + 1;
         query->CloseStatement();
         organizeParams.iBeforeEntryId = unexistingIdInGroup1;
         entryIds.Reset();
         entryIds.AppendL(unexistingIdInGroup2);

         TInt unexistingBeforeIdError(KErrNone);
         TRAP(unexistingBeforeIdError, mStorage->OrganizeL(entryIds,organizeParams));
    if (KErrNone == unexistingBeforeIdError) {
    err = KErrBadParams;
}

CleanupStack::PopAndDestroy(&resultArray3);
CleanupStack::PopAndDestroy(query);
CleanupStack::PopAndDestroy(&resultArray2);
CleanupStack::PopAndDestroy(&resultArray1);
CleanupStack::PopAndDestroy(innerQuery);
CleanupStack::PopAndDestroy(&entryIds);
    );
    __UHEAP_MARKEND;
    QCOMPARE(err, KErrNone);
}

// ---------------------------------------------------------------------------
//
// ---------------------------------------------------------------------------
//
void CCAStorageTest::testOrganizeBadParams()
{
    TInt err(KErrNone);
    __UHEAP_MARK;
    TRAP(err,
         RArray<TInt> errorArray;
         CleanupClosePushL(errorArray);
         RArray<TInt> entryIds;
         CleanupClosePushL(entryIds);
         TCaOperationParams organizeParams;
         CCaSqLiteStorage *storage = STATIC_CAST(CCaSqLiteStorage *,mStorage);
         CCaSqlQuery *query = CCaSqlQuery::NewLC(storage->iSqlDb);

         query->SetQueryL(KSQLGetUnexistingIdFromGroup);
         query->PrepareL();
         query->BindIntL(query->iStatement.ParameterIndex(KSQLGEIdGroup), KAllCollectionsGroupId);
         TInt unexistingIdInGroup;
         query->ExecuteL(unexistingIdInGroup);
         query->CloseStatement();
         organizeParams.iOperationType = TCaOperationParams::ERemove;
         entryIds.Append(unexistingIdInGroup);
         entryIds.Append(unexistingIdInGroup + 1);
         TRAP(err, mStorage->OrganizeL(entryIds,organizeParams));
         errorArray.AppendL(err);
         err = KErrNone;
         entryIds.Reset();

         organizeParams.iOperationType = TCaOperationParams::EInsert;
         organizeParams.iBeforeEntryId = unexistingIdInGroup;
         entryIds.Append(unexistingIdInGroup + 1);
         TRAP(err, mStorage->OrganizeL(entryIds,organizeParams));
         errorArray.AppendL(err);
         err = KErrNone;

    for (TInt i = 0; i < errorArray.Count(); i++) {
    if (errorArray[i] == 0) {
            err = KErrBadParams;
            break;
        }
    }

    CleanupStack::PopAndDestroy(query);
    CleanupStack::PopAndDestroy(&entryIds);
    CleanupStack::PopAndDestroy(&errorArray);
        );
    __UHEAP_MARKEND;
    QCOMPARE(err, KErrNone);
}

// ---------------------------------------------------------------------------
//
// ---------------------------------------------------------------------------
//
void CCAStorageTest::testCustomSort()
{
    TInt err(KErrNone);
    __UHEAP_MARK;
    TRAP(err,
         const TInt KGroupId = 12;

         RArray<TInt> entryIds;
         CleanupClosePushL(entryIds);
         RArray<TInt> entryIdsFromDb;
         CleanupClosePushL(entryIdsFromDb);

         // array with custom sorted ids
         entryIds.AppendL(14);   // 13
         entryIds.AppendL(15);   // 14
         entryIds.AppendL(13);   // 15
         entryIds.AppendL(16);   // 16
         entryIds.AppendL(17);   // 17

         // test
         mStorage->CustomSortL(entryIds, KGroupId);

         // check results
         CCaSqLiteStorage *storage = STATIC_CAST(CCaSqLiteStorage *, mStorage);
         CCaSqlQuery *query = CCaSqlQuery::NewLC(storage->iSqlDb);
         query->SetQueryL(KSQLGetEntryIdsFromGroupInPositionOrder);
         query->PrepareL();
         query->BindIntL(query->iStatement.ParameterIndex(KSQLGEIdGroup), KGroupId);
         TInt count = query->ExecuteL(entryIdsFromDb, CCaSqlQuery::EGroupTable);
         query->CloseStatement();

    for (TInt i=0; i<entryIds.Count(); i++) {
    if (entryIds[i] != entryIdsFromDb[i]) {
            err = KErrBadParams;
            break;
        }
    }

    CleanupStack::PopAndDestroy(query);
    CleanupStack::PopAndDestroy(&entryIdsFromDb);
    CleanupStack::PopAndDestroy(&entryIds);
        );
    __UHEAP_MARKEND;
    QCOMPARE(err, KErrNone);
}