diff --git a/selfdrive/controls/lib/alerts.py b/selfdrive/controls/lib/alerts.py index 7ddb9f86c..63896638e 100644 --- a/selfdrive/controls/lib/alerts.py +++ b/selfdrive/controls/lib/alerts.py @@ -110,7 +110,7 @@ ALERTS = [ Alert( "preDriverDistracted", - "KEEP EYES ON ROAD: Driver Appears Distracted", + "KEEP EYES ON ROAD: Driver Distracted", "", AlertStatus.normal, AlertSize.small, Priority.LOW, VisualAlert.steerRequired, AudibleAlert.none, .0, .1, .1, alert_rate=0.75), @@ -187,7 +187,7 @@ ALERTS = [ Alert( "startupNoCar", - "Dashcam mode with unsupported car", + "Dashcam mode for unsupported car", "Always keep hands on wheel and eyes on road", AlertStatus.normal, AlertSize.mid, Priority.LOWER, VisualAlert.none, AudibleAlert.none, 0., 0., 15.), @@ -272,7 +272,7 @@ ALERTS = [ Alert( "dataNeededNoEntry", "openpilot Unavailable", - "Data Needed for Calibration. Upload Drive, Try Again", + "Calibration Needs Data. Upload Drive, Try Again", AlertStatus.normal, AlertSize.mid, Priority.LOW, VisualAlert.none, AudibleAlert.chimeError, .4, 0., 3.), @@ -544,7 +544,7 @@ ALERTS = [ Alert( "calibrationInvalidNoEntry", "openpilot Unavailable", - "Calibration Invalid: Reposition Device and Recalibrate", + "Calibration Invalid: Reposition Device & Recalibrate", AlertStatus.normal, AlertSize.mid, Priority.LOW, VisualAlert.none, AudibleAlert.chimeError, .4, 2., 3.), diff --git a/selfdrive/controls/tests/test_alerts.py b/selfdrive/controls/tests/test_alerts.py new file mode 100755 index 000000000..faea3c06b --- /dev/null +++ b/selfdrive/controls/tests/test_alerts.py @@ -0,0 +1,48 @@ +#!/usr/bin/env python3 +import os +import unittest +from PIL import Image, ImageDraw, ImageFont + +from cereal import log +from common.basedir import BASEDIR +from selfdrive.controls.lib.alerts import ALERTS + +AlertSize = log.ControlsState.AlertSize + +FONT_PATH = os.path.join(BASEDIR, "selfdrive/assets/fonts") +REGULAR_FONT_PATH = os.path.join(FONT_PATH, "opensans_semibold.ttf") +BOLD_FONT_PATH = os.path.join(FONT_PATH, "opensans_semibold.ttf") +SEMIBOLD_FONT_PATH = os.path.join(FONT_PATH, "opensans_semibold.ttf") + +MAX_TEXT_WIDTH = 1920 - 300 # full screen width is useable, minus sidebar +# TODO: get exact scale factor. found this empirically, works well enough +FONT_SIZE_SCALE = 1.85 # factor to scale from nanovg units to PIL + +class TestAlerts(unittest.TestCase): + + # ensure alert text doesn't exceed allowed width + def test_alert_text_length(self): + draw = ImageDraw.Draw(Image.new('RGB', (0, 0))) + + fonts = { + AlertSize.small: [ImageFont.truetype(SEMIBOLD_FONT_PATH, int(40*FONT_SIZE_SCALE))], + AlertSize.mid: [ImageFont.truetype(BOLD_FONT_PATH, int(48*FONT_SIZE_SCALE)), + ImageFont.truetype(REGULAR_FONT_PATH, int(36*FONT_SIZE_SCALE))], + } + + for alert in ALERTS: + # for full size alerts, both text fields wrap the text, + # so it's unlikely that they would go past the max width + if alert.alert_size in [AlertSize.none, AlertSize.full]: + continue + + for i, txt in enumerate([alert.alert_text_1, alert.alert_text_2]): + if i >= len(fonts[alert.alert_size]): break + + font = fonts[alert.alert_size][i] + w, h = draw.textsize(txt, font) + msg = "type: %s msg: %s" % (alert.alert_type, txt) + self.assertLessEqual(w, MAX_TEXT_WIDTH, msg=msg) + +if __name__ == "__main__": + unittest.main() diff --git a/selfdrive/test/process_replay/ref_commit b/selfdrive/test/process_replay/ref_commit index 022d6d368..5a2b9f024 100644 --- a/selfdrive/test/process_replay/ref_commit +++ b/selfdrive/test/process_replay/ref_commit @@ -1 +1 @@ -852b5b42981cf17a18c7eebc9b501db2f5b0c33b +852b5b42981cf17a18c7eebc9b501db2f5b0c33b \ No newline at end of file