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

Commit caf1df6

Browse files
rafernMichael Klimushyn
authored and
Michael Klimushyn
committed
Tests for #11283 (#12322)
Added tests for #11283. The itEncodesNullObjects test fails with the branch before the merge and succeeds with the master branch
1 parent 851f460 commit caf1df6

File tree

3 files changed

+83
-0
lines changed

3 files changed

+83
-0
lines changed

shell/platform/android/BUILD.gn

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,7 @@ action("robolectric_tests") {
416416
"test/io/flutter/embedding/engine/RenderingComponentTest.java",
417417
"test/io/flutter/embedding/engine/renderer/FlutterRendererTest.java",
418418
"test/io/flutter/embedding/engine/systemchannels/PlatformChannelTest.java",
419+
"test/io/flutter/plugin/common/StandardMessageCodecTest.java",
419420
"test/io/flutter/plugin/platform/SingleViewPresentationTest.java",
420421
"test/io/flutter/util/PreconditionsTest.java",
421422
]

shell/platform/android/test/io/flutter/FlutterTestSuite.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import io.flutter.embedding.engine.RenderingComponentTest;
1616
import io.flutter.embedding.engine.renderer.FlutterRendererTest;
1717
import io.flutter.embedding.engine.systemchannels.PlatformChannelTest;
18+
import io.flutter.plugin.common.StandardMessageCodecTest;
1819
import io.flutter.plugin.platform.SingleViewPresentationTest;
1920
import io.flutter.util.PreconditionsTest;
2021

@@ -29,6 +30,7 @@
2930
PlatformChannelTest.class,
3031
PreconditionsTest.class,
3132
RenderingComponentTest.class,
33+
StandardMessageCodecTest.class,
3234
SingleViewPresentationTest.class,
3335
SmokeTest.class,
3436
})
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package io.flutter.plugin.common;
2+
3+
import io.flutter.plugin.common.StandardMessageCodec;
4+
import java.nio.ByteBuffer;
5+
import java.util.ArrayList;
6+
7+
import org.junit.Test;
8+
import org.junit.runner.RunWith;
9+
import org.robolectric.RobolectricTestRunner;
10+
import org.robolectric.annotation.Config;
11+
12+
import static org.junit.Assert.assertEquals;
13+
14+
@Config(manifest=Config.NONE)
15+
@RunWith(RobolectricTestRunner.class)
16+
public class StandardMessageCodecTest {
17+
// Data types defined as per StandardMessageCodec.Java
18+
// XXX Please consider exposing these so that tests can access them
19+
private static final byte NULL = 0;
20+
private static final byte TRUE = 1;
21+
private static final byte FALSE = 2;
22+
private static final byte INT = 3;
23+
private static final byte LONG = 4;
24+
private static final byte BIGINT = 5;
25+
private static final byte DOUBLE = 6;
26+
private static final byte STRING = 7;
27+
private static final byte BYTE_ARRAY = 8;
28+
private static final byte INT_ARRAY = 9;
29+
private static final byte LONG_ARRAY = 10;
30+
private static final byte DOUBLE_ARRAY = 11;
31+
private static final byte LIST = 12;
32+
private static final byte MAP = 13;
33+
34+
@Test
35+
public void itEncodesNullLiterals() {
36+
// Setup message codec
37+
StandardMessageCodec codec = new StandardMessageCodec();
38+
39+
// Attempt to encode message with a null literal inside a list
40+
// A list with a null is used instead of just a null literal because if
41+
// only null is encoded, then no message is returned; null is returned instead
42+
ArrayList<Object> messageContent = new ArrayList();
43+
messageContent.add(null);
44+
ByteBuffer message = codec.encodeMessage(messageContent);
45+
message.flip();
46+
ByteBuffer expected = ByteBuffer.allocateDirect(3);
47+
expected.put(new byte[]{LIST, 1, NULL});
48+
expected.flip();
49+
assertEquals(expected, message);
50+
}
51+
52+
@Test
53+
public void itEncodesNullObjects() {
54+
// An example class that equals null
55+
class ExampleNullObject {
56+
@Override
57+
public boolean equals(Object other) {
58+
return other == null || other == this;
59+
}
60+
61+
@Override
62+
public int hashCode() {
63+
return 0;
64+
}
65+
}
66+
67+
// Setup message codec
68+
StandardMessageCodec codec = new StandardMessageCodec();
69+
70+
// Same as itEncodesNullLiterals but with objects that equal null instead
71+
ArrayList<Object> messageContent = new ArrayList();
72+
messageContent.add(new ExampleNullObject());
73+
ByteBuffer message = codec.encodeMessage(messageContent);
74+
message.flip();
75+
ByteBuffer expected = ByteBuffer.allocateDirect(3);
76+
expected.put(new byte[]{LIST, 1, NULL});
77+
expected.flip();
78+
assertEquals(expected, message);
79+
}
80+
}

0 commit comments

Comments
 (0)