Skip to content

Visual Studio Code, rebuilt in Rust

Rust
10.3 KB
Raw Blame
use std::path::Path;

use exthost::scan::scan_extension;
use gpui::TestAppContext;
use serde_json::json;

use crate::host_sync::HostSync;
use crate::languages::Languages;
use crate::menus::MenuAction;
use crate::theme::load_theme;
use crate::workbench::Workbench;

#[gpui::test]
fn a_view_with_hidden_visibility_stays_out_until_toggled_from_the_views_submenu(cx: &mut TestAppContext) {
    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 npm = scan_extension(&app_root.join("extensions/npm"), true).unwrap();
    let (workbench, cx) = cx.add_window_view(|_, cx| {
        let mut workbench = Workbench::new(theme, app_root.clone(), None, Languages::new(&[]), crate::default_settings(), HostSync::new(std::sync::mpsc::channel().0), cx);
        workbench.set_tree_views(&[npm]);
        workbench
    });
    workbench.update_in(cx, |workbench, window, cx| drop(workbench.execute_command("_setContext", vec![json!("npm:showScriptExplorer"), json!(true)], window, cx)));
    let toggle = |workbench: &mut Workbench| {
        let views = workbench.views_submenu("explorer");
        views.into_iter().find_map(|action| match action {
            MenuAction::Command { id, title, enabled, checked, .. } if id == "npm.toggleVisibility" => Some((title, enabled, checked)),
            _ => None,
        })
    };
    workbench.update(cx, |workbench, _| {
        assert!(!workbench.shown_tree_views().contains(&"npm"));
        assert_eq!(toggle(workbench), Some(("NPM Scripts".to_string(), true, false)));
    });
    workbench.update_in(cx, |workbench, window, cx| drop(workbench.execute_command("npm.toggleVisibility", vec![], window, cx)));
    workbench.update(cx, |workbench, _| {
        assert!(workbench.shown_tree_views().contains(&"npm"));
        assert_eq!(toggle(workbench), Some(("NPM Scripts".to_string(), true, true)));
        let stored = workbench.profile_storage.lock().unwrap().get("workbench.explorer.views.state.hidden").map(str::to_owned);
        assert_eq!(stored.as_deref(), Some(r#"[{"id":"npm","isHidden":false}]"#));
    });
}

/// panecomposite.ts `getSecondaryActions`: the container's "..." with only the Views submenu lists its toggles flat.
#[gpui::test]
fn a_merged_view_without_actions_lists_the_view_toggles_flat(cx: &mut TestAppContext) {
    use crate::workbench::view_panes::{EMPTY_VIEW, OUTLINE_VIEW, TIMELINE_VIEW};
    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 (workbench, cx) = cx.add_window_view(|_, cx| Workbench::new(theme, app_root.clone(), None, Languages::new(&[]), crate::default_settings(), HostSync::new(std::sync::mpsc::channel().0), cx));
    cx.simulate_resize(gpui::size(gpui::px(1024.), gpui::px(768.)));
    workbench.update_in(cx, |workbench, window, cx| {
        workbench.toggle_view_visibility("explorer", OUTLINE_VIEW, cx);
        workbench.toggle_view_visibility("explorer", TIMELINE_VIEW, cx);
        drop(workbench.execute_command("workbench.view.explorer", Vec::new(), window, cx));
    });
    cx.run_until_parked();
    workbench.update_in(cx, |workbench, window, cx| {
        assert_eq!(workbench.panes(crate::workbench::view_panes::PaneContainer::Explorer).view.as_ref().unwrap().ids(), [EMPTY_VIEW]);
        let more = workbench.action_bar_focus(EMPTY_VIEW, "more", cx);
        assert!(workbench.drawn_action_bar_items(EMPTY_VIEW).contains(&more), "the container's \"...\" draws");
        window.focus(&more, cx);
    });
    cx.simulate_keystrokes("enter");
    cx.simulate_event(gpui::KeyUpEvent { keystroke: gpui::Keystroke::parse("enter").unwrap() });
    cx.run_until_parked();
    let menu = workbench.read_with(cx, |workbench, _| workbench.context_menu.as_ref().expect("the \"...\" opens its menu").levels[0].actions.clone());
    let titles: Vec<(String, bool)> = menu.iter().filter_map(|action| match action {
        MenuAction::Command { title, checked, .. } => Some((title.clone(), *checked)),
        _ => None,
    }).collect();
    assert_eq!(titles.len(), menu.len(), "no Views submenu: {menu:?}");
    assert!(titles.contains(&("Outline".to_string(), false)), "{titles:?}");
}

/// panecomposite.ts `getSecondaryActions` with viewMenuActions.ts `ViewContainerMenuActions`: the Run and Debug
/// container's `...` lists its `MenuId.ViewContainerTitle` items outside `navigation`, read with `viewContainer` set.
#[gpui::test]
fn the_debug_container_more_actions_list_its_view_container_title_items(cx: &mut TestAppContext) {
    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 (workbench, cx) = cx.add_window_view(|_, cx| Workbench::new(theme, app_root.clone(), None, Languages::new(&[]), crate::default_settings(), HostSync::new(std::sync::mpsc::channel().0), cx));
    let extension = exthost::scan::ScannedExtension {
        id: "test.container".into(),
        location: app_root.clone(),
        description: json!({ "enabledApiProposals": ["contribViewContainerTitle"], "contributes": {
            "commands": [{ "command": "c.more", "title": "Container Action" }, { "command": "c.explorer", "title": "Explorer Action" }],
            "menus": { "viewContainer/title": [
                { "command": "c.more", "group": "1_port", "when": "viewContainer == workbench.view.debug" },
                { "command": "c.explorer", "group": "1_port", "when": "viewContainer == workbench.view.debug && viewContainer == workbench.view.explorer" }
            ] }
        } }),
    };
    workbench.update(cx, |workbench, _| workbench.menus.extend(crate::menus::extension_menus(&[extension])));
    workbench.update(cx, |workbench, cx| workbench.show_debug_view(cx));
    cx.run_until_parked();
    let more = cx.debug_bounds("debug-more").expect("the title shows its \"...\"");
    cx.simulate_click(more.center(), gpui::Modifiers::none());
    // sidebarPart.ts `getTitleAreaDropDownAnchorAlignment`: below the button, left aligned in a left side bar.
    let origin = workbench.read_with(cx, |workbench, _| workbench.context_menu.as_ref().expect("the \"...\" opens its menu").levels[0].origin);
    assert_eq!(origin, more.bottom_left());
    let menu = workbench.read_with(cx, |workbench, _| workbench.context_menu.as_ref().unwrap().levels[0].actions.clone());
    let commands: Vec<&str> = menu.iter().filter_map(|action| match action {
        MenuAction::Command { id, .. } => Some(id.as_str()),
        _ => None,
    }).collect();
    assert!(commands.contains(&"c.more"), "{menu:?}");
    assert!(!commands.contains(&"c.explorer"), "{menu:?}");
    // debugViewlet.ts `debug.toggleReplIgnoreFocus` in group `3_toggleRepl` of the Views submenu, before the view toggles.
    let views = menu.iter().find_map(|action| match action {
        MenuAction::Submenu { title, actions, .. } if title == "Views" => Some(actions.clone()),
        _ => None,
    });
    let views = views.expect("the Views submenu");
    assert!(matches!(&views[0], MenuAction::Command { id, title, checked: false, .. } if id == "debug.toggleReplIgnoreFocus" && title == "Debug Console"), "{views:?}");
    assert!(matches!(views[1], MenuAction::Separator), "{views:?}");
    assert!(workbench.read_with(cx, |workbench, _| workbench.container_more_menu.is_some_and(|(container, shown)| container == "debug" && shown == workbench.context_menus_shown)), "the `...` shows pressed");
    workbench.update_in(cx, |workbench, window, cx| drop(workbench.execute_command("debug.toggleReplIgnoreFocus", Vec::new(), window, cx)));
    assert!(workbench.read_with(cx, |workbench, _| workbench.panel_view_visible(crate::workbench::debug_console::DEBUG_PANEL_ID)));
}

/// modernUI padding.css and debugViewlet.css: beside the launch picker the title label takes `flex: 1` and the actions
/// `flex: 2`, and only the picker shrinks, so a narrow side bar cuts the configuration name before the title.
#[gpui::test]
fn the_launch_picker_shrinks_before_the_run_and_debug_title(cx: &mut TestAppContext) {
    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-launch-picker-{}", std::process::id()));
    let folder = dir.join("folder");
    std::fs::create_dir_all(folder.join(".vscode")).unwrap();
    std::fs::create_dir_all(dir.join("User")).unwrap();
    let name = "Launch a configuration whose name is wider than the side bar";
    std::fs::write(folder.join(".vscode/launch.json"), json!({ "configurations": [{ "name": name, "type": "node", "request": "launch" }] }).to_string()).unwrap();
    let workspace = config::workspace::Workspace::folder(folder.clone());
    let settings = std::sync::Arc::new(std::sync::Mutex::new(config::service::Settings::load(config::registry::Registry::with_extensions(&[]), dir.join("User"), Some(workspace.clone()))));
    let (workbench, cx) = cx.add_window_view(|_, cx| {
        let mut workbench = Workbench::new(theme, app_root.clone(), Some(workspace), Languages::new(&[]), settings, HostSync::new(std::sync::mpsc::channel().0), cx);
        workbench.debug.debuggers.push(crate::debug::configuration::Debugger { debug_type: "node".into(), contribution: json!({}), extension_id: "a.node".into() });
        workbench.debug.selected = Some((Some(folder.clone()), name.into()));
        workbench
    });
    workbench.update(cx, |workbench, cx| workbench.show_debug_view(cx));
    cx.run_until_parked();
    let label = cx.debug_bounds("side-bar-title-label").expect("the title shows its label");
    let actions = cx.debug_bounds("debug-title-actions").expect("the title shows the launch picker");
    let picker = cx.debug_bounds("start-debug-action-item").unwrap();
    // The label's 8px padding-left sits outside its share.
    assert_eq!((f32::from(label.size.width) - 8.) * 2., f32::from(actions.size.width), "label {label:?}, actions {actions:?}");
    assert!(picker.right() <= actions.right(), "picker {picker:?} overflows actions {actions:?}");
    std::fs::remove_dir_all(dir).unwrap();
}