grim-rs 0.1.9

Rust implementation of grim screenshot utility for Wayland
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
use grim_rs::pixel_format::{self, PixelFormat};
use grim_rs::{Backend, CaptureParameters, CaptureResult, Error, Grim, Region};
use std::collections::HashMap;

#[test]
fn test_box_struct_creation() {
    let box1 = Region::new(10, 20, 100, 200);
    assert_eq!(box1.x(), 10);
    assert_eq!(box1.y(), 20);
    assert_eq!(box1.width(), 100);
    assert_eq!(box1.height(), 200);
}

#[test]
fn test_box_is_empty() {
    let box1 = Region::new(0, 0, 0, 0);
    assert!(box1.is_empty());
    let box2 = Region::new(0, 0, -10, 10);
    assert!(box2.is_empty());
    let box3 = Region::new(0, 0, 10, -5);
    assert!(box3.is_empty());
    let box4 = Region::new(0, 0, 10, 10);
    assert!(!box4.is_empty());
}

#[test]
fn test_box_intersection() {
    let box1 = Region::new(0, 0, 100, 100);
    let box2 = Region::new(50, 50, 100, 100);
    assert!(box1.intersects(&box2));
    let intersection = box1.intersection(&box2).unwrap();
    assert_eq!(intersection.x(), 50);
    assert_eq!(intersection.y(), 50);
    assert_eq!(intersection.width(), 50);
    assert_eq!(intersection.height(), 50);
    let box3 = Region::new(0, 0, 10, 10);
    let box4 = Region::new(100, 100, 10, 10);
    assert!(!box3.intersects(&box4));
    assert!(box3.intersection(&box4).is_none());
}

#[test]
fn test_box_string_parsing() {
    let box_str = "10,20 300x400";
    let parsed: Region = box_str.parse().unwrap();
    assert_eq!(parsed.x(), 10);
    assert_eq!(parsed.y(), 20);
    assert_eq!(parsed.width(), 300);
    assert_eq!(parsed.height(), 400);
    assert_eq!(parsed.to_string(), "10,20 300x400");
}

#[test]
fn test_capture_result_struct() {
    let data = vec![255u8; 400];
    let result = CaptureResult::new(data, 10, 10);
    assert_eq!(result.width(), 10);
    assert_eq!(result.height(), 10);
    assert_eq!(result.data().len(), 400); // 10x10x4
}

#[test]
fn test_capture_parameters_struct() {
    let params = CaptureParameters::new("eDP-1")
        .region(Region::new(0, 0, 800, 600))
        .overlay_cursor(true)
        .scale(1.5);
    assert_eq!(params.output_name(), "eDP-1");
    assert_eq!(params.region_ref(), Some(&Region::new(0, 0, 800, 600)));
    assert!(params.overlay_cursor_enabled());
    assert_eq!(params.scale_factor(), Some(1.5));
}

#[test]
fn test_error_messages() {
    let error = grim_rs::Error::InvalidGeometry("test".to_string());
    assert!(error.to_string().contains("Invalid geometry format"));
    let error = grim_rs::Error::NoOutputs;
    assert_eq!(error.to_string(), "No outputs available");
    let error = grim_rs::Error::OutputNotFound("test".to_string());
    assert!(error.to_string().contains("Output not found"));
}

#[test]
fn test_crate_export_structs() {
    let _box = Region::new(0, 0, 100, 100);
    let _params = CaptureParameters::new("test");
    let _result = CaptureResult::new(vec![], 0, 0);
}

#[test]
fn test_image_data_format() {
    let width = 2;
    let height = 2;
    let data = [
        255, 0, 0, 255, // Red pixel
        0, 255, 0, 255, // Green pixel
        0, 0, 255, 255, // Blue pixel
        255, 255, 255, 255, // White pixel
    ];
    assert_eq!(data.len(), (width * height * 4) as usize);
    assert_eq!(data[0], 255); // R
    assert_eq!(data[1], 0); // G
    assert_eq!(data[2], 0); // B
    assert_eq!(data[3], 255); // A
}

