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