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