Skip to content
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

Use test context in component override tests #6499

Merged
merged 1 commit into from
Jun 5, 2024
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
28 changes: 17 additions & 11 deletions crates/re_viewer_context/src/test_context.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use crate::{
command_channel, ApplicationSelectionState, ComponentUiRegistry, StoreContext, ViewerContext,
command_channel, ApplicationSelectionState, ComponentUiRegistry, RecordingConfig,
SpaceViewClassRegistry, StoreContext, ViewerContext,
};

use re_data_store::LatestAtQuery;
use re_entity_db::EntityDb;
use re_log_types::{StoreId, StoreKind};
use re_log_types::{StoreId, StoreKind, Timeline};

/// Harness to execute code that rely on [`crate::ViewerContext`].
///
Expand All @@ -19,19 +20,24 @@ use re_log_types::{StoreId, StoreKind};
/// });
/// ```
pub struct TestContext {
recording_store: EntityDb,
blueprint_store: EntityDb,
selection_state: ApplicationSelectionState,
pub recording_store: EntityDb,
pub blueprint_store: EntityDb,
pub space_view_class_registry: SpaceViewClassRegistry,
pub selection_state: ApplicationSelectionState,
pub active_timeline: Timeline,
}

impl Default for TestContext {
fn default() -> Self {
let recording_store = EntityDb::new(StoreId::random(StoreKind::Recording));
let blueprint_store = EntityDb::new(StoreId::random(StoreKind::Blueprint));
let active_timeline = Timeline::new("time", re_log_types::TimeType::Time);
Self {
recording_store,
blueprint_store,
space_view_class_registry: Default::default(),
selection_state: Default::default(),
active_timeline,
}
}
}
Expand All @@ -47,10 +53,7 @@ impl TestContext {
pub fn run(&self, mut func: impl FnMut(&ViewerContext<'_>, &mut egui::Ui)) {
egui::__run_test_ui(|ui| {
let re_ui = re_ui::ReUi::load_and_apply(ui.ctx());
let blueprint_query = LatestAtQuery::latest(re_log_types::Timeline::new(
"timeline",
re_log_types::TimeType::Time,
));
let blueprint_query = LatestAtQuery::latest(self.active_timeline);
let (command_sender, _) = command_channel();
let component_ui_registry = ComponentUiRegistry::new(Box::new(
|_ctx, _ui, _ui_layout, _query, _db, _entity_path, _component, _instance| {},
Expand All @@ -65,16 +68,19 @@ impl TestContext {
hub: &Default::default(),
};

let rec_cfg = RecordingConfig::default();
rec_cfg.time_ctrl.write().set_timeline(self.active_timeline);

let ctx = ViewerContext {
app_options: &Default::default(),
cache: &Default::default(),
component_ui_registry: &component_ui_registry,
space_view_class_registry: &Default::default(),
space_view_class_registry: &self.space_view_class_registry,
store_context: &store_context,
applicable_entities_per_visualizer: &Default::default(),
indicated_entities_per_visualizer: &Default::default(),
query_results: &Default::default(),
rec_cfg: &Default::default(),
rec_cfg: &rec_cfg,
blueprint_cfg: &Default::default(),
selection_state: &self.selection_state,
blueprint_query: &blueprint_query,
Expand Down
160 changes: 71 additions & 89 deletions crates/re_viewport_blueprint/src/space_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -457,11 +457,13 @@ mod tests {
};
use re_types::{archetypes::Points3D, ComponentBatch, ComponentName, Loggable as _};
use re_viewer_context::{
blueprint_timeline, IndicatedEntities, OverridePath, PerVisualizer, SpaceViewClassRegistry,
StoreContext, VisualizableEntities,
test_context::TestContext, IndicatedEntities, OverridePath, PerVisualizer, StoreContext,
VisualizableEntities,
};
use std::collections::HashMap;

use crate::space_view_contents::DataQueryPropertyResolver;

use super::*;

fn save_override(props: EntityProperties, path: &EntityPath, store: &mut EntityDb) {
Expand All @@ -479,10 +481,7 @@ mod tests {

#[test]
fn test_entity_properties() {
let space_view_class_registry = SpaceViewClassRegistry::default();
let timeline = Timeline::new("time", re_log_types::TimeType::Time);
let mut recording = EntityDb::new(StoreId::random(re_log_types::StoreKind::Recording));
let mut blueprint = EntityDb::new(StoreId::random(re_log_types::StoreKind::Blueprint));
let mut test_ctx = TestContext::default();
let legacy_auto_properties = EntityPropertyMap::default();

let points = Points3D::new(vec![[1.0, 2.0, 3.0]]);
Expand All @@ -494,7 +493,7 @@ mod tests {
] {
let row =
DataRow::from_archetype(RowId::new(), TimePoint::default(), path, &points).unwrap();
recording.add_data_row(row).ok();
test_ctx.recording_store.add_data_row(row).ok();
}

let recommended = RecommendedSpaceView::new(
Expand Down Expand Up @@ -526,35 +525,23 @@ mod tests {
.collect(),
);

let blueprint_query = LatestAtQuery::latest(blueprint_timeline());
let contents = &space_view.contents;

let resolver = contents.build_resolver(
&space_view_class_registry,
&test_ctx.space_view_class_registry,
&space_view,
&visualizable_entities,
&indicated_entities_per_visualizer,
);

// No overrides set. Everybody has default values.
{
let ctx = StoreContext {
app_id: re_log_types::ApplicationId::unknown(),
blueprint: &blueprint,
default_blueprint: None,
recording: &recording,
bundle: &Default::default(),
hub: &re_viewer_context::StoreHub::test_hub(),
};

let mut query_result = contents.execute_query(&ctx, &visualizable_entities);
resolver.update_overrides(
&blueprint,
&blueprint_query,
&timeline,
&space_view_class_registry,
let query_result = update_overrides(
&test_ctx,
contents,
&visualizable_entities,
&resolver,
&legacy_auto_properties,
&mut query_result,
);

let parent = query_result
Expand Down Expand Up @@ -584,29 +571,18 @@ mod tests {
save_override(
overrides,
parent.individual_override_path().unwrap(),
&mut blueprint,
&mut test_ctx.blueprint_store,
);
}

// Parent is not interactive, but children are
{
let ctx = StoreContext {
app_id: re_log_types::ApplicationId::unknown(),
blueprint: &blueprint,
default_blueprint: None,
recording: &recording,
bundle: &Default::default(),
hub: &re_viewer_context::StoreHub::test_hub(),
};

let mut query_result = contents.execute_query(&ctx, &visualizable_entities);
resolver.update_overrides(
&blueprint,
&blueprint_query,
&timeline,
&space_view_class_registry,
let query_result = update_overrides(
&test_ctx,
contents,
&visualizable_entities,
&resolver,
&legacy_auto_properties,
&mut query_result,
);

let parent_group = query_result
Expand Down Expand Up @@ -642,31 +618,19 @@ mod tests {
save_override(
overrides,
parent_group.recursive_override_path().unwrap(),
&mut blueprint,
&mut test_ctx.blueprint_store,
);
}

// Nobody is interactive
{
let ctx = StoreContext {
app_id: re_log_types::ApplicationId::unknown(),
blueprint: &blueprint,
default_blueprint: None,
recording: &recording,
bundle: &Default::default(),
hub: &re_viewer_context::StoreHub::test_hub(),
};

let mut query_result = contents.execute_query(&ctx, &visualizable_entities);
resolver.update_overrides(
&blueprint,
&blueprint_query,
&timeline,
&space_view_class_registry,
let query_result = update_overrides(
&test_ctx,
contents,
&visualizable_entities,
&resolver,
&legacy_auto_properties,
&mut query_result,
);

let parent = query_result
.tree
.lookup_result_by_path(&EntityPath::from("parent"))
Expand All @@ -688,12 +652,9 @@ mod tests {

#[test]
fn test_component_overrides() {
let space_view_class_registry = SpaceViewClassRegistry::default();
let timeline = Timeline::new("time", re_log_types::TimeType::Time);
let legacy_auto_properties = EntityPropertyMap::default();
let mut recording = EntityDb::new(StoreId::random(re_log_types::StoreKind::Recording));
let mut visualizable_entities_per_visualizer =
PerVisualizer::<VisualizableEntities>::default();
let mut test_ctx = TestContext::default();
let mut visualizable_entities = PerVisualizer::<VisualizableEntities>::default();

// Set up a store DB with some entities.
{
Expand All @@ -710,11 +671,11 @@ mod tests {
[&[MyPoint::new(1.0, 2.0)] as _],
)
.unwrap();
recording.add_data_row(row).unwrap();
test_ctx.recording_store.add_data_row(row).unwrap();
}

// All of them are visualizable with some arbitrary visualizer.
visualizable_entities_per_visualizer
visualizable_entities
.0
.entry("Points3D".into())
.or_insert_with(|| VisualizableEntities(entity_paths.into_iter().collect()));
Expand All @@ -734,9 +695,9 @@ mod tests {
// Things needed to resolve properties:
let indicated_entities_per_visualizer = PerVisualizer::<IndicatedEntities>::default(); // Don't care about indicated entities.
let resolver = space_view.contents.build_resolver(
&space_view_class_registry,
&test_ctx.space_view_class_registry,
&space_view,
&visualizable_entities_per_visualizer,
&visualizable_entities,
&indicated_entities_per_visualizer,
);

Expand Down Expand Up @@ -908,7 +869,9 @@ mod tests {
},
) in scenarios.into_iter().enumerate()
{
let mut blueprint = EntityDb::new(StoreId::random(re_log_types::StoreKind::Blueprint));
// Reset blueprint store for each scenario.
test_ctx.blueprint_store = EntityDb::new(StoreId::random(StoreKind::Blueprint));

let mut add_to_blueprint = |path: &EntityPath, batch: &dyn ComponentBatch| {
let row = DataRow::from_component_batches(
RowId::new(),
Expand All @@ -917,7 +880,7 @@ mod tests {
std::iter::once(batch),
)
.unwrap();
blueprint.add_data_row(row).unwrap();
test_ctx.blueprint_store.add_data_row(row).unwrap();
};

// log individual and override components as instructed.
Expand All @@ -929,25 +892,12 @@ mod tests {
}

// Set up a store query and update the overrides.
let ctx = StoreContext {
app_id: re_log_types::ApplicationId::unknown(),
blueprint: &blueprint,
default_blueprint: None,
recording: &recording,
bundle: &Default::default(),
hub: &re_viewer_context::StoreHub::test_hub(),
};
let mut query_result = space_view
.contents
.execute_query(&ctx, &visualizable_entities_per_visualizer);
let blueprint_query = LatestAtQuery::latest(blueprint_timeline());
resolver.update_overrides(
&blueprint,
&blueprint_query,
&timeline,
&space_view_class_registry,
let query_result = update_overrides(
&test_ctx,
&space_view.contents,
&visualizable_entities,
&resolver,
&legacy_auto_properties,
&mut query_result,
);

// Extract component overrides for testing.
Expand Down Expand Up @@ -976,4 +926,36 @@ mod tests {
assert_eq!(visited, expected_overrides, "Scenario {i}");
}
}

fn update_overrides(
test_ctx: &TestContext,
contents: &SpaceViewContents,
visualizable_entities: &PerVisualizer<VisualizableEntities>,
resolver: &DataQueryPropertyResolver<'_>,
legacy_auto_properties: &EntityPropertyMap,
) -> re_viewer_context::DataQueryResult {
let store_ctx = StoreContext {
app_id: re_log_types::ApplicationId::unknown(),
blueprint: &test_ctx.blueprint_store,
default_blueprint: None,
recording: &test_ctx.recording_store,
bundle: &Default::default(),
hub: &re_viewer_context::StoreHub::test_hub(),
};

let mut query_result = contents.execute_query(&store_ctx, visualizable_entities);

test_ctx.run(|ctx, _ui| {
resolver.update_overrides(
ctx.blueprint_db(),
ctx.blueprint_query,
&test_ctx.active_timeline,
ctx.space_view_class_registry,
legacy_auto_properties,
&mut query_result,
);
});

query_result
}
}
Loading