Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import android.content.ClipDescription;
import android.content.ClipboardManager;
import android.content.Context;
import android.net.Uri;
import android.os.Build;
import android.view.HapticFeedbackConstants;
import android.view.SoundEffectConstants;
Expand Down Expand Up @@ -517,8 +518,39 @@ private CharSequence getClipboardData(PlatformChannel.ClipboardContentFormat for
if (clip == null) return null;
if (format == null || format == PlatformChannel.ClipboardContentFormat.PLAIN_TEXT) {
ClipData.Item item = clip.getItemAt(0);
if (item.getUri() != null)
activity.getContentResolver().openTypedAssetFileDescriptor(item.getUri(), "text/*", null);
// First, try getting clipboard data as text; no further processing
// required if so.
CharSequence itemText = item.getText();

if (itemText == null) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nonblocking nit: consider extracting uri logic into a method or methods.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved logic trying to extract text from URI to a new method!

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Undid this due to some recent changes here that actually made the new method more confusing.

// Clipboard data does not contain text, so check whether or not it
// contains a URI to extract text from.
Uri itemUri = item.getUri();

if (itemUri == null) {
Log.w(
TAG, "Clipboard item contained no textual content nor a URI to retrieve it from.");
return null;
}

// Clipboard data contains a URI to potentially check extract text
// from, but first ensure its scheme is content in order to do so.
String uriScheme = itemUri.getScheme();

if (!uriScheme.equals("content")) {
Log.w(
TAG,
"Clipboard item contains a Uri with scheme '" + uriScheme + "'that is unhandled.");
return null;
}

// Try extracting text from content URI; FileNotFoundException will be
// thrown if text cannot be extracted from the URI.
activity.getContentResolver().openTypedAssetFileDescriptor(itemUri, "text/*", null);
}

// Safely return clipboard data coerced into text; will return either
// itemText or text retrieved from its URI.
return item.coerceToText(activity);
}
} catch (SecurityException e) {
Expand All @@ -530,6 +562,7 @@ private CharSequence getClipboardData(PlatformChannel.ClipboardContentFormat for
e);
return null;
} catch (FileNotFoundException e) {
Log.w(TAG, "Clipboard text was unable to be received from content URI.");
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.anyBoolean;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
Expand All @@ -23,6 +24,7 @@
import android.content.ClipboardManager;
import android.content.ContentResolver;
import android.content.Context;
import android.content.res.AssetFileDescriptor;
import android.net.Uri;
import android.os.Build;
import android.view.View;
Expand Down Expand Up @@ -70,9 +72,8 @@ public void itIgnoresNewHapticEventsOnOldAndroidPlatforms() {
platformPlugin.vibrateHapticFeedback(PlatformChannel.HapticFeedbackType.SELECTION_CLICK);
}

@Config(sdk = Build.VERSION_CODES.Q)
@Test
public void platformPlugin_getClipboardData() throws IOException {
public void platformPlugin_getClipboardDataIsNonNullWhenPlainTextCopied() throws IOException {
ClipboardManager clipboardManager = ctx.getSystemService(ClipboardManager.class);

View fakeDecorView = mock(View.class);
Expand All @@ -85,15 +86,119 @@ public void platformPlugin_getClipboardData() throws IOException {
PlatformPlugin platformPlugin = new PlatformPlugin(fakeActivity, fakePlatformChannel);

ClipboardContentFormat clipboardFormat = ClipboardContentFormat.PLAIN_TEXT;
assertNull(platformPlugin.mPlatformMessageHandler.getClipboardData(clipboardFormat));
ClipData clip = ClipData.newPlainText("label", "Text");

assertNull(platformPlugin.mPlatformMessageHandler.getClipboardData(clipboardFormat));
clipboardManager.setPrimaryClip(clip);
assertNotNull(platformPlugin.mPlatformMessageHandler.getClipboardData(clipboardFormat));
}

@Test
public void platformPlugin_getClipboardDataIsNonNullWhenContentUriWithTextProvided()
throws IOException {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can any of this setup logic be extracted into a logical helper method to share between tests?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved overlapping logic to setup methods!

ClipboardManager clipboardManager = ctx.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);

ContentResolver contentResolver = mock(ContentResolver.class);
ClipboardContentFormat clipboardFormat = ClipboardContentFormat.PLAIN_TEXT;
ClipData.Item mockItem = mock(ClipData.Item.class);
Uri mockUri = mock(Uri.class);

when(fakeActivity.getContentResolver()).thenReturn(contentResolver);
when(mockUri.getScheme()).thenReturn("content");
when(mockItem.getText()).thenReturn(null);
when(mockItem.getUri()).thenReturn(mockUri);
when(contentResolver.openTypedAssetFileDescriptor(any(Uri.class), any(), any()))
.thenReturn(mock(AssetFileDescriptor.class));
when(mockItem.coerceToText(fakeActivity)).thenReturn("something non-null");

ClipData clip = new ClipData("label", new String[0], mockItem);

assertNull(platformPlugin.mPlatformMessageHandler.getClipboardData(clipboardFormat));
clipboardManager.setPrimaryClip(clip);
assertNotNull(platformPlugin.mPlatformMessageHandler.getClipboardData(clipboardFormat));
}

@Test
public void platformPlugin_getClipboardDataIsNullWhenContentUriProvidedContainsNoText()
throws IOException {
ClipboardManager clipboardManager = ctx.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);

ContentResolver contentResolver = ctx.getContentResolver();
ClipboardContentFormat clipboardFormat = ClipboardContentFormat.PLAIN_TEXT;

when(fakeActivity.getContentResolver()).thenReturn(contentResolver);

Uri uri = Uri.parse("content://media/external_primary/images/media/");
clip = ClipData.newUri(contentResolver, "URI", uri);
ClipData clip = ClipData.newUri(contentResolver, "URI", uri);

clipboardManager.setPrimaryClip(clip);
assertNull(platformPlugin.mPlatformMessageHandler.getClipboardData(clipboardFormat));
}

@Test
public void platformPlugin_getClipboardDataIsNullWhenNonContentUriProvided() throws IOException {
ClipboardManager clipboardManager = ctx.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);

ContentResolver contentResolver = ctx.getContentResolver();
ClipboardContentFormat clipboardFormat = ClipboardContentFormat.PLAIN_TEXT;

when(fakeActivity.getContentResolver()).thenReturn(contentResolver);

Uri uri = Uri.parse("file:///");
ClipData clip = ClipData.newUri(contentResolver, "URI", uri);

clipboardManager.setPrimaryClip(clip);
assertNull(platformPlugin.mPlatformMessageHandler.getClipboardData(clipboardFormat));
}

@Test
public void platformPlugin_getClipboardDataIsNullWhenItemHasNoTextNorUri() throws IOException {
ClipboardManager clipboardManager = ctx.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;
ClipData.Item mockItem = mock(ClipData.Item.class);

when(mockItem.getText()).thenReturn(null);
when(mockItem.getUri()).thenReturn(null);

ClipData clip = new ClipData("label", new String[0], mockItem);

clipboardManager.setPrimaryClip(clip);
assertNull(platformPlugin.mPlatformMessageHandler.getClipboardData(clipboardFormat));
}
Expand Down