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>
This commit is contained in:
Adeeb 2020-04-07 21:37:17 -07:00 committed by GitHub
parent 017b1f6770
commit 0f6c22ce8b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 53 additions and 5 deletions

View file

@ -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.),

View 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()

View file

@ -1 +1 @@
852b5b42981cf17a18c7eebc9b501db2f5b0c33b
852b5b42981cf17a18c7eebc9b501db2f5b0c33b