email/imap4mtm/imapsession/test/src/ctestimapatombuilder.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 "ctestimapatombuilder.h"
       
    17 
       
    18 #include <utf.h>
       
    19 
       
    20 #include "cimapatom.h"
       
    21 #include "cimapatomparser.h"
       
    22 #include "cimapatomwalker.h"
       
    23 #include "cimaplogger.h"
       
    24 
       
    25 CTestImapAtomBuilder::CTestImapAtomBuilder()
       
    26 	: iAtomParser(NULL)
       
    27 	{}
       
    28 
       
    29 CTestImapAtomBuilder::~CTestImapAtomBuilder()
       
    30 	{
       
    31 	delete iAtomParser;
       
    32 	}
       
    33 
       
    34 void CTestImapAtomBuilder::SetupL()
       
    35 	{
       
    36 	iAtomParser = CImapAtomParser::NewL(EFalse, KDefaultLog);
       
    37 	}
       
    38 	
       
    39 void CTestImapAtomBuilder::TearDownL()
       
    40 	{
       
    41 	delete iAtomParser;
       
    42 	iAtomParser = NULL;
       
    43 	}
       
    44 
       
    45 // Tests
       
    46 void CTestImapAtomBuilder::TestAtomParserL()
       
    47 	{
       
    48 	INFO_PRINTF1(_L("TestL"));
       
    49 	
       
    50 	_LIT8(KLine, "sugar (one (two \"33 () three\" )) apple wibble");
       
    51 	TPtrC8 ptrLine = KLine();
       
    52 	TBool wantMore = iAtomParser->ProcessLineL(ptrLine);
       
    53 	
       
    54 	ASSERT_FALSE(wantMore);
       
    55 	
       
    56 	CImapAtom* root = iAtomParser->RootAtom();
       
    57 	RPointerArray<CImapAtom> stack;
       
    58 	stack.AppendL(root);
       
    59 	
       
    60 	CImapAtom* current = root;
       
    61 	
       
    62 	while(current!=NULL)
       
    63 		{
       
    64 		TPtrC8 ptrAtom = current->Atom(EFalse);
       
    65 		TInt topOfStack = stack.Count() - 1;
       
    66 				
       
    67 		// Log the data (SendDataReq is non-leaving, so avoid leaving.  Hence no cleanup stack)
       
    68 		HBufC16* unicodeAtom = NULL;
       
    69 		TRAPD(err,
       
    70 			unicodeAtom = CnvUtfConverter::ConvertToUnicodeFromUtf8L(ptrAtom);
       
    71 		);
       
    72 		
       
    73 		if (err == KErrNone)
       
    74 			{
       
    75 			INFO_PRINTF3(_L("%d [%S]"), topOfStack, unicodeAtom);
       
    76 			}
       
    77 			
       
    78 		delete unicodeAtom;	
       
    79 		unicodeAtom=NULL;
       
    80 		
       
    81 		// Try going down
       
    82 		CImapAtom* candidateAtom = current->Child();
       
    83 		if (candidateAtom != NULL)
       
    84 			{
       
    85 			stack.AppendL(candidateAtom);
       
    86 			++topOfStack;
       
    87 			}
       
    88 		// Try going along
       
    89 		else
       
    90 			{
       
    91 			candidateAtom = current->Next();
       
    92 			// Try going back-up-and-along
       
    93 			while (candidateAtom == NULL && topOfStack > 0)
       
    94 				{
       
    95 				stack.Remove(topOfStack);
       
    96 				--topOfStack;
       
    97 				candidateAtom = stack[topOfStack];
       
    98 				candidateAtom = candidateAtom->Next();
       
    99 				}
       
   100 			
       
   101 			stack[topOfStack] = candidateAtom;
       
   102 			}
       
   103 			
       
   104 		// Could use a table (or at least tables - need repeats) to parse different BODYSTRUCTURE's !!!
       
   105 		current = candidateAtom;
       
   106 	}
       
   107 		
       
   108 	stack.Reset();
       
   109 	
       
   110 	INFO_PRINTF1(_L("Complete"));
       
   111 	}
       
   112 	
       
   113 void CTestImapAtomBuilder::TestWalkerL()
       
   114 	{
       
   115 	INFO_PRINTF1(_L("TestWalkerL"));
       
   116 	
       
   117 //	_LIT8(KLine, "spandex (one (two \"33 () three\" )) arse wibble (apple ((pear) banana) cherry) dog");
       
   118 //	TPtrC8 ptrLine = KLine();
       
   119 //	TBool wantMore = iAtomParser->ProcessLineL(ptrLine);
       
   120 //	ASSERT_FALSE(wantMore);
       
   121 
       
   122 	_LIT8(KLine1, "(TEXT PLAIN (name1 value1 name2 value2) ID {25}");
       
   123 	_LIT8(KLit2, "first line\r\nsecond line\r\n");
       
   124 //	_LIT8(KLine3, "encoding size lines");
       
   125 //	_LIT8(KLine3, "encoding size \"missing quote"); // and missing end bracket!
       
   126 	_LIT8(KLine3, "encoding size lines) Stuff at the end that we're not interested in");
       
   127 //	_LIT8(KLine3, "encoding size lines)");
       
   128 	
       
   129 	TBool wantMore = iAtomParser->ProcessLineL(KLine1);
       
   130 	ASSERT_TRUE(wantMore);
       
   131 	iAtomParser->ProcessLiteralBlockL(KLit2);
       
   132 	wantMore = iAtomParser->ProcessLineL(KLine3);
       
   133 	ASSERT_FALSE(wantMore);
       
   134 	
       
   135 	DumpAtomTreeL(iAtomParser->RootAtom());
       
   136 
       
   137 	TPtrC8 unparsed = iAtomParser->UnparsedData();
       
   138 	TInt len = unparsed.Length();
       
   139 	}
       
   140 
       
   141 void CTestImapAtomBuilder::DumpAtomTreeL(CImapAtom* aRoot)
       
   142 	{
       
   143 	CImapAtomWalker* atomWalker = CImapAtomWalker::NewL(KDefaultLog);
       
   144 	CleanupStack::PushL(atomWalker);
       
   145 	atomWalker->SetRootL(aRoot);
       
   146 		
       
   147 	TBool bFound = ETrue;
       
   148 		
       
   149 	while(bFound)
       
   150 		{
       
   151 		TPtrC8 ptrAtom = atomWalker->CurrentDes(EFalse);
       
   152 						
       
   153 		// Log the data (SendDataReq is non-leaving, so avoid leaving.  Hence no cleanup stack)
       
   154 		HBufC16* unicodeAtom = NULL;
       
   155 		TRAPD(err,
       
   156 			unicodeAtom = CnvUtfConverter::ConvertToUnicodeFromUtf8L(ptrAtom);
       
   157 		);
       
   158 		
       
   159 		if (err == KErrNone)
       
   160 			{
       
   161 			INFO_PRINTF3(_L("%d [%S]"), atomWalker->StackCount(), unicodeAtom);
       
   162 			}
       
   163 			
       
   164 		delete unicodeAtom;	
       
   165 		unicodeAtom=NULL;
       
   166 		
       
   167 		// Try going down
       
   168 		if (atomWalker->PeekDown() == NULL)
       
   169 			{
       
   170 			// Try going across
       
   171 			if (atomWalker->WalkAcrossL(EFalse) == NULL)
       
   172 				{
       
   173 				// Might not find something on the way up
       
   174 				bFound = EFalse;
       
   175 				
       
   176 				// Try going up
       
   177 				while (atomWalker->StackCount() > 1)
       
   178 					{
       
   179 					atomWalker->WalkUpL();
       
   180 					CImapAtom* upAtom = atomWalker->Current();
       
   181 					
       
   182 					if (upAtom && atomWalker->WalkAcrossL(EFalse) != NULL)
       
   183 						{
       
   184 						// found something!
       
   185 						bFound = ETrue;
       
   186 						break;
       
   187 						}
       
   188 					}
       
   189 				}
       
   190 			}
       
   191 		else
       
   192 			{
       
   193 			atomWalker->WalkDownL();
       
   194 			}
       
   195 		}
       
   196 		
       
   197 	CleanupStack::PopAndDestroy(atomWalker);
       
   198 	
       
   199 	INFO_PRINTF1(_L("Complete"));
       
   200 	}
       
   201 CTestSuite* CTestImapAtomBuilder::CreateSuiteL(const TDesC& aName)
       
   202 // static
       
   203 	{
       
   204 	SUB_SUITE;
       
   205 	ADD_TEST_STEP(TestWalkerL);
       
   206 	END_SUITE;
       
   207 	}