First public contribution.
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.
19 @internalComponent - Internal Symbian test code
23 #include "fbsmessage.h"
25 CTFbsBase::CTFbsBase(CTestStep* aStep, TBool aRunWithLowMemory):
26 CTGraphicsBase(aStep),
28 iLastTestCase(EFalse),
29 iRunWithLowMemory(aRunWithLowMemory)
33 CTFbsBase::~CTFbsBase()
37 /** Run the passed the test case. Classes that inherit from CTFbsBase should override this
38 and choose which test case to run depending on the passed test case number. Test cases are
39 run once normally, 1..n times with out of memory (OOM) testing switched on for the FBServ
40 heap and 1..n times with OOM testing switched on for the current heap.
42 @param aCurTestCase The number of the test case to run
44 void CTFbsBase::RunTestCaseL(TInt aCurTestCase)
52 INFO_PRINTF2(_L("***** Starting test case %i *****"), aCurTestCase);
54 // Run the test normally first
55 iCurrentRunIsLowMemory = EFalse;
56 TRAPD(err, RunFbsTestL(aCurTestCase));
59 iTestStep.SetTestStepResult(EFail);
65 iRunWithLowMemory = EFalse;
66 INFO_PRINTF1(_L("WARNING: Can't run out of memory tests under a release build. OOM tests set to run in ini file so turning OOM tests off."));
70 // Run the test in out of memory conditions, checking both the FBServ heap and the
71 // current thread's heap for memory leaks
74 iCurrentRunIsLowMemory = ETrue;
75 RFbsSession* fbsSession = RFbsSession::GetSession();
78 INFO_PRINTF2(_L("***** Running Out Of Memory Tests on test case %i (FBSERV heap) *****"), aCurTestCase);
80 // Save the current state of test step results
81 TVerdict currentTestStepResult = iTestStep.TestStepResult();
83 // Create 1000 pixel wide bitmap to prevent allocation of scanline buffer
84 // during testings, to allow for memory leak testing
85 const TSize KSizeInPixels = TSize(1000,1);
86 const TDisplayMode KDisplayMode = EColor64K;
87 CFbsBitmap* bmp = new(ELeave) CFbsBitmap;
88 CleanupStack::PushL(bmp);
89 User::LeaveIfError(bmp->Create(KSizeInPixels, KDisplayMode));
90 CleanupStack::PopAndDestroy(bmp);
92 // Run the test with heap checking for the FbServ heap
93 for(TInt failAfter=1; failAfter < 1000; ++failAfter)
95 INFO_PRINTF2(_L("***** Set fail after %i allocs (FBSERV heap) *****"), failAfter);
97 // Count cells so we can know if any leaked
98 TInt cellsStart = fbsSession->SendCommand(EFbsMessHeapCount, RFbsSession::EHeapFailTypeHeapMemory);
99 fbsSession->SendCommand(EFbsMessSetHeapFail, RFbsSession::EHeapFailTypeHeapMemory, failAfter);
101 // Run test case (implemented by sub class)
102 TRAPD(err, RunFbsTestL(aCurTestCase));
104 fbsSession->SendCommand(EFbsMessSetHeapReset, RFbsSession::EHeapFailTypeHeapMemory);
105 TInt cellsEnd = fbsSession->SendCommand(EFbsMessHeapCount, RFbsSession::EHeapFailTypeHeapMemory);
106 if(cellsStart < cellsEnd)
109 TInt leakedCells = cellsEnd - cellsStart;
110 ERR_PRINTF3(_L("***** On loop number %i we leaked %i cells (FBSERV heap) *****"), failAfter, leakedCells);
111 currentTestStepResult = EFail;
114 // Check to see if any failures reported within test case run
117 INFO_PRINTF3(_L("***** Test case %i completed successfully after %d iterations (FBSERV heap) *****"), aCurTestCase, failAfter);
122 // Run the test with heap checking for the current thread's heap
124 INFO_PRINTF2(_L("***** Running Out Of Memory Tests on test case %i, current thread's heap (current heap) *****"), aCurTestCase);
130 // count cells so we can know how many we leaked
131 TInt cellsStart = User::CountAllocCells();
133 INFO_PRINTF2(_L("***** Set fail after %d allocs (current heap) *****"), tryCount);
135 __UHEAP_FAILNEXT(tryCount);
138 TRAP(err, RunFbsTestL(aCurTestCase));
140 TBool finishedCorrectly = EFalse;
141 if ((err == KErrNone))
143 // claims to have finished correctly, and we're not failing every alloc
144 finishedCorrectly = iStep->CheckForHeapFailNext();
147 TInt cellsEnd = User::CountAllocCells();
148 if (cellsStart < cellsEnd)
151 TInt leakedCells = cellsEnd - cellsStart;
152 ERR_PRINTF3(_L("***** On loop number %d we leaked %d cells (current heap). About to cause panic."),tryCount,leakedCells);
156 // check to see if we finished all OOM testing successfully
157 if ((err == KErrNone) && finishedCorrectly)
159 INFO_PRINTF3(_L("***** Test case %i completed successfully after %d iterations (current heap) *****"), aCurTestCase, tryCount);
165 ERR_PRINTF1(_L("***** OOM testing stopped after 999 iterations (current heap)"));
170 // Restore test step result and ignore any test failures the out of memory tests produce
171 iTestStep.SetTestStepResult(currentTestStepResult);
175 /** Helper method for extracting a TRgb colour from the passed buffer given a pixel
176 offset in to the buffer and a display mode.
178 @param aBuffer A buffer to extract the colour from.
179 @param aPixelOffset A pixel offset to use in to the buffer.
180 @param aDispMode The display mode to use when converting the pixel colour to TRgb.
182 TRgb CTFbsBase::ExtractRgb(TUint8* aBuffer, TInt aPixelOffset, TDisplayMode aDispMode)
188 TUint8 byte = *(aBuffer + (aPixelOffset >> 3));
189 if (byte & (1 << (aPixelOffset & 7)))
195 TUint8 byte = *(aBuffer + (aPixelOffset >> 2));
196 byte >>= ((aPixelOffset & 3) << 1);
197 return TRgb::Gray4(byte & 3);
201 TUint8 byte = *(aBuffer + (aPixelOffset >> 1));
202 if (aPixelOffset & 1)
204 return TRgb::Gray16(byte & 0xf);
207 return TRgb::Gray256(*(aBuffer + aPixelOffset));
210 TUint8 byte = *(aBuffer + (aPixelOffset >> 1));
211 if (aPixelOffset & 1)
213 return TRgb::Color16(byte & 0xf);
216 return TRgb::Color256(*(aBuffer + aPixelOffset));
219 TUint16 doubleByte = *(((TUint16*)aBuffer) + aPixelOffset);
220 return TRgb::Color4K(doubleByte & 0xfff);
224 TUint16 doubleByte = *(((TUint16*)aBuffer) + aPixelOffset);
225 return TRgb::Color64K(doubleByte);
229 aBuffer += aPixelOffset * 3;
230 TInt value = *aBuffer++;
231 value |= *aBuffer++ << 8;
232 value |= *aBuffer << 16;
233 return TRgb::Color16M(value);
236 return *(((TRgb*)aBuffer) + aPixelOffset);
239 return TRgb::Color16MU(*(((TUint32*)aBuffer) + aPixelOffset));
243 return TRgb::Color16MA(*(((TUint32*)aBuffer) + aPixelOffset));
247 return TRgb::_Color16MAP(*(((TUint32*)aBuffer) + aPixelOffset));
255 /** Function to be used by classes that inherit from CTFbsBase. SetLastTestCase()
256 should be called after the last test case for a class is called to signal that
257 testing has finished for that class.
259 void CTFbsBase::SetLastTestCase()
261 iLastTestCase = ETrue;