aboutsummaryrefslogtreecommitdiffstats
path: root/src/test/integtests/platform/OSXKeyStateTests.cpp
blob: 4957aaa4022125aa1401fab3b90d4ff12c6fbbaa (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*
 * barrier -- mouse and keyboard sharing utility
 * Copyright (C) 2012-2016 Symless Ltd.
 * Copyright (C) 2011 Nick Bolton
 *
 * This package is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * found in the file LICENSE that should have accompanied this file.
 *
 * This package is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "test/mock/barrier/MockKeyMap.h"
#include "test/mock/barrier/MockEventQueue.h"
#include "platform/OSXKeyState.h"
#include "base/Log.h"

#include "test/global/gtest.h"
#include "test/global/gmock.h"

#define SHIFT_ID_L kKeyShift_L
#define SHIFT_ID_R kKeyShift_R
#define SHIFT_BUTTON 57
#define A_CHAR_ID 0x00000061
#define A_CHAR_BUTTON 001

class OSXKeyStateTests : public ::testing::Test {
public:
    static bool isKeyPressed(const OSXKeyState& keyState, KeyButton button);
};

// fakeAndPoll_shift seems to always fail on osx10.6
#if __MAC_OS_X_VERSION_MIN_REQUIRED > 1060

TEST_F(OSXKeyStateTests, fakeAndPoll_shift)
{
    barrier::KeyMap keyMap;
    MockEventQueue eventQueue;
    OSXKeyState keyState(&eventQueue, keyMap);
    keyState.updateKeyMap();

    keyState.fakeKeyDown(SHIFT_ID_L, 0, 1);
    EXPECT_TRUE(isKeyPressed(keyState, SHIFT_BUTTON));

    keyState.fakeKeyUp(1);
    EXPECT_TRUE(!isKeyPressed(keyState, SHIFT_BUTTON));

    keyState.fakeKeyDown(SHIFT_ID_R, 0, 2);
    EXPECT_TRUE(isKeyPressed(keyState, SHIFT_BUTTON));

    keyState.fakeKeyUp(2);
    EXPECT_TRUE(!isKeyPressed(keyState, SHIFT_BUTTON));
}

TEST_F(OSXKeyStateTests, fakeAndPoll_charKey)
{
    barrier::KeyMap keyMap;
    MockEventQueue eventQueue;
    OSXKeyState keyState(&eventQueue, keyMap);
    keyState.updateKeyMap();

    keyState.fakeKeyDown(A_CHAR_ID, 0, 1);
    EXPECT_TRUE(isKeyPressed(keyState, A_CHAR_BUTTON));

    keyState.fakeKeyUp(1);
    EXPECT_TRUE(!isKeyPressed(keyState, A_CHAR_BUTTON));

    // HACK: delete the key in case it was typed into a text editor.
    // we should really set focus to an invisible window.
    keyState.fakeKeyDown(kKeyBackSpace, 0, 2);
    keyState.fakeKeyUp(2);
}

TEST_F(OSXKeyStateTests, fakeAndPoll_charKeyAndModifier)
{
    barrier::KeyMap keyMap;
    MockEventQueue eventQueue;
    OSXKeyState keyState(&eventQueue, keyMap);
    keyState.updateKeyMap();

    keyState.fakeKeyDown(A_CHAR_ID, KeyModifierShift, 1);
    EXPECT_TRUE(isKeyPressed(keyState, A_CHAR_BUTTON));

    keyState.fakeKeyUp(1);
    EXPECT_TRUE(!isKeyPressed(keyState, A_CHAR_BUTTON));

    // HACK: delete the key in case it was typed into a text editor.
    // we should really set focus to an invisible window.
    keyState.fakeKeyDown(kKeyBackSpace, 0, 2);
    keyState.fakeKeyUp(2);
}

bool
OSXKeyStateTests::isKeyPressed(const OSXKeyState& keyState, KeyButton button)
{
    // HACK: allow os to realize key state changes.
    ARCH->sleep(.2);

    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;
}

#endif