From 11889dd0249b6965ab534d553d0e373b36baa8f8 Mon Sep 17 00:00:00 2001 From: Cameron Clough Date: Wed, 31 May 2023 23:04:30 -0700 Subject: [PATCH] UI snapshot tool (#28356) * UI: snapshot testing tool * gitignore * cleanup * remove prime type arg * rename * add script * commit snapshots * use base setup * updates * always run base * echo * use imagemagick * test_snapshots * fixes * update * set language * revert * default arg * just remove translations * formatting * output arg * improved help * fix assets * change dir back before saving snapshot * unused imports * simpler * add default to description * use uiUpdate signal old-commit-hash: 15ac8c041b638e8989e1391387530c7195ef7403 --- selfdrive/ui/SConscript | 1 + selfdrive/ui/tests/.gitignore | 1 + selfdrive/ui/tests/ui_snapshot.cc | 66 +++++++++++++++++++++++++++++++ selfdrive/ui/tests/ui_snapshot.h | 5 +++ 4 files changed, 73 insertions(+) create mode 100644 selfdrive/ui/tests/ui_snapshot.cc create mode 100644 selfdrive/ui/tests/ui_snapshot.h diff --git a/selfdrive/ui/SConscript b/selfdrive/ui/SConscript index a8c8463bd7..f54d72ae31 100644 --- a/selfdrive/ui/SConscript +++ b/selfdrive/ui/SConscript @@ -64,6 +64,7 @@ qt_env.Program("_ui", qt_src + [asset_obj], LIBS=qt_libs) if GetOption('test'): qt_src.remove("main.cc") # replaced by test_runner qt_env.Program('tests/test_translations', [asset_obj, 'tests/test_runner.cc', 'tests/test_translations.cc'] + qt_src, LIBS=qt_libs) + qt_env.Program('tests/ui_snapshot', [asset_obj, "tests/ui_snapshot.cc"] + qt_src, LIBS=qt_libs) # build translation files diff --git a/selfdrive/ui/tests/.gitignore b/selfdrive/ui/tests/.gitignore index 26335744f3..94ba9a3a97 100644 --- a/selfdrive/ui/tests/.gitignore +++ b/selfdrive/ui/tests/.gitignore @@ -2,3 +2,4 @@ test playsound test_sound test_translations +ui_snapshot diff --git a/selfdrive/ui/tests/ui_snapshot.cc b/selfdrive/ui/tests/ui_snapshot.cc new file mode 100644 index 0000000000..14e0fab835 --- /dev/null +++ b/selfdrive/ui/tests/ui_snapshot.cc @@ -0,0 +1,66 @@ +#include "selfdrive/ui/tests/ui_snapshot.h" + +#include +#include +#include +#include +#include + +#include "selfdrive/ui/qt/home.h" +#include "selfdrive/ui/qt/util.h" +#include "selfdrive/ui/qt/window.h" +#include "selfdrive/ui/ui.h" + +void saveWidgetAsImage(QWidget *widget, const QString &fileName) { + QImage image(widget->size(), QImage::Format_ARGB32); + QPainter painter(&image); + widget->render(&painter); + image.save(fileName); +} + +int main(int argc, char *argv[]) { + initApp(argc, argv); + + QApplication app(argc, argv); + + QCommandLineParser parser; + parser.setApplicationDescription("Take a snapshot of the UI."); + parser.addHelpOption(); + parser.addOption(QCommandLineOption(QStringList() << "o" + << "output", + "Output image file path. The file's suffix is used to " + "determine the format. Supports PNG and JPEG formats. " + "Defaults to \"snapshot.png\".", + "file", "snapshot.png")); + parser.process(app); + + const QString output = parser.value("output"); + if (output.isEmpty()) { + qCritical() << "No output file specified"; + return 1; + } + + auto current = QDir::current(); + + // change working directory to find assets + if (!QDir::setCurrent(QCoreApplication::applicationDirPath() + QDir::separator() + "..")) { + qCritical() << "Failed to set current directory"; + return 1; + } + + MainWindow w; + w.setFixedSize(2160, 1080); + w.show(); + app.installEventFilter(&w); + + // restore working directory + QDir::setCurrent(current.absolutePath()); + + // wait for the UI to update + QObject::connect(uiState(), &UIState::uiUpdate, [&](const UIState &s) { + saveWidgetAsImage(&w, output); + app.quit(); + }); + + return app.exec(); +} diff --git a/selfdrive/ui/tests/ui_snapshot.h b/selfdrive/ui/tests/ui_snapshot.h new file mode 100644 index 0000000000..b4699f6af5 --- /dev/null +++ b/selfdrive/ui/tests/ui_snapshot.h @@ -0,0 +1,5 @@ +#pragma once + +#include + +void saveWidgetAsImage(QWidget *widget, const QString &fileName);