From 7eebc19d379814df26af2f14e2daae0a2e7de71a Mon Sep 17 00:00:00 2001 From: Mehmet Fidanboylu Date: Tue, 19 May 2020 21:38:17 -0700 Subject: [PATCH 1/4] Add tests for StandardMethodCodec --- shell/platform/android/BUILD.gn | 1 + .../flutter/plugin/common/MethodChannel.java | 8 +- .../io/flutter/plugin/common/MethodCodec.java | 3 +- .../test/io/flutter/FlutterTestSuite.java | 2 + .../common/StandardMethodCodecTest.java | 82 +++++++++++++++++++ 5 files changed, 91 insertions(+), 5 deletions(-) create mode 100644 shell/platform/android/test/io/flutter/plugin/common/StandardMethodCodecTest.java diff --git a/shell/platform/android/BUILD.gn b/shell/platform/android/BUILD.gn index 05b176b421273..8869eadc95489 100644 --- a/shell/platform/android/BUILD.gn +++ b/shell/platform/android/BUILD.gn @@ -439,6 +439,7 @@ action("robolectric_tests") { "test/io/flutter/embedding/engine/renderer/FlutterRendererTest.java", "test/io/flutter/external/FlutterLaunchTests.java", "test/io/flutter/plugin/common/StandardMessageCodecTest.java", + "test/io/flutter/plugin/common/StandardMethodCodecTest.java", "test/io/flutter/plugin/editing/InputConnectionAdaptorTest.java", "test/io/flutter/plugin/editing/TextInputPluginTest.java", "test/io/flutter/plugin/platform/PlatformPluginTest.java", diff --git a/shell/platform/android/io/flutter/plugin/common/MethodChannel.java b/shell/platform/android/io/flutter/plugin/common/MethodChannel.java index 65ea2393d6cd4..f8cbb2c71090b 100644 --- a/shell/platform/android/io/flutter/plugin/common/MethodChannel.java +++ b/shell/platform/android/io/flutter/plugin/common/MethodChannel.java @@ -166,8 +166,8 @@ public interface Result { * Handles a successful result. * * @param result The result, possibly null. The result must be an Object type supported by the - * codec. For instance, if you are using StandardCodec (default), please see {@link - * StandardMessageCodec} documentation on what types are supported. + * codec. For instance, if you are using {@link StandardMessageCodec} (default), please see + * its documentation on what types are supported. */ @UiThread void success(@Nullable Object result); @@ -178,8 +178,8 @@ public interface Result { * @param errorCode An error code String. * @param errorMessage A human-readable error message String, possibly null. * @param errorDetails Error details, possibly null. The details must be an Object type - * supported by the codec. For instance, if you are using StandardCodec (default), please - * see {@link StandardMessageCodec} documentation on what types are supported. + * supported by the codec. For instance, if you are using {@link StandardMessageCodec} + * (default), please see its documentation on what types are supported. */ @UiThread void error(String errorCode, @Nullable String errorMessage, @Nullable Object errorDetails); diff --git a/shell/platform/android/io/flutter/plugin/common/MethodCodec.java b/shell/platform/android/io/flutter/plugin/common/MethodCodec.java index 8c9a867785ae9..fba950f9a499c 100644 --- a/shell/platform/android/io/flutter/plugin/common/MethodCodec.java +++ b/shell/platform/android/io/flutter/plugin/common/MethodCodec.java @@ -48,7 +48,8 @@ public interface MethodCodec { * * @param errorCode An error code String. * @param errorMessage An error message String, possibly null. - * @param errorDetails Error details, possibly null. + * @param errorDetails Error details, possibly null. Consider supporting {@link Throwable} in your + * codec. This is the most common value passed to this field. * @return a {@link ByteBuffer} containing the encoding between position 0 and the current * position. */ diff --git a/shell/platform/android/test/io/flutter/FlutterTestSuite.java b/shell/platform/android/test/io/flutter/FlutterTestSuite.java index 509c91c298988..820e428fce715 100644 --- a/shell/platform/android/test/io/flutter/FlutterTestSuite.java +++ b/shell/platform/android/test/io/flutter/FlutterTestSuite.java @@ -17,6 +17,7 @@ import io.flutter.embedding.engine.renderer.FlutterRendererTest; import io.flutter.external.FlutterLaunchTests; import io.flutter.plugin.common.StandardMessageCodecTest; +import io.flutter.plugin.common.StandardMethodCodecTest; import io.flutter.plugin.editing.InputConnectionAdaptorTest; import io.flutter.plugin.editing.TextInputPluginTest; import io.flutter.plugin.platform.PlatformPluginTest; @@ -52,6 +53,7 @@ PreconditionsTest.class, RenderingComponentTest.class, StandardMessageCodecTest.class, + StandardMethodCodecTest.class, ShimPluginRegistryTest.class, SingleViewPresentationTest.class, SmokeTest.class, diff --git a/shell/platform/android/test/io/flutter/plugin/common/StandardMethodCodecTest.java b/shell/platform/android/test/io/flutter/plugin/common/StandardMethodCodecTest.java new file mode 100644 index 0000000000000..2ce2f4a524d61 --- /dev/null +++ b/shell/platform/android/test/io/flutter/plugin/common/StandardMethodCodecTest.java @@ -0,0 +1,82 @@ +package io.flutter.plugin.common; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import java.nio.ByteBuffer; +import java.util.HashMap; +import java.util.Map; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.RobolectricTestRunner; +import org.robolectric.annotation.Config; + +@Config(manifest = Config.NONE) +@RunWith(RobolectricTestRunner.class) +public class StandardMethodCodecTest { + + @Test + public void encodeMethodTest() { + Map args = new HashMap<>(); + args.put("testArg", "testValue"); + MethodCall call = new MethodCall("testMethod", args); + ByteBuffer buffer = StandardMethodCodec.INSTANCE.encodeMethodCall(call); + assertNotNull(buffer); + MethodCall result = StandardMethodCodec.INSTANCE.decodeMethodCall(buffer); + assertEquals(call.method, result.method); + assertEquals(call.arguments, result.arguments); + } + + @Test + public void encodeSuccessEnvelopeTest() { + Map success = new HashMap<>(); + success.put("result", 1); + ByteBuffer buffer = StandardMethodCodec.INSTANCE.encodeSuccessEnvelope(success); + assertNotNull(buffer); + Object result = StandardMethodCodec.INSTANCE.decodeEnvelope(buffer); + assertEquals(success, result); + } + + @Test + public void encodeSuccessEnvelopeUnsupportedObjectTest() { + StandardMethodCodecTest joke = new StandardMethodCodecTest(); + try { + ByteBuffer buffer = StandardMethodCodec.INSTANCE.encodeSuccessEnvelope(joke); + fail("Should have failed to convert unsupported type."); + } catch (IllegalArgumentException e) { + // pass. + } + } + + @Test + public void encodeErrorEnvelopeWithNullDetailsTest() { + ByteBuffer buffer = StandardMethodCodec.INSTANCE.encodeErrorEnvelope("code", "error", null); + assertNotNull(buffer); + FlutterException result = + (FlutterException) StandardMethodCodec.INSTANCE.decodeEnvelope(buffer); + assertEquals("code", result.code); + assertEquals("error", result.getMessage()); + assertNull(result.details); + } + + @Test + public void encodeErrorEnvelopeWithThrowableTest() { + try { + throw new IllegalArgumentException("foo"); + } catch (IllegalArgumentException e) { + ByteBuffer buffer = + StandardMethodCodec.INSTANCE.encodeErrorEnvelope("code", e.getMessage(), e); + assertNotNull(buffer); + FlutterException result = + (FlutterException) StandardMethodCodec.INSTANCE.decodeEnvelope(buffer); + assertEquals("code", result.code); + assertEquals("error", result.getMessage()); + // Must contain part of a stack. + String stack = (String) result.details; + assertTrue(stack.contains("StandardMethodCodecTest.java:15")); + } + } +} From 3503b4dd66314e60340dbba0a2d7f0cf61364421 Mon Sep 17 00:00:00 2001 From: Mehmet Fidanboylu Date: Wed, 20 May 2020 17:37:08 -0700 Subject: [PATCH 2/4] Fix test failures. --- .../common/StandardMethodCodecTest.java | 35 +++++++++++-------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/shell/platform/android/test/io/flutter/plugin/common/StandardMethodCodecTest.java b/shell/platform/android/test/io/flutter/plugin/common/StandardMethodCodecTest.java index 2ce2f4a524d61..5db4fd44f2902 100644 --- a/shell/platform/android/test/io/flutter/plugin/common/StandardMethodCodecTest.java +++ b/shell/platform/android/test/io/flutter/plugin/common/StandardMethodCodecTest.java @@ -25,6 +25,7 @@ public void encodeMethodTest() { MethodCall call = new MethodCall("testMethod", args); ByteBuffer buffer = StandardMethodCodec.INSTANCE.encodeMethodCall(call); assertNotNull(buffer); + buffer.flip(); MethodCall result = StandardMethodCodec.INSTANCE.decodeMethodCall(buffer); assertEquals(call.method, result.method); assertEquals(call.arguments, result.arguments); @@ -36,6 +37,7 @@ public void encodeSuccessEnvelopeTest() { success.put("result", 1); ByteBuffer buffer = StandardMethodCodec.INSTANCE.encodeSuccessEnvelope(success); assertNotNull(buffer); + buffer.flip(); Object result = StandardMethodCodec.INSTANCE.decodeEnvelope(buffer); assertEquals(success, result); } @@ -55,28 +57,33 @@ public void encodeSuccessEnvelopeUnsupportedObjectTest() { public void encodeErrorEnvelopeWithNullDetailsTest() { ByteBuffer buffer = StandardMethodCodec.INSTANCE.encodeErrorEnvelope("code", "error", null); assertNotNull(buffer); - FlutterException result = - (FlutterException) StandardMethodCodec.INSTANCE.decodeEnvelope(buffer); - assertEquals("code", result.code); - assertEquals("error", result.getMessage()); - assertNull(result.details); + buffer.flip(); + try { + StandardMethodCodec.INSTANCE.decodeEnvelope(buffer); + fail("Should have thrown a FlutterException since this is an error envelope."); + } catch (FlutterException result) { + assertEquals("code", result.code); + assertEquals("error", result.getMessage()); + assertNull(result.details); + } } @Test public void encodeErrorEnvelopeWithThrowableTest() { + Exception e = new IllegalArgumentException("foo"); + ByteBuffer buffer = + StandardMethodCodec.INSTANCE.encodeErrorEnvelope("code", e.getMessage(), e); + assertNotNull(buffer); + buffer.flip(); try { - throw new IllegalArgumentException("foo"); - } catch (IllegalArgumentException e) { - ByteBuffer buffer = - StandardMethodCodec.INSTANCE.encodeErrorEnvelope("code", e.getMessage(), e); - assertNotNull(buffer); - FlutterException result = - (FlutterException) StandardMethodCodec.INSTANCE.decodeEnvelope(buffer); + StandardMethodCodec.INSTANCE.decodeEnvelope(buffer); + fail("Should have thrown a FlutterException since this is an error envelope."); + } catch (FlutterException result) { assertEquals("code", result.code); - assertEquals("error", result.getMessage()); + assertEquals("foo", result.getMessage()); // Must contain part of a stack. String stack = (String) result.details; - assertTrue(stack.contains("StandardMethodCodecTest.java:15")); + assertTrue(stack.contains("at io.flutter.plugin.common.StandardMethodCodecTest.encodeErrorEnvelopeWithThrowableTest(StandardMethodCodecTest.java:")); } } } From 9d70f960ec3f0aeba21088106888f5128575a332 Mon Sep 17 00:00:00 2001 From: Mehmet Fidanboylu Date: Wed, 20 May 2020 17:37:42 -0700 Subject: [PATCH 3/4] Fix formatting. --- .../io/flutter/plugin/common/StandardMethodCodecTest.java | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/shell/platform/android/test/io/flutter/plugin/common/StandardMethodCodecTest.java b/shell/platform/android/test/io/flutter/plugin/common/StandardMethodCodecTest.java index 5db4fd44f2902..74f23daab6e57 100644 --- a/shell/platform/android/test/io/flutter/plugin/common/StandardMethodCodecTest.java +++ b/shell/platform/android/test/io/flutter/plugin/common/StandardMethodCodecTest.java @@ -71,8 +71,7 @@ public void encodeErrorEnvelopeWithNullDetailsTest() { @Test public void encodeErrorEnvelopeWithThrowableTest() { Exception e = new IllegalArgumentException("foo"); - ByteBuffer buffer = - StandardMethodCodec.INSTANCE.encodeErrorEnvelope("code", e.getMessage(), e); + ByteBuffer buffer = StandardMethodCodec.INSTANCE.encodeErrorEnvelope("code", e.getMessage(), e); assertNotNull(buffer); buffer.flip(); try { @@ -83,7 +82,9 @@ public void encodeErrorEnvelopeWithThrowableTest() { assertEquals("foo", result.getMessage()); // Must contain part of a stack. String stack = (String) result.details; - assertTrue(stack.contains("at io.flutter.plugin.common.StandardMethodCodecTest.encodeErrorEnvelopeWithThrowableTest(StandardMethodCodecTest.java:")); + assertTrue( + stack.contains( + "at io.flutter.plugin.common.StandardMethodCodecTest.encodeErrorEnvelopeWithThrowableTest(StandardMethodCodecTest.java:")); } } } From 2cd869ccefcec82aad31277bc625cdfe7e4f13ab Mon Sep 17 00:00:00 2001 From: Mehmet Fidanboylu Date: Wed, 20 May 2020 19:32:09 -0700 Subject: [PATCH 4/4] Review comments --- .../common/StandardMethodCodecTest.java | 24 ++++++++++--------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/shell/platform/android/test/io/flutter/plugin/common/StandardMethodCodecTest.java b/shell/platform/android/test/io/flutter/plugin/common/StandardMethodCodecTest.java index 74f23daab6e57..c99a30f5d7fac 100644 --- a/shell/platform/android/test/io/flutter/plugin/common/StandardMethodCodecTest.java +++ b/shell/platform/android/test/io/flutter/plugin/common/StandardMethodCodecTest.java @@ -20,33 +20,33 @@ public class StandardMethodCodecTest { @Test public void encodeMethodTest() { - Map args = new HashMap<>(); + final Map args = new HashMap<>(); args.put("testArg", "testValue"); MethodCall call = new MethodCall("testMethod", args); - ByteBuffer buffer = StandardMethodCodec.INSTANCE.encodeMethodCall(call); + final ByteBuffer buffer = StandardMethodCodec.INSTANCE.encodeMethodCall(call); assertNotNull(buffer); buffer.flip(); - MethodCall result = StandardMethodCodec.INSTANCE.decodeMethodCall(buffer); + final MethodCall result = StandardMethodCodec.INSTANCE.decodeMethodCall(buffer); assertEquals(call.method, result.method); assertEquals(call.arguments, result.arguments); } @Test public void encodeSuccessEnvelopeTest() { - Map success = new HashMap<>(); + final Map success = new HashMap<>(); success.put("result", 1); - ByteBuffer buffer = StandardMethodCodec.INSTANCE.encodeSuccessEnvelope(success); + final ByteBuffer buffer = StandardMethodCodec.INSTANCE.encodeSuccessEnvelope(success); assertNotNull(buffer); buffer.flip(); - Object result = StandardMethodCodec.INSTANCE.decodeEnvelope(buffer); + final Object result = StandardMethodCodec.INSTANCE.decodeEnvelope(buffer); assertEquals(success, result); } @Test public void encodeSuccessEnvelopeUnsupportedObjectTest() { - StandardMethodCodecTest joke = new StandardMethodCodecTest(); + final StandardMethodCodecTest joke = new StandardMethodCodecTest(); try { - ByteBuffer buffer = StandardMethodCodec.INSTANCE.encodeSuccessEnvelope(joke); + final ByteBuffer buffer = StandardMethodCodec.INSTANCE.encodeSuccessEnvelope(joke); fail("Should have failed to convert unsupported type."); } catch (IllegalArgumentException e) { // pass. @@ -55,7 +55,8 @@ public void encodeSuccessEnvelopeUnsupportedObjectTest() { @Test public void encodeErrorEnvelopeWithNullDetailsTest() { - ByteBuffer buffer = StandardMethodCodec.INSTANCE.encodeErrorEnvelope("code", "error", null); + final ByteBuffer buffer = + StandardMethodCodec.INSTANCE.encodeErrorEnvelope("code", "error", null); assertNotNull(buffer); buffer.flip(); try { @@ -70,8 +71,9 @@ public void encodeErrorEnvelopeWithNullDetailsTest() { @Test public void encodeErrorEnvelopeWithThrowableTest() { - Exception e = new IllegalArgumentException("foo"); - ByteBuffer buffer = StandardMethodCodec.INSTANCE.encodeErrorEnvelope("code", e.getMessage(), e); + final Exception e = new IllegalArgumentException("foo"); + final ByteBuffer buffer = + StandardMethodCodec.INSTANCE.encodeErrorEnvelope("code", e.getMessage(), e); assertNotNull(buffer); buffer.flip(); try {