#[test]
fn test_convert_xrgb8888_to_rgba() {
    let mut pixel_data = vec![10, 20, 30, 99];
    pixel_format::convert_to_rgba(&mut pixel_data, PixelFormat::Xrgb8888);
    assert_eq!(pixel_data, vec![30, 20, 10, 255]);
}

#[test]
fn test_convert_argb8888_to_rgba_preserves_alpha() {
    let mut pixel_data = vec![10, 20, 30, 40];
    pixel_format::convert_to_rgba(&mut pixel_data, PixelFormat::Argb8888);
    assert_eq!(pixel_data, vec![30, 20, 10, 40]);
}

#[test]
fn test_png_compression_levels() {
    let test_data = vec![255u8; 100 * 100 * 4]; // 100x100 image
    if let Ok(grim) = Grim::new() {
        let _ = grim.to_png(&test_data, 100, 100);
        let _ = grim.to_png_with_compression(&test_data, 100, 100, 0);
        let _ = grim.to_png_with_compression(&test_data, 100, 100, 6);
        let _ = grim.to_png_with_compression(&test_data, 100, 100, 9);
    }
}

#[test]
fn test_capture_parameters_default_behavior() {
    let params = CaptureParameters::new("test");
    assert_eq!(params.output_name(), "test");
    assert!(params.region_ref().is_none());
    assert!(!params.overlay_cursor_enabled());
    assert!(params.scale_factor().is_none());
}

#[cfg(feature = "jpeg")]
#[test]
fn test_jpeg_functionality_available() {
    let test_data = vec![255u8; 10 * 10 * 4];
    if let Ok(grim) = Grim::new() {
        let jpeg_result = grim.to_jpeg(&test_data, 10, 10);
        assert!(jpeg_result.is_ok());
        let jpeg_result_with_quality = grim.to_jpeg_with_quality(&test_data, 10, 10, 85);
        assert!(jpeg_result_with_quality.is_ok());
    }
}

#[cfg(not(feature = "jpeg"))]
#[test]
fn test_jpeg_functionality_unavailable() {
    let test_data = vec![255u8; 10 * 10 * 4];
    match Grim::new() {
        Ok(grim) => {
            let jpeg_result = grim.to_jpeg(&test_data, 10, 10);
            assert!(jpeg_result.is_err());
        }
        Err(_) => {}
    }
}

#[test]
fn test_multi_output_capture_result() {
    let mut outputs_map = HashMap::new();
    outputs_map.insert(
        "output1".to_string(),
        CaptureResult::new(vec![255u8; 100 * 100 * 4], 100, 100),
    );
    outputs_map.insert(
        "output2".to_string(),
        CaptureResult::new(vec![128u8; 200 * 150 * 4], 200, 150),
    );
    let multi_result = grim_rs::MultiOutputCaptureResult::new(outputs_map);
    assert_eq!(multi_result.outputs().len(), 2);
    assert!(multi_result.outputs().contains_key("output1"));
    assert!(multi_result.outputs().contains_key("output2"));
    let output1_result = multi_result.get("output1").unwrap();
    assert_eq!(output1_result.width(), 100);
    assert_eq!(output1_result.height(), 100);
    assert_eq!(output1_result.data().len(), 100 * 100 * 4);
}

#[test]
fn test_scale_functionality_validation() {
    let scales = [0.5, 1.0, 1.5, 2.0, 0.25];
    for scale in scales.iter() {
        let new_width = (800.0 * scale) as u32;
        let new_height = (600.0 * scale) as u32;
        assert!(new_width > 0);
        assert!(new_height > 0);
    }
}

