summaryrefslogtreecommitdiff
path: root/tests/test_common/test_fixture.cpp
blob: 44694cd390c32a5ac0859019f551cdad641bb03a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#include "test_fixture.hpp"
#include <algorithm>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include "gmock/gmock-cardinalities.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "keyboard_report_util.hpp"
#include "keycode.h"
#include "test_driver.hpp"
#include "test_logger.hpp"
#include "test_matrix.h"
#include "test_keymap_key.hpp"

extern "C" {
#include "action.h"
#include "action_tapping.h"
#include "action_util.h"
#include "action_layer.h"
#include "debug.h"
#include "eeconfig.h"
#include "keyboard.h"
#include "keymap.h"

void set_time(uint32_t t);
void advance_time(uint32_t ms);
}

using testing::_;

/* This is used for dynamic dispatching keymap_key_to_keycode calls to the current active test_fixture. */
TestFixture* TestFixture::m_this = nullptr;

/* Override weak QMK function to allow the usage of isolated per-test keymaps in unit-tests.
 * The actual call is dynamicaly dispatched to the current active test fixture, which in turn has it's own keymap. */
extern "C" uint16_t keymap_key_to_keycode(uint8_t layer, keypos_t position) {
    uint16_t keycode;
    TestFixture::m_this->get_keycode(layer, position, &keycode);
    return keycode;
}

void TestFixture::SetUpTestCase() {
    test_logger.info() << "TestFixture setup-up start." << std::endl;

    // The following is enough to bootstrap the values set in main
    eeconfig_init_quantum();
    eeconfig_update_debug(debug_config.raw);

    TestDriver driver;
    keyboard_init();

    test_logger.info() << "TestFixture setup-up end." << std::endl;
}

void TestFixture::TearDownTestCase() {}

TestFixture::TestFixture() {
    m_this = this;
}

TestFixture::~TestFixture() {
    test_logger.info() << "TestFixture clean-up start." << std::endl;
    TestDriver driver;

    /* Reset keyboard state. */
    clear_all_keys();

    clear_keyboard();

    clear_oneshot_mods();
    clear_oneshot_locked_mods();
    reset_oneshot_layer();

    layer_clear();

#if defined(SWAP_HANDS_ENABLE)
    clear_oneshot_swaphands();
#endif

    idle_for(TAPPING_TERM * 10);
    testing::Mock::VerifyAndClearExpectations(&driver);

    /* Verify that the matrix really is cleared */
    EXPECT_NO_REPORT(driver);
    idle_for(TAPPING_TERM * 10);
    testing::Mock::VerifyAndClearExpectations(&driver);

    m_this = nullptr;

    test_logger.info() << "TestFixture clean-up end." << std::endl;

    print_test_log();
}

void TestFixture::add_key(KeymapKey key) {
    if (this->find_key(key.layer, key.position)) {
        FAIL() << "Key is already mapped for layer " << +key.layer << " and (column,row) (" << +key.position.col << "," << +key.position.row << ")";
    }

    this->keymap.push_back(key);
}

void TestFixture::tap_key(KeymapKey key, unsigned delay_ms) {
    key.press();
    idle_for(delay_ms);
    key.release();
    run_one_scan_loop();
}

void TestFixture::tap_combo(const std::vector<KeymapKey>& chord_keys, unsigned delay_ms) {
    for (KeymapKey key : chord_keys) { // Press each key.
        key.press();
        run_one_scan_loop();
    }

    if (delay_ms > 1) {
        idle_for(delay_ms - 1);
    }

    for (KeymapKey key : chord_keys) { // Release each key.
        key.release();
        run_one_scan_loop();
    }
}

void TestFixture::set_keymap(std::initializer_list<KeymapKey> keys) {
    this->keymap.clear();
    for (auto& key : keys) {
        add_key(key);
    }
}

const KeymapKey* TestFixture::find_key(layer_t layer, keypos_t position) const {
    auto keymap_key_predicate = [&](KeymapKey candidate) { return candidate.layer == layer && candidate.position.col == position.col && candidate.position.row == position.row; };

    auto result = std::find_if(this->keymap.begin(), this->keymap.end(), keymap_key_predicate);

    if (result != std::end(this->keymap)) {
        return &(*result);
    }
    return nullptr;
}

void TestFixture::get_keycode(const layer_t layer, const keypos_t position, uint16_t* result) const {
    bool key_is_out_of_bounds = position.col >= MATRIX_COLS && position.row >= MATRIX_ROWS;

    if (key_is_out_of_bounds) {
        /* See if this is done in hardware as well, because this is 100% out of bounds reads on all QMK keebs out there. */
        auto msg = [&]() {
            std::stringstream msg;
            msg << "Keycode for position (" << +position.col << "," << +position.row << ") requested! This is out of bounds." << std::endl;
            return msg.str();
        }();

        *result = KC_NO;
        test_logger.error() << msg;
        EXPECT_FALSE(key_is_out_of_bounds) << msg;
        return;
    }

    if (auto key = this->find_key(layer, position)) {
        *result = key->code;
        return;
    }

    FAIL() << "No key is mapped for layer " << +layer << " and (column,row) " << +position.col << "," << +position.row << ")";
}

void TestFixture::run_one_scan_loop() {
    keyboard_task();
    advance_time(1);
}

void TestFixture::idle_for(unsigned time) {
    for (unsigned i = 0; i < time; i++) {
        run_one_scan_loop();
    }
}

void TestFixture::print_test_log() const {
    const ::testing::TestInfo* const test_info = ::testing::UnitTest::GetInstance()->current_test_info();
    if (HasFailure()) {
        std::cerr << test_info->test_case_name() << "." << test_info->name() << " failed!" << std::endl;
        test_logger.print_log();
    }
    test_logger.reset();
}

void TestFixture::expect_layer_state(layer_t layer_state) const {
    test_logger.trace() << "Layer state: (" << +layer_state << ") Highest layer bit: (" << +get_highest_layer(layer_state) << ")" << std::endl;
    EXPECT_TRUE(layer_state_is(layer_state));
}