1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
//! Management of apps on the device
use std::time::Duration;
use async_trait::async_trait;
use fantoccini::error::CmdError;
use http::Method;
use serde_json::json;
use serde::{Serialize, Deserialize};
use crate::{AndroidClient, AppiumClientTrait, IOSClient};
use crate::commands::AppiumCommand;

/// Inspect or install other apps
#[async_trait]
pub trait InteractsWithApps: AppiumClientTrait {
    async fn install_app(&self, path: &str) -> Result<(), CmdError> {
        self.issue_cmd(AppiumCommand::Custom(
            Method::POST,
            "appium/device/install_app".to_string(),
            Some(json!({
                "appPath": path
            })),
        )).await?;
        Ok(())
    }

    async fn is_app_installed(&self, bundle_id: &str) -> Result<bool, CmdError> {
        let value = self.issue_cmd(AppiumCommand::Custom(
            Method::POST,
            "appium/device/app_installed".to_string(),
            Some(json!({
                "bundleId": bundle_id
            })),
        )).await?;

        Ok(serde_json::from_value(value)?)
    }

    async fn run_app_in_background(&self, duration: Duration) -> Result<(), CmdError> {
        self.issue_cmd(AppiumCommand::Custom(
            Method::POST,
            "appium/app/background".to_string(),
            Some(json!({
                "seconds": duration.as_secs()
            })),
        )).await?;

        Ok(())
    }

    async fn remove_app(&self, bundle_id: &str) -> Result<(), CmdError> {
        self.issue_cmd(AppiumCommand::Custom(
            Method::POST,
            "appium/device/remove_app".to_string(),
            Some(json!({
                "bundleId": bundle_id
            })),
        )).await?;

        Ok(())
    }

    async fn activate_app(&self, bundle_id: &str) -> Result<(), CmdError> {
        self.issue_cmd(AppiumCommand::Custom(
            Method::POST,
            "appium/device/activate_app".to_string(),
            Some(json!({
                "bundleId": bundle_id
            })),
        )).await?;

        Ok(())
    }

    async fn app_state(&self, bundle_id: &str) -> Result<AppState, CmdError> {
        let value = self.issue_cmd(AppiumCommand::Custom(
            Method::POST,
            "appium/device/app_state".to_string(),
            Some(json!({
                "bundleId": bundle_id
            })),
        )).await?;

        Ok(serde_json::from_value(value)?)
    }

    async fn terminate_app(&self, bundle_id: &str) -> Result<(), CmdError> {
        self.issue_cmd(AppiumCommand::Custom(
            Method::POST,
            "appium/device/terminate_app".to_string(),
            Some(json!({
                "bundleId": bundle_id
            })),
        )).await?;

        Ok(())
    }
}

bitflags::bitflags! {
    #[repr(transparent)]
    #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
    pub struct AppState: i8 {
        const NOT_INSTALLED = 0;
        const NOT_RUNNING = 1;
        const RUNNING_IN_BACKGROUND_SUSPENDED = 2;
        const RUNNING_IN_BACKGROUND = 3;
        const RUNNING_IN_FOREGROUND = 4;
    }
}

#[async_trait]
impl InteractsWithApps for AndroidClient {}

#[async_trait]
impl InteractsWithApps for IOSClient {}