Moved each test class to a separate file (#4592)

This commit is contained in:
Arjun Balgovind 2020-07-02 10:25:57 -07:00 committed by GitHub
parent 4330338285
commit 656c33e942
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 274 additions and 212 deletions

View File

@ -102,11 +102,14 @@
</ClCompile>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="KeyboardManagerRemapLogicTest.cpp" />
<ClCompile Include="MockedInputSanityTests.cpp" />
<ClCompile Include="SetKeyEventTests.cpp" />
<ClCompile Include="OSLevelShortcutRemappingTests.cpp" />
<ClCompile Include="MockedInput.cpp" />
<ClCompile Include="pch.cpp">
<PrecompiledHeader Condition="'$(CIBuild)'!='true'">Create</PrecompiledHeader>
</ClCompile>
<ClCompile Include="SingleKeyRemappingTests.cpp" />
<ClCompile Include="TestHelpers.cpp" />
</ItemGroup>
<ItemGroup>

View File

@ -24,7 +24,16 @@
<ClCompile Include="TestHelpers.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="KeyboardManagerRemapLogicTest.cpp">
<ClCompile Include="SingleKeyRemappingTests.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="MockedInputSanityTests.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="SetKeyEventTests.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="OSLevelShortcutRemappingTests.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>

View File

@ -0,0 +1,49 @@
#include "pch.h"
#include "CppUnitTest.h"
#include "MockedInput.h"
#include <keyboardmanager/common/KeyboardManagerState.h>
#include <keyboardmanager/dll/KeyboardEventHandlers.h>
#include "TestHelpers.h"
using namespace Microsoft::VisualStudio::CppUnitTestFramework;
namespace RemappingLogicTests
{
// Tests for MockedInput test helper - to ensure simulated keyboard input behaves as expected
TEST_CLASS (MockedInputSanityTests)
{
private:
MockedInput mockedInputHandler;
KeyboardManagerState testState;
public:
TEST_METHOD_INITIALIZE(InitializeTestEnv)
{
// Reset test environment
TestHelpers::ResetTestEnv(mockedInputHandler, testState);
}
// Test if mocked input is working
TEST_METHOD (MockedInput_ShouldSetKeyboardState_OnKeyEvent)
{
// Send key down and key up for A key (0x41) and check keyboard state both times
const int nInputs = 1;
INPUT input[nInputs] = {};
input[0].type = INPUT_KEYBOARD;
input[0].ki.wVk = 0x41;
// Send A keydown
mockedInputHandler.SendVirtualInput(nInputs, input, sizeof(INPUT));
// A key state should be true
Assert::AreEqual(mockedInputHandler.GetVirtualKeyState(0x41), true);
input[0].ki.dwFlags = KEYEVENTF_KEYUP;
// Send A keyup
mockedInputHandler.SendVirtualInput(nInputs, input, sizeof(INPUT));
// A key state should be false
Assert::AreEqual(mockedInputHandler.GetVirtualKeyState(0x41), false);
}
};
}

View File

@ -7,219 +7,15 @@
using namespace Microsoft::VisualStudio::CppUnitTestFramework;
MockedInput mockedInputHandler;
KeyboardManagerState testState;
namespace KeyboardManagerRemapLogicTests
namespace RemappingLogicTests
{
TEST_CLASS (MockedInputSanityTests)
{
public:
TEST_METHOD_INITIALIZE(InitializeTestEnv)
{
// Reset test environment
TestHelpers::ResetTestEnv(mockedInputHandler, testState);
}
// Test if mocked input is working
TEST_METHOD (MockedInput_ShouldSetKeyboardState_OnKeyEvent)
{
// Send key down and key up for A key (0x41) and check keyboard state both times
const int nInputs = 1;
INPUT input[nInputs] = {};
input[0].type = INPUT_KEYBOARD;
input[0].ki.wVk = 0x41;
// Send A keydown
mockedInputHandler.SendVirtualInput(nInputs, input, sizeof(INPUT));
// A key state should be true
Assert::AreEqual(mockedInputHandler.GetVirtualKeyState(0x41), true);
input[0].ki.dwFlags = KEYEVENTF_KEYUP;
// Send A keyup
mockedInputHandler.SendVirtualInput(nInputs, input, sizeof(INPUT));
// A key state should be false
Assert::AreEqual(mockedInputHandler.GetVirtualKeyState(0x41), false);
}
};
TEST_CLASS (SetKeyEventTests)
{
public:
TEST_METHOD_INITIALIZE(InitializeTestEnv)
{
// Reset test environment
TestHelpers::ResetTestEnv(mockedInputHandler, testState);
}
// Test if SetKeyEvent sets the extended key flag for all the extended keys
TEST_METHOD (SetKeyEvent_ShouldUseExtendedKeyFlag_WhenArgumentIsExtendedKey)
{
const int nInputs = 15;
INPUT input[nInputs] = {};
// List of extended keys
WORD keyCodes[nInputs] = { VK_RCONTROL, VK_RMENU, VK_NUMLOCK, VK_SNAPSHOT, VK_CANCEL, VK_INSERT, VK_HOME, VK_PRIOR, VK_DELETE, VK_END, VK_NEXT, VK_LEFT, VK_DOWN, VK_RIGHT, VK_UP };
for (int i = 0; i < nInputs; i++)
{
// Set key events for all the extended keys
KeyboardManagerHelper::SetKeyEvent(input, i, INPUT_KEYBOARD, keyCodes[i], 0, 0);
// Extended key flag should be set
Assert::AreEqual(true, bool(input[i].ki.dwFlags & KEYEVENTF_EXTENDEDKEY));
}
}
};
TEST_CLASS (SingleKeyRemappingTests)
{
public:
TEST_METHOD_INITIALIZE(InitializeTestEnv)
{
// Reset test environment
TestHelpers::ResetTestEnv(mockedInputHandler, testState);
// Set HandleSingleKeyRemapEvent as the hook procedure
std::function<intptr_t(LowlevelKeyboardEvent*)> currentHookProc = std::bind(&KeyboardEventHandlers::HandleSingleKeyRemapEvent, std::ref(mockedInputHandler), std::placeholders::_1, std::ref(testState));
mockedInputHandler.SetHookProc(currentHookProc);
}
// Test if correct keyboard states are set for a single key remap
TEST_METHOD (RemappedKey_ShouldSetTargetKeyState_OnKeyEvent)
{
// Remap A to B
testState.AddSingleKeyRemap(0x41, 0x42);
const int nInputs = 1;
INPUT input[nInputs] = {};
input[0].type = INPUT_KEYBOARD;
input[0].ki.wVk = 0x41;
// Send A keydown
mockedInputHandler.SendVirtualInput(1, input, sizeof(INPUT));
// A key state should be unchanged, and B key state should be true
Assert::AreEqual(mockedInputHandler.GetVirtualKeyState(0x41), false);
Assert::AreEqual(mockedInputHandler.GetVirtualKeyState(0x42), true);
input[0].ki.dwFlags = KEYEVENTF_KEYUP;
// Send A keyup
mockedInputHandler.SendVirtualInput(1, input, sizeof(INPUT));
// A key state should be unchanged, and B key state should be false
Assert::AreEqual(mockedInputHandler.GetVirtualKeyState(0x41), false);
Assert::AreEqual(mockedInputHandler.GetVirtualKeyState(0x42), false);
}
// Test if key is suppressed if a key is disabled by single key remap
TEST_METHOD (RemappedKeyDisabled_ShouldNotChangeKeyState_OnKeyEvent)
{
// Remap A to 0x0 (disabled)
testState.AddSingleKeyRemap(0x41, 0x0);
const int nInputs = 1;
INPUT input[nInputs] = {};
input[0].type = INPUT_KEYBOARD;
input[0].ki.wVk = 0x41;
// Send A keydown
mockedInputHandler.SendVirtualInput(1, input, sizeof(INPUT));
// A key state should be unchanged
Assert::AreEqual(mockedInputHandler.GetVirtualKeyState(0x41), false);
input[0].ki.dwFlags = KEYEVENTF_KEYUP;
// Send A keyup
mockedInputHandler.SendVirtualInput(1, input, sizeof(INPUT));
// A key state should be unchanged
Assert::AreEqual(mockedInputHandler.GetVirtualKeyState(0x41), false);
}
// Test if correct keyboard states are set for a remap to Win (Both) key
TEST_METHOD (RemappedKeyToWinBoth_ShouldSetWinLeftKeyState_OnKeyEvent)
{
// Remap A to Common Win key
testState.AddSingleKeyRemap(0x41, CommonSharedConstants::VK_WIN_BOTH);
const int nInputs = 1;
INPUT input[nInputs] = {};
input[0].type = INPUT_KEYBOARD;
input[0].ki.wVk = 0x41;
// Send A keydown
mockedInputHandler.SendVirtualInput(1, input, sizeof(INPUT));
// A key state should be unchanged, and common Win key state should be true
Assert::AreEqual(mockedInputHandler.GetVirtualKeyState(0x41), false);
Assert::AreEqual(mockedInputHandler.GetVirtualKeyState(VK_LWIN), true);
input[0].ki.dwFlags = KEYEVENTF_KEYUP;
// Send A keyup
mockedInputHandler.SendVirtualInput(1, input, sizeof(INPUT));
// A key state should be unchanged, and common Win key state should be false
Assert::AreEqual(mockedInputHandler.GetVirtualKeyState(0x41), false);
Assert::AreEqual(mockedInputHandler.GetVirtualKeyState(VK_LWIN), false);
}
// Test if SendVirtualInput is sent exactly once with the suppress flag when Caps Lock is remapped to Ctrl
TEST_METHOD (HandleSingleKeyRemapEvent_ShouldSendVirutalInputWithSuppressFlagExactlyOnce_WhenCapsLockIsMappedToCtrlAltShift)
{
// Set sendvirtualinput call count condition to return true if the key event was sent with the suppress flag
mockedInputHandler.SetSendVirtualInputTestHandler([](LowlevelKeyboardEvent* data) {
if (data->lParam->dwExtraInfo == KeyboardManagerConstants::KEYBOARDMANAGER_SUPPRESS_FLAG)
return true;
else
return false;
});
// Remap Caps Lock to Ctrl
testState.AddSingleKeyRemap(VK_CAPITAL, VK_CONTROL);
const int nInputs = 1;
INPUT input[nInputs] = {};
input[0].type = INPUT_KEYBOARD;
input[0].ki.wVk = VK_CAPITAL;
// Send Caps Lock keydown
mockedInputHandler.SendVirtualInput(1, input, sizeof(INPUT));
// SendVirtualInput should be called exactly once with the above condition
Assert::AreEqual(1, mockedInputHandler.GetSendVirtualInputCallCount());
}
// Test if SendVirtualInput is sent exactly once with the suppress flag when Ctrl is remapped to Caps Lock
TEST_METHOD (HandleSingleKeyRemapEvent_ShouldSendVirutalInputWithSuppressFlagExactlyOnce_WhenCtrlAltShiftIsMappedToCapsLock)
{
// Set sendvirtualinput call count condition to return true if the key event was sent with the suppress flag
mockedInputHandler.SetSendVirtualInputTestHandler([](LowlevelKeyboardEvent* data) {
if (data->lParam->dwExtraInfo == KeyboardManagerConstants::KEYBOARDMANAGER_SUPPRESS_FLAG)
return true;
else
return false;
});
// Remap Ctrl to Caps Lock
testState.AddSingleKeyRemap(VK_CONTROL, VK_CAPITAL);
const int nInputs = 1;
INPUT input[nInputs] = {};
input[0].type = INPUT_KEYBOARD;
input[0].ki.wVk = VK_CONTROL;
// Send Ctrl keydown
mockedInputHandler.SendVirtualInput(1, input, sizeof(INPUT));
// SendVirtualInput should be called exactly once with the above condition
Assert::AreEqual(1, mockedInputHandler.GetSendVirtualInputCallCount());
}
};
// Tests for shortcut remapping logic
TEST_CLASS (OSLevelShortcutRemappingTests)
{
private:
MockedInput mockedInputHandler;
KeyboardManagerState testState;
public:
TEST_METHOD_INITIALIZE(InitializeTestEnv)
{

View File

@ -0,0 +1,44 @@
#include "pch.h"
#include "CppUnitTest.h"
#include "MockedInput.h"
#include <keyboardmanager/common/KeyboardManagerState.h>
#include <keyboardmanager/dll/KeyboardEventHandlers.h>
#include "TestHelpers.h"
using namespace Microsoft::VisualStudio::CppUnitTestFramework;
namespace RemappingLogicTests
{
// Tests for the SetKeyEvent method
TEST_CLASS (SetKeyEventTests)
{
private:
MockedInput mockedInputHandler;
KeyboardManagerState testState;
public:
TEST_METHOD_INITIALIZE(InitializeTestEnv)
{
// Reset test environment
TestHelpers::ResetTestEnv(mockedInputHandler, testState);
}
// Test if SetKeyEvent sets the extended key flag for all the extended keys
TEST_METHOD (SetKeyEvent_ShouldUseExtendedKeyFlag_WhenArgumentIsExtendedKey)
{
const int nInputs = 15;
INPUT input[nInputs] = {};
// List of extended keys
WORD keyCodes[nInputs] = { VK_RCONTROL, VK_RMENU, VK_NUMLOCK, VK_SNAPSHOT, VK_CANCEL, VK_INSERT, VK_HOME, VK_PRIOR, VK_DELETE, VK_END, VK_NEXT, VK_LEFT, VK_DOWN, VK_RIGHT, VK_UP };
for (int i = 0; i < nInputs; i++)
{
// Set key events for all the extended keys
KeyboardManagerHelper::SetKeyEvent(input, i, INPUT_KEYBOARD, keyCodes[i], 0, 0);
// Extended key flag should be set
Assert::AreEqual(true, bool(input[i].ki.dwFlags & KEYEVENTF_EXTENDEDKEY));
}
}
};
}

View File

@ -0,0 +1,161 @@
#include "pch.h"
#include "CppUnitTest.h"
#include "MockedInput.h"
#include <keyboardmanager/common/KeyboardManagerState.h>
#include <keyboardmanager/dll/KeyboardEventHandlers.h>
#include "TestHelpers.h"
using namespace Microsoft::VisualStudio::CppUnitTestFramework;
namespace RemappingLogicTests
{
// Tests for single key remapping logic
TEST_CLASS (SingleKeyRemappingTests)
{
private:
MockedInput mockedInputHandler;
KeyboardManagerState testState;
public:
TEST_METHOD_INITIALIZE(InitializeTestEnv)
{
// Reset test environment
TestHelpers::ResetTestEnv(mockedInputHandler, testState);
// Set HandleSingleKeyRemapEvent as the hook procedure
std::function<intptr_t(LowlevelKeyboardEvent*)> currentHookProc = std::bind(&KeyboardEventHandlers::HandleSingleKeyRemapEvent, std::ref(mockedInputHandler), std::placeholders::_1, std::ref(testState));
mockedInputHandler.SetHookProc(currentHookProc);
}
// Test if correct keyboard states are set for a single key remap
TEST_METHOD (RemappedKey_ShouldSetTargetKeyState_OnKeyEvent)
{
// Remap A to B
testState.AddSingleKeyRemap(0x41, 0x42);
const int nInputs = 1;
INPUT input[nInputs] = {};
input[0].type = INPUT_KEYBOARD;
input[0].ki.wVk = 0x41;
// Send A keydown
mockedInputHandler.SendVirtualInput(1, input, sizeof(INPUT));
// A key state should be unchanged, and B key state should be true
Assert::AreEqual(mockedInputHandler.GetVirtualKeyState(0x41), false);
Assert::AreEqual(mockedInputHandler.GetVirtualKeyState(0x42), true);
input[0].ki.dwFlags = KEYEVENTF_KEYUP;
// Send A keyup
mockedInputHandler.SendVirtualInput(1, input, sizeof(INPUT));
// A key state should be unchanged, and B key state should be false
Assert::AreEqual(mockedInputHandler.GetVirtualKeyState(0x41), false);
Assert::AreEqual(mockedInputHandler.GetVirtualKeyState(0x42), false);
}
// Test if key is suppressed if a key is disabled by single key remap
TEST_METHOD (RemappedKeyDisabled_ShouldNotChangeKeyState_OnKeyEvent)
{
// Remap A to 0x0 (disabled)
testState.AddSingleKeyRemap(0x41, 0x0);
const int nInputs = 1;
INPUT input[nInputs] = {};
input[0].type = INPUT_KEYBOARD;
input[0].ki.wVk = 0x41;
// Send A keydown
mockedInputHandler.SendVirtualInput(1, input, sizeof(INPUT));
// A key state should be unchanged
Assert::AreEqual(mockedInputHandler.GetVirtualKeyState(0x41), false);
input[0].ki.dwFlags = KEYEVENTF_KEYUP;
// Send A keyup
mockedInputHandler.SendVirtualInput(1, input, sizeof(INPUT));
// A key state should be unchanged
Assert::AreEqual(mockedInputHandler.GetVirtualKeyState(0x41), false);
}
// Test if correct keyboard states are set for a remap to Win (Both) key
TEST_METHOD (RemappedKeyToWinBoth_ShouldSetWinLeftKeyState_OnKeyEvent)
{
// Remap A to Common Win key
testState.AddSingleKeyRemap(0x41, CommonSharedConstants::VK_WIN_BOTH);
const int nInputs = 1;
INPUT input[nInputs] = {};
input[0].type = INPUT_KEYBOARD;
input[0].ki.wVk = 0x41;
// Send A keydown
mockedInputHandler.SendVirtualInput(1, input, sizeof(INPUT));
// A key state should be unchanged, and common Win key state should be true
Assert::AreEqual(mockedInputHandler.GetVirtualKeyState(0x41), false);
Assert::AreEqual(mockedInputHandler.GetVirtualKeyState(VK_LWIN), true);
input[0].ki.dwFlags = KEYEVENTF_KEYUP;
// Send A keyup
mockedInputHandler.SendVirtualInput(1, input, sizeof(INPUT));
// A key state should be unchanged, and common Win key state should be false
Assert::AreEqual(mockedInputHandler.GetVirtualKeyState(0x41), false);
Assert::AreEqual(mockedInputHandler.GetVirtualKeyState(VK_LWIN), false);
}
// Test if SendVirtualInput is sent exactly once with the suppress flag when Caps Lock is remapped to Ctrl
TEST_METHOD (HandleSingleKeyRemapEvent_ShouldSendVirutalInputWithSuppressFlagExactlyOnce_WhenCapsLockIsMappedToCtrlAltShift)
{
// Set sendvirtualinput call count condition to return true if the key event was sent with the suppress flag
mockedInputHandler.SetSendVirtualInputTestHandler([](LowlevelKeyboardEvent* data) {
if (data->lParam->dwExtraInfo == KeyboardManagerConstants::KEYBOARDMANAGER_SUPPRESS_FLAG)
return true;
else
return false;
});
// Remap Caps Lock to Ctrl
testState.AddSingleKeyRemap(VK_CAPITAL, VK_CONTROL);
const int nInputs = 1;
INPUT input[nInputs] = {};
input[0].type = INPUT_KEYBOARD;
input[0].ki.wVk = VK_CAPITAL;
// Send Caps Lock keydown
mockedInputHandler.SendVirtualInput(1, input, sizeof(INPUT));
// SendVirtualInput should be called exactly once with the above condition
Assert::AreEqual(1, mockedInputHandler.GetSendVirtualInputCallCount());
}
// Test if SendVirtualInput is sent exactly once with the suppress flag when Ctrl is remapped to Caps Lock
TEST_METHOD (HandleSingleKeyRemapEvent_ShouldSendVirutalInputWithSuppressFlagExactlyOnce_WhenCtrlAltShiftIsMappedToCapsLock)
{
// Set sendvirtualinput call count condition to return true if the key event was sent with the suppress flag
mockedInputHandler.SetSendVirtualInputTestHandler([](LowlevelKeyboardEvent* data) {
if (data->lParam->dwExtraInfo == KeyboardManagerConstants::KEYBOARDMANAGER_SUPPRESS_FLAG)
return true;
else
return false;
});
// Remap Ctrl to Caps Lock
testState.AddSingleKeyRemap(VK_CONTROL, VK_CAPITAL);
const int nInputs = 1;
INPUT input[nInputs] = {};
input[0].type = INPUT_KEYBOARD;
input[0].ki.wVk = VK_CONTROL;
// Send Ctrl keydown
mockedInputHandler.SendVirtualInput(1, input, sizeof(INPUT));
// SendVirtualInput should be called exactly once with the above condition
Assert::AreEqual(1, mockedInputHandler.GetSendVirtualInputCallCount());
}
};
}