contentstorage/castorage/tsrc/t_castorage/src/castoragetestadd.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:
*
*/
/*
 * castoragetestadd.cpp
 *
 *  Created on: 2009-08-05
 *      Author: michal.czerwiec
 */


#include "castoragetest.h"
#include "castoragefactory.h"
#include "castorage.h"
#include "cainnerentry.h"
#include "casqlquery.h"
#include "casqlitestorage.h"
#include "castoragetestsqlcommands.h"
#include "caarraycleanup.inl"
#include <QMap>
#include <QString>

// ============================ MEMBER FUNCTIONS ===============================

// ---------------------------------------------------------------------------
//
// ---------------------------------------------------------------------------
//
void CCAStorageTest::testAdd()
{
    __UHEAP_MARK;
    TInt entryId(0);

    TRAPD(err,
          CCaInnerEntry *innerEntry = CCaInnerEntry::NewLC();
          innerEntry->SetTextL(KTestName);
          innerEntry->SetEntryTypeNameL(KTypeName);
          innerEntry->SetFlags(flag);
          innerEntry->SetRole(CCaInnerQuery::Group);
          mStorage->AddL(innerEntry);
          entryId = innerEntry->GetId();

          CleanupStack::PopAndDestroy(innerEntry);
         );

    QCOMPARE(err, KErrNone);
    QVERIFY(entryId > 0);

    __UHEAP_MARKEND;

}
// ---------------------------------------------------------------------------
//
// ---------------------------------------------------------------------------
//
void CCAStorageTest::testAddEmptyIcon()
{
    TInt entryId(0);
    __UHEAP_MARK;
    TRAPD(err,
          CCaInnerEntry *innerEntry = CCaInnerEntry::NewLC();
          innerEntry->SetTextL(KTestName);
          innerEntry->SetEntryTypeNameL(KTypeName);
          innerEntry->SetFlags(flag);
          innerEntry->SetRole(CCaInnerQuery::Group);
          innerEntry->SetIconDataL(KNullDesC, KNullDesC, KNullDesC);
          mStorage->AddL(innerEntry);
          entryId = innerEntry->GetId();
          CleanupStack::PopAndDestroy(innerEntry);
         );
    QCOMPARE(err, KErrNone);
    QVERIFY(entryId > 0);

    __UHEAP_MARKEND;
}

// ---------------------------------------------------------------------------
//
void CCAStorageTest::testAddWithIcon()
{
    __UHEAP_MARK;
    TInt entryId(0);
    TInt iconId(0);
    TRAPD(err,
          CCaInnerEntry *innerEntry = CCaInnerEntry::NewLC();
          innerEntry->SetTextL(KTestName);
          innerEntry->SetEntryTypeNameL(KTypeName);
          innerEntry->SetFlags(flag);
          innerEntry->SetRole(CCaInnerQuery::Group);
          innerEntry->SetIconDataL(_L("qtg_large_applications_user"), KNullDesC, KNullDesC);
          mStorage->AddL(innerEntry);

          entryId = innerEntry->GetId();
          iconId = innerEntry->GetIconId();

          CleanupStack::PopAndDestroy(innerEntry);
         );

    QCOMPARE(err, KErrNone);

    QVERIFY(entryId > 0);
    QVERIFY(iconId > 0);


    __UHEAP_MARKEND;
}

// ---------------------------------------------------------------------------
//
void CCAStorageTest::testAddWithExistingIcon()
{
    __UHEAP_MARK;
    TInt entryId1(0);
    TInt iconId1(0);
    TInt entryId2(0);
    TInt iconId2(0);
    TInt countIconsAfterFirstAdd (0);
    TInt countIconsAfterSecondAdd (0);

    TRAPD(err,
          CCaInnerEntry *innerEntry = CCaInnerEntry::NewLC();
          innerEntry->SetTextL(KTestName);
          innerEntry->SetEntryTypeNameL(KTypeName);
          innerEntry->SetFlags(flag);
          innerEntry->SetRole(CCaInnerQuery::Group);
          innerEntry->SetIconDataL(_L("filename"), _L("skinId"), _L("268458321"));
          mStorage->AddL(innerEntry);

          CCaSqlQuery *query = CCaSqlQuery::NewLC(((CCaSqLiteStorage *)mStorage)->iSqlDb);
          query->SetQueryL(KSQLGetIdCountFromIconTable);
          query->PrepareL();
          query->ExecuteL(countIconsAfterFirstAdd);
          CleanupStack::PopAndDestroy(query);

          // Add second entry with the some icon
          CCaInnerEntry *innerEntry2 = CCaInnerEntry::NewLC();
          innerEntry2->SetTextL(KTestName2);
          innerEntry2->SetEntryTypeNameL(KTypeName);
          innerEntry2->SetFlags(flag);
          innerEntry2->SetRole(CCaInnerQuery::Group);
          innerEntry2->SetIconDataL(_L("filename"), _L("skinId"), _L("268458321"));
          mStorage->AddL(innerEntry2);

          query = CCaSqlQuery::NewLC(((CCaSqLiteStorage *)mStorage)->iSqlDb);
          query->SetQueryL(KSQLGetIdCountFromIconTable);
          query->PrepareL();
          query->ExecuteL(countIconsAfterSecondAdd);
          CleanupStack::PopAndDestroy(query);
          
          entryId1 = innerEntry->GetId();
          iconId1 = innerEntry->GetIconId();

          entryId2 = innerEntry2->GetId();
          iconId2 = innerEntry2->GetIconId();

          CleanupStack::PopAndDestroy(innerEntry2);
          CleanupStack::PopAndDestroy(innerEntry);
         );

    QCOMPARE(err, KErrNone);

    // No new icon created; Icon which is the same should be shared by newly added entries
    QCOMPARE(countIconsAfterFirstAdd, countIconsAfterSecondAdd);

    QVERIFY(entryId1 > 0);
    QVERIFY(iconId1 > 0);
    QVERIFY(entryId2 > 0);
    QVERIFY(iconId2 > 0);
    QVERIFY(!(entryId1 == entryId2));
    QCOMPARE(iconId1, iconId2);

    __UHEAP_MARKEND;
}

