/ src / modules / keyboardmanager / KeyboardManagerEditorTest / EditorHelpersTests.cpp
EditorHelpersTests.cpp
  1  #include "pch.h"
  2  
  3  // Suppressing 26466 - Don't use static_cast downcasts - in CppUnitTest.h
  4  #pragma warning(push)
  5  #pragma warning(disable : 26466)
  6  #include "CppUnitTest.h"
  7  #pragma warning(pop)
  8  
  9  #include <keyboardmanager/KeyboardManagerEditorLibrary/ShortcutErrorType.h>
 10  #include <keyboardmanager/common/Helpers.h>
 11  #include <common/interop/keyboard_layout.h>
 12  #include <keyboardmanager/KeyboardManagerEditorLibrary/EditorHelpers.h>
 13  
 14  using namespace Microsoft::VisualStudio::CppUnitTestFramework;
 15  
 16  namespace EditorHelpersTests
 17  {
 18      TEST_CLASS (EditorHelpersTests)
 19      {
 20      public:
 21          // Test if the DoKeysOverlap method returns SameKeyPreviouslyMapped on passing the same key for both arguments
 22          TEST_METHOD (DoKeysOverlap_ShouldReturnSameKeyPreviouslyMapped_OnPassingSameKeyForBothArguments)
 23          {
 24              // Arrange
 25              DWORD key1 = 0x41;
 26              DWORD key2 = key1;
 27  
 28              // Act
 29              auto result = EditorHelpers::DoKeysOverlap(key1, key2);
 30  
 31              // Assert
 32              Assert::IsTrue(result == ShortcutErrorType::SameKeyPreviouslyMapped);
 33          }
 34  
 35          // Test if the DoKeysOverlap method returns ConflictingModifierKey on passing left modifier and common modifier
 36          TEST_METHOD (DoKeysOverlap_ShouldReturnConflictingModifierKey_OnPassingLeftModifierAndCommonModifierOfSameType)
 37          {
 38              // Arrange
 39              DWORD key1 = VK_LCONTROL;
 40              DWORD key2 = VK_CONTROL;
 41  
 42              // Act
 43              auto result = EditorHelpers::DoKeysOverlap(key1, key2);
 44  
 45              // Assert
 46              Assert::IsTrue(result == ShortcutErrorType::ConflictingModifierKey);
 47          }
 48  
 49          // Test if the DoKeysOverlap method returns ConflictingModifierKey on passing right modifier and common modifier
 50          TEST_METHOD (DoKeysOverlap_ShouldReturnConflictingModifierKey_OnPassingRightModifierAndCommonModifierOfSameType)
 51          {
 52              // Arrange
 53              DWORD key1 = VK_RCONTROL;
 54              DWORD key2 = VK_CONTROL;
 55  
 56              // Act
 57              auto result = EditorHelpers::DoKeysOverlap(key1, key2);
 58  
 59              // Assert
 60              Assert::IsTrue(result == ShortcutErrorType::ConflictingModifierKey);
 61          }
 62  
 63          // Test if the DoKeysOverlap method returns NoError on passing left modifier and right modifier
 64          TEST_METHOD (DoKeysOverlap_ShouldReturnNoError_OnPassingLeftModifierAndRightModifierOfSameType)
 65          {
 66              // Arrange
 67              DWORD key1 = VK_LCONTROL;
 68              DWORD key2 = VK_RCONTROL;
 69  
 70              // Act
 71              auto result = EditorHelpers::DoKeysOverlap(key1, key2);
 72  
 73              // Assert
 74              Assert::IsTrue(result == ShortcutErrorType::NoError);
 75          }
 76  
 77          // Test if the DoKeysOverlap method returns NoError on passing keys of different types
 78          TEST_METHOD (DoKeysOverlap_ShouldReturnNoError_OnPassingKeysOfDifferentTypes)
 79          {
 80              // Arrange
 81              DWORD key1 = VK_CONTROL;
 82              DWORD key2 = VK_SHIFT;
 83  
 84              // Act
 85              auto result = EditorHelpers::DoKeysOverlap(key1, key2);
 86  
 87              // Assert
 88              Assert::IsTrue(result == ShortcutErrorType::NoError);
 89          }
 90  
 91          // Test if the DoKeysOverlap method returns NoError on passing different action keys
 92          TEST_METHOD (DoKeysOverlap_ShouldReturnNoError_OnPassingDifferentActionKeys)
 93          {
 94              // Arrange
 95              DWORD key1 = 0x41;
 96              DWORD key2 = 0x42;
 97  
 98              // Act
 99              auto result = EditorHelpers::DoKeysOverlap(key1, key2);
100  
101              // Assert
102              Assert::IsTrue(result == ShortcutErrorType::NoError);
103          }
104  
105          // Test if the CheckRepeatedModifier method returns true on passing vector with same modifier repeated
106          TEST_METHOD (CheckRepeatedModifier_ShouldReturnTrue_OnPassingSameModifierRepeated)
107          {
108              // Arrange
109              std::vector<int32_t> keys = { VK_CONTROL, VK_CONTROL, 0x41 };
110  
111              // Act
112              bool result = EditorHelpers::CheckRepeatedModifier(keys, VK_CONTROL);
113  
114              // Assert
115              Assert::IsTrue(result);
116          }
117  
118          // Test if the CheckRepeatedModifier method returns true on passing vector with conflicting modifier repeated
119          TEST_METHOD (CheckRepeatedModifier_ShouldReturnTrue_OnPassingConflictingModifierRepeated)
120          {
121              // Arrange
122              std::vector<int32_t> keys = { VK_CONTROL, VK_LCONTROL, 0x41 };
123  
124              // Act
125              bool result = EditorHelpers::CheckRepeatedModifier(keys, VK_LCONTROL);
126  
127              // Assert
128              Assert::IsTrue(result);
129          }
130  
131          // Test if the CheckRepeatedModifier method returns false on passing vector with different modifiers
132          TEST_METHOD (CheckRepeatedModifier_ShouldReturnFalse_OnPassingDifferentModifiers)
133          {
134              // Arrange
135              std::vector<int32_t> keys = { VK_CONTROL, VK_SHIFT, 0x41 };
136  
137              // Act
138              bool result = EditorHelpers::CheckRepeatedModifier(keys, VK_SHIFT);
139  
140              // Assert
141              Assert::IsFalse(result);
142          }
143  
144          
145          // Test if the IsValidShortcut method returns false on passing shortcut with null action key
146          TEST_METHOD (IsValidShortcut_ShouldReturnFalse_OnPassingShortcutWithNullActionKey)
147          {
148              // Arrange
149              Shortcut s;
150              s.SetKey(NULL);
151  
152              // Act
153              bool result = EditorHelpers::IsValidShortcut(s);
154  
155              // Assert
156              Assert::IsFalse(result);
157          }
158  
159          // Test if the IsValidShortcut method returns false on passing shortcut with only action key
160          TEST_METHOD (IsValidShortcut_ShouldReturnFalse_OnPassingShortcutWithOnlyActionKey)
161          {
162              // Arrange
163              Shortcut s;
164              s.SetKey(0x41);
165  
166              // Act
167              bool result = EditorHelpers::IsValidShortcut(s);
168  
169              // Assert
170              Assert::IsFalse(result);
171          }
172  
173          // Test if the IsValidShortcut method returns false on passing shortcut with only modifier keys
174          TEST_METHOD (IsValidShortcut_ShouldReturnFalse_OnPassingShortcutWithOnlyModifierKeys)
175          {
176              // Arrange
177              Shortcut s;
178              s.SetKey(VK_CONTROL);
179              s.SetKey(VK_SHIFT);
180  
181              // Act
182              bool result = EditorHelpers::IsValidShortcut(s);
183  
184              // Assert
185              Assert::IsFalse(result);
186          }
187  
188          // Test if the IsValidShortcut method returns true on passing shortcut with modifier and action key
189          TEST_METHOD (IsValidShortcut_ShouldReturnFalse_OnPassingShortcutWithModifierAndActionKey)
190          {
191              // Arrange
192              Shortcut s;
193              s.SetKey(VK_CONTROL);
194              s.SetKey(0x41);
195  
196              // Act
197              bool result = EditorHelpers::IsValidShortcut(s);
198  
199              // Assert
200              Assert::IsTrue(result);
201          }
202  
203          // Test if the DoKeysOverlap method returns NoError on passing invalid shortcut for one of the arguments
204          TEST_METHOD (DoKeysOverlap_ShouldReturnNoError_OnPassingInvalidShortcutForOneOfTheArguments)
205          {
206              // Arrange
207              Shortcut s1(std::vector<int32_t>{ NULL });
208              Shortcut s2(std::vector<int32_t>{ VK_CONTROL, 0x41 });
209  
210              // Act
211              auto result = EditorHelpers::DoShortcutsOverlap(s1, s2);
212  
213              // Assert
214              Assert::IsTrue(result == ShortcutErrorType::NoError);
215          }
216  
217          // Test if the DoKeysOverlap method returns SameShortcutPreviouslyMapped on passing same shortcut for both arguments
218          TEST_METHOD (DoKeysOverlap_ShouldReturnSameShortcutPreviouslyMapped_OnPassingSameShortcutForBothArguments)
219          {
220              // Arrange
221              Shortcut s1(std::vector<int32_t>{ VK_CONTROL, 0x41 });
222              Shortcut s2 = s1;
223  
224              // Act
225              auto result = EditorHelpers::DoShortcutsOverlap(s1, s2);
226  
227              // Assert
228              Assert::IsTrue(result == ShortcutErrorType::SameShortcutPreviouslyMapped);
229          }
230  
231          // Test if the DoKeysOverlap method returns NoError on passing shortcuts with different action keys
232          TEST_METHOD (DoKeysOverlap_ShouldReturnNoError_OnPassingShortcutsWithDifferentActionKeys)
233          {
234              // Arrange
235              Shortcut s1(std::vector<int32_t>{ VK_CONTROL, 0x42 });
236              Shortcut s2(std::vector<int32_t>{ VK_CONTROL, 0x41 });
237  
238              // Act
239              auto result = EditorHelpers::DoShortcutsOverlap(s1, s2);
240  
241              // Assert
242              Assert::IsTrue(result == ShortcutErrorType::NoError);
243          }
244  
245          // Test if the DoKeysOverlap method returns NoError on passing shortcuts with different modifiers
246          TEST_METHOD (DoKeysOverlap_ShouldReturnNoError_OnPassingShortcutsWithDifferentModifiers)
247          {
248              // Arrange
249              Shortcut s1(std::vector<int32_t>{ VK_CONTROL, 0x42 });
250              Shortcut s2(std::vector<int32_t>{ VK_SHIFT, 0x42 });
251  
252              // Act
253              auto result = EditorHelpers::DoShortcutsOverlap(s1, s2);
254  
255              // Assert
256              Assert::IsTrue(result == ShortcutErrorType::NoError);
257          }
258  
259          // Test if the DoKeysOverlap method returns ConflictingModifierShortcut on passing shortcuts with left modifier and common modifier
260          TEST_METHOD (DoKeysOverlap_ShouldReturnConflictingModifierShortcut_OnPassingShortcutsWithLeftModifierAndCommonModifierOfSameType)
261          {
262              // Arrange
263              Shortcut s1(std::vector<int32_t>{ VK_LCONTROL, 0x42 });
264              Shortcut s2(std::vector<int32_t>{ VK_CONTROL, 0x42 });
265  
266              // Act
267              auto result = EditorHelpers::DoShortcutsOverlap(s1, s2);
268  
269              // Assert
270              Assert::IsTrue(result == ShortcutErrorType::ConflictingModifierShortcut);
271          }
272  
273          // Test if the DoKeysOverlap method returns ConflictingModifierShortcut on passing shortcuts with right modifier and common modifier
274          TEST_METHOD (DoKeysOverlap_ShouldReturnConflictingModifierShortcut_OnPassingShortcutsWithRightModifierAndCommonModifierOfSameType)
275          {
276              // Arrange
277              Shortcut s1(std::vector<int32_t>{ VK_RCONTROL, 0x42 });
278              Shortcut s2(std::vector<int32_t>{ VK_CONTROL, 0x42 });
279  
280              // Act
281              auto result = EditorHelpers::DoShortcutsOverlap(s1, s2);
282  
283              // Assert
284              Assert::IsTrue(result == ShortcutErrorType::ConflictingModifierShortcut);
285          }
286  
287          // Test if the DoKeysOverlap method returns ConflictingModifierShortcut on passing shortcuts with left modifier and right modifier
288          TEST_METHOD (DoKeysOverlap_ShouldReturnConflictingModifierShortcut_OnPassingShortcutsWithLeftModifierAndRightModifierOfSameType)
289          {
290              // Arrange
291              Shortcut s1(std::vector<int32_t>{ VK_LCONTROL, 0x42 });
292              Shortcut s2(std::vector<int32_t>{ VK_RCONTROL, 0x42 });
293  
294              // Act
295              auto result = EditorHelpers::DoShortcutsOverlap(s1, s2);
296  
297              // Assert
298              Assert::IsTrue(result == ShortcutErrorType::NoError);
299          }
300      };
301  }