#[cfg(not(any(target_os = "ios", target_os = "macos")))]
fn main() {}
#[cfg(target_os = "ios")]
fn main() {
phone::main();
}
#[cfg(target_os = "macos")]
fn main() {
mac::main();
}
#[cfg(any(target_os = "ios", target_os = "macos"))]
#[allow(dead_code, unused_attributes)]
#[path = "../examples/gallery.rs"]
mod gallery;
#[cfg(target_os = "macos")]
#[allow(dead_code)]
#[path = "../examples/ios_bundle.rs"]
mod ios_bundle;
#[cfg(target_os = "ios")]
#[derive(Debug, Default)]
struct Parts {
presses: u32,
text: String,
tick: bool,
amount: f32,
}
#[cfg(target_os = "ios")]
fn parts(parts: &mut Parts, ui: &mut taut::Ui) {
ui.theme = ui.system_theme();
ui.column().pad(16.0).gap(12.0).open();
ui.label("Parts");
parts.presses += ui.button("Press").clicked as u32;
ui.text_box(&mut parts.text, "Type here");
ui.checkbox(&mut parts.tick, "Tick");
ui.slider(&mut parts.amount, 0.0..=10.0);
ui.label(&format!("Pressed {} times", parts.presses));
ui.close();
}
#[cfg(target_os = "ios")]
mod phone {
use super::{Parts, parts};
use std::cell::RefCell;
use std::ffi::{CStr, c_char, c_void};
use std::io::Write;
use std::path::PathBuf;
use std::rc::Rc;
use std::time::{Duration, Instant};
use taut::{Role, Ui, Window};
type Obj = *mut c_void;
#[repr(C)]
#[derive(Clone, Copy, Debug, Default)]
struct Point {
x: f64,
y: f64,
}
#[repr(C)]
#[derive(Clone, Copy, Debug, Default)]
struct Rect {
origin: Point,
size: Point,
}
#[repr(C)]
#[derive(Clone, Copy, Debug, Default)]
struct Insets {
top: f64,
left: f64,
bottom: f64,
right: f64,
}
#[repr(C)]
#[derive(Clone, Copy, Debug, Default)]
struct Range {
location: usize,
length: usize,
}
#[link(name = "objc")]
unsafe extern "C" {
fn objc_getClass(name: *const c_char) -> Obj;
fn sel_registerName(name: *const c_char) -> *const c_void;
fn objc_msgSend();
fn objc_allocateClassPair(superclass: Obj, name: *const c_char, extra: usize) -> Obj;
fn objc_registerClassPair(class: Obj);
fn class_addMethod(class: Obj, name: *const c_void, imp: *const c_void, types: *const c_char) -> u8;
}
fn class(name: &CStr) -> Obj {
unsafe { objc_getClass(name.as_ptr()) }
}
fn sel(name: &CStr) -> *const c_void {
unsafe { sel_registerName(name.as_ptr()) }
}
unsafe fn send0<R>(to: Obj, name: &CStr) -> R {
let call: unsafe extern "C" fn(Obj, *const c_void) -> R = unsafe { std::mem::transmute(objc_msgSend as unsafe extern "C" fn()) };
unsafe { call(to, sel(name)) }
}
unsafe fn send1<R, A>(to: Obj, name: &CStr, a: A) -> R {
let call: unsafe extern "C" fn(Obj, *const c_void, A) -> R = unsafe { std::mem::transmute(objc_msgSend as unsafe extern "C" fn()) };
unsafe { call(to, sel(name), a) }
}
unsafe fn send2<R, A, B>(to: Obj, name: &CStr, a: A, b: B) -> R {
let call: unsafe extern "C" fn(Obj, *const c_void, A, B) -> R = unsafe { std::mem::transmute(objc_msgSend as unsafe extern "C" fn()) };
unsafe { call(to, sel(name), a, b) }
}
fn string(text: &str) -> Obj {
let text = std::ffi::CString::new(text).unwrap();
unsafe { send1(class(c"NSString"), c"stringWithUTF8String:", text.as_ptr()) }
}
fn text_of(string: Obj) -> String {
if string.is_null() {
return String::new();
}
let bytes: *const c_char = unsafe { send0(string, c"UTF8String") };
unsafe { CStr::from_ptr(bytes) }.to_string_lossy().into_owned()
}
fn items(array: Obj) -> Vec<Obj> {
let count: usize = unsafe { send0(array, c"count") };
(0..count).map(|at| unsafe { send1(array, c"objectAtIndex:", at) }).collect()
}
thread_local! {
static TOUCHES: RefCell<Vec<(usize, Point, isize)>> = const { RefCell::new(Vec::new()) };
static KEYS: RefCell<Vec<(usize, isize, isize, &'static str)>> = const { RefCell::new(Vec::new()) };
}
extern "C" fn touch_place(touch: Obj, _: *const c_void, _: Obj) -> Point {
TOUCHES.with_borrow(|touches| touches.iter().find(|(of, ..)| *of == touch as usize).map(|(_, at, _)| *at).unwrap_or_default())
}
extern "C" fn touch_type(touch: Obj, _: *const c_void) -> isize {
TOUCHES.with_borrow(|touches| touches.iter().find(|(of, ..)| *of == touch as usize).map_or(0, |(.., kind)| *kind))
}
extern "C" fn key_code(key: Obj, _: *const c_void) -> isize {
KEYS.with_borrow(|keys| keys.iter().find(|(of, ..)| *of == key as usize).map_or(0, |(_, code, ..)| *code))
}
extern "C" fn key_flags(key: Obj, _: *const c_void) -> isize {
KEYS.with_borrow(|keys| keys.iter().find(|(of, ..)| *of == key as usize).map_or(0, |(_, _, flags, _)| *flags))
}
extern "C" fn key_letters(key: Obj, _: *const c_void) -> Obj {
string(KEYS.with_borrow(|keys| keys.iter().find(|(of, ..)| *of == key as usize).map_or("", |(.., letters)| letters)))
}
extern "C" fn press_key(press: Obj, _: *const c_void) -> Obj {
KEYS.with_borrow(|keys| keys.iter().find(|(of, ..)| *of == press as usize + 1).map_or(std::ptr::null_mut(), |(_, code, ..)| *code as Obj))
}
fn stand_in(name: &CStr, of: &CStr, methods: &[(&CStr, *const c_void, &CStr)]) -> Obj {
let known = class(name);
if !known.is_null() {
return known;
}
unsafe {
let made = objc_allocateClassPair(class(of), name.as_ptr(), 0);
for (method, imp, types) in methods {
class_addMethod(made, sel(method), *imp, types.as_ptr());
}
objc_registerClassPair(made);
made
}
}
fn touch_at(at: Point, kind: isize) -> Obj {
let class = stand_in(c"CheckTouch", c"UITouch", &[(c"locationInView:", touch_place as *const c_void, c"{CGPoint=dd}@:@"), (c"type", touch_type as *const c_void, c"q@:")]);
let touch: Obj = unsafe { send0(send0::<Obj>(class, c"alloc"), c"init") };
TOUCHES.with_borrow_mut(|touches| touches.push((touch as usize, at, kind)));
touch
}
fn press_of(code: isize, flags: isize, letters: &'static str) -> Obj {
let key_class = stand_in(
c"CheckKey",
c"UIKey",
&[(c"keyCode", key_code as *const c_void, c"q@:"), (c"modifierFlags", key_flags as *const c_void, c"q@:"), (c"charactersIgnoringModifiers", key_letters as *const c_void, c"@@:")],
);
let press_class = stand_in(c"CheckPress", c"UIPress", &[(c"key", press_key as *const c_void, c"@@:")]);
let key: Obj = unsafe { send0(key_class, c"alloc") };
let press: Obj = unsafe { send0(send0::<Obj>(press_class, c"alloc"), c"init") };
KEYS.with_borrow_mut(|keys| {
keys.push((key as usize, code, flags, letters));
keys.push((press as usize + 1, key as isize, 0, ""));
});
press
}
fn set_of(object: Obj) -> Obj {
unsafe { send1(class(c"NSSet"), c"setWithObject:", object) }
}
fn view() -> Obj {
unsafe {
let app: Obj = send0(class(c"UIApplication"), c"sharedApplication");
let scene: Obj = send0(send0::<Obj>(app, c"connectedScenes"), c"anyObject");
let window = items(send0(scene, c"windows")).first().copied().unwrap_or(std::ptr::null_mut());
send0(send0::<Obj>(window, c"rootViewController"), c"view")
}
}
fn subview(name: &CStr) -> Obj {
let views = items(unsafe { send0(view(), c"subviews") });
views.into_iter().find(|one| unsafe { send1::<u8, _>(*one, c"isKindOfClass:", class(name)) } != 0).unwrap_or(std::ptr::null_mut())
}
fn input() -> Obj {
subview(c"TautTextInput")
}
fn origin() -> Point {
let insets: Insets = unsafe { send0(view(), c"safeAreaInsets") };
Point { x: insets.left, y: insets.top }
}
fn folder() -> PathBuf {
let folder = std::env::temp_dir().join("taut-check");
_ = std::fs::create_dir_all(&folder);
folder
}
fn note(file: &str, line: &str) {
eprintln!("{file}: {line}");
let mut file = std::fs::OpenOptions::new().create(true).append(true).open(folder().join(file)).unwrap();
writeln!(file, "{line}").unwrap();
}
fn elements() -> Vec<(String, String, u64, Rect)> {
let elements: Obj = unsafe { send0(view(), c"accessibilityElements") };
let read = |element: Obj| unsafe {
(
text_of(send0(element, c"accessibilityLabel")),
text_of(send0(element, c"accessibilityValue")),
send0::<u64>(element, c"accessibilityTraits"),
send0::<Rect>(element, c"accessibilityFrame"),
)
};
items(elements).into_iter().map(read).collect()
}
fn element(label: &str) -> Obj {
let elements: Obj = unsafe { send0(view(), c"accessibilityElements") };
items(elements).into_iter().find(|element| text_of(unsafe { send0(*element, c"accessibilityLabel") }) == label).unwrap_or(std::ptr::null_mut())
}
pub fn main() {
let mode = std::env::var("TAUT_IOS_CHECK").unwrap_or_default();
_ = std::fs::remove_dir_all(folder());
std::thread::spawn(|| {
loop {
std::thread::sleep(Duration::from_millis(100));
taut::platform::Waker.wake();
}
});
let window = Window { title: "taut check", ..Default::default() };
if mode == "parts" {
let state = Rc::new(RefCell::new((Parts::default(), Script::default())));
taut::run(window, move |ui| {
let (parts_now, script) = &mut *state.borrow_mut();
parts(parts_now, ui);
script.step(parts_now, ui);
});
} else {
let mut app = super::gallery::app();
let mut shown = Shown::default();
taut::run(window, move |ui| {
super::gallery::describe(&mut app, ui);
shown.step(ui);
});
}
}
#[derive(Default)]
struct Shown {
start: Option<Instant>,
stage: u32,
dark: Option<bool>,
away: Option<bool>,
}
impl Shown {
fn step(&mut self, ui: &mut Ui) {
let since = self.start.get_or_insert_with(Instant::now).elapsed();
let at = origin();
match self.stage {
0 if since > Duration::from_secs(3) => {
let size = ui.size();
drawn_here(ui);
note("ready.txt", &format!("{} {} {} {} {} {} {}", size.w, size.h, ui.scale(), at.x, at.y, ui.system_dark() as u8, ui.text_scale()));
self.stage = 1;
}
1 if folder().join("shot").exists() => {
_ = elements();
self.stage = 2;
}
2 if !ui.visible_access_nodes().is_empty() => self.stage = 3,
3 => {
for (label, value, traits, frame) in elements() {
note("elements.txt", &format!("{label:?} {value:?} {traits:#x} {:.0},{:.0} {:.0}x{:.0}", frame.origin.x, frame.origin.y, frame.size.x, frame.size.y));
}
note("elements.txt", "done");
self.stage = 4;
}
_ => {}
}
self.looks(ui);
}
fn looks(&mut self, ui: &mut Ui) {
if self.stage < 4 {
return;
}
if self.dark.replace(ui.system_dark()) != Some(ui.system_dark()) {
note("looks.txt", &format!("dark {}", ui.system_dark()));
}
if self.away.replace(ui.window_minimized()) != Some(ui.window_minimized()) {
note("looks.txt", &format!("away {}", ui.window_minimized()));
}
}
}
fn drawn_here(shown: &Ui) {
let mut ui = Ui::new(taut::platform::system_text(), Box::new(taut::LocalClipboard::default()));
ui.resize(shown.size(), shown.scale());
let looks = taut::Preferences { dark: shown.system_dark(), text_scale: shown.text_scale(), ..taut::Preferences::default() };
ui.set_preferences(looks);
let mut app = super::gallery::app();
let mut surface = taut::Surface::default();
for run in 0..4 {
ui.set_time(Duration::from_millis(run * 500));
ui.run(&mut |ui| super::gallery::describe(&mut app, ui));
surface.update(&mut ui);
ui.changed();
}
let solid: Vec<u32> = surface.pixels.iter().map(|pixel| pixel | 0xff00_0000).collect();
_ = std::fs::write(folder().join("drawn.png"), taut::png::encode_rgba(surface.width, &solid).unwrap_or_default());
}
#[derive(Default)]
struct Script {
step: usize,
since: Option<Instant>,
acted: bool,
held: Obj,
}
impl Script {
fn step(&mut self, parts: &mut Parts, ui: &mut Ui) {
let since = *self.since.get_or_insert_with(Instant::now);
if since.elapsed() < Duration::from_millis(500) && self.step == 0 {
return;
}
let name = STEPS.get(self.step).copied().unwrap_or("");
if name.is_empty() {
return;
}
if !self.acted {
self.acted = true;
self.act(name, ui);
return;
}
let passed = self.check(name, parts, ui);
match passed {
Some(true) => note("parts.txt", &format!("ok {name}")),
None if since.elapsed() < Duration::from_secs(8) => return,
_ => note("parts.txt", &format!("FAILED {name}: {parts:?}")),
}
(self.step, self.since, self.acted) = (self.step + 1, Some(Instant::now()), false);
if STEPS[self.step].is_empty() {
note("parts.txt", "done");
}
}
fn middle_of(ui: &Ui, role: Role) -> Point {
let node = ui.visible_access_nodes().iter().find(|node| node.role == role).map(|node| node.rect).unwrap_or_default();
let at = origin();
Point { x: at.x + f64::from(node.x + node.w / 2.0), y: at.y + f64::from(node.y + node.h / 2.0) }
}
fn tap(&mut self, at: Point, kind: isize) {
let touch = touch_at(at, kind);
unsafe { send2::<(), _, _>(view(), c"touchesBegan:withEvent:", set_of(touch), std::ptr::null_mut::<c_void>()) };
self.held = touch;
}
fn key(code: isize, flags: isize, letters: &'static str) {
let press = press_of(code, flags, letters);
let target = if unsafe { send0::<u8>(input(), c"isFirstResponder") } != 0 { input() } else { view() };
unsafe {
send2::<(), _, _>(target, c"pressesBegan:withEvent:", set_of(press), std::ptr::null_mut::<c_void>());
send2::<(), _, _>(target, c"pressesEnded:withEvent:", set_of(press), std::ptr::null_mut::<c_void>());
}
}
fn act(&mut self, name: &str, ui: &mut Ui) {
let input = input();
unsafe {
match name {
"reader asked" => _ = elements(),
"finger taps the button" => self.tap(Self::middle_of(ui, Role::Button), 0),
"pointer clicks the button" => self.tap(Self::middle_of(ui, Role::Button), 3),
"pencil taps the button" => self.tap(Self::middle_of(ui, Role::Button), 2),
"finger taps the text box" => self.tap(Self::middle_of(ui, Role::TextBox), 0),
"typed text" => send1::<(), _>(input, c"insertText:", string("h\u{e9}llo")),
"composed text" => {
send2::<(), _, _>(input, c"setMarkedText:selectedRange:", string("\u{304b}"), Range { location: 1, length: 0 });
send0::<()>(input, c"unmarkText");
}
"keyboard's delete" => send0::<()>(input, c"deleteBackward"),
"hardware backspace" => Self::key(0x2a, 0, ""),
"select all and copy" => {
send1::<(), _>(input, c"selectAll:", std::ptr::null_mut::<c_void>());
send1::<(), _>(input, c"copy:", std::ptr::null_mut::<c_void>());
}
"command-a and backspace" => {
Self::key(0x04, 1 << 20, "a");
Self::key(0x2a, 0, "");
}
"paste" => send1::<(), _>(input, c"paste:", std::ptr::null_mut::<c_void>()),
"reader ticks" => _ = send0::<u8>(element("Tick"), c"accessibilityActivate"),
"reader steps the slider" => {
let slider = elements().iter().position(|(.., traits, _)| traits & 0x1000 != 0);
let elements: Obj = send0(view(), c"accessibilityElements");
let slider = slider.map(|at| items(elements)[at]).unwrap_or(std::ptr::null_mut());
send0::<()>(slider, c"accessibilityIncrement");
}
"turned sideways" => turn(true),
"turned upright" => turn(false),
_ => {}
}
}
}
fn check(&mut self, name: &str, parts: &Parts, ui: &Ui) -> Option<bool> {
if !self.held.is_null() {
let touch = std::mem::replace(&mut self.held, std::ptr::null_mut());
unsafe { send2::<(), _, _>(view(), c"touchesEnded:withEvent:", set_of(touch), std::ptr::null_mut::<c_void>()) };
return None;
}
let pasted = || text_of(unsafe { send0(send0::<Obj>(class(c"UIPasteboard"), c"generalPasteboard"), c"string") });
let typing = || unsafe { send0::<u8>(input(), c"isFirstResponder") } != 0;
let tick = || elements().iter().find(|(label, ..)| label == "Tick").map(|(_, _, traits, _)| *traits);
let size = ui.size();
let shows = match name {
"reader asked" => !ui.visible_access_nodes().is_empty() && !elements().is_empty(),
"finger taps the button" => parts.presses == 1,
"pointer clicks the button" => parts.presses == 2,
"pencil taps the button" => parts.presses == 3,
"finger taps the text box" => typing() && ui.ime_area().is_some(),
"typed text" => parts.text == "h\u{e9}llo",
"composed text" => parts.text == "h\u{e9}llo\u{304b}",
"keyboard's delete" => parts.text == "h\u{e9}llo",
"hardware backspace" => parts.text == "h\u{e9}ll",
"select all and copy" => pasted() == "h\u{e9}ll",
"command-a and backspace" => parts.text.is_empty(),
"paste" => parts.text == "h\u{e9}ll",
"reader ticks" => parts.tick && tick().is_some_and(|traits| traits & 0x9 == 0x9),
"reader steps the slider" => parts.amount > 0.0,
"turned sideways" => size.w > size.h,
"turned upright" => size.w < size.h,
_ => true,
};
shows.then_some(true)
}
}
const STEPS: [&str; 17] = [
"reader asked",
"finger taps the button",
"pointer clicks the button",
"pencil taps the button",
"finger taps the text box",
"typed text",
"composed text",
"keyboard's delete",
"hardware backspace",
"select all and copy",
"command-a and backspace",
"paste",
"reader ticks",
"reader steps the slider",
"turned sideways",
"turned upright",
"",
];
fn turn(sideways: bool) {
let orientations: usize = if sideways { 1 << 3 | 1 << 4 } else { 1 << 1 };
unsafe {
let app: Obj = send0(class(c"UIApplication"), c"sharedApplication");
let scene: Obj = send0(send0::<Obj>(app, c"connectedScenes"), c"anyObject");
let wanted: Obj = send1(send0::<Obj>(class(c"UIWindowSceneGeometryPreferencesIOS"), c"alloc"), c"initWithInterfaceOrientations:", orientations);
send2::<(), _, _>(scene, c"requestGeometryUpdateWithPreferences:errorHandler:", wanted, std::ptr::null_mut::<c_void>());
}
}
}
#[cfg(target_os = "macos")]
mod mac {
use super::ios_bundle;
use std::path::{Path, PathBuf};
use std::process::Command;
use std::time::{Duration, Instant};
use taut::geom::Size;
use taut::{LocalClipboard, Preferences, Surface, Ui};
const CHECK_ID: &str = "dev.taut.check";
pub fn main() {
if std::env::var_os("TAUT_IOS_SIMULATOR").is_none() {
return println!("The iOS checks run with TAUT_IOS_SIMULATOR=1 (they boot a simulator).");
}
let failed = match checks() {
Ok(failed) => failed,
Err(error) => vec![error],
};
if !failed.is_empty() {
eprintln!("FAILED:\n{}", failed.join("\n"));
std::process::exit(1);
}
println!("all iOS checks passed");
}
fn built() -> PathBuf {
Path::new(env!("CARGO_MANIFEST_DIR")).join("target").join("ios-check")
}
fn shots() -> Option<PathBuf> {
let folder = PathBuf::from(std::env::var_os("TAUT_SHOTS")?);
std::fs::create_dir_all(&folder).ok()?;
Some(folder)
}
fn check_app() -> Result<PathBuf, String> {
let mut cargo = Command::new(std::env::var("CARGO").unwrap_or_else(|_| "cargo".into()));
cargo.args(["build", "--test", "ios", "--features", "touch", "--target", "aarch64-apple-ios-sim", "--message-format=json"]).env("CARGO_TARGET_DIR", built());
let said = ios_bundle::ran(&mut cargo)?;
let program = said.lines().filter(|line| line.contains("\"executable\":\"") && line.contains("\"name\":\"ios\"")).find_map(|line| line.split("\"executable\":\"").nth(1)?.split('"').next());
let program = PathBuf::from(program.ok_or("no test program built")?);
let app = ios_bundle::bundle(&program, "check", CHECK_ID, false, &built().join("ios")).map_err(|error| error.to_string())?;
ios_bundle::sign_for_simulator(&app)?;
Ok(app)
}
fn simctl(args: &[&str]) -> Result<String, String> {
ios_bundle::ran(Command::new("xcrun").arg("simctl").args(args))
}
fn launch(device: &str, id: &str, mode: &str) -> Result<(), String> {
let logs = built().join(format!("{id}-{mode}"));
let (out, err) = (format!("--stdout={}.out", logs.display()), format!("--stderr={}.err", logs.display()));
let mut command = Command::new("xcrun");
command.args(["simctl", "launch", "--terminate-running-process", &out, &err, device, id]).env("SIMCTL_CHILD_TAUT_IOS_CHECK", mode);
ios_bundle::ran(&mut command).map(drop)
}
fn note(device: &str, id: &str, file: &str, until: impl Fn(&str) -> bool, wait: Duration) -> Result<String, String> {
let since = Instant::now();
let container = simctl(&["get_app_container", device, id, "data"])?;
let path = Path::new(container.trim()).join("tmp").join("taut-check").join(file);
loop {
let text = std::fs::read_to_string(&path).unwrap_or_default();
if until(&text) {
return Ok(text);
}
if since.elapsed() > wait {
return Err(format!("{file} after {wait:?}: {text:?}"));
}
std::thread::sleep(Duration::from_millis(250));
}
}
fn signal(device: &str, id: &str, file: &str) -> Result<(), String> {
let container = simctl(&["get_app_container", device, id, "data"])?;
std::fs::write(Path::new(container.trim()).join("tmp").join("taut-check").join(file), "").map_err(|error| error.to_string())
}
fn screen(device: &str, name: &str) -> Result<(usize, Vec<u32>), String> {
let file = built().join(format!("{name}.png"));
simctl(&["io", device, "screenshot", &file.to_string_lossy()])?;
if let Some(folder) = shots() {
_ = std::fs::copy(&file, folder.join(format!("{name}.png")));
}
taut::png::decode(&std::fs::read(&file).map_err(|error| error.to_string())?).ok_or_else(|| "the screenshot is no PNG".into())
}
struct Page {
size: Size,
scale: f32,
at: [f32; 2],
preferences: Preferences,
}
fn page_of(ready: &str) -> Result<Page, String> {
let numbers: Vec<f32> = ready.split_whitespace().filter_map(|word| word.parse().ok()).collect();
let [w, h, scale, x, y, dark, text_scale] = numbers[..] else { return Err(format!("ready.txt: {ready:?}")) };
let preferences = Preferences { dark: dark != 0.0, text_scale, ..Preferences::default() };
Ok(Page { size: Size { w, h }, scale, at: [x, y], preferences })
}
fn drawn_here(page: &Page) -> Surface {
let mut ui = Ui::new(taut::platform::system_text(), Box::new(LocalClipboard::default()));
ui.resize(page.size, page.scale);
ui.set_preferences(page.preferences);
let mut app = super::gallery::app();
let mut surface = Surface::default();
for run in 0..4 {
ui.set_time(Duration::from_millis(run * 500));
ui.run(&mut |ui| super::gallery::describe(&mut app, ui));
surface.update(&mut ui);
ui.changed();
}
surface
}
fn compared(screen: &(usize, Vec<u32>), page: &Page, here: &Surface, name: &str) -> (f64, u32) {
let (width, pixels) = screen;
let (left, top) = ((page.at[0] * page.scale).round() as usize, (page.at[1] * page.scale).round() as usize);
let (mut apart, mut most, mut diff) = (0usize, 0u32, vec![0u32; here.width * here.height]);
for y in 0..here.height {
for x in 0..here.width {
let theirs = pixels.get((top + y) * width + left + x).copied().unwrap_or(0);
let ours = here.pixels[y * here.width + x];
let worst = (0..3).map(|channel| ((theirs >> (channel * 8)) & 0xff).abs_diff((ours >> (channel * 8)) & 0xff)).max().unwrap_or(0);
most = most.max(worst);
apart += usize::from(worst > 32);
diff[y * here.width + x] = if worst > 32 { 0xffff_0000 } else { 0xff00_0000 | (((ours & 0x00ff_ffff) / 2) & 0x007f_7f7f) };
}
}
if let Some(folder) = shots() {
let solid: Vec<u32> = here.pixels.iter().map(|pixel| pixel | 0xff00_0000).collect();
_ = std::fs::write(folder.join(format!("{name}-mac.png")), taut::png::encode_rgba(here.width, &solid).unwrap_or_default());
_ = std::fs::write(folder.join(format!("{name}-diff.png")), taut::png::encode_rgba(here.width, &diff).unwrap_or_default());
}
(apart as f64 / (here.width * here.height).max(1) as f64, most)
}
fn checks() -> Result<Vec<String>, String> {
let mut failed = Vec::new();
let check = check_app()?;
let asked = ios_bundle::Asked { example: "gallery".into(), id: "dev.taut.gallery".into(), ..Default::default() };
let gallery = ios_bundle::build(&asked, &built())?;
let device = ios_bundle::newest_iphone()?;
println!("simulator {device}");
ios_bundle::boot(&device)?;
simctl(&["ui", &device, "appearance", "light"])?;
simctl(&["install", &device, &check.to_string_lossy()])?;
simctl(&["install", &device, &gallery.to_string_lossy()])?;
launch(&device, CHECK_ID, "gallery")?;
let ready = note(&device, CHECK_ID, "ready.txt", |text| text.ends_with('\n'), Duration::from_secs(120))?;
let page = page_of(&ready)?;
println!("page {}x{} at {:?}, scale {}", page.size.w, page.size.h, page.at, page.scale);
let here = drawn_here(&page);
let shown = screen(&device, "check-gallery")?;
let (share, most) = compared(&shown, &page, &here, "check-gallery");
println!("the screen against the Mac's drawing: {:.2}% of pixels apart, at most {most}", share * 100.0);
if share > 0.03 {
failed.push(format!("the screen differs from the Mac's drawing in {:.2}% of its pixels", share * 100.0));
}
let container = simctl(&["get_app_container", &device, CHECK_ID, "data"])?;
let drawn = std::fs::read(Path::new(container.trim()).join("tmp/taut-check/drawn.png")).unwrap_or_default();
let (width, pixels) = taut::png::decode(&drawn).ok_or("the app drew nothing")?;
let phone = Surface { width, height: pixels.len() / width.max(1), pixels };
let (share, most) = compared(&shown, &page, &phone, "check-gallery-phone");
println!("the screen against the phone's own drawing: {:.3}% of pixels apart, at most {most}", share * 100.0);
if share > 0.001 {
failed.push(format!("the screen differs from the phone's own drawing in {:.3}% of its pixels", share * 100.0));
}
signal(&device, CHECK_ID, "shot")?;
let told = note(&device, CHECK_ID, "elements.txt", |text| text.contains("done"), Duration::from_secs(60))?;
let count = told.lines().count() - 1;
println!("{count} elements told to readers; the first:\n{}", told.lines().take(25).collect::<Vec<_>>().join("\n"));
if count < 10 {
failed.push(format!("only {count} elements told to readers"));
}
simctl(&["ui", &device, "appearance", "dark"])?;
let dark = note(&device, CHECK_ID, "looks.txt", |text| text.contains("dark true"), Duration::from_secs(30));
failed.extend(dark.err().map(|error| format!("dark mode not heard: {error}")));
simctl(&["ui", &device, "appearance", "light"])?;
simctl(&["launch", &device, "com.apple.Preferences"])?;
let away = note(&device, CHECK_ID, "looks.txt", |text| text.contains("away true"), Duration::from_secs(30));
failed.extend(away.err().map(|error| format!("the background not heard: {error}")));
simctl(&["launch", &device, CHECK_ID])?;
let back = note(&device, CHECK_ID, "looks.txt", |text| text.contains("away false"), Duration::from_secs(30));
failed.extend(back.err().map(|error| format!("coming back not heard: {error}")));
if let Ok(looks) = note(&device, CHECK_ID, "looks.txt", |_| true, Duration::ZERO) {
println!("looks:\n{looks}");
}
launch(&device, CHECK_ID, "parts")?;
let parts = note(&device, CHECK_ID, "parts.txt", |text| text.contains("done"), Duration::from_secs(240));
let parts = parts.unwrap_or_else(|error| error);
println!("parts:\n{parts}");
failed.extend(parts.lines().filter(|line| line.starts_with("FAILED") || line.contains("after")).map(str::to_owned));
launch(&device, "dev.taut.gallery", "")?;
std::thread::sleep(Duration::from_secs(6));
let shown = screen(&device, "gallery")?;
let (share, most) = compared(&shown, &page, &here, "gallery");
println!("the gallery example against the Mac's drawing: {:.2}% of pixels apart, at most {most}", share * 100.0);
if share > 0.03 {
failed.push(format!("the gallery example differs from the Mac's drawing in {:.2}% of its pixels", share * 100.0));
}
for log in std::fs::read_dir(built()).map_err(|error| error.to_string())?.flatten() {
let path = log.path();
if path.extension().is_some_and(|ending| ending == "err" || ending == "out") {
let text = std::fs::read_to_string(&path).unwrap_or_default();
let tail: Vec<&str> = text.lines().rev().take(15).collect();
println!("--- {}:\n{}", path.display(), tail.into_iter().rev().collect::<Vec<_>>().join("\n"));
}
}
_ = simctl(&["terminate", &device, CHECK_ID]);
_ = simctl(&["terminate", &device, "dev.taut.gallery"]);
Ok(failed)
}
}