// ---------------------------------------------------------------------------
//
void CCAStorageTest::testAddWithExistingIconOnlyFileName()
{
    __UHEAP_MARK;
    TInt entryId1(0);
    TInt iconId1(0);
    TInt entryId2(0);
    TInt iconId2(0);
    TInt countIconsAfterFirstAdd (0);
    TInt countIconsAfterSecondAdd (0);

    TRAPD(err,
          CCaInnerEntry *innerEntry = CCaInnerEntry::NewLC();
          innerEntry->SetTextL(KTestName);
          innerEntry->SetEntryTypeNameL(KTypeName);
          innerEntry->SetFlags(flag);
          innerEntry->SetRole(CCaInnerQuery::Group);
          innerEntry->SetIconDataL(_L("qtg_large_applications_user"), KNullDesC, KNullDesC);
          mStorage->AddL(innerEntry);

          CCaSqlQuery *query = CCaSqlQuery::NewLC(((CCaSqLiteStorage *)mStorage)->iSqlDb);
          query->SetQueryL(KSQLGetIdCountFromIconTable);
          query->PrepareL();
          query->ExecuteL(countIconsAfterFirstAdd);
          CleanupStack::PopAndDestroy(query);

          // Add second entry with the some icon
          CCaInnerEntry *innerEntry2 = CCaInnerEntry::NewLC();
          innerEntry2->SetTextL(KTestName2);
          innerEntry2->SetEntryTypeNameL(KTypeName);
          innerEntry2->SetFlags(flag);
          innerEntry2->SetRole(CCaInnerQuery::Group);
          innerEntry2->SetIconDataL(_L("qtg_large_applications_user"), KNullDesC, KNullDesC);
          mStorage->AddL(innerEntry2);

          query = CCaSqlQuery::NewLC(((CCaSqLiteStorage *)mStorage)->iSqlDb);
          query->SetQueryL(KSQLGetIdCountFromIconTable);
          query->PrepareL();
          query->ExecuteL(countIconsAfterSecondAdd);
          CleanupStack::PopAndDestroy(query);
          
          entryId1 = innerEntry->GetId();
          iconId1 = innerEntry->GetIconId();

          entryId2 = innerEntry2->GetId();
          iconId2 = innerEntry2->GetIconId();

          CleanupStack::PopAndDestroy(innerEntry2);
          CleanupStack::PopAndDestroy(innerEntry);
          
         );

    QCOMPARE(err, KErrNone);

    // No new icon created; Icon which is the same should be shared by newly added entries
    QCOMPARE(countIconsAfterFirstAdd, countIconsAfterSecondAdd);
    

    QVERIFY(entryId1 > 0);
    QVERIFY(iconId1 > 0);
    QVERIFY(entryId2 > 0);
    QVERIFY(iconId2 > 0);
    QVERIFY(!(entryId1 == entryId2));
    QCOMPARE(iconId1, iconId2);


    __UHEAP_MARKEND;
}

// ---------------------------------------------------------------------------
//
void CCAStorageTest::testAddWithPartlyMatchingIcon()
{
    __UHEAP_MARK;
    TInt entryId1(0);
    TInt iconId1(0);
    TInt entryId2(0);
    TInt iconId2(0);
    TInt countIconsAfterFirstAdd (0);
    TInt countIconsAfterSecondAdd (0);

    TRAPD(err,
          CCaInnerEntry *innerEntry = CCaInnerEntry::NewLC();
          innerEntry->SetTextL(KTestName);
          innerEntry->SetEntryTypeNameL(KTypeName);
          innerEntry->SetFlags(flag);
          innerEntry->SetRole(CCaInnerQuery::Group);
          innerEntry->SetIconDataL(_L("filename"), _L("skinId"), _L("268458321"));
          mStorage->AddL(innerEntry);

          CCaSqlQuery *query = CCaSqlQuery::NewLC(((CCaSqLiteStorage *)mStorage)->iSqlDb);
          query->SetQueryL(KSQLGetIdCountFromIconTable);
          query->PrepareL();
          query->ExecuteL(countIconsAfterFirstAdd);
          CleanupStack::PopAndDestroy(query);

          // Add second entry with the some icon
          CCaInnerEntry *innerEntry2 = CCaInnerEntry::NewLC();
          innerEntry2->SetTextL(KTestName2);
          innerEntry2->SetEntryTypeNameL(KTypeName);
          innerEntry2->SetFlags(flag);
          innerEntry2->SetRole(CCaInnerQuery::Group);
          innerEntry2->SetIconDataL(_L("filename"), _L("skinId2"), _L("111111111"));
          mStorage->AddL(innerEntry2);

          query = CCaSqlQuery::NewLC(((CCaSqLiteStorage *)mStorage)->iSqlDb);
          query->SetQueryL(KSQLGetIdCountFromIconTable);
          query->PrepareL();
          query->ExecuteL(countIconsAfterSecondAdd);
          CleanupStack::PopAndDestroy(query);
          
          entryId1 = innerEntry->GetId();
          iconId1 = innerEntry->GetIconId();

          entryId2 = innerEntry2->GetId();
          iconId2 = innerEntry2->GetIconId();

          CleanupStack::PopAndDestroy(innerEntry2);
          CleanupStack::PopAndDestroy(innerEntry);
         );

    QCOMPARE(err, KErrNone);

    // New icon should be added
    QCOMPARE(countIconsAfterSecondAdd - countIconsAfterFirstAdd, 1);

    QVERIFY(entryId1 > 0);
    QVERIFY(iconId1 > 0);
    QVERIFY(entryId2 > 0);
    QVERIFY(iconId2 > 0);
    QVERIFY(!(entryId1 == entryId2));
    QVERIFY(!(iconId1 == iconId2));

    __UHEAP_MARKEND;
}


