Parent directory

test_fixture.hpp

2196 bytes
 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
17#pragma once
18
19#include <cstdint>
20#include <unordered_map>
21#include <optional>
22#include "gtest/gtest.h"
23#include "keyboard.h"
24#include "test_keymap_key.hpp"
25
26class TestFixture : public testing::Test {
27   public:
28    static TestFixture* m_this;
29
30    TestFixture();
31    ~TestFixture();
32    static void SetUpTestCase();
33    static void TearDownTestCase();
34
35    void set_keymap(std::initializer_list<KeymapKey> keycodes);
36    void add_key(const KeymapKey key);
37
38    const KeymapKey* find_key(const layer_t layer_t, const keypos_t position) const;
39    void             get_keycode(const layer_t layer, const keypos_t position, uint16_t* result) const;
40
41    /**
42     * @brief Taps `key` with `delay_ms` delay between press and release.
43     */
44    void tap_key(KeymapKey key, unsigned delay_ms = 1);
45
46    /**
47     * @brief Taps multiple KeymapKey keys in order, e.g. `tap_keys(key_a, key_b)`.
48     */
49    template <typename... Ts>
50    void tap_keys(Ts... keys) {
51        for (KeymapKey key : {keys...}) {
52            tap_key(key);
53        }
54    }
55
56    /**
57     * @brief Taps a combo with `delay_ms` delay between press and release.
58     *
59     * Example: `tap_combo({key_a, key_b})` to tap the chord A + B.
60     */
61    void tap_combo(const std::vector<KeymapKey>& chord_keys, unsigned delay_ms = 1);
62
63    void run_one_scan_loop();
64    void idle_for(unsigned ms);
65
66    void expect_layer_state(layer_t layer) const;
67
68   protected:
69    void                   print_test_log() const;
70    std::vector<KeymapKey> keymap;
71};