Refactored Keyboard Manager UI code and added unit tests for Keyboard Manager UI (#5718)

* Added tests for loading and saving remappings in the UI

* Added tests for ApplyRemappings methods

* Moved single key remap validation logic to separate method so that it can be tested

* Added tests for single key remap validation in UI

* Refactored shortcut validation code to be testable

* Added some shortcut validation tests

* Refactored code to be cleaner

* Added tests for action key and modifier key selection and formatted file

* Added tests for selecting None

* Added tests for selecting Null

* Added tests for WinL error

* Added CtrlAltDel tests

* Added tests for MapToSameKey

* Added tests for mapping repeated shortcut

* Fixed const correctness

* Clean up type names with type alias

* Clean up ValidateAndUpdateKeyBufferElement tests and tweak ValidateShortcutBufferElement signature

* Fixed bug when None selected

* Refactored one test as per test case framework

* Cleaned up more tests

* Cleaned up buffer validation tests

* Added tests for KBM Common Helpers and Shortcut
This commit is contained in:
Arjun Balgovind
2020-08-13 16:32:15 -07:00
committed by GitHub
parent 0b5749d491
commit 9e8b0d2807
30 changed files with 3077 additions and 602 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,173 @@
#include "pch.h"
#include "CppUnitTest.h"
#include <keyboardmanager/common/Helpers.h>
#include "TestHelpers.h"
#include <common/keyboard_layout.h>
using namespace Microsoft::VisualStudio::CppUnitTestFramework;
namespace KeyboardManagerCommonTests
{
// Tests for methods in the KeyboardManagerHelper namespace
TEST_CLASS (KeyboardManagerHelperTests)
{
public:
TEST_METHOD_INITIALIZE(InitializeTestEnv)
{
}
// Test if the DoKeysOverlap method returns SameKeyPreviouslyMapped on passing the same key for both arguments
TEST_METHOD (DoKeysOverlap_ShouldReturnSameKeyPreviouslyMapped_OnPassingSameKeyForBothArguments)
{
// Arrange
DWORD key1 = 0x41;
DWORD key2 = key1;
// Act
auto result = KeyboardManagerHelper::DoKeysOverlap(key1, key2);
// Assert
Assert::IsTrue(result == KeyboardManagerHelper::ErrorType::SameKeyPreviouslyMapped);
}
// Test if the DoKeysOverlap method returns ConflictingModifierKey on passing left modifier and common modifier
TEST_METHOD (DoKeysOverlap_ShouldReturnConflictingModifierKey_OnPassingLeftModifierAndCommonModifierOfSameType)
{
// Arrange
DWORD key1 = VK_LCONTROL;
DWORD key2 = VK_CONTROL;
// Act
auto result = KeyboardManagerHelper::DoKeysOverlap(key1, key2);
// Assert
Assert::IsTrue(result == KeyboardManagerHelper::ErrorType::ConflictingModifierKey);
}
// Test if the DoKeysOverlap method returns ConflictingModifierKey on passing right modifier and common modifier
TEST_METHOD (DoKeysOverlap_ShouldReturnConflictingModifierKey_OnPassingRightModifierAndCommonModifierOfSameType)
{
// Arrange
DWORD key1 = VK_RCONTROL;
DWORD key2 = VK_CONTROL;
// Act
auto result = KeyboardManagerHelper::DoKeysOverlap(key1, key2);
// Assert
Assert::IsTrue(result == KeyboardManagerHelper::ErrorType::ConflictingModifierKey);
}
// Test if the DoKeysOverlap method returns NoError on passing left modifier and right modifier
TEST_METHOD (DoKeysOverlap_ShouldReturnNoError_OnPassingLeftModifierAndRightModifierOfSameType)
{
// Arrange
DWORD key1 = VK_LCONTROL;
DWORD key2 = VK_RCONTROL;
// Act
auto result = KeyboardManagerHelper::DoKeysOverlap(key1, key2);
// Assert
Assert::IsTrue(result == KeyboardManagerHelper::ErrorType::NoError);
}
// Test if the DoKeysOverlap method returns NoError on passing keys of different types
TEST_METHOD (DoKeysOverlap_ShouldReturnNoError_OnPassingKeysOfDifferentTypes)
{
// Arrange
DWORD key1 = VK_CONTROL;
DWORD key2 = VK_SHIFT;
// Act
auto result = KeyboardManagerHelper::DoKeysOverlap(key1, key2);
// Assert
Assert::IsTrue(result == KeyboardManagerHelper::ErrorType::NoError);
}
// Test if the DoKeysOverlap method returns NoError on passing different action keys
TEST_METHOD (DoKeysOverlap_ShouldReturnNoError_OnPassingDifferentActionKeys)
{
// Arrange
DWORD key1 = 0x41;
DWORD key2 = 0x42;
// Act
auto result = KeyboardManagerHelper::DoKeysOverlap(key1, key2);
// Assert
Assert::IsTrue(result == KeyboardManagerHelper::ErrorType::NoError);
}
// Test if the CheckRepeatedModifier method returns true on passing vector with same modifier repeated
TEST_METHOD (CheckRepeatedModifier_ShouldReturnTrue_OnPassingSameModifierRepeated)
{
// Arrange
std::vector<DWORD> keyList = LayoutMap().GetKeyCodeList(true);
std::vector<DWORD> keys = { VK_CONTROL, VK_CONTROL, 0x41 };
// Act
bool result = KeyboardManagerHelper::CheckRepeatedModifier(keys, TestHelpers::GetDropDownIndexFromDropDownList(VK_CONTROL, keyList), keyList);
// Assert
Assert::IsTrue(result);
}
// Test if the CheckRepeatedModifier method returns true on passing vector with conflicting modifier repeated
TEST_METHOD (CheckRepeatedModifier_ShouldReturnTrue_OnPassingConflictingModifierRepeated)
{
// Arrange
std::vector<DWORD> keyList = LayoutMap().GetKeyCodeList(true);
std::vector<DWORD> keys = { VK_CONTROL, VK_LCONTROL, 0x41 };
// Act
bool result = KeyboardManagerHelper::CheckRepeatedModifier(keys, TestHelpers::GetDropDownIndexFromDropDownList(VK_LCONTROL, keyList), keyList);
// Assert
Assert::IsTrue(result);
}
// Test if the CheckRepeatedModifier method returns false on passing vector with different modifiers
TEST_METHOD (CheckRepeatedModifier_ShouldReturnFalse_OnPassingDifferentModifiers)
{
// Arrange
std::vector<DWORD> keyList = LayoutMap().GetKeyCodeList(true);
std::vector<DWORD> keys = { VK_CONTROL, VK_SHIFT, 0x41 };
// Act
bool result = KeyboardManagerHelper::CheckRepeatedModifier(keys, TestHelpers::GetDropDownIndexFromDropDownList(VK_SHIFT, keyList), keyList);
// Assert
Assert::IsFalse(result);
}
// Test if the GetKeyCodesFromSelectedIndices method ignores -1 and 0 from argument in return value
TEST_METHOD (GetKeyCodesFromSelectedIndices_ShouldIgnoreMinus1And0_OnPassingArgumentWithMinus1Or0)
{
// Arrange
std::vector<DWORD> keyList = LayoutMap().GetKeyCodeList(true);
std::vector<int32_t> indices = { -1, 0, -1, 0 };
// Act
auto result = KeyboardManagerHelper::GetKeyCodesFromSelectedIndices(indices, keyList);
// Assert
Assert::IsTrue(result.empty());
}
// Test if the GetKeyCodesFromSelectedIndices method returns vector with key codes from vector with indices as per key code list
TEST_METHOD (GetKeyCodesFromSelectedIndices_ShouldReturnVectorWithKeyCodes_OnPassingVectorWithIndicesAsPerKeyCodeList)
{
// Arrange
std::vector<DWORD> keyList = LayoutMap().GetKeyCodeList(true);
std::vector<int32_t> indices = { TestHelpers::GetDropDownIndexFromDropDownList(0x41, keyList), TestHelpers::GetDropDownIndexFromDropDownList(0x42, keyList) };
// Act
auto result = KeyboardManagerHelper::GetKeyCodesFromSelectedIndices(indices, keyList);
// Assert
Assert::IsTrue(result == std::vector<DWORD>{ 0x41, 0x42 });
}
};
}

View File

@@ -107,6 +107,8 @@
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="AppSpecificShortcutRemappingTests.cpp" />
<ClCompile Include="BufferValidationTests.cpp" />
<ClCompile Include="LoadingAndSavingRemappingTests.cpp" />
<ClCompile Include="MockedInputSanityTests.cpp" />
<ClCompile Include="SetKeyEventTests.cpp" />
<ClCompile Include="OSLevelShortcutRemappingTests.cpp" />
@@ -114,7 +116,9 @@
<ClCompile Include="pch.cpp">
<PrecompiledHeader Condition="'$(CIBuild)'!='true'">Create</PrecompiledHeader>
</ClCompile>
<ClCompile Include="ShortcutTests.cpp" />
<ClCompile Include="SingleKeyRemappingTests.cpp" />
<ClCompile Include="KeyboardManagerHelperTests.cpp" />
<ClCompile Include="TestHelpers.cpp" />
</ItemGroup>
<ItemGroup>

View File

@@ -39,6 +39,18 @@
<ClCompile Include="AppSpecificShortcutRemappingTests.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="LoadingAndSavingRemappingTests.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="BufferValidationTests.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="KeyboardManagerHelperTests.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="ShortcutTests.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="pch.h">

View File

@@ -0,0 +1,521 @@
#include "pch.h"
#include "CppUnitTest.h"
#include <keyboardmanager/common/KeyboardManagerState.h>
#include <keyboardmanager/ui/LoadingAndSavingRemappingHelper.h>
#include "TestHelpers.h"
#include "../common/shared_constants.h"
using namespace Microsoft::VisualStudio::CppUnitTestFramework;
namespace RemappingUITests
{
// Tests for methods in the LoadingAndSavingRemappingHelper namespace
TEST_CLASS (LoadingAndSavingRemappingTests)
{
std::wstring testApp1 = L"testtrocess1.exe";
std::wstring testApp2 = L"testprocess2.exe";
public:
TEST_METHOD_INITIALIZE(InitializeTestEnv)
{
}
// Test if the CheckIfRemappingsAreValid method is successful when no remaps are passed
TEST_METHOD (CheckIfRemappingsAreValid_ShouldReturnNoError_OnPassingNoRemaps)
{
RemapBuffer remapBuffer;
// Assert that remapping set is valid
bool isSuccess = (LoadingAndSavingRemappingHelper::CheckIfRemappingsAreValid(remapBuffer) == KeyboardManagerHelper::ErrorType::NoError);
Assert::AreEqual(true, isSuccess);
}
// Test if the CheckIfRemappingsAreValid method is successful when valid key to key remaps are passed
TEST_METHOD (CheckIfRemappingsAreValid_ShouldReturnNoError_OnPassingValidKeyToKeyRemaps)
{
RemapBuffer remapBuffer;
// Remap A to B and B to C
remapBuffer.push_back(std::make_pair(RemapBufferItem({ 0x41, 0x42 }), std::wstring()));
remapBuffer.push_back(std::make_pair(RemapBufferItem({ 0x42, 0x43 }), std::wstring()));
// Assert that remapping set is valid
bool isSuccess = (LoadingAndSavingRemappingHelper::CheckIfRemappingsAreValid(remapBuffer) == KeyboardManagerHelper::ErrorType::NoError);
Assert::AreEqual(true, isSuccess);
}
// Test if the CheckIfRemappingsAreValid method is successful when valid key to shortcut remaps are passed
TEST_METHOD (CheckIfRemappingsAreValid_ShouldReturnNoError_OnPassingValidKeyToShortcutRemaps)
{
RemapBuffer remapBuffer;
// Remap A to Ctrl+V and B to Alt+Tab
Shortcut s1;
s1.SetKey(VK_CONTROL);
s1.SetKey(0x56);
Shortcut s2;
s2.SetKey(VK_MENU);
s2.SetKey(VK_TAB);
remapBuffer.push_back(std::make_pair(RemapBufferItem({ 0x41, s1 }), std::wstring()));
remapBuffer.push_back(std::make_pair(RemapBufferItem({ 0x42, s2 }), std::wstring()));
// Assert that remapping set is valid
bool isSuccess = (LoadingAndSavingRemappingHelper::CheckIfRemappingsAreValid(remapBuffer) == KeyboardManagerHelper::ErrorType::NoError);
Assert::AreEqual(true, isSuccess);
}
// Test if the CheckIfRemappingsAreValid method is successful when valid shortcut to key remaps are passed
TEST_METHOD (CheckIfRemappingsAreValid_ShouldReturnNoError_OnPassingValidShortcutToKeyRemaps)
{
RemapBuffer remapBuffer;
// Remap Ctrl+V to A and Alt+Tab to B
Shortcut s1;
s1.SetKey(VK_CONTROL);
s1.SetKey(0x56);
Shortcut s2;
s2.SetKey(VK_MENU);
s2.SetKey(VK_TAB);
remapBuffer.push_back(std::make_pair(RemapBufferItem({ s1, 0x41 }), std::wstring()));
remapBuffer.push_back(std::make_pair(RemapBufferItem({ s2, 0x42 }), std::wstring()));
// Assert that remapping set is valid
bool isSuccess = (LoadingAndSavingRemappingHelper::CheckIfRemappingsAreValid(remapBuffer) == KeyboardManagerHelper::ErrorType::NoError);
Assert::AreEqual(true, isSuccess);
}
// Test if the CheckIfRemappingsAreValid method is successful when valid shortcut to shortcut remaps are passed
TEST_METHOD (CheckIfRemappingsAreValid_ShouldReturnNoError_OnPassingValidShortcutToShortcutRemaps)
{
RemapBuffer remapBuffer;
// Remap Ctrl+V to Ctrl+D and Alt+Tab to Win+A
Shortcut src1;
src1.SetKey(VK_CONTROL);
src1.SetKey(0x56);
Shortcut dest1;
dest1.SetKey(VK_CONTROL);
dest1.SetKey(0x44);
Shortcut src2;
src2.SetKey(VK_MENU);
src2.SetKey(VK_TAB);
Shortcut dest2;
dest2.SetKey(CommonSharedConstants::VK_WIN_BOTH);
dest2.SetKey(0x41);
remapBuffer.push_back(std::make_pair(RemapBufferItem({ src1, dest1 }), std::wstring()));
remapBuffer.push_back(std::make_pair(RemapBufferItem({ src2, dest2 }), std::wstring()));
// Assert that remapping set is valid
bool isSuccess = (LoadingAndSavingRemappingHelper::CheckIfRemappingsAreValid(remapBuffer) == KeyboardManagerHelper::ErrorType::NoError);
Assert::AreEqual(true, isSuccess);
}
// Test if the CheckIfRemappingsAreValid method is successful when valid remaps are passed
TEST_METHOD (CheckIfRemappingsAreValid_ShouldReturnNoError_OnPassingValidRemapsOfAllTypes)
{
RemapBuffer remapBuffer;
// Remap Ctrl+V to Ctrl+D, Alt+Tab to A, A to B and B to Win+A
Shortcut src1;
src1.SetKey(VK_CONTROL);
src1.SetKey(0x56);
Shortcut dest1;
dest1.SetKey(VK_CONTROL);
dest1.SetKey(0x44);
Shortcut src2;
src2.SetKey(VK_MENU);
src2.SetKey(VK_TAB);
Shortcut dest2;
dest2.SetKey(CommonSharedConstants::VK_WIN_BOTH);
dest2.SetKey(0x41);
remapBuffer.push_back(std::make_pair(RemapBufferItem({ src1, dest1 }), std::wstring()));
remapBuffer.push_back(std::make_pair(RemapBufferItem({ src2, 0x41 }), std::wstring()));
remapBuffer.push_back(std::make_pair(RemapBufferItem({ 0x41, 0x42 }), std::wstring()));
remapBuffer.push_back(std::make_pair(RemapBufferItem({ 0x42, dest2 }), std::wstring()));
// Assert that remapping set is valid
bool isSuccess = (LoadingAndSavingRemappingHelper::CheckIfRemappingsAreValid(remapBuffer) == KeyboardManagerHelper::ErrorType::NoError);
Assert::AreEqual(true, isSuccess);
}
// Test if the CheckIfRemappingsAreValid method is unsuccessful when remaps with null keys are passed
TEST_METHOD (CheckIfRemappingsAreValid_ShouldReturnRemapUnsuccessful_OnPassingRemapsWithNullKeys)
{
RemapBuffer remapBuffer;
// Remap A to NULL
remapBuffer.push_back(std::make_pair(RemapBufferItem({ 0x41, NULL }), std::wstring()));
// Assert that remapping set is invalid
bool isSuccess = (LoadingAndSavingRemappingHelper::CheckIfRemappingsAreValid(remapBuffer) == KeyboardManagerHelper::ErrorType::RemapUnsuccessful);
Assert::AreEqual(true, isSuccess);
}
// Test if the CheckIfRemappingsAreValid method is unsuccessful when remaps with invalid shortcuts are passed
TEST_METHOD (CheckIfRemappingsAreValid_ShouldReturnRemapUnsuccessful_OnPassingRemapsWithInvalidShortcut)
{
RemapBuffer remapBuffer;
// Remap A to incomplete shortcut (Ctrl)
Shortcut src1;
src1.SetKey(VK_CONTROL);
remapBuffer.push_back(std::make_pair(RemapBufferItem({ 0x41, src1 }), std::wstring()));
// Assert that remapping set is invalid
bool isSuccess = (LoadingAndSavingRemappingHelper::CheckIfRemappingsAreValid(remapBuffer) == KeyboardManagerHelper::ErrorType::RemapUnsuccessful);
Assert::AreEqual(true, isSuccess);
}
// Test if the CheckIfRemappingsAreValid method is unsuccessful when remaps with the same key remapped twice are passed
TEST_METHOD (CheckIfRemappingsAreValid_ShouldReturnRemapUnsuccessful_OnPassingRemapsWithSameKeyRemappedTwice)
{
RemapBuffer remapBuffer;
// Remap A to B and A to Ctrl+C
Shortcut src1;
src1.SetKey(VK_CONTROL);
src1.SetKey(0x43);
remapBuffer.push_back(std::make_pair(RemapBufferItem({ 0x41, 0x42 }), std::wstring()));
remapBuffer.push_back(std::make_pair(RemapBufferItem({ 0x41, src1 }), std::wstring()));
// Assert that remapping set is invalid
bool isSuccess = (LoadingAndSavingRemappingHelper::CheckIfRemappingsAreValid(remapBuffer) == KeyboardManagerHelper::ErrorType::RemapUnsuccessful);
Assert::AreEqual(true, isSuccess);
}
// Test if the CheckIfRemappingsAreValid method is unsuccessful when remaps with the same shortcut remapped twice are passed
TEST_METHOD (CheckIfRemappingsAreValid_ShouldReturnRemapUnsuccessful_OnPassingRemapsWithSameShortcutRemappedTwice)
{
RemapBuffer remapBuffer;
// Remap Ctrl+A to B and Ctrl+A to Ctrl+V
Shortcut src1;
src1.SetKey(VK_CONTROL);
src1.SetKey(0x41);
Shortcut dest1;
dest1.SetKey(VK_CONTROL);
dest1.SetKey(0x56);
remapBuffer.push_back(std::make_pair(RemapBufferItem({ src1, 0x42 }), std::wstring()));
remapBuffer.push_back(std::make_pair(RemapBufferItem({ src1, dest1 }), std::wstring()));
// Assert that remapping set is invalid
bool isSuccess = (LoadingAndSavingRemappingHelper::CheckIfRemappingsAreValid(remapBuffer) == KeyboardManagerHelper::ErrorType::RemapUnsuccessful);
Assert::AreEqual(true, isSuccess);
}
// Test if the CheckIfRemappingsAreValid method is unsuccessful when app specific remaps with the same shortcut remapped twice for the same target app are passed
TEST_METHOD (CheckIfRemappingsAreValid_ShouldReturnRemapUnsuccessful_OnPassingAppSpecificRemapsWithSameShortcutRemappedTwiceForTheSameTargetApp)
{
RemapBuffer remapBuffer;
// Remap Ctrl+A to B and Ctrl+A to Ctrl+V for testApp1
Shortcut src1;
src1.SetKey(VK_CONTROL);
src1.SetKey(0x41);
Shortcut dest1;
dest1.SetKey(VK_CONTROL);
dest1.SetKey(0x56);
remapBuffer.push_back(std::make_pair(RemapBufferItem({ src1, 0x42 }), testApp1));
remapBuffer.push_back(std::make_pair(RemapBufferItem({ src1, dest1 }), testApp1));
// Assert that remapping set is invalid
bool isSuccess = (LoadingAndSavingRemappingHelper::CheckIfRemappingsAreValid(remapBuffer) == KeyboardManagerHelper::ErrorType::RemapUnsuccessful);
Assert::AreEqual(true, isSuccess);
}
// Test if the CheckIfRemappingsAreValid method is successful when app specific remaps with the same shortcut remapped twice for different target apps are passed
TEST_METHOD (CheckIfRemappingsAreValid_ShouldReturnNoError_OnPassingAppSpecificRemapsWithSameShortcutRemappedTwiceForDifferentTargetApps)
{
RemapBuffer remapBuffer;
// Remap Ctrl+A to B for testApp1 and Ctrl+A to Ctrl+V for testApp2
Shortcut src1;
src1.SetKey(VK_CONTROL);
src1.SetKey(0x41);
Shortcut dest1;
dest1.SetKey(VK_CONTROL);
dest1.SetKey(0x56);
remapBuffer.push_back(std::make_pair(RemapBufferItem({ src1, 0x42 }), testApp1));
remapBuffer.push_back(std::make_pair(RemapBufferItem({ src1, dest1 }), testApp2));
// Assert that remapping set is valid
bool isSuccess = (LoadingAndSavingRemappingHelper::CheckIfRemappingsAreValid(remapBuffer) == KeyboardManagerHelper::ErrorType::NoError);
Assert::AreEqual(true, isSuccess);
}
// Test if the GetOrphanedKeys method return an empty vector on passing no remaps
TEST_METHOD (GetOrphanedKeys_ShouldReturnEmptyVector_OnPassingNoRemaps)
{
RemapBuffer remapBuffer;
// Assert that there are no orphaned keys
Assert::AreEqual(true, LoadingAndSavingRemappingHelper::GetOrphanedKeys(remapBuffer).empty());
}
// Test if the GetOrphanedKeys method return one orphaned on passing one key remap
TEST_METHOD (GetOrphanedKeys_ShouldReturnOneOrphanedKey_OnPassingOneKeyRemap)
{
RemapBuffer remapBuffer;
// Remap A to B
remapBuffer.push_back(std::make_pair(RemapBufferItem({ 0x41, 0x42 }), std::wstring()));
// Assert that only A is orphaned
Assert::AreEqual((size_t)1, LoadingAndSavingRemappingHelper::GetOrphanedKeys(remapBuffer).size());
Assert::AreEqual((DWORD)0x41, LoadingAndSavingRemappingHelper::GetOrphanedKeys(remapBuffer)[0]);
}
// Test if the GetOrphanedKeys method return an empty vector on passing swapped key remaps
TEST_METHOD (GetOrphanedKeys_ShouldReturnEmptyVector_OnPassingSwappedKeyRemap)
{
RemapBuffer remapBuffer;
// Remap A to B and B to A
remapBuffer.push_back(std::make_pair(RemapBufferItem({ 0x41, 0x42 }), std::wstring()));
remapBuffer.push_back(std::make_pair(RemapBufferItem({ 0x42, 0x41 }), std::wstring()));
// Assert that there are no orphaned keys
Assert::AreEqual(true, LoadingAndSavingRemappingHelper::GetOrphanedKeys(remapBuffer).empty());
}
// Test if the GetOrphanedKeys method return one orphaned on passing two key remaps where one key is mapped to a remapped key
TEST_METHOD (GetOrphanedKeys_ShouldReturnOneOrphanedKey_OnPassingTwoKeyRemapsWhereOneKeyIsMappedToARemappedKey)
{
RemapBuffer remapBuffer;
// Remap A to Ctrl+B and C to A
Shortcut dest1;
dest1.SetKey(VK_CONTROL);
dest1.SetKey(0x42);
remapBuffer.push_back(std::make_pair(RemapBufferItem({ 0x41, dest1 }), std::wstring()));
remapBuffer.push_back(std::make_pair(RemapBufferItem({ 0x43, 0x41 }), std::wstring()));
// Assert that only C is orphaned
Assert::AreEqual((size_t)1, LoadingAndSavingRemappingHelper::GetOrphanedKeys(remapBuffer).size());
Assert::AreEqual((DWORD)0x43, LoadingAndSavingRemappingHelper::GetOrphanedKeys(remapBuffer)[0]);
}
// Test if the PreProcessRemapTable method combines all the modifier pairs when the left and right modifiers are remapped to the same target
TEST_METHOD (PreProcessRemapTable_ShouldCombineAllPairs_OnPassingLeftAndRightModifiersRemappedToTheSameTarget)
{
std::unordered_map<DWORD, std::variant<DWORD, Shortcut>> remapTable;
// Remap LCtrl and RCtrl to A, LAlt and RAlt to B, LShift and RShift to C, LWin and RWin to D
remapTable[VK_LCONTROL] = 0x41;
remapTable[VK_RCONTROL] = 0x41;
remapTable[VK_LMENU] = 0x42;
remapTable[VK_RMENU] = 0x42;
remapTable[VK_LSHIFT] = 0x43;
remapTable[VK_RSHIFT] = 0x43;
remapTable[VK_LWIN] = 0x44;
remapTable[VK_RWIN] = 0x44;
// Pre process table
LoadingAndSavingRemappingHelper::PreProcessRemapTable(remapTable);
// Expected Ctrl remapped to A, Alt to B, Shift to C, Win to D
std::unordered_map<DWORD, std::variant<DWORD, Shortcut>> expectedTable;
expectedTable[VK_CONTROL] = 0x41;
expectedTable[VK_MENU] = 0x42;
expectedTable[VK_SHIFT] = 0x43;
expectedTable[CommonSharedConstants::VK_WIN_BOTH] = 0x44;
bool areTablesEqual = (expectedTable == remapTable);
Assert::AreEqual(true, areTablesEqual);
}
// Test if the PreProcessRemapTable method does not combines any of the modifier pairs when the left and right modifiers are remapped to different targets
TEST_METHOD (PreProcessRemapTable_ShouldNotCombineAnyPairs_OnPassingLeftAndRightModifiersRemappedToTheDifferentTargets)
{
std::unordered_map<DWORD, std::variant<DWORD, Shortcut>> remapTable;
// Remap left modifiers to A and right modifiers to B
remapTable[VK_LCONTROL] = 0x41;
remapTable[VK_RCONTROL] = 0x42;
remapTable[VK_LMENU] = 0x41;
remapTable[VK_RMENU] = 0x42;
remapTable[VK_LSHIFT] = 0x41;
remapTable[VK_RSHIFT] = 0x42;
remapTable[VK_LWIN] = 0x41;
remapTable[VK_RWIN] = 0x42;
// Pre process table
LoadingAndSavingRemappingHelper::PreProcessRemapTable(remapTable);
// Expected unchanged table
std::unordered_map<DWORD, std::variant<DWORD, Shortcut>> expectedTable;
expectedTable[VK_LCONTROL] = 0x41;
expectedTable[VK_RCONTROL] = 0x42;
expectedTable[VK_LMENU] = 0x41;
expectedTable[VK_RMENU] = 0x42;
expectedTable[VK_LSHIFT] = 0x41;
expectedTable[VK_RSHIFT] = 0x42;
expectedTable[VK_LWIN] = 0x41;
expectedTable[VK_RWIN] = 0x42;
bool areTablesEqual = (expectedTable == remapTable);
Assert::AreEqual(true, areTablesEqual);
}
// Test if the ApplySingleKeyRemappings method resets the keyboard manager state's single key remappings on passing an empty buffer
TEST_METHOD (ApplySingleKeyRemappings_ShouldResetSingleKeyRemappings_OnPassingEmptyBuffer)
{
KeyboardManagerState testState;
RemapBuffer remapBuffer;
// Remap A to B
testState.AddSingleKeyRemap(0x41, 0x42);
// Apply the single key remaps from the buffer to the keyboard manager state variable
LoadingAndSavingRemappingHelper::ApplySingleKeyRemappings(testState, remapBuffer, false);
// Assert that single key remapping in the kbm state variable is empty
Assert::AreEqual((size_t)0, testState.singleKeyReMap.size());
}
// Test if the ApplySingleKeyRemappings method copies only the valid remappings to the keyboard manager state variable when some of the remappings are invalid
TEST_METHOD (ApplySingleKeyRemappings_ShouldCopyOnlyValidRemappings_OnPassingBufferWithSomeInvalidRemappings)
{
KeyboardManagerState testState;
RemapBuffer remapBuffer;
// Add A->B, B->Ctrl+V, C to incomplete shortcut and D to incomplete key remappings to the buffer
Shortcut s1;
s1.SetKey(VK_CONTROL);
s1.SetKey(0x56);
Shortcut s2;
s2.SetKey(VK_LMENU);
remapBuffer.push_back(std::make_pair(RemapBufferItem({ 0x41, 0x42 }), std::wstring()));
remapBuffer.push_back(std::make_pair(RemapBufferItem({ 0x42, s1 }), std::wstring()));
remapBuffer.push_back(std::make_pair(RemapBufferItem({ 0x43, NULL }), std::wstring()));
remapBuffer.push_back(std::make_pair(RemapBufferItem({ 0x44, s2 }), std::wstring()));
// Apply the single key remaps from the buffer to the keyboard manager state variable
LoadingAndSavingRemappingHelper::ApplySingleKeyRemappings(testState, remapBuffer, false);
// Expected A remapped to B, B remapped to Ctrl+V
std::unordered_map<DWORD, std::variant<DWORD, Shortcut>> expectedTable;
expectedTable[0x41] = 0x42;
expectedTable[0x42] = s1;
bool areTablesEqual = (expectedTable == testState.singleKeyReMap);
Assert::AreEqual(true, areTablesEqual);
}
// Test if the ApplySingleKeyRemappings method splits common modifiers to their left and right version when copying to the keyboard manager state variable if remappings from common modifiers are passed
TEST_METHOD (ApplySingleKeyRemappings_ShouldSplitRemappingsFromCommonModifiers_OnPassingBufferWithSomeMappingsFromCommonModifiers)
{
KeyboardManagerState testState;
RemapBuffer remapBuffer;
// Add Ctrl->A, Alt->B, Shift->C and Win->D remappings to the buffer
remapBuffer.push_back(std::make_pair(RemapBufferItem({ VK_CONTROL, 0x41 }), std::wstring()));
remapBuffer.push_back(std::make_pair(RemapBufferItem({ VK_MENU, 0x42 }), std::wstring()));
remapBuffer.push_back(std::make_pair(RemapBufferItem({ VK_SHIFT, 0x43 }), std::wstring()));
remapBuffer.push_back(std::make_pair(RemapBufferItem({ CommonSharedConstants::VK_WIN_BOTH, 0x44 }), std::wstring()));
// Apply the single key remaps from the buffer to the keyboard manager state variable
LoadingAndSavingRemappingHelper::ApplySingleKeyRemappings(testState, remapBuffer, false);
// Expected LCtrl/RCtrl remapped to A, LAlt/RAlt to B, LShift/RShift to C, LWin/RWin to D
std::unordered_map<DWORD, std::variant<DWORD, Shortcut>> expectedTable;
expectedTable[VK_LCONTROL] = 0x41;
expectedTable[VK_RCONTROL] = 0x41;
expectedTable[VK_LMENU] = 0x42;
expectedTable[VK_RMENU] = 0x42;
expectedTable[VK_LSHIFT] = 0x43;
expectedTable[VK_RSHIFT] = 0x43;
expectedTable[VK_LWIN] = 0x44;
expectedTable[VK_RWIN] = 0x44;
bool areTablesEqual = (expectedTable == testState.singleKeyReMap);
Assert::AreEqual(true, areTablesEqual);
}
// Test if the ApplyShortcutRemappings method resets the keyboard manager state's os level and app specific shortcut remappings on passing an empty buffer
TEST_METHOD (ApplyShortcutRemappings_ShouldResetShortcutRemappings_OnPassingEmptyBuffer)
{
KeyboardManagerState testState;
RemapBuffer remapBuffer;
// Remap Ctrl+A to Ctrl+B for all apps and Ctrl+C to Alt+V for testApp1
Shortcut src1;
src1.SetKey(VK_CONTROL);
src1.SetKey(0x41);
Shortcut dest1;
dest1.SetKey(VK_CONTROL);
dest1.SetKey(0x42);
Shortcut src2;
src2.SetKey(VK_CONTROL);
src2.SetKey(0x43);
Shortcut dest2;
dest2.SetKey(VK_MENU);
dest2.SetKey(0x56);
testState.AddOSLevelShortcut(src1, dest1);
testState.AddAppSpecificShortcut(testApp1, src1, dest1);
// Apply the shortcut remaps from the buffer to the keyboard manager state variable
LoadingAndSavingRemappingHelper::ApplyShortcutRemappings(testState, remapBuffer, false);
// Assert that shortcut remappings in the kbm state variable is empty
Assert::AreEqual((size_t)0, testState.osLevelShortcutReMap.size());
Assert::AreEqual((size_t)0, testState.appSpecificShortcutReMap.size());
}
// Test if the ApplyShortcutRemappings method copies only the valid remappings to the keyboard manager state variable when some of the remappings are invalid
TEST_METHOD (ApplyShortcutRemappings_ShouldCopyOnlyValidRemappings_OnPassingBufferWithSomeInvalidRemappings)
{
KeyboardManagerState testState;
RemapBuffer remapBuffer;
// Add Ctrl+A->Ctrl+B, Ctrl+C->Alt+V, Ctrl+F->incomplete shortcut and Ctrl+G->incomplete key os level remappings to buffer
// Add Ctrl+F->Alt+V, Ctrl+G->Ctrl+B, Ctrl+A->incomplete shortcut and Ctrl+C->incomplete key app specific remappings to buffer
Shortcut src1;
src1.SetKey(VK_CONTROL);
src1.SetKey(0x41);
Shortcut dest1;
dest1.SetKey(VK_CONTROL);
dest1.SetKey(0x42);
Shortcut src2;
src2.SetKey(VK_CONTROL);
src2.SetKey(0x43);
Shortcut dest2;
dest2.SetKey(VK_MENU);
dest2.SetKey(0x56);
Shortcut src3;
src3.SetKey(VK_CONTROL);
src3.SetKey(0x46);
Shortcut src4;
src4.SetKey(VK_CONTROL);
src4.SetKey(0x47);
Shortcut dest4;
dest4.SetKey(VK_CONTROL);
remapBuffer.push_back(std::make_pair(RemapBufferItem({ src1, dest1 }), std::wstring()));
remapBuffer.push_back(std::make_pair(RemapBufferItem({ src2, dest2 }), std::wstring()));
remapBuffer.push_back(std::make_pair(RemapBufferItem({ src3, NULL }), std::wstring()));
remapBuffer.push_back(std::make_pair(RemapBufferItem({ src4, dest4 }), std::wstring()));
remapBuffer.push_back(std::make_pair(RemapBufferItem({ src3, dest2 }), testApp1));
remapBuffer.push_back(std::make_pair(RemapBufferItem({ src4, dest1 }), testApp1));
remapBuffer.push_back(std::make_pair(RemapBufferItem({ src1, NULL }), testApp1));
remapBuffer.push_back(std::make_pair(RemapBufferItem({ src2, dest4 }), testApp1));
// Apply the shortcut remaps from the buffer to the keyboard manager state variable
LoadingAndSavingRemappingHelper::ApplyShortcutRemappings(testState, remapBuffer, false);
// Ctrl+A->Ctrl+B and Ctrl+C->Alt+V
std::map<Shortcut, RemapShortcut> expectedOSLevelTable;
expectedOSLevelTable[src1] = RemapShortcut(dest1);
expectedOSLevelTable[src2] = RemapShortcut(dest2);
// Ctrl+F->Alt+V and Ctrl+G->Ctrl+B for testApp1
std::map<std::wstring, std::map<Shortcut, RemapShortcut>> expectedAppSpecificLevelTable;
expectedAppSpecificLevelTable[testApp1][src3] = RemapShortcut(dest2);
expectedAppSpecificLevelTable[testApp1][src4] = RemapShortcut(dest1);
bool areOSLevelTablesEqual = (expectedOSLevelTable == testState.osLevelShortcutReMap);
bool areAppSpecificTablesEqual = (expectedAppSpecificLevelTable == testState.appSpecificShortcutReMap);
Assert::AreEqual(true, areOSLevelTablesEqual);
Assert::AreEqual(true, areAppSpecificTablesEqual);
}
};
}

View File

@@ -0,0 +1,176 @@
#include "pch.h"
#include "CppUnitTest.h"
#include <keyboardmanager/common/Shortcut.h>
#include <keyboardmanager/common/Helpers.h>
#include "TestHelpers.h"
#include "../common/keyboard_layout.h"
using namespace Microsoft::VisualStudio::CppUnitTestFramework;
namespace KeyboardManagerCommonTests
{
// Tests for methods in the Shortcut class
TEST_CLASS (KeyboardManagerHelperTests)
{
public:
TEST_METHOD_INITIALIZE(InitializeTestEnv)
{
}
// Test if the IsValidShortcut method returns false on passing shortcut with null action key
TEST_METHOD (IsValidShortcut_ShouldReturnFalse_OnPassingShortcutWithNullActionKey)
{
// Arrange
Shortcut s;
s.SetKey(NULL);
// Act
bool result = s.IsValidShortcut();
// Assert
Assert::IsFalse(result);
}
// Test if the IsValidShortcut method returns false on passing shortcut with only action key
TEST_METHOD (IsValidShortcut_ShouldReturnFalse_OnPassingShortcutWithOnlyActionKey)
{
// Arrange
Shortcut s;
s.SetKey(0x41);
// Act
bool result = s.IsValidShortcut();
// Assert
Assert::IsFalse(result);
}
// Test if the IsValidShortcut method returns false on passing shortcut with only modifier keys
TEST_METHOD (IsValidShortcut_ShouldReturnFalse_OnPassingShortcutWithOnlyModifierKeys)
{
// Arrange
Shortcut s;
s.SetKey(VK_CONTROL);
s.SetKey(VK_SHIFT);
// Act
bool result = s.IsValidShortcut();
// Assert
Assert::IsFalse(result);
}
// Test if the IsValidShortcut method returns true on passing shortcut with modifier and action key
TEST_METHOD (IsValidShortcut_ShouldReturnFalse_OnPassingShortcutWithModifierAndActionKey)
{
// Arrange
Shortcut s;
s.SetKey(VK_CONTROL);
s.SetKey(0x41);
// Act
bool result = s.IsValidShortcut();
// Assert
Assert::IsTrue(result);
}
// Test if the DoKeysOverlap method returns NoError on passing invalid shortcut for one of the arguments
TEST_METHOD (DoKeysOverlap_ShouldReturnNoError_OnPassingInvalidShortcutForOneOfTheArguments)
{
// Arrange
Shortcut s1(std::vector<DWORD>{ NULL });
Shortcut s2(std::vector<DWORD>{ VK_CONTROL, 0x41 });
// Act
auto result = Shortcut::DoKeysOverlap(s1, s2);
// Assert
Assert::IsTrue(result == KeyboardManagerHelper::ErrorType::NoError);
}
// Test if the DoKeysOverlap method returns SameShortcutPreviouslyMapped on passing same shortcut for both arguments
TEST_METHOD (DoKeysOverlap_ShouldReturnSameShortcutPreviouslyMapped_OnPassingSameShortcutForBothArguments)
{
// Arrange
Shortcut s1(std::vector<DWORD>{ VK_CONTROL, 0x41 });
Shortcut s2 = s1;
// Act
auto result = Shortcut::DoKeysOverlap(s1, s2);
// Assert
Assert::IsTrue(result == KeyboardManagerHelper::ErrorType::SameShortcutPreviouslyMapped);
}
// Test if the DoKeysOverlap method returns NoError on passing shortcuts with different action keys
TEST_METHOD (DoKeysOverlap_ShouldReturnNoError_OnPassingShortcutsWithDifferentActionKeys)
{
// Arrange
Shortcut s1(std::vector<DWORD>{ VK_CONTROL, 0x42 });
Shortcut s2(std::vector<DWORD>{ VK_CONTROL, 0x41 });
// Act
auto result = Shortcut::DoKeysOverlap(s1, s2);
// Assert
Assert::IsTrue(result == KeyboardManagerHelper::ErrorType::NoError);
}
// Test if the DoKeysOverlap method returns NoError on passing shortcuts with different modifiers
TEST_METHOD (DoKeysOverlap_ShouldReturnNoError_OnPassingShortcutsWithDifferentModifiers)
{
// Arrange
Shortcut s1(std::vector<DWORD>{ VK_CONTROL, 0x42 });
Shortcut s2(std::vector<DWORD>{ VK_SHIFT, 0x42 });
// Act
auto result = Shortcut::DoKeysOverlap(s1, s2);
// Assert
Assert::IsTrue(result == KeyboardManagerHelper::ErrorType::NoError);
}
// Test if the DoKeysOverlap method returns ConflictingModifierShortcut on passing shortcuts with left modifier and common modifier
TEST_METHOD (DoKeysOverlap_ShouldReturnConflictingModifierShortcut_OnPassingShortcutsWithLeftModifierAndCommonModifierOfSameType)
{
// Arrange
Shortcut s1(std::vector<DWORD>{ VK_LCONTROL, 0x42 });
Shortcut s2(std::vector<DWORD>{ VK_CONTROL, 0x42 });
// Act
auto result = Shortcut::DoKeysOverlap(s1, s2);
// Assert
Assert::IsTrue(result == KeyboardManagerHelper::ErrorType::ConflictingModifierShortcut);
}
// Test if the DoKeysOverlap method returns ConflictingModifierShortcut on passing shortcuts with right modifier and common modifier
TEST_METHOD (DoKeysOverlap_ShouldReturnConflictingModifierShortcut_OnPassingShortcutsWithRightModifierAndCommonModifierOfSameType)
{
// Arrange
Shortcut s1(std::vector<DWORD>{ VK_RCONTROL, 0x42 });
Shortcut s2(std::vector<DWORD>{ VK_CONTROL, 0x42 });
// Act
auto result = Shortcut::DoKeysOverlap(s1, s2);
// Assert
Assert::IsTrue(result == KeyboardManagerHelper::ErrorType::ConflictingModifierShortcut);
}
// Test if the DoKeysOverlap method returns ConflictingModifierShortcut on passing shortcuts with left modifier and right modifier
TEST_METHOD (DoKeysOverlap_ShouldReturnConflictingModifierShortcut_OnPassingShortcutsWithLeftModifierAndRightModifierOfSameType)
{
// Arrange
Shortcut s1(std::vector<DWORD>{ VK_LCONTROL, 0x42 });
Shortcut s2(std::vector<DWORD>{ VK_RCONTROL, 0x42 });
// Act
auto result = Shortcut::DoKeysOverlap(s1, s2);
// Assert
Assert::IsTrue(result == KeyboardManagerHelper::ErrorType::NoError);
}
};
}

View File

@@ -22,4 +22,10 @@ namespace TestHelpers
state.SetActivatedApp(maxLengthString);
state.SetActivatedApp(KeyboardManagerConstants::NoActivatedApp);
}
// Function to return the index of the given key code from the drop down key list
int GetDropDownIndexFromDropDownList(DWORD key, const std::vector<DWORD>& keyList)
{
return (int)std::distance(keyList.begin(), std::find(keyList.begin(), keyList.end(), key));
}
}

View File

@@ -6,4 +6,7 @@ namespace TestHelpers
{
// Function to reset the environment variables for tests
void ResetTestEnv(MockedInput& input, KeyboardManagerState& state);
// Function to return the index of the given key code from the drop down key list
int GetDropDownIndexFromDropDownList(DWORD key, const std::vector<DWORD>& keyList);
}

View File

@@ -6,3 +6,4 @@
#include <shlwapi.h>
#include <stdexcept>
#include <unordered_set>
#include "winrt/Windows.Foundation.h"