// ---------------------------------------------------------------------------
//
void CCAStorageTest::testAddWithPartlyMatchingIconNotAllParams()
{
    __UHEAP_MARK;
    TInt entryId1(0);
    TInt iconId1(0);
    TInt entryId2(0);
    TInt iconId2(0);
    TInt countIconsAfterFirstAdd (0);
    TInt countIconsAfterSecondAdd (0);

    TRAPD(err,
          CCaInnerEntry *innerEntry = CCaInnerEntry::NewLC();
          innerEntry->SetTextL(KTestName);
          innerEntry->SetEntryTypeNameL(KTypeName);
          innerEntry->SetFlags(flag);
          innerEntry->SetRole(CCaInnerQuery::Group);
          innerEntry->SetIconDataL(_L("filename"), _L("skinId"), KNullDesC);
          mStorage->AddL(innerEntry);

          CCaSqlQuery *query = CCaSqlQuery::NewLC(((CCaSqLiteStorage *)mStorage)->iSqlDb);
          query->SetQueryL(KSQLGetIdCountFromIconTable);
          query->PrepareL();
          query->ExecuteL(countIconsAfterFirstAdd);
          CleanupStack::PopAndDestroy(query);

          // Add second entry with the some icon
          CCaInnerEntry *innerEntry2 = CCaInnerEntry::NewLC();
          innerEntry2->SetTextL(KTestName2);
          innerEntry2->SetEntryTypeNameL(KTypeName);
          innerEntry2->SetFlags(flag);
          innerEntry2->SetRole(CCaInnerQuery::Group);
          innerEntry2->SetIconDataL(KNullDesC, _L("skinId"), KNullDesC);
          mStorage->AddL(innerEntry2);

          query = CCaSqlQuery::NewLC(((CCaSqLiteStorage *)mStorage)->iSqlDb);
          query->SetQueryL(KSQLGetIdCountFromIconTable);
          query->PrepareL();
          query->ExecuteL(countIconsAfterSecondAdd);
          CleanupStack::PopAndDestroy(query);
          
          entryId1 = innerEntry->GetId();
          iconId1 = innerEntry->GetIconId();

          entryId2 = innerEntry2->GetId();
          iconId2 = innerEntry2->GetIconId();

          CleanupStack::PopAndDestroy(innerEntry2);
          CleanupStack::PopAndDestroy(innerEntry);
         );

    QCOMPARE(err, KErrNone);

    // New icon should be added
    QCOMPARE(countIconsAfterSecondAdd - countIconsAfterFirstAdd, 1);

    QVERIFY(entryId1 > 0);
    QVERIFY(iconId1 > 0);
    QVERIFY(entryId2 > 0);
    QVERIFY(iconId2 > 0);
    QVERIFY(!(entryId1 == entryId2));
    QVERIFY(!(iconId1 == iconId2));

    __UHEAP_MARKEND;
}


// ---------------------------------------------------------------------------
//
void CCAStorageTest::testAddWithNotCompleteIcon()
{
    TInt entryId(0);
    TInt iconId(0);
    __UHEAP_MARK;
    TRAPD(err,
          CCaInnerEntry *innerEntry = CCaInnerEntry::NewLC();
          innerEntry->SetTextL(KTestName);
          innerEntry->SetEntryTypeNameL(KTypeName);
          innerEntry->SetFlags(flag);
          innerEntry->SetRole(CCaInnerQuery::Group);
          innerEntry->SetIconDataL(_L("fake"), KNullDesC, KNullDesC);
          mStorage->AddL(innerEntry);

          entryId = innerEntry->GetId();
          iconId = innerEntry->GetIconId();

          CleanupStack::PopAndDestroy(innerEntry);
         );

    QCOMPARE(err, KErrNone);
    QVERIFY(entryId > 0);
    QVERIFY(iconId > 0);

    __UHEAP_MARKEND;
}

