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