This source file includes following definitions.
- Feature
- testCopyDocumentFragment
- copy
- selectAll
- hasPrimaryClip
package org.chromium.content.browser;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.os.Build;
import android.test.suitebuilder.annotation.LargeTest;
import android.text.TextUtils;
import org.chromium.base.ThreadUtils;
import org.chromium.base.test.util.Feature;
import org.chromium.base.test.util.UrlUtils;
import org.chromium.content.browser.input.ImeAdapter;
import org.chromium.content.browser.test.util.Criteria;
import org.chromium.content.browser.test.util.CriteriaHelper;
import org.chromium.content_shell_apk.ContentShellTestBase;
import java.util.concurrent.Callable;
public class ClipboardTest extends ContentShellTestBase {
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?";
private static final String EXPECTED_HTML_NEEDLE = "http://www.example.com/";
@LargeTest
@Feature({"Clipboard","TextInput"})
public void testCopyDocumentFragment() throws Throwable {
launchContentShellWithUrl(TEST_PAGE_DATA_URL);
assertTrue("Page failed to load", waitForActiveShellToBeDoneLoading());
final ClipboardManager clipboardManager = (ClipboardManager)
getActivity().getSystemService(Context.CLIPBOARD_SERVICE);
assertNotNull(clipboardManager);
clipboardManager.setPrimaryClip(ClipData.newPlainText(null, ""));
assertFalse(hasPrimaryClip(clipboardManager));
ImeAdapter adapter = getContentViewCore().getImeAdapterForTest();
selectAll(adapter);
copy(adapter);
assertTrue(CriteriaHelper.pollForCriteria(new Criteria() {
@Override
public boolean isSatisfied() {
return ThreadUtils.runOnUiThreadBlockingNoException(new Callable<Boolean>() {
@Override
public Boolean call() throws Exception {
return hasPrimaryClip(clipboardManager);
}
});
}
}));
final ClipData clip = clipboardManager.getPrimaryClip();
assertEquals(EXPECTED_TEXT_RESULT, clip.getItemAt(0).coerceToText(getActivity()));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
String htmlText = clip.getItemAt(0).getHtmlText();
assertNotNull(htmlText);
assertTrue(htmlText.contains(EXPECTED_HTML_NEEDLE));
}
}
private void copy(final ImeAdapter adapter) {
ThreadUtils.runOnUiThreadBlocking(new Runnable() {
@Override
public void run() {
adapter.copy();
}
});
}
private void selectAll(final ImeAdapter adapter) {
ThreadUtils.runOnUiThreadBlocking(new Runnable() {
@Override
public void run() {
adapter.selectAll();
}
});
}
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;
}