native-theme-qt 0.1.0

Resolve the active Qt 6 theme into a serializable toolkit-neutral snapshot
Documentation
//! Subprocess integration tests for Qt theme resolution.

use std::process::{Command, Output};

use native_theme_qt::{Rgba, ThemeSnapshot};

fn command(style: &str) -> Command {
    let mut process = Command::new(env!("CARGO_BIN_EXE_native-theme-qt"));
    process
        .env("QT_QPA_PLATFORM", "offscreen")
        .env_remove("QT_QPA_PLATFORMTHEME")
        .env("QT_STYLE_OVERRIDE", style);
    process
}

fn parse_success(output: Output) -> ThemeSnapshot {
    assert!(
        output.status.success(),
        "probe failed: {}",
        String::from_utf8_lossy(&output.stderr)
    );
    serde_json::from_slice(&output.stdout).expect("probe emitted invalid JSON")
}

fn run_style(style: &str) -> ThemeSnapshot {
    parse_success(
        command(style)
            .output()
            .expect("failed to run Qt theme probe"),
    )
}

fn approximately_equal(actual: Rgba, expected: Rgba) -> bool {
    const TOLERANCE: f32 = 1.0 / 255.0;
    (actual.red - expected.red).abs() <= TOLERANCE
        && (actual.green - expected.green).abs() <= TOLERANCE
        && (actual.blue - expected.blue).abs() <= TOLERANCE
        && (actual.alpha - expected.alpha).abs() <= TOLERANCE
}

#[test]
fn fusion_resolves_without_a_display_server() {
    let snapshot = run_style("Fusion");

    assert_eq!(snapshot.backend, "qt6");
    assert_eq!(snapshot.identity.style_name, "fusion");
    assert_eq!(snapshot.identity.platform_backend, "offscreen");
    assert_eq!(snapshot.palette.len(), 3);
    assert!(snapshot.controls.len() >= 10);
    assert!(snapshot.controls["button"].size_hint.width > 0);
    assert!(snapshot.controls["line_edit"].size_hint.height > 0);
    assert!(snapshot.metrics.len() >= 20);
    assert!(snapshot.semantic_palette.window_background.is_some());
    assert!(snapshot.semantic_palette.window_text.is_some());

    let bridge = snapshot.resolve_native_theme().unwrap();
    let expected = snapshot.semantic_palette.window_background.unwrap();
    assert!(!bridge.mode.is_dark());
    assert_eq!(
        bridge.resolved.defaults.background_color.to_f32_array(),
        [expected.red, expected.green, expected.blue, expected.alpha]
    );
    assert_eq!(
        bridge.resolved.button.min_width,
        snapshot.controls["button"].minimum_size_hint.width as f32
    );

    #[cfg(feature = "gpui")]
    assert_eq!(
        snapshot.to_gpui_theme().unwrap().is_dark(),
        bridge.mode.is_dark()
    );
}

#[test]
fn windows_and_fusion_styles_are_distinct() {
    let fusion = run_style("Fusion");
    let windows = run_style("Windows");

    assert_eq!(windows.identity.style_name, "windows");
    assert_ne!(fusion.identity.style_name, windows.identity.style_name);
    assert_ne!(fusion.metrics, windows.metrics);
    assert_ne!(fusion.controls, windows.controls);
}

#[test]
fn repeated_probes_are_deterministic() {
    assert_eq!(run_style("Fusion"), run_style("Fusion"));
}

#[test]
fn unavailable_platform_plugin_is_reported_as_process_failure() {
    let output = Command::new(env!("CARGO_BIN_EXE_native-theme-qt"))
        .env("QT_QPA_PLATFORM", "native-theme-no-such-platform")
        .env_remove("QT_QPA_PLATFORMTHEME")
        .output()
        .expect("failed to run Qt theme probe");

    // Qt terminates the process while constructing QApplication if the QPA
    // plugin cannot be loaded. Callers must treat any non-zero status as a
    // probe failure rather than assuming a structured JSON error is possible.
    assert!(!output.status.success());
}

#[test]
fn qt6ct_custom_palette_is_resolved() {
    if std::env::var_os("NATIVE_THEME_RUN_QT6CT_TESTS").is_none() {
        return;
    }

    let config_home = tempfile::tempdir().unwrap();
    let qt6ct = config_home.path().join("qt6ct");
    std::fs::create_dir(&qt6ct).unwrap();
    let scheme = qt6ct.join("fixture.conf");
    let colors = [
        "#fffedcba",
        "#ff222222",
        "#ff333333",
        "#ff444444",
        "#ff555555",
        "#ff666666",
        "#ffdddddd",
        "#ffffffff",
        "#ffeeeeee",
        "#ff111111",
        "#ff123456",
        "#ff000000",
        "#ff336699",
        "#ffffffff",
        "#ff0000ff",
        "#ffff00ff",
        "#ff181818",
        "#fffedcba",
        "#ffffffdc",
        "#ffaaaaaa",
        "#80ffffff",
    ]
    .join(", ");
    std::fs::write(
        &scheme,
        format!(
            "[ColorScheme]\nactive_colors={colors}\ndisabled_colors={colors}\ninactive_colors={colors}\n"
        ),
    )
    .unwrap();
    std::fs::write(
        qt6ct.join("qt6ct.conf"),
        format!(
            "[Appearance]\nstyle=Fusion\ncustom_palette=true\ncolor_scheme_path={}\n",
            scheme.display()
        ),
    )
    .unwrap();

    let output = Command::new(env!("CARGO_BIN_EXE_native-theme-qt"))
        .env("QT_QPA_PLATFORM", "offscreen")
        .env("QT_QPA_PLATFORMTHEME", "qt6ct")
        .env_remove("QT_STYLE_OVERRIDE")
        .env("XDG_CONFIG_HOME", config_home.path())
        .output()
        .expect("failed to run Qt theme probe");
    let snapshot = parse_success(output);

    assert_eq!(snapshot.identity.platform_theme.as_deref(), Some("qt6ct"));
    assert!(approximately_equal(
        snapshot.semantic_palette.window_background.unwrap(),
        Rgba {
            red: 0x12 as f32 / 255.0,
            green: 0x34 as f32 / 255.0,
            blue: 0x56 as f32 / 255.0,
            alpha: 1.0,
        }
    ));
    assert!(approximately_equal(
        snapshot.semantic_palette.window_text.unwrap(),
        Rgba {
            red: 0xfe as f32 / 255.0,
            green: 0xdc as f32 / 255.0,
            blue: 0xba as f32 / 255.0,
            alpha: 1.0,
        }
    ));
    let bridge = snapshot.resolve_native_theme().unwrap();
    assert!(bridge.mode.is_dark());
    assert_eq!(
        bridge.resolved.defaults.background_color,
        native_theme_gpui_compatible_color(0x12, 0x34, 0x56)
    );

    #[cfg(feature = "gpui")]
    assert!(snapshot.to_gpui_theme().unwrap().is_dark());
}

fn native_theme_gpui_compatible_color(red: u8, green: u8, blue: u8) -> native_theme::color::Rgba {
    native_theme::color::Rgba::rgb(red, green, blue)
}