logsui/logsengine/tsrc/at_logsengine/src/at_logsengine.cpp
changeset 0 4a5361db8937
child 21 2f0af9ba7665
equal deleted inserted replaced
-1:000000000000 0:4a5361db8937
       
     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 "at_logsengine.h"
       
    18 #include <logsmodel.h>
       
    19 #include <logsevent.h>
       
    20 #include <logsfilter.h>
       
    21 #include <logscustomfilter.h>
       
    22 #include <apacmdln.h>
       
    23 #include <apgtask.h>
       
    24 #include <apgcli.h>
       
    25 #include <hbicon.h>
       
    26 
       
    27 #include <QtTest/QtTest>
       
    28 
       
    29 _LIT( KLogTestEvents, "at_logsengine" );
       
    30 _LIT( KLogTestClientWriter, "logclienttest.exe" );
       
    31 _LIT( KLogTestClientClearer, "clearlisttest.exe" );
       
    32 
       
    33 const int logsTestAllEventsCount = 20;
       
    34 const int logsTestMissedCallsCount = 10;
       
    35 const int logsTestOtherThanMissedEventsCount = 10;
       
    36 const int logsTestContact2EventsCount = 2;
       
    37 const int logsTestContact3EventsCount = 3;
       
    38 
       
    39 // Note, events are written from .dat file in order where last event is going to
       
    40 // be first event at db
       
    41 const QString logsTestAllEvent1Name = "deeppu";
       
    42 const LogsEvent::LogsDirection logsTestAllEvent1Dir = LogsEvent::DirIn;
       
    43 
       
    44 const QString logsTestContact2Event2Name = "Jeppis";
       
    45 const LogsEvent::LogsDirection logsTestContact2Event2Dir = LogsEvent::DirIn;
       
    46 const QString logsTestContact2Event1Name = "Jeppa";
       
    47 const LogsEvent::LogsDirection logsTestContact2Event1Dir = LogsEvent::DirMissed;
       
    48 
       
    49 const QString logsTestContact3Event3Name = "aeppu";
       
    50 const LogsEvent::LogsDirection logsTestContact3Event3Dir = LogsEvent::DirOut;
       
    51 const QString logsTestContact3Event2Name = "beppu";
       
    52 const LogsEvent::LogsDirection logsTestContact3Event2Dir = LogsEvent::DirIn;
       
    53 const QString logsTestContact3Event1Name = "deppu";
       
    54 const LogsEvent::LogsDirection logsTestContact3Event1Dir = LogsEvent::DirOut;
       
    55 
       
    56 const QString logsTestHomeScreenMissedCallerName = "Jeppa";
       
    57 
       
    58 Q_DECLARE_METATYPE(LogsEvent *)
       
    59 
       
    60 #define AT_LOGSENG_VERIFY(m, idx, remote, dir) { \
       
    61 LogsEvent* event = qVariantValue<LogsEvent*>( m.data( m.index(idx, 0), LogsModel::RoleFullEvent ) ); \
       
    62 QVERIFY( event && event->remoteParty() == remote && event->direction() == dir ); }
       
    63 
       
    64 void AT_LogsEngine::initTestCase()
       
    65 {
       
    66     TRAPD( err, clearEventsL() );
       
    67     Q_ASSERT( err == KErrNone );
       
    68     QTest::qWait(2000);
       
    69     TRAP( err, writeEventsL() );
       
    70     Q_ASSERT( err == KErrNone );
       
    71     QTest::qWait(2000);
       
    72     mModel = new LogsModel(LogsModel::LogsFullModel);
       
    73     QTest::qWait(5000); // wait model to be populated from db
       
    74 }
       
    75 
       
    76 void AT_LogsEngine::cleanupTestCase()
       
    77 {
       
    78     delete mModel;
       
    79     mModel = 0;
       
    80 }
       
    81 
       
    82 
       
    83 void AT_LogsEngine::init()
       
    84 {
       
    85 }
       
    86 
       
    87 void AT_LogsEngine::cleanup()
       
    88 {
       
    89 
       
    90 }
       
    91 
       
    92 void AT_LogsEngine::testAllEvents()
       
    93 {
       
    94     QVERIFY( mModel->rowCount(QModelIndex()) == logsTestAllEventsCount );
       
    95     AT_LOGSENG_VERIFY( (*mModel), 0, logsTestAllEvent1Name, logsTestAllEvent1Dir )
       
    96 }
       
    97 
       
    98 void AT_LogsEngine::testFilteredEvents()
       
    99 {
       
   100     LogsCustomFilter filter;
       
   101     filter.setContactId(2);
       
   102     filter.setSourceModel(mModel);
       
   103     QVERIFY( filter.rowCount() == logsTestContact2EventsCount );
       
   104     AT_LOGSENG_VERIFY( filter, 0, logsTestContact2Event1Name, logsTestContact2Event1Dir )
       
   105     AT_LOGSENG_VERIFY( filter, 1, logsTestContact2Event2Name, logsTestContact2Event2Dir )
       
   106     
       
   107     filter.setContactId(3);
       
   108     filter.setSourceModel(0);
       
   109     filter.setSourceModel(mModel);
       
   110     QVERIFY( filter.rowCount() == logsTestContact3EventsCount );
       
   111     AT_LOGSENG_VERIFY( filter, 0, logsTestContact3Event1Name, logsTestContact3Event1Dir )
       
   112     AT_LOGSENG_VERIFY( filter, 1, logsTestContact3Event2Name, logsTestContact3Event2Dir )
       
   113     AT_LOGSENG_VERIFY( filter, 2, logsTestContact3Event3Name, logsTestContact3Event3Dir )
       
   114     
       
   115     filter.setContactId(10345624);
       
   116     filter.setSourceModel(0);
       
   117     filter.setSourceModel(mModel);
       
   118     QVERIFY( filter.rowCount() == 0 ); 
       
   119 }
       
   120 
       
   121 void AT_LogsEngine::testMarkEventsSeen()
       
   122 {
       
   123     LogsCustomFilter filter;
       
   124     filter.setContactId(2);
       
   125     filter.setSourceModel(mModel);
       
   126     int unseenCount = 0;
       
   127     QVERIFY( filter.rowCount() == 2 );
       
   128     for ( int i = 0; i < filter.rowCount(); i++ ){
       
   129         LogsEvent* event = qVariantValue<LogsEvent*>( 
       
   130                 filter.data( filter.index(i, 0), LogsModel::RoleFullEvent ) );
       
   131         if ( event && !event->isRead() ){
       
   132             unseenCount++;
       
   133         }
       
   134     }
       
   135     QVERIFY( unseenCount == 2 );
       
   136     QSignalSpy spy( &filter, SIGNAL( markingCompleted(int)));
       
   137     QVERIFY( filter.markEventsSeen() );
       
   138     QTest::qWait(1000); // wait marking completion
       
   139    
       
   140     QVERIFY( spy.count() == 1 );
       
   141     int seenCount = 0;
       
   142     for ( int i = 0; i < filter.rowCount(); i++ ){
       
   143         LogsEvent* event = qVariantValue<LogsEvent*>( 
       
   144                 filter.data( filter.index(i, 0), LogsModel::RoleFullEvent ) );
       
   145         if ( event && event->isRead() ){
       
   146             seenCount++;
       
   147         }
       
   148     }
       
   149     QVERIFY( seenCount == 2 );
       
   150     
       
   151 }
       
   152 
       
   153 void AT_LogsEngine::testClearEvents()
       
   154 {
       
   155     LogsCustomFilter filter;
       
   156     filter.setContactId(2);
       
   157     filter.setSourceModel(mModel);
       
   158     QVERIFY( filter.rowCount() == 2 );
       
   159     int unseenCount = 0;
       
   160     QSignalSpy spy( &filter, SIGNAL( clearingCompleted(int)));
       
   161     QVERIFY( filter.clearEvents() );
       
   162     QTest::qWait(1000); // wait clearing completion
       
   163     QVERIFY( filter.rowCount() == 0 );
       
   164     QVERIFY( spy.count() == 1 );
       
   165 }
       
   166 
       
   167 void AT_LogsEngine::testHomeScreenUsecase()
       
   168 {
       
   169     // Homescreen missed calls notification plugin uses full model with
       
   170     // missed calls filter to find out how many sequential events was
       
   171     // received from one caller. If number of them is the same as missed call
       
   172     // counter, name of the caller is shown at HS.
       
   173     
       
   174     
       
   175     LogsFilter filter(LogsFilter::Missed);
       
   176     const int maxNumMissedCalls = 2;
       
   177     filter.setMaxSize(maxNumMissedCalls);
       
   178     LogsModel optimizedModel(LogsModel::LogsFullModel);   
       
   179     QTest::qWait(5000); // wait model to be populated from db
       
   180     filter.setSourceModel(&optimizedModel);
       
   181     
       
   182     // Test data has 10 missed calls from Jeppa but readsize has been configured to 2 so
       
   183     // reading stops when 2 missed calls has been read
       
   184     QCOMPARE( filter.rowCount(), maxNumMissedCalls );
       
   185     AT_LOGSENG_VERIFY( filter, 0, logsTestHomeScreenMissedCallerName, LogsEvent::DirMissed )
       
   186     AT_LOGSENG_VERIFY( filter, 1, logsTestHomeScreenMissedCallerName, LogsEvent::DirMissed )
       
   187     
       
   188     // Filter assigned to full model without read size has all those 10 missed calls
       
   189     LogsFilter filter2(LogsFilter::Missed);
       
   190     filter2.setSourceModel(mModel);
       
   191     QVERIFY( filter2.rowCount() == logsTestMissedCallsCount );
       
   192     
       
   193     // Verify decoration and display data
       
   194     QStringList displayData = filter.data( filter.index(0, 0), Qt::DisplayRole ).toStringList();
       
   195     QVERIFY( displayData.count() == 2 );
       
   196     QVERIFY( displayData.at(0) == logsTestHomeScreenMissedCallerName );
       
   197     HbIcon icon = qVariantValue<HbIcon>( filter.data( filter.index(0, 0), Qt::DecorationRole ).toList().at(0) );
       
   198     QVERIFY( !icon.isNull() );
       
   199     
       
   200     // Cenrep missed calls counter is not tested here
       
   201 }
       
   202 
       
   203 void AT_LogsEngine::clearEventsL()
       
   204 {
       
   205     executeL(KLogTestClientClearer, KNullDesC);
       
   206 }
       
   207 
       
   208 void AT_LogsEngine::writeEventsL()
       
   209 {
       
   210     TBuf<100> commandLine;
       
   211     // Run and write events named at_logsengine in the .dat
       
   212     commandLine.Append( KLogTestEvents );
       
   213     executeL(KLogTestClientWriter, commandLine);
       
   214 }
       
   215 
       
   216 void AT_LogsEngine::executeL(const TDesC& exeName, const TDesC& commandLine)
       
   217 {
       
   218     RProcess process;
       
   219     CleanupClosePushL( process );
       
   220     User::LeaveIfError( process.Create( exeName, commandLine ) );
       
   221     process.Resume();
       
   222     CleanupStack::PopAndDestroy( &process );
       
   223 }