Unit test for alert width (#1307)
* unit test for alert width * fix scale * comment * fix offending alert text * rename * update process replay refs Co-authored-by: Willem Melching <willem.melching@gmail.com> old-commit-hash: 0f6c22ce8b3fbbbbb8fff59e44f49b8ec1578e90
This commit is contained in:
@@ -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.),
|
||||
|
||||
|
||||
48
selfdrive/controls/tests/test_alerts.py
Executable file
48
selfdrive/controls/tests/test_alerts.py
Executable file
@@ -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()
|
||||
@@ -1 +1 @@
|
||||
852b5b42981cf17a18c7eebc9b501db2f5b0c33b
|
||||
852b5b42981cf17a18c7eebc9b501db2f5b0c33b
|
||||
Reference in New Issue
Block a user