contentstorage/castorage/tsrc/t_castorage/src/castoragetestorganize.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  * castoragetestorganize.cpp
       
    19  *
       
    20  *  Created on: 2009-08-14
       
    21  *      Author: maciej.walczynski
       
    22  */
       
    23 
       
    24 #include "castoragetest.h"
       
    25 #include "caoperationparams.h"
       
    26 #include "castorage.h"
       
    27 #include "caarraycleanup.inl"
       
    28 #include "castoragetestsqlcommands.h"
       
    29 #include "casqlcommands.h"
       
    30 #include "cainnerentry.h"
       
    31 #include "casqlitestorage.h"
       
    32 #include "casqlquery.h"
       
    33 
       
    34 // ---------------------------------------------------------------------------
       
    35 //
       
    36 // ---------------------------------------------------------------------------
       
    37 //
       
    38 void CCAStorageTest::testOrganizeRemove()
       
    39 {
       
    40     TInt err(KErrNone);
       
    41 
       
    42     __UHEAP_MARK;
       
    43     TRAP(err,
       
    44          RArray<TInt> entryIds;
       
    45          CleanupClosePushL(entryIds);
       
    46          entryIds.AppendL(15);
       
    47          entryIds.AppendL(16);
       
    48 
       
    49          TCaOperationParams organizeParams;
       
    50          organizeParams.iGroupId = 12;
       
    51          organizeParams.iOperationType = TCaOperationParams::ERemove;
       
    52          mStorage->OrganizeL(entryIds, organizeParams);
       
    53 
       
    54          CCaInnerQuery *innerQuery = CCaInnerQuery::NewLC();
       
    55          innerQuery->SetParentId(12);
       
    56          RPointerArray<CCaInnerEntry> resultArray;
       
    57          CleanupResetAndDestroyPushL(resultArray);
       
    58          mStorage->GetEntriesL(innerQuery, resultArray);
       
    59 
       
    60          //    Check that entries with ids 15 and 16 are not in group 12
       
    61     for (TInt j = 0; j < entryIds.Count(); j++) {
       
    62     for (TInt i = 0; i < resultArray.Count(); i++) {
       
    63             if (resultArray[i]->GetId() == entryIds[j]) {
       
    64                 err = KErrWrongData;
       
    65                 break;
       
    66             }
       
    67         }
       
    68     }
       
    69 
       
    70     CleanupStack::PopAndDestroy(&resultArray);
       
    71     CleanupStack::PopAndDestroy(innerQuery);
       
    72     CleanupStack::PopAndDestroy(&entryIds);
       
    73         );
       
    74     __UHEAP_MARKEND;
       
    75 
       
    76     QCOMPARE(err, KErrNone);
       
    77 }
       
    78 
       
    79 
       
    80 // ---------------------------------------------------------------------------
       
    81 //
       
    82 // ---------------------------------------------------------------------------
       
    83 //
       
    84 void CCAStorageTest::testOrganizeAppend()
       
    85 {
       
    86     TInt err(KErrNone);
       
    87 
       
    88     __UHEAP_MARK;
       
    89     TRAP(err,
       
    90          RArray<TInt> entryIds;
       
    91          CleanupClosePushL(entryIds);
       
    92          entryIds.AppendL(8);
       
    93          entryIds.AppendL(7);
       
    94 
       
    95          TCaOperationParams organizeParams;
       
    96          organizeParams.iGroupId = 3;
       
    97          organizeParams.iOperationType = TCaOperationParams::EAppend;
       
    98          mStorage->OrganizeL(entryIds,organizeParams);
       
    99 
       
   100          CCaInnerQuery *innerQuery = CCaInnerQuery::NewLC();
       
   101          innerQuery->SetParentId(3);
       
   102          innerQuery->SetSort(CCaInnerQuery::Default);
       
   103          RPointerArray<CCaInnerEntry> resultArray;
       
   104          CleanupResetAndDestroyPushL(resultArray);
       
   105          mStorage->GetEntriesL(innerQuery, resultArray);
       
   106 
       
   107          TInt j = resultArray.Count() - 1;
       
   108     for (TInt i = entryIds.Count() - 1; i >= 0; i--, j--) {
       
   109     if (resultArray[j]->GetId() != entryIds[i]) {
       
   110             err = KErrWrongData;
       
   111         }
       
   112     }
       
   113 
       
   114     CleanupStack::PopAndDestroy(&resultArray);
       
   115     CleanupStack::PopAndDestroy(innerQuery);
       
   116     CleanupStack::PopAndDestroy(&entryIds);
       
   117         );
       
   118 
       
   119     __UHEAP_MARKEND;
       
   120 
       
   121     QCOMPARE(err, KErrNone);
       
   122 }
       
   123 
       
   124 
       
   125 // ---------------------------------------------------------------------------
       
   126 //
       
   127 // ---------------------------------------------------------------------------
       
   128 //
       
   129 void CCAStorageTest::testOrganizePrepend()
       
   130 {
       
   131     TInt err(KErrNone);
       
   132 
       
   133     __UHEAP_MARK;
       
   134     TRAP(err,
       
   135          RArray<TInt> entryIds;
       
   136          CleanupClosePushL(entryIds);
       
   137          entryIds.AppendL(8);
       
   138          entryIds.AppendL(7);
       
   139 
       
   140          TCaOperationParams organizeParams;
       
   141          organizeParams.iGroupId = 3;
       
   142          organizeParams.iOperationType = TCaOperationParams::EPrepend;
       
   143          mStorage->OrganizeL(entryIds,organizeParams);
       
   144 
       
   145          CCaInnerQuery *innerQuery = CCaInnerQuery::NewLC();
       
   146          innerQuery->SetParentId(3);
       
   147          innerQuery->SetSort(CCaInnerQuery::Default);
       
   148          RPointerArray<CCaInnerEntry> resultArray;
       
   149          CleanupResetAndDestroyPushL(resultArray);
       
   150          mStorage->GetEntriesL(innerQuery, resultArray);
       
   151 
       
   152          TInt j = 0;
       
   153     for (TInt i = 0; i < entryIds.Count(); i++, j++) {
       
   154     if (resultArray[j]->GetId() != entryIds[i]) {
       
   155             err = KErrWrongData;
       
   156         }
       
   157     }
       
   158 
       
   159     CleanupStack::PopAndDestroy(&resultArray);
       
   160     CleanupStack::PopAndDestroy(innerQuery);
       
   161     CleanupStack::PopAndDestroy(&entryIds);
       
   162         );
       
   163     __UHEAP_MARKEND;
       
   164 
       
   165     QCOMPARE(err, KErrNone);
       
   166 }
       
   167 
       
   168 
       
   169 
       
   170 // ---------------------------------------------------------------------------
       
   171 //
       
   172 // ---------------------------------------------------------------------------
       
   173 //
       
   174 void CCAStorageTest::testOrganizeInsertNewIntoGroup()
       
   175 {
       
   176     TInt err(KErrNone);
       
   177 
       
   178     __UHEAP_MARK;
       
   179     TRAP(err,
       
   180          //insert somewhere in the middle
       
   181          RArray<TInt> entryIds;
       
   182          CleanupClosePushL(entryIds);
       
   183          TInt newInsertEntryId = 7;
       
   184 
       
   185          entryIds.AppendL(newInsertEntryId);
       
   186 
       
   187          CCaInnerQuery *innerQuery = CCaInnerQuery::NewLC();
       
   188          innerQuery->SetParentId(2);
       
   189          innerQuery->SetSort(CCaInnerQuery::Default);
       
   190 
       
   191          RPointerArray<CCaInnerEntry> resultArray1;
       
   192          CleanupResetAndDestroyPushL(resultArray1);
       
   193          mStorage->GetEntriesL(innerQuery, resultArray1);
       
   194 
       
   195          TInt count = resultArray1.Count();
       
   196          TInt insertPosition = count / 2;
       
   197          TInt beforeId = resultArray1[insertPosition - 1]->GetId();
       
   198 
       
   199          TCaOperationParams organizeParams;
       
   200          organizeParams.iGroupId = 2;
       
   201          organizeParams.iBeforeEntryId = beforeId;
       
   202          organizeParams.iOperationType = TCaOperationParams::EInsert;
       
   203          mStorage->OrganizeL(entryIds,organizeParams);
       
   204 
       
   205          RPointerArray<CCaInnerEntry> resultArray2;
       
   206          CleanupResetAndDestroyPushL(resultArray2);
       
   207          mStorage->GetEntriesL(innerQuery, resultArray2);
       
   208 
       
   209          TInt idNew = resultArray2[ insertPosition - 1 ]->GetId();
       
   210          err = (resultArray2[ insertPosition - 1 ]->GetId() == newInsertEntryId) ? err : KErrWrongData;
       
   211          err = (resultArray2[ insertPosition ]->GetId() == beforeId) ? err : KErrWrongData;
       
   212 
       
   213          //insert into 1st position (prepend)
       
   214 
       
   215          CCaSqLiteStorage *storage = STATIC_CAST(CCaSqLiteStorage *,mStorage);
       
   216          CCaSqlQuery *query = CCaSqlQuery::NewLC(storage->iSqlDb);
       
   217          query->SetQueryL(KSQLGetUnexistingIdFromGroup);
       
   218          query->PrepareL();
       
   219          query->BindIntL(query->iStatement.ParameterIndex(KSQLGEIdGroup), KAllCollectionsGroupId);
       
   220          TInt unexistingIdInGroup;
       
   221          query->ExecuteL(unexistingIdInGroup);
       
   222          query->CloseStatement();
       
   223          entryIds.Reset();
       
   224          entryIds.AppendL(unexistingIdInGroup);
       
   225          TInt idFirst = resultArray2[ 0 ]->GetId();
       
   226          organizeParams.iBeforeEntryId = idFirst;
       
   227          mStorage->OrganizeL(entryIds,organizeParams);
       
   228 
       
   229          RPointerArray<CCaInnerEntry> resultArray3;
       
   230          CleanupResetAndDestroyPushL(resultArray3);
       
   231          mStorage->GetEntriesL(innerQuery, resultArray3);
       
   232 
       
   233          err = (resultArray3[ 0 ]->GetId() == unexistingIdInGroup) ? err : KErrWrongData;
       
   234 
       
   235          //insert before unexisting item
       
   236          TInt unexistingIdInGroup1;
       
   237          TInt unexistingIdInGroup2;
       
   238          query->SetQueryL(KSQLGetUnexistingIdFromGroup);
       
   239          query->PrepareL();
       
   240          query->BindIntL(query->iStatement.ParameterIndex(KSQLGEIdGroup), KAllCollectionsGroupId);
       
   241          query->ExecuteL(unexistingIdInGroup1);
       
   242          unexistingIdInGroup2 = unexistingIdInGroup1 + 1;
       
   243          query->CloseStatement();
       
   244          organizeParams.iBeforeEntryId = unexistingIdInGroup1;
       
   245          entryIds.Reset();
       
   246          entryIds.AppendL(unexistingIdInGroup2);
       
   247 
       
   248          TInt unexistingBeforeIdError(KErrNone);
       
   249          TRAP(unexistingBeforeIdError, mStorage->OrganizeL(entryIds,organizeParams));
       
   250     if (KErrNone == unexistingBeforeIdError) {
       
   251     err = KErrBadParams;
       
   252 }
       
   253 
       
   254 CleanupStack::PopAndDestroy(&resultArray3);
       
   255 CleanupStack::PopAndDestroy(query);
       
   256 CleanupStack::PopAndDestroy(&resultArray2);
       
   257 CleanupStack::PopAndDestroy(&resultArray1);
       
   258 CleanupStack::PopAndDestroy(innerQuery);
       
   259 CleanupStack::PopAndDestroy(&entryIds);
       
   260     );
       
   261     __UHEAP_MARKEND;
       
   262     QCOMPARE(err, KErrNone);
       
   263 }
       
   264 
       
   265 // ---------------------------------------------------------------------------
       
   266 //
       
   267 // ---------------------------------------------------------------------------
       
   268 //
       
   269 void CCAStorageTest::testOrganizeBadParams()
       
   270 {
       
   271     TInt err(KErrNone);
       
   272     __UHEAP_MARK;
       
   273     TRAP(err,
       
   274          RArray<TInt> errorArray;
       
   275          CleanupClosePushL(errorArray);
       
   276          RArray<TInt> entryIds;
       
   277          CleanupClosePushL(entryIds);
       
   278          TCaOperationParams organizeParams;
       
   279          CCaSqLiteStorage *storage = STATIC_CAST(CCaSqLiteStorage *,mStorage);
       
   280          CCaSqlQuery *query = CCaSqlQuery::NewLC(storage->iSqlDb);
       
   281 
       
   282          query->SetQueryL(KSQLGetUnexistingIdFromGroup);
       
   283          query->PrepareL();
       
   284          query->BindIntL(query->iStatement.ParameterIndex(KSQLGEIdGroup), KAllCollectionsGroupId);
       
   285          TInt unexistingIdInGroup;
       
   286          query->ExecuteL(unexistingIdInGroup);
       
   287          query->CloseStatement();
       
   288          organizeParams.iOperationType = TCaOperationParams::ERemove;
       
   289          entryIds.Append(unexistingIdInGroup);
       
   290          entryIds.Append(unexistingIdInGroup + 1);
       
   291          TRAP(err, mStorage->OrganizeL(entryIds,organizeParams));
       
   292          errorArray.AppendL(err);
       
   293          err = KErrNone;
       
   294          entryIds.Reset();
       
   295 
       
   296          organizeParams.iOperationType = TCaOperationParams::EInsert;
       
   297          organizeParams.iBeforeEntryId = unexistingIdInGroup;
       
   298          entryIds.Append(unexistingIdInGroup + 1);
       
   299          TRAP(err, mStorage->OrganizeL(entryIds,organizeParams));
       
   300          errorArray.AppendL(err);
       
   301          err = KErrNone;
       
   302 
       
   303     for (TInt i = 0; i < errorArray.Count(); i++) {
       
   304     if (errorArray[i] == 0) {
       
   305             err = KErrBadParams;
       
   306             break;
       
   307         }
       
   308     }
       
   309 
       
   310     CleanupStack::PopAndDestroy(query);
       
   311     CleanupStack::PopAndDestroy(&entryIds);
       
   312     CleanupStack::PopAndDestroy(&errorArray);
       
   313         );
       
   314     __UHEAP_MARKEND;
       
   315     QCOMPARE(err, KErrNone);
       
   316 }
       
   317 
       
   318 // ---------------------------------------------------------------------------
       
   319 //
       
   320 // ---------------------------------------------------------------------------
       
   321 //
       
   322 void CCAStorageTest::testCustomSort()
       
   323 {
       
   324     TInt err(KErrNone);
       
   325     __UHEAP_MARK;
       
   326     TRAP(err,
       
   327          const TInt KGroupId = 12;
       
   328 
       
   329          RArray<TInt> entryIds;
       
   330          CleanupClosePushL(entryIds);
       
   331          RArray<TInt> entryIdsFromDb;
       
   332          CleanupClosePushL(entryIdsFromDb);
       
   333 
       
   334          // array with custom sorted ids
       
   335          entryIds.AppendL(14);   // 13
       
   336          entryIds.AppendL(15);   // 14
       
   337          entryIds.AppendL(13);   // 15
       
   338          entryIds.AppendL(16);   // 16
       
   339          entryIds.AppendL(17);   // 17
       
   340 
       
   341          // test
       
   342          mStorage->CustomSortL(entryIds, KGroupId);
       
   343 
       
   344          // check results
       
   345          CCaSqLiteStorage *storage = STATIC_CAST(CCaSqLiteStorage *, mStorage);
       
   346          CCaSqlQuery *query = CCaSqlQuery::NewLC(storage->iSqlDb);
       
   347          query->SetQueryL(KSQLGetEntryIdsFromGroupInPositionOrder);
       
   348          query->PrepareL();
       
   349          query->BindIntL(query->iStatement.ParameterIndex(KSQLGEIdGroup), KGroupId);
       
   350          TInt count = query->ExecuteL(entryIdsFromDb, CCaSqlQuery::EGroupTable);
       
   351          query->CloseStatement();
       
   352 
       
   353     for (TInt i=0; i<entryIds.Count(); i++) {
       
   354     if (entryIds[i] != entryIdsFromDb[i]) {
       
   355             err = KErrBadParams;
       
   356             break;
       
   357         }
       
   358     }
       
   359 
       
   360     CleanupStack::PopAndDestroy(query);
       
   361     CleanupStack::PopAndDestroy(&entryIdsFromDb);
       
   362     CleanupStack::PopAndDestroy(&entryIds);
       
   363         );
       
   364     __UHEAP_MARKEND;
       
   365     QCOMPARE(err, KErrNone);
       
   366 }