Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 5d5c9b2

Browse files
committed
Test hasStrings message
1 parent f723cfc commit 5d5c9b2

File tree

2 files changed

+33
-17
lines changed

2 files changed

+33
-17
lines changed

shell/platform/android/io/flutter/embedding/engine/systemchannels/PlatformChannel.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public class PlatformChannel {
3030
@Nullable private PlatformMessageHandler platformMessageHandler;
3131

3232
@NonNull @VisibleForTesting
33-
protected final MethodChannel.MethodCallHandler parsingMethodCallHandler =
33+
public final MethodChannel.MethodCallHandler parsingMethodCallHandler =
3434
new MethodChannel.MethodCallHandler() {
3535
@Override
3636
public void onMethodCall(@NonNull MethodCall call, @NonNull MethodChannel.Result result) {

shell/platform/android/test/io/flutter/plugin/platform/PlatformPluginTest.java

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,29 @@
22

33
import static org.junit.Assert.assertTrue;
44
import static org.mockito.Mockito.mock;
5+
import static org.mockito.Mockito.spy;
6+
import static org.mockito.Mockito.verify;
57
import static org.mockito.Mockito.when;
68

79
import android.app.Activity;
10+
import android.content.res.AssetManager;
811
import android.content.ClipboardManager;
912
import android.view.View;
1013
import android.view.Window;
14+
import io.flutter.embedding.engine.FlutterJNI;
15+
import io.flutter.embedding.engine.dart.DartExecutor;
1116
import io.flutter.embedding.engine.systemchannels.PlatformChannel;
17+
import io.flutter.embedding.engine.systemchannels.TextInputChannel;
18+
import io.flutter.plugin.common.MethodCall;
19+
import io.flutter.plugin.common.MethodChannel;
20+
import java.util.HashMap;
21+
import java.util.Map;
22+
import org.json.JSONException;
23+
import org.json.JSONObject;
1224
import org.junit.Test;
1325
import org.junit.runner.RunWith;
26+
import org.mockito.ArgumentCaptor;
27+
import org.mockito.Matchers;
1428
import org.robolectric.RobolectricTestRunner;
1529
import org.robolectric.RuntimeEnvironment;
1630
import org.robolectric.annotation.Config;
@@ -37,22 +51,24 @@ public void itIgnoresNewHapticEventsOnOldAndroidPlatforms() {
3751
}
3852

3953
@Test
40-
public void platformPlugin_hasStrings() {
41-
View fakeDecorView = mock(View.class);
42-
Window fakeWindow = mock(Window.class);
43-
when(fakeWindow.getDecorView()).thenReturn(fakeDecorView);
44-
Activity fakeActivity = mock(Activity.class);
45-
when(fakeActivity.getWindow()).thenReturn(fakeWindow);
46-
PlatformChannel fakePlatformChannel = mock(PlatformChannel.class);
47-
PlatformPlugin platformPlugin = new PlatformPlugin(fakeActivity, fakePlatformChannel);
48-
49-
ClipboardManager clipboardManager =
50-
RuntimeEnvironment.application.getSystemService(ClipboardManager.class);
51-
clipboardManager.setText("iamastring");
54+
public void platformPlugin_hasStringsMessage() {
55+
MethodChannel rawChannel = mock(MethodChannel.class);
56+
FlutterJNI mockFlutterJNI = mock(FlutterJNI.class);
57+
DartExecutor dartExecutor = new DartExecutor(mockFlutterJNI, mock(AssetManager.class));
58+
PlatformChannel fakePlatformChannel = new PlatformChannel(dartExecutor);
59+
PlatformChannel.PlatformMessageHandler mockMessageHandler = mock(PlatformChannel.PlatformMessageHandler.class);
60+
fakePlatformChannel.setPlatformMessageHandler(mockMessageHandler);
61+
Boolean returnValue = true;
62+
when(mockMessageHandler.clipboardHasStrings()).thenReturn(returnValue);
63+
MethodCall methodCall = new MethodCall("Clipboard.hasStrings", null);
64+
MethodChannel.Result mockResult = mock(MethodChannel.Result.class);
65+
fakePlatformChannel.parsingMethodCallHandler.onMethodCall(methodCall, mockResult);
5266

53-
// TODO(justinmc): Can't do this because mPlatformMessageHandler is private.
54-
// How can I send a message to the platform plugin?
55-
//boolean hasStrings = platformPlugin.mPlatformMessageHandler.clipboardHasStrings();
56-
//assertTrue(hasStrings);
67+
JSONObject expected = new JSONObject();
68+
try {
69+
expected.put("value", returnValue);
70+
} catch (JSONException e) {
71+
}
72+
verify(mockResult).success(Matchers.refEq(expected));
5773
}
5874
}

0 commit comments

Comments
 (0)