added working key tests on osx

This commit is contained in:
Nick Bolton 2012-07-24 22:08:38 +00:00
parent 6dcefaf353
commit e2f8e65298
1 changed files with 56 additions and 59 deletions

View File

@ -22,77 +22,74 @@
#include "CMockKeyMap.h" #include "CMockKeyMap.h"
#include "CMockEventQueue.h" #include "CMockEventQueue.h"
CGKeyCode escKeyCode = 53; #include "CLog.h"
CGKeyCode shiftKeyCode = 56;
CGKeyCode controlKeyCode = 59;
// TODO: make pollActiveModifiers tests work reliably. #define SHIFT_ID_L kKeyShift_L
/* #define SHIFT_ID_R kKeyShift_R
TEST(COSXKeyStateTests, pollActiveModifiers_shiftKeyDownThenUp_masksAreCorrect) #define SHIFT_BUTTON 057
#define A_CHAR_ID 0x00000061
#define A_CHAR_BUTTON 001
class COSXKeyStateTests : public ::testing::Test {
public:
static bool isKeyPressed(const COSXKeyState& keyState, KeyButton button);
};
TEST_F(COSXKeyStateTests, fakeAndPoll_shift)
{ {
CMockKeyMap keyMap; CKeyMap keyMap;
CMockEventQueue eventQueue; CMockEventQueue eventQueue;
COSXKeyState keyState((IEventQueue&)keyMap, (CKeyMap&)eventQueue); COSXKeyState keyState((IEventQueue&)eventQueue, keyMap);
keyState.updateKeyMap();
// fake shift key down (without using synergy). this is a bit weird; keyState.fakeKeyDown(SHIFT_ID_L, 0, 1);
// looks like you need to create a shift down event *and* set the ASSERT_TRUE(isKeyPressed(keyState, SHIFT_BUTTON));
// shift modifier.
CGEventRef shiftDown = CGEventCreateKeyboardEvent(NULL, shiftKeyCode, true);
CGEventSetFlags(shiftDown, kCGEventFlagMaskShift);
CGEventPost(kCGHIDEventTap, shiftDown);
CFRelease(shiftDown);
// function under test (1st call) keyState.fakeKeyUp(1);
KeyModifierMask downMask = keyState.pollActiveModifiers(); ASSERT_TRUE(!isKeyPressed(keyState, SHIFT_BUTTON));
// fake shift key up (without using synergy). also as weird as the keyState.fakeKeyDown(SHIFT_ID_L, 0, 2);
// shift down; use a non-shift key down and reset the pressed modifiers. ASSERT_TRUE(isKeyPressed(keyState, SHIFT_BUTTON));
CGEventRef shiftUp = CGEventCreateKeyboardEvent(NULL, escKeyCode, true);
CGEventSetFlags(shiftUp, 0);
CGEventPost(kCGHIDEventTap, shiftUp);
CFRelease(shiftUp);
// function under test (2nd call) keyState.fakeKeyUp(2);
KeyModifierMask upMask = keyState.pollActiveModifiers(); ASSERT_TRUE(!isKeyPressed(keyState, SHIFT_BUTTON));
EXPECT_TRUE((downMask & KeyModifierShift) == KeyModifierShift)
<< "shift key not in mask (" << downMask << ") - key was not pressed";
EXPECT_TRUE((upMask & KeyModifierShift) == 0)
<< "shift key still in mask (" << upMask << ") - make sure no keys are being held down";
} }
TEST(COSXKeyStateTests, pollActiveModifiers_controlKeyDownThenUp_masksAreCorrect) TEST_F(COSXKeyStateTests, fakeAndPoll_charKey)
{ {
CMockKeyMap keyMap; CKeyMap keyMap;
CMockEventQueue eventQueue; CMockEventQueue eventQueue;
COSXKeyState keyState((IEventQueue&)keyMap, (CKeyMap&)eventQueue); COSXKeyState keyState((IEventQueue&)eventQueue, keyMap);
keyState.updateKeyMap();
// fake control key down (without using synergy). this is a bit weird; keyState.fakeKeyDown(A_CHAR_ID, 0, 1);
// looks like you need to create a shift down event *and* set the ASSERT_TRUE(isKeyPressed(keyState, A_CHAR_BUTTON));
// shift modifier.
CGEventRef controlDown = CGEventCreateKeyboardEvent(NULL, controlKeyCode, true);
CGEventSetFlags(controlDown, kCGEventFlagMaskControl);
CGEventPost(kCGHIDEventTap, controlDown);
CFRelease(controlDown);
// function under test (1st call) keyState.fakeKeyUp(1);
KeyModifierMask downMask = keyState.pollActiveModifiers(); ASSERT_TRUE(!isKeyPressed(keyState, A_CHAR_BUTTON));
// fake control key up (without using synergy). also as weird as the // HACK: delete the key in case it was typed into a text editor.
// shift down; use a non-shift key down and reset the pressed modifiers. // we should really set focus to an invisible window.
CGEventRef controlUp = CGEventCreateKeyboardEvent(NULL, escKeyCode, true); keyState.fakeKeyDown(kKeyBackSpace, 0, 2);
CGEventSetFlags(controlUp, 0); keyState.fakeKeyUp(2);
CGEventPost(kCGHIDEventTap, controlUp);
CFRelease(controlUp);
// function under test (2nd call)
KeyModifierMask upMask = keyState.pollActiveModifiers();
EXPECT_TRUE((downMask & KeyModifierControl) == KeyModifierControl)
<< "control key not in mask (" << downMask << ") - key was not pressed";
EXPECT_TRUE((upMask & KeyModifierControl) == 0)
<< "control key still in mask (" << upMask << ") - make sure no keys are being held down";
} }
*/
bool
COSXKeyStateTests::isKeyPressed(const COSXKeyState& keyState, KeyButton button)
{
// allow os to realize key state changes.
ARCH->sleep(.01);
IKeyState::KeyButtonSet pressed;
keyState.pollPressedKeys(pressed);
IKeyState::KeyButtonSet::const_iterator it;
for (it = pressed.begin(); it != pressed.end(); ++it) {
LOG((CLOG_DEBUG "checking key %d", *it));
if (*it == button) {
return true;
}
}
return false;
}