• Main Page
  • Classes
  • Files
  • File List

COSXKeyStateTests.cpp

00001 /*
00002  * synergy -- mouse and keyboard sharing utility
00003  * Copyright (C) 2012 Bolton Software Ltd.
00004  * Copyright (C) 2011 Nick Bolton
00005  *
00006  * This package is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU General Public License
00008  * found in the file COPYING that should have accompanied this file.
00009  *
00010  * This package is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  * GNU General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU General Public License
00016  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00017  */
00018 
00019 #include <gtest/gtest.h>
00020 #include <gmock/gmock.h>
00021 
00022 #include "COSXKeyState.h"
00023 #include "CMockKeyMap.h"
00024 #include "CMockEventQueue.h"
00025 
00026 #include "CLog.h"
00027 
00028 #define SHIFT_ID_L kKeyShift_L
00029 #define SHIFT_ID_R kKeyShift_R
00030 #define SHIFT_BUTTON 57
00031 #define A_CHAR_ID 0x00000061
00032 #define A_CHAR_BUTTON 001
00033 
00034 class COSXKeyStateTests : public ::testing::Test {
00035 public:
00036     static bool isKeyPressed(const COSXKeyState& keyState, KeyButton button);
00037 };
00038 
00039 TEST_F(COSXKeyStateTests, fakeAndPoll_shift)
00040 {
00041     CKeyMap keyMap;
00042     CMockEventQueue eventQueue;
00043     COSXKeyState keyState((IEventQueue&)eventQueue, keyMap);
00044     keyState.updateKeyMap();
00045 
00046     keyState.fakeKeyDown(SHIFT_ID_L, 0, 1);
00047     EXPECT_TRUE(isKeyPressed(keyState, SHIFT_BUTTON));
00048 
00049     keyState.fakeKeyUp(1);
00050     EXPECT_TRUE(!isKeyPressed(keyState, SHIFT_BUTTON));
00051 
00052     keyState.fakeKeyDown(SHIFT_ID_R, 0, 2);
00053     EXPECT_TRUE(isKeyPressed(keyState, SHIFT_BUTTON));
00054 
00055     keyState.fakeKeyUp(2);
00056     EXPECT_TRUE(!isKeyPressed(keyState, SHIFT_BUTTON));
00057 }
00058 
00059 TEST_F(COSXKeyStateTests, fakeAndPoll_charKey)
00060 {
00061     CKeyMap keyMap;
00062     CMockEventQueue eventQueue;
00063     COSXKeyState keyState((IEventQueue&)eventQueue, keyMap);
00064     keyState.updateKeyMap();
00065 
00066     keyState.fakeKeyDown(A_CHAR_ID, 0, 1);
00067     EXPECT_TRUE(isKeyPressed(keyState, A_CHAR_BUTTON));
00068 
00069     keyState.fakeKeyUp(1);
00070     EXPECT_TRUE(!isKeyPressed(keyState, A_CHAR_BUTTON));
00071 
00072     // HACK: delete the key in case it was typed into a text editor.
00073     // we should really set focus to an invisible window.
00074     keyState.fakeKeyDown(kKeyBackSpace, 0, 2);
00075     keyState.fakeKeyUp(2);
00076 }
00077 
00078 TEST_F(COSXKeyStateTests, fakeAndPoll_charKeyAndModifier)
00079 {
00080     CKeyMap keyMap;
00081     CMockEventQueue eventQueue;
00082     COSXKeyState keyState((IEventQueue&)eventQueue, keyMap);
00083     keyState.updateKeyMap();
00084 
00085     keyState.fakeKeyDown(A_CHAR_ID, KeyModifierShift, 1);
00086     EXPECT_TRUE(isKeyPressed(keyState, A_CHAR_BUTTON));
00087 
00088     keyState.fakeKeyUp(1);
00089     EXPECT_TRUE(!isKeyPressed(keyState, A_CHAR_BUTTON));
00090 
00091     // HACK: delete the key in case it was typed into a text editor.
00092     // we should really set focus to an invisible window.
00093     keyState.fakeKeyDown(kKeyBackSpace, 0, 2);
00094     keyState.fakeKeyUp(2);
00095 }
00096 
00097 bool
00098 COSXKeyStateTests::isKeyPressed(const COSXKeyState& keyState, KeyButton button)
00099 {
00100     // allow os to realize key state changes.
00101     ARCH->sleep(.01);
00102 
00103     IKeyState::KeyButtonSet pressed;
00104     keyState.pollPressedKeys(pressed);
00105 
00106     IKeyState::KeyButtonSet::const_iterator it;
00107     for (it = pressed.begin(); it != pressed.end(); ++it) {
00108         LOG((CLOG_DEBUG "checking key %d", *it));
00109         if (*it == button) {
00110             return true;
00111         }
00112     }
00113     return false;
00114 }
00115 

Generated on Sat May 18 2013 00:00:04 for Synergy by  doxygen 1.7.1