email/imap4mtm/imapsession/test/src/ctestimapcancelandflush.cpp
changeset 80 8b14b30db193
parent 0 72b543305e3a
equal deleted inserted replaced
79:2981cb3aa489 80:8b14b30db193
       
     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 "ctestimapcancelandflush.h"
       
    17 
       
    18 #include "cfakeoutputstream.h"
       
    19 
       
    20 #include "moutputstream.h"
       
    21 #include "cimapsession.h"
       
    22 #include "cimapsessionconsts.h"
       
    23 #include "cimapfolderinfo.h"
       
    24 #include "cimaplistfolderinfo.h"
       
    25 #include "cimaputils.h"
       
    26 
       
    27 CTestImapCancelAndFlush::CTestImapCancelAndFlush()
       
    28 	: iInputStream(NULL)
       
    29 	, iOutputStream(NULL)
       
    30 	, iActiveWaiter(NULL)
       
    31 	, iImapSession(NULL)
       
    32 	{}
       
    33 
       
    34 CTestImapCancelAndFlush::~CTestImapCancelAndFlush()
       
    35 	{
       
    36 	delete iImapSession;
       
    37 	delete iActiveWaiter;
       
    38 	delete iOutputStream;
       
    39 	delete iInputStream;
       
    40 	CImapUtils::Delete();
       
    41 	}
       
    42 
       
    43 void CTestImapCancelAndFlush::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 	
       
    60 	INFO_PRINTF1(_L("Setup: ServerGreeting"));
       
    61 	iInputStream->ResetInputStrings();
       
    62 	iInputStream->AppendInputStringL(_L8("* OK Microsoft Exchange 2000 IMAP4rev1 server version 6.0.6249.0 (lon-cn-exchng2k.msexchange2k.closedtest.intra) ready.\r\n"));
       
    63 
       
    64 	ASSERT_EQUALS(CImapSession::EServerStateNone, iImapSession->ServerState());
       
    65 	
       
    66 	iImapSession->ReadServerGreetingL(iActiveWaiter->iStatus);
       
    67 	iActiveWaiter->WaitActive();
       
    68 	
       
    69 	INFO_PRINTF1(_L("...Login"));
       
    70 	iInputStream->ResetInputStrings();
       
    71 	iInputStream->AppendInputStringL(_L8("1 OK LOGIN completed\r\n"));
       
    72 		
       
    73 	iImapSession->LoginL(iActiveWaiter->iStatus, _L8("username"), _L8("password"));
       
    74 	iActiveWaiter->WaitActive();
       
    75 
       
    76 	ASSERT_EQUALS(CImapSession::EServerStateAuthenticated, iImapSession->ServerState());
       
    77 
       
    78 
       
    79 	INFO_PRINTF1(_L("...Select inbox"));
       
    80 	iInputStream->ResetInputStrings();
       
    81 	iInputStream->AppendInputStringL(_L8("* 23 EXISTS\r\n"));
       
    82 	iInputStream->AppendInputStringL(_L8("* 1 RECENT\r\n"));
       
    83 	iInputStream->AppendInputStringL(_L8("* OK [UNSEEN 12] Message 12 is first unseen\r\n"));
       
    84 	iInputStream->AppendInputStringL(_L8("* OK [UIDVALIDITY 3857529045] UIDs valid\r\n"));
       
    85 	iInputStream->AppendInputStringL(_L8("2 OK [READ-WRITE] SELECT completed\r\n"));
       
    86 	
       
    87 	CImapFolderInfo* folderInfo = CImapFolderInfo::NewL();
       
    88 	CleanupStack::PushL(folderInfo);
       
    89 	
       
    90 	folderInfo->SetNameL(_L16("inbox"));
       
    91 	CleanupStack::Pop(folderInfo);
       
    92 	iImapSession->SelectL(iActiveWaiter->iStatus, folderInfo);
       
    93 	iActiveWaiter->WaitActive();
       
    94 
       
    95 	ASSERT_EQUALS(CImapSession::EServerStateSelected, iImapSession->ServerState());
       
    96 	ASSERT_EQUALS(folderInfo, iImapSession->SelectedFolderInfo());
       
    97 	folderInfo = NULL;
       
    98 	}
       
    99 	
       
   100 void CTestImapCancelAndFlush::TearDownL()
       
   101 	{
       
   102 	delete iImapSession;
       
   103 	iImapSession = NULL;
       
   104 	
       
   105 	delete iActiveWaiter;
       
   106 	iActiveWaiter = NULL;
       
   107 	
       
   108 	delete iOutputStream;
       
   109 	iOutputStream = NULL;
       
   110 	
       
   111 	delete iInputStream;
       
   112 	iInputStream = NULL;
       
   113 
       
   114 	CImapUtils::Delete();
       
   115 	}
       
   116 
       
   117 // Tests
       
   118 void CTestImapCancelAndFlush::TestListInPartsL()
       
   119 	{
       
   120 	INFO_PRINTF1(_L("TestListInPartsL"));
       
   121 	iInputStream->ResetInputStrings();
       
   122 	iInputStream->AppendInputStringL(_L8("* LIST (\\UnMarked) \"/\" Drafts\r\n"));
       
   123 	iInputStream->AppendInputStringL(_L8("* LIST (\\UnMarked) \"/\" INBOX\r\n"));
       
   124 	iInputStream->AppendInputStringL(_L8("* LIST (\\UnMarked) \"/\" Folder1\r\n"));
       
   125 	iInputStream->AppendInputStringL(_L8("* LIST (\\UnMarked) \"/\" Folder2\r\n"));
       
   126 		
       
   127 	iInputStreamNotifyOp = ENotifyOpListInParts;
       
   128 	iInputStream->NotifyWhenStreamIsEmpty(*this);
       
   129 	
       
   130 	RArrayImapListFolderInfo folderList;
       
   131 	iImapSession->ListL(iActiveWaiter->iStatus, _L(""), _L("%"), folderList);
       
   132 	iActiveWaiter->WaitActive();
       
   133 	
       
   134 	ASSERT_EQUALS(folderList.Count(), 7);
       
   135 
       
   136 	folderList.ResetAndDestroy();
       
   137  	INFO_PRINTF1(_L("Complete"));
       
   138 	}
       
   139 	
       
   140 void CTestImapCancelAndFlush::TestCancelAndFlushL()
       
   141 	{
       
   142 	INFO_PRINTF1(_L("TestCancelAndFlushL"));
       
   143 	iInputStream->ResetInputStrings();
       
   144 	iInputStream->AppendInputStringL(_L8("* LIST (\\UnMarked) \"/\" Drafts\r\n"));
       
   145 	iInputStream->AppendInputStringL(_L8("* LIST (\\UnMarked) \"/\" INBOX\r\n"));
       
   146 	iInputStream->AppendInputStringL(_L8("* LIST (\\UnMarked) \"/\" Folder1\r\n"));
       
   147 	iInputStream->AppendInputStringL(_L8("* LIST (\\UnMarked) \"/\" Folder2\r\n"));
       
   148 	
       
   149 	iInputStreamNotifyOp = ENotifyOpCancelWaitActive;
       
   150 	iInputStream->NotifyWhenStreamIsEmpty(*this);
       
   151 	
       
   152 	RArrayImapListFolderInfo folderList;
       
   153 	iImapSession->ListL(iActiveWaiter->iStatus, _L(""), _L("%"), folderList);
       
   154 	iActiveWaiter->WaitActive(KErrCancel); // returns because a cancel occured.
       
   155 	
       
   156 	iInputStream->ResetInputStrings();
       
   157 	iInputStream->AppendInputStringL(_L8("* LIST (\\UnMarked) \"/\" Folder3\r\n"));
       
   158 	iInputStream->AppendInputStringL(_L8("* LIST (\\UnMarked) \"/\" Folder4\r\n"));
       
   159 	iInputStream->AppendInputStringL(_L8("* LIST (\\UnMarked) \"/\" Folder5\r\n"));
       
   160 	iInputStream->AppendInputStringL(_L8("3 OK LIST completed\r\n"));
       
   161 		
       
   162 	iImapSession->FlushCancelledCommand(iActiveWaiter->iStatus);
       
   163 	iActiveWaiter->WaitActive();
       
   164 	
       
   165 	folderList.ResetAndDestroy();
       
   166  	INFO_PRINTF1(_L("Complete"));
       
   167 	}
       
   168 	
       
   169 void CTestImapCancelAndFlush::TestCancelAndFlushWithTimeoutL()
       
   170 	{
       
   171 	INFO_PRINTF1(_L("TestCancelAndFlushWithTimeoutL"));
       
   172 	
       
   173 	iInputStream->ResetInputStrings();
       
   174 	iInputStream->AppendInputStringL(_L8("* LIST (\\UnMarked) \"/\" Drafts\r\n"));
       
   175 	iInputStream->AppendInputStringL(_L8("* LIST (\\UnMarked) \"/\" INBOX\r\n"));
       
   176 	iInputStream->AppendInputStringL(_L8("* LIST (\\UnMarked) \"/\" Folder1\r\n"));
       
   177 	iInputStream->AppendInputStringL(_L8("* LIST (\\UnMarked) \"/\" Folder2\r\n"));
       
   178 		
       
   179 	iInputStreamNotifyOp = ENotifyOpCancelWaitActive;
       
   180 	iInputStream->NotifyWhenStreamIsEmpty(*this);
       
   181 	
       
   182 	RArrayImapListFolderInfo folderList;
       
   183 	iImapSession->ListL(iActiveWaiter->iStatus, _L(""), _L("%"), folderList);
       
   184 	iActiveWaiter->WaitActive(KErrCancel); // returns because a cancel occured.
       
   185 		
       
   186 	// Unlike TestCancelAndFlushL(), the remainder of the response is not 
       
   187 	// provided fake input stream.  So FlushCancelledCommand should time out.
       
   188 	
       
   189 	iImapSession->FlushCancelledCommand(iActiveWaiter->iStatus);
       
   190 	iActiveWaiter->WaitActive(KErrImapFlushTimeout);
       
   191 	
       
   192 	folderList.ResetAndDestroy();
       
   193  	INFO_PRINTF1(_L("Complete"));
       
   194 	}
       
   195 	
       
   196 void CTestImapCancelAndFlush::OnInputStreamIsEmptyL()
       
   197 	{
       
   198 	switch (iInputStreamNotifyOp)
       
   199 		{
       
   200 		case ENotifyOpCancelWaitActive:
       
   201 			{
       
   202 			iActiveWaiter->CancelWaitActive(*this);
       
   203 			}
       
   204 			break;
       
   205 		case ENotifyOpListInParts:
       
   206 			{
       
   207 			iInputStream->ResetInputStrings();
       
   208 			iInputStream->AppendInputStringL(_L8("* LIST (\\UnMarked) \"/\" Folder3\r\n"));
       
   209 			iInputStream->AppendInputStringL(_L8("* LIST (\\UnMarked) \"/\" Folder4\r\n"));
       
   210 			iInputStream->AppendInputStringL(_L8("* LIST (\\UnMarked) \"/\" Folder5\r\n"));
       
   211 			iInputStream->AppendInputStringL(_L8("3 OK LIST completed\r\n"));
       
   212 			}
       
   213 			break;
       
   214 		}
       
   215 	}
       
   216 	
       
   217 void CTestImapCancelAndFlush::DoCancel()
       
   218 	{
       
   219 	iImapSession->Cancel();
       
   220 	}
       
   221 	
       
   222 CTestSuite* CTestImapCancelAndFlush::CreateSuiteL(const TDesC& aName)
       
   223 // static
       
   224 	{
       
   225 	SUB_SUITE;
       
   226 	ADD_ASYNC_TEST_STEP(TestListInPartsL);
       
   227 	ADD_ASYNC_TEST_STEP(TestCancelAndFlushL);
       
   228 	ADD_ASYNC_TEST_STEP(TestCancelAndFlushWithTimeoutL);
       
   229 	END_SUITE;
       
   230 	}