Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
StandardMethodCodecTest.java
Go to the documentation of this file.
1package io.flutter.plugin.common;
2
3import static org.junit.Assert.assertEquals;
4import static org.junit.Assert.assertNotNull;
5import static org.junit.Assert.assertNull;
6import static org.junit.Assert.assertTrue;
7import static org.junit.Assert.fail;
8
9import androidx.test.ext.junit.runners.AndroidJUnit4;
10import java.nio.ByteBuffer;
11import java.nio.ByteOrder;
12import java.util.HashMap;
13import java.util.Map;
14import org.junit.Test;
15import org.junit.runner.RunWith;
16import org.robolectric.annotation.Config;
17
18@Config(manifest = Config.NONE)
19@RunWith(AndroidJUnit4.class)
21
22 @Test
23 public void encodeMethodTest() {
24 final Map<String, String> args = new HashMap<>();
25 args.put("testArg", "testValue");
26 MethodCall call = new MethodCall("testMethod", args);
27 final ByteBuffer buffer = StandardMethodCodec.INSTANCE.encodeMethodCall(call);
28 assertNotNull(buffer);
29 buffer.flip();
31 assertEquals(call.method, result.method);
32 assertEquals(call.arguments, result.arguments);
33 }
34
35 @Test
37 final Map<String, Integer> success = new HashMap<>();
38 success.put("result", 1);
39 final ByteBuffer buffer = StandardMethodCodec.INSTANCE.encodeSuccessEnvelope(success);
40 assertNotNull(buffer);
41 buffer.flip();
43 assertEquals(success, result);
44 }
45
46 @Test
49 try {
51 fail("Should have failed to convert unsupported type.");
52 } catch (IllegalArgumentException e) {
53 // pass.
54 }
55 }
56
57 @Test
59 final ByteBuffer buffer =
61 assertNotNull(buffer);
62 buffer.flip();
63 try {
65 fail("Should have thrown a FlutterException since this is an error envelope.");
66 } catch (FlutterException result) {
67 assertEquals("code", result.code);
68 assertEquals("error", result.getMessage());
69 assertNull(result.details);
70 }
71 }
72
73 @Test
75 final Exception e = new IllegalArgumentException("foo");
76 final ByteBuffer buffer =
77 StandardMethodCodec.INSTANCE.encodeErrorEnvelope("code", e.getMessage(), e);
78 assertNotNull(buffer);
79 buffer.flip();
80 try {
82 fail("Should have thrown a FlutterException since this is an error envelope.");
83 } catch (FlutterException result) {
84 assertEquals("code", result.code);
85 assertEquals("foo", result.getMessage());
86 // Must contain part of a stack.
87 String stack = (String) result.details;
88 assertTrue(
89 stack.contains(
90 "at io.flutter.plugin.common.StandardMethodCodecTest.encodeErrorEnvelopeWithThrowableTest(StandardMethodCodecTest.java:"));
91 }
92 }
93
94 @Test
96 final Exception e = new IllegalArgumentException("foo");
97 final ByteBuffer buffer =
99 "code", e.getMessage(), e, "error stacktrace");
100 assertNotNull(buffer);
101 buffer.flip();
102 buffer.order(ByteOrder.nativeOrder());
103 final byte flag = buffer.get();
104 final Object code = StandardMessageCodec.INSTANCE.readValue(buffer);
106 final Object details = StandardMessageCodec.INSTANCE.readValue(buffer);
107 final Object stacktrace = StandardMessageCodec.INSTANCE.readValue(buffer);
108 assertEquals("code", (String) code);
109 assertEquals("foo", (String) message);
110 String stack = (String) details;
111 assertTrue(
112 stack.contains(
113 "at io.flutter.plugin.common.StandardMethodCodecTest.encodeErrorEnvelopeWithStacktraceTest(StandardMethodCodecTest.java:"));
114 assertEquals("error stacktrace", (String) stacktrace);
115 }
116}
static void fail(const SkString &err)
Definition DM.cpp:234
final Object readValue(@NonNull ByteBuffer buffer)
ByteBuffer encodeSuccessEnvelope(@NonNull Object result)
ByteBuffer encodeErrorEnvelopeWithStacktrace( @NonNull String errorCode, @NonNull String errorMessage, @NonNull Object errorDetails, @NonNull String errorStacktrace)
ByteBuffer encodeErrorEnvelope( @NonNull String errorCode, @NonNull String errorMessage, @NonNull Object errorDetails)
Object decodeEnvelope(@NonNull ByteBuffer envelope)
ByteBuffer encodeMethodCall(@NonNull MethodCall methodCall)
MethodCall decodeMethodCall(@NonNull ByteBuffer methodCall)
FlutterSemanticsFlag flag
G_BEGIN_DECLS G_MODULE_EXPORT FlValue * args
static const uint8_t buffer[]
GAsyncResult * result
Win32Message message