forked from appium/java-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHasClipboard.java
More file actions
93 lines (85 loc) · 3.58 KB
/
HasClipboard.java
File metadata and controls
93 lines (85 loc) · 3.58 KB
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
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* See the NOTICE file distributed with this work for additional
* information regarding copyright ownership.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.appium.java_client.clipboard;
import io.appium.java_client.CanRememberExtensionPresence;
import io.appium.java_client.CommandExecutionHelper;
import io.appium.java_client.ExecutesMethod;
import org.openqa.selenium.UnsupportedCommandException;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.Map;
import static io.appium.java_client.MobileCommand.GET_CLIPBOARD;
import static io.appium.java_client.MobileCommand.SET_CLIPBOARD;
import static java.util.Locale.ROOT;
import static java.util.Objects.requireNonNull;
public interface HasClipboard extends ExecutesMethod, CanRememberExtensionPresence {
/**
* Set the content of device's clipboard.
*
* @param contentType one of supported content types.
* @param base64Content base64-encoded content to be set.
*/
default void setClipboard(ClipboardContentType contentType, byte[] base64Content) {
final String extName = "mobile: setClipboard";
var args = Map.of(
"content", new String(requireNonNull(base64Content), StandardCharsets.UTF_8),
"contentType", contentType.name().toLowerCase(ROOT)
);
try {
CommandExecutionHelper.executeScript(assertExtensionExists(extName), extName, args);
} catch (UnsupportedCommandException e) {
// TODO: Remove the fallback
CommandExecutionHelper.execute(this, Map.entry(SET_CLIPBOARD, args));
}
}
/**
* Get the content of the clipboard.
*
* @param contentType one of supported content types.
* @return the actual content of the clipboard as base64-encoded string or an empty string if the clipboard is empty
*/
default String getClipboard(ClipboardContentType contentType) {
final String extName = "mobile: getClipboard";
var args = Map.of("contentType", contentType.name().toLowerCase(ROOT));
try {
return CommandExecutionHelper.executeScript(assertExtensionExists(extName), extName, args);
} catch (UnsupportedCommandException e) {
// TODO: Remove the fallback
return CommandExecutionHelper.execute(this, Map.entry(GET_CLIPBOARD, args));
}
}
/**
* Set the clipboard text.
*
* @param text The actual text to be set.
*/
default void setClipboardText(String text) {
setClipboard(ClipboardContentType.PLAINTEXT, Base64
.getMimeEncoder()
.encode(text.getBytes(StandardCharsets.UTF_8)));
}
/**
* Get the clipboard text.
*
* @return Either the text, which is stored in the clipboard or an empty string if the clipboard is empty
*/
default String getClipboardText() {
byte[] base64decodedBytes = Base64
.getMimeDecoder()
.decode(getClipboard(ClipboardContentType.PLAINTEXT));
return new String(base64decodedBytes, StandardCharsets.UTF_8);
}
}