Skip to content

Visual Studio Code, rebuilt in Rust

Rust
11.7 KB
Raw Blame
use std::cell::Cell;
use std::path::{Path, PathBuf};
use std::rc::Rc;
use std::sync::{Arc, Mutex};

use config::registry::Registry;
use config::service::Settings;
use config::workspace::Workspace;
use exthost::scan::scan_extension;
use gpui::{Modifiers, ScrollDelta, ScrollWheelEvent, TestAppContext, VisualTestContext, point};

use crate::host_sync::HostSync;
use crate::languages::Languages;
use crate::theme::load_theme;
use crate::workbench::Workbench;
use crate::workbench::layout::Part;
use crate::workbench::list_scroll::ListId;
use crate::workbench::part_view::CachedPart;

/// A folder of 200 files, expanded in the Explorer.
fn explorer<'a>(name: &str, cx: &'a mut TestAppContext) -> (PathBuf, gpui::Entity<Workbench>, &'a mut VisualTestContext) {
    let app_root = Path::new(env!("VSCODE_SRC")).to_path_buf();
    let themes = scan_extension(&app_root.join("extensions/theme-defaults"), true).unwrap();
    let theme = load_theme(&[themes], "Dark Modern").unwrap();
    let dir = std::env::temp_dir().join(format!("code-gpui-part-view-{name}-{}", std::process::id()));
    let _ = std::fs::remove_dir_all(&dir);
    std::fs::create_dir_all(dir.join("User")).unwrap();
    for index in 0..200 {
        std::fs::write(dir.join(format!("f{index:03}.txt")), "").unwrap();
    }
    let folder = dir.clone();
    let settings = Arc::new(Mutex::new(Settings::load(Registry::with_extensions(&[]), dir.join("User"), None)));
    let (workbench, cx) = cx.add_window_view(|_, cx| {
        let mut workbench = Workbench::new(theme, app_root.clone(), Some(Workspace::folder(folder.clone())), Languages::new(&[]), settings.clone(), HostSync::new(std::sync::mpsc::channel().0), cx);
        workbench.skip_dialogs = true;
        workbench
    });
    cx.run_until_parked();
    (dir, workbench, cx)
}

fn draw(cx: &mut VisualTestContext) {
    cx.update(|window, cx| drop(window.draw(cx)));
}

fn part_renders(workbench: &gpui::Entity<Workbench>, part: Part, cx: &mut VisualTestContext) -> usize {
    view_renders(workbench, CachedPart::Grid(part), cx)
}

fn view_renders(workbench: &gpui::Entity<Workbench>, part: CachedPart, cx: &mut VisualTestContext) -> usize {
    workbench.read_with(cx, |workbench, cx| workbench.part_view_entity(part).expect("the part draws").read(cx).renders)
}

/// A frame with nothing changed reuses the side bar; a change of the workbench draws it again; a scroll of the
/// Explorer draws the side bar again without notifying the workbench, so the other parts can stay cached.
#[gpui::test]
fn the_side_bar_draws_again_on_a_workbench_change_and_its_own_scroll_only(cx: &mut TestAppContext) {
    let (dir, workbench, cx) = explorer("side-bar", cx);
    let notified = Rc::new(Cell::new(0));
    let _observer = {
        let notified = notified.clone();
        cx.update(|_, cx| cx.observe(&workbench, move |_, _| notified.set(notified.get() + 1)))
    };
    draw(cx);
    let renders = part_renders(&workbench, Part::SideBar, cx);
    draw(cx);
    assert_eq!(part_renders(&workbench, Part::SideBar, cx), renders, "an unchanged frame reuses the side bar");

    workbench.update(cx, |_, cx| cx.notify());
    cx.run_until_parked();
    draw(cx);
    assert!(part_renders(&workbench, Part::SideBar, cx) > renders, "a workbench change draws the side bar");
    // The pointer enters the pane first: the hover change notifies the workbench, as upstream's does a CSS repaint.
    let bounds = workbench.read_with(cx, |workbench, _| workbench.list_scroller(ListId::Explorer).bounds());
    cx.simulate_mouse_move(bounds.center(), None, Modifiers::none());
    cx.run_until_parked();
    draw(cx);
    cx.run_until_parked();

    let (renders, notifications) = (part_renders(&workbench, Part::SideBar, cx), notified.get());
    cx.simulate_event(ScrollWheelEvent { position: bounds.center(), delta: ScrollDelta::Lines(point(0., -1.)), modifiers: Modifiers::none(), ..Default::default() });
    cx.run_until_parked();
    draw(cx);
    assert_eq!(workbench.read_with(cx, |workbench, _| workbench.list_scroller(ListId::Explorer).position().y), 50.);
    assert!(part_renders(&workbench, Part::SideBar, cx) > renders, "the scroll draws the side bar");
    assert_eq!(notified.get(), notifications, "the scroll leaves the workbench unnotified");
    std::fs::remove_dir_all(dir).unwrap();
}

