ClipboardTest.java revision a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7
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
24public class ClipboardTest extends ContentShellTestBase {
25    private static final String TEST_PAGE_DATA_URL = UrlUtils.encodeHtmlDataUri(
26            "<html><body>Hello, <a href=\"http://www.example.com/\">world</a>, how <b> " +
27            "Chromium</b> doing today?</body></html>");
28
29    private static final String EXPECTED_TEXT_RESULT = "Hello, world, how Chromium doing today?";
30
31    // String to search for in the HTML representation on the clipboard.
32    private static final String EXPECTED_HTML_NEEDLE = "http://www.example.com/";
33
34    /**
35     * Tests that copying document fragments will put at least a plain-text representation
36     * of the contents on the clipboard. For Android JellyBean and higher, we also expect
37     * the HTML representation of the fragment to be available.
38     */
39    @LargeTest
40    @Feature({"Clipboard","TextInput"})
41    public void testCopyDocumentFragment() throws Throwable {
42        launchContentShellWithUrl(TEST_PAGE_DATA_URL);
43        assertTrue("Page failed to load", waitForActiveShellToBeDoneLoading());
44
45        final ClipboardManager clipboardManager = (ClipboardManager)
46                getActivity().getSystemService(Context.CLIPBOARD_SERVICE);
47        assertNotNull(clipboardManager);
48
49        // Clear the clipboard to make sure we start with a clean state.
50        clipboardManager.setPrimaryClip(ClipData.newPlainText(null, ""));
51        assertFalse(hasPrimaryClip(clipboardManager));
52
53        getImeAdapter().selectAll();
54        getImeAdapter().copy();
55
56        // Waits until data has been made available on the Android clipboard.
57        assertTrue(CriteriaHelper.pollForCriteria(new Criteria() {
58            @Override
59            public boolean isSatisfied() {
60                return ThreadUtils.runOnUiThreadBlockingNoException(new Callable<Boolean>() {
61                    @Override
62                    public Boolean call() throws Exception {
63                        return hasPrimaryClip(clipboardManager);
64                    }
65                });
66            }
67        }));
68
69        // Verify that the data on the clipboard is what we expect it to be. For Android JB MR2
70        // and higher we expect HTML content, for other versions the plain-text representation.
71        final ClipData clip = clipboardManager.getPrimaryClip();
72        assertEquals(EXPECTED_TEXT_RESULT, clip.getItemAt(0).coerceToText(getActivity()));
73
74        // Android JellyBean and higher should have a HTML representation on the clipboard as well.
75        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
76            String htmlText = clip.getItemAt(0).getHtmlText();
77
78            assertNotNull(htmlText);
79            assertTrue(htmlText.contains(EXPECTED_HTML_NEEDLE));
80        }
81    }
82
83    private ImeAdapter getImeAdapter() {
84        return getContentViewCore().getImeAdapterForTest();
85    }
86
87    // Returns whether there is a primary clip with content on the current clipboard.
88    private Boolean hasPrimaryClip(ClipboardManager clipboardManager) {
89        final ClipData clip = clipboardManager.getPrimaryClip();
90        if (clip != null && clip.getItemCount() > 0) {
91            return !TextUtils.isEmpty(clip.getItemAt(0).getText());
92        }
93
94        return false;
95    }
96}