-
Notifications
You must be signed in to change notification settings - Fork 6k
hasStrings on Android #20393
hasStrings on Android #20393
Changes from 4 commits
e4ddb31
f723cfc
5d5c9b2
fdf79d6
d2f40f4
585c7e2
3e1eb3d
f606805
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
| @@ -1,18 +1,32 @@ | ||||||||
| package io.flutter.plugin.platform; | ||||||||
|
|
||||||||
| import static org.junit.Assert.assertTrue; | ||||||||
| import static org.junit.Assert.assertFalse; | ||||||||
| import static org.mockito.Mockito.mock; | ||||||||
| import static org.mockito.Mockito.verify; | ||||||||
| import static org.mockito.Mockito.when; | ||||||||
|
|
||||||||
| import android.app.Activity; | ||||||||
| import android.content.res.AssetManager; | ||||||||
| import android.content.ClipboardManager; | ||||||||
| import android.view.View; | ||||||||
| import android.view.Window; | ||||||||
| import io.flutter.embedding.engine.FlutterJNI; | ||||||||
| import io.flutter.embedding.engine.dart.DartExecutor; | ||||||||
| import io.flutter.embedding.engine.systemchannels.PlatformChannel; | ||||||||
| 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.RuntimeEnvironment; | ||||||||
| import org.robolectric.annotation.Config; | ||||||||
| import org.robolectric.shadows.ShadowClipboardManager; | ||||||||
|
|
||||||||
| @Config(manifest = Config.NONE) | ||||||||
| @Config(manifest = Config.NONE, shadows = ShadowClipboardManager.class) | ||||||||
| @RunWith(RobolectricTestRunner.class) | ||||||||
| public class PlatformPluginTest { | ||||||||
| @Config(sdk = 16) | ||||||||
|
|
@@ -32,4 +46,45 @@ 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_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)); | ||||||||
| } | ||||||||
|
|
||||||||
| @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"); | ||||||||
| assertTrue(platformPlugin.mPlatformMessageHandler.clipboardHasStrings()); | ||||||||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @gaaclarke Thanks for all the help with the tests. The first test above works, but this one is failing because the engine is unable to use the clipboard internally. I thought that my use of ShadowClipboardManager would solve this problem, but maybe I'm doing this wrong? I got this from InputConnectionAdaptorTest. The engine is unable to get a ClipboardManager here (it's null): engine/shell/platform/android/io/flutter/plugin/platform/PlatformPlugin.java Lines 272 to 274 in f1855fb
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's weird, you can try moving the ClipboardManager to the top of the test? I'm not sure if there is a temporal problem here. I don't see a difference between what you are doing and the other test. Worse case scenario you can make your own interface on top of the ClipboardManager that the PlatformPlugin uses. It would be nice if we could get this working though. Sorry, gotta run to a meeting.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks, I think I'm on to something with that. I'll post back later with results.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe it's working, thank you! Let me know if it looks alright. I did have to make one protected method public at some point. |
||||||||
|
|
||||||||
| clipboardManager.setText(""); | ||||||||
| assertFalse(platformPlugin.mPlatformMessageHandler.clipboardHasStrings()); | ||||||||
| } | ||||||||
| } | ||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think you have to make this public. I think you can get away with making it package visible since the tests should be in the same package. In java that means just omitting the visibility.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the pointer. The test is not in the same package, but I think it should be. I'll split the two tests I added and create a new PlatformChannelTest.java for the one that uses this method. Then it should work like you describe, and that organization is a lot more clear in my opinion.