/// The panel, the secondary side bar, the status bar, the activity bar and the title bar are cached: a scroll of the
/// Explorer leaves them as drawn, a workbench change draws them again.
#[gpui::test]
fn the_other_parts_stay_cached_through_a_side_bar_scroll(cx: &mut TestAppContext) {
    let (dir, workbench, cx) = explorer("panel", cx);
    workbench.update(cx, |workbench, cx| {
        workbench.set_part_hidden(Part::Panel, false, cx);
        workbench.set_part_hidden(Part::AuxiliaryBar, false, cx);
    });
    cx.run_until_parked();
    let bounds = workbench.read_with(cx, |workbench, _| workbench.list_scroller(ListId::Explorer).bounds());
    cx.simulate_mouse_move(bounds.center(), None, Modifiers::none());
    cx.run_until_parked();
    draw(cx);
    cx.run_until_parked();

    let (renders, status_bar, activity_bar) = (part_renders(&workbench, Part::Panel, cx), view_renders(&workbench, CachedPart::StatusBar, cx), part_renders(&workbench, Part::ActivityBar, cx));
    let (title_bar, auxiliary_bar) = (view_renders(&workbench, CachedPart::TitleBar, cx), part_renders(&workbench, Part::AuxiliaryBar, cx));
    cx.simulate_event(ScrollWheelEvent { position: bounds.center(), delta: ScrollDelta::Lines(point(0., -1.)), modifiers: Modifiers::none(), ..Default::default() });
    cx.run_until_parked();
    draw(cx);
    assert_eq!(workbench.read_with(cx, |workbench, _| workbench.list_scroller(ListId::Explorer).position().y), 50.);
    assert_eq!(part_renders(&workbench, Part::Panel, cx), renders, "the Explorer's scroll reuses the panel");
    assert_eq!(view_renders(&workbench, CachedPart::StatusBar, cx), status_bar, "the Explorer's scroll reuses the status bar");
    assert_eq!(part_renders(&workbench, Part::ActivityBar, cx), activity_bar, "the Explorer's scroll reuses the activity bar");
    assert_eq!(view_renders(&workbench, CachedPart::TitleBar, cx), title_bar, "the Explorer's scroll reuses the title bar");
    assert_eq!(part_renders(&workbench, Part::AuxiliaryBar, cx), auxiliary_bar, "the Explorer's scroll reuses the secondary side bar");

    workbench.update(cx, |_, cx| cx.notify());
    cx.run_until_parked();
    draw(cx);
    assert!(part_renders(&workbench, Part::Panel, cx) > renders, "a workbench change draws the panel");
    assert!(view_renders(&workbench, CachedPart::StatusBar, cx) > status_bar, "a workbench change draws the status bar");
    assert!(part_renders(&workbench, Part::ActivityBar, cx) > activity_bar, "a workbench change draws the activity bar");
    assert!(view_renders(&workbench, CachedPart::TitleBar, cx) > title_bar, "a workbench change draws the title bar");
    assert!(part_renders(&workbench, Part::AuxiliaryBar, cx) > auxiliary_bar, "a workbench change draws the secondary side bar");
    std::fs::remove_dir_all(dir).unwrap();
}