#[test]
fn test_geometry_bounds_checking() {
    let invalid_box = Region::new(0, 0, -10, 100);
    assert!(invalid_box.is_empty());
    let invalid_box2 = Region::new(0, 0, 100, -10);
    assert!(invalid_box2.is_empty());
    let valid_box = Region::new(10, 10, 100, 100);
    assert!(!valid_box.is_empty());
}

#[test]
fn test_region_intersection_with_outputs() {
    let output_box = Region::new(0, 0, 1920, 1080);
    let capture_region = Region::new(100, 100, 500, 500);
    assert!(output_box.intersects(&capture_region));
    let intersection = output_box.intersection(&capture_region).unwrap();
    assert_eq!(intersection.x(), 100);
    assert_eq!(intersection.y(), 100);
    assert_eq!(intersection.width(), 500);
    assert_eq!(intersection.height(), 500);
    let region_outside = Region::new(2000, 2000, 100, 100);
    assert!(!output_box.intersects(&region_outside));
    assert!(output_box.intersection(&region_outside).is_none());
}

mod transform_tests {
    #[test]
    fn test_transform_normal() {
        let width = 1920;
        let height = 1080;
        assert_eq!(width, 1920);
        assert_eq!(height, 1080);
    }

    /// Test that 90° rotation swaps width and height
    #[test]
    fn test_transform_90_degree_rotation() {
        let original_width = 1920;
        let original_height = 1080;
        let expected_width = 1080;
        let expected_height = 1920;
        assert_ne!(original_width, expected_width);
        assert_ne!(original_height, expected_height);
        assert_eq!(original_width, expected_height);
        assert_eq!(original_height, expected_width);
    }

    /// Test that 180° rotation keeps same dimensions
    #[test]
    fn test_transform_180_degree_rotation() {
        let width = 1920;
        let height = 1080;
        assert_eq!(width, 1920);
        assert_eq!(height, 1080);
    }

    /// Test that 270° rotation swaps width and height
    #[test]
    fn test_transform_270_degree_rotation() {
        let original_width = 1920;
        let original_height = 1080;
        let expected_width = 1080;
        let expected_height = 1920;
        assert_eq!(original_width, expected_height);
        assert_eq!(original_height, expected_width);
    }

    /// Test flipped transform behavior
    #[test]
    fn test_transform_flipped() {
        let width = 1920;
        let height = 1080;
        assert_eq!(width, 1920);
        assert_eq!(height, 1080);
    }

    /// Test flipped 90° rotation
    #[test]
    fn test_transform_flipped_90() {
        let original_width = 1920;
        let original_height = 1080;
        let expected_width = 1080;
        let expected_height = 1920;
        assert_eq!(original_width, expected_height);
        assert_eq!(original_height, expected_width);
    }

    /// Test multiple outputs with different transforms
    #[test]
    fn test_multi_output_with_transforms() {
        struct TestOutput {
            width: i32,
            height: i32,
            rotated: bool,
        }
        let outputs = [
            TestOutput {
                width: 1920,
                height: 1080,
                rotated: false,
            },
            TestOutput {
                width: 1080,
                height: 1920,
                rotated: true,
            },
        ];
        assert_eq!(outputs[0].width, 1920);
        assert_eq!(outputs[0].height, 1080);
        assert!(!outputs[0].rotated);
        assert_eq!(outputs[1].width, 1080);
        assert_eq!(outputs[1].height, 1920);
        assert!(outputs[1].rotated);
        assert_eq!(outputs[0].width, outputs[1].height);
        assert_eq!(outputs[0].height, outputs[1].width);
    }

    /// Test logical geometry calculation with transforms
    #[test]
    fn test_logical_geometry_with_scale_and_transform() {
        let physical_width = 3840;
        let physical_height = 2160;
        let scale = 2;
        let logical_width = physical_width / scale;
        let logical_height = physical_height / scale;
        assert_eq!(logical_width, 1920);
        assert_eq!(logical_height, 1080);
        let logical_width_rotated = logical_height;
        let logical_height_rotated = logical_width;
        assert_eq!(logical_width_rotated, 1080);
        assert_eq!(logical_height_rotated, 1920);
    }

