2020-05-14 15:21:21 -07:00
|
|
|
#!/usr/bin/env python3
|
2023-06-22 22:04:28 +08:00
|
|
|
import copy
|
2020-08-11 16:23:57 -07:00
|
|
|
import json
|
2020-05-14 15:21:21 -07:00
|
|
|
import os
|
|
|
|
|
import unittest
|
2020-08-11 16:23:57 -07:00
|
|
|
import random
|
2020-05-14 15:21:21 -07:00
|
|
|
from PIL import Image, ImageDraw, ImageFont
|
|
|
|
|
|
|
|
|
|
from cereal import log, car
|
2023-05-24 02:24:16 +02:00
|
|
|
from cereal.messaging import SubMaster
|
2020-05-14 15:21:21 -07:00
|
|
|
from common.basedir import BASEDIR
|
2020-08-11 16:23:57 -07:00
|
|
|
from common.params import Params
|
2021-12-09 14:29:50 -08:00
|
|
|
from selfdrive.controls.lib.events import Alert, EVENTS, ET
|
2020-08-11 16:23:57 -07:00
|
|
|
from selfdrive.controls.lib.alertmanager import set_offroad_alert
|
2023-05-24 02:24:16 +02:00
|
|
|
from selfdrive.test.process_replay.process_replay import CONFIGS
|
2020-05-14 15:21:21 -07:00
|
|
|
|
|
|
|
|
AlertSize = log.ControlsState.AlertSize
|
|
|
|
|
|
2020-08-11 16:23:57 -07:00
|
|
|
OFFROAD_ALERTS_PATH = os.path.join(BASEDIR, "selfdrive/controls/lib/alerts_offroad.json")
|
2020-05-14 15:21:21 -07:00
|
|
|
|
2020-10-14 15:56:18 -07:00
|
|
|
# TODO: add callback alerts
|
|
|
|
|
ALERTS = []
|
|
|
|
|
for event_types in EVENTS.values():
|
|
|
|
|
for alert in event_types.values():
|
2022-05-16 16:25:52 +02:00
|
|
|
ALERTS.append(alert)
|
2020-10-14 15:56:18 -07:00
|
|
|
|
|
|
|
|
|
2020-06-02 16:29:32 -07:00
|
|
|
class TestAlerts(unittest.TestCase):
|
2020-08-11 16:23:57 -07:00
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
|
def setUpClass(cls):
|
|
|
|
|
with open(OFFROAD_ALERTS_PATH) as f:
|
|
|
|
|
cls.offroad_alerts = json.loads(f.read())
|
|
|
|
|
|
2022-05-16 16:25:52 +02:00
|
|
|
# Create fake objects for callback
|
2022-05-16 21:19:38 -07:00
|
|
|
cls.CS = car.CarState.new_message()
|
2022-05-16 16:25:52 +02:00
|
|
|
cls.CP = car.CarParams.new_message()
|
|
|
|
|
cfg = [c for c in CONFIGS if c.proc_name == 'controlsd'][0]
|
2023-05-24 02:24:16 +02:00
|
|
|
cls.sm = SubMaster(cfg.pubs)
|
2022-05-16 16:25:52 +02:00
|
|
|
|
2020-05-14 15:21:21 -07:00
|
|
|
def test_events_defined(self):
|
|
|
|
|
# Ensure all events in capnp schema are defined in events.py
|
|
|
|
|
events = car.CarEvent.EventName.schema.enumerants
|
|
|
|
|
|
|
|
|
|
for name, e in events.items():
|
|
|
|
|
if not name.endswith("DEPRECATED"):
|
|
|
|
|
fail_msg = "%s @%d not in EVENTS" % (name, e)
|
|
|
|
|
self.assertTrue(e in EVENTS.keys(), msg=fail_msg)
|
|
|
|
|
|
|
|
|
|
# ensure alert text doesn't exceed allowed width
|
|
|
|
|
def test_alert_text_length(self):
|
|
|
|
|
font_path = os.path.join(BASEDIR, "selfdrive/assets/fonts")
|
2022-06-22 11:45:38 +02:00
|
|
|
regular_font_path = os.path.join(font_path, "Inter-SemiBold.ttf")
|
|
|
|
|
bold_font_path = os.path.join(font_path, "Inter-Bold.ttf")
|
|
|
|
|
semibold_font_path = os.path.join(font_path, "Inter-SemiBold.ttf")
|
2020-05-14 15:21:21 -07:00
|
|
|
|
2022-08-30 11:20:55 -07:00
|
|
|
max_text_width = 2160 - 300 # full screen width is usable, minus sidebar
|
2020-05-14 15:21:21 -07:00
|
|
|
draw = ImageDraw.Draw(Image.new('RGB', (0, 0)))
|
|
|
|
|
|
|
|
|
|
fonts = {
|
2022-06-22 11:45:38 +02:00
|
|
|
AlertSize.small: [ImageFont.truetype(semibold_font_path, 74)],
|
|
|
|
|
AlertSize.mid: [ImageFont.truetype(bold_font_path, 88),
|
|
|
|
|
ImageFont.truetype(regular_font_path, 66)],
|
2020-05-14 15:21:21 -07:00
|
|
|
}
|
|
|
|
|
|
2020-10-14 15:56:18 -07:00
|
|
|
for alert in ALERTS:
|
2022-05-16 16:25:52 +02:00
|
|
|
if not isinstance(alert, Alert):
|
2022-05-16 21:19:38 -07:00
|
|
|
alert = alert(self.CP, self.CS, self.sm, metric=False, soft_disable_time=100)
|
2022-05-16 16:25:52 +02:00
|
|
|
|
2020-05-14 15:21:21 -07:00
|
|
|
# for full size alerts, both text fields wrap the text,
|
|
|
|
|
# so it's unlikely that they would go past the max width
|
2022-01-10 23:36:51 +08:00
|
|
|
if alert.alert_size in (AlertSize.none, AlertSize.full):
|
2020-05-14 15:21:21 -07:00
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
for i, txt in enumerate([alert.alert_text_1, alert.alert_text_2]):
|
2020-05-30 20:14:58 -07:00
|
|
|
if i >= len(fonts[alert.alert_size]):
|
|
|
|
|
break
|
2020-05-14 15:21:21 -07:00
|
|
|
|
|
|
|
|
font = fonts[alert.alert_size][i]
|
2023-06-16 13:22:30 -07:00
|
|
|
left, _, right, _ = draw.textbbox((0, 0), txt, font)
|
|
|
|
|
width = right - left
|
2021-12-16 14:58:17 +01:00
|
|
|
msg = f"type: {alert.alert_type} msg: {txt}"
|
2023-06-16 13:22:30 -07:00
|
|
|
self.assertLessEqual(width, max_text_width, msg=msg)
|
2020-05-14 15:21:21 -07:00
|
|
|
|
2020-10-14 15:56:18 -07:00
|
|
|
def test_alert_sanity_check(self):
|
2021-12-09 14:29:50 -08:00
|
|
|
for event_types in EVENTS.values():
|
|
|
|
|
for event_type, a in event_types.items():
|
|
|
|
|
# TODO: add callback alerts
|
|
|
|
|
if not isinstance(a, Alert):
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
if a.alert_size == AlertSize.none:
|
|
|
|
|
self.assertEqual(len(a.alert_text_1), 0)
|
|
|
|
|
self.assertEqual(len(a.alert_text_2), 0)
|
|
|
|
|
elif a.alert_size == AlertSize.small:
|
|
|
|
|
self.assertGreater(len(a.alert_text_1), 0)
|
|
|
|
|
self.assertEqual(len(a.alert_text_2), 0)
|
|
|
|
|
elif a.alert_size == AlertSize.mid:
|
|
|
|
|
self.assertGreater(len(a.alert_text_1), 0)
|
|
|
|
|
self.assertGreater(len(a.alert_text_2), 0)
|
|
|
|
|
else:
|
|
|
|
|
self.assertGreater(len(a.alert_text_1), 0)
|
|
|
|
|
|
|
|
|
|
self.assertGreaterEqual(a.duration, 0.)
|
|
|
|
|
|
|
|
|
|
if event_type not in (ET.WARNING, ET.PERMANENT, ET.PRE_ENABLE):
|
|
|
|
|
self.assertEqual(a.creation_delay, 0.)
|
2020-10-14 15:56:18 -07:00
|
|
|
|
2020-08-11 16:23:57 -07:00
|
|
|
def test_offroad_alerts(self):
|
|
|
|
|
params = Params()
|
|
|
|
|
for a in self.offroad_alerts:
|
|
|
|
|
# set the alert
|
2023-06-22 22:04:28 +08:00
|
|
|
alert = copy.copy(self.offroad_alerts[a])
|
2020-08-11 16:23:57 -07:00
|
|
|
set_offroad_alert(a, True)
|
2023-06-22 22:04:28 +08:00
|
|
|
alert['extra'] = ''
|
2020-08-11 16:23:57 -07:00
|
|
|
self.assertTrue(json.dumps(alert) == params.get(a, encoding='utf8'))
|
|
|
|
|
|
|
|
|
|
# then delete it
|
|
|
|
|
set_offroad_alert(a, False)
|
|
|
|
|
self.assertTrue(params.get(a) is None)
|
|
|
|
|
|
|
|
|
|
def test_offroad_alerts_extra_text(self):
|
|
|
|
|
params = Params()
|
|
|
|
|
for i in range(50):
|
|
|
|
|
# set the alert
|
|
|
|
|
a = random.choice(list(self.offroad_alerts))
|
|
|
|
|
alert = self.offroad_alerts[a]
|
|
|
|
|
set_offroad_alert(a, True, extra_text="a"*i)
|
|
|
|
|
|
2023-06-22 22:04:28 +08:00
|
|
|
written_alert = json.loads(params.get(a, encoding='utf8'))
|
|
|
|
|
self.assertTrue("a"*i == written_alert['extra'])
|
|
|
|
|
self.assertTrue(alert["text"] == written_alert['text'])
|
2020-06-02 16:29:32 -07:00
|
|
|
|
2020-05-14 15:21:21 -07:00
|
|
|
if __name__ == "__main__":
|
|
|
|
|
unittest.main()
|