blob: c9c7d66b619a862ca190069ce7d390467c66ddcb [file] [log] [blame]
Philipp Schrader790cb542023-07-05 21:06:52 -07001#include "gtest/gtest.h"
James Kuszmaul59a308f2023-01-28 19:14:07 -08002
3#include "aos/events/simulated_event_loop.h"
4#include "aos/json_to_flatbuffer.h"
5#include "aos/testing/path.h"
6#include "aos/testing/tmpdir.h"
Philipp Schrader790cb542023-07-05 21:06:52 -07007#include "frc971/vision/foxglove_image_converter_lib.h"
James Kuszmaul59a308f2023-01-28 19:14:07 -08008
James Kuszmaulacff22f2023-03-04 21:00:02 -08009DECLARE_int32(jpeg_quality);
10
James Kuszmaul59a308f2023-01-28 19:14:07 -080011namespace frc971::vision {
12std::ostream &operator<<(std::ostream &os, ImageCompression compression) {
13 os << ExtensionForCompression(compression);
14 return os;
15}
16namespace testing {
17class ImageConverterTest : public ::testing::TestWithParam<ImageCompression> {
18 protected:
19 ImageConverterTest()
20 : config_(aos::configuration::ReadConfig(
21 aos::testing::ArtifactPath("frc971/vision/converter_config.json"))),
22 factory_(&config_.message()),
23 camera_image_(
24 aos::FileToFlatbuffer<CameraImage>(aos::testing::ArtifactPath(
25 "external/april_tag_test_image/test.bfbs"))),
26 node_(aos::configuration::GetNode(&config_.message(), "test")),
27 test_event_loop_(factory_.MakeEventLoop("test", node_)),
28 image_sender_(test_event_loop_->MakeSender<CameraImage>("/camera")),
29 converter_event_loop_(factory_.MakeEventLoop("converter", node_)),
30 converter_(converter_event_loop_.get(), "/camera", "/visualize",
31 GetParam()),
32 output_path_(absl::StrCat(aos::testing::TestTmpDir(), "/test.",
33 ExtensionForCompression(GetParam()))) {
James Kuszmaulacff22f2023-03-04 21:00:02 -080034 // Because our test image for comparison was generated with a JPEG quality
35 // of 95, we need to use that for the test to work. This also protects the
36 // tests against future changes to the default JPEG quality.
37 FLAGS_jpeg_quality = 95;
James Kuszmaul59a308f2023-01-28 19:14:07 -080038 test_event_loop_->OnRun(
39 [this]() { image_sender_.CheckOk(image_sender_.Send(camera_image_)); });
40 test_event_loop_->MakeWatcher(
41 "/visualize", [this](const foxglove::CompressedImage &image) {
42 ASSERT_TRUE(image.has_data());
43 std::string expected_contents =
44 aos::util::ReadFileToStringOrDie(aos::testing::ArtifactPath(
45 absl::StrCat("external/april_tag_test_image/expected.",
46 ExtensionForCompression(GetParam()))));
47 std::string_view data(
48 reinterpret_cast<const char *>(image.data()->data()),
49 image.data()->size());
50 EXPECT_EQ(expected_contents, data);
51 aos::util::WriteStringToFileOrDie(output_path_, data);
52 factory_.Exit();
53 });
54 }
55
56 aos::FlatbufferDetachedBuffer<aos::Configuration> config_;
57 aos::SimulatedEventLoopFactory factory_;
58 aos::FlatbufferVector<CameraImage> camera_image_;
59 const aos::Node *const node_;
60 std::unique_ptr<aos::EventLoop> test_event_loop_;
61 aos::Sender<CameraImage> image_sender_;
62 std::unique_ptr<aos::EventLoop> converter_event_loop_;
63 FoxgloveImageConverter converter_;
64 std::string output_path_;
65};
66
67TEST_P(ImageConverterTest, ImageToFoxglove) { factory_.Run(); }
68
69INSTANTIATE_TEST_SUITE_P(CompressionOptions, ImageConverterTest,
70 ::testing::Values(ImageCompression::kJpeg,
71 ImageCompression::kPng));
72
73} // namespace testing
74} // namespace frc971::vision