|
1 | 1 | package io.flutter.plugin.platform; |
2 | 2 |
|
| 3 | +import static org.junit.Assert.assertEquals; |
3 | 4 | import static org.junit.Assert.assertFalse; |
| 5 | +import static org.junit.Assert.assertNotNull; |
| 6 | +import static org.junit.Assert.assertNull; |
4 | 7 | import static org.junit.Assert.assertTrue; |
5 | 8 | import static org.mockito.Mockito.mock; |
6 | 9 | import static org.mockito.Mockito.when; |
7 | 10 |
|
8 | 11 | import android.app.Activity; |
| 12 | +import android.content.ClipData; |
9 | 13 | import android.content.ClipboardManager; |
| 14 | +import android.content.ContentResolver; |
10 | 15 | import android.content.Context; |
| 16 | +import android.media.RingtoneManager; |
| 17 | +import android.net.Uri; |
11 | 18 | import android.view.View; |
12 | 19 | import android.view.Window; |
13 | 20 | import io.flutter.embedding.engine.systemchannels.PlatformChannel; |
| 21 | +import io.flutter.embedding.engine.systemchannels.PlatformChannel.ClipboardContentFormat; |
| 22 | +import java.io.ByteArrayInputStream; |
| 23 | +import java.io.IOException; |
| 24 | +import java.io.InputStream; |
14 | 25 | import org.junit.Test; |
15 | 26 | import org.junit.runner.RunWith; |
16 | 27 | import org.robolectric.RobolectricTestRunner; |
@@ -38,6 +49,45 @@ public void itIgnoresNewHapticEventsOnOldAndroidPlatforms() { |
38 | 49 | platformPlugin.vibrateHapticFeedback(PlatformChannel.HapticFeedbackType.SELECTION_CLICK); |
39 | 50 | } |
40 | 51 |
|
| 52 | + @Config(sdk = 29) |
| 53 | + @Test |
| 54 | + public void platformPlugin_getClipboardData() throws IOException { |
| 55 | + ClipboardManager clipboardManager = |
| 56 | + RuntimeEnvironment.application.getSystemService(ClipboardManager.class); |
| 57 | + |
| 58 | + View fakeDecorView = mock(View.class); |
| 59 | + Window fakeWindow = mock(Window.class); |
| 60 | + when(fakeWindow.getDecorView()).thenReturn(fakeDecorView); |
| 61 | + Activity fakeActivity = mock(Activity.class); |
| 62 | + when(fakeActivity.getWindow()).thenReturn(fakeWindow); |
| 63 | + when(fakeActivity.getSystemService(Context.CLIPBOARD_SERVICE)).thenReturn(clipboardManager); |
| 64 | + PlatformChannel fakePlatformChannel = mock(PlatformChannel.class); |
| 65 | + PlatformPlugin platformPlugin = new PlatformPlugin(fakeActivity, fakePlatformChannel); |
| 66 | + |
| 67 | + ClipboardContentFormat clipboardFormat = ClipboardContentFormat.PLAIN_TEXT; |
| 68 | + assertNull(platformPlugin.mPlatformMessageHandler.getClipboardData(clipboardFormat)); |
| 69 | + ClipData clip = ClipData.newPlainText("label", "Text"); |
| 70 | + clipboardManager.setPrimaryClip(clip); |
| 71 | + assertNotNull(platformPlugin.mPlatformMessageHandler.getClipboardData(clipboardFormat)); |
| 72 | + |
| 73 | + ContentResolver contentResolver = RuntimeEnvironment.application.getContentResolver(); |
| 74 | + Uri uri = Uri.parse("content://media/external_primary/images/media/"); |
| 75 | + clip = ClipData.newUri(contentResolver, "URI", uri); |
| 76 | + clipboardManager.setPrimaryClip(clip); |
| 77 | + assertNull(platformPlugin.mPlatformMessageHandler.getClipboardData(clipboardFormat)); |
| 78 | + |
| 79 | + uri = |
| 80 | + RingtoneManager.getActualDefaultRingtoneUri( |
| 81 | + RuntimeEnvironment.application.getApplicationContext(), RingtoneManager.TYPE_RINGTONE); |
| 82 | + clip = ClipData.newUri(contentResolver, "URI", uri); |
| 83 | + clipboardManager.setPrimaryClip(clip); |
| 84 | + String uriData = |
| 85 | + platformPlugin.mPlatformMessageHandler.getClipboardData(clipboardFormat).toString(); |
| 86 | + InputStream uriInputStream = contentResolver.openInputStream(uri); |
| 87 | + InputStream dataInputStream = new ByteArrayInputStream(uriData.getBytes()); |
| 88 | + assertEquals(dataInputStream.read(), uriInputStream.read()); |
| 89 | + } |
| 90 | + |
41 | 91 | @Test |
42 | 92 | public void platformPlugin_hasStrings() { |
43 | 93 | ClipboardManager clipboardManager = |
|
0 commit comments