windowing/windowserver/tman/TKREPEAT.CPP
changeset 0 5d03bc08d59c
equal deleted inserted replaced
-1:000000000000 0:5d03bc08d59c
       
     1 // Copyright (c) 1996-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 // Keyboard repeat test
       
    15 // 
       
    16 //
       
    17 
       
    18 #include <e32std.h>
       
    19 #include <e32svr.h>
       
    20 #include "W32STD.H"
       
    21 #include "../tlib/testbase.h"
       
    22 #include "TMAN.H"
       
    23 
       
    24 class CRKWindow;
       
    25 
       
    26 class TKRepeatTest : public CTestBase
       
    27 	{
       
    28 public:
       
    29 	TKRepeatTest();
       
    30 	~TKRepeatTest();
       
    31 	TestState DoTestL();
       
    32 	void ConstructL();
       
    33 	void TestKeyboardRepeatRateL(const TTimeIntervalMicroSeconds32 &aInitialTime, const TTimeIntervalMicroSeconds32 &aTime);
       
    34 	TBool CheckReportL();
       
    35 public:
       
    36 	TBool iAbort;
       
    37 private:
       
    38 	TTimeIntervalMicroSeconds32 iOldInitialTime;
       
    39 	TTimeIntervalMicroSeconds32 iOldTime;
       
    40 	CRKWindow *iWin;
       
    41 	TSize iWinSize;
       
    42 	TInt iState;
       
    43 	};
       
    44 
       
    45 class CRKWindow : public CTWin
       
    46 	{
       
    47 	enum TRKStates {
       
    48 		EStateWaitingForKeyDown,
       
    49 		EStateWaitingForKeyCode,
       
    50 		EStateWaitingForFirstRepeat,
       
    51 		EStateWaitingForNthRepeat,
       
    52 		EStateWaitingForKeyUp,
       
    53 		EStateInactive,
       
    54 		EStateError,
       
    55 		};
       
    56 public:
       
    57 	CRKWindow(TKRepeatTest *aTest);
       
    58 	void SetUpL(TPoint pos,TSize size,CTWinBase *parent, CWindowGc &aGc);
       
    59 	void SetState(TRKStates aState);
       
    60 	void SetKeyboardRepeatRate(const TTimeIntervalMicroSeconds32 &aInitialTime, const TTimeIntervalMicroSeconds32 &aTime);
       
    61 	void WinKeyL(const TKeyEvent &,const TTime &);
       
    62 	void KeyUpL(const TKeyEvent &aKey,const TTime &aTime);
       
    63 	void KeyDownL(const TKeyEvent &aKey,const TTime &aTime);
       
    64 	void Draw();
       
    65 	TDesC& Report();
       
    66 	TBool CheckResults();
       
    67 protected:
       
    68 	TInt iConnIndex;
       
    69 	TKRepeatTest *iTest;
       
    70 	TRgb iBack;
       
    71 	TRKStates iState;
       
    72 	TInt iDownCode;
       
    73 	TInt iRepCount;
       
    74 	TTimeIntervalMicroSeconds32 iInitialRepeatSet;
       
    75 	TTimeIntervalMicroSeconds32 iRepeatSet;
       
    76 	TTime iPrevTime;
       
    77 	TTimeIntervalMicroSeconds32 iInitialGap;
       
    78 	TTimeIntervalMicroSeconds32 iTotalGap;
       
    79 	TTimeIntervalMicroSeconds32 iMinGap;
       
    80 	TTimeIntervalMicroSeconds32 iMaxGap;
       
    81 	TBuf<0x40> iReport;
       
    82 	};
       
    83 
       
    84 GLDEF_C CTestBase *CreateKRepeatTest()
       
    85 	{
       
    86 	return(new(ELeave) TKRepeatTest());
       
    87 	}
       
    88 
       
    89 //
       
    90 // CRKWindow, class //
       
    91 //
       
    92 
       
    93 CRKWindow::CRKWindow(TKRepeatTest *aTest) : CTWin(), iTest(aTest)
       
    94 	{
       
    95 	}
       
    96 
       
    97 void CRKWindow::SetUpL(TPoint pos,TSize size,CTWinBase *parent, CWindowGc &aGc)
       
    98 	{
       
    99 	ConstructExtLD(*parent,pos,size);
       
   100 	iWin.SetBackgroundColor(TRgb::Gray256(230));
       
   101 	Activate();
       
   102 	AssignGC(aGc);
       
   103 	}
       
   104 
       
   105 void CRKWindow::Draw()
       
   106 	{
       
   107 	iGc->Clear();
       
   108 	switch(iState)
       
   109 		{
       
   110 		case EStateWaitingForKeyDown:
       
   111 			iGc->DrawText(_L("Press and hold the space bar"), TPoint(10,20));
       
   112 			break;
       
   113 		case EStateWaitingForFirstRepeat:
       
   114 		case EStateWaitingForNthRepeat:
       
   115 			{
       
   116 			TBuf<0x40> buf;
       
   117 			buf.Format(TRefByValue<const TDesC>(_L("Keep space bar down (%d repeats so far)")),iRepCount);
       
   118 			iGc->DrawText(buf, TPoint(10,20));
       
   119 			}
       
   120 			break;
       
   121 		case EStateWaitingForKeyUp:
       
   122 			iGc->DrawText(_L("Release space bar"), TPoint(10,20));
       
   123 		default:
       
   124 			break;
       
   125 		}
       
   126 	}
       
   127 
       
   128 void CRKWindow::SetState(TRKStates aState)
       
   129 	{
       
   130 	iState=aState;
       
   131 	DrawNow();
       
   132 	}
       
   133 
       
   134 TBool CRKWindow::CheckResults()
       
   135 	{
       
   136 //
       
   137 // Checks repeat results, first convert everything to 10th's as that what is actually used 
       
   138 // for the timer in the window server.
       
   139 //
       
   140 // Return ETrue if the inacuracy in the average time is greater than 1/10th either way
       
   141 // Allow initial 2/10ths either
       
   142 // Allow min 2/10ths below
       
   143 // Allow max 2/10ths above
       
   144 //
       
   145 	if (iState!=EStateInactive)
       
   146 		return(ETrue);
       
   147 	TInt initial=iInitialGap.Int()/100000;
       
   148 	TInt initialX=iInitialRepeatSet.Int()/100000;
       
   149 	if (initialX==0)
       
   150 		initialX=1;
       
   151 	TInt average=(iTotalGap.Int()/100000)/(iRepCount-1);
       
   152 	TInt repeatX=iRepeatSet.Int()/100000;
       
   153 	if (repeatX==0)
       
   154 		repeatX=1;
       
   155 	TInt min=iMinGap.Int()/100000;
       
   156 	TInt max=iMaxGap.Int()/100000;
       
   157 	if (average>(repeatX+1) || average<(repeatX-1))
       
   158 		return(ETrue);
       
   159 	if (initial>(initialX+2) || initial<(initialX-2))
       
   160 		return(ETrue);
       
   161 	if (min>(repeatX+1) || min<(repeatX-2))
       
   162 		return(ETrue);
       
   163 	if (max>(repeatX+3) || max<repeatX)
       
   164 		return(ETrue);
       
   165 	return(EFalse);
       
   166 	}
       
   167 
       
   168 TDesC& CRKWindow::Report()
       
   169 	{
       
   170 	if (iState!=EStateInactive)
       
   171 		{
       
   172 		iReport.Format(_L("Error, test not completed"));
       
   173 		}
       
   174 	else
       
   175 		{
       
   176 		TInt initial=iInitialGap.Int()/10000;
       
   177 		TInt initialX=iInitialRepeatSet.Int()/10000;
       
   178 		TInt average=(iTotalGap.Int()/10000/(iRepCount-1));
       
   179 		TInt repeatX=iRepeatSet.Int()/10000;
       
   180 		TInt min=iMinGap.Int()/10000;
       
   181 		TInt max=iMaxGap.Int()/10000;
       
   182 		iReport.Format(TRefByValue<const TDesC>(_L("Initial=%d [%d], Av=%d [%d], Min=%d, Max=%d")),initial,initialX,average,repeatX,min,max);
       
   183 		}
       
   184 	return(iReport);
       
   185 	}
       
   186 
       
   187 void CRKWindow::KeyDownL(const TKeyEvent &aKey,const TTime &)
       
   188 	{
       
   189 	switch(iState)
       
   190 		{
       
   191 		case EStateWaitingForKeyDown:
       
   192 			SetState(EStateWaitingForKeyCode);
       
   193 			iDownCode=aKey.iScanCode;
       
   194 			break;
       
   195 		default:;
       
   196 		}
       
   197 	}
       
   198 
       
   199 void CRKWindow::KeyUpL(const TKeyEvent &aKey,const TTime &)
       
   200 	{
       
   201 	if (aKey.iScanCode==iDownCode)
       
   202 		{
       
   203 		switch(iState)
       
   204 			{
       
   205 			case EStateWaitingForKeyUp:
       
   206 				SetState(EStateInactive);
       
   207 				break;
       
   208 			default:
       
   209 				SetState(EStateError);
       
   210 				break;
       
   211 			}
       
   212 		CActiveScheduler::Stop();
       
   213 		}
       
   214 	}
       
   215 
       
   216 void CRKWindow::WinKeyL(const TKeyEvent &aKey,const TTime &aTime)
       
   217 	{
       
   218 	if (aKey.iCode==EKeyEscape)
       
   219 		{
       
   220 		CActiveScheduler::Stop();
       
   221 		iTest->iAbort=ETrue;
       
   222 		}
       
   223 	if (aKey.iCode==32)
       
   224 		{
       
   225 		switch(iState)
       
   226 			{
       
   227 			case EStateWaitingForKeyCode:
       
   228 				SetState(EStateWaitingForFirstRepeat);
       
   229 				iPrevTime=aTime;
       
   230 				break;
       
   231 			case EStateWaitingForFirstRepeat:
       
   232 				iRepCount=1;
       
   233 				iInitialGap = I64LOW(aTime.MicroSecondsFrom(iPrevTime).Int64());
       
   234 				SetState(EStateWaitingForNthRepeat);
       
   235 				break;
       
   236 			case EStateWaitingForNthRepeat:
       
   237 				if (iRepCount==5)
       
   238 					SetState(EStateWaitingForKeyUp);
       
   239 				else
       
   240 					{
       
   241 					TTimeIntervalMicroSeconds32 gap(I64LOW(aTime.MicroSecondsFrom(iPrevTime).Int64()));
       
   242 					if (gap<iMinGap)
       
   243 						iMinGap=gap;
       
   244 					if (gap>iMaxGap)
       
   245 						iMaxGap=gap;
       
   246 					iTotalGap=iTotalGap.Int()+gap.Int();	// Horrible way to do a +=
       
   247 					iRepCount++;
       
   248 					SetState(EStateWaitingForNthRepeat);
       
   249 					}
       
   250 			case EStateWaitingForKeyUp: 	// Do nothing here
       
   251 				break;
       
   252 			default:
       
   253 				iTest->TestL(EFalse);
       
   254 			}
       
   255 		iPrevTime=aTime;
       
   256 		}
       
   257 	}
       
   258 
       
   259 void CRKWindow::SetKeyboardRepeatRate(const TTimeIntervalMicroSeconds32 &aInitialTime, const TTimeIntervalMicroSeconds32 &aTime)
       
   260 	{
       
   261 	iInitialRepeatSet=aInitialTime;
       
   262 	iRepeatSet=aTime;
       
   263 	iMinGap=TTimeIntervalMicroSeconds32(100000000);	// Any very big number will do
       
   264 	iMaxGap=TTimeIntervalMicroSeconds32(0);
       
   265 	iTotalGap=TTimeIntervalMicroSeconds32(0);
       
   266 	SetState(EStateWaitingForKeyDown);
       
   267 	Client()->iWs.Flush();
       
   268 	}
       
   269 
       
   270 //
       
   271 
       
   272 TKRepeatTest::TKRepeatTest() : CTestBase(_L("KRepeat"))
       
   273 	{}
       
   274 
       
   275 TKRepeatTest::~TKRepeatTest()
       
   276 	{
       
   277 	CTWin::Delete(iWin);
       
   278 	Client()->iWs.SetKeyboardRepeatRate(iOldInitialTime, iOldTime);
       
   279 	}
       
   280 
       
   281 void TKRepeatTest::ConstructL()
       
   282 	{
       
   283 	iWin=new(ELeave) CRKWindow(this);
       
   284 	TSize screenSize=Client()->iGroup->Size();
       
   285 	iWin->SetUpL(TPoint(5,5),TSize(screenSize.iWidth/2,screenSize.iHeight-10),Client()->iGroup,*Client()->iGc);
       
   286 	Client()->iGroup->SetCurrentWindow(iWin);
       
   287 	Client()->iWs.GetKeyboardRepeatRate(iOldInitialTime, iOldTime);
       
   288 	}
       
   289 
       
   290 TInt TKRepeatTest::CheckReportL()
       
   291 	{
       
   292 	if (iWin->CheckResults())
       
   293 		{
       
   294 		CTDialog *dialog=new(ELeave) CTDialog();
       
   295 		dialog->SetTitle(_L("Keyboard repeat innacuracies"));
       
   296 		dialog->SetLine1(iWin->Report());
       
   297 		dialog->SetNumButtons(2);
       
   298 		dialog->SetButtonText(0,_L("Okay"));
       
   299 		dialog->SetButtonText(1,_L("Retest"));
       
   300 		dialog->SetButtonText(2,_L("Fail"));
       
   301 		dialog->ConstructLD(*Client()->iGroup,*Client()->iGc);
       
   302 		switch(dialog->Display())
       
   303 			{
       
   304 			case 0:
       
   305 				break;
       
   306 			case 1:
       
   307 				return(ETrue);	// Redo test
       
   308 			case 2:
       
   309 				TestL(EFalse);
       
   310 				break;
       
   311 			}
       
   312 		}
       
   313 	return(EFalse);
       
   314 	}
       
   315 
       
   316 void TKRepeatTest::TestKeyboardRepeatRateL(const TTimeIntervalMicroSeconds32 &aInitialTime, const TTimeIntervalMicroSeconds32 &aTime)
       
   317 	{
       
   318 	do
       
   319 		{
       
   320 		Client()->iWs.SetKeyboardRepeatRate(aInitialTime, aTime);
       
   321 		iWin->SetKeyboardRepeatRate(aInitialTime, aTime);
       
   322 		CActiveScheduler::Start();
       
   323 		if (iAbort)
       
   324 			AbortL();
       
   325 		} while(CheckReportL());
       
   326 	}
       
   327 
       
   328 TestState TKRepeatTest::DoTestL()
       
   329 	{
       
   330 	switch(iState)
       
   331 		{
       
   332 		case 0:
       
   333 			LogSubTest(_L("Keyboard Repeat"),1);
       
   334 			TestKeyboardRepeatRateL(TTimeIntervalMicroSeconds32(1000000), TTimeIntervalMicroSeconds32(500000));
       
   335 			LogSubTest(_L("Keyboard Repeat"),2);
       
   336 			TestKeyboardRepeatRateL(TTimeIntervalMicroSeconds32(200000), TTimeIntervalMicroSeconds32(100000));
       
   337 			LogSubTest(_L("Keyboard Repeat"),3);
       
   338 			TestKeyboardRepeatRateL(TTimeIntervalMicroSeconds32(0), TTimeIntervalMicroSeconds32(100000));
       
   339 			LogSubTest(_L("Keyboard Repeat"),4);
       
   340 			TestKeyboardRepeatRateL(TTimeIntervalMicroSeconds32(100000), TTimeIntervalMicroSeconds32(100000));
       
   341 			iState++;
       
   342 			break;
       
   343 		default:
       
   344 			return(EFinished);
       
   345 		}
       
   346 	return(ENext);
       
   347 	}
       
   348