Skip to content

Filter entities in the UI (part 5): Add snapshot tests for the blueprint tree #8728

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Jan 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5636,7 +5636,9 @@ name = "re_blueprint_tree"
version = "0.22.0-alpha.1+dev"
dependencies = [
"egui",
"egui_kittest",
"itertools 0.13.0",
"re_chunk_store",
"re_context_menu",
"re_data_ui",
"re_entity_db",
Expand All @@ -5645,6 +5647,7 @@ dependencies = [
"re_tracing",
"re_types",
"re_ui",
"re_view_spatial",
"re_viewer_context",
"re_viewport_blueprint",
"smallvec",
Expand Down Expand Up @@ -6761,7 +6764,6 @@ version = "0.22.0-alpha.1+dev"
dependencies = [
"ahash",
"egui",
"egui_kittest",
"fjadra",
"itertools 0.13.0",
"nohash-hasher",
Expand Down
1 change: 1 addition & 0 deletions crates/store/re_log_types/src/path/entity_path_filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ pub enum EntityPathFilterError {
///
/// Important: the same substitutions must be used in every place we resolve [`EntityPathFilter`] to
/// [`ResolvedEntityPathFilter`].
#[derive(Debug)]
pub struct EntityPathSubs(HashMap<String, String>);

impl EntityPathSubs {
Expand Down
9 changes: 9 additions & 0 deletions crates/viewer/re_blueprint_tree/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,12 @@ re_viewport_blueprint.workspace = true
egui.workspace = true
itertools.workspace = true
smallvec.workspace = true


[dev-dependencies]
re_viewer_context = { workspace = true, features = ["testing"] }
re_viewport_blueprint = { workspace = true, features = ["testing"] }
re_chunk_store.workspace = true
re_view_spatial.workspace = true

egui_kittest.workspace = true
5 changes: 5 additions & 0 deletions crates/viewer/re_blueprint_tree/src/blueprint_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ pub struct BlueprintTree {
}

impl BlueprintTree {
/// Activates the search filter (for e.g. test purposes).
pub fn activate_filter(&mut self, query: &str) {
self.filter_state.activate(query);
}

/// Show the Blueprint section of the left panel based on the current [`ViewportBlueprint`]
pub fn show(
&mut self,
Expand Down
208 changes: 208 additions & 0 deletions crates/viewer/re_blueprint_tree/tests/blueprint_tree_tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,208 @@
use egui::Vec2;

use re_blueprint_tree::BlueprintTree;
use re_chunk_store::external::re_chunk::ChunkBuilder;
use re_chunk_store::RowId;
use re_log_types::build_frame_nr;
use re_types::archetypes::Points3D;
use re_viewer_context::{test_context::TestContext, CollapseScope, RecommendedView, ViewClass};
use re_viewport_blueprint::{
test_context_ext::TestContextExt as _, ViewBlueprint, ViewportBlueprint,
};

#[test]
fn basic_blueprint_panel_should_match_snapshot() {
let mut test_context = TestContext::default();

test_context.register_view_class::<re_view_spatial::SpatialView3D>();

test_context.log_entity("/entity0".into(), add_point_to_chunk_builder);
test_context.log_entity("/entity1".into(), add_point_to_chunk_builder);
test_context.log_entity("/entity2".into(), add_point_to_chunk_builder);

test_context.setup_viewport_blueprint(|_, blueprint| {
blueprint.add_views(
std::iter::once(ViewBlueprint::new(
re_view_spatial::SpatialView3D::identifier(),
RecommendedView::root(),
)),
None,
None,
);
});

let blueprint_tree = BlueprintTree::default();
run_blueprint_panel_and_save_snapshot(test_context, blueprint_tree, "basic_blueprint_panel");
}

// ---

#[test]
fn collapse_expand_all_blueprint_panel_should_match_snapshot() {
for (snapshot_name, should_expand) in &[
("expand_all_blueprint_panel", true),
("collapse_all_blueprint_panel", false),
] {
let mut test_context = TestContext::default();

test_context.register_view_class::<re_view_spatial::SpatialView3D>();

test_context.log_entity("/path/to/entity0".into(), add_point_to_chunk_builder);
test_context.log_entity("/path/to/entity1".into(), add_point_to_chunk_builder);
test_context.log_entity("/another/way/to/entity2".into(), add_point_to_chunk_builder);

let view_id = test_context.setup_viewport_blueprint(|_ctx, blueprint| {
let view = ViewBlueprint::new(
re_view_spatial::SpatialView3D::identifier(),
RecommendedView::root(),
);

let view_id = view.id;
blueprint.add_views(std::iter::once(view), None, None);

// TODO(ab): add containers in the hierarchy (requires work on the container API,
// currently very cumbersome to use for testing purposes).

view_id
});

let mut blueprint_tree = BlueprintTree::default();

let mut harness = test_context
.setup_kittest_for_rendering()
.with_size(Vec2::new(400.0, 800.0))
.build_ui(|ui| {
test_context.run(&ui.ctx().clone(), |viewer_ctx| {
re_context_menu::collapse_expand::collapse_expand_view(
viewer_ctx,
&view_id,
CollapseScope::BlueprintTree,
*should_expand,
);

let blueprint = ViewportBlueprint::try_from_db(
viewer_ctx.store_context.blueprint,
viewer_ctx.blueprint_query,
);

blueprint_tree.show(viewer_ctx, &blueprint, ui);
});

test_context.handle_system_commands();
});

harness.run();
harness.snapshot(snapshot_name);
}
}

// ---

#[test]
fn blueprint_panel_filter_active_inside_origin_should_match_snapshot() {
let (test_context, blueprint_tree) = setup_filter_test(Some("left"));

run_blueprint_panel_and_save_snapshot(
test_context,
blueprint_tree,
"blueprint_panel_filter_active_inside_origin",
);
}

#[test]
fn blueprint_panel_filter_active_outside_origin_should_match_snapshot() {
let (test_context, blueprint_tree) = setup_filter_test(Some("out"));

run_blueprint_panel_and_save_snapshot(
test_context,
blueprint_tree,
"blueprint_panel_filter_active_outside_origin",
);
}

#[test]
fn blueprint_panel_filter_active_above_origin_should_match_snapshot() {
let (test_context, blueprint_tree) = setup_filter_test(Some("path"));

run_blueprint_panel_and_save_snapshot(
test_context,
blueprint_tree,
"blueprint_panel_filter_active_above_origin",
);
}

fn setup_filter_test(query: Option<&str>) -> (TestContext, BlueprintTree) {
let mut test_context = TestContext::default();
test_context.register_view_class::<re_view_spatial::SpatialView3D>();

test_context.log_entity("/path/to/left".into(), add_point_to_chunk_builder);
test_context.log_entity("/path/to/right".into(), add_point_to_chunk_builder);
test_context.log_entity("/path/is/outside".into(), add_point_to_chunk_builder);

test_context.setup_viewport_blueprint(|_, blueprint| {
blueprint.add_views(
std::iter::once(ViewBlueprint::new(
re_view_spatial::SpatialView3D::identifier(),
RecommendedView {
origin: "/path/to".into(),
query_filter: "+ /**".try_into().expect("valid entity path filter"),
},
)),
None,
None,
);
});

let mut blueprint_tree = BlueprintTree::default();

// This trick here is to run the blueprint panel for a frame, such that it registers the current
// application id. This way, the blueprint panel will not discard the filter state we set up
// when it's run for the snapshot.
test_context.run_in_egui_central_panel(|ctx, ui| {
let blueprint =
ViewportBlueprint::try_from_db(ctx.store_context.blueprint, ctx.blueprint_query);

blueprint_tree.show(ctx, &blueprint, ui);
});

if let Some(query) = query {
blueprint_tree.activate_filter(query);
}

(test_context, blueprint_tree)
}

// ---

fn add_point_to_chunk_builder(builder: ChunkBuilder) -> ChunkBuilder {
builder.with_archetype(
RowId::new(),
[build_frame_nr(0)],
&Points3D::new([[0.0, 0.0, 0.0]]),
)
}

fn run_blueprint_panel_and_save_snapshot(
mut test_context: TestContext,
mut blueprint_tree: BlueprintTree,
snapshot_name: &str,
) {
let mut harness = test_context
.setup_kittest_for_rendering()
.with_size(Vec2::new(400.0, 800.0))
.build_ui(|ui| {
test_context.run(&ui.ctx().clone(), |viewer_ctx| {
let blueprint = ViewportBlueprint::try_from_db(
viewer_ctx.store_context.blueprint,
viewer_ctx.blueprint_query,
);

blueprint_tree.show(viewer_ctx, &blueprint, ui);
});

test_context.handle_system_commands();
});

harness.run();
harness.snapshot(snapshot_name);
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion crates/viewer/re_view_graph/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ itertools.workspace = true
nohash-hasher.workspace = true

[dev-dependencies]
egui_kittest.workspace = true
re_chunk_store.workspace = true
re_viewer_context = { workspace = true, features = ["testing"] }
re_viewport.workspace = true
re_viewport_blueprint = { workspace = true, features = ["testing"] }
Loading
Loading