Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 1 | #include "absl/flags/declare.h" |
| 2 | #include "absl/flags/flag.h" |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame] | 3 | #include "gtest/gtest.h" |
James Kuszmaul | 59a308f | 2023-01-28 19:14:07 -0800 | [diff] [blame] | 4 | |
| 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 Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame] | 9 | #include "frc971/vision/foxglove_image_converter_lib.h" |
James Kuszmaul | 59a308f | 2023-01-28 19:14:07 -0800 | [diff] [blame] | 10 | |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 11 | ABSL_DECLARE_FLAG(int32_t, jpeg_quality); |
James Kuszmaul | acff22f | 2023-03-04 21:00:02 -0800 | [diff] [blame] | 12 | |
James Kuszmaul | 59a308f | 2023-01-28 19:14:07 -0800 | [diff] [blame] | 13 | namespace frc971::vision { |
| 14 | std::ostream &operator<<(std::ostream &os, ImageCompression compression) { |
| 15 | os << ExtensionForCompression(compression); |
| 16 | return os; |
| 17 | } |
| 18 | namespace testing { |
| 19 | class 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 Kuszmaul | acff22f | 2023-03-04 21:00:02 -0800 | [diff] [blame] | 36 | // 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 Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 39 | absl::SetFlag(&FLAGS_jpeg_quality, 95); |
James Kuszmaul | 59a308f | 2023-01-28 19:14:07 -0800 | [diff] [blame] | 40 | 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 | |
| 69 | TEST_P(ImageConverterTest, ImageToFoxglove) { factory_.Run(); } |
| 70 | |
| 71 | INSTANTIATE_TEST_SUITE_P(CompressionOptions, ImageConverterTest, |
| 72 | ::testing::Values(ImageCompression::kJpeg, |
| 73 | ImageCompression::kPng)); |
| 74 | |
| 75 | } // namespace testing |
| 76 | } // namespace frc971::vision |