Visual Studio Code, rebuilt in Rust
Rust
test(gpui): compare settled retained frames with a full render
rokarturad7af52d0ad6
diff --git a/native/crates/app/src/main.rs b/native/crates/app/src/main.rsindex 43415eeafd2..0cff52cd215 100644--- a/native/crates/app/src/main.rs+++ b/native/crates/app/src/main.rs@@ -21,6 +21,8 @@ mod codicons;// wu's gpui_platform `current_headless_renderer` is Metal only, None elsewhere.#[cfg(all(test, target_os = "macos"))]mod corner_lab;+#[cfg(test)]+mod retained_check;mod command_palette;mod commands;mod custom_tree;diff --git a/native/crates/app/src/retained_check.rs b/native/crates/app/src/retained_check.rsnew file mode 100644index 00000000000..39ddfba64b3--- /dev/null+++ b/native/crates/app/src/retained_check.rs@@ -0,0 +1,45 @@+//! Retained Mode against full renders. `GPUI_RETENTION_CHECK=1 cargo test -p app` draws every frame each test draws+//! a second time from scratch once it is settled and fails the test where the two differ (vendor gpui+//! `fast/retained.rs` `check_settled_frames`); this test checks that the comparison sees a view whose state changed without a notify.++use gpui::{AppContext, Context, Entity, IntoElement, ParentElement, Render, Styled, TestAppContext, Window, div, px, rgb};+use std::{cell::Cell, rc::Rc};++struct Swatch(Rc<Cell<u32>>);++impl Render for Swatch {+ fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {+ div().size(px(10.)).bg(rgb(self.0.get()))+ }+}++struct Pair {+ stale: Entity<Swatch>,+ other: Entity<Swatch>,+}++impl Render for Pair {+ fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {+ div().flex().child(self.stale.clone()).child(self.other.clone())+ }+}++#[gpui::test]+fn a_view_changed_without_a_notify_differs_from_a_full_render(cx: &mut TestAppContext) {+ // The check under GPUI_RETENTION_CHECK=1 fails this test's deliberately stale frame first.+ if std::env::var("GPUI_RETENTION_CHECK").is_ok_and(|value| value == "1") {+ return;+ }+ let color = Rc::new(Cell::new(0xFF0000));+ let stale = cx.new(|_| Swatch(color.clone()));+ let other = cx.new(|_| Swatch(Rc::new(Cell::new(0x00FF00))));+ let (_, cx) = cx.add_window_view(|_, _| Pair { stale: stale.clone(), other: other.clone() });+ color.set(0x0000FF);+ other.update(cx, |_, cx| cx.notify());+ cx.run_until_parked();+ let difference = cx.update(|window, cx| window.retained_frame_difference(cx));+ assert!(difference.is_some(), "the swatch read its color outside GPUI and kept its last frame");+ stale.update(cx, |_, cx| cx.notify());+ cx.run_until_parked();+ assert_eq!(cx.update(|window, cx| window.retained_frame_difference(cx)), None);+}diff --git a/native/vendor/gpui-fast/crates/gpui/src/app.rs b/native/vendor/gpui-fast/crates/gpui/src/app.rsindex a7562841a7a..d3f0408db7b 100644--- a/native/vendor/gpui-fast/crates/gpui/src/app.rs+++ b/native/vendor/gpui-fast/crates/gpui/src/app.rs@@ -2337,17 +2337,23 @@ impl App {} else {#[cfg(any(test, feature = "test-support"))]if matches!(self.mode, GpuiMode::Test { .. }) {- for window in self+ let dirty_windows = self.windows.values().filter_map(|window| {let window = window.as_deref()?;window.invalidator.is_dirty().then_some(window.handle)})- .collect::<Vec<_>>()- {- self.update_window(window, |_, window, cx| window.draw(cx).clear(cx))- .unwrap();+ .collect::<Vec<_>>();+ if dirty_windows.is_empty() {+ crate::fast::retained::check_settled_frames(self);+ }+ for window in dirty_windows {+ self.update_window(window, |_, window, cx| {+ window.draw(cx).clear(cx);+ crate::fast::retained::note_test_frame(window);+ })+ .unwrap();}}diff --git a/native/vendor/gpui-fast/crates/gpui/src/fast/retained.rs b/native/vendor/gpui-fast/crates/gpui/src/fast/retained.rsindex 8a190cacb14..ad783c54ce8 100644--- a/native/vendor/gpui-fast/crates/gpui/src/fast/retained.rs+++ b/native/vendor/gpui-fast/crates/gpui/src/fast/retained.rs@@ -268,6 +268,9 @@ pub(crate) struct RetainedState {/// nothing that was, is drawn again from what it drew then. See/// [`Window::set_view_retention`].pub(crate) view_retention: bool,+ /// A test drew a frame that [`check_settled_frames`] has not compared yet.+ #[cfg(any(test, feature = "test-support"))]+ pub(crate) retention_check_pending: bool,/// Records reads of the pointer and modifier keys while views are drawn.pub(crate) ambient_reads: crate::fast::dependencies::AmbientReads,/// Room to sort out the layout keys a spliced view keeps, kept from one@@ -304,6 +307,8 @@ impl RetainedState {prebuilt: FxHashMap::default(),notified_entities: FxHashSet::default(),view_retention: std::env::var("GPUI_VIEW_RETENTION").map_or(true, |value| value != "0"),+ #[cfg(any(test, feature = "test-support"))]+ retention_check_pending: false,splice_keys: FxHashSet::default(),rendering_since: FxHashMap::default(),render_writes: FxHashMap::default(),@@ -2088,3 +2093,77 @@ impl HitboxId {self.hit_test(window)}}++/// What a frame drew and where it can be hit: its primitives in a canonical+/// order (see [`Window::painted_primitives`]) and its hitboxes in order.+#[cfg(any(test, feature = "test-support"))]+fn frame_contents(window: &Window) -> Vec<String> {+ let mut lines = window.painted_primitives();+ lines.extend(window.rendered_frame.hitboxes.iter().map(|hitbox| {+ format!("hitbox {:?} {:?} {:?}", hitbox.bounds, hitbox.content_mask, hitbox.behavior)+ }));+ lines+}++#[cfg(any(test, feature = "test-support"))]+static RETENTION_CHECK: std::sync::LazyLock<bool> =+ std::sync::LazyLock::new(|| std::env::var("GPUI_RETENTION_CHECK").is_ok_and(|value| value == "1"));++/// VSRustium: with `GPUI_RETENTION_CHECK=1`, a frame a test draws with+/// Retained Mode on waits for [`check_settled_frames`].+#[cfg(any(test, feature = "test-support"))]+pub(crate) fn note_test_frame(window: &mut Window) {+ window.retained_state.retention_check_pending = *RETENTION_CHECK && window.retained_state.view_retention;+}++/// VSRustium: compares each window's last test frame with a full render by+/// [`Window::retained_frame_difference`] once it is settled: no effect, dirty+/// window or next-frame callback is left, so a value measured as it painted+/// has already drawn the frame after it.+#[cfg(any(test, feature = "test-support"))]+pub(crate) fn check_settled_frames(cx: &mut App) {+ let pending = cx+ .windows+ .values()+ .filter_map(|window| window.as_deref())+ .filter(|window| window.retained_state.retention_check_pending && window.next_frame_callbacks.borrow().is_empty())+ .map(|window| window.handle)+ .collect::<Vec<_>>();+ for handle in pending {+ let difference = handle+ .update(cx, |_, window, cx| {+ window.retained_state.retention_check_pending = false;+ window.retained_frame_difference(cx)+ })+ .unwrap();+ if let Some(difference) = difference {+ panic!("retention check: the retained frame differs from a full render\n{difference}");+ }+ }+}++impl Window {+ /// VSRustium: draws the last frame again from scratch, as the retained+ /// tests in `fast/tests/retained.rs` do, and returns what it painted or+ /// hit differently, `None` when nothing. A difference is a view whose+ /// state changed without telling GPUI.+ #[cfg(any(test, feature = "test-support"))]+ pub fn retained_frame_difference(&mut self, cx: &mut App) -> Option<String> {+ let retained = frame_contents(self);+ let was_dirty = self.invalidator.is_dirty();+ let retention = mem::replace(&mut self.retained_state.view_retention, false);+ self.refreshing = true;+ self.draw(cx).clear(cx);+ self.retained_state.view_retention = retention;+ if was_dirty {+ self.invalidator.set_dirty(true);+ }+ let full = frame_contents(self);+ if retained == full {+ return None;+ }+ let only_retained: Vec<&String> = retained.iter().filter(|line| !full.contains(line)).collect();+ let only_full: Vec<&String> = full.iter().filter(|line| !retained.contains(line)).collect();+ Some(format!("only retained: {only_retained:#?}\nonly full: {only_full:#?}"))+ }+}