icedwright 0.0.1

The icedwright test harness: run an iced 0.14 program headlessly on virtual time
Documentation
//! [`Environment`]: the combination a test runs in, and how it reaches the harness.

use icedwright_types::units::ScaleFactor;

use crate::config::HarnessConfig;
use crate::harness::TestProgram;
use crate::snapshot::Variant;

use super::matrix::{Locale, Size, Theme};

/// Which axes a test's matrix varies, and so which appear in snapshot names.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct Varies {
    /// The window size.
    pub size: bool,
    /// The system theme.
    pub theme: bool,
    /// The scale factor.
    pub scale: bool,
}

/// The current combination of the run matrix, and the test's path.
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub struct Environment {
    /// The main window's size, or `None` to keep the program's own.
    pub size: Option<Size>,
    /// The system theme.
    pub theme: Theme,
    /// The system scale factor.
    pub scale_factor: ScaleFactor,
    /// The locale the app is given.
    pub locale: Locale,
    /// The axes the matrix varies.
    pub varies: Varies,
    test: String,
    package: &'static str,
}

impl Environment {
    /// The defaults for a test at `test`: the program's own size, the light theme, scale one
    /// and British English, none of them varied.
    #[must_use]
    pub fn new(test: impl Into<String>) -> Self {
        Self {
            size: None,
            theme: Theme::default(),
            scale_factor: ScaleFactor::ONE,
            locale: Locale::default(),
            varies: Varies::default(),
            test: test.into(),
            package: "unknown",
        }
    }

    pub(crate) fn with_package(mut self, package: &'static str) -> Self {
        self.package = package;
        self
    }

    /// The package the test is in, for paths that must not collide across a workspace.
    #[must_use]
    pub fn package(&self) -> &'static str {
        self.package
    }

    /// The same environment with another size, which it then varies.
    #[must_use]
    pub fn with_size(mut self, size: Size) -> Self {
        self.size = Some(size);
        self.varies.size = true;
        self
    }

    /// The same environment with another theme, which it then varies.
    #[must_use]
    pub fn with_theme(mut self, theme: Theme) -> Self {
        self.theme = theme;
        self.varies.theme = true;
        self
    }

    /// The same environment with another scale factor, which it then varies.
    #[must_use]
    pub fn with_scale_factor(mut self, factor: ScaleFactor) -> Self {
        self.scale_factor = factor;
        self.varies.scale = true;
        self
    }

    /// The same environment with another locale.
    #[must_use]
    pub fn with_locale(mut self, locale: Locale) -> Self {
        self.locale = locale;
        self
    }

    /// The test's path as libtest prints it, such as `ui::saves::phone_dark`.
    #[must_use]
    pub fn test(&self) -> &str {
        &self.test
    }

    /// The snapshot variant: each axis the matrix varies, so a visual snapshot in this
    /// combination is named like `board@dark-phone-2x`.
    pub fn variant(&self) -> Variant {
        let mut variant = Variant::new();
        if self.varies.theme {
            variant = variant.theme(self.theme);
        }
        if let (true, Some(size)) = (self.varies.size, self.size) {
            variant = variant.size(size);
        }
        if self.varies.scale {
            variant = variant.scale(self.scale_factor);
        }
        variant
    }

    /// Writes the size, the system scale factor, the system theme and the snapshot variant
    /// into `config`, and nothing else. The system theme goes through the harness's fake,
    /// never `ICED_THEME`, which iced reads once per process.
    pub fn apply<P: TestProgram>(&self, config: HarnessConfig<P>) -> HarnessConfig<P> {
        let config = config
            .scale_factor(self.scale_factor)
            .system_theme(self.theme.into());
        let config = match self.size {
            Some(size) => config.size(size),
            None => config,
        };
        config.variant(self.variant())
    }
}