// ---------------------------------------------------------------------------
//
void CCAStorageTest::testAddUpdateIcon()
{
    TInt entryId(0);
    TInt iconId(0);
    TInt updatedEntryId(0);
    TInt updatedIconId(0);
    TInt countIconsAfterAdd (0);
    TInt countIconsAfterUpdate (0);

    RBuf updatedIconFileName;
    RBuf updatedIconSkinId;
    RBuf updatedIconAppId;
    TInt updatedIconIdFormIcon(0);

    __UHEAP_MARK;
    updatedIconFileName.CreateL( KMaxFileName );
    updatedIconSkinId.CreateL( KMaxFileName );
    updatedIconAppId.CreateL( KMaxUidName );

    TRAPD(err,
          CCaInnerEntry *innerEntry = CCaInnerEntry::NewLC();
          innerEntry->SetTextL(KTestName);
          innerEntry->SetEntryTypeNameL(KTypeName);
          innerEntry->SetFlags(flag);
          innerEntry->SetRole(CCaInnerQuery::Group);
          innerEntry->SetIconDataL(_L("filename"), _L("skinId"), _L("268458321"));
          mStorage->AddL(innerEntry);
          
          // Check db after add with icon
          CCaSqlQuery *query = CCaSqlQuery::NewLC(((CCaSqLiteStorage *)mStorage)->iSqlDb);
          query->SetQueryL(KSQLGetIdCountFromIconTable);
          query->PrepareL();
          query->ExecuteL(countIconsAfterAdd);
          CleanupStack::PopAndDestroy(query);
          
          entryId = innerEntry->GetId();
          iconId = innerEntry->GetIconId();

          innerEntry->SetTextL(KTestNameUpdate);
          innerEntry->SetFlags(flagUpdate);
          innerEntry->SetIconDataL( _L("filename2"), _L("skinId2"), _L("537001519"));
          mStorage->AddL(innerEntry);

          // Check db after update with icon
          query = CCaSqlQuery::NewLC(((CCaSqLiteStorage *)mStorage)->iSqlDb);
          query->SetQueryL(KSQLGetIdCountFromIconTable);
          query->PrepareL();
          query->ExecuteL(countIconsAfterUpdate);
          CleanupStack::PopAndDestroy(query);
          
          updatedEntryId = innerEntry->GetId();
          updatedIconId = innerEntry->GetIconId();

          updatedIconFileName = innerEntry->Icon()->FileName();
          updatedIconSkinId= innerEntry->Icon()->SkinId();
          updatedIconAppId = innerEntry->Icon()->ApplicationId();
          updatedIconIdFormIcon = innerEntry->Icon()->Id();



          CleanupStack::PopAndDestroy(innerEntry);
         );
    QCOMPARE(err, KErrNone);
    QVERIFY(entryId > 0);
    QVERIFY(iconId > 0);
    QCOMPARE(updatedEntryId, entryId);
    QCOMPARE(updatedIconId, iconId);
    QCOMPARE(updatedIconIdFormIcon, updatedIconId);
    QVERIFY(!updatedIconFileName.Compare(_L("filename2")));
    QVERIFY(!updatedIconSkinId.Compare(_L("skinId2")));
    QVERIFY(!updatedIconAppId.Compare(_L("537001519")));

    // No new icon should be added to DB
    QCOMPARE(countIconsAfterAdd, countIconsAfterUpdate);

    
    updatedIconFileName.Close();
    updatedIconSkinId.Close();
    updatedIconAppId.Close();    
    __UHEAP_MARKEND;
}

// ---------------------------------------------------------------------------
//
void CCAStorageTest::testAddUpdateIconOnlyFewParams()
{
    TInt entryId(0);
    TInt iconId(0);
    TInt updatedEntryId(0);
    TInt updatedIconId(0);
    TInt countIconsAfterAdd (0);
    TInt countIconsAfterUpdate (0);


    RBuf updatedIconFileName;
    RBuf updatedIconSkinId;
    RBuf updatedIconAppId;
    TInt updatedIconIdFormIcon(0);

    __UHEAP_MARK;
    updatedIconFileName.CreateL( KMaxFileName );
    updatedIconSkinId.CreateL( KMaxFileName );
    updatedIconAppId.CreateL( KMaxUidName );

    TRAPD(err,
          CCaInnerEntry *innerEntry = CCaInnerEntry::NewLC();
          innerEntry->SetTextL(KTestName);
          innerEntry->SetEntryTypeNameL(KTypeName);
          innerEntry->SetFlags(flag);
          innerEntry->SetRole(CCaInnerQuery::Group);
          innerEntry->SetIconDataL(_L("filename"), KNullDesC, _L("268458321"));
          mStorage->AddL(innerEntry);

          // Check db after add with icon
          CCaSqlQuery *query = CCaSqlQuery::NewLC(((CCaSqLiteStorage *)mStorage)->iSqlDb);
          query->SetQueryL(KSQLGetIdCountFromIconTable);
          query->PrepareL();
          query->ExecuteL(countIconsAfterAdd);
          CleanupStack::PopAndDestroy(query);

          
          entryId = innerEntry->GetId();
          iconId = innerEntry->GetIconId();

          innerEntry->SetTextL(KTestNameUpdate);
          innerEntry->SetFlags(flagUpdate);
          innerEntry->SetIconDataL( _L("filename2"), KNullDesC, _L("537001519"));
          mStorage->AddL(innerEntry);
          
          // Check db after update with icon
          query = CCaSqlQuery::NewLC(((CCaSqLiteStorage *)mStorage)->iSqlDb);
          query->SetQueryL(KSQLGetIdCountFromIconTable);
          query->PrepareL();
          query->ExecuteL(countIconsAfterUpdate);
          CleanupStack::PopAndDestroy(query);
          

          updatedEntryId = innerEntry->GetId();
          updatedIconId = innerEntry->GetIconId();

          updatedIconFileName = innerEntry->Icon()->FileName();
          updatedIconSkinId= innerEntry->Icon()->SkinId();
          updatedIconAppId = innerEntry->Icon()->ApplicationId();
          updatedIconIdFormIcon = innerEntry->Icon()->Id();



          CleanupStack::PopAndDestroy(innerEntry);
         );
    QCOMPARE(err, KErrNone);
    QVERIFY(entryId > 0);
    QVERIFY(iconId > 0);
    QCOMPARE(updatedEntryId, entryId);
    QCOMPARE(updatedIconId, iconId);
    QCOMPARE(updatedIconIdFormIcon, updatedIconId);
    QVERIFY(!updatedIconFileName.Compare(_L("filename2")));
    QVERIFY(!updatedIconSkinId.Compare(KNullDesC));
    QVERIFY(!updatedIconAppId.Compare(_L("537001519")));
    // No new icon should be added to DB
    QCOMPARE(countIconsAfterAdd, countIconsAfterUpdate);
    
    updatedIconFileName.Close();
    updatedIconSkinId.Close();
    updatedIconAppId.Close();    
    __UHEAP_MARKEND;
}



