test_fixture.hpp 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /* Copyright 2017 Fred Sundvik
  2. *
  3. * This program is free software: you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation, either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #pragma once
  17. #include <cstdint>
  18. #include <unordered_map>
  19. #include <optional>
  20. #include "gtest/gtest.h"
  21. #include "keyboard.h"
  22. #include "test_keymap_key.hpp"
  23. class TestFixture : public testing::Test {
  24. public:
  25. static TestFixture* m_this;
  26. TestFixture();
  27. ~TestFixture();
  28. static void SetUpTestCase();
  29. static void TearDownTestCase();
  30. void set_keymap(std::initializer_list<KeymapKey> keycodes);
  31. void add_key(const KeymapKey key);
  32. const KeymapKey* find_key(const layer_t layer_t, const keypos_t position) const;
  33. void get_keycode(const layer_t layer, const keypos_t position, uint16_t* result) const;
  34. /**
  35. * @brief Taps `key` with `delay_ms` delay between press and release.
  36. */
  37. void tap_key(KeymapKey key, unsigned delay_ms = 1);
  38. /**
  39. * @brief Taps multiple KeymapKey keys in order, e.g. `tap_keys(key_a, key_b)`.
  40. */
  41. template <typename... Ts>
  42. void tap_keys(Ts... keys) {
  43. for (KeymapKey key : {keys...}) {
  44. tap_key(key);
  45. }
  46. }
  47. /**
  48. * @brief Taps a combo with `delay_ms` delay between press and release.
  49. *
  50. * Example: `tap_combo({key_a, key_b})` to tap the chord A + B.
  51. */
  52. void tap_combo(const std::vector<KeymapKey>& chord_keys, unsigned delay_ms = 1);
  53. void run_one_scan_loop();
  54. void idle_for(unsigned ms);
  55. void expect_layer_state(layer_t layer) const;
  56. protected:
  57. void print_test_log() const;
  58. std::vector<KeymapKey> keymap;
  59. };