Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame^] | 1 | #include "gtest/gtest.h" |
James Kuszmaul | 59a308f | 2023-01-28 19:14:07 -0800 | [diff] [blame] | 2 | |
| 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 Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame^] | 7 | #include "frc971/vision/foxglove_image_converter_lib.h" |
James Kuszmaul | 59a308f | 2023-01-28 19:14:07 -0800 | [diff] [blame] | 8 | |
James Kuszmaul | acff22f | 2023-03-04 21:00:02 -0800 | [diff] [blame] | 9 | DECLARE_int32(jpeg_quality); |
| 10 | |
James Kuszmaul | 59a308f | 2023-01-28 19:14:07 -0800 | [diff] [blame] | 11 | namespace frc971::vision { |
| 12 | std::ostream &operator<<(std::ostream &os, ImageCompression compression) { |
| 13 | os << ExtensionForCompression(compression); |
| 14 | return os; |
| 15 | } |
| 16 | namespace testing { |
| 17 | class 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 Kuszmaul | acff22f | 2023-03-04 21:00:02 -0800 | [diff] [blame] | 34 | // 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 Kuszmaul | 59a308f | 2023-01-28 19:14:07 -0800 | [diff] [blame] | 38 | 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 | |
| 67 | TEST_P(ImageConverterTest, ImageToFoxglove) { factory_.Run(); } |
| 68 | |
| 69 | INSTANTIATE_TEST_SUITE_P(CompressionOptions, ImageConverterTest, |
| 70 | ::testing::Values(ImageCompression::kJpeg, |
| 71 | ImageCompression::kPng)); |
| 72 | |
| 73 | } // namespace testing |
| 74 | } // namespace frc971::vision |