|
| 1 | +import pytest |
| 2 | +from synapse.api.datatype_pb2 import PixelFormat |
| 3 | +from synapse.api.nodes.image_source_pb2 import ImageSourceConfig |
| 4 | +from synapse.client.nodes.image_source import ImageSource |
| 5 | + |
| 6 | + |
| 7 | +def test_image_source_to_proto(): |
| 8 | + source = ImageSource( |
| 9 | + peripheral_id=1, |
| 10 | + width=1920, |
| 11 | + height=1080, |
| 12 | + format=PixelFormat.kRGB888, |
| 13 | + frame_rate_hz=30, |
| 14 | + ) |
| 15 | + |
| 16 | + proto = source._to_proto() |
| 17 | + |
| 18 | + assert proto.image_source.peripheral_id == 1 |
| 19 | + assert proto.image_source.width == 1920 |
| 20 | + assert proto.image_source.height == 1080 |
| 21 | + assert proto.image_source.format == PixelFormat.kRGB888 |
| 22 | + assert proto.image_source.frame_rate_hz == 30 |
| 23 | + |
| 24 | + |
| 25 | +def test_image_source_to_proto_pixel_formats(): |
| 26 | + for fmt in [ |
| 27 | + PixelFormat.kPixelFormatUnknown, |
| 28 | + PixelFormat.kYUV420_888, |
| 29 | + PixelFormat.kRGB888, |
| 30 | + PixelFormat.kRGBA8888, |
| 31 | + PixelFormat.kGrayscale8, |
| 32 | + PixelFormat.kRAW10, |
| 33 | + PixelFormat.kRAW16, |
| 34 | + PixelFormat.kNV12, |
| 35 | + PixelFormat.kNV21, |
| 36 | + ]: |
| 37 | + source = ImageSource( |
| 38 | + peripheral_id=0, |
| 39 | + width=640, |
| 40 | + height=480, |
| 41 | + format=fmt, |
| 42 | + frame_rate_hz=60, |
| 43 | + ) |
| 44 | + proto = source._to_proto() |
| 45 | + assert proto.image_source.format == fmt |
| 46 | + |
| 47 | + |
| 48 | +def test_image_source_from_proto(): |
| 49 | + proto = ImageSourceConfig( |
| 50 | + peripheral_id=2, |
| 51 | + width=3840, |
| 52 | + height=2160, |
| 53 | + format=PixelFormat.kNV21, |
| 54 | + frame_rate_hz=15, |
| 55 | + ) |
| 56 | + |
| 57 | + source = ImageSource._from_proto(proto) |
| 58 | + |
| 59 | + assert source.peripheral_id == 2 |
| 60 | + assert source.width == 3840 |
| 61 | + assert source.height == 2160 |
| 62 | + assert source.format == PixelFormat.kNV21 |
| 63 | + assert source.frame_rate_hz == 15 |
| 64 | + |
| 65 | + |
| 66 | +def test_image_source_from_proto_roundtrip(): |
| 67 | + original = ImageSource( |
| 68 | + peripheral_id=3, |
| 69 | + width=1280, |
| 70 | + height=720, |
| 71 | + format=PixelFormat.kGrayscale8, |
| 72 | + frame_rate_hz=120, |
| 73 | + ) |
| 74 | + |
| 75 | + proto = original._to_proto() |
| 76 | + restored = ImageSource._from_proto(proto.image_source) |
| 77 | + |
| 78 | + assert restored.peripheral_id == original.peripheral_id |
| 79 | + assert restored.width == original.width |
| 80 | + assert restored.height == original.height |
| 81 | + assert restored.format == original.format |
| 82 | + assert restored.frame_rate_hz == original.frame_rate_hz |
| 83 | + |
| 84 | + |
| 85 | +def test_image_source_from_invalid_proto(): |
| 86 | + with pytest.raises(ValueError, match="missing"): |
| 87 | + ImageSource._from_proto(None) |
| 88 | + |
| 89 | + with pytest.raises(ValueError, match="not of type"): |
| 90 | + ImageSource._from_proto("invalid") |
0 commit comments