taskswitcher/server/tsrc/t_tsstorage/t_tsstorage.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 <QtTest/QtTest>
#include "tsstorage.h"
#include "t_tsstorage.h"
#include "t_tsmodel.h"

const int KBigSet(100);

void T_TsStorage::testItemDataChanged()
{
    CTsStorage* storage(0);
    T_TsModel emptyProvider(0),
              bigSetProvider(KBigSet);
    RPointerArray<MTsModel>providers;
    CleanupClosePushL(providers);
    
    //storage has no data providers
    mNotifications = 0;
    storage = CTsStorage::NewL(providers.Array());
    storage->dataChanged(bigSetProvider.itemL(0));
    QVERIFY(0 == mNotifications);//storage has no observers
    verifyProviders(*storage, providers.Array());
    storage->setObserver(this);
    storage->dataChanged(bigSetProvider.itemL(0));
    QVERIFY(1 == mNotifications);//storage has observers
    verifyProviders(*storage, providers.Array());
    delete storage;
    
    providers.Append(&emptyProvider);
    providers.Append(&bigSetProvider);
    
    //storage has some
    mNotifications = 0;
    storage = CTsStorage::NewL(providers.Array());
    storage->dataChanged(bigSetProvider.itemL(0));
    QVERIFY(0 == mNotifications);//storage has no observers
    verifyProviders(*storage, providers.Array());
    storage->setObserver(this);
    storage->dataChanged(bigSetProvider.itemL(0));
    QVERIFY(1 == mNotifications);//storage has observers
    verifyProviders(*storage, providers.Array());
    delete storage;
    CleanupStack::PopAndDestroy(&providers);
}

void T_TsStorage::testModelDataChanged()
{
    CTsStorage* storage(0);
    T_TsModel emptyProvider(0),
              bigSetProvider(KBigSet);
    RPointerArray<MTsModel>providers;
    CleanupClosePushL(providers);
    
    //storage has no data providers
    mNotifications = 0;
    storage = CTsStorage::NewL(providers.Array());
    storage->dataChanged(emptyProvider);
    QVERIFY(0 == mNotifications);//storage has no observers
    verifyProviders(*storage, providers.Array());
    storage->setObserver(this);
    storage->dataChanged(emptyProvider);
    QVERIFY(1 == mNotifications);//storage has observers
    verifyProviders(*storage, providers.Array());
    delete storage;
    
    providers.Append(&emptyProvider);
    providers.Append(&bigSetProvider);
    
    //storage has some
    mNotifications = 0;
    storage = CTsStorage::NewL(providers.Array());
    storage->dataChanged(emptyProvider);
    QVERIFY(0 == mNotifications);//storage has no observers
    verifyProviders(*storage, providers.Array());
    storage->setObserver(this);
    storage->dataChanged(emptyProvider);
    QVERIFY(1 == mNotifications);//storage has observers
    verifyProviders(*storage, providers.Array());
    delete storage;
    CleanupStack::PopAndDestroy(&providers);
}

void T_TsStorage::verifyProviders(const MTsModel &set, const TArray<MTsModel*>& providers)
{
    int sum(0);
    for (int iter(0); iter < providers.Count(); ++iter) {
        sum += (providers[iter])->count();
        QVERIFY(set.count() >= (providers[iter])->count());
        QVERIFY(EFalse != isSubset(set, *(providers[iter])));
    }
    QVERIFY(set.count() == sum);
}

TBool T_TsStorage::isSubset(const MTsModel &set, const MTsModel& subset) const
{
    bool isOk = true;
    for (int i(0); i < subset.count() && isOk; ++i) {
        isOk = false;
        for(int k(0); k < set.count() && !isOk; ++k){
            set.itemL(k).launchL();
            set.itemL(k).closeL();
            isOk = (subset.itemL(i).keyL() == set.itemL(k).keyL()) &&
                   (subset.itemL(i).timestampL() == set.itemL(k).timestampL()) &&
                   (subset.itemL(i).iconHandleL() == set.itemL(k).iconHandleL()) &&
                   (subset.itemL(i).isActiveL() == set.itemL(k).isActiveL()) &&
                   (subset.itemL(i).isClosableL() == set.itemL(k).isClosableL()) &&
                   (0 == subset.itemL(i).displayNameL().Compare(set.itemL(k).displayNameL()));
        }
    }
    return isOk ? ETrue : EFalse;
}

QTEST_MAIN(T_TsStorage)