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 2 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 @@ -155,6 +155,14 @@ public void onMethodCall(@NonNull MethodCall call, @NonNull MethodChannel.Result
result.success(null);
break;
}
case "Clipboard.hasStrings":
{
boolean hasStrings = platformMessageHandler.clipboardHasStrings();
JSONObject response = new JSONObject();
response.put("value", hasStrings);
result.success(response);
break;
}
default:
result.notImplemented();
break;
Expand Down Expand Up @@ -426,6 +434,11 @@ public interface PlatformMessageHandler {
* {@code text}.
*/
void setClipboardData(@NonNull String text);

/** The Flutter application would like to know if the clipboard currently contains a string
* that can be pasted.
*/
boolean clipboardHasStrings();
}

/** Types of sounds the Android OS can play on behalf of an application. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ public CharSequence getClipboardData(
public void setClipboardData(@NonNull String text) {
PlatformPlugin.this.setClipboardData(text);
}

@Override
public boolean clipboardHasStrings() {
CharSequence data = PlatformPlugin.this.getClipboardData(PlatformChannel.ClipboardContentFormat.PLAIN_TEXT);
return data != null && data.length() > 0;
}
};

public PlatformPlugin(Activity activity, PlatformChannel platformChannel) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
package io.flutter.plugin.platform;

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.ClipboardManager;
import android.view.View;
import android.view.Window;
import io.flutter.embedding.engine.systemchannels.PlatformChannel;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;

@Config(manifest = Config.NONE)
Expand All @@ -32,4 +35,24 @@ public void itIgnoresNewHapticEventsOnOldAndroidPlatforms() {
// SELECTION_CLICK haptic response is only available on "LOLLIPOP" (21) and later.
platformPlugin.vibrateHapticFeedback(PlatformChannel.HapticFeedbackType.SELECTION_CLICK);
}

@Test
public void platformPlugin_hasStrings() {
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);
PlatformChannel fakePlatformChannel = mock(PlatformChannel.class);
PlatformPlugin platformPlugin = new PlatformPlugin(fakeActivity, fakePlatformChannel);

ClipboardManager clipboardManager =
RuntimeEnvironment.application.getSystemService(ClipboardManager.class);
clipboardManager.setText("iamastring");

// TODO(justinmc): Can't do this because mPlatformMessageHandler is private.
// How can I send a message to the platform plugin?
//boolean hasStrings = platformPlugin.mPlatformMessageHandler.clipboardHasStrings();
//assertTrue(hasStrings);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@gaaclarke You helped me test this same thing on iOS, do you know how I can do it on Android? I'm sure there's a way for me to just send the Clipboard.hasStrings message, but I'm struggling to figure it out.

Copy link
Member

Choose a reason for hiding this comment

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

I'd make a test against PlatformChannel that has a mock platformMessageHandler and you can call into onMethodCall. As for testing PlatformPlugin, can't you just call setClipboardData on it then call clipboardHasStrings to test clipboardHasStrings? Once you have those 2 tests written you'll have complete test coverage of your PR (in isolation as unit tests, not integration tests).

}
}