ClipboardTest.java revision 116680a4aac90f2aa7413d9095a592090648e557
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    @Override
38    protected void setUp() throws Exception {
39        super.setUp();
40        launchContentShellWithUrl(TEST_PAGE_DATA_URL);
41        assertTrue("Page failed to load", waitForActiveShellToBeDoneLoading());
42    }
43
44    /**
45     * Tests that copying document fragments will put at least a plain-text representation
46     * of the contents on the clipboard. For Android JellyBean and higher, we also expect
47     * the HTML representation of the fragment to be available.
48     */
49    @LargeTest
50    @Feature({"Clipboard","TextInput"})
51    @RerunWithUpdatedContainerView
52    public void testCopyDocumentFragment() throws Throwable {
53        final ClipboardManager clipboardManager = (ClipboardManager)
54                getActivity().getSystemService(Context.CLIPBOARD_SERVICE);
55        assertNotNull(clipboardManager);
56
57        // Clear the clipboard to make sure we start with a clean state.
58        clipboardManager.setPrimaryClip(ClipData.newPlainText(null, ""));
59        assertFalse(hasPrimaryClip(clipboardManager));
60
61        ImeAdapter adapter = getContentViewCore().getImeAdapterForTest();
62        selectAll(adapter);
63        copy(adapter);
64
65        // Waits until data has been made available on the Android clipboard.
66        assertTrue(CriteriaHelper.pollForCriteria(new Criteria() {
67            @Override
68            public boolean isSatisfied() {
69                return ThreadUtils.runOnUiThreadBlockingNoException(new Callable<Boolean>() {
70                    @Override
71                    public Boolean call() throws Exception {
72                        return hasPrimaryClip(clipboardManager);
73                    }
74                });
75            }
76        }));
77
78        // Verify that the data on the clipboard is what we expect it to be. For Android JB MR2
79        // and higher we expect HTML content, for other versions the plain-text representation.
80        final ClipData clip = clipboardManager.getPrimaryClip();
81        assertEquals(EXPECTED_TEXT_RESULT, clip.getItemAt(0).coerceToText(getActivity()));
82
83        // Android JellyBean and higher should have a HTML representation on the clipboard as well.
84        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
85            String htmlText = clip.getItemAt(0).getHtmlText();
86
87            assertNotNull(htmlText);
88            assertTrue(htmlText.contains(EXPECTED_HTML_NEEDLE));
89        }
90    }
91
92    private void copy(final ImeAdapter adapter) {
93        ThreadUtils.runOnUiThreadBlocking(new Runnable() {
94            @Override
95            public void run() {
96                adapter.copy();
97            }
98        });
99    }
100
101    private void selectAll(final ImeAdapter adapter) {
102        ThreadUtils.runOnUiThreadBlocking(new Runnable() {
103            @Override
104            public void run() {
105                adapter.selectAll();
106            }
107        });
108    }
109
110    // Returns whether there is a primary clip with content on the current clipboard.
111    private Boolean hasPrimaryClip(ClipboardManager clipboardManager) {
112        final ClipData clip = clipboardManager.getPrimaryClip();
113        if (clip != null && clip.getItemCount() > 0) {
114            return !TextUtils.isEmpty(clip.getItemAt(0).getText());
115        }
116
117        return false;
118    }
119}