messagingfw/msgtestfw/TestActionUtils/src/TestFrameworkActionsUtils.cpp
changeset 0 8e480a14352b
equal deleted inserted replaced
-1:000000000000 0:8e480a14352b
       
     1 // Copyright (c) 2003-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
       
    18 */
       
    19 
       
    20 #include "TestFrameworkActionsUtils.h"
       
    21 #include "watcherdef.h"
       
    22 
       
    23 #include <es_sock.h>
       
    24 #include <smsuaddr.h>
       
    25 #include <etelmm.h>
       
    26 #include <etel.h>
       
    27 #include <etelmm.h>
       
    28 
       
    29 const TInt KDefBufferSize = 1024;
       
    30 
       
    31 TBool TestFrameworkActionsUtils::CheckIfWatchersAlreadyRunningL()
       
    32 	{
       
    33 	TFindProcess find(_L("Watcher*"));
       
    34 	TFileName name;	// not used
       
    35 	return (find.Next(name) == KErrNone);
       
    36 	}
       
    37 	
       
    38 	
       
    39 TBool TestFrameworkActionsUtils::CheckIfSmsWatcherAlreadyRunningL()
       
    40 	{
       
    41 	RSocketServ socketServer;
       
    42 	RSocket socket;
       
    43 	
       
    44 	User::LeaveIfError(socketServer.Connect());
       
    45 	CleanupClosePushL(socketServer);
       
    46 
       
    47 	TProtocolDesc protoInfo;
       
    48 	TProtocolName protocolname;
       
    49 	protocolname.Copy(KSmsDatagram);
       
    50 	User::LeaveIfError(socketServer.FindProtocol(protocolname,protoInfo));
       
    51 	User::LeaveIfError(socket.Open(socketServer,
       
    52 										protoInfo.iAddrFamily,
       
    53 										protoInfo.iSockType, 
       
    54 										protoInfo.iProtocol));
       
    55 
       
    56 	CleanupClosePushL(socket);
       
    57 	
       
    58 	TSmsAddr addr;
       
    59 	addr.SetSmsAddrFamily(ESmsAddrRecvAny);
       
    60 	TInt err = socket.Bind(addr);
       
    61 	
       
    62 	socket.CancelAll();
       
    63 	CleanupStack::PopAndDestroy(2); //socketServer, socket
       
    64 		
       
    65 	return (err == KErrAlreadyExists);
       
    66 	}
       
    67 /**
       
    68 Compares two input files (ASCII & Unicode).
       
    69 @return the error code
       
    70 @param aFileOne	- First file
       
    71 @param aFileTwo	- Second file
       
    72 @param aIsUnicode - Whether unicode or ascii
       
    73 @param aIgnoreCharList - Ignore characters list
       
    74 @param aDiffFlag - Out parameter ETrue if the files are different
       
    75 @leave KErrNoMemory
       
    76  */
       
    77 TInt TestFrameworkActionsUtils::CompareFilesL(TPtrC aFileOne, TPtrC aFileTwo, TBool aIsUnicode,
       
    78 									  CArrayFixFlat<TUint16>* aIgnoreCharList, TBool& aDiffFlag)
       
    79 	{
       
    80 	// Retern Code
       
    81 	TInt retCode = KErrNone;
       
    82 
       
    83 	// Streams for the file1 and file2 to compare
       
    84 	RFileReadStream fileReadStream1;
       
    85 	RFileReadStream fileReadStream2;
       
    86 
       
    87 	// Files difference flag
       
    88 	aDiffFlag = EFalse;
       
    89 
       
    90 	RFs iFs;
       
    91 	User::LeaveIfError(iFs.Connect());
       
    92 
       
    93 	// Open file one to compare
       
    94 	retCode = fileReadStream1.Open( iFs, aFileOne, EFileShareReadersOnly);//EFileRead);
       
    95 	if(retCode == KErrNone)
       
    96 		{
       
    97 		retCode = fileReadStream2.Open( iFs, aFileTwo, EFileShareReadersOnly);//EFileRead);
       
    98 		if(retCode == KErrNone)
       
    99 			{
       
   100 			// To hold the line content of file one 
       
   101 			HBufC* lineOneBuffer = HBufC::NewLC(KDefBufferSize);
       
   102 			TPtr ptrLineOne = lineOneBuffer->Des();
       
   103 			
       
   104 			// To hold the line content of file two
       
   105 			HBufC* lineTwoBuffer = HBufC::NewLC(KDefBufferSize);
       
   106 			TPtr ptrLineTwo = lineTwoBuffer->Des();
       
   107 
       
   108 			// Eof indicators
       
   109 			TBool	eofOne = EFalse;
       
   110 			TBool	eofTwo = EFalse; 
       
   111 			
       
   112 			// Read the file one and file two data
       
   113 			do   
       
   114 				{
       
   115 				// Read file one data
       
   116 				eofOne = ReadDataL(fileReadStream1, ptrLineOne, aIsUnicode, aIgnoreCharList);
       
   117 				// Read file two data
       
   118 				eofTwo = ReadDataL(fileReadStream2, ptrLineTwo, aIsUnicode, aIgnoreCharList);
       
   119 				// Check EOF state of the files.
       
   120 				// Either both the files will be in EOF state or 
       
   121 				// none of the files will be in EOF state			  
       
   122 				if((!eofOne && !eofTwo)||(eofOne && eofTwo))
       
   123 	            	{			
       
   124 					// Compare the read lines from file one and the file two
       
   125 					if(ptrLineOne.Compare(ptrLineTwo) != 0)
       
   126 						{
       
   127 						// Different data content
       
   128 						aDiffFlag = ETrue;
       
   129 						}
       
   130 					}
       
   131 				else
       
   132 					{
       
   133 					//Different EOF
       
   134 					aDiffFlag = ETrue;
       
   135 					}
       
   136 				} while(!eofOne && !eofTwo && !aDiffFlag);				
       
   137 			
       
   138 			// Delete line buffers
       
   139 			CleanupStack::PopAndDestroy(2, lineOneBuffer);				
       
   140 			}
       
   141 		}
       
   142 	
       
   143 	// Close the open streams
       
   144 	fileReadStream1.Close();
       
   145 	fileReadStream2.Close();	
       
   146 
       
   147 	iFs.Close();
       
   148 	// Return the difference flag
       
   149 	return retCode;
       
   150 	}
       
   151 
       
   152 /**
       
   153 Reads data chunk from the file stream.
       
   154 @return ETrue if the EOF found.
       
   155 @param aFileReadStream - reference to file stream 
       
   156 @param aPtrLineBuffer - reference to the buffer data read
       
   157 @param aIsUnicode - flag to check is it Unicode or ASCII file
       
   158 @param aIgnoreCharList - pointer to ingnore char list
       
   159 @leave KErrNotFound
       
   160  */
       
   161 TBool TestFrameworkActionsUtils::ReadDataL(RFileReadStream& aFileReadStream,
       
   162 	  TPtr& aPtrLineBuffer, TBool aIsUnicode, CArrayFixFlat<TUint16>* aIgnoreCharList)
       
   163  	{
       
   164 	TUint16			element = 0;
       
   165 	TKeyArrayFix	key(0, ECmpTUint16);
       
   166 	TInt			charPosition = 0;
       
   167 	TBool			eof = EFalse;
       
   168 	TInt			errorCode = KErrNone;
       
   169 	TUint8			aCharASCII = 0;
       
   170 
       
   171 	aPtrLineBuffer.FillZ();
       
   172 	aPtrLineBuffer.SetLength(0);	
       
   173 	
       
   174 	// Validate the input ignore char list
       
   175 	if (!aIgnoreCharList)
       
   176 		{
       
   177 		// Invalid parameter passed to ReadDataL: No ignore char list passed
       
   178 		User::Leave(KErrNotFound);
       
   179 		}
       
   180 
       
   181 	// Read data from file and store it in lineBuffer 
       
   182 	do   
       
   183 		{
       
   184 		if(aIsUnicode)
       
   185 			{
       
   186 			TRAP(errorCode, (element = aFileReadStream.ReadUint16L()));
       
   187 			}
       
   188 		else
       
   189 			{
       
   190 			TRAP(errorCode, (aCharASCII = aFileReadStream.ReadUint8L()));
       
   191 			element = (TUint16) aCharASCII;
       
   192 			}
       
   193 			
       
   194 		if (errorCode!=KErrEof)
       
   195 			{		
       
   196 			// Check the availability of ignore char in the array							
       
   197 			if ( aIgnoreCharList->Find(element, key, charPosition) != KErrNone )
       
   198 				{
       
   199 				// Append the char to the buffer if the read char is not ignore char
       
   200 				aPtrLineBuffer.Append(element);
       
   201 				}
       
   202 			}								
       
   203 			else
       
   204 				{
       
   205 				eof = ETrue;
       
   206 				break;
       
   207 				}
       
   208 				
       
   209 			// Stop at the end of line or no more data 
       
   210 			} while(aPtrLineBuffer.Length()<KDefBufferSize);
       
   211 	
       
   212 	// Return end of file flag		
       
   213 	return eof;	
       
   214 	}
       
   215 
       
   216 
       
   217 
       
   218 /**
       
   219 Get Byte from Unicode Hex Text character
       
   220 @return 
       
   221 @param Hex character
       
   222 @leave None
       
   223  */
       
   224 TUint8 TestFrameworkActionsUtils::GetByteFromUnicodeHexTextChar(const TUint16 *input)
       
   225 	{
       
   226 	TUint8 result = (TUint8)*input;
       
   227 
       
   228 	if(result >='0' && result <='9')
       
   229 		result = (TUint8)(result - '0');
       
   230 	else if(result >='a' && result <='f')
       
   231 		result = (TUint8)(result - 'a' + 10);
       
   232 	else if(result >='A' && result <='F')
       
   233 		result = (TUint8)(result - 'A' + 10);
       
   234 	else
       
   235 		result = 0;
       
   236 
       
   237 	return result;
       
   238 	}