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 all 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
1 change: 1 addition & 0 deletions shell/platform/android/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down
2 changes: 2 additions & 0 deletions shell/platform/android/test/io/flutter/FlutterTestSuite.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -52,6 +53,7 @@
PreconditionsTest.class,
RenderingComponentTest.class,
StandardMessageCodecTest.class,
StandardMethodCodecTest.class,
ShimPluginRegistryTest.class,
SingleViewPresentationTest.class,
SmokeTest.class,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
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() {
final Map<String, String> args = new HashMap<>();
args.put("testArg", "testValue");
MethodCall call = new MethodCall("testMethod", args);
final ByteBuffer buffer = StandardMethodCodec.INSTANCE.encodeMethodCall(call);
assertNotNull(buffer);
buffer.flip();
final MethodCall result = StandardMethodCodec.INSTANCE.decodeMethodCall(buffer);
assertEquals(call.method, result.method);
assertEquals(call.arguments, result.arguments);
}

@Test
public void encodeSuccessEnvelopeTest() {
final Map<String, Integer> success = new HashMap<>();
success.put("result", 1);
final ByteBuffer buffer = StandardMethodCodec.INSTANCE.encodeSuccessEnvelope(success);
assertNotNull(buffer);
buffer.flip();
final Object result = StandardMethodCodec.INSTANCE.decodeEnvelope(buffer);
assertEquals(success, result);
}

@Test
public void encodeSuccessEnvelopeUnsupportedObjectTest() {
final StandardMethodCodecTest joke = new StandardMethodCodecTest();
try {
final ByteBuffer buffer = StandardMethodCodec.INSTANCE.encodeSuccessEnvelope(joke);
fail("Should have failed to convert unsupported type.");
} catch (IllegalArgumentException e) {
// pass.
}
}

@Test
public void encodeErrorEnvelopeWithNullDetailsTest() {
final ByteBuffer buffer =
StandardMethodCodec.INSTANCE.encodeErrorEnvelope("code", "error", null);
assertNotNull(buffer);
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() {
final Exception e = new IllegalArgumentException("foo");
final ByteBuffer buffer =
StandardMethodCodec.INSTANCE.encodeErrorEnvelope("code", e.getMessage(), e);
assertNotNull(buffer);
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("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:"));
}
}
}