sl@0: /* sl@0: * Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies). sl@0: * All rights reserved. sl@0: * This component and the accompanying materials are made available sl@0: * under the terms of the License "Eclipse Public License v1.0" sl@0: * which accompanies this distribution, and is available sl@0: * at the URL "http://www.eclipse.org/legal/epl-v10.html". sl@0: * sl@0: * Initial Contributors: sl@0: * Nokia Corporation - initial contribution. sl@0: * sl@0: * Contributors: sl@0: * sl@0: * Description: sl@0: * sl@0: */ sl@0: sl@0: sl@0: /** sl@0: @file sl@0: @internalComponent - Internal Symbian test code sl@0: */ sl@0: sl@0: sl@0: #include "trecogstep.h" sl@0: sl@0: // Literals & Constants sl@0: _LIT(KCompleted, "Completed."); sl@0: _LIT(KMethodName, "byName"); sl@0: _LIT(KMethodHandle, "byHandle"); sl@0: _LIT(KMethodBuffer, "byBuffer"); sl@0: _LIT8(KEmptyBuffer8,""); sl@0: sl@0: sl@0: class RDeletefile sl@0: { sl@0: public: sl@0: RDeletefile(RFs &aFs, TDesC &aFileName) sl@0: : iFs(aFs), sl@0: iFileName(aFileName) sl@0: { sl@0: } sl@0: void Close() sl@0: { sl@0: (void) iFs.Delete(iFileName); sl@0: } sl@0: sl@0: private: sl@0: RFs &iFs; sl@0: TDesC &iFileName; sl@0: }; sl@0: sl@0: void CRecogStep::TestRecognizeDataL() sl@0: { sl@0: TDataRecognitionResult recogResult; sl@0: sl@0: TPtrC tmp; sl@0: GetStringFromConfig(ConfigSection(), _L("fileName"), tmp); sl@0: iFileName = tmp; sl@0: sl@0: sl@0: TPtrC expectedDataType16; sl@0: GetStringFromConfig(ConfigSection(), _L("expectedDataType"), expectedDataType16); sl@0: HBufC8 *expectedDataBuf8 = ConvertDes16toHBufC8LC(expectedDataType16); sl@0: sl@0: TInt expectedConfidence; sl@0: GetIntFromConfig(ConfigSection(), _L("expectedConfidence"), expectedConfidence); sl@0: sl@0: TInt maxDataBufSize; sl@0: if (GetIntFromConfig(ConfigSection(), _L("maxDataBufSize"), maxDataBufSize)) sl@0: { sl@0: iLs.SetMaxDataBufSize(maxDataBufSize); sl@0: } sl@0: sl@0: TPtrC method; sl@0: GetStringFromConfig(ConfigSection(), _L("method"), method); sl@0: sl@0: TBool checkSpecific = EFalse; sl@0: TBool matchedSpecificMimeType = EFalse; sl@0: TPtrC specificMimeType; sl@0: if (method == KMethodHandle) sl@0: { sl@0: checkSpecific = GetStringFromConfig(ConfigSection(), _L("checkSpecificMimeType"), specificMimeType); sl@0: } sl@0: sl@0: TInt usePrivateFile = 0; sl@0: GetIntFromConfig(ConfigSection(), _L("usePrivateFile"), usePrivateFile); sl@0: sl@0: RDeletefile deletefile(iTheFs, iFileName); sl@0: if(!usePrivateFile) sl@0: { sl@0: // Tweak file modification time to defeat the apparch recognizer result cache.... sl@0: TTime time; sl@0: User::LeaveIfError(iTheFs.Modified(iFileName, time)); sl@0: time += TTimeIntervalSeconds(1); sl@0: User::LeaveIfError(iTheFs.SetModified(iFileName, time)); sl@0: } sl@0: else sl@0: { sl@0: // Copy file to private dir, this will make it inaccesible to the recognizer code (except via handle). sl@0: ConvertFileToPrivateL(); sl@0: CleanupClosePushL(deletefile); sl@0: } sl@0: sl@0: sl@0: if (method == KMethodName) sl@0: { sl@0: INFO_PRINTF2(_L("Test Recognizing %S by File Name"), &iFileName); sl@0: User::LeaveIfError(iLs.RecognizeData(iFileName, KEmptyBuffer8, recogResult)); sl@0: } sl@0: else if((method == KMethodHandle) || (method == KMethodBuffer)) sl@0: { sl@0: RFile fileToRead; sl@0: User::LeaveIfError(fileToRead.Open(iTheFs, iFileName, EFileShareReadersOrWriters | EFileRead | EFileStream)); sl@0: CleanupClosePushL(fileToRead); sl@0: if(method == KMethodHandle) sl@0: { sl@0: if (checkSpecific) sl@0: { sl@0: HBufC8* specificMimeType8 = ConvertDes16toHBufC8LC(specificMimeType); sl@0: TDataType mime(*specificMimeType8); sl@0: INFO_PRINTF2(_L("Test matching specific mime-type %S by Handle"), &iFileName); sl@0: User::LeaveIfError(iLs.RecognizeSpecificData(fileToRead, mime, matchedSpecificMimeType)); sl@0: CleanupStack::PopAndDestroy(specificMimeType8); sl@0: } sl@0: else sl@0: { sl@0: INFO_PRINTF2(_L("Test Recognizing %S by Handle"), &iFileName); sl@0: User::LeaveIfError(iLs.RecognizeData(fileToRead, recogResult)); sl@0: } sl@0: } sl@0: else sl@0: { sl@0: INFO_PRINTF2(_L("Test Recognizing %S by Buffer"), &iFileName); sl@0: TInt size; sl@0: User::LeaveIfError(fileToRead.Size(size)); sl@0: sl@0: TInt maxBufferSize; sl@0: if(GetIntFromConfig(ConfigSection(), _L("maxBufferSize"), maxBufferSize)) sl@0: { sl@0: if(size > maxBufferSize) sl@0: { sl@0: size = maxBufferSize; sl@0: } sl@0: } sl@0: sl@0: HBufC8* memForFile = HBufC8::NewLC(size); sl@0: TPtr8 fileContent(memForFile->Des()); sl@0: User::LeaveIfError(fileToRead.Read(fileContent, size)); sl@0: User::LeaveIfError(iLs.RecognizeData(iFileName, fileContent, recogResult)); sl@0: CleanupStack::PopAndDestroy(); //memForFile, sl@0: } sl@0: CleanupStack::PopAndDestroy(&fileToRead); sl@0: } sl@0: else sl@0: { sl@0: ERR_PRINTF1(_L("method not set correctly")); sl@0: User::Leave(KErrArgument); sl@0: } sl@0: sl@0: sl@0: if (checkSpecific) sl@0: { sl@0: if (matchedSpecificMimeType) sl@0: { sl@0: INFO_PRINTF2(_L("Specific type '%S' matched\n"), &specificMimeType); sl@0: SetTestStepResult(EPass); sl@0: } sl@0: else sl@0: { sl@0: INFO_PRINTF2(_L("Specific type '%S' not matched\n"), &specificMimeType); sl@0: SetTestStepResult(EFail); sl@0: } sl@0: } sl@0: else sl@0: { sl@0: TPtrC result16 = recogResult.iDataType.Des(); sl@0: if (// Expected failure sl@0: ((expectedConfidence == CApaDataRecognizerType::ENotRecognized) && sl@0: (recogResult.iDataType != expectedDataBuf8->Des())) || sl@0: // Expected success sl@0: ((recogResult.iConfidence == expectedConfidence) && sl@0: (recogResult.iDataType == expectedDataBuf8->Des()))) sl@0: { sl@0: INFO_PRINTF3(_L("PASSED - type '%S' confidence=%d\n"), sl@0: &result16, sl@0: recogResult.iConfidence); sl@0: SetTestStepResult(EPass); sl@0: } sl@0: else sl@0: { sl@0: ERR_PRINTF5(_L("FAILED - expected '%S', got type '%S' - expected confidence=%d got confidence %d\n"), sl@0: &expectedDataType16, sl@0: &result16, sl@0: expectedConfidence, sl@0: recogResult.iConfidence); sl@0: SetTestStepResult(EFail); sl@0: } sl@0: } sl@0: sl@0: if(usePrivateFile) sl@0: { sl@0: CleanupStack::PopAndDestroy(&deletefile); sl@0: } sl@0: sl@0: CleanupStack::PopAndDestroy(expectedDataBuf8); sl@0: INFO_PRINTF1(KCompleted); sl@0: } sl@0: sl@0: HBufC8* CRecogStep::ConvertDes16toHBufC8LC(TDesC& source) sl@0: { sl@0: HBufC8 *buf = HBufC8::NewL(source.Length()); sl@0: CleanupStack::PushL(buf); sl@0: TPtr8 ptr=buf->Des(); sl@0: sl@0: CCnvCharacterSetConverter *converter = CCnvCharacterSetConverter::NewLC(); sl@0: converter->PrepareToConvertToOrFromL(KCharacterSetIdentifierAscii, iTheFs); sl@0: converter->ConvertFromUnicode(ptr, source); sl@0: CleanupStack::PopAndDestroy(converter); sl@0: return buf; sl@0: } sl@0: sl@0: sl@0: sl@0: void CRecogStep::RunTestCasesL() sl@0: { sl@0: __UHEAP_MARK; sl@0: TRAPD(r,TestRecognizeDataL()); sl@0: TEST(r==KErrNone); sl@0: __UHEAP_MARKEND; sl@0: } sl@0: sl@0: sl@0: /** sl@0: Destructor sl@0: */ sl@0: CRecogStep::~CRecogStep() sl@0: { sl@0: iTheFs.Close(); sl@0: iLs.Close(); sl@0: } sl@0: sl@0: /** sl@0: Constructor sl@0: */ sl@0: CRecogStep::CRecogStep() sl@0: { sl@0: sl@0: TInt ret=iTheFs.Connect(); sl@0: TEST(ret==KErrNone); sl@0: iTheFs.ShareProtected(); sl@0: sl@0: ret = iLs.Connect(); sl@0: TEST(ret==KErrNone); sl@0: sl@0: sl@0: // Call base class method to set up the human readable name for logging sl@0: SetTestStepName(KRecogStep); sl@0: } sl@0: sl@0: /** sl@0: @return - TVerdict code sl@0: Override of base class virtual sl@0: */ sl@0: TVerdict CRecogStep::doTestStepPreambleL() sl@0: { sl@0: SetTestStepResult(EPass); sl@0: return TestStepResult(); sl@0: } sl@0: sl@0: /** sl@0: @return - TVerdict code sl@0: Override of base class virtual sl@0: */ sl@0: TVerdict CRecogStep::doTestStepPostambleL() sl@0: { sl@0: return TestStepResult(); sl@0: } sl@0: sl@0: sl@0: TVerdict CRecogStep::doTestStepL() sl@0: { sl@0: TRAPD(ret,RunTestCasesL()) sl@0: TEST(ret==KErrNone); sl@0: return TestStepResult(); sl@0: } sl@0: sl@0: void CRecogStep::ConvertFileToPrivateL() sl@0: { sl@0: // Read source file sl@0: RFile fromFile; sl@0: User::LeaveIfError(fromFile.Open(iTheFs, iFileName, EFileShareReadersOrWriters | EFileRead | EFileStream)); sl@0: CleanupClosePushL(fromFile); sl@0: TInt size; sl@0: User::LeaveIfError(fromFile.Size(size)); sl@0: HBufC8* memForFileContents = HBufC8::NewLC(size); sl@0: TPtr8 fileContents(memForFileContents->Des()); sl@0: User::LeaveIfError(fromFile.Read(fileContents, size)); sl@0: sl@0: // Create destination path sl@0: TDriveUnit sysDrive = RFs::GetSystemDrive(); sl@0: (void)iTheFs.CreatePrivatePath(static_cast (sysDrive)); sl@0: TFileName newFileName; sl@0: User::LeaveIfError(iTheFs.PrivatePath(newFileName)); sl@0: newFileName.Insert(0, sysDrive.Name()); sl@0: TParsePtr parseFrom(iFileName); sl@0: newFileName.Append(parseFrom.NameAndExt()); sl@0: sl@0: // Make sure destination does not already exist. sl@0: (void) iTheFs.Delete(newFileName); sl@0: sl@0: // Copy file to private location sl@0: RFile toFile; sl@0: User::LeaveIfError(toFile.Create(iTheFs, newFileName, EFileShareExclusive | EFileWrite | EFileStream)); sl@0: CleanupClosePushL(toFile); sl@0: sl@0: User::LeaveIfError(toFile.Write(fileContents)); sl@0: sl@0: // Update iFileName sl@0: iFileName = newFileName; sl@0: sl@0: // Cleanup sl@0: CleanupStack::PopAndDestroy(&toFile); sl@0: CleanupStack::PopAndDestroy(memForFileContents); sl@0: CleanupStack::PopAndDestroy(&fromFile); sl@0: } sl@0: sl@0: // End of file