Flutter Engine
The Flutter Engine
StandardMessageCodecTest.java
Go to the documentation of this file.
1package io.flutter.plugin.common;
2
3import static org.junit.Assert.assertArrayEquals;
4import static org.junit.Assert.assertEquals;
5import static org.junit.Assert.assertThrows;
6import static org.junit.Assert.assertTrue;
7
8import android.text.SpannableString;
9import androidx.test.ext.junit.runners.AndroidJUnit4;
10import java.nio.ByteBuffer;
11import java.util.ArrayList;
12import org.junit.Test;
13import org.junit.runner.RunWith;
14import org.robolectric.annotation.Config;
15
16@Config(manifest = Config.NONE)
17@RunWith(AndroidJUnit4.class)
19 // Data types defined as per StandardMessageCodec.Java
20 // XXX Please consider exposing these so that tests can access them
21 private static final byte NULL = 0;
22 private static final byte TRUE = 1;
23 private static final byte FALSE = 2;
24 private static final byte INT = 3;
25 private static final byte LONG = 4;
26 private static final byte BIGINT = 5;
27 private static final byte DOUBLE = 6;
28 private static final byte STRING = 7;
29 private static final byte BYTE_ARRAY = 8;
30 private static final byte INT_ARRAY = 9;
31 private static final byte LONG_ARRAY = 10;
32 private static final byte DOUBLE_ARRAY = 11;
33 private static final byte LIST = 12;
34 private static final byte MAP = 13;
35 private static final byte FLOAT_ARRAY = 14;
36
37 @Test
38 public void itEncodesNullLiterals() {
39 // Setup message codec
41
42 // Attempt to encode message with a null literal inside a list
43 // A list with a null is used instead of just a null literal because if
44 // only null is encoded, then no message is returned; null is returned instead
45 ArrayList<Object> messageContent = new ArrayList();
46 messageContent.add(null);
47 ByteBuffer message = codec.encodeMessage(messageContent);
48 message.flip();
49 ByteBuffer expected = ByteBuffer.allocateDirect(3);
50 expected.put(new byte[] {LIST, 1, NULL});
51 expected.flip();
52 assertEquals(expected, message);
53 }
54
55 @Test
56 public void itEncodesNullObjects() {
57 // An example class that equals null
58 class ExampleNullObject {
59 @Override
60 public boolean equals(Object other) {
61 return other == null || other == this;
62 }
63
64 @Override
65 public int hashCode() {
66 return 0;
67 }
68 }
69
70 // Setup message codec
72
73 // Same as itEncodesNullLiterals but with objects that equal null instead
74 ArrayList<Object> messageContent = new ArrayList();
75 messageContent.add(new ExampleNullObject());
76 ByteBuffer message = codec.encodeMessage(messageContent);
77 message.flip();
78 ByteBuffer expected = ByteBuffer.allocateDirect(3);
79 expected.put(new byte[] {LIST, 1, NULL});
80 expected.flip();
81 assertEquals(expected, message);
82 }
83
84 @Test
85 @SuppressWarnings("deprecation")
86 public void itEncodesBooleans() {
87 // Setup message codec
89
90 ArrayList<Object> messageContent = new ArrayList();
91 // Test handling of Boolean objects other than the static TRUE and FALSE constants.
92 messageContent.add(new Boolean(true));
93 messageContent.add(new Boolean(false));
94 ByteBuffer message = codec.encodeMessage(messageContent);
95 message.flip();
96 ByteBuffer expected = ByteBuffer.allocateDirect(4);
97 expected.put(new byte[] {LIST, 2, TRUE, FALSE});
98 expected.flip();
99 assertEquals(expected, message);
100 }
101
102 @Test
103 public void itEncodesFloatArrays() {
105
106 float[] expectedValues = new float[] {1.0f, 2.2f, 5.3f};
107
108 ByteBuffer message = codec.encodeMessage(expectedValues);
109 message.flip();
110
111 float[] values = (float[]) codec.decodeMessage(message);
112 assertArrayEquals(expectedValues, values, 0.01f);
113 }
114
115 @Test
118
119 CharSequence cs = new SpannableString("hello world");
120
121 ByteBuffer message = codec.encodeMessage(cs);
122 message.flip();
123
124 String value = (String) codec.decodeMessage(message);
125 assertEquals(value, "hello world");
126 }
127
128 private static class NotEncodable {
129 @Override
130 public String toString() {
131 return "not encodable";
132 }
133 }
134
135 @Test
136 public void errorHasType() {
138 NotEncodable notEncodable = new NotEncodable();
139 Exception exception =
140 assertThrows(
141 IllegalArgumentException.class,
142 () -> {
143 codec.encodeMessage(notEncodable);
144 });
145 assertTrue(exception.getMessage().contains("NotEncodable"));
146 }
147}
bool equals(SkDrawable *a, SkDrawable *b)
ByteBuffer encodeMessage(@Nullable Object message)
Object decodeMessage(@Nullable ByteBuffer message)
uint8_t value
Win32Message message
return FALSE
long LONG
Definition: windows_types.h:23
int INT
Definition: windows_types.h:31