Skip to content

Commit ecf8dd0

Browse files
authored
Snapshot utility function in TestContext (#11328)
### What Bikeshedding: replaces copies of the `run_view_ui_and_save_snapshot` function and adds it to TestContext. Some of the snapshots had to be updated because the new utility function has one more `harness.run()`.
1 parent b80b427 commit ecf8dd0

22 files changed

+83
-210
lines changed

crates/viewer/re_test_viewport/src/lib.rs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ mod test_view;
44

55
use ahash::HashMap;
66

7-
use re_test_context::TestContext;
7+
use re_test_context::{TestContext, external::egui_kittest::SnapshotOptions};
88
use re_viewer_context::{Contents, ViewId, ViewerContext, VisitorControlFlow};
99

1010
use re_viewport_blueprint::{DataQueryPropertyResolver, ViewBlueprint, ViewportBlueprint};
@@ -26,6 +26,14 @@ pub trait TestContextExt {
2626

2727
/// [`TestContext::run`] inside a central panel that displays the ui for a single given view.
2828
fn run_with_single_view(&mut self, ui: &mut egui::Ui, view_id: ViewId);
29+
30+
fn run_view_ui_and_save_snapshot(
31+
&mut self,
32+
view_id: ViewId,
33+
snapshot_name: &str,
34+
size: egui::Vec2,
35+
snapshot_options: Option<SnapshotOptions>,
36+
);
2937
}
3038

3139
impl TestContextExt for TestContext {
@@ -166,4 +174,22 @@ impl TestContextExt for TestContext {
166174

167175
self.handle_system_commands();
168176
}
177+
178+
fn run_view_ui_and_save_snapshot(
179+
&mut self,
180+
view_id: ViewId,
181+
snapshot_name: &str,
182+
size: egui::Vec2,
183+
snapshot_options: Option<SnapshotOptions>,
184+
) {
185+
let snapshot_options = snapshot_options.unwrap_or_default();
186+
let mut harness = self
187+
.setup_kittest_for_rendering()
188+
.with_size(size)
189+
.build_ui(|ui| {
190+
self.run_with_single_view(ui, view_id);
191+
});
192+
harness.run();
193+
harness.snapshot_options(snapshot_name, &snapshot_options);
194+
}
169195
}

crates/viewer/re_view_bar_chart/tests/tensor_1d.rs renamed to crates/viewer/re_view_bar_chart/tests/bar_chart_test.rs

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ fn x() -> Vec<f32> {
1515
fn test_bar_chart() {
1616
let mut test_context = TestContext::new_with_view_class::<BarChartView>();
1717

18-
test_context.log_entity("tensor", |builder| {
18+
test_context.log_entity("time_series", |builder| {
1919
builder.with_archetype(
2020
RowId::new(),
2121
TimePoint::STATIC,
@@ -24,11 +24,11 @@ fn test_bar_chart() {
2424
});
2525

2626
let view_id = setup_blueprint(&mut test_context);
27-
run_view_ui_and_save_snapshot(
28-
&mut test_context,
27+
test_context.run_view_ui_and_save_snapshot(
2928
view_id,
30-
"tensor_1d",
29+
"bar_chart_1d",
3130
egui::vec2(400.0, 300.0),
31+
None,
3232
);
3333
}
3434

@@ -39,19 +39,3 @@ fn setup_blueprint(test_context: &mut TestContext) -> ViewId {
3939
))
4040
})
4141
}
42-
43-
fn run_view_ui_and_save_snapshot(
44-
test_context: &mut TestContext,
45-
view_id: ViewId,
46-
name: &str,
47-
size: egui::Vec2,
48-
) {
49-
let mut harness = test_context
50-
.setup_kittest_for_rendering()
51-
.with_size(size)
52-
.build_ui(|ui| {
53-
test_context.run_with_single_view(ui, view_id);
54-
});
55-
harness.run();
56-
harness.snapshot(name);
57-
}
Lines changed: 2 additions & 2 deletions
Loading

crates/viewer/re_view_bar_chart/tests/snapshots/tensor_1d.png

Lines changed: 0 additions & 3 deletions
This file was deleted.

crates/viewer/re_view_dataframe/tests/basic.rs

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@ pub fn test_null_timeline() {
2828
});
2929

3030
let view_id = setup_blueprint(&mut test_context, timeline_a.name());
31-
run_view_ui_and_save_snapshot(
32-
&mut test_context,
31+
test_context.run_view_ui_and_save_snapshot(
3332
view_id,
3433
"null_timeline",
3534
egui::vec2(400.0, 200.0),
35+
None,
3636
);
3737
}
3838

@@ -51,11 +51,11 @@ pub fn test_unknown_timeline() {
5151

5252
let view_id = setup_blueprint(&mut test_context, &TimelineName::from("unknown_timeline"));
5353

54-
run_view_ui_and_save_snapshot(
55-
&mut test_context,
54+
test_context.run_view_ui_and_save_snapshot(
5655
view_id,
5756
"unknown_timeline_view_ui",
5857
egui::vec2(300.0, 150.0),
58+
None,
5959
);
6060

6161
run_view_selection_panel_ui_and_save_snapshot(
@@ -89,23 +89,6 @@ fn setup_blueprint(test_context: &mut TestContext, timeline_name: &TimelineName)
8989
})
9090
}
9191

92-
fn run_view_ui_and_save_snapshot(
93-
test_context: &mut TestContext,
94-
view_id: ViewId,
95-
name: &str,
96-
size: egui::Vec2,
97-
) {
98-
let mut harness = test_context
99-
.setup_kittest_for_rendering()
100-
.with_size(size)
101-
.build_ui(|ui| {
102-
test_context.run_with_single_view(ui, view_id);
103-
});
104-
105-
harness.run();
106-
harness.snapshot(name);
107-
}
108-
10992
fn run_view_selection_panel_ui_and_save_snapshot(
11093
test_context: &mut TestContext,
11194
view_id: ViewId,

crates/viewer/re_view_spatial/tests/bgr_images.rs

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use re_types::{
1313
Archetype as _, blueprint::components::BackgroundKind, datatypes::ColorModel,
1414
image::ImageChannelType,
1515
};
16-
use re_viewer_context::{ViewClass as _, ViewId};
16+
use re_viewer_context::ViewClass as _;
1717
use re_viewport_blueprint::ViewBlueprint;
1818

1919
fn convert_pixels_to<T: From<u8> + Copy>(u8s: &[u8]) -> Vec<T> {
@@ -55,30 +55,11 @@ fn run_bgr_test<T: ImageChannelType>(image: &[T], size: [u32; 2], color_model: C
5555
color_model.to_string().to_lowercase(),
5656
);
5757

58-
run_view_ui_and_save_snapshot(
59-
&mut test_context,
58+
test_context.run_view_ui_and_save_snapshot(
6059
view_id,
6160
&snapshot_name,
6261
egui::vec2(160.0, 120.0),
63-
);
64-
}
65-
66-
fn run_view_ui_and_save_snapshot(
67-
test_context: &mut TestContext,
68-
view_id: ViewId,
69-
name: &str,
70-
size: egui::Vec2,
71-
) {
72-
let mut harness = test_context
73-
.setup_kittest_for_rendering()
74-
.with_size(size)
75-
.build_ui(|ui| {
76-
test_context.run_with_single_view(ui, view_id);
77-
});
78-
harness.run();
79-
harness.snapshot_options(
80-
name,
81-
&SnapshotOptions::new().failed_pixel_count_threshold(OsThreshold::new(2)),
62+
Some(SnapshotOptions::new().failed_pixel_count_threshold(OsThreshold::new(2))),
8263
);
8364
}
8465

crates/viewer/re_view_spatial/tests/blueprint_2d.rs

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ pub fn test_blueprint_no_overrides_or_defaults_with_spatial_2d() {
1616
log_arrows(&mut test_context);
1717

1818
let view_id = setup_blueprint(&mut test_context, None, None);
19-
run_view_ui_and_save_snapshot(
20-
&mut test_context,
19+
test_context.run_view_ui_and_save_snapshot(
2120
view_id,
2221
"blueprint_no_overrides_or_defaults_with_spatial_2d",
2322
SNAPSHOT_SIZE,
23+
None,
2424
);
2525
}
2626

@@ -31,11 +31,11 @@ pub fn test_blueprint_overrides_with_spatial_2d() {
3131
log_arrows(&mut test_context);
3232

3333
let view_id = setup_blueprint(&mut test_context, Some(&arrow_overrides()), None);
34-
run_view_ui_and_save_snapshot(
35-
&mut test_context,
34+
test_context.run_view_ui_and_save_snapshot(
3635
view_id,
3736
"blueprint_overrides_with_spatial_2d",
3837
SNAPSHOT_SIZE,
38+
None,
3939
);
4040
}
4141

@@ -46,11 +46,11 @@ pub fn test_blueprint_defaults_with_spatial_2d() {
4646
log_arrows(&mut test_context);
4747

4848
let view_id = setup_blueprint(&mut test_context, None, Some(&arrow_defaults()));
49-
run_view_ui_and_save_snapshot(
50-
&mut test_context,
49+
test_context.run_view_ui_and_save_snapshot(
5150
view_id,
5251
"blueprint_defaults_with_spatial_2d",
5352
SNAPSHOT_SIZE,
53+
None,
5454
);
5555
}
5656

@@ -114,19 +114,3 @@ fn setup_blueprint(
114114
blueprint.add_view_at_root(view)
115115
})
116116
}
117-
118-
fn run_view_ui_and_save_snapshot(
119-
test_context: &mut TestContext,
120-
view_id: ViewId,
121-
name: &str,
122-
size: egui::Vec2,
123-
) {
124-
let mut harness = test_context
125-
.setup_kittest_for_rendering()
126-
.with_size(size)
127-
.build_ui(|ui| {
128-
test_context.run_with_single_view(ui, view_id);
129-
});
130-
harness.run();
131-
harness.snapshot(name);
132-
}

crates/viewer/re_view_spatial/tests/visible_time_range.rs

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ fn run_visible_time_range_test(
304304
add_data(&mut test_context);
305305

306306
let view_id = setup_blueprint(&mut test_context, view_time_range, green_time_range);
307-
run_view_ui_and_save_snapshot(&mut test_context, view_id, name, egui::vec2(200.0, 200.0));
307+
test_context.run_view_ui_and_save_snapshot(view_id, name, egui::vec2(200.0, 200.0), None);
308308
}
309309

310310
fn setup_blueprint(
@@ -358,20 +358,3 @@ fn setup_blueprint(
358358
view_id
359359
})
360360
}
361-
362-
fn run_view_ui_and_save_snapshot(
363-
test_context: &mut TestContext,
364-
view_id: ViewId,
365-
name: &str,
366-
size: egui::Vec2,
367-
) {
368-
let mut harness = test_context
369-
.setup_kittest_for_rendering()
370-
.with_size(size)
371-
.build_ui(|ui| {
372-
test_context.run_with_single_view(ui, view_id);
373-
});
374-
375-
harness.run();
376-
harness.snapshot(name);
377-
}

crates/viewer/re_view_tensor/tests/tensor_1d.rs

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,7 @@ fn test_tensor() {
2424
});
2525

2626
let view_id = setup_blueprint(&mut test_context);
27-
run_view_ui_and_save_snapshot(
28-
&mut test_context,
29-
view_id,
30-
"tensor_1d",
31-
egui::vec2(300.0, 50.0),
32-
);
27+
test_context.run_view_ui_and_save_snapshot(view_id, "tensor_1d", egui::vec2(300.0, 50.0), None);
3328
}
3429

3530
fn setup_blueprint(test_context: &mut TestContext) -> ViewId {
@@ -39,19 +34,3 @@ fn setup_blueprint(test_context: &mut TestContext) -> ViewId {
3934
))
4035
})
4136
}
42-
43-
fn run_view_ui_and_save_snapshot(
44-
test_context: &mut TestContext,
45-
view_id: ViewId,
46-
name: &str,
47-
size: egui::Vec2,
48-
) {
49-
let mut harness = test_context
50-
.setup_kittest_for_rendering()
51-
.with_size(size)
52-
.build_ui(|ui| {
53-
test_context.run_with_single_view(ui, view_id);
54-
});
55-
harness.run();
56-
harness.snapshot(name);
57-
}

0 commit comments

Comments
 (0)