// ---------------------------------------------------------------------------
//
void CCAStorageTest::testAddUpdateWithEmptyIcon()
{
    TInt countIconsAfterAdd(0);
    TInt countIconsAfterUpdateWithEmptyIcon(0);
    TInt entryId(0);
    TInt iconId(0);
    __UHEAP_MARK;
    TRAPD(err,
          CCaInnerEntry *innerEntry = CCaInnerEntry::NewLC();
          innerEntry->SetTextL(KTestName);
          innerEntry->SetEntryTypeNameL(KTypeName);
          innerEntry->SetFlags(flag);
          innerEntry->SetRole(CCaInnerQuery::Group);
          innerEntry->SetIconDataL(_L("filename"), _L("skinId"), _L("268458321"));
          // Add entry with icon - not null icon
          mStorage->AddL(innerEntry);

          // Check db after add with icon
          CCaSqlQuery *query = CCaSqlQuery::NewLC(((CCaSqLiteStorage *)mStorage)->iSqlDb);
          query->SetQueryL(KSQLGetIdCountFromIconTable);
          query->PrepareL();

          query->ExecuteL(countIconsAfterAdd);


          CleanupStack::PopAndDestroy(query);

          entryId = innerEntry->GetId();
          iconId = innerEntry->GetIconId();

          //CCaInnerEntry::TIconAttributes attr;
          innerEntry->SetTextL(KTestNameUpdate);
          innerEntry->SetFlags(flagUpdate);
          //innerEntry->SetIconId( 0 );
          innerEntry->SetIconDataL(KNullDesC, KNullDesC, KNullDesC);
          //attr = innerEntry->GetIcon();
          // Update entry with empty icon
          mStorage->AddL(innerEntry);

          // Check db after update with empty icon
          query = CCaSqlQuery::NewLC(((CCaSqLiteStorage *)mStorage)->iSqlDb);
          query->SetQueryL(KSQLGetIdCountFromIconTable);
          query->PrepareL();
          query->ExecuteL(countIconsAfterUpdateWithEmptyIcon);

          CleanupStack::PopAndDestroy(query);
          CleanupStack::PopAndDestroy(innerEntry);
         );

    QCOMPARE(err, KErrNone);

    QVERIFY(entryId > 0);
    QVERIFY(iconId > 0);
    // icon should be removed from db because we set params to NULL
    QCOMPARE(countIconsAfterAdd - countIconsAfterUpdateWithEmptyIcon, 1);

    __UHEAP_MARKEND;
}

