polyhorn_cli/test/device.rs
1use super::{Error, Metadata};
2
3/// Platform-independent wrapper around the platform-dependent device types.
4/// Note: on iOS, UI automation only works in the iOS Simulator, not on physical
5/// devices. As such, it's not necessary to make that distinction here.
6#[derive(Clone, Debug)]
7pub enum Device {
8 /// Wrapper for iOS simulators.
9 IOS {
10 /// Wrapper around the iOS simulator device itself.
11 device: simctl::Device,
12
13 /// Wrapper around the iOS runtime (i.e. version of iOS) that runs on
14 /// the simulated device.
15 runtime: simctl::list::Runtime,
16
17 /// Device type (i.e. hardware) that is simulated by the device.
18 device_type: simctl::list::DeviceType,
19 },
20}
21
22impl Device {
23 /// Opens an URL on this device.
24 pub fn open_url(&self, url: &str) -> Result<(), Error> {
25 match self {
26 Device::IOS { device, .. } => device.open_url(url).map_err(|error| Error::IOS(error)),
27 }
28 }
29
30 /// Takes a screenshot on this device and returns the PNG-encoded result.
31 pub fn screenshot(&self) -> Result<Vec<u8>, Error> {
32 match self {
33 Device::IOS { device, .. } => device
34 .io()
35 .screenshot(
36 simctl::io::ImageType::Png,
37 simctl::io::Display::Internal,
38 simctl::io::Mask::Ignored,
39 )
40 .map_err(|error| Error::IOS(error)),
41 }
42 }
43
44 /// Summarizes this device into snapshot metadata.
45 pub fn metadata(&self) -> Metadata {
46 match self {
47 Device::IOS {
48 device_type,
49 runtime,
50 ..
51 } => Metadata {
52 device: Some(device_type.name.clone()),
53 os: Some(runtime.name.clone()),
54 os_version: Some(runtime.version.clone()),
55 ..Default::default()
56 },
57 }
58 }
59}