98 lines
3.8 KiB
Python
98 lines
3.8 KiB
Python
#!/usr/bin/env python3
|
|
"""Eingabe als Strategie (hand/eingabe.py), einheitlich für Lobby und Tisch, mit einem Schalter.
|
|
|
|
* ohne Spielplan oder ohne Angabe: mit Maus (bisheriges Verhalten, bis pid gemessen ist)
|
|
* ohne Maus am Tisch: Klick an den Prozess - kein Fenster nach vorn, der Zeiger bleibt stehen
|
|
* ohne Maus in der Lobby: JavaScript-Klick (cdp.mjs), wie bisher
|
|
* mit Maus in der Lobby: cdp.mjs liefert "MAUS {x, y}" (Bildschirmpunkte), Python klickt echt
|
|
* Ausweichen nennt sich: act.ausfuehren(..., wie="maus") erzwingt die Maus für den zweiten Versuch
|
|
|
|
python3 tests/eingabe_spec.py -v
|
|
"""
|
|
import json
|
|
import os
|
|
import sys
|
|
import tempfile
|
|
import unittest
|
|
|
|
_w = os.path.dirname(os.path.abspath(__file__))
|
|
while _w != "/" and not os.path.isdir(os.path.join(_w, "hand")):
|
|
_w = os.path.dirname(_w)
|
|
for _u in ("hand", "betrieb", "mcp", "lobby", "lesen"):
|
|
sys.path.insert(0, os.path.join(_w, _u))
|
|
|
|
import eingabe
|
|
|
|
click = eingabe._click() # dasselbe Modul, das hand/act.py nutzt
|
|
FENSTER = {"pid": 4242, "titel": "CoinPoker", "rect": (100, 100, 800, 600)}
|
|
|
|
|
|
class Art(unittest.TestCase):
|
|
def setUp(self):
|
|
self.ordner = tempfile.TemporaryDirectory()
|
|
self.alt = eingabe.SPIELPLAN
|
|
eingabe.SPIELPLAN = os.path.join(self.ordner.name, "spielplan.json")
|
|
|
|
def tearDown(self):
|
|
eingabe.SPIELPLAN = self.alt
|
|
self.ordner.cleanup()
|
|
|
|
def plan(self, **d):
|
|
with open(eingabe.SPIELPLAN, "w") as f:
|
|
json.dump(dict({"modus": "uebung", "tische": 1}, **d), f)
|
|
|
|
def test_standard_mit_maus(self):
|
|
self.assertEqual(eingabe.art(), "maus")
|
|
self.plan()
|
|
self.assertEqual(eingabe.art(), "maus")
|
|
|
|
def test_ohne_maus(self):
|
|
self.plan(maus=False)
|
|
self.assertEqual(eingabe.art(), "ohne")
|
|
|
|
|
|
class Tisch(unittest.TestCase):
|
|
def setUp(self):
|
|
self.alt = (click.nach_vorn, click._senden, click._senden_an, click.click)
|
|
self.global_, self.an_pid, self.vorn = [], [], []
|
|
click.nach_vorn = lambda f: self.vorn.append(f)
|
|
click._senden = lambda art, x, y: self.global_.append(art)
|
|
click._senden_an = lambda pid, art, x, y: self.an_pid.append((pid, art, x, y))
|
|
click.click = lambda px, py, **k: self.global_.append(("click", px, py)) or (px, py)
|
|
|
|
def tearDown(self):
|
|
click.nach_vorn, click._senden, click._senden_an, click.click = self.alt
|
|
|
|
def test_ohne_maus_an_den_prozess(self):
|
|
pos = eingabe.tisch_klick(300, 400, FENSTER, wie="ohne")
|
|
self.assertEqual([a for _, a, _, _ in self.an_pid], [click.MOVED, click.DOWN, click.UP])
|
|
self.assertTrue(all(p == 4242 and (x, y) == (300, 400) for p, _, x, y in self.an_pid))
|
|
self.assertEqual((self.global_, self.vorn), ([], [])) # kein Systemzeiger, nichts nach vorn
|
|
self.assertEqual(pos, (300, 400))
|
|
|
|
def test_mit_maus_global(self):
|
|
eingabe.tisch_klick(300, 400, FENSTER, wie="maus")
|
|
self.assertEqual(self.global_, [("click", 300, 400)])
|
|
self.assertEqual(self.an_pid, [])
|
|
|
|
|
|
class Lobby(unittest.TestCase):
|
|
def test_maus_antwort_wird_echter_klick(self):
|
|
geklickt = []
|
|
alt = eingabe.lobby_maus_klick
|
|
eingabe.lobby_maus_klick = lambda x, y: geklickt.append((x, y)) or True
|
|
try:
|
|
text = eingabe.lobby_antwort('MAUS {"x": 812.5, "y": 433, "text": "Join in Zeile Big Ben 9"}')
|
|
finally:
|
|
eingabe.lobby_maus_klick = alt
|
|
self.assertEqual(geklickt, [(812.5, 433)])
|
|
self.assertEqual(text, "per Maus geklickt: Join in Zeile Big Ben 9")
|
|
|
|
def test_andere_antworten_bleiben(self):
|
|
self.assertEqual(eingabe.lobby_antwort("Join geklickt in Zeile Big Ben 9"), "Join geklickt in Zeile Big Ben 9")
|
|
self.assertEqual(eingabe.lobby_antwort("MAUS kaputt"), "MAUS kaputt")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|