Update contrib.
1 // Copyright (c) 2008-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".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
20 #include "srtdecoder.h"
21 #include "srtreader.h"
22 #include "testsrtdecoder.h"
24 _LIT(KSampleSubtitleSRTFilepath1, "c:\\mm\\subtitle1.srt");
25 _LIT(KSampleSubtitleSRTFilepath2, "c:\\mm\\subtitle2.srt");
27 // subtitlebmp0.mbm -- subtitlebmp9.mbm: bitmaps for comparison
28 _LIT(KSubtitleTargetBitmapFilePathSpec, "c:\\mm\\subtitlebmp%d.mbm");
29 // subtitlecurbmp0.mbm -- subtitlecurbmp9.mbm: runtime bitmaps for comparison,
30 // will be removed unless MMF_SUBTITLE_SUPPORT_TEST_SAVEDATA is defined
31 _LIT(KSubtitleCurrentBitmapFilePathSpec, "c:\\mm\\subtitlecurbmp%d.mbm");
33 // defining MMF_SUBTITLE_SUPPORT_TEST_SAVEDATA means re-creating the test data (bitmaps) for comparison, instead
34 // of doing the comparison with the existing test data
35 // at runtime with the spec of KSubtitleCurrentBitmapFilePathSpec
36 #ifndef MMF_SUBTITLE_SUPPORT_TEST_SAVEDATA
37 // #define MMF_SUBTITLE_SUPPORT_TEST_SAVEDATA
40 void RTestSrtDecoderStep::InitializeTestStepL(TBool aCreateSrtReaderOnly, const TDesC& aSrtFilePath)
43 iSrtReader = CSrtReader::NewL(aSrtFilePath);
45 iCreateSrtReaderOnly = aCreateSrtReaderOnly;
46 if (!iCreateSrtReaderOnly)
48 User::LeaveIfError(iRbsSession.Connect());
49 iSrtDecoder = CSrtSubtitleDecoder::NewL(*iSrtReader);
53 void RTestSrtDecoderStep::UnInitializeTestStep()
55 if (!iCreateSrtReaderOnly)
57 iRbsSession.Disconnect();
64 // Implementation of the generic test step
65 TBool RTestSrtDecoderStep::CompareFilesL(RFs &aFs, const TDesC& aFilePath1, const TDesC& aFilePath2)
67 TBool identical = EFalse;
73 if (aFilePath1 == aFilePath2)
79 User::LeaveIfError(file1.Open(aFs, aFilePath1, EFileRead));
80 CleanupClosePushL(file1);
81 User::LeaveIfError(file2.Open(aFs, aFilePath2, EFileRead));
82 CleanupClosePushL(file2);
84 User::LeaveIfError(file1.Size(file1Size));
85 User::LeaveIfError(file2.Size(file2Size));
87 if (file1Size == file2Size)
95 User::LeaveIfError(file1.Read(buffer1));
96 User::LeaveIfError(file2.Read(buffer2));
98 if (buffer1 != buffer2)
104 if (0 == buffer1.Length())
111 CleanupStack::PopAndDestroy(&file2);
112 CleanupStack::PopAndDestroy(&file1);
118 TBool RTestSrtDecoderStep::IsFontAvailableL(const TDesC& aTypefaceName)
120 TBool avail = EFalse;
121 TSize bmpSize(100, 100);
123 CFbsBitmap* sampleBitmap = new (ELeave) CFbsBitmap();
124 CleanupStack::PushL(sampleBitmap);
126 User::LeaveIfError(sampleBitmap->Create(bmpSize, EColor16MA));
128 CFbsBitmapDevice* bmpDevice = CFbsBitmapDevice::NewL(sampleBitmap);
129 CleanupStack::PushL(bmpDevice);
131 if (0 != FontUtils::TypefaceAttributes(*bmpDevice, aTypefaceName))
133 const TInt KSubtitleFontMaxSpecHeights = 128;
134 CArrayFix<TInt> *listHeights = new (ELeave) CArrayFixFlat<TInt>(KSubtitleFontMaxSpecHeights);
135 CleanupStack::PushL(listHeights);
137 FontUtils::GetAvailableHeightsInTwipsL(*bmpDevice, aTypefaceName, *listHeights);
138 for (TInt i = 0; i < listHeights->Count(); i++)
140 if (KSrtTargetTypefaceHeightInTwips == (*listHeights)[i])
147 CleanupStack::PopAndDestroy(listHeights);
150 CleanupStack::PopAndDestroy(bmpDevice);
151 CleanupStack::PopAndDestroy(sampleBitmap);
156 TBool RTestSrtDecoderStep::CompareBmpFilesL(TInt aStartIndex, TInt aEndIndex, const TDesC& aSrcFilePathSpec, const TDesC& aDestFilePathSpec)
158 TBool identical = ETrue;
159 TBuf <KMaxFullName> bitmapFilename1;
160 TBuf <KMaxFullName> bitmapFilename2;
162 User::LeaveIfError(rfs.Connect());
163 CleanupClosePushL(rfs);
164 for (TInt i = aStartIndex; i <= aEndIndex; i++)
166 bitmapFilename1.Format(aDestFilePathSpec, i);
167 bitmapFilename2.Format(aSrcFilePathSpec, i);
168 if (!CompareFilesL(rfs, bitmapFilename1, bitmapFilename2))
174 CleanupStack::PopAndDestroy(&rfs);
179 void RTestSrtDecoderStep::DeleteTempFiles(TInt aStartIndex, TInt aEndIndex, const TDesC& aFilePathSpec)
181 TBuf <KMaxFullName> bitmapFilename;
184 if (KErrNone == rfs.Connect())
186 for (TInt i = aStartIndex ; i <= aEndIndex; i++)
188 bitmapFilename.Format(aFilePathSpec, i);
189 BaflUtils::DeleteFile(rfs, bitmapFilename);
195 // Implementation of RTestSrtDecoderStep0002
197 RTestSrtDecoderStep0002::RTestSrtDecoderStep0002()
199 iTestStepName = _L("MM-MMF-SUBTITLE-SRTDECODER-U-0002-HP");
202 TVerdict RTestSrtDecoderStep0002::DoTestStepPreambleL()
204 InitializeTestStepL(EFalse, KSampleSubtitleSRTFilepath1);
206 // Install the Active Scheduler
207 iActiveScheduler = new(ELeave) CActiveScheduler;
208 CActiveScheduler::Install(iActiveScheduler);
209 iActiveSchedulerStarted = EFalse;
216 TVerdict RTestSrtDecoderStep0002::DoTestStepPostambleL()
219 CActiveScheduler::Install(NULL);
220 delete iActiveScheduler;
221 iActiveScheduler = NULL;
223 UnInitializeTestStep();
228 TVerdict RTestSrtDecoderStep0002::DoTestStepL()
230 INFO_PRINTF1(_L("Enter DoTestStepL"));
231 TVerdict result = EPass;
233 TRAPD(err, TestGetNextFrameL());
237 ERR_PRINTF2(_L("Error - RTestSrtDecoderStep0002::TestGetNextFrameL failed. error code %d. "), err);
240 INFO_PRINTF1(_L("Exit DoTestStepL"));
244 void RTestSrtDecoderStep0002::InitWservL()
246 TInt err = iWs.Connect();
250 // Access violation if ws is null
251 ERR_PRINTF2(_L("Error - Failed to connect to RWsSession. error code %d. "), err);
255 iScreen = new (ELeave) CWsScreenDevice(iWs); // make device for this session
256 User::LeaveIfError(iScreen->Construct()); // and complete its construction
258 iRootWindow = RWindowGroup(iWs);
259 User::LeaveIfError(iRootWindow.Construct((TUint32)this, ETrue));
261 iWindow = new(ELeave) RWindow(iWs);
262 User::LeaveIfError(((RWindow*)iWindow)->Construct(iRootWindow,((TUint32)(this)) + 1));
263 iWindow->SetExtent(TPoint(0,0), iScreen->SizeInPixels());
264 iWindow->SetVisible(ETrue);
265 iWindow->SetRequiredDisplayMode(EColor16MA);
267 // Light Sky Blue 135-206-250
268 TRgb backgroundColour = TRgb(135, 206, 250);
269 iWindow->SetBackgroundColor(backgroundColour);
271 iGc = new(ELeave) CWindowGc(iScreen);
272 User::LeaveIfError(iGc->Construct());
278 void RTestSrtDecoderStep0002::UninitWserv()
298 void RTestSrtDecoderStep0002::PrepGc()
300 iGc->Activate(*iWindow);
301 iWindow->Invalidate();
302 iWindow->BeginRedraw();
304 iGc->Clear(TRect(iScreen->SizeInPixels()));
308 void RTestSrtDecoderStep0002::RetireGc()
311 iWindow->EndRedraw();
315 void RTestSrtDecoderStep0002::DrawBitmap(CFbsBitmap& aBitmap)
319 TSize size = iScreen->SizeInPixels();
320 TInt width = size.iWidth;
321 TInt height = size.iHeight;
324 // Draw a square border
325 iGc->SetPenColor(TRgb(255,0,0));
326 iGc->DrawLine(TPoint(0,0),TPoint(0,height-1));
327 iGc->DrawLine (TPoint (0, height-1), TPoint (width-1, height-1));
328 iGc->DrawLine(TPoint(width-1,height-1),TPoint(width-1,0));
329 iGc->DrawLine (TPoint (width-1, 0), TPoint (0, 0));
331 // Draw a line between the corners of the window
332 iGc->DrawLine(TPoint(0,0),TPoint(width, height));
333 iGc->DrawLine (TPoint (0, height), TPoint (width, 0));
336 iGc->BitBlt(pos, &aBitmap);
341 void RTestSrtDecoderStep0002::TestGetNextFrameL()
343 // start/stop for multiple times is also tested.
344 const TInt64 KSrtMicroSecondsInAMilliSecond = 1000;
345 const TInt64 KSrtMicroSecondsInASecond = KSrtMicroSecondsInAMilliSecond * 1000;
346 const TInt64 KSrtMicroSecondsInAMinute = KSrtMicroSecondsInASecond * 60;
347 const TInt64 KSrtMicroSecondsInAnHour = KSrtMicroSecondsInAMinute * 60;
348 const TInt KSrtSetPosTestCount = 6;
349 const TInt KSrtNumOfFrames = 10;
351 TTimeIntervalMicroSeconds videoPos[KSrtSetPosTestCount] =
354 KSrtMicroSecondsInAMinute,
355 1 * KSrtMicroSecondsInAMinute + 2 * KSrtMicroSecondsInASecond + 1 * KSrtMicroSecondsInAMilliSecond,
356 1 * KSrtMicroSecondsInAMinute + 6 * KSrtMicroSecondsInASecond + 20 * KSrtMicroSecondsInAMilliSecond,
357 1 * KSrtMicroSecondsInAnHour + 1 * KSrtMicroSecondsInAMinute + 20 * KSrtMicroSecondsInASecond + 1 * KSrtMicroSecondsInAMilliSecond,
358 2 * KSrtMicroSecondsInAnHour
361 TInt expectedNumOfFrames[KSrtSetPosTestCount] =
371 // check if the required Font is available before do the bitmap file comparison
372 TInt requiredFontAvailable = IsFontAvailableL(KSrtTargetTypefaceName);
373 if (!requiredFontAvailable)
375 #ifdef MMF_SUBTITLE_SUPPORT_TEST_SAVEDATA
376 INFO_PRINTF1(_L("Required font is not available, cannot save bitmap data. "));
377 User::Leave(KErrGeneral);
379 INFO_PRINTF1(_L("Required font is not available, no bitmap comparison. "));
380 #endif //MMF_SUBTITLE_SUPPORT_TEST_SAVEDATA
383 for (TInt i = 0; i < KSrtSetPosTestCount; i++)
386 TBool compareBitmap = (0 == i) && requiredFontAvailable;
388 TBool compareBitmap = EFalse;
391 TestGetNextFrameByPositionsL(videoPos[i], expectedNumOfFrames[i], compareBitmap);
393 // verify the bitmaps
396 #ifndef MMF_SUBTITLE_SUPPORT_TEST_SAVEDATA
397 TBool compResult = CompareBmpFilesL(KSrtNumOfFrames - expectedNumOfFrames[i], KSrtNumOfFrames - 1, KSubtitleCurrentBitmapFilePathSpec, KSubtitleTargetBitmapFilePathSpec);
399 DeleteTempFiles(KSrtNumOfFrames - expectedNumOfFrames[i], KSrtNumOfFrames - 1, KSubtitleCurrentBitmapFilePathSpec);
403 INFO_PRINTF1(_L("At least one bitmap file does not match the expected one. "));
404 User::Leave(KErrGeneral);
406 #endif //MMF_SUBTITLE_SUPPORT_TEST_SAVEDATA
411 void RTestSrtDecoderStep0002::TestGetNextFrameByPositionsL(const TTimeIntervalMicroSeconds& aPosition, TInt aExpectedNumOfFrames, TBool aSaveBitmap)
414 TTimeIntervalMicroSeconds displayTime = 0;
415 TTimeIntervalMicroSeconds displayDuration = 0;
416 TInt64 tDisplayTime = 0;
417 TInt64 tDisplayDuration = 0;
418 TSize bmpSize(600, 300);
419 TInt numOfFrames = 0;
421 TBuf <KMaxFullName> bitmapFilename;
423 CFbsBitmap* sampleBitmap = new (ELeave) CFbsBitmap();
424 CleanupStack::PushL(sampleBitmap);
426 User::LeaveIfError(sampleBitmap->Create(bmpSize, EColor16MA));
428 iSrtDecoder->SetVideoPosition(aPosition);
429 iSrtDecoder->Start();
431 while (KErrNone == err)
433 TRAP(err, iSrtDecoder->GetNextFrameL(*sampleBitmap, dirtyRegion, displayTime, displayDuration));
437 DrawBitmap(*sampleBitmap);
439 // save bitmap for the possible comparison
442 bitmapFilename.Format(KSubtitleCurrentBitmapFilePathSpec, numOfFrames);
443 User::LeaveIfError(sampleBitmap->Save(bitmapFilename));
446 if ((dirtyRegion.iTl.iX >= dirtyRegion.iBr.iX) ||
447 (dirtyRegion.iTl.iY >= dirtyRegion.iBr.iY))
449 INFO_PRINTF2(_L("Invalid dirty region received. (frame %d)"), numOfFrames);
450 User::Leave(KErrGeneral);
453 tDisplayTime = displayTime.Int64()/1000;
454 tDisplayDuration = displayDuration.Int64()/1000;
455 INFO_PRINTF4(_L("Frame[%d]: displayTime: %dms, displayDuration: %dms."),
457 I64LOW(tDisplayTime),
458 I64LOW(tDisplayDuration));
464 CleanupStack::PopAndDestroy(sampleBitmap);
473 if (aExpectedNumOfFrames != numOfFrames)
475 INFO_PRINTF2(_L("The number of frame (%d) is unexpected. "), numOfFrames);
476 User::Leave(KErrGeneral);
481 // Implementation of RTestSrtDecoderStep0101
483 RTestSrtDecoderStep0101::RTestSrtDecoderStep0101()
485 iTestStepName = _L("MM-MMF-SUBTITLE-SRTDECODER-U-0101-HP");
488 TVerdict RTestSrtDecoderStep0101::DoTestStepPreambleL()
490 InitializeTestStepL(EFalse, KSampleSubtitleSRTFilepath2);
495 TVerdict RTestSrtDecoderStep0101::DoTestStepPostambleL()
497 UnInitializeTestStep();
502 TVerdict RTestSrtDecoderStep0101::DoTestStepL()
504 INFO_PRINTF1(_L("Enter DoTestStepL"));
505 TVerdict result = EPass;
507 TRAPD(err, TestGetNextFrameL());
511 ERR_PRINTF2(_L("Error - RTestSrtDecoderStep0101::TestGetNextFrameL failed. error code %d. "), err);
512 INFO_PRINTF1(_L("Exit CSrtDecoder"));
516 INFO_PRINTF1(_L("Exit DoTestStepL"));
520 void RTestSrtDecoderStep0101::TestGetNextFrameL()
522 TInt numOfValidFrame = 0;
523 const TInt KSrtCase0101ExpectedValidFrame = 6;
525 TTimeIntervalMicroSeconds displayTime = 0;
526 TTimeIntervalMicroSeconds displayDuration = 0;
527 TSize bmpSize(320, 120);
529 CFbsBitmap* sampleBitmap = new (ELeave) CFbsBitmap();
530 CleanupStack::PushL(sampleBitmap);
532 TInt err = sampleBitmap->Create(
536 User::LeaveIfError(err);
538 iSrtDecoder->SetVideoPosition(0);
539 iSrtDecoder->Start();
541 while (KErrNone == err)
543 TRAP(err, iSrtDecoder->GetNextFrameL(*sampleBitmap, dirtyRegion, displayTime, displayDuration));
548 else if (KErrArgument == err)
554 CleanupStack::PopAndDestroy(sampleBitmap);
563 if (KSrtCase0101ExpectedValidFrame != numOfValidFrame)
565 INFO_PRINTF2(_L("The number of valid frame (%d) is unexpected. "), numOfValidFrame);
566 User::Leave(KErrGeneral);
570 // Implementation of RTestSrtDecoderStep0103
572 RTestSrtDecoderStep0103::RTestSrtDecoderStep0103()
574 iTestStepName = _L("MM-MMF-SUBTITLE-SRTDECODER-U-0103-HP");
577 TVerdict RTestSrtDecoderStep0103::DoTestStepPreambleL()
579 User::LeaveIfError(RFbsSession::Connect());
584 TVerdict RTestSrtDecoderStep0103::DoTestStepPostambleL()
586 RFbsSession::Disconnect();
591 TVerdict RTestSrtDecoderStep0103::DoTestStepL()
593 INFO_PRINTF1(_L("Enter DoTestStepL"));
594 TVerdict result = EFail;
595 TSize bmpSize(120, 320);
597 CFbsBitmap* sampleBitmap = new (ELeave) CFbsBitmap();
598 CleanupStack::PushL(sampleBitmap);
599 User::LeaveIfError(sampleBitmap->Create(bmpSize, EColor16MA));
601 for (TInt failRate = 1; ; ++failRate)
603 __UHEAP_SETFAIL(RHeap::EFailNext, failRate);
606 TRAPD(error, TestGetNextFrameL(*sampleBitmap));
607 RDebug::Printf("Ending iteration %d. Result = %d. Failures = %d", failRate, error, __UHEAP_CHECKFAILURE);
611 if ((error != KErrNone) && (error != KErrNoMemory))
613 ERR_PRINTF3(_L("RTestSrtDecoderStep0103: TESTS FAILED TO COMPLETE (failRate=%i) error code: %d\n"), failRate, error);
617 TAny* const pointer = User::Alloc(1);
628 CleanupStack::PopAndDestroy(sampleBitmap);
630 INFO_PRINTF1(_L("Exit DoTestStepL"));
634 void RTestSrtDecoderStep0103::TestGetNextFrameL(CFbsBitmap& aBitmap)
637 CSrtReader *srtReader = CSrtReader::NewL(KSampleSubtitleSRTFilepath1);
638 CleanupStack::PushL(srtReader);
640 CSrtSubtitleDecoder *srtDecoder = CSrtSubtitleDecoder::NewL(*srtReader);
641 CleanupStack::PushL(srtDecoder);
644 TTimeIntervalMicroSeconds displayTime = 0;
645 TTimeIntervalMicroSeconds displayDuration = 0;
647 srtDecoder->SetVideoPosition(0);
650 while (KErrNone == err)
652 TRAP(err, srtDecoder->GetNextFrameL(aBitmap, dirtyRegion, displayTime, displayDuration));
657 User::LeaveIfError(err);
662 CleanupStack::PopAndDestroy(srtDecoder);
663 CleanupStack::PopAndDestroy(srtReader);
666 RTestSrtDecoderStep0105::RTestSrtDecoderStep0105()
668 iTestStepName = _L("MM-MMF-SUBTITLE-SRTDECODER-U-0105-HP");
671 TVerdict RTestSrtDecoderStep0105::DoTestStepL()
673 iSrtDecoder->Start();
674 iSrtDecoder->Start();
676 ERR_PRINTF1(_L("Panic expected, so failing"));
680 TVerdict RTestSrtDecoderStep0105::DoTestStepPreambleL()
682 InitializeTestStepL(EFalse, KSampleSubtitleSRTFilepath1);
686 TVerdict RTestSrtDecoderStep0105::DoTestStepPostambleL()
688 UnInitializeTestStep();