os/kernelhwsrv/kerneltest/e32test/window/t_keys.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     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 the License "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 // e32test\window\t_keys.cpp
    15 // 
    16 //
    17 
    18 #include <e32test.h>
    19 #include <e32hal.h>
    20 #include <e32twin.h>
    21 #include <e32svr.h>
    22 
    23 const TBool KEY_UP=ETrue;
    24 const TBool KEY_DOWN=EFalse;
    25 const TBool EXPECT_NO_KEY_PRESS=EFalse;
    26 const TBool EXPECT_KEY_PRESS=ETrue;
    27 
    28 LOCAL_D RTest test(_L("T_KEYS"));
    29 LOCAL_D CKeyTranslator *KeyTranslator;
    30 LOCAL_D CCaptureKeys *CaptureKeys;
    31 
    32 LOCAL_C TBool testType()
    33 //
    34 // Determine whether the Func key sets Alt or Func modifier
    35 //
    36     {
    37 	TKeyData keyData;
    38     TBool ret;
    39     KeyTranslator->TranslateKey(EStdKeyLeftFunc,EFalse,*CaptureKeys,keyData);
    40     ret=(keyData.iModifiers==(EModifierLeftFunc|EModifierFunc));
    41     KeyTranslator->TranslateKey(EStdKeyLeftFunc,ETrue,*CaptureKeys,keyData);
    42     return(ret);
    43     }
    44 
    45 LOCAL_C void testConv(const TDesC& aDes,TBool aKeyup,TBool aRet,TUint aScanCode,TUint aKeyCode,TInt aModifiers)
    46 	{
    47 	TKeyData keyData;
    48     TBool ret=KeyTranslator->TranslateKey(aScanCode, aKeyup,*CaptureKeys,keyData);
    49 	test.Next(aDes);
    50 	test(ret==aRet);
    51 	test((keyData.iKeyCode==aKeyCode));
    52 	test((keyData.iModifiers==aModifiers));
    53 	}
    54 
    55 LOCAL_C void testCtrl(TInt aKeyCode)
    56 //
    57 // Test that prssing Ctrl-xyz returns ascii code xyz
    58 //
    59     {
    60 
    61     TBuf<16> down;
    62     TBuf<16> up;
    63 
    64     TInt val[4];
    65     TInt ii=0;
    66 	TInt key=aKeyCode;
    67 
    68     while(key)
    69         {
    70         val[ii++]=key%10;
    71         key/=10;
    72         }
    73 
    74 	testConv(_L("Left control down"),KEY_DOWN,EXPECT_NO_KEY_PRESS,EStdKeyLeftCtrl,0,EModifierCtrl|EModifierLeftCtrl);
    75 
    76 	TUint downModifiers=EModifierCtrl|EModifierLeftCtrl|EModifierPureKeycode;
    77 	TUint upModifiers=EModifierCtrl|EModifierLeftCtrl|EModifierKeyUp;
    78 
    79     while(ii--)
    80         {
    81         down.Format(_L("%d down"),val[ii]);
    82         up.Format(_L("%d up"),val[ii]);
    83     	testConv(down,KEY_DOWN,EXPECT_NO_KEY_PRESS,val[ii]+'0',0,downModifiers);
    84     	testConv(up,KEY_UP,EXPECT_NO_KEY_PRESS,val[ii]+'0',0,upModifiers);
    85 		downModifiers&=~(EModifierPureKeycode);
    86         }
    87 
    88 	testConv(_L("Left control up"),KEY_UP,EXPECT_KEY_PRESS,EStdKeyLeftCtrl,aKeyCode,EModifierKeyUp|EModifierSpecial);
    89     }
    90 
    91 /*
    92 LOCAL_C void testAccents(TInt aNum,TInt aLetter,TInt aKeycode)
    93 //
    94 // Check accents by pressing Ctrl-n followed by a letter
    95 //
    96 	{
    97 
    98 	TBuf<16> down;
    99 	TBuf<16> up;
   100 //
   101 // Press the Ctrl key
   102 //
   103 	testConv(_L("Left control down"),KEY_DOWN,EXPECT_NO_KEY_PRESS,EStdKeyLeftCtrl,0,EModifierCtrl|EModifierLeftCtrl);
   104 //
   105 // While it's down, press the given number key...
   106 //
   107     down.Format(_L("%c down"),aNum);
   108    	testConv(down,KEY_DOWN,EXPECT_NO_KEY_PRESS,aNum,0,EModifierCtrl|EModifierLeftCtrl|EModifierPureKeycode);
   109 //
   110 // Release the number and Ctrl keys
   111 //
   112     up.Format(_L("%c up"),aNum);
   113    	testConv(up,KEY_UP,EXPECT_NO_KEY_PRESS,aNum,0,EModifierCtrl|EModifierLeftCtrl|EModifierKeyUp);
   114 	testConv(_L("Left control up"),KEY_UP,EXPECT_NO_KEY_PRESS,EStdKeyLeftCtrl,0,EModifierKeyUp);
   115 //
   116 // Press the letter key and check an accent is returned...
   117 //
   118     down.Format(_L("%c down"),aLetter);
   119     up.Format(_L("%c up"),aLetter);
   120    	testConv(down,KEY_DOWN,EXPECT_KEY_PRESS,aLetter,aKeycode,EModifierSpecial);
   121    	testConv(up,KEY_UP,EXPECT_NO_KEY_PRESS,aLetter,0,EModifierKeyUp);
   122 	}
   123 */
   124 
   125 GLDEF_C TInt E32Main()
   126 //
   127 // Test the keyboard translator
   128 //
   129     {
   130 
   131     test.Start(_L("Keyboard translator tests"));
   132 
   133     KeyTranslator=CKeyTranslator::New();
   134     CaptureKeys=new CCaptureKeys();
   135 
   136 	CaptureKeys->Construct();
   137 
   138     TUint scancode=EStdKeyLeftArrow;
   139 //
   140 // Test that the special keys pass through and anything after
   141 // or before raises an error
   142 //
   143 	testConv(_L("First Special key down"),KEY_DOWN,EXPECT_KEY_PRESS,ESpecialKeyBase,ESpecialKeyBase,0);
   144 	testConv(_L("First Special key up"),KEY_UP,EXPECT_NO_KEY_PRESS,ESpecialKeyBase,0,EModifierKeyUp);
   145 
   146 	testConv(_L("Last Sepcial key down"),KEY_DOWN,EXPECT_KEY_PRESS,ESpecialKeyBase+ESpecialKeyCount-1,ESpecialKeyBase+ESpecialKeyCount-1,0);
   147 	testConv(_L("Last Special key up"),KEY_UP,EXPECT_NO_KEY_PRESS,ESpecialKeyBase+ESpecialKeyCount-1,0,EModifierKeyUp);
   148 
   149 	testConv(_L("Key before Special range down"),KEY_DOWN,EXPECT_NO_KEY_PRESS,ESpecialKeyBase-1,0,0);
   150 	testConv(_L("Key before Special range up"),KEY_UP,EXPECT_NO_KEY_PRESS,0,0,EModifierKeyUp);
   151 
   152 	testConv(_L("Key after Special range down"),KEY_DOWN,EXPECT_NO_KEY_PRESS,ESpecialKeyBase+ESpecialKeyCount,0,0);
   153 	testConv(_L("Key after Sepcial range up"),KEY_UP,EXPECT_NO_KEY_PRESS,ESpecialKeyBase+ESpecialKeyCount,0,EModifierKeyUp);
   154 //
   155 // Test a range of keys...
   156 //
   157 	for(scancode='A';scancode<='Z';scancode++)
   158 		{
   159 		TBuf<16> buf;
   160 
   161 		buf.Format(_L("Testing key %c"),scancode);
   162 		testConv(buf,KEY_DOWN,EXPECT_KEY_PRESS,scancode,'a'+scancode-'A',EModifierAutorepeatable);
   163 		testConv(buf,KEY_UP,EXPECT_NO_KEY_PRESS,scancode,0,EModifierKeyUp);
   164 		}
   165 //
   166 // Test shift and control modifiers
   167 //
   168 	testConv(_L("Left shift down"),KEY_DOWN,EXPECT_NO_KEY_PRESS,EStdKeyLeftShift,0,EModifierShift|EModifierLeftShift);
   169 	testConv(_L("Left shift up"),KEY_UP,EXPECT_NO_KEY_PRESS,EStdKeyLeftShift,0,EModifierKeyUp);
   170 
   171 	testConv(_L("Right shift down"),KEY_DOWN,EXPECT_NO_KEY_PRESS,EStdKeyRightShift,0,EModifierShift|EModifierRightShift);
   172 	testConv(_L("Right shift up"),KEY_UP,EXPECT_NO_KEY_PRESS,EStdKeyRightShift,0,EModifierKeyUp);
   173 
   174 	testConv(_L("Left shift down"),KEY_DOWN,EXPECT_NO_KEY_PRESS,EStdKeyLeftShift,0,EModifierShift|EModifierLeftShift);
   175 	testConv(_L("Right shift down"),KEY_DOWN,EXPECT_NO_KEY_PRESS,EStdKeyRightShift,0,EModifierShift|EModifierRightShift|EModifierLeftShift);
   176 	testConv(_L("Left shift up"),KEY_UP,EXPECT_NO_KEY_PRESS,EStdKeyLeftShift,0,EModifierKeyUp|EModifierShift|EModifierRightShift);
   177 	testConv(_L("Right shift up"),KEY_UP,EXPECT_NO_KEY_PRESS,EStdKeyRightShift,0,EModifierKeyUp);
   178 
   179 	testConv(_L("Left control down"),KEY_DOWN,EXPECT_NO_KEY_PRESS,EStdKeyLeftCtrl,0,EModifierCtrl|EModifierLeftCtrl);
   180 	testConv(_L("Left control up"),KEY_UP,EXPECT_NO_KEY_PRESS,EStdKeyLeftCtrl,0,EModifierKeyUp|EModifierPureKeycode);
   181 
   182 	testConv(_L("Right control down"),KEY_DOWN,EXPECT_NO_KEY_PRESS,EStdKeyRightCtrl,0,EModifierCtrl|EModifierRightCtrl);
   183 	testConv(_L("Right control up"),KEY_UP,EXPECT_NO_KEY_PRESS,EStdKeyRightCtrl,0,EModifierKeyUp|EModifierPureKeycode);
   184 
   185 	testConv(_L("Left control down"),KEY_DOWN,EXPECT_NO_KEY_PRESS,EStdKeyLeftCtrl,0,EModifierCtrl|EModifierLeftCtrl);
   186 	testConv(_L("Right control down"),KEY_DOWN,EXPECT_NO_KEY_PRESS,EStdKeyRightCtrl,0,EModifierCtrl|EModifierRightCtrl|EModifierLeftCtrl);
   187 	testConv(_L("Left control up"),KEY_UP,EXPECT_NO_KEY_PRESS,EStdKeyLeftCtrl,0,EModifierKeyUp|EModifierCtrl|EModifierRightCtrl|EModifierPureKeycode);
   188 	testConv(_L("Right control up"),KEY_UP,EXPECT_NO_KEY_PRESS,EStdKeyRightCtrl,0,EModifierKeyUp|EModifierPureKeycode);
   189 //
   190 // Test function and alt keys - these are swapped on some platforms
   191 //
   192     if(testType())
   193         {
   194     	testConv(_L("Left func down"),KEY_DOWN,EXPECT_NO_KEY_PRESS,EStdKeyLeftFunc,0,EModifierFunc|EModifierLeftFunc);
   195     	testConv(_L("Left func up"),KEY_UP,EXPECT_NO_KEY_PRESS,EStdKeyLeftFunc,0,EModifierKeyUp);
   196 
   197     	testConv(_L("Right func down"),KEY_DOWN,EXPECT_NO_KEY_PRESS,EStdKeyRightFunc,0,EModifierFunc|EModifierRightFunc);
   198     	testConv(_L("Right func up"),KEY_UP,EXPECT_NO_KEY_PRESS,EStdKeyRightFunc,0,EModifierKeyUp);
   199 
   200     	testConv(_L("Left func down"),KEY_DOWN,EXPECT_NO_KEY_PRESS,EStdKeyLeftFunc,0,EModifierFunc|EModifierLeftFunc);
   201     	testConv(_L("Right func down"),KEY_DOWN,EXPECT_NO_KEY_PRESS,EStdKeyRightFunc,0,EModifierFunc|EModifierRightFunc|EModifierLeftFunc);
   202     	testConv(_L("Left func up"),KEY_UP,EXPECT_NO_KEY_PRESS,EStdKeyLeftFunc,0,EModifierKeyUp|EModifierFunc|EModifierRightFunc);
   203     	testConv(_L("Right func up"),KEY_UP,EXPECT_NO_KEY_PRESS,EStdKeyRightFunc,0,EModifierKeyUp);
   204 
   205     	testConv(_L("Left alt down"),KEY_DOWN,EXPECT_NO_KEY_PRESS,EStdKeyLeftAlt,0,EModifierAlt|EModifierLeftAlt);
   206     	testConv(_L("Left alt up"),KEY_UP,EXPECT_NO_KEY_PRESS,EStdKeyLeftAlt,0,EModifierKeyUp);
   207 
   208     	testConv(_L("Right alt down"),KEY_DOWN,EXPECT_NO_KEY_PRESS,EStdKeyRightAlt,0,EModifierAlt|EModifierRightAlt);
   209     	testConv(_L("Right alt up"),KEY_UP,EXPECT_NO_KEY_PRESS,EStdKeyRightAlt,0,EModifierKeyUp);
   210 
   211     	testConv(_L("Left alt down"),KEY_DOWN,EXPECT_NO_KEY_PRESS,EStdKeyLeftAlt,0,EModifierAlt|EModifierLeftAlt);
   212     	testConv(_L("Right alt down"),KEY_DOWN,EXPECT_NO_KEY_PRESS,EStdKeyRightAlt,0,EModifierAlt|EModifierRightAlt|EModifierLeftAlt);
   213     	testConv(_L("Left alt up"),KEY_UP,EXPECT_NO_KEY_PRESS,EStdKeyLeftAlt,0,EModifierKeyUp|EModifierAlt|EModifierRightAlt);
   214     	testConv(_L("Right alt up"),KEY_UP,EXPECT_NO_KEY_PRESS,EStdKeyRightAlt,0,EModifierKeyUp);
   215         }
   216     else
   217         {
   218     	testConv(_L("Left func down"),KEY_DOWN,EXPECT_NO_KEY_PRESS,EStdKeyLeftAlt,0,EModifierFunc|EModifierLeftFunc);
   219     	testConv(_L("Left func up"),KEY_UP,EXPECT_NO_KEY_PRESS,EStdKeyLeftAlt,0,EModifierKeyUp);
   220 
   221     	testConv(_L("Right func down"),KEY_DOWN,EXPECT_NO_KEY_PRESS,EStdKeyRightAlt,0,EModifierFunc|EModifierRightFunc);
   222     	testConv(_L("Right func up"),KEY_UP,EXPECT_NO_KEY_PRESS,EStdKeyRightAlt,0,EModifierKeyUp);
   223 
   224     	testConv(_L("Left func down"),KEY_DOWN,EXPECT_NO_KEY_PRESS,EStdKeyLeftAlt,0,EModifierFunc|EModifierLeftFunc);
   225     	testConv(_L("Right func down"),KEY_DOWN,EXPECT_NO_KEY_PRESS,EStdKeyRightAlt,0,EModifierFunc|EModifierRightFunc|EModifierLeftFunc);
   226     	testConv(_L("Left func up"),KEY_UP,EXPECT_NO_KEY_PRESS,EStdKeyLeftAlt,0,EModifierKeyUp|EModifierFunc|EModifierRightFunc);
   227 	    testConv(_L("Right func up"),KEY_UP,EXPECT_NO_KEY_PRESS,EStdKeyRightAlt,0,EModifierKeyUp);
   228 
   229     	testConv(_L("Left alt down"),KEY_DOWN,EXPECT_NO_KEY_PRESS,EStdKeyLeftFunc,0,EModifierAlt|EModifierLeftAlt);
   230     	testConv(_L("Left alt up"),KEY_UP,EXPECT_NO_KEY_PRESS,EStdKeyLeftFunc,0,EModifierKeyUp);
   231 
   232     	testConv(_L("Right alt down"),KEY_DOWN,EXPECT_NO_KEY_PRESS,EStdKeyRightFunc,0,EModifierAlt|EModifierRightAlt);
   233     	testConv(_L("Right alt up"),KEY_UP,EXPECT_NO_KEY_PRESS,EStdKeyRightFunc,0,EModifierKeyUp);
   234 
   235     	testConv(_L("Left alt down"),KEY_DOWN,EXPECT_NO_KEY_PRESS,EStdKeyLeftFunc,0,EModifierAlt|EModifierLeftAlt);
   236     	testConv(_L("Right alt down"),KEY_DOWN,EXPECT_NO_KEY_PRESS,EStdKeyRightFunc,0,EModifierAlt|EModifierRightAlt|EModifierLeftAlt);
   237     	testConv(_L("Left alt up"),KEY_UP,EXPECT_NO_KEY_PRESS,EStdKeyLeftFunc,0,EModifierKeyUp|EModifierAlt|EModifierRightAlt);
   238     	testConv(_L("Right alt up"),KEY_UP,EXPECT_NO_KEY_PRESS,EStdKeyRightFunc,0,EModifierKeyUp);
   239         }
   240 //
   241 // Test control entering of ascii codes
   242 //
   243     testCtrl(123);
   244     testCtrl(217);
   245     testCtrl(53);
   246     testCtrl(37);
   247 
   248 /* These accent tests are dependent upon the keyboard mapping being used. They need enhancing
   249    so that they adjust according to the platform on which they are being run
   250 
   251 //
   252 // Test that pressing Ctrl-1 followed by 'e' gives accents
   253 //
   254 	testAccents('1','E',0xe6);
   255 	testAccents('2','E',0xea);
   256 	testAccents('3','E',0xe8);
   257 	testAccents('4','E',0xe9);
   258 	testAccents('5','A',0xe3);
   259 	testAccents('6','E',0xea);
   260 
   261 */
   262 	test.End();
   263 	return(KErrNone);
   264     }
   265