use std::process::Stdio;
use super::{Device, Result, Validate};
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum Appearance {
Light,
Dark,
Custom(String),
}
#[derive(Clone, Debug)]
pub struct UI {
device: Device,
}
impl Device {
pub fn ui(&self) -> UI {
UI {
device: self.clone(),
}
}
}
impl UI {
pub fn appearance(&self) -> Result<Appearance> {
let output = self
.device
.simctl()
.command("ui")
.arg(&self.device.udid)
.arg("appearance")
.stdout(Stdio::piped())
.output()?;
let output = output.validate_with_output()?;
let appearance = String::from_utf8(output.stdout)?.trim().to_owned();
Ok(match appearance.as_str() {
"light" => Appearance::Light,
"dark" => Appearance::Dark,
_ => Appearance::Custom(appearance),
})
}
pub fn set_appearance(&self, appearance: Appearance) -> Result<()> {
let appearance = match &appearance {
Appearance::Light => "light",
Appearance::Dark => "dark",
Appearance::Custom(appearance) => appearance,
};
self.device
.simctl()
.command("ui")
.arg(&self.device.udid)
.arg("appearance")
.arg(appearance)
.output()?
.validate()
}
}
#[cfg(test)]
mod tests {
use serial_test::serial;
use super::*;
use crate::mock;
#[test]
#[serial]
fn test_appearance() -> Result<()> {
mock::device()?.boot()?;
mock::device()?.ui().set_appearance(Appearance::Dark)?;
assert_eq!(mock::device()?.ui().appearance()?, Appearance::Dark);
mock::device()?.ui().set_appearance(Appearance::Light)?;
assert_eq!(mock::device()?.ui().appearance()?, Appearance::Light);
mock::device()?.shutdown()?;
Ok(())
}
}