mirror of
https://github.com/flutter/flutter.git
synced 2026-02-20 02:29:02 +08:00
hasStrings on Android (flutter/engine#20393)
hasStrings message for checking for pasteable clipboard contents without actually reading them, for iOS14 clipboard alerts.
This commit is contained in:
parent
ded8cf713d
commit
1fec9ca767
@ -435,6 +435,7 @@ action("robolectric_tests") {
|
||||
"test/io/flutter/embedding/engine/loader/ApplicationInfoLoaderTest.java",
|
||||
"test/io/flutter/embedding/engine/plugins/shim/ShimPluginRegistryTest.java",
|
||||
"test/io/flutter/embedding/engine/renderer/FlutterRendererTest.java",
|
||||
"test/io/flutter/embedding/engine/systemchannels/PlatformChannelTest.java",
|
||||
"test/io/flutter/embedding/engine/systemchannels/RestorationChannelTest.java",
|
||||
"test/io/flutter/external/FlutterLaunchTests.java",
|
||||
"test/io/flutter/plugin/common/StandardMessageCodecTest.java",
|
||||
|
||||
@ -30,7 +30,7 @@ public class PlatformChannel {
|
||||
@Nullable private PlatformMessageHandler platformMessageHandler;
|
||||
|
||||
@NonNull @VisibleForTesting
|
||||
protected final MethodChannel.MethodCallHandler parsingMethodCallHandler =
|
||||
final MethodChannel.MethodCallHandler parsingMethodCallHandler =
|
||||
new MethodChannel.MethodCallHandler() {
|
||||
@Override
|
||||
public void onMethodCall(@NonNull MethodCall call, @NonNull MethodChannel.Result result) {
|
||||
@ -155,6 +155,14 @@ public class PlatformChannel {
|
||||
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;
|
||||
@ -426,6 +434,12 @@ public class PlatformChannel {
|
||||
* {@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. */
|
||||
|
||||
@ -30,7 +30,8 @@ public class PlatformPlugin {
|
||||
private PlatformChannel.SystemChromeStyle currentTheme;
|
||||
private int mEnabledOverlays;
|
||||
|
||||
private final PlatformChannel.PlatformMessageHandler mPlatformMessageHandler =
|
||||
@VisibleForTesting
|
||||
final PlatformChannel.PlatformMessageHandler mPlatformMessageHandler =
|
||||
new PlatformChannel.PlatformMessageHandler() {
|
||||
@Override
|
||||
public void playSystemSound(@NonNull PlatformChannel.SoundType soundType) {
|
||||
@ -85,6 +86,14 @@ public class PlatformPlugin {
|
||||
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) {
|
||||
|
||||
@ -17,6 +17,7 @@ import io.flutter.embedding.engine.LocalizationPluginTest;
|
||||
import io.flutter.embedding.engine.RenderingComponentTest;
|
||||
import io.flutter.embedding.engine.plugins.shim.ShimPluginRegistryTest;
|
||||
import io.flutter.embedding.engine.renderer.FlutterRendererTest;
|
||||
import io.flutter.embedding.engine.systemchannels.PlatformChannelTest;
|
||||
import io.flutter.embedding.engine.systemchannels.RestorationChannelTest;
|
||||
import io.flutter.external.FlutterLaunchTests;
|
||||
import io.flutter.plugin.common.StandardMessageCodecTest;
|
||||
@ -68,6 +69,7 @@ import test.io.flutter.embedding.engine.dart.DartExecutorTest;
|
||||
TextInputPluginTest.class,
|
||||
MouseCursorPluginTest.class,
|
||||
AccessibilityBridgeTest.class,
|
||||
PlatformChannelTest.class,
|
||||
RestorationChannelTest.class,
|
||||
})
|
||||
/** Runs all of the unit tests listed in the {@code @SuiteClasses} annotation. */
|
||||
|
||||
@ -0,0 +1,45 @@
|
||||
package io.flutter.embedding.engine.systemchannels;
|
||||
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import android.content.res.AssetManager;
|
||||
import io.flutter.embedding.engine.FlutterJNI;
|
||||
import io.flutter.embedding.engine.dart.DartExecutor;
|
||||
import io.flutter.plugin.common.MethodCall;
|
||||
import io.flutter.plugin.common.MethodChannel;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Matchers;
|
||||
import org.robolectric.RobolectricTestRunner;
|
||||
import org.robolectric.annotation.Config;
|
||||
|
||||
@Config(manifest = Config.NONE)
|
||||
@RunWith(RobolectricTestRunner.class)
|
||||
public class PlatformChannelTest {
|
||||
@Test
|
||||
public void platformChannel_hasStringsMessage() {
|
||||
MethodChannel rawChannel = mock(MethodChannel.class);
|
||||
FlutterJNI mockFlutterJNI = mock(FlutterJNI.class);
|
||||
DartExecutor dartExecutor = new DartExecutor(mockFlutterJNI, mock(AssetManager.class));
|
||||
PlatformChannel fakePlatformChannel = new PlatformChannel(dartExecutor);
|
||||
PlatformChannel.PlatformMessageHandler mockMessageHandler =
|
||||
mock(PlatformChannel.PlatformMessageHandler.class);
|
||||
fakePlatformChannel.setPlatformMessageHandler(mockMessageHandler);
|
||||
Boolean returnValue = true;
|
||||
when(mockMessageHandler.clipboardHasStrings()).thenReturn(returnValue);
|
||||
MethodCall methodCall = new MethodCall("Clipboard.hasStrings", null);
|
||||
MethodChannel.Result mockResult = mock(MethodChannel.Result.class);
|
||||
fakePlatformChannel.parsingMethodCallHandler.onMethodCall(methodCall, mockResult);
|
||||
|
||||
JSONObject expected = new JSONObject();
|
||||
try {
|
||||
expected.put("value", returnValue);
|
||||
} catch (JSONException e) {
|
||||
}
|
||||
verify(mockResult).success(Matchers.refEq(expected));
|
||||
}
|
||||
}
|
||||
@ -1,15 +1,20 @@
|
||||
package io.flutter.plugin.platform;
|
||||
|
||||
import static org.junit.Assert.assertFalse;
|
||||
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.content.Context;
|
||||
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)
|
||||
@ -32,4 +37,25 @@ public class PlatformPluginTest {
|
||||
// SELECTION_CLICK haptic response is only available on "LOLLIPOP" (21) and later.
|
||||
platformPlugin.vibrateHapticFeedback(PlatformChannel.HapticFeedbackType.SELECTION_CLICK);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void platformPlugin_hasStrings() {
|
||||
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);
|
||||
|
||||
clipboardManager.setText("iamastring");
|
||||
assertTrue(platformPlugin.mPlatformMessageHandler.clipboardHasStrings());
|
||||
|
||||
clipboardManager.setText("");
|
||||
assertFalse(platformPlugin.mPlatformMessageHandler.clipboardHasStrings());
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user