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
//! Clipboard management
use async_trait::async_trait;
use base64::Engine;
use base64::engine::general_purpose;
use fantoccini::error::CmdError;
use http::Method;
use serde_derive::Serialize;
use serde_json::json;

use crate::{AndroidClient, AppiumClientTrait, IOSClient};
use crate::commands::AppiumCommand;

#[derive(Copy, Clone, Serialize, Debug, Eq, PartialEq)]
#[serde(rename_all = "lowercase")]
pub enum ClipboardContentType {
    PlainText,
    Image,
    URL,
}

/// Retrieve and save data in device's clipboard
#[async_trait]
pub trait HasClipboard: AppiumClientTrait {
    async fn get_clipboard(&self, content_type: ClipboardContentType) -> Result<Vec<u8>, CmdError> {
        let value = self.issue_cmd(AppiumCommand::Custom(
            Method::POST,
            "appium/device/get_clipboard".to_string(),
            Some(json!({
                "contentType": content_type
            })),
        )).await?;

        let base64: String = serde_json::from_value::<String>(value)?
            .replace('\n', "");

        Ok(general_purpose::STANDARD.decode(base64)
            .map_err(|e| CmdError::NotJson(format!("{e}")))?)
    }

    async fn set_clipboard<CT>(&self, content_type: ClipboardContentType, content: CT) -> Result<(), CmdError>
        where CT: AsRef<[u8]> + Send
    {
        let content = general_purpose::STANDARD.encode(content);

        self.issue_cmd(AppiumCommand::Custom(
            Method::POST,
            "appium/device/set_clipboard".to_string(),
            Some(json!({
                "contentType": content_type,
                "content": content
            })),
        )).await?;

        Ok(())
    }

    async fn set_clipboard_text<CT>(&self, content: CT) -> Result<(), CmdError>
        where CT: AsRef<[u8]> + Send
    {
        self.set_clipboard(ClipboardContentType::PlainText, content).await
    }

    async fn get_clipboard_text(&self) -> Result<String, CmdError> {
        let clipboard = self.get_clipboard(ClipboardContentType::PlainText).await?;
        Ok(String::from_utf8(clipboard)
            .map_err(|e| CmdError::NotJson(format!("{e}")))?)
    }
}

#[async_trait]
impl HasClipboard for AndroidClient {}

#[async_trait]
impl HasClipboard for IOSClient {}

#[async_trait]
pub trait HasAndroidClipboard: HasClipboard {
    async fn set_clipboard_labeled<CT>(&self, label: &str, content_type: ClipboardContentType, content: CT) -> Result<(), CmdError>
        where CT: AsRef<[u8]> + Send
    {
        let content = general_purpose::STANDARD.encode(content);

        self.issue_cmd(AppiumCommand::Custom(
            Method::POST,
            "appium/device/set_clipboard".to_string(),
            Some(json!({
                "label": label,
                "contentType": content_type,
                "content": content
            })),
        )).await?;

        Ok(())
    }

    async fn set_clipboard_text_labeled<CT>(&self, label: &str, content: CT) -> Result<(), CmdError>
        where CT: AsRef<[u8]> + Send {
        self.set_clipboard_labeled(label, ClipboardContentType::PlainText, content).await
    }
}

#[async_trait]
impl HasAndroidClipboard for AndroidClient {}