/// A scroll of the main part's editor draws the editor part again and leaves the side bar and the workbench alone.
#[gpui::test]
fn an_editor_scroll_draws_the_editor_part_alone(cx: &mut TestAppContext) {
    let (dir, workbench, cx) = explorer("editor", cx);
    let file = dir.join("long.txt");
    std::fs::write(&file, "line\n".repeat(500)).unwrap();
    workbench.update_in(cx, |workbench, window, cx| workbench.open_file(file, true, window, cx));
    cx.run_until_parked();
    let notified = Rc::new(Cell::new(0));
    let _observer = {
        let notified = notified.clone();
        cx.update(|_, cx| cx.observe(&workbench, move |_, _| notified.set(notified.get() + 1)))
    };
    let viewport = cx.update(|window, _| window.viewport_size());
    let position = point(viewport.width * 0.7, viewport.height * 0.5);
    cx.simulate_mouse_move(position, None, Modifiers::none());
    cx.run_until_parked();
    draw(cx);
    cx.run_until_parked();

    let (side_bar, editor, notifications) = (part_renders(&workbench, Part::SideBar, cx), part_renders(&workbench, Part::Editor, cx), notified.get());
    cx.simulate_event(ScrollWheelEvent { position, delta: ScrollDelta::Lines(point(0., -1.)), modifiers: Modifiers::none(), ..Default::default() });
    cx.run_until_parked();
    draw(cx);
    assert!(workbench.read_with(cx, |workbench, _| workbench.editors.active().unwrap().scroll.top) > 0.);
    assert!(part_renders(&workbench, Part::Editor, cx) > editor, "the scroll draws the editor part");
    assert_eq!(part_renders(&workbench, Part::SideBar, cx), side_bar, "the side bar stays cached");
    assert_eq!(notified.get(), notifications, "the scroll leaves the workbench unnotified");
    std::fs::remove_dir_all(dir).unwrap();
}

/// viewCursors.ts's 500ms blink toggles the focused editor's cursors: the editor part draws again, the others stay.
#[gpui::test]
fn a_cursor_blink_draws_the_editor_part_alone(cx: &mut TestAppContext) {
    // GPUI_RETENTION_CHECK=1 draws every view again after the blink's frame, the side bar too.
    if std::env::var("GPUI_RETENTION_CHECK").is_ok_and(|value| value == "1") {
        return;
    }
    let (dir, workbench, cx) = explorer("blink", cx);
    let file = dir.join("a.txt");
    std::fs::write(&file, "text\n").unwrap();
    workbench.update_in(cx, |workbench, window, cx| workbench.open_file(file, true, window, cx));
    // The word highlight and the other delayed editor work settle in the first blinks.
    for _ in 0..4 {
        cx.executor().advance_clock(std::time::Duration::from_millis(500));
        cx.run_until_parked();
        draw(cx);
    }
    let notified = Rc::new(Cell::new(0));
    let _observer = {
        let notified = notified.clone();
        cx.update(|_, cx| cx.observe(&workbench, move |_, _| notified.set(notified.get() + 1)))
    };
    let hidden = |cx: &mut VisualTestContext| workbench.read_with(cx, |workbench, _| workbench.cursor_blink.borrow().hidden);
    let (side_bar, editor, was_hidden) = (part_renders(&workbench, Part::SideBar, cx), part_renders(&workbench, Part::Editor, cx), hidden(cx));

    cx.executor().advance_clock(std::time::Duration::from_millis(500));
    cx.run_until_parked();
    draw(cx);
    assert_ne!(hidden(cx), was_hidden, "the cursors toggled");
    assert!(part_renders(&workbench, Part::Editor, cx) > editor, "the blink draws the editor part");
    assert_eq!(notified.get(), 0, "the blink leaves the workbench unnotified");
    assert_eq!(part_renders(&workbench, Part::SideBar, cx), side_bar, "the side bar stays cached");
    std::fs::remove_dir_all(dir).unwrap();
}

/// activityaction.css `.action-item:focus .action-label`: a focused unchecked activity item draws in
/// activityBar.foreground; the focus change draws the cached activity bar again.
#[gpui::test]
fn a_focused_unchecked_activity_item_draws_in_the_activity_bar_foreground(cx: &mut TestAppContext) {
    let (dir, workbench, cx) = explorer("activity-focus", cx);
    draw(cx);
    let renders = part_renders(&workbench, Part::ActivityBar, cx);
    let search = "workbench.view.search";
    let (inactive, foreground) = workbench.update_in(cx, |workbench, window, cx| {
        let inactive = workbench.activity_item_foreground("activity", search, window, cx);
        window.focus(&workbench.action_bar_focus("activity", search, cx), cx);
        (inactive, workbench.activity_item_foreground("activity", search, window, cx))
    });
    workbench.read_with(cx, |workbench, _| {
        assert_eq!(inactive, workbench.color("activityBar.inactiveForeground"));
        assert_eq!(foreground, workbench.color("activityBar.foreground"));
    });
    draw(cx);
    assert!(part_renders(&workbench, Part::ActivityBar, cx) > renders, "the focus change draws the activity bar");
    std::fs::remove_dir_all(dir).unwrap();
}