blob: 65b3b6b8d94a08105dab785bf083c6a27cf566fc [file] [log] [blame]
James Kuszmaul59a308f2023-01-28 19:14:07 -08001#include "frc971/vision/foxglove_image_converter.h"
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"
7#include "gtest/gtest.h"
8
9namespace frc971::vision {
10std::ostream &operator<<(std::ostream &os, ImageCompression compression) {
11 os << ExtensionForCompression(compression);
12 return os;
13}
14namespace testing {
15class ImageConverterTest : public ::testing::TestWithParam<ImageCompression> {
16 protected:
17 ImageConverterTest()
18 : config_(aos::configuration::ReadConfig(
19 aos::testing::ArtifactPath("frc971/vision/converter_config.json"))),
20 factory_(&config_.message()),
21 camera_image_(
22 aos::FileToFlatbuffer<CameraImage>(aos::testing::ArtifactPath(
23 "external/april_tag_test_image/test.bfbs"))),
24 node_(aos::configuration::GetNode(&config_.message(), "test")),
25 test_event_loop_(factory_.MakeEventLoop("test", node_)),
26 image_sender_(test_event_loop_->MakeSender<CameraImage>("/camera")),
27 converter_event_loop_(factory_.MakeEventLoop("converter", node_)),
28 converter_(converter_event_loop_.get(), "/camera", "/visualize",
29 GetParam()),
30 output_path_(absl::StrCat(aos::testing::TestTmpDir(), "/test.",
31 ExtensionForCompression(GetParam()))) {
32 test_event_loop_->OnRun(
33 [this]() { image_sender_.CheckOk(image_sender_.Send(camera_image_)); });
34 test_event_loop_->MakeWatcher(
35 "/visualize", [this](const foxglove::CompressedImage &image) {
36 ASSERT_TRUE(image.has_data());
37 std::string expected_contents =
38 aos::util::ReadFileToStringOrDie(aos::testing::ArtifactPath(
39 absl::StrCat("external/april_tag_test_image/expected.",
40 ExtensionForCompression(GetParam()))));
41 std::string_view data(
42 reinterpret_cast<const char *>(image.data()->data()),
43 image.data()->size());
44 EXPECT_EQ(expected_contents, data);
45 aos::util::WriteStringToFileOrDie(output_path_, data);
46 factory_.Exit();
47 });
48 }
49
50 aos::FlatbufferDetachedBuffer<aos::Configuration> config_;
51 aos::SimulatedEventLoopFactory factory_;
52 aos::FlatbufferVector<CameraImage> camera_image_;
53 const aos::Node *const node_;
54 std::unique_ptr<aos::EventLoop> test_event_loop_;
55 aos::Sender<CameraImage> image_sender_;
56 std::unique_ptr<aos::EventLoop> converter_event_loop_;
57 FoxgloveImageConverter converter_;
58 std::string output_path_;
59};
60
61TEST_P(ImageConverterTest, ImageToFoxglove) { factory_.Run(); }
62
63INSTANTIATE_TEST_SUITE_P(CompressionOptions, ImageConverterTest,
64 ::testing::Values(ImageCompression::kJpeg,
65 ImageCompression::kPng));
66
67} // namespace testing
68} // namespace frc971::vision