    /// Test transform integration - verify dimensions change correctly
    #[test]
    fn test_transform_integration_dimensions() {
        let original_width = 1920;
        let original_height = 1080;
        let rotated_width = 1080;
        let rotated_height = 1920;
        assert_eq!(original_width, rotated_height);
        assert_eq!(original_height, rotated_width);
    }

    /// Test that transform is applied to captured data
    #[test]
    fn test_image_transform_application() {
        let test_data: Vec<u8> = vec![
            255, 0, 0, 255, // Red
            0, 255, 0, 255, // Green
            0, 0, 255, 255, // Blue
            255, 255, 255, 255, // White
        ];
        assert_eq!(test_data.len(), 2 * 2 * 4);
    }

    /// Test flipped transforms preserve dimensions
    #[test]
    fn test_flipped_transforms_dimensions() {
        let width = 1920;
        let height = 1080;
        assert_eq!(width, 1920);
        assert_eq!(height, 1080);
        assert_eq!(width, 1920);
        assert_eq!(height, 1080);
    }

    /// Test rotation angle constants
    #[test]
    fn test_rotation_angles() {
        use std::f64::consts::{FRAC_PI_2, PI};
        let angle_90 = FRAC_PI_2; // π/2
        let angle_180 = PI; // π
        let angle_270 = 3.0 * FRAC_PI_2; // 3π/2
        assert!(angle_90 > 0.0 && angle_90 < PI);
        assert_eq!(angle_180, PI);
        assert!(angle_270 > PI && angle_270 < 2.0 * PI);
    }
}

#[cfg(test)]
mod y_invert_tests {
    /// Test Y-invert flag constant
    #[test]
    fn test_y_invert_flag_value() {
        const Y_INVERT: u32 = 1;
        assert_eq!(Y_INVERT, 1);
        assert_eq!(Y_INVERT & 1, 1);
    }

    /// Test Y-invert flag detection
    #[test]
    fn test_y_invert_flag_detection() {
        const Y_INVERT: u32 = 1;
        let flags_with_invert = 1u32;
        assert_ne!(flags_with_invert & Y_INVERT, 0);
        let flags_without_invert = 0u32;
        assert_eq!(flags_without_invert & Y_INVERT, 0);
        let flags_mixed = 3u32;
        assert_ne!(flags_mixed & Y_INVERT, 0);
    }

    /// Test that Y-invert preserves dimensions
    #[test]
    fn test_y_invert_preserves_dimensions() {
        let width = 1920;
        let height = 1080;
        assert_eq!(width, 1920);
        assert_eq!(height, 1080);
    }

    /// Test Y-invert with transform combination
    #[test]
    fn test_y_invert_with_transform() {
        let _original_width = 1920;
        let _original_height = 1080;
        let transformed_width = 1080;
        let transformed_height = 1920;
        let final_width = transformed_width;
        let final_height = transformed_height;
        assert_eq!(final_width, 1080);
        assert_eq!(final_height, 1920);
    }

    #[test]
    fn test_frame_state_flags_field() {
        let flags: u32 = 0;
        assert_eq!(flags, 0);

        let flags_with_invert: u32 = 1;
        assert_eq!(flags_with_invert, 1);
    }
}

#[test]
fn test_mock_capture() {
    let result = std::panic::catch_unwind(|| {
        let mut grim = Grim::new().unwrap();
        grim.capture_all()
    });

    match result {
        Ok(capture_result) => {
            if let Ok(capture) = capture_result {
                assert_eq!(
                    capture.data().len(),
                    (capture.width() * capture.height() * 4) as usize
                );
            } else {
                assert!(matches!(capture_result, Err(Error::NoOutputs)));
            }
        }
        Err(_) => {
            panic!("Test panicked unexpectedly");
        }
    }
}

