messagingapp/msgui/appengine/tsrc/testconversationengine/src/testconversationenginestub.cpp
changeset 27 e4592d119491
child 48 4f501b74aeb1
equal deleted inserted replaced
25:84d9eb65b26f 27:e4592d119491
       
     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 #include <msgitem.h>
       
    19 #include <ccsdefs.h>
       
    20 #include <ccsconversationentry.h>
       
    21 #include <ccsclientconversation.h>
       
    22 #include <conversationsenginedefines.h>
       
    23 #include "testconversationenginestub.h"
       
    24 #include "testconversationnotifier.h"
       
    25 #include "convergedmessage.h"
       
    26 
       
    27 TestConversationEngineStub* TestConversationEngineStub::Instance()
       
    28 {
       
    29     static TestConversationEngineStub* iStubEngine 
       
    30                                 = new TestConversationEngineStub();
       
    31     return iStubEngine;
       
    32 }
       
    33 
       
    34 void TestConversationEngineStub::InitL()
       
    35 {
       
    36     iNotifier = new TestConversationNotifier();
       
    37     iConversationID = 1234;
       
    38     iTimeStamp = 123456789;
       
    39     iMsgType = ECsSMS;
       
    40     iUnReadCount = 3;
       
    41 
       
    42     _LIT(Kname , "9343434343");
       
    43     _LIT(KDescription , "This is the test msg.");
       
    44 
       
    45     iContactID = Kname;
       
    46     iDescription = KDescription;
       
    47     iDeletedEntry = NULL;
       
    48 }
       
    49 
       
    50 TestConversationEngineStub::TestConversationEngineStub()
       
    51 {
       
    52     TRAP_IGNORE(InitL());
       
    53 }
       
    54 
       
    55 TestConversationEngineStub::~TestConversationEngineStub()
       
    56 {
       
    57     iConversationClientList.ResetAndDestroy();
       
    58     iConversationEntryList.ResetAndDestroy();
       
    59     delete iNotifier;
       
    60 }
       
    61 
       
    62 void TestConversationEngineStub
       
    63 ::SetResultObserver(MCsResultsObserver *aResultObserver)
       
    64 {
       
    65     iNotifier->SetResultNotifier(aResultObserver);
       
    66 }
       
    67 
       
    68 void TestConversationEngineStub::RemoveResultObserver()
       
    69 {
       
    70     iNotifier->RemoveResultNotifier();
       
    71 }
       
    72 
       
    73 void TestConversationEngineStub
       
    74 ::SetConversationChangeObserver(MCsConversationChangeObserver* aObserver,
       
    75             CCsClientConversation* /*aClientConversation*/)
       
    76 {
       
    77     iNotifier->SetConversationChangeNotifier(aObserver);
       
    78 }
       
    79     
       
    80 void TestConversationEngineStub::RemoveConversationChangeObserver()
       
    81 {
       
    82     iNotifier->RemoveConversationChangeNotifier();
       
    83 }
       
    84 
       
    85 void TestConversationEngineStub
       
    86 ::SetConversationListChangeObserver(MCsConversationListChangeObserver* 
       
    87                                         aConversationListChangeObserver)
       
    88 {
       
    89     iNotifier->SetConversationListNotifier(aConversationListChangeObserver);
       
    90 }
       
    91 
       
    92 void TestConversationEngineStub::RemoveConversationListChangeObserver()
       
    93 {
       
    94     iNotifier->RemoveConversationListNotifier();
       
    95 }
       
    96 
       
    97 void TestConversationEngineStub
       
    98 ::MarkConversationRead(const TInt aConversationId)
       
    99 {
       
   100     for (TInt i = 0; i < iConversationEntryList.Count(); ++i)
       
   101          {
       
   102              if (iConversationEntryList[i]->EntryId() == aConversationId)
       
   103                  {
       
   104                      iConversationEntryList[i]->
       
   105                          ChangeAttributes(ECsAttributeNone, ECsAttributeUnread);
       
   106                      break;
       
   107                  }
       
   108           }
       
   109 }
       
   110 
       
   111 void TestConversationEngineStub
       
   112 ::DeleteConversation(const TInt aConversationId)
       
   113 {
       
   114     for (TInt i = 0; i < iConversationEntryList.Count(); ++i)
       
   115          {
       
   116              if (iConversationEntryList[i]->EntryId() == aConversationId)
       
   117                  {
       
   118                      iDeletedEntry  = iConversationEntryList[i];
       
   119                      iConversationEntryList.Remove(i);
       
   120                  }
       
   121           }
       
   122 }
       
   123 
       
   124 TInt TestConversationEngineStub::GetConvListSize()
       
   125 {
       
   126     return iConversationEntryList.Count();
       
   127 }
       
   128 
       
   129 TInt TestConversationEngineStub::GetUnreadCount()
       
   130 {
       
   131     TInt unRead = 0;
       
   132     for (TInt i = 0; i < iConversationEntryList.Count(); ++i)
       
   133      {
       
   134          if (iConversationEntryList[i]->IsAttributeSet(ECsAttributeUnread))
       
   135              {
       
   136                  unRead++;
       
   137              }
       
   138       }
       
   139     return unRead;
       
   140 }
       
   141 
       
   142 TInt TestConversationEngineStub::GetConversationID()
       
   143 {
       
   144     return iConversationID;
       
   145 }
       
   146 
       
   147 TInt TestConversationEngineStub::GetTimeStamp()
       
   148 {
       
   149     return iTimeStamp;
       
   150 }
       
   151 
       
   152 TInt TestConversationEngineStub::GetMsgType()
       
   153 {
       
   154     return iMsgType;
       
   155 }
       
   156 
       
   157 TDesC& TestConversationEngineStub::GetContactID()
       
   158 {
       
   159     return iContactID;
       
   160 }
       
   161 
       
   162 TDesC& TestConversationEngineStub::GetDescription()
       
   163 {
       
   164     return iDescription;
       
   165 }
       
   166 
       
   167 RPointerArray<CCsClientConversation>& 
       
   168 TestConversationEngineStub::GetConversationClientList()
       
   169 {
       
   170     return iConversationClientList;
       
   171 }
       
   172 
       
   173 CCsConversationEntry*
       
   174 TestConversationEngineStub
       
   175 ::CreateConversationEntryL(TInt aConvID, bool aUnread)
       
   176 {
       
   177     CCsConversationEntry* conversationEntry = CCsConversationEntry::NewL();
       
   178     CleanupStack::PushL(conversationEntry);
       
   179 
       
   180     conversationEntry->SetType(iMsgType);
       
   181     conversationEntry->SetEntryId(aConvID);
       
   182 
       
   183     conversationEntry->SetTimeStampL(iTimeStamp);
       
   184 
       
   185     conversationEntry->SetContactL(iContactID);
       
   186 
       
   187     conversationEntry->SetDescriptionL(iDescription);
       
   188     
       
   189     if (aUnread)
       
   190         {
       
   191         conversationEntry->ChangeAttributes(ECsAttributeUnread, 
       
   192                                                 ECsAttributeNone);
       
   193         conversationEntry->SetConversationDir(0/*MsgDirectionIncoming*/);
       
   194         }
       
   195     else
       
   196         {
       
   197         conversationEntry->SetConversationDir(1/*MsgDirectionOutgoing*/);
       
   198         conversationEntry->ChangeAttributes(ECsAttributeSent, 
       
   199                                                 ECsAttributeNone);
       
   200         }
       
   201    
       
   202     CleanupStack::Pop(conversationEntry);
       
   203     
       
   204     return conversationEntry;
       
   205 }
       
   206 
       
   207 void TestConversationEngineStub
       
   208 ::PrepareConversationListL(TInt aConvID, TInt aToatalCnt, TInt aUnreadCnt)
       
   209 {
       
   210     // create conversation entries
       
   211     for ( TInt iloop = 0; iloop < aToatalCnt; iloop++ )
       
   212         {
       
   213         CCsConversationEntry* conversationEntry 
       
   214                 = CreateConversationEntryL(aConvID++, (aUnreadCnt-- > 0));
       
   215         CleanupStack::PushL(conversationEntry);
       
   216     
       
   217         // append conversation entries to list
       
   218         iConversationEntryList.AppendL(conversationEntry);
       
   219 
       
   220         CleanupStack::Pop(conversationEntry);
       
   221         }
       
   222 }
       
   223 
       
   224 //Prerae a list with : conv id, no of unread msgs, 
       
   225 void TestConversationEngineStub::UpdateConversationList()
       
   226 {
       
   227     TInt error;
       
   228     TRAP(error, PrepareConversationListL(iConversationID, 5, iUnReadCount));    
       
   229     iNotifier->SendListResultUpdate(iConversationEntryList);
       
   230 }
       
   231 
       
   232 void TestConversationEngineStub::UpdateConvEntry(TInt aConversationId)
       
   233 {
       
   234     for (TInt i = 0; i < iConversationEntryList.Count(); ++i)
       
   235      {
       
   236          if (iConversationEntryList[i]->EntryId() == aConversationId)
       
   237          {
       
   238              iNotifier->
       
   239              UpdateConversationEntryModify(*iConversationEntryList[i]);
       
   240              break;
       
   241          }
       
   242       }
       
   243 }
       
   244 
       
   245 void TestConversationEngineStub::UpdateDeletedConvEntry()
       
   246 {
       
   247     if (iDeletedEntry)
       
   248     {
       
   249         iNotifier->UpdateConversationEntryDelete(*iDeletedEntry);
       
   250         delete iDeletedEntry;
       
   251         iDeletedEntry = NULL;
       
   252     }
       
   253 }
       
   254 
       
   255 void TestConversationEngineStub::UpdateAddConvEntryL()
       
   256 {
       
   257     CCsConversationEntry*
       
   258     conversationEntry = CreateConversationEntryL(9999/*iConversationID - 1*/, true);
       
   259     CleanupStack::PushL(conversationEntry);
       
   260     iConversationEntryList.AppendL(conversationEntry);
       
   261     CleanupStack::Pop(conversationEntry);
       
   262     iNotifier->UpdateConversationEntryAdd(*conversationEntry);
       
   263 }
       
   264 
       
   265 void TestConversationEngineStub::UpdateAddConvEntry()
       
   266 {
       
   267     TInt error;
       
   268     TRAP(error, UpdateAddConvEntryL());
       
   269 }
       
   270 
       
   271 void TestConversationEngineStub::UpdateAddConvClientL()
       
   272 {
       
   273     _LIT(KFName , "New User");
       
   274     
       
   275     //need to free alloced descriptor
       
   276     TBuf<10>  BufName(KFName);
       
   277     HBufC * fname = BufName.AllocL();
       
   278     CleanupStack::PushL(fname);
       
   279     
       
   280     CCsConversationEntry* 
       
   281     convEntry = CreateConversationEntryL(iConversationID - 1, true);
       
   282     CleanupStack::PushL(convEntry);
       
   283     
       
   284     CCsClientConversation* 
       
   285     convClient = CreateConversationClientL(fname,4000, 2, *convEntry);
       
   286     CleanupStack::PushL(convClient);
       
   287     
       
   288     iConversationClientList.AppendL(convClient);
       
   289     
       
   290     CleanupStack::Pop(convClient);
       
   291     CleanupStack::PopAndDestroy(convEntry);
       
   292     CleanupStack::PopAndDestroy(fname);
       
   293     iNotifier->UpdateConversationClientAdd(*convClient);
       
   294 }
       
   295 
       
   296 void TestConversationEngineStub::UpdateAddConvClient()
       
   297 {
       
   298     TInt error;
       
   299     TRAP(error, UpdateAddConvClientL());
       
   300 }
       
   301 
       
   302 void TestConversationEngineStub::UpdateDeletedConvClient()
       
   303 {
       
   304     CCsClientConversation* convClient = iConversationClientList[0];
       
   305     iConversationClientList.Remove(0);
       
   306 
       
   307     if (convClient)
       
   308     {
       
   309         iNotifier->UpdateConversationClientDelete(*convClient);
       
   310         delete convClient;
       
   311     }
       
   312 }
       
   313 
       
   314 
       
   315 void TestConversationEngineStub::UpdateConvClient(TInt aConversationId)
       
   316 {
       
   317     for (TInt i = 0; i < iConversationClientList.Count(); ++i)
       
   318      {
       
   319          if (iConversationClientList[i]->GetConversationEntryId() 
       
   320                                                  == aConversationId)
       
   321          {
       
   322              iConversationClientList[i]->
       
   323              GetConversationEntry()->
       
   324              ChangeAttributes(ECsAttributeNone, ECsAttributeUnread);
       
   325              
       
   326              iConversationClientList[i]->SetUnreadMessageCount(0);
       
   327              
       
   328              iNotifier->
       
   329              UpdateConversationClientModify(*iConversationClientList[i]);
       
   330              break;
       
   331          }
       
   332       }
       
   333 }
       
   334 
       
   335 CCsClientConversation* 
       
   336 TestConversationEngineStub::
       
   337 CreateConversationClientL(const HBufC* aDispName, TInt aContactID,
       
   338                           TInt aUnreadCnt,
       
   339                           CCsConversationEntry& aConvEntry)
       
   340 {
       
   341     CCsClientConversation* conversationClient = CCsClientConversation::NewL();
       
   342     CleanupStack::PushL(conversationClient);
       
   343     
       
   344     conversationClient->SetContactId(aContactID);
       
   345     conversationClient->SetUnreadMessageCount(aUnreadCnt);
       
   346     
       
   347     conversationClient->SetDisplayNameL(aDispName);
       
   348 
       
   349     conversationClient->SetConversationEntryL(&aConvEntry);
       
   350     
       
   351     conversationClient->SetConversationEntryId(aConvEntry.EntryId());
       
   352     
       
   353     CleanupStack::Pop(conversationClient);
       
   354     
       
   355     return conversationClient;
       
   356 }
       
   357 
       
   358 void TestConversationEngineStub
       
   359 ::PrepareConversationClientListL(TInt aContactID, TInt aListSize, 
       
   360                                 TInt aUnRead, TInt aUnReadCnt)
       
   361 {
       
   362     TInt convID = iConversationID;
       
   363     
       
   364     _LIT(KFName , "abc");
       
   365     
       
   366     //need to free alloced descriptor
       
   367     TBufC<10>  BufName(KFName);
       
   368     HBufC * fname = BufName.AllocL();
       
   369     CleanupStack::PushL(fname);
       
   370     
       
   371     for (TInt loop = 0; loop < aListSize; loop++)
       
   372         {
       
   373             CCsConversationEntry* 
       
   374             convEntry = CreateConversationEntryL(convID++,
       
   375                                   (aUnRead-- > 0 ? true : false));
       
   376             CleanupStack::PushL(convEntry);
       
   377             
       
   378             CCsClientConversation* 
       
   379             convClient = CreateConversationClientL(fname, aContactID++, aUnReadCnt++,
       
   380                                               *convEntry);
       
   381             CleanupStack::PushL(convClient);
       
   382             
       
   383             iConversationClientList.AppendL(convClient);
       
   384             
       
   385             CleanupStack::Pop(convClient);
       
   386             CleanupStack::PopAndDestroy(convEntry);
       
   387         }
       
   388     CleanupStack::Pop(fname);
       
   389 }
       
   390 
       
   391 //conversation client notifications
       
   392 void TestConversationEngineStub::UpdateConversationClientList()
       
   393 {
       
   394     TInt error;
       
   395     TRAP(error, PrepareConversationClientListL(2000, 5, 3, 0));
       
   396     if (error == KErrNone)
       
   397     {
       
   398         iNotifier->SendClientListUpdate(iConversationClientList);
       
   399     }
       
   400 }
       
   401 
       
   402 TInt TestConversationEngineStub
       
   403 ::GetClientConversationID(const TInt aContactId)
       
   404 {
       
   405     for (TInt i = 0; i < iConversationClientList.Count(); ++i)
       
   406      {
       
   407          if (iConversationClientList[i]->GetContactId() ==  aContactId)
       
   408          {
       
   409              return iConversationClientList[i]->GetConversationEntryId();
       
   410          }
       
   411       }
       
   412     return -1;
       
   413 }
       
   414 
       
   415 TInt TestConversationEngineStub
       
   416 ::GetClientConversationID(const TDesC& aContactAddress)
       
   417 {
       
   418     for (TInt i = 0; i < iConversationClientList.Count(); ++i)
       
   419      {
       
   420          if (iConversationClientList[i]->
       
   421                  GetConversationEntry()->
       
   422                  Contact()->Compare(aContactAddress) == 0)
       
   423          {
       
   424              return iConversationClientList[i]->GetConversationEntryId();
       
   425          }
       
   426       }
       
   427     return -1;
       
   428 }