email/imap4mtm/imapsession/test/src/ctestimapsearch.cpp
changeset 0 72b543305e3a
equal deleted inserted replaced
-1:000000000000 0:72b543305e3a
       
     1 // Copyright (c) 2006-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 #include "ctestimapsearch.h"
       
    17 
       
    18 #include "cfakeinputstream.h"
       
    19 #include "cfakeoutputstream.h"
       
    20 #include "cactivewaiter.h"
       
    21 
       
    22 #include "moutputstream.h"
       
    23 #include "cimapsession.h"
       
    24 #include "cimaputils.h"
       
    25 #include "cimapfolderinfo.h"
       
    26 
       
    27 CTestImapSearch::CTestImapSearch()
       
    28 	: iInputStream(NULL)
       
    29 	, iOutputStream(NULL)
       
    30 	, iActiveWaiter(NULL)
       
    31 	, iImapSession(NULL)
       
    32 	{}
       
    33 
       
    34 CTestImapSearch::~CTestImapSearch()
       
    35 	{
       
    36 	delete iImapSession;
       
    37 	delete iActiveWaiter;
       
    38 	delete iOutputStream;
       
    39 	delete iInputStream;
       
    40 	CImapUtils::Delete();
       
    41 	}
       
    42 
       
    43 void CTestImapSearch::SetupL()
       
    44 	{
       
    45 	ASSERT(iInputStream == NULL);
       
    46 	ASSERT(iOutputStream == NULL);
       
    47 	ASSERT(iActiveWaiter == NULL);
       
    48 	ASSERT(iImapSession == NULL);
       
    49 
       
    50 	CImapUtils::CreateL();
       
    51 	iInputStream = CFakeInputStream::NewL(Logger());
       
    52 	iOutputStream = CFakeOutputStream::NewL(Logger());
       
    53 	iActiveWaiter = new(ELeave)CActiveWaiter(Logger());
       
    54 	
       
    55 	CImapSettings* imapSettings=NULL; 
       
    56 	CImapMailStore* imapMailStore=NULL;
       
    57 	
       
    58 	iImapSession = CImapSession::NewL(*imapSettings,*imapMailStore,*iInputStream, *iOutputStream);
       
    59 	INFO_PRINTF1(_L("Setup: ServerGreeting"));
       
    60 	iInputStream->ResetInputStrings();
       
    61 	iInputStream->AppendInputStringL(_L8("* OK Microsoft Exchange 2000 IMAP4rev1 server version 6.0.6249.0 (lon-cn-exchng2k.msexchange2k.closedtest.intra) ready.\r\n"));
       
    62 	
       
    63 	ASSERT_EQUALS(CImapSession::EServerStateNone, iImapSession->ServerState());
       
    64 	
       
    65 	iImapSession->ReadServerGreetingL(iActiveWaiter->iStatus);
       
    66 	iActiveWaiter->WaitActive();
       
    67 	
       
    68 	INFO_PRINTF1(_L("...Login"));
       
    69 	iInputStream->ResetInputStrings();
       
    70 	iInputStream->AppendInputStringL(_L8("1 OK LOGIN completed\r\n"));
       
    71 			
       
    72 	iImapSession->LoginL(iActiveWaiter->iStatus, _L8("username"), _L8("password"));
       
    73 	iActiveWaiter->WaitActive();
       
    74 
       
    75 	ASSERT_EQUALS(CImapSession::EServerStateAuthenticated, iImapSession->ServerState());
       
    76 
       
    77 
       
    78 	INFO_PRINTF1(_L("...Select inbox"));
       
    79 	iInputStream->ResetInputStrings();
       
    80 	iInputStream->AppendInputStringL(_L8("2 OK [READ-WRITE] SELECT completed\r\n"));
       
    81 	
       
    82 	CImapFolderInfo* folderInfo = CImapFolderInfo::NewL();
       
    83 	CleanupStack::PushL(folderInfo);
       
    84 	
       
    85 	folderInfo->SetNameL(_L16("inbox"));
       
    86 	CleanupStack::Pop(folderInfo);
       
    87 	iImapSession->SelectL(iActiveWaiter->iStatus, folderInfo);
       
    88 	iActiveWaiter->WaitActive();
       
    89 
       
    90 	ASSERT_EQUALS(CImapSession::EServerStateSelected, iImapSession->ServerState());
       
    91 	ASSERT_EQUALS(folderInfo, iImapSession->SelectedFolderInfo());
       
    92 	folderInfo = NULL;
       
    93 
       
    94 	
       
    95 	INFO_PRINTF1(_L("Setup: Complete"));
       
    96 	}
       
    97 	
       
    98 void CTestImapSearch::TearDownL()
       
    99 	{
       
   100 	delete iImapSession;
       
   101 	iImapSession = NULL;
       
   102 	
       
   103 	delete iActiveWaiter;
       
   104 	iActiveWaiter = NULL;
       
   105 	
       
   106 	delete iOutputStream;
       
   107 	iOutputStream = NULL;
       
   108 	
       
   109 	delete iInputStream;
       
   110 	iInputStream = NULL;
       
   111 
       
   112 	CImapUtils::Delete();
       
   113 	}
       
   114 
       
   115 // Tests
       
   116 void CTestImapSearch::TestSimpleSearchL()
       
   117 	{
       
   118 	INFO_PRINTF1(_L("TestSimpleSearchL"));
       
   119 	
       
   120 	iInputStream->ResetInputStrings();
       
   121 	iInputStream->AppendInputStringL(_L8("* SEARCH\r\n"));
       
   122 	iInputStream->AppendInputStringL(_L8("3 OK Search completed\r\n"));
       
   123 	
       
   124 	TBufC16<100> searchstring;
       
   125 	RArray<TUint> matchingMessageIds;
       
   126 	CleanupClosePushL(matchingMessageIds);
       
   127 
       
   128 	iImapSession->SearchL(iActiveWaiter->iStatus,_L8(" TEXT \" string not in mailbox \" ") ,matchingMessageIds);
       
   129 	iActiveWaiter->WaitActive();
       
   130 	
       
   131 	ASSERT_EQUALS(matchingMessageIds.Count(), 0);
       
   132 	
       
   133 	CleanupStack::PopAndDestroy(&matchingMessageIds);
       
   134 	INFO_PRINTF1(_L("Complete"));
       
   135 	}
       
   136 	
       
   137 void CTestImapSearch::TestSearchWithMessageL()
       
   138 	{
       
   139 	INFO_PRINTF1(_L("TestSearchWithFlagsL"));
       
   140 	iInputStream->ResetInputStrings();
       
   141 	iInputStream->AppendInputStringL(_L8("* SEARCH 2 84 882\r\n"));
       
   142 	iInputStream->AppendInputStringL(_L8("3 OK Search completed\r\n"));
       
   143 
       
   144 	RArray<TUint> matchingMessageIds;
       
   145 	CleanupClosePushL(matchingMessageIds);
       
   146 	
       
   147 	iImapSession->SearchL(iActiveWaiter->iStatus,_L8("TEXT \" string not in mailbox \" "), matchingMessageIds);
       
   148 	iActiveWaiter->WaitActive();
       
   149 	for(TInt i=0; i <matchingMessageIds.Count(); ++i )
       
   150 		{
       
   151 		INFO_PRINTF2(_L("Messages Found are %d"),matchingMessageIds[i]);
       
   152 		}
       
   153 		
       
   154 	ASSERT_EQUALS(matchingMessageIds.Count(), 3);
       
   155 	ASSERT_EQUALS(matchingMessageIds[0], (TUint)2);
       
   156 	ASSERT_EQUALS(matchingMessageIds[1], (TUint)84);
       
   157 	ASSERT_EQUALS(matchingMessageIds[2], (TUint)882);
       
   158 	
       
   159 	CleanupStack::PopAndDestroy(&matchingMessageIds);
       
   160 	INFO_PRINTF1(_L("Complete"));
       
   161 	}
       
   162 
       
   163 CTestSuite* CTestImapSearch::CreateSuiteL(const TDesC& aName)
       
   164 // static
       
   165 	{
       
   166 	SUB_SUITE;
       
   167 	ADD_ASYNC_TEST_STEP(TestSimpleSearchL);
       
   168 	ADD_ASYNC_TEST_STEP(TestSearchWithMessageL);
       
   169 	END_SUITE;
       
   170 	}