#[test]
#[cfg(feature = "png")]
fn test_to_png() {
    let grim = Grim::new().unwrap();
    let test_data = vec![255u8; 64];
    let png_data = grim.to_png(&test_data, 4, 4).unwrap();
    assert!(!png_data.is_empty());
}

#[test]
#[cfg(feature = "jpeg")]
fn test_to_jpeg() {
    let grim = Grim::new().unwrap();
    let test_data = vec![255u8; 64];
    let jpeg_data = grim.to_jpeg(&test_data, 4, 4).unwrap();
    assert!(!jpeg_data.is_empty());
}

#[test]
#[cfg(not(feature = "jpeg"))]
fn test_jpeg_disabled() {
    let grim = Grim::new().unwrap();
    let test_data = vec![255u8; 16];
    let jpeg_result = grim.to_jpeg(&test_data, 4, 4);
    assert!(jpeg_result.is_err());
}

#[test]
fn test_read_region_from_stdin() {
    let region_str = "10,20 300x400";
    let result: std::result::Result<Region, _> = region_str.parse();
    assert!(result.is_ok());
    let region = result.unwrap();
    assert_eq!(region.x(), 10);
    assert_eq!(region.y(), 20);
    assert_eq!(region.width(), 300);
    assert_eq!(region.height(), 400);
}

#[test]
fn test_scale_functionality() {
    let mut grim = Grim::new().unwrap();
    let test_capture = grim.capture_all_with_scale(1.0);
    match test_capture {
        Ok(_) => {}
        Err(Error::NoOutputs) => {}
        Err(e) => panic!("Unexpected error: {:?}", e),
    }
}

#[test]
fn test_backend_enum_variants() {
    let auto = Backend::Auto;
    let ext = Backend::ExtImageCopyCapture;
    let wlr = Backend::WlrScreencopy;
    assert_ne!(auto, ext);
    assert_ne!(ext, wlr);
    assert_eq!(auto, Backend::Auto);
    // Auto is the default-like variant
    assert!(matches!(auto, Backend::Auto));
}

#[test]
fn test_new_ext_constructor() {
    match Grim::new_ext() {
        Ok(_) => {}
        Err(e) => {
            assert!(
                e.to_string().contains("not available"),
                "expected UnsupportedProtocol, got: {e}"
            );
        }
    }
}

#[test]
fn test_new_wlr_constructor() {
    match Grim::new_wlr() {
        Ok(_) => {}
        Err(e) => {
            assert!(
                e.to_string().contains("not available"),
                "expected UnsupportedProtocol, got: {e}"
            );
        }
    }
}

#[test]
fn test_new_auto_is_equivalent_to_new() {
    let result_auto = Grim::new();
    match result_auto {
        Ok(_) | Err(Error::NoOutputs) => {} // expected
        Err(e) => panic!("Unexpected error from Grim::new(): {e}"),
    }
}

#[test]
fn test_new_ext_can_capture() {
    if let Ok(mut grim) = Grim::new_ext() {
        match grim.capture_all() {
            Ok(result) => {
                assert_eq!(
                    result.data().len(),
                    (result.width() * result.height() * 4) as usize
                );
            }
            Err(Error::NoOutputs) => {} // no monitors connected
            Err(e) => panic!("Capture failed on ext backend: {e}"),
        }
    }
    // ext not available — skip
}

#[test]
fn test_new_wlr_can_capture() {
    if let Ok(mut grim) = Grim::new_wlr() {
        match grim.capture_all() {
            Ok(result) => {
                assert_eq!(
                    result.data().len(),
                    (result.width() * result.height() * 4) as usize
                );
            }
            Err(Error::NoOutputs) => {} // no monitors connected
            Err(e) => panic!("Capture failed on wlr backend: {e}"),
        }
    }
    // wlr not available — skip
}