KeyboardKeysTest.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. /*
  3. * This file is part of Chrome PHP.
  4. *
  5. * (c) Soufiane Ghzal <sghzal@gmail.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace HeadlessChromium\Test;
  11. /**
  12. * @covers \HeadlessChromium\Input\KeyboardKeys
  13. */
  14. class KeyboardKeysTest extends BaseTestCase
  15. {
  16. private $keyboard;
  17. protected function setUp(): void
  18. {
  19. $this->keyboard = new KeyboardForTests();
  20. }
  21. public function keyProvider(): array
  22. {
  23. return [
  24. // Key, expectedKey
  25. ['a', 'A'],
  26. ['A', 'A'],
  27. ['key', 'Key'],
  28. [' KEY ', 'KEY'],
  29. ];
  30. }
  31. public function modifierKeyProvider(): array
  32. {
  33. return [
  34. // Key, expectedModifier
  35. ['Alt', 1],
  36. ['AltGr', 1],
  37. ['Alt Gr', 1],
  38. ['Control', 2],
  39. ['ctrl', 2],
  40. ['Ctr', 2],
  41. ['Meta', 4],
  42. ['Cmd', 4],
  43. ['Command', 4],
  44. ['Shift', 8],
  45. ];
  46. }
  47. public function keyCodesProvider(): array
  48. {
  49. return [
  50. ['a', 65],
  51. ['A', 65],
  52. ];
  53. }
  54. /**
  55. * @dataProvider keyProvider
  56. */
  57. public function testOnKeyPressAndRelease($key, $expectedKey): void
  58. {
  59. self::assertFalse($this->keyboard->isKeyPressed());
  60. self::assertEquals(0, $this->keyboard->getModifiers());
  61. $this->keyboard->onKeyPress($key);
  62. self::assertEquals($expectedKey, $this->keyboard->getCurrentKey());
  63. self::assertEquals(0, $this->keyboard->getModifiers());
  64. self::assertEquals(1, \count($this->keyboard->getPressedKeys()));
  65. self::assertTrue($this->keyboard->isKeyPressed());
  66. $this->keyboard->onKeyRelease($key);
  67. self::assertEquals($expectedKey, $this->keyboard->getCurrentKey());
  68. self::assertEquals(0, \count($this->keyboard->getPressedKeys()));
  69. self::assertEquals(0, $this->keyboard->getModifiers());
  70. self::assertFalse($this->keyboard->isKeyPressed());
  71. }
  72. /**
  73. * @dataProvider modifierKeyProvider
  74. */
  75. public function testToggleModifierFromKey($key, $expectedModifier): void
  76. {
  77. $this->keyboard->setCurrentKey($key);
  78. $this->keyboard->toggleModifierFromKey();
  79. $this->keyboard->setCurrentKey('NonModifierKey');
  80. $this->keyboard->toggleModifierFromKey();
  81. self::assertEquals($expectedModifier, $this->keyboard->getModifiers());
  82. $this->keyboard->setCurrentKey($key);
  83. $this->keyboard->toggleModifierFromKey();
  84. self::assertEquals(0, $this->keyboard->getModifiers());
  85. }
  86. public function testToggleModifier(): void
  87. {
  88. $this->keyboard->toggleModifier(0b0001);
  89. $this->keyboard->toggleModifier(0b0010);
  90. self::assertEquals(0b0011, $this->keyboard->getModifiers());
  91. $this->keyboard->toggleModifier(0b0010);
  92. self::assertEquals(0b0001, $this->keyboard->getModifiers());
  93. }
  94. /**
  95. * @dataProvider keyCodesProvider
  96. */
  97. public function testGetKeyCode($key, $code): void
  98. {
  99. $this->keyboard->setCurrentKey($key);
  100. self::assertEquals($code, $this->keyboard->getKeyCode());
  101. }
  102. }