summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/audio/config.h18
-rw-r--r--tests/audio/test.mk16
-rw-r--r--tests/audio/test_audio.cpp118
-rw-r--r--tests/caps_word/caps_word_unicodemap/test_caps_word_unicodemap.cpp4
-rw-r--r--tests/tap_hold_configurations/retro_tapping/config.h5
-rw-r--r--tests/tap_hold_configurations/retro_tapping/test_neutralization.cpp201
6 files changed, 359 insertions, 3 deletions
diff --git a/tests/audio/config.h b/tests/audio/config.h
new file mode 100644
index 0000000000..d0c4ddadbd
--- /dev/null
+++ b/tests/audio/config.h
@@ -0,0 +1,18 @@
+// Copyright 2023 Google LLC
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 2 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+#pragma once
+
+#include "test_common.h"
diff --git a/tests/audio/test.mk b/tests/audio/test.mk
new file mode 100644
index 0000000000..a2c71d9587
--- /dev/null
+++ b/tests/audio/test.mk
@@ -0,0 +1,16 @@
+# Copyright 2023 Google LLC
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+AUDIO_ENABLE = yes
diff --git a/tests/audio/test_audio.cpp b/tests/audio/test_audio.cpp
new file mode 100644
index 0000000000..ef34748b06
--- /dev/null
+++ b/tests/audio/test_audio.cpp
@@ -0,0 +1,118 @@
+// Copyright 2023 Google LLC
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 2 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+#include <cmath>
+#include <random>
+
+#include "gtest/gtest.h"
+#include "keyboard_report_util.hpp"
+#include "test_common.hpp"
+
+namespace {
+
+class AudioTest : public TestFixture {
+ public:
+ uint16_t infer_tempo() {
+ return audio_ms_to_duration(1875) / 2;
+ }
+};
+
+TEST_F(AudioTest, OnOffToggle) {
+ audio_on();
+ EXPECT_TRUE(audio_is_on());
+
+ audio_off();
+ EXPECT_FALSE(audio_is_on());
+
+ audio_toggle();
+ EXPECT_TRUE(audio_is_on());
+
+ audio_toggle();
+ EXPECT_FALSE(audio_is_on());
+}
+
+TEST_F(AudioTest, ChangeTempo) {
+ for (int tempo = 50; tempo <= 250; tempo += 50) {
+ audio_set_tempo(tempo);
+ EXPECT_EQ(infer_tempo(), tempo);
+ }
+
+ audio_set_tempo(10);
+ audio_increase_tempo(25);
+ EXPECT_EQ(infer_tempo(), 35);
+
+ audio_decrease_tempo(4);
+ EXPECT_EQ(infer_tempo(), 31);
+
+ audio_increase_tempo(250);
+ EXPECT_EQ(infer_tempo(), 255);
+
+ audio_set_tempo(9);
+ EXPECT_EQ(infer_tempo(), 10);
+
+ audio_decrease_tempo(100);
+ EXPECT_EQ(infer_tempo(), 10);
+}
+
+TEST_F(AudioTest, BpmConversion) {
+ const int tol = 1;
+
+ audio_set_tempo(120);
+ // At 120 bpm, there are 2 beats per second, and a whole note is 500 ms.
+ EXPECT_NEAR(audio_duration_to_ms(64 /* whole note */), 500, tol);
+ EXPECT_NEAR(audio_ms_to_duration(500), 64, tol);
+ EXPECT_EQ(audio_duration_to_ms(0), 0);
+ EXPECT_EQ(audio_ms_to_duration(0), 0);
+
+ audio_set_tempo(10);
+ // At 10 bpm, UINT16_MAX ms corresponds to 699/64 beats and is the longest
+ // duration that can be converted without overflow.
+ EXPECT_NEAR(audio_ms_to_duration(UINT16_MAX), 699, tol);
+ EXPECT_NEAR(audio_duration_to_ms(699), 65531, tol);
+
+ audio_set_tempo(255);
+ // At 255 bpm, UINT16_MAX ms corresponds to 17825/64 beats and is the longest
+ // duration that can be converted without overflow.
+ EXPECT_NEAR(audio_ms_to_duration(UINT16_MAX), 17825, tol);
+ EXPECT_NEAR(audio_duration_to_ms(17825), 65533, tol);
+
+ std::mt19937 rng(0 /*seed*/);
+ std::uniform_int_distribution<int> dist_tempo(10, 255);
+ std::uniform_int_distribution<int> dist_ms(0, UINT16_MAX);
+
+ // Test bpm <-> ms conversions for random tempos and durations.
+ for (int trial = 0; trial < 50; ++trial) {
+ const int tempo = dist_tempo(rng);
+ const int duration_ms = dist_ms(rng);
+ SCOPED_TRACE("tempo " + testing::PrintToString(tempo) + ", duration " + testing::PrintToString(duration_ms) + " ms");
+
+ audio_set_tempo(tempo);
+ int duration_bpm = std::round((64.0f / (60.0f * 1000.0f)) * duration_ms * tempo);
+ ASSERT_NEAR(audio_ms_to_duration(duration_ms), duration_bpm, tol);
+
+ int roundtrip_ms = std::round((60.0f * 1000.0f / 64.0f) * duration_bpm / tempo);
+ // Because of round-off error, duration_ms and roundtrip_ms may differ by
+ // about (60 * 1000 / 64) / tempo.
+ int roundtrip_tol = tol * (60.0f * 1000.0f / 64.0f) / tempo;
+ ASSERT_NEAR(roundtrip_ms, duration_ms, roundtrip_tol);
+
+ // Only test converting back to ms if the result would be in uint16_t range.
+ if (roundtrip_ms <= UINT16_MAX) {
+ ASSERT_NEAR(audio_duration_to_ms(duration_bpm), roundtrip_ms, tol);
+ }
+ }
+}
+
+} // namespace
diff --git a/tests/caps_word/caps_word_unicodemap/test_caps_word_unicodemap.cpp b/tests/caps_word/caps_word_unicodemap/test_caps_word_unicodemap.cpp
index 01cdfd6408..21e5493526 100644
--- a/tests/caps_word/caps_word_unicodemap/test_caps_word_unicodemap.cpp
+++ b/tests/caps_word/caps_word_unicodemap/test_caps_word_unicodemap.cpp
@@ -39,8 +39,8 @@ const uint32_t unicode_map[] PROGMEM = {
[DELTA_UPPERCASE] = 0x0394,
};
-#define U_DASH XP(ENDASH, EMDASH)
-#define U_DELTA XP(DELTA_LOWERCASE, DELTA_UPPERCASE)
+#define U_DASH UP(ENDASH, EMDASH)
+#define U_DELTA UP(DELTA_LOWERCASE, DELTA_UPPERCASE)
bool caps_word_press_user(uint16_t keycode) {
switch (keycode) {
diff --git a/tests/tap_hold_configurations/retro_tapping/config.h b/tests/tap_hold_configurations/retro_tapping/config.h
index 4b38f2644b..cc9f162477 100644
--- a/tests/tap_hold_configurations/retro_tapping/config.h
+++ b/tests/tap_hold_configurations/retro_tapping/config.h
@@ -18,4 +18,7 @@
#include "test_common.h"
-#define RETRO_TAPPING \ No newline at end of file
+#define RETRO_TAPPING
+#define DUMMY_MOD_NEUTRALIZER_KEYCODE KC_RIGHT_CTRL
+#define MODS_TO_NEUTRALIZE \
+ { MOD_BIT(KC_LEFT_GUI) }
diff --git a/tests/tap_hold_configurations/retro_tapping/test_neutralization.cpp b/tests/tap_hold_configurations/retro_tapping/test_neutralization.cpp
new file mode 100644
index 0000000000..10d675a3b1
--- /dev/null
+++ b/tests/tap_hold_configurations/retro_tapping/test_neutralization.cpp
@@ -0,0 +1,201 @@
+/* Copyright 2023 Vladislav Kucheriavykh
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "keyboard_report_util.hpp"
+#include "keycode.h"
+#include "test_common.hpp"
+#include "action_tapping.h"
+#include "test_keymap_key.hpp"
+
+using testing::_;
+using testing::InSequence;
+
+class RetroTapNeutralization : public TestFixture {};
+
+TEST_F(RetroTapNeutralization, neutralize_retro_tapped_left_gui_mod_tap) {
+ TestDriver driver;
+ InSequence s;
+ auto mod_tap_hold_key = KeymapKey(0, 7, 0, LGUI_T(KC_P));
+
+ set_keymap({mod_tap_hold_key});
+
+ EXPECT_NO_REPORT(driver);
+ mod_tap_hold_key.press();
+ idle_for(TAPPING_TERM);
+ VERIFY_AND_CLEAR(driver);
+
+ EXPECT_REPORT(driver, (KC_LGUI));
+ run_one_scan_loop();
+ VERIFY_AND_CLEAR(driver);
+
+ EXPECT_REPORT(driver, (DUMMY_MOD_NEUTRALIZER_KEYCODE, KC_LGUI));
+ EXPECT_REPORT(driver, (KC_LGUI));
+ EXPECT_EMPTY_REPORT(driver);
+ EXPECT_REPORT(driver, (KC_P));
+ EXPECT_EMPTY_REPORT(driver);
+ mod_tap_hold_key.release();
+ run_one_scan_loop();
+ VERIFY_AND_CLEAR(driver);
+}
+
+TEST_F(RetroTapNeutralization, do_not_neutralize_retro_tapped_left_shift_mod_tap) {
+ TestDriver driver;
+ InSequence s;
+ auto mod_tap_hold_key = KeymapKey(0, 7, 0, LSFT_T(KC_P));
+
+ set_keymap({mod_tap_hold_key});
+
+ EXPECT_NO_REPORT(driver);
+ mod_tap_hold_key.press();
+ idle_for(TAPPING_TERM);
+ VERIFY_AND_CLEAR(driver);
+
+ EXPECT_REPORT(driver, (KC_LEFT_SHIFT));
+ run_one_scan_loop();
+ VERIFY_AND_CLEAR(driver);
+
+ EXPECT_EMPTY_REPORT(driver);
+ EXPECT_REPORT(driver, (KC_P));
+ EXPECT_EMPTY_REPORT(driver);
+ mod_tap_hold_key.release();
+ run_one_scan_loop();
+ VERIFY_AND_CLEAR(driver);
+}
+
+TEST_F(RetroTapNeutralization, do_not_neutralize_retro_tapped_right_gui_mod_tap) {
+ TestDriver driver;
+ InSequence s;
+ auto mod_tap_hold_key = KeymapKey(0, 7, 0, RGUI_T(KC_P));
+
+ set_keymap({mod_tap_hold_key});
+
+ EXPECT_NO_REPORT(driver);
+ mod_tap_hold_key.press();
+ idle_for(TAPPING_TERM);
+ VERIFY_AND_CLEAR(driver);
+
+ EXPECT_REPORT(driver, (KC_RGUI));
+ run_one_scan_loop();
+ VERIFY_AND_CLEAR(driver);
+
+ EXPECT_EMPTY_REPORT(driver);
+ EXPECT_REPORT(driver, (KC_P));
+ EXPECT_EMPTY_REPORT(driver);
+ mod_tap_hold_key.release();
+ run_one_scan_loop();
+ VERIFY_AND_CLEAR(driver);
+}
+
+TEST_F(RetroTapNeutralization, do_not_neutralize_retro_tapped_left_gui_shift_mod_tap) {
+ TestDriver driver;
+ InSequence s;
+ auto mod_tap_hold_key = KeymapKey(0, 7, 0, MT(MOD_LGUI | MOD_LSFT, KC_P));
+
+ set_keymap({mod_tap_hold_key});
+
+ EXPECT_NO_REPORT(driver);
+ mod_tap_hold_key.press();
+ idle_for(TAPPING_TERM);
+ VERIFY_AND_CLEAR(driver);
+
+ EXPECT_REPORT(driver, (KC_LSFT, KC_LGUI));
+ run_one_scan_loop();
+ VERIFY_AND_CLEAR(driver);
+
+ EXPECT_EMPTY_REPORT(driver);
+ EXPECT_REPORT(driver, (KC_P));
+ EXPECT_EMPTY_REPORT(driver);
+ mod_tap_hold_key.release();
+ run_one_scan_loop();
+ VERIFY_AND_CLEAR(driver);
+}
+
+TEST_F(RetroTapNeutralization, do_not_neutralize_roll_of_regular_and_mod_tap_keys) {
+ TestDriver driver;
+ InSequence s;
+ auto mod_tap_hold_key = KeymapKey(0, 1, 0, LGUI_T(KC_P));
+ auto regular_key = KeymapKey(0, 2, 0, KC_A);
+
+ set_keymap({mod_tap_hold_key, regular_key});
+
+ /* Press mod-tap-hold key. */
+ EXPECT_NO_REPORT(driver);
+ mod_tap_hold_key.press();
+ run_one_scan_loop();
+ VERIFY_AND_CLEAR(driver);
+
+ /* Press regular key. */
+ EXPECT_NO_REPORT(driver);
+ regular_key.press();
+ run_one_scan_loop();
+ VERIFY_AND_CLEAR(driver);
+
+ /* Release regular key. */
+ EXPECT_NO_REPORT(driver);
+ regular_key.release();
+ run_one_scan_loop();
+ VERIFY_AND_CLEAR(driver);
+
+ /* Release mod-tap-hold key. */
+ EXPECT_REPORT(driver, (KC_P));
+ EXPECT_REPORT(driver, (KC_P, KC_A));
+ EXPECT_REPORT(driver, (KC_P));
+ EXPECT_EMPTY_REPORT(driver);
+ mod_tap_hold_key.release();
+ run_one_scan_loop();
+ VERIFY_AND_CLEAR(driver);
+
+ /* Idle for tapping term of mod tap hold key. */
+ idle_for(TAPPING_TERM - 3);
+ VERIFY_AND_CLEAR(driver);
+}
+
+TEST_F(RetroTapNeutralization, do_not_neutralize_tap_regular_key_while_mod_tap_is_held) {
+ TestDriver driver;
+ InSequence s;
+ auto mod_tap_hold_key = KeymapKey(0, 1, 0, LGUI_T(KC_P));
+ auto regular_key = KeymapKey(0, 2, 0, KC_A);
+
+ set_keymap({mod_tap_hold_key, regular_key});
+
+ /* Press and hold mod-tap key. */
+ EXPECT_REPORT(driver, (KC_LEFT_GUI));
+ mod_tap_hold_key.press();
+ idle_for(TAPPING_TERM + 1);
+ VERIFY_AND_CLEAR(driver);
+
+ /* Press regular key. */
+ EXPECT_REPORT(driver, (KC_A, KC_LEFT_GUI));
+ regular_key.press();
+ run_one_scan_loop();
+ VERIFY_AND_CLEAR(driver);
+
+ /* Release regular key. */
+ EXPECT_REPORT(driver, (KC_LEFT_GUI));
+ regular_key.release();
+ run_one_scan_loop();
+ VERIFY_AND_CLEAR(driver);
+
+ /* Release mod-tap-hold key. */
+ EXPECT_EMPTY_REPORT(driver);
+ mod_tap_hold_key.release();
+ run_one_scan_loop();
+ VERIFY_AND_CLEAR(driver);
+
+ /* Idle for tapping term of mod tap hold key. */
+ idle_for(TAPPING_TERM - 3);
+ VERIFY_AND_CLEAR(driver);
+}