OLD | NEW |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.ui.base; | 5 package org.chromium.ui.base; |
6 | 6 |
| 7 import android.content.ClipData; |
| 8 import android.content.Intent; |
| 9 import android.text.SpannableString; |
| 10 import android.text.style.RelativeSizeSpan; |
| 11 |
7 import static org.junit.Assert.assertEquals; | 12 import static org.junit.Assert.assertEquals; |
| 13 import static org.junit.Assert.assertNotNull; |
| 14 import static org.junit.Assert.assertNull; |
8 import static org.junit.Assert.assertTrue; | 15 import static org.junit.Assert.assertTrue; |
9 | 16 |
10 import org.junit.AfterClass; | 17 import org.junit.AfterClass; |
11 import org.junit.BeforeClass; | 18 import org.junit.BeforeClass; |
12 import org.junit.Test; | 19 import org.junit.Test; |
13 import org.junit.runner.RunWith; | 20 import org.junit.runner.RunWith; |
14 import org.robolectric.RuntimeEnvironment; | 21 import org.robolectric.RuntimeEnvironment; |
15 import org.robolectric.annotation.Config; | 22 import org.robolectric.annotation.Config; |
16 | 23 |
17 import org.chromium.base.ContextUtils; | 24 import org.chromium.base.ContextUtils; |
18 import org.chromium.base.metrics.RecordHistogram; | 25 import org.chromium.base.metrics.RecordHistogram; |
19 import org.chromium.base.metrics.RecordUserAction; | 26 import org.chromium.base.metrics.RecordUserAction; |
20 import org.chromium.testing.local.LocalRobolectricTestRunner; | 27 import org.chromium.testing.local.LocalRobolectricTestRunner; |
21 | 28 |
22 /** | 29 /** |
23 * Tests logic in the Clipboard class. | 30 * Tests logic in the Clipboard class. |
24 */ | 31 */ |
25 @RunWith(LocalRobolectricTestRunner.class) | 32 @RunWith(LocalRobolectricTestRunner.class) |
26 @Config(manifest = Config.NONE) | 33 @Config(manifest = Config.NONE) |
27 public class ClipboardTest { | 34 public class ClipboardTest { |
| 35 private static final String PLAIN_TEXT = "plain"; |
| 36 private static final String HTML_TEXT = "<span style=\"color: red;\">HTML</s
pan>"; |
| 37 |
28 @BeforeClass | 38 @BeforeClass |
29 public static void beforeClass() { | 39 public static void beforeClass() { |
30 RecordHistogram.setDisabledForTests(true); | 40 RecordHistogram.setDisabledForTests(true); |
31 RecordUserAction.setDisabledForTests(true); | 41 RecordUserAction.setDisabledForTests(true); |
32 } | 42 } |
33 | 43 |
34 @AfterClass | 44 @AfterClass |
35 public static void afterClass() { | 45 public static void afterClass() { |
36 RecordHistogram.setDisabledForTests(false); | 46 RecordHistogram.setDisabledForTests(false); |
37 RecordUserAction.setDisabledForTests(false); | 47 RecordUserAction.setDisabledForTests(false); |
38 } | 48 } |
39 | 49 |
40 @Test | 50 @Test |
41 public void testGetClipboardContentChangeTimeInMillis() { | 51 public void testGetClipboardContentChangeTimeInMillis() { |
42 ContextUtils.initApplicationContext(RuntimeEnvironment.application); | 52 ContextUtils.initApplicationContextForTests(RuntimeEnvironment.applicati
on); |
43 // Upon launch, the clipboard should be at start of epoch, i.e., ancient
. | 53 // Upon launch, the clipboard should be at start of epoch, i.e., ancient
. |
44 Clipboard clipboard = Clipboard.getInstance(); | 54 Clipboard clipboard = Clipboard.getInstance(); |
45 assertEquals(0, clipboard.getClipboardContentChangeTimeInMillis()); | 55 assertEquals(0, clipboard.getClipboardContentChangeTimeInMillis()); |
46 // After updating the clipboard, it should have a new time. | 56 // After updating the clipboard, it should have a new time. |
47 clipboard.onPrimaryClipChanged(); | 57 clipboard.onPrimaryClipChanged(); |
48 assertTrue(clipboard.getClipboardContentChangeTimeInMillis() > 0); | 58 assertTrue(clipboard.getClipboardContentChangeTimeInMillis() > 0); |
49 } | 59 } |
| 60 |
| 61 @Test |
| 62 public void testGetHTMLTextAndGetCoercedText() { |
| 63 ContextUtils.initApplicationContextForTests(RuntimeEnvironment.applicati
on); |
| 64 Clipboard clipboard = Clipboard.getInstance(); |
| 65 |
| 66 // HTML text |
| 67 ClipData html = ClipData.newHtmlText("html", PLAIN_TEXT, HTML_TEXT); |
| 68 clipboard.setPrimaryClipNoException(html); |
| 69 assertEquals(PLAIN_TEXT, clipboard.getCoercedText()); |
| 70 assertEquals(HTML_TEXT, clipboard.getHTMLText()); |
| 71 |
| 72 // Plain text without span |
| 73 ClipData plainTextNoSpan = ClipData.newPlainText("plain", PLAIN_TEXT); |
| 74 clipboard.setPrimaryClipNoException(plainTextNoSpan); |
| 75 assertEquals(PLAIN_TEXT, clipboard.getCoercedText()); |
| 76 assertNull(clipboard.getHTMLText()); |
| 77 |
| 78 // Plain text with span |
| 79 SpannableString spanned = new SpannableString(PLAIN_TEXT); |
| 80 spanned.setSpan(new RelativeSizeSpan(2f), 0, 5, 0); |
| 81 ClipData plainTextSpan = ClipData.newPlainText("plain", spanned); |
| 82 clipboard.setPrimaryClipNoException(plainTextSpan); |
| 83 assertEquals(PLAIN_TEXT, clipboard.getCoercedText()); |
| 84 assertNotNull(clipboard.getHTMLText()); |
| 85 |
| 86 // Intent |
| 87 Intent intent = new Intent(Intent.ACTION_GET_CONTENT); |
| 88 intent.addCategory(Intent.CATEGORY_OPENABLE); |
| 89 intent.setType("*/*"); |
| 90 ClipData intentClip = ClipData.newIntent("intent", intent); |
| 91 clipboard.setPrimaryClipNoException(intentClip); |
| 92 assertNotNull(clipboard.getCoercedText()); |
| 93 assertNull(clipboard.getHTMLText()); |
| 94 } |
50 } | 95 } |
OLD | NEW |