// ---------------------------------------------------------------------------
//
void CCAStorageTest::testAddUpdateWithEmptyIconAdvanced()
{
    TInt countIconsAfterAdd(0);
    TInt countIconsAfterUpdateWithEmptyIcon(0);
    TInt entryId(0);
    TInt iconId(0);
    __UHEAP_MARK;
    TRAPD(err,
          CCaInnerEntry *innerEntry = CCaInnerEntry::NewLC();
          innerEntry->SetTextL(KTestName);
          innerEntry->SetEntryTypeNameL(KTypeName);
          innerEntry->SetFlags(flag);
          innerEntry->SetRole(CCaInnerQuery::Group);
          innerEntry->SetIconDataL(_L("filename"), _L("skinId"), _L("268458321"));
          // Add entry with icon - not null icon
          mStorage->AddL(innerEntry);

          CCaInnerEntry *innerEntry2 = CCaInnerEntry::NewLC();
          innerEntry2->SetTextL(KTestName2);
          innerEntry2->SetEntryTypeNameL(KTypeName);
          innerEntry2->SetFlags(flag);
          innerEntry2->SetRole(CCaInnerQuery::Group);
          innerEntry2->SetIconDataL(_L("filename"), _L("skinId"), _L("268458321"));

          // Add second entry with the some icon - not null icon
          mStorage->AddL(innerEntry2);

          CCaSqlQuery *query = CCaSqlQuery::NewLC(((CCaSqLiteStorage *)mStorage)->iSqlDb);
          query->SetQueryL(KSQLGetIdCountFromIconTable);
          query->PrepareL();

          query->ExecuteL(countIconsAfterAdd);
          CleanupStack::PopAndDestroy(query);

          entryId = innerEntry->GetId();

          iconId = innerEntry->GetIconId();

          innerEntry->SetTextL(KTestNameUpdate);
          innerEntry->SetFlags(flagUpdate);
          //innerEntry->SetIconId( 0 );
          innerEntry->SetIconDataL(KNullDesC, KNullDesC, KNullDesC);
          // Update entry with empty icon
          mStorage->AddL(innerEntry);


          // Check db after update with empty icon

          query = CCaSqlQuery::NewLC(((CCaSqLiteStorage *)mStorage)->iSqlDb);
          query->SetQueryL(KSQLGetIdCountFromIconTable);
          query->PrepareL();
          query->ExecuteL(countIconsAfterUpdateWithEmptyIcon);
          CleanupStack::PopAndDestroy(query);
          CleanupStack::PopAndDestroy(innerEntry2);
          CleanupStack::PopAndDestroy(innerEntry);
         );
    QCOMPARE(err, KErrNone);

    QVERIFY(entryId > 0);
    QVERIFY(iconId > 0);

    // icon shouldn't be removed from db because it is use by other entry
    QCOMPARE(countIconsAfterUpdateWithEmptyIcon, countIconsAfterAdd);

    __UHEAP_MARKEND;
}


// ---------------------------------------------------------------------------
//
void CCAStorageTest::testAddUpdateWithExistingIcon()
{
    TInt countIconsAfterAdd(0);
    TInt countIconsAfterUpdateWithEmptyIcon(0);
    TInt entryId(0);
    TInt iconId(0);
    __UHEAP_MARK;
    TRAPD(err,
          CCaInnerEntry *innerEntry = CCaInnerEntry::NewLC();
          innerEntry->SetTextL(KTestName);
          innerEntry->SetEntryTypeNameL(KTypeName);
          innerEntry->SetFlags(flag);
          innerEntry->SetRole(CCaInnerQuery::Group);
          innerEntry->SetIconDataL(_L("filename"), _L("skinId"), _L("158321"));
          // Add entry with icon - not null icon
          mStorage->AddL(innerEntry);

          CCaInnerEntry *innerEntry2 = CCaInnerEntry::NewLC();
          innerEntry2->SetTextL(KTestName2);
          innerEntry2->SetEntryTypeNameL(KTypeName);
          innerEntry2->SetFlags(flag);
          innerEntry2->SetRole(CCaInnerQuery::Group);
          innerEntry2->SetIconDataL(_L("filename2"), _L("skinId2"), KNullDesC);

          // Add second entry with the some icon - not null icon
          mStorage->AddL(innerEntry2);

          CCaSqlQuery *query = CCaSqlQuery::NewLC(((CCaSqLiteStorage *)mStorage)->iSqlDb);
          query->SetQueryL(KSQLGetIdCountFromIconTable);
          query->PrepareL();

          query->ExecuteL(countIconsAfterAdd);
          CleanupStack::PopAndDestroy(query);

          entryId = innerEntry->GetId();

          iconId = innerEntry->GetIconId();

          innerEntry->SetTextL(KTestNameUpdate);
          innerEntry->SetFlags(flagUpdate);
          //innerEntry->SetIconId( 0 );
          innerEntry->SetIconDataL(_L("filename2"), _L("skinId2"), KNullDesC);
          // Update entry with empty icon
          mStorage->AddL(innerEntry);


          // Check db after update with empty icon

          query = CCaSqlQuery::NewLC(((CCaSqLiteStorage *)mStorage)->iSqlDb);
          query->SetQueryL(KSQLGetIdCountFromIconTable);
          query->PrepareL();
          query->ExecuteL(countIconsAfterUpdateWithEmptyIcon);
          CleanupStack::PopAndDestroy(query);
          CleanupStack::PopAndDestroy(innerEntry2);
          CleanupStack::PopAndDestroy(innerEntry);
         );
    QCOMPARE(err, KErrNone);

    QVERIFY(entryId > 0);
    QVERIFY(iconId > 0);

    // icon shouldn't be removed from db because it is use by other entry
    QCOMPARE(countIconsAfterUpdateWithEmptyIcon, countIconsAfterAdd);

    __UHEAP_MARKEND;
}



