ClipboardTest.java revision a1401311d1ab56c4ed0a474bd38c108f75cb0cd9
1// Copyright 2013 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5package org.chromium.content.browser;
6
7import android.content.ClipData;
8import android.content.ClipboardManager;
9import android.content.Context;
10import android.os.Build;
11import android.test.suitebuilder.annotation.LargeTest;
12import android.text.TextUtils;
13
14import org.chromium.base.ThreadUtils;
15import org.chromium.base.test.util.Feature;
16import org.chromium.base.test.util.UrlUtils;
17import org.chromium.content.browser.input.ImeAdapter;
18import org.chromium.content.browser.test.util.Criteria;
19import org.chromium.content.browser.test.util.CriteriaHelper;
20import org.chromium.content_shell_apk.ContentShellTestBase;
21
22import java.util.concurrent.Callable;
23
24/**
25 * Tests rich text clipboard functionality.
26 */
27public class ClipboardTest extends ContentShellTestBase {
28    private static final String TEST_PAGE_DATA_URL = UrlUtils.encodeHtmlDataUri(
29            "<html><body>Hello, <a href=\"http://www.example.com/\">world</a>, how <b> " +
30            "Chromium</b> doing today?</body></html>");
31
32    private static final String EXPECTED_TEXT_RESULT = "Hello, world, how Chromium doing today?";
33
34    // String to search for in the HTML representation on the clipboard.
35    private static final String EXPECTED_HTML_NEEDLE = "http://www.example.com/";
36
37    /**
38     * Tests that copying document fragments will put at least a plain-text representation
39     * of the contents on the clipboard. For Android JellyBean and higher, we also expect
40     * the HTML representation of the fragment to be available.
41     */
42    @LargeTest
43    @Feature({"Clipboard","TextInput"})
44    public void testCopyDocumentFragment() throws Throwable {
45        launchContentShellWithUrl(TEST_PAGE_DATA_URL);
46        assertTrue("Page failed to load", waitForActiveShellToBeDoneLoading());
47
48        final ClipboardManager clipboardManager = (ClipboardManager)
49                getActivity().getSystemService(Context.CLIPBOARD_SERVICE);
50        assertNotNull(clipboardManager);
51
52        // Clear the clipboard to make sure we start with a clean state.
53        clipboardManager.setPrimaryClip(ClipData.newPlainText(null, ""));
54        assertFalse(hasPrimaryClip(clipboardManager));
55
56        ImeAdapter adapter = getContentViewCore().getImeAdapterForTest();
57        selectAll(adapter);
58        copy(adapter);
59
60        // Waits until data has been made available on the Android clipboard.
61        assertTrue(CriteriaHelper.pollForCriteria(new Criteria() {
62            @Override
63            public boolean isSatisfied() {
64                return ThreadUtils.runOnUiThreadBlockingNoException(new Callable<Boolean>() {
65                    @Override
66                    public Boolean call() throws Exception {
67                        return hasPrimaryClip(clipboardManager);
68                    }
69                });
70            }
71        }));
72
73        // Verify that the data on the clipboard is what we expect it to be. For Android JB MR2
74        // and higher we expect HTML content, for other versions the plain-text representation.
75        final ClipData clip = clipboardManager.getPrimaryClip();
76        assertEquals(EXPECTED_TEXT_RESULT, clip.getItemAt(0).coerceToText(getActivity()));
77
78        // Android JellyBean and higher should have a HTML representation on the clipboard as well.
79        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
80            String htmlText = clip.getItemAt(0).getHtmlText();
81
82            assertNotNull(htmlText);
83            assertTrue(htmlText.contains(EXPECTED_HTML_NEEDLE));
84        }
85    }
86
87    private void copy(final ImeAdapter adapter) {
88        ThreadUtils.runOnUiThreadBlocking(new Runnable() {
89            @Override
90            public void run() {
91                adapter.copy();
92            }
93        });
94    }
95
96    private void selectAll(final ImeAdapter adapter) {
97        ThreadUtils.runOnUiThreadBlocking(new Runnable() {
98            @Override
99            public void run() {
100                adapter.selectAll();
101            }
102        });
103    }
104
105    // Returns whether there is a primary clip with content on the current clipboard.
106    private Boolean hasPrimaryClip(ClipboardManager clipboardManager) {
107        final ClipData clip = clipboardManager.getPrimaryClip();
108        if (clip != null && clip.getItemCount() > 0) {
109            return !TextUtils.isEmpty(clip.getItemAt(0).getText());
110        }
111
112        return false;
113    }
114}