xml/libxml2libs/test/tef/xml2/src/texmlutils.cpp
changeset 0 e35f40988205
equal deleted inserted replaced
-1:000000000000 0:e35f40988205
       
     1 // Copyright (c) 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 /**
       
    17  @file texmlutils.cpp
       
    18  @internalTechnology
       
    19 */
       
    20 
       
    21 #include "texml2teststep.h"
       
    22 #include "libxml2_globals_private.h"
       
    23 #include <xml/utils/xmlengutils.h>
       
    24 #include <utf.h>
       
    25 /**
       
    26  * Class CXmlUtilStep Implementation
       
    27  */
       
    28 
       
    29 /**
       
    30  * Constructor. Sets the test step name so that it can be 
       
    31  * differentiated from within doTestStepL()
       
    32  */
       
    33 CXmlUtilStep::CXmlUtilStep(const TDesC& aStepName)
       
    34 	{
       
    35 	SetTestStepName(aStepName);
       
    36 	}
       
    37 
       
    38 /**
       
    39  * TEF invokes doTestStepL interface along with the step name
       
    40  * to let appropriate action to be taken for the test step.
       
    41  */
       
    42 TVerdict CXmlUtilStep::doTestStepL(void)
       
    43 	{
       
    44 	if (TestStepName() == KXmlEngXmlCharFromDes)
       
    45 		{
       
    46 		INFO_PRINTF1(KXmlEngXmlCharFromDes);
       
    47 		SetTestStepResult(TestKXmlEngXmlCharFromDes());
       
    48 		}
       
    49 	else if (TestStepName() == KXmlEngEscapeForXmlValueStep2)
       
    50 		{
       
    51 		INFO_PRINTF1(KXmlEngEscapeForXmlValueStep2);
       
    52 		SetTestStepResult(TestKXmlEngEscapeForXmlValueStep2());
       
    53 		}
       
    54 	else if (TestStepName() == KXmlEngEscapeForXmlValueStep1)
       
    55 		{
       
    56 		INFO_PRINTF1(KXmlEngEscapeForXmlValueStep2);
       
    57 		SetTestStepResult(TestKXmlEngEscapeForXmlValueStep1());
       
    58 		}
       
    59 	return TestStepResult();
       
    60 	}
       
    61 /**
       
    62  * Improving on CLibxml2Tester::UtilsXmlCharFromDesL
       
    63  */
       
    64 TVerdict CXmlUtilStep::TestKXmlEngXmlCharFromDes()
       
    65 	{
       
    66 	_LIT(KText, "Test Data");
       
    67 	TBufC<10> buf(KText);
       
    68 	TPtrC desc = buf.Des();
       
    69 	TBuf8<10> desc8;
       
    70 	char * tekst = XmlEngXmlCharFromDesL(desc);
       
    71 	
       
    72 	CnvUtfConverter::ConvertFromUnicodeToUtf8(desc8, desc);
       
    73 	if (!CompareDescAndChar(tekst, desc8))
       
    74 		{
       
    75 		return EFail;
       
    76 		}
       
    77 	TBufC8<10> buf8(desc8);
       
    78 	TPtrC8 inDesc8 = buf8.Des();
       
    79 	const char * compareString = "Test Data";
       
    80 	char * tekst2 = XmlEngXmlCharFromDes8L(inDesc8);
       
    81 	if(!CompareChars(tekst2, compareString))
       
    82 		{
       
    83 		return EFail;
       
    84 		}
       
    85 	return EPass;
       
    86 	}
       
    87 
       
    88 /**
       
    89  * Improving on CLibxml2Tester::UtilsEscapeForXmlValueL
       
    90  */
       
    91 TVerdict CXmlUtilStep::TestKXmlEngEscapeForXmlValueStep1()
       
    92 	{
       
    93 	const char * testString = "Test < & > text.";
       
    94 	const char * compareString = "Test &lt; &amp; &gt; text.";
       
    95 	//Test step 1
       
    96 	char *resultString = XmlEngEscapeForXmlValueL(testString);
       
    97 	if (!CompareChars(resultString, compareString))
       
    98 		{
       
    99 		return EFail;
       
   100 		}
       
   101 	return EPass;
       
   102 	}
       
   103 
       
   104 /**
       
   105  * Tests XmlEngEscapeForXmlValueL under different memory conditions
       
   106  * This test step should be run only on debug builds owing to the 
       
   107  * __UHEAP macros that are defined only in debug builds.
       
   108  */
       
   109 TVerdict CXmlUtilStep::TestKXmlEngEscapeForXmlValueStep2()
       
   110 	{
       
   111 #ifdef _DEBUG
       
   112 	TInt err = KErrNone;
       
   113 	const char * testString = "Test < & > text.";
       
   114 	xmlGlobalStatePtr gsPtr = xmlCreateAndInitializeGlobalState();
       
   115 	if (!gsPtr)
       
   116 		{
       
   117 		INFO_PRINTF1(_L("Warning: Global state pointer is NULL\n"));
       
   118 		}
       
   119 	__UHEAP_MARK;
       
   120 	__UHEAP_SETFAIL(RHeap::EDeterministic, 1);
       
   121 
       
   122 	TRAP(err, XmlEngEscapeForXmlValueL(testString));
       
   123 	if (err != KErrNoMemory)
       
   124 		{
       
   125 		return EFail;
       
   126 		}
       
   127 	char* result= NULL;
       
   128 	TRAP(err, result = XmlEngEscapeForXmlValueL(NULL));
       
   129 	if (err != KErrNone && !result)
       
   130 		{
       
   131 		return EFail;
       
   132 		}
       
   133 	__UHEAP_SETFAIL(RHeap::ENone, 0);
       
   134 	__UHEAP_MARKEND;
       
   135 #else
       
   136 	INFO_PRINTF1(_L("This test is meant to be run only in DEBUG builds. Returning EPASS"));
       
   137 #endif
       
   138 	return EPass;
       
   139 	}
       
   140 
       
   141 /**
       
   142  * Utility function to compare the characters
       
   143  */
       
   144 TBool CXmlUtilStep::CompareChars(const char* aString1, const char* aString2)
       
   145 	{
       
   146 	TInt strLen = strlen(aString1);
       
   147 	TInt desLen = strlen(aString2);
       
   148 	if (strLen != desLen)
       
   149 		{
       
   150 		return FALSE;
       
   151 		}
       
   152 	else
       
   153 		{
       
   154 		for (TInt i = 0; i < strLen; i++)
       
   155 			{
       
   156 			if (aString1[i] != aString2[i])
       
   157 				return FALSE;
       
   158 			}
       
   159 		}
       
   160 	return TRUE;
       
   161 	}
       
   162 
       
   163 /**
       
   164  * Utility function to compare C string with Desc8
       
   165  */
       
   166 TBool CXmlUtilStep::CompareDescAndChar(const char* aString,TDesC8& aDesc)
       
   167 {
       
   168 	TInt strLen = strlen(aString);
       
   169 	TInt desLen = aDesc.Length();
       
   170 	if (strLen != desLen)
       
   171 	{
       
   172 		return FALSE;	
       
   173 	}
       
   174 	else
       
   175 	{
       
   176 		for(TInt i = 0; i < strLen; i++)
       
   177 		{
       
   178 			if (aString[i] != aDesc[i])
       
   179 				return FALSE;
       
   180 		}
       
   181 	}	
       
   182 	return TRUE;
       
   183 }