/
ClipboardTest.java
136 lines (115 loc) · 5.18 KB
/
ClipboardTest.java
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package org.chromium.content.browser;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.support.test.filters.LargeTest;
import android.text.TextUtils;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.chromium.base.ThreadUtils;
import org.chromium.base.test.BaseJUnit4ClassRunner;
import org.chromium.base.test.util.DisabledTest;
import org.chromium.base.test.util.Feature;
import org.chromium.base.test.util.UrlUtils;
import org.chromium.content.browser.webcontents.WebContentsImpl;
import org.chromium.content_public.browser.test.util.Criteria;
import org.chromium.content_public.browser.test.util.CriteriaHelper;
import org.chromium.content_shell_apk.ContentShellActivityTestRule;
import org.chromium.content_shell_apk.ContentShellActivityTestRule.RerunWithUpdatedContainerView;
import java.util.concurrent.Callable;
/**
* Tests rich text clipboard functionality.
*/
@RunWith(BaseJUnit4ClassRunner.class)
public class ClipboardTest {
@Rule
public ContentShellActivityTestRule mActivityTestRule = new ContentShellActivityTestRule();
private static final String TEST_PAGE_DATA_URL = UrlUtils.encodeHtmlDataUri(
"<html><body>Hello, <a href=\"http://www.example.com/\">world</a>, how <b> "
+ "Chromium</b> doing today?</body></html>");
private static final String EXPECTED_TEXT_RESULT = "Hello, world, how Chromium doing today?";
// String to search for in the HTML representation on the clipboard.
private static final String EXPECTED_HTML_NEEDLE = "http://www.example.com/";
@Before
public void setUp() throws Exception {
mActivityTestRule.launchContentShellWithUrl(TEST_PAGE_DATA_URL);
mActivityTestRule.waitForActiveShellToBeDoneLoading();
}
/**
* Tests that copying document fragments will put at least a plain-text representation
* of the contents on the clipboard. For Android JellyBean and higher, we also expect
* the HTML representation of the fragment to be available.
*/
@Test
@LargeTest
@Feature({"Clipboard", "TextInput"})
@RerunWithUpdatedContainerView
@DisabledTest(message = "https://crbug.com/791021")
public void testCopyDocumentFragment() throws Throwable {
ClipboardManager clipboardManager =
ThreadUtils.runOnUiThreadBlockingNoException(new Callable<ClipboardManager>() {
@Override
public ClipboardManager call() {
return (ClipboardManager) mActivityTestRule.getActivity().getSystemService(
Context.CLIPBOARD_SERVICE);
}
});
Assert.assertNotNull(clipboardManager);
// Clear the clipboard to make sure we start with a clean state.
clipboardManager.setPrimaryClip(ClipData.newPlainText(null, ""));
Assert.assertFalse(hasPrimaryClip(clipboardManager));
final WebContentsImpl webContents = (WebContentsImpl) mActivityTestRule.getWebContents();
selectAll(webContents);
copy(webContents);
// Waits until data has been made available on the Android clipboard.
CriteriaHelper.pollUiThread(new Criteria() {
@Override
public boolean isSatisfied() {
return hasPrimaryClip(clipboardManager);
}
});
// Verify that the data on the clipboard is what we expect it to be. For Android JB MR2
// and higher we expect HTML content, for other versions the plain-text representation.
ThreadUtils.runOnUiThreadBlocking(new Runnable() {
@Override
public void run() {
final ClipData clip = clipboardManager.getPrimaryClip();
Assert.assertEquals(EXPECTED_TEXT_RESULT,
clip.getItemAt(0).coerceToText(mActivityTestRule.getActivity()));
String htmlText = clip.getItemAt(0).getHtmlText();
Assert.assertNotNull(htmlText);
Assert.assertTrue(htmlText.contains(EXPECTED_HTML_NEEDLE));
}
});
}
private void copy(final WebContentsImpl webContents) {
ThreadUtils.runOnUiThreadBlocking(new Runnable() {
@Override
public void run() {
webContents.copy();
}
});
}
private void selectAll(final WebContentsImpl webContents) {
ThreadUtils.runOnUiThreadBlocking(new Runnable() {
@Override
public void run() {
webContents.selectAll();
}
});
}
// Returns whether there is a primary clip with content on the current clipboard.
private Boolean hasPrimaryClip(ClipboardManager clipboardManager) {
final ClipData clip = clipboardManager.getPrimaryClip();
if (clip != null && clip.getItemCount() > 0) {
return !TextUtils.isEmpty(clip.getItemAt(0).getText());
}
return false;
}
}