From 489c648b42a593f6b5e9957252063ba6baf7d8ac Mon Sep 17 00:00:00 2001 From: Hamdi Kahloun <32666446+hamdikahloun@users.noreply.github.com> Date: Wed, 16 Sep 2020 01:00:57 +0100 Subject: [PATCH] Fix NPE in PlatformPlugin.getClipboardData() (flutter/engine#21189) --- .../plugin/platform/PlatformPlugin.java | 3 ++ .../plugin/platform/PlatformPluginTest.java | 31 +++++++++++++++++-- 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/engine/src/flutter/shell/platform/android/io/flutter/plugin/platform/PlatformPlugin.java b/engine/src/flutter/shell/platform/android/io/flutter/plugin/platform/PlatformPlugin.java index cc87aa2eaf..7237576790 100644 --- a/engine/src/flutter/shell/platform/android/io/flutter/plugin/platform/PlatformPlugin.java +++ b/engine/src/flutter/shell/platform/android/io/flutter/plugin/platform/PlatformPlugin.java @@ -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; diff --git a/engine/src/flutter/shell/platform/android/test/io/flutter/plugin/platform/PlatformPluginTest.java b/engine/src/flutter/shell/platform/android/test/io/flutter/plugin/platform/PlatformPluginTest.java index 355ec4e9ea..495b332678 100644 --- a/engine/src/flutter/shell/platform/android/test/io/flutter/plugin/platform/PlatformPluginTest.java +++ b/engine/src/flutter/shell/platform/android/test/io/flutter/plugin/platform/PlatformPluginTest.java @@ -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()); } }