use super::{Device, Result, Validate};
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum PrivacyService {
All,
Calendar,
ContactsLimited,
Contacts,
Location,
LocationAlways,
PhotosAdd,
Photos,
MediaLibrary,
Microphone,
Motion,
Reminders,
Siri,
}
impl ToString for PrivacyService {
fn to_string(&self) -> String {
match self {
PrivacyService::All => "all",
PrivacyService::Calendar => "calendar",
PrivacyService::ContactsLimited => "contacts-limited",
PrivacyService::Contacts => "contacts",
PrivacyService::Location => "location",
PrivacyService::LocationAlways => "location-always",
PrivacyService::PhotosAdd => "photos-add",
PrivacyService::Photos => "photos",
PrivacyService::MediaLibrary => "media-library",
PrivacyService::Microphone => "microphone",
PrivacyService::Motion => "motion",
PrivacyService::Reminders => "reminders",
PrivacyService::Siri => "siri",
}
.to_owned()
}
}
pub struct Privacy {
device: Device,
}
impl Device {
pub fn privacy(&self) -> Privacy {
Privacy {
device: self.clone(),
}
}
}
impl Privacy {
pub fn grant(&self, service: PrivacyService, bundle_id: &str) -> Result<()> {
self.device
.simctl()
.command("privacy")
.arg(&self.device.udid)
.arg("grant")
.arg(service.to_string())
.arg(bundle_id)
.output()?
.validate()
}
pub fn revoke(&self, service: PrivacyService, bundle_id: &str) -> Result<()> {
self.device
.simctl()
.command("privacy")
.arg(&self.device.udid)
.arg("revoke")
.arg(service.to_string())
.arg(bundle_id)
.output()?
.validate()
}
pub fn reset(&self, service: PrivacyService, bundle_id: &str) -> Result<()> {
self.device
.simctl()
.command("privacy")
.arg(&self.device.udid)
.arg("reset")
.arg(service.to_string())
.arg(bundle_id)
.output()?
.validate()
}
pub fn reset_all(&self, service: PrivacyService) -> Result<()> {
self.device
.simctl()
.command("privacy")
.arg(&self.device.udid)
.arg("reset")
.arg(service.to_string())
.output()?
.validate()
}
}
#[cfg(test)]
mod tests {
use serial_test::serial;
use super::*;
use crate::mock;
#[test]
#[serial]
fn test_privacy_grant() -> Result<()> {
mock::device()?.boot()?;
mock::device()?
.privacy()
.grant(PrivacyService::Location, "com.apple.Maps")?;
mock::device()?.shutdown()?;
Ok(())
}
#[test]
#[serial]
fn test_privacy_revoke() -> Result<()> {
mock::device()?.boot()?;
mock::device()?
.privacy()
.revoke(PrivacyService::Location, "com.apple.Maps")?;
mock::device()?.shutdown()?;
Ok(())
}
#[test]
#[serial]
fn test_privacy_reset() -> Result<()> {
mock::device()?.boot()?;
mock::device()?
.privacy()
.grant(PrivacyService::Location, "com.apple.Maps")?;
mock::device()?
.privacy()
.reset(PrivacyService::Location, "com.apple.Maps")?;
mock::device()?.shutdown()?;
Ok(())
}
#[test]
#[serial]
fn test_privacy_reset_all() -> Result<()> {
mock::device()?.boot()?;
mock::device()?
.privacy()
.grant(PrivacyService::Location, "com.apple.Maps")?;
mock::device()?
.privacy()
.reset_all(PrivacyService::Location)?;
mock::device()?.shutdown()?;
Ok(())
}
}