Wednesday, 15 August 2012

c# - Unit Test for virtual Keyboard -



c# - Unit Test for virtual Keyboard -

one of projects developing keyboard hook traps of higher numbered function buttons (f13-f20). tablet work on has buttons on mapped higher function buttons. making class has constructor input key (from system.windows.forms.keys) , abstracttask. since utilize hook perform various tasks decided slick way of doing. 1 of tasks keyboardtask. super simple class (i hope atleast)

public class keyboardtask : abstracttask { private keyboardtask () { } public keyboardtask (keyboardcommand key) { options = "{" + key + "}"; } public override void performtask() { globals.writelog("keyboardtask:performtask()+"); seek { system.windows.forms.sendkeys.send(options); } grab (system.exception ex) { globals.writeexceptionlog(ex); } globals.writelog("keyboardtask:performtask()-"); } } public enum keyboardcommand { backspace,//{backspace}, {bs}, or {bksp} break,//{break} capslock,//{capslock} delete,//{delete} or {del} down, //{down} end,//{end} enter,//{enter}or ~ esc,//{esc} //etc }

so non-unit-test compiled programme added it

ksel1 = new keyboardsystemeventlistener((keys.f13), new keyboardtask(keyboardcommand.f1));//f1 ksel6 = new keyboardsystemeventlistener((keys.f18), new ectask(embeddedcontrollercommand.decreasebacklight));//rb

(there huge bug doing way, solved have implement it) set programme on tablet, , had ie open, pressed f13 button , opened ie's help (yeah!).. pressed f18 , backlight decreased.. (no big suprise there)

so got thinking.. there has improve way of doing (what should have said is, why didn't write unit test first) started write unit test.. problem don't have f13 key.. ok not huge deal i'll alter home button on keyboard, tried check f1 , realized have no clue how unit test. prefer see instead (take me out of equation)

[test] public void testkeyboardtask() { keyboardtask kkt = new keyboardtask(keyboardcommand.f1); kkt.performtask(); assert.istrue(/*f1 key pressed*/false); }

any ideas? i'm using nunit 2.6.2 , visual studio 2012 pro. prefer utilize nunit vs test suite doesn't seem refined (althoough billion times more conveniant if worked)

test class

[test] public void testkeyboardtask() { keyboardtask kkt = new keyboardtask(keyboardcommand.f1); using (mockkeyboardtest f = new mockkeyboardtest()) { f.showdialog(kkt); assert.areequal(keys.f1, f.pressedkey); } }

mock keyboard test

class mockkeyboardtest : form { public mockkeyboardtest() { initializecomponent(); pressedkey = keys.browserback; } public void showdialog(keyboardtask kkt) { keyboard = kkt; base.showdialog(); } public void initializecomponent() { this.shown += mockkeyboardtest_shown; keyboardtesttextbox.acceptstab = true; keyboardtesttextbox.location = new point(2, 22); keyboardtesttextbox.maxlength = 50; keyboardtesttextbox.multiline = true; keyboardtesttextbox.size = new size(195, 25); keyboardtesttextbox.keydown += this.keyboardtesttextbox_keydown; controls.add(keyboardtesttextbox); } void mockkeyboardtest_shown(object sender, system.eventargs e) { keyboard.performtask(); } private void keyboardtesttextbox_keydown(object sender, keyeventargs e) { pressedkey = e.keydata; this.dialogresult = dialogresult.ok; } private textbox keyboardtesttextbox = new textbox(); private keyboardtask keyboard; public keys pressedkey; }

works charm.

c# unit-testing nunit keyboard-events keyboard-hook

No comments:

Post a Comment