// ---------------------------------------------------------------------------
//
void CCAStorageTest::testAddAttributes()
{
    TInt err(KErrGeneral);
    __UHEAP_MARK;
    TRAP(err,
         CCaInnerEntry *innerEntry = CCaInnerEntry::NewL();
         RPointerArray<CCaInnerEntry> innerEntryArray;
         CleanupResetAndDestroyPushL(innerEntryArray);
         innerEntryArray.AppendL(innerEntry);

         innerEntry->SetTextL(KTestName);
         innerEntry->SetEntryTypeNameL(KTypeName);
         innerEntry->SetFlags(flag);
         innerEntry->SetRole(CCaInnerQuery::Group);
         innerEntry->SetIconDataL(_L("filename"), _L("skinId"), _L("268458321"));

         innerEntry->AddAttributeL(KAttrName_1, KAttrValue_1);
         innerEntry->AddAttributeL(KAttrName_2, KAttrValue_2);
         innerEntry->AddAttributeL(KAttrName_3, KAttrValue_3);

         mStorage->AddL(innerEntry);
         // check status
         CCaSqlQuery *query = CCaSqlQuery::NewLC(((CCaSqLiteStorage *)mStorage)->iSqlDb);
         query->SetQueryL(KSQLCheckAttribute);
         query->SetTableType(CCaSqlQuery::EAttributeTable);
         query->PrepareL();
         query->BindEntryIdL(innerEntry->GetId());
         RPointerArray<CCaInnerEntry> entryArray;
         CleanupResetAndDestroyPushL(entryArray);
         TInt count = query->ExecuteL(entryArray, CCaSqlQuery::EAttribute);
         query->CloseStatement();
         CleanupStack::PopAndDestroy(&entryArray);

         if (innerEntry->GetId() > 0)
         if (innerEntry->GetIconId() > 0)
        if (innerEntry->GetAttributes().Count() == count) {
            err = KErrNone;
        } else
            err = KErrArgument;
            else
                err = KErrNotFound;
                else
                    err = KErrNotFound;

                    if (!err) {
                        query->SetQueryL(KSQLGetAttribute);
                            query->SetTableType(CCaSqlQuery::EAttributeTable);
                            query->PrepareL();
                            query->BindValuesForAddAttributeL(innerEntry, NULL);
                            query->ExecuteL(innerEntryArray, CCaSqlQuery::EAttribute);
                            query->CloseStatement();
                            RCaEntryAttrArray attributes = innerEntry->GetAttributes();

                            if (!(
                                        (attributes[0])->Name().Compare(KAttrName_1)
                                        && (attributes[0])->Value().Compare(KAttrValue_1)
                                        && (attributes[1])->Name().Compare(KAttrName_2)
                                        && (attributes[1])->Value().Compare(KAttrValue_2)
                                        && (attributes[2])->Name().Compare(KAttrName_3)
                                        && (attributes[2])->Value().Compare(KAttrValue_3)
                                    ))
                                err = KErrNone;
                            else
                                err = KErrNotSupported;
                        }

    CleanupStack::PopAndDestroy(query);
    CleanupStack::PopAndDestroy(&innerEntryArray);
        );

    __UHEAP_MARKEND;
    QCOMPARE(err, KErrNone);
}
// ---------------------------------------------------------------------------
//
void CCAStorageTest::testUpdateAttributes()
{
    TInt err(KErrGeneral);
    __UHEAP_MARK;
//    TRAP(err,
         CCaInnerEntry *innerEntry = CCaInnerEntry::NewL();
         RPointerArray<CCaInnerEntry> innerEntryArray;
         CleanupResetAndDestroyPushL(innerEntryArray);
         innerEntryArray.AppendL(innerEntry);

         innerEntry->SetTextL(KTestName);
         innerEntry->SetEntryTypeNameL(KTypeName);
         innerEntry->SetFlags(flag);
         innerEntry->SetRole(CCaInnerQuery::Group);
         innerEntry->SetIconDataL(_L("filename"), _L("skinId"), _L("268458321"));

         innerEntry->AddAttributeL(KAttrName_1, KAttrValue_1);
         innerEntry->AddAttributeL(KAttrName_2, KAttrValue_2);
         innerEntry->AddAttributeL(KAttrName_3, KAttrValue_3);

         mStorage->AddL(innerEntry);
         // check status
         CCaSqlQuery *query = CCaSqlQuery::NewLC(((CCaSqLiteStorage *)mStorage)->iSqlDb);
         query->SetQueryL(KSQLCheckAttribute);
         query->SetTableType(CCaSqlQuery::EAttributeTable);
         query->PrepareL();
         query->BindEntryIdL(innerEntry->GetId());
         RPointerArray<CCaInnerEntry> entryArray;
         CleanupResetAndDestroyPushL(entryArray);
         TInt count = query->ExecuteL(entryArray, CCaSqlQuery::EAttribute);
         query->CloseStatement();
         CleanupStack::PopAndDestroy(&entryArray);

         if (innerEntry->GetId() > 0)
         if (innerEntry->GetIconId() > 0)
        if (innerEntry->GetAttributes().Count() == count) {
            err = KErrNone;
        } else
            err = KErrArgument;
            else
                err = KErrNotFound;
                else
                    err = KErrNotFound;

                    if (!err) {
                        innerEntry->AddAttributeL(KAttrName_2, KAttrName_1);
                            mStorage->AddL(innerEntry);
                            // check status
                            query->SetQueryL(KSQLCheckAttribute);
                            query->SetTableType(CCaSqlQuery::EAttributeTable);
                            query->PrepareL();
                            query->BindEntryIdL(innerEntry->GetId());
                            RPointerArray<CCaInnerEntry> entryArray;
                            CleanupResetAndDestroyPushL(entryArray);
                            TInt count = query->ExecuteL(entryArray, CCaSqlQuery::EAttribute);
                            query->CloseStatement();
                            CleanupStack::PopAndDestroy(&entryArray);

                            RCaEntryAttrArray attributes = innerEntry->GetAttributes();
                            if (innerEntry->GetAttributes().Count() == count) {
                                if (!(
                                            (attributes[0])->Name().Compare(KAttrName_1)
                                            && (attributes[0])->Value().Compare(KAttrValue_1)
                                            && (attributes[1])->Name().Compare(KAttrName_2)
                                            && (attributes[1])->Value().Compare(KAttrValue_1)
                                            && (attributes[2])->Name().Compare(KAttrName_3)
                                            && (attributes[2])->Value().Compare(KAttrValue_3)
                                )) {
                                    innerEntry->RemoveAttributeL(KAttrName_1);
                                    innerEntry->RemoveAttributeL(KAttrName_2);
                                    innerEntry->RemoveAttributeL(KAttrName_3);
                                    query->SetQueryL(KSQLGetAttribute);
                                    query->SetTableType(CCaSqlQuery::EAttributeTable);
                                    query->PrepareL();
                                    query->BindValuesForAddAttributeL(innerEntry, NULL);
                                    query->ExecuteL(innerEntryArray, CCaSqlQuery::EAttribute);
                                    query->CloseStatement();

                                    RCaEntryAttrArray newAttributes = innerEntry->GetAttributes();
                                    if (!(
                                                (newAttributes[0])->Name().Compare(KAttrName_1)
                                                && (newAttributes[0])->Value().Compare(KAttrValue_1)
                                                && (newAttributes[1])->Name().Compare(KAttrName_2)
                                                && (newAttributes[1])->Value().Compare(KAttrValue_1)
                                                && (newAttributes[2])->Name().Compare(KAttrName_3)
                                                && (newAttributes[2])->Value().Compare(KAttrValue_3)
                                            ))
                                        err = KErrNone;
                                    else
                                        err = KErrNotSupported;
                                } else {
                                    err = KErrNotReady;
                                }
                            } else
                                err = KErrNotReady;
                        }

    CleanupStack::PopAndDestroy(query);
    CleanupStack::PopAndDestroy(&innerEntryArray);
//        );
    __UHEAP_MARKEND;
    QCOMPARE(err, KErrNone);
}
// ---------------------------------------------------------------------------
//
void CCAStorageTest::testDeleteAttributes()
{
    TInt entryId(0);
    TInt iconId(0);
    TInt attributesACount(0);
    TInt queryAResultCount(0);
    TInt attributesBCount(0);
    TInt queryBResultCount(0);
    __UHEAP_MARK;
    TRAPD(err,
          // A query section starts
          CCaInnerEntry *innerEntry = CCaInnerEntry::NewLC();
          innerEntry->SetTextL(KTestName);
          innerEntry->SetEntryTypeNameL(KTypeName);
          innerEntry->SetFlags(flag);
          innerEntry->SetRole(CCaInnerQuery::Group);
          innerEntry->SetIconDataL(_L("filename"), _L("skinId"), _L("268458321"));

          innerEntry->AddAttributeL(KAttrName_1, KAttrValue_1);
          innerEntry->AddAttributeL(KAttrName_2, KAttrValue_2);
          innerEntry->AddAttributeL(KAttrName_3, KAttrValue_3);

          mStorage->AddL(innerEntry);
          // check status
          CCaSqlQuery *query = CCaSqlQuery::NewLC(((CCaSqLiteStorage *)mStorage)->iSqlDb);
          query->SetQueryL(KSQLCheckAttribute);
          query->SetTableType(CCaSqlQuery::EAttributeTable);
          query->PrepareL();
          query->BindEntryIdL(innerEntry->GetId());
          RPointerArray<CCaInnerEntry> entryArray;
          CleanupResetAndDestroyPushL(entryArray);
          queryAResultCount = query->ExecuteL(entryArray, CCaSqlQuery::EAttribute);
          query->CloseStatement();
          CleanupStack::PopAndDestroy(&entryArray);

    if (!err) {
    entryId = innerEntry->GetId();
        iconId = innerEntry->GetIconId();
        attributesACount = innerEntry->GetAttributes().Count();

        // B query section starts

        innerEntry->RemoveAttributeL(KAttrName_1);
        innerEntry->RemoveAttributeL(KAttrName_2);
        innerEntry->RemoveAttributeL(KAttrName_3);
        mStorage->AddL(innerEntry);
        // check status
        query->SetQueryL(KSQLCheckAttribute);
        query->SetTableType(CCaSqlQuery::EAttributeTable);
        query->PrepareL();
        query->BindEntryIdL(innerEntry->GetId());
        RPointerArray<CCaInnerEntry> entryArray;
        CleanupResetAndDestroyPushL(entryArray);
        queryBResultCount= query->ExecuteL(entryArray, CCaSqlQuery::EAttribute);
        query->CloseStatement();
        CleanupStack::PopAndDestroy(&entryArray);
        attributesBCount = innerEntry->GetAttributes().Count();
    }
    CleanupStack::PopAndDestroy(query);
    CleanupStack::PopAndDestroy(innerEntry);
         );
    __UHEAP_MARKEND;

    QVERIFY(entryId > 0);
    QVERIFY(iconId > 0);
    QCOMPARE(queryAResultCount, attributesACount);
    QCOMPARE(queryBResultCount, 0);
    QCOMPARE(queryBResultCount, attributesBCount);
}
// end of file