homescreenapp/serviceproviders/hsmenuserviceprovider/tsrc/t_hsmenuserviceprovider/src/t_menuitemmodel.cpp
changeset 90 3ac3aaebaee5
child 97 66b5fe3c07fd
equal deleted inserted replaced
86:e4f038c420f7 90:3ac3aaebaee5
       
     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 #include <qcoreapplication.h>
       
    18 #include <qstandarditemmodel.h>
       
    19 #include "caquery.h"
       
    20 #include <hstest_global.h>
       
    21 #include "hsmenuservice.h"
       
    22 #include "hsmenucollectionsitemmodel.h"
       
    23 #include "hsmenuitemmodel.h"
       
    24 #include "hsmenuserviceutils.h"
       
    25 #include "t_hsmenuserviceprovider.h"
       
    26 #include "hsiconsidleloader.h"
       
    27 
       
    28 void MenuServiceTest::HsMenuItemModel_construction()
       
    29 {
       
    30     QScopedPointer<QObject> parent(new QObject);
       
    31     CaQuery query;
       
    32     query.mQueryId = "__HsMenuItemModel_construction_CaQuery";
       
    33     HsMenuItemModel model(query, parent.data());
       
    34     QCOMPARE(model.mCaItemModelQuery.mQueryId, query.mQueryId);
       
    35     QCOMPARE(model.QObject::parent(), parent.data());
       
    36     QVERIFY(model.getIconSize().isValid());
       
    37 }
       
    38 
       
    39 void MenuServiceTest::HsMenuItemModel_setSort()
       
    40 {
       
    41     HsMenuItemModel model(CaQuery(), this);
       
    42     
       
    43     {
       
    44         const Hs::HsSortAttribute sortAttribute = Hs::NoHsSortAttribute;
       
    45     
       
    46         model.setSort(sortAttribute);
       
    47         QCOMPARE(model.mSetSortAttribute, HsMenuServiceUtils::sortBy(sortAttribute));
       
    48         QCOMPARE(model.mSetSortOrder, HsMenuServiceUtils::sortOrder(sortAttribute));
       
    49     }
       
    50     {
       
    51         const Hs::HsSortAttribute sortAttribute = Hs::DescendingNameHsSortAttribute;
       
    52     
       
    53         model.setSort(sortAttribute);
       
    54         QCOMPARE(model.mSetSortAttribute, HsMenuServiceUtils::sortBy(sortAttribute));
       
    55         QCOMPARE(model.mSetSortOrder, HsMenuServiceUtils::sortOrder(sortAttribute));        
       
    56     }
       
    57 }
       
    58 
       
    59 void MenuServiceTest::HsMenuItemModel_getData()
       
    60 {
       
    61     HsMenuItemModel model(CaQuery(), this);
       
    62     
       
    63     Qt::ItemDataRole role = Qt::DecorationRole;
       
    64     const QModelIndex index = model.index(0, 0);
       
    65     const QVariant expectedDecoration(QString("entryText"));
       
    66     QList<QVariant> expectedResult;
       
    67     expectedResult << expectedDecoration;
       
    68     expectedResult << HbIcon(Hs::newIconId);
       
    69     
       
    70     model.mRootResult = index;
       
    71     model.mEntryResult->mFlagsResult = 0;
       
    72     model.mEntryResult->mEntryTypeNameResult = Hs::collectionDownloadedTypeName;
       
    73     model.setSecondLineVisibility(true);
       
    74     model.mDataResult = QVariant(expectedDecoration);
       
    75 
       
    76     QList<QVariant> actualResult = model.data(index, role).toList();
       
    77     
       
    78     QCOMPARE(actualResult.at(0).toString(), 
       
    79         expectedDecoration.toString());
       
    80     QCOMPARE(actualResult.at(1).value<HbIcon>().iconName(), 
       
    81         QString(Hs::newIconId));
       
    82     
       
    83     QCOMPARE(model.data(index), model.mDataResult);
       
    84 }
       
    85 
       
    86 
       
    87 void MenuServiceTest::HsMenuItemModel_newIconNeeded()
       
    88 {
       
    89     HsMenuItemModel model(CaQuery(), this);
       
    90     
       
    91     const QModelIndex index = model.index(0, 0);
       
    92     model.mRootResult = index;
       
    93     model.mEntryResult->mFlagsResult = 0;
       
    94     model.mEntryResult->mEntryTypeNameResult = Hs::collectionDownloadedTypeName;
       
    95     model.setSecondLineVisibility(true);
       
    96     QVERIFY(model.newIconNeeded(index));
       
    97     
       
    98     model.mRootResult = QModelIndex(); // invalid
       
    99     QVERIFY(!model.root().isValid() && 
       
   100         !model.newIconNeeded(index));
       
   101     
       
   102     model.mRootResult = index;
       
   103     model.mEntryResult->mFlagsResult = UsedEntryFlag;
       
   104     QVERIFY((model.entry(index)->flags() & UsedEntryFlag) && 
       
   105         !model.newIconNeeded(index));
       
   106     
       
   107     model.mEntryResult->mFlagsResult = 0;
       
   108     model.mEntryResult->mEntryTypeNameResult = "";
       
   109     QVERIFY((model.entry(index)->entryTypeName() != Hs::collectionDownloadedTypeName ) && 
       
   110         !model.newIconNeeded(index));
       
   111     
       
   112 }
       
   113 
       
   114 
       
   115 void MenuServiceTest::HsMenuItemModel_uninstallChange()
       
   116 {
       
   117     HsMenuItemModel model(CaQuery(), this);
       
   118     const QModelIndex index = model.index(0, 0);
       
   119     model.mRootResult = index;
       
   120     model.mEntryResult->mFlagsResult = 0;
       
   121     model.mEntryResult->setAttribute(QString("component_id"), 
       
   122             QString().setNum(1));
       
   123     
       
   124     model.uninstallChange(1, 5);
       
   125     QCOMPARE(model.mComponentId, 1);
       
   126     QCOMPARE(model.mIds.count(), 2);
       
   127     QCOMPARE(model.mEntryResult->attribute(
       
   128             QString("uninstall_progress")), QString("5"));
       
   129     
       
   130     model.uninstallChange(1, 20);
       
   131     QCOMPARE(model.mEntryResult->attribute(
       
   132                 QString("uninstall_progress")), QString("20"));
       
   133 }
       
   134 
       
   135 void MenuServiceTest::HsMenuCollectionsItemModel_construction()
       
   136 {
       
   137     QScopedPointer<QObject> parent(new QObject);
       
   138     CaQuery query;
       
   139     query.mQueryId = "__HsMenuCollectionItemModel_construction_CaQuery";
       
   140     HsMenuCollectionsItemModel model(query, parent.data());
       
   141     QCOMPARE(model.mCaItemModelQuery.mQueryId, query.mQueryId);
       
   142     QCOMPARE(model.QObject::parent(), parent.data());
       
   143     QVERIFY(model.getIconSize().isValid());
       
   144 }
       
   145 
       
   146 
       
   147 
       
   148 void MenuServiceTest::HsMenuCollectionsItemModel_getData()
       
   149 {
       
   150     HsMenuCollectionsItemModel model(CaQuery(), this);
       
   151     
       
   152     const QModelIndex index = model.index(0, 0);
       
   153     const QString expectedText("entryText");
       
   154     const QString expectedSecondLine(hbTrId("txt_applib_dblist_downloaded_val_empty"));
       
   155     
       
   156     model.mRootResult = index;
       
   157     model.mEntryResult->mFlagsResult = 0;
       
   158     model.mEntryResult->mTextResult = expectedText;
       
   159     model.setSecondLineVisibility(true);
       
   160 
       
   161     QList<QVariant> actualResult = model.data(index).toList();
       
   162     
       
   163     QCOMPARE(actualResult.at(0).toString(), 
       
   164         expectedText);
       
   165     QCOMPARE(actualResult.at(1).toString(), 
       
   166         expectedSecondLine);
       
   167 
       
   168     
       
   169     Qt::ItemDataRole role = Qt::DecorationRole;
       
   170     const QVariant expectedData;
       
   171 
       
   172     model.mDataResult = expectedData;
       
   173     QCOMPARE(model.data(index, role), expectedData);
       
   174 
       
   175     model.setSecondLineVisibility(false);
       
   176     QCOMPARE(model.data(index), expectedData);
       
   177 }
       
   178 // ---------------------------------------------------------------------------
       
   179 // ---------------------------------------------------------------------------
       
   180 void MenuServiceTest::HsMenuCollectionsItemModel_getSecondLine()
       
   181 {    
       
   182     HsMenuCollectionsItemModel model(CaQuery(), this);
       
   183     Qt::ItemDataRole role = Qt::DisplayRole;
       
   184     model.setSecondLineVisibility(true);
       
   185     const QModelIndex index = model.index(0, 0);
       
   186     
       
   187     // set up mock data    
       
   188    
       
   189     // make up 2 items to be stored in a collection
       
   190     QSharedPointer<CaEntry> collectionItemA(new CaEntry);
       
   191     QString collectionItemAName("A");
       
   192     collectionItemA->mTextResult = collectionItemAName;
       
   193     QSharedPointer<CaEntry> collectionItemB(new CaEntry);
       
   194     QString collectionItemBName("B");
       
   195     collectionItemB->mTextResult = collectionItemBName;
       
   196     
       
   197     // make sure CaService will return the 2 entries on getEntries call
       
   198     CaService::instance()->mGetEntriesResult.append(collectionItemA);
       
   199     CaService::instance()->mGetEntriesResult.append(collectionItemB);
       
   200     
       
   201     QString expectedSecondLine(hbTrId("txt_applib_dblist_val_ln_new_applications",
       
   202         CaService::instance()->mGetEntriesResult.count()));
       
   203     
       
   204     // make data() call getSecondLine for 'downloaded' collection
       
   205     model.mEntryResult->setEntryTypeName(Hs::collectionDownloadedTypeName);
       
   206     model.mEntryResult->mIdResult = 5; // just to have any id
       
   207     
       
   208     QString actualSecondLine = model.data(index, role).toList().at(1).toString();
       
   209     
       
   210     QCOMPARE(actualSecondLine, expectedSecondLine);    
       
   211 
       
   212     // check what CaQuery was prepared by getSecondLine()
       
   213     CaQuery resultQuery = CaService::instance()->mGetEntriesQuery;
       
   214     QCOMPARE(resultQuery.mSetParentId, model.mEntryResult->mIdResult);
       
   215     QCOMPARE(resultQuery.mSetEntryRoles, ItemEntryRole);
       
   216     const EntryFlags expectedCollectionDownloadedFlags = 
       
   217         RemovableEntryFlag | VisibleEntryFlag;
       
   218     QCOMPARE(resultQuery.mSetFlagsOn, expectedCollectionDownloadedFlags);
       
   219    
       
   220     
       
   221     // case for static collection
       
   222     const EntryFlags expectedCollectionFlag = VisibleEntryFlag;
       
   223     expectedSecondLine = collectionItemAName + "," + collectionItemBName;
       
   224     
       
   225     model.mEntryResult->setEntryTypeName(Hs::collectionTypeName);
       
   226     
       
   227     actualSecondLine = model.data(index, role).toList().at(1).toString();
       
   228     
       
   229     QCOMPARE(actualSecondLine, expectedSecondLine);
       
   230 
       
   231     resultQuery = CaService::instance()->mGetEntriesQuery;
       
   232     QCOMPARE(resultQuery.mSetFlagsOn, expectedCollectionFlag);
       
   233 }
       
   234 
       
   235 // ---------------------------------------------------------------------------
       
   236 //
       
   237 // ---------------------------------------------------------------------------
       
   238 //
       
   239 void MenuServiceTest::HsMenuItemModel_preloadIcons()
       
   240 {
       
   241     HsMenuItemModel model(CaQuery(), this);
       
   242     
       
   243     // set up mock data
       
   244     Qt::ItemDataRole role = Qt::DecorationRole;
       
   245     const QModelIndex index = model.index(0, 0);
       
   246     const QVariant expectedDecoration(QString("entryText"));
       
   247     QList<QVariant> expectedResult;
       
   248     expectedResult << expectedDecoration;
       
   249     expectedResult << HbIcon(Hs::newIconId);
       
   250     model.mRootResult = index;
       
   251     model.mEntryResult->mFlagsResult = 0;
       
   252     model.mEntryResult->mEntryTypeNameResult = Hs::collectionDownloadedTypeName;
       
   253     model.setSecondLineVisibility(true);
       
   254     model.mDataResult = QVariant(expectedDecoration);
       
   255     model.mRowCountResult = 1; // i.e. how many rows the mock model pretends to have
       
   256     QList<QVariant> actualResult = model.data(index, role).toList();
       
   257     model.mEntryResult->mMakeIconResult = HbIcon(Hs::newIconId);
       
   258     
       
   259     // verify mock setup
       
   260     QCOMPARE(actualResult.at(0).toString(), 
       
   261         expectedDecoration.toString());
       
   262     QCOMPARE(actualResult.at(1).value<HbIcon>().iconName(), 
       
   263         QString(Hs::newIconId));
       
   264     QCOMPARE(model.data(index), model.mDataResult);
       
   265     
       
   266     // invoke icon preloader construction
       
   267     model.preloadIcons();
       
   268     QVERIFY(model.mIconsIdleLoader);
       
   269     
       
   270     // invoke icon preloading
       
   271 
       
   272     model.mIconsIdleLoader->processWhenIdle();
       
   273     
       
   274     // compare what has been logged in mock object with referece data
       
   275     QCOMPARE(model.mEntryResult->mMakeIconSize, model.getIconSize());
       
   276     QCOMPARE(model.mEntryResult->mMakeIconResult.iconName(),
       
   277         QString(Hs::newIconId));
       
   278 }
       
   279 
       
   280 // ---------------------------------------------------------------------------
       
   281 //
       
   282 // ---------------------------------------------------------------------------
       
   283 //
       
   284 void MenuServiceTest::HsIconsIdleLoader_construction()
       
   285 {
       
   286     // precondtitons - prepare model
       
   287     HsMenuItemModel model(CaQuery(), this);
       
   288     
       
   289     // set up mock data
       
   290     Qt::ItemDataRole role = Qt::DecorationRole;
       
   291     const QModelIndex index = model.index(0, 0);
       
   292     const QVariant expectedDecoration(QString("entryText"));
       
   293     QList<QVariant> expectedResult;
       
   294     expectedResult << expectedDecoration;
       
   295     expectedResult << HbIcon(Hs::newIconId);
       
   296     model.mRootResult = index;
       
   297     model.mEntryResult->mFlagsResult = 0;
       
   298     model.mEntryResult->mEntryTypeNameResult = Hs::collectionDownloadedTypeName;
       
   299     model.setSecondLineVisibility(true);
       
   300     model.mDataResult = QVariant(expectedDecoration);
       
   301     model.mRowCountResult = 1; // i.e. how many rows the mock model pretends to have
       
   302     QList<QVariant> actualResult = model.data(index, role).toList();
       
   303     model.mEntryResult->mMakeIconResult = HbIcon(Hs::newIconId);
       
   304     
       
   305     QCOMPARE(actualResult.at(0).toString(), 
       
   306         expectedDecoration.toString());
       
   307     QCOMPARE(actualResult.at(1).value<HbIcon>().iconName(), 
       
   308         QString(Hs::newIconId));
       
   309     
       
   310     QCOMPARE(model.data(index), model.mDataResult);
       
   311     
       
   312     // test
       
   313     HsIconsIdleLoader *iconsIdleLoader = new HsIconsIdleLoader(&model);
       
   314     
       
   315     QVERIFY(iconsIdleLoader->mModel);
       
   316     QVERIFY(iconsIdleLoader->mTimer);
       
   317     QVERIFY(iconsIdleLoader->mTimer->isActive());
       
   318     QTest::qWait(10000);
       
   319     QVERIFY(!iconsIdleLoader->mTimer->isActive());
       
   320     
       
   321     delete iconsIdleLoader;
       
   322 }
       
   323 
       
   324 // ---------------------------------------------------------------------------
       
   325 // ---------------------------------------------------------------------------
       
   326 int MenuServiceTest::getId(HsMenuItemModel *model , int idx)
       
   327 {
       
   328     return model->data(model->index(idx), CaItemModel::IdRole).toInt();
       
   329 }
       
   330