Repository navigation
Expand file tree
/
Copy pathMockKeyboard.cpp
More file actions
109 lines (84 loc) · 3.28 KB
/
Copy pathMockKeyboard.cpp
File metadata and controls
109 lines (84 loc) · 3.28 KB
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
/***************************************************************************************************
* Xidi
* DirectInput interface for XInput controllers.
***************************************************************************************************
* Authored by Samuel Grossman
* Copyright (c) 2016-2026
***********************************************************************************************//**
* @file MockKeyboard.cpp
* Implementation of a mock version of the keyboard interface along with additional
* testing-specific functions.
**************************************************************************************************/
#include "MockKeyboard.h"
#include <mutex>
#include <Infra/Test/TestCase.h>
#include "VirtualControllerTypes.h"
namespace XidiTest
{
using namespace ::Xidi::Keyboard;
/// Holds the mock keyboard object that is capturing input from the keyboard interface functions.
static MockKeyboard* capturingVirtualKeyboard = nullptr;
/// For ensuring proper concurrency control over the virtual keyboard capture state.
static std::mutex captureGuard;
MockKeyboard::~MockKeyboard(void)
{
if (this == capturingVirtualKeyboard) EndCapture();
}
void MockKeyboard::BeginCapture(void)
{
std::scoped_lock lock(captureGuard);
if (nullptr != capturingVirtualKeyboard)
TEST_FAILED_BECAUSE(
L"%s: Test implementation error due to attempting to replace another mock keyboard already capturing events.",
__FUNCTIONW__);
capturingVirtualKeyboard = this;
}
void MockKeyboard::EndCapture(void)
{
std::scoped_lock lock(captureGuard);
if (this != capturingVirtualKeyboard)
TEST_FAILED_BECAUSE(
L"%s: Test implementation error due to attempting to end capture for a mock keyboard not currently capturing events.",
__FUNCTIONW__);
capturingVirtualKeyboard = nullptr;
}
void MockKeyboard::SubmitKeyPressedState(TKeyIdentifier key)
{
if (key >= virtualKeyboardState.max_size())
TEST_FAILED_BECAUSE(
L"%s: Test implementation error due to out-of-bounds keyboard key identifier.",
__FUNCTIONW__);
virtualKeyboardState.insert(key);
}
void MockKeyboard::SubmitKeyReleasedState(TKeyIdentifier key)
{
if (key >= virtualKeyboardState.max_size())
TEST_FAILED_BECAUSE(
L"%s: Test implementation error due to out-of-bounds keyboard key identifier.",
__FUNCTIONW__);
virtualKeyboardState.erase(key);
}
} // namespace XidiTest
namespace Xidi
{
namespace Keyboard
{
using namespace ::XidiTest;
void SubmitKeyPressedState(TKeyIdentifier key)
{
std::scoped_lock lock(captureGuard);
if (nullptr == capturingVirtualKeyboard)
TEST_FAILED_BECAUSE(
L"%s: No mock keyboard is installed to capture a key press event.", __FUNCTIONW__);
capturingVirtualKeyboard->SubmitKeyPressedState(key);
}
void SubmitKeyReleasedState(TKeyIdentifier key)
{
std::scoped_lock lock(captureGuard);
if (nullptr == capturingVirtualKeyboard)
TEST_FAILED_BECAUSE(
L"%s: No mock keyboard is installed to capture a key release event.", __FUNCTIONW__);
capturingVirtualKeyboard->SubmitKeyReleasedState(key);
}
} // namespace Keyboard
} // namespace Xidi