Fix NPE in PlatformPlugin.getClipboardData() (flutter/engine#21189)
This commit is contained in:
@@ -280,6 +280,9 @@ public class PlatformPlugin {
|
||||
private CharSequence getClipboardData(PlatformChannel.ClipboardContentFormat format) {
|
||||
ClipboardManager clipboard =
|
||||
(ClipboardManager) activity.getSystemService(Context.CLIPBOARD_SERVICE);
|
||||
|
||||
if (!clipboard.hasPrimaryClip()) return null;
|
||||
|
||||
ClipData clip = clipboard.getPrimaryClip();
|
||||
if (clip == null) return null;
|
||||
|
||||
|
||||
@@ -1,16 +1,20 @@
|
||||
package io.flutter.plugin.platform;
|
||||
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.ClipData;
|
||||
import android.content.ClipboardManager;
|
||||
import android.content.Context;
|
||||
import android.view.View;
|
||||
import android.view.Window;
|
||||
import io.flutter.embedding.engine.systemchannels.PlatformChannel;
|
||||
import io.flutter.embedding.engine.systemchannels.PlatformChannel.ClipboardContentFormat;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.robolectric.RobolectricTestRunner;
|
||||
@@ -38,6 +42,27 @@ public class PlatformPluginTest {
|
||||
platformPlugin.vibrateHapticFeedback(PlatformChannel.HapticFeedbackType.SELECTION_CLICK);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void platformPlugin_getClipboardData() {
|
||||
ClipboardManager clipboardManager =
|
||||
RuntimeEnvironment.application.getSystemService(ClipboardManager.class);
|
||||
|
||||
View fakeDecorView = mock(View.class);
|
||||
Window fakeWindow = mock(Window.class);
|
||||
when(fakeWindow.getDecorView()).thenReturn(fakeDecorView);
|
||||
Activity fakeActivity = mock(Activity.class);
|
||||
when(fakeActivity.getWindow()).thenReturn(fakeWindow);
|
||||
when(fakeActivity.getSystemService(Context.CLIPBOARD_SERVICE)).thenReturn(clipboardManager);
|
||||
PlatformChannel fakePlatformChannel = mock(PlatformChannel.class);
|
||||
PlatformPlugin platformPlugin = new PlatformPlugin(fakeActivity, fakePlatformChannel);
|
||||
|
||||
ClipboardContentFormat clipboardFormat = ClipboardContentFormat.PLAIN_TEXT;
|
||||
assertNull(platformPlugin.mPlatformMessageHandler.getClipboardData(clipboardFormat));
|
||||
ClipData clip = ClipData.newPlainText("label", "Text");
|
||||
clipboardManager.setPrimaryClip(clip);
|
||||
assertNotNull(platformPlugin.mPlatformMessageHandler.getClipboardData(clipboardFormat));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void platformPlugin_hasStrings() {
|
||||
ClipboardManager clipboardManager =
|
||||
@@ -52,10 +77,12 @@ public class PlatformPluginTest {
|
||||
PlatformChannel fakePlatformChannel = mock(PlatformChannel.class);
|
||||
PlatformPlugin platformPlugin = new PlatformPlugin(fakeActivity, fakePlatformChannel);
|
||||
|
||||
clipboardManager.setText("iamastring");
|
||||
ClipData clip = ClipData.newPlainText("label", "Text");
|
||||
clipboardManager.setPrimaryClip(clip);
|
||||
assertTrue(platformPlugin.mPlatformMessageHandler.clipboardHasStrings());
|
||||
|
||||
clipboardManager.setText("");
|
||||
clip = ClipData.newPlainText("", "");
|
||||
clipboardManager.setPrimaryClip(clip);
|
||||
assertFalse(platformPlugin.mPlatformMessageHandler.clipboardHasStrings());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user