Files
DroidDeck/tools/tests/test_gamescope_touch_click_hover.py
T

93 lines
3.5 KiB
Python

from pathlib import Path
import shutil
import subprocess
import tempfile
import unittest
ROOT = Path(__file__).resolve().parents[2]
PATCH = ROOT / "tools/gamescope/patches/0147-touch-click-press-after-hover.patch"
def added_source(patch):
return "\n".join(line[1:] for line in patch.read_text().splitlines() if line.startswith("+") and not line.startswith("+++"))
def rules(text):
begin = text.index("static constexpr uint64_t k_ulTouchClickHoverNs")
end = text.index("static TouchClickTap s_TouchTap;")
return text[begin:end]
PREAMBLE = r'''
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdint>
#define None 0L
'''
MAIN = r'''
static const uint64_t MS = 1'000'000ul;
int main() {
TouchClickTap tap;
uint64_t delay = 0;
assert(touch_click_tap_down(tap, 272, 10, 100 * MS, 50.0, 60.0, true) == k_ulTouchClickHoverNs);
assert(tap.bPressPending);
assert(!touch_click_tap_motion(tap, 55.0, 64.0));
assert(touch_click_tap_motion(tap, 80.0, 60.0));
assert(touch_click_tap_fire(tap, 134 * MS, &delay) == TouchClickStep::Press);
assert(delay == 0);
assert(!touch_click_tap_motion(tap, 300.0, 300.0));
assert(!touch_click_tap_up(tap, 400, 500 * MS, &delay));
assert(touch_click_tap_down(tap, 272, 10, 100 * MS, 50.0, 60.0, true) > 0);
assert(touch_click_tap_up(tap, 15, 105 * MS, &delay));
assert(delay == 0);
assert(touch_click_tap_fire(tap, 134 * MS, &delay) == TouchClickStep::Press);
assert(delay == k_ulTouchClickHoldNs);
assert(touch_click_tap_time(tap, 134 * MS) == 44);
assert(touch_click_tap_fire(tap, 184 * MS, &delay) == TouchClickStep::Release);
assert(touch_click_tap_fire(tap, 200 * MS, &delay) == TouchClickStep::Idle);
assert(touch_click_tap_down(tap, 272, 10, 100 * MS, 50.0, 60.0, false) == 0);
assert(!tap.bPressPending);
assert(touch_click_tap_up(tap, 30, 120 * MS, &delay));
assert(delay == 30 * MS);
assert(touch_click_tap_fire(tap, 150 * MS, &delay) == TouchClickStep::Release);
assert(touch_click_tap_down(tap, 273, 10, 100 * MS, 0.0, 0.0, false) == 0);
assert(!touch_click_tap_up(tap, 90, 180 * MS, &delay));
assert(touch_click_tap_fire(tap, 190 * MS, &delay) == TouchClickStep::Idle);
return 0;
}
'''
class GamescopeTouchClickHoverTest(unittest.TestCase):
def test_press_waits_for_the_hover_and_a_tap_is_held(self):
compiler = shutil.which("c++") or shutil.which("g++") or shutil.which("clang++")
if not compiler:
self.skipTest("no C++ compiler")
source = PREAMBLE + rules(added_source(PATCH)) + MAIN
with tempfile.TemporaryDirectory() as tmp:
src = Path(tmp) / "touch_click_hover.cpp"
exe = Path(tmp) / "touch_click_hover"
src.write_text(source)
subprocess.run([compiler, "-std=c++17", "-Wall", "-Wextra", "-Werror", "-UNDEBUG", str(src), "-o", str(exe)], check=True)
subprocess.run([str(exe)], check=True)
def test_touch_down_defers_only_a_moved_press(self):
added = added_source(PATCH)
self.assertIn("bMoved = flFromX != wlserver.mouse_surface_cursorx || flFromY != wlserver.mouse_surface_cursory;", added)
self.assertIn("if ( !ulDelay || !touch_click_tap_arm( ulDelay ) )", added)
self.assertIn("touch_click_tap_settle( ulNow );", added)
self.assertEqual(1, added.count("touch_click_tap_motion( s_TouchTap, tx, ty )"))
self.assertIn("if ( bClickTouch && !touch_click_tap_up( s_TouchTap, time, get_time_in_nanos(), &ulDelay ) )", added)
if __name__ == "__main__":
unittest.main()