diff --git a/shell/platform/android/BUILD.gn b/shell/platform/android/BUILD.gn index 522fafd631b74..5b57be100f218 100644 --- a/shell/platform/android/BUILD.gn +++ b/shell/platform/android/BUILD.gn @@ -416,6 +416,7 @@ action("robolectric_tests") { "test/io/flutter/embedding/engine/RenderingComponentTest.java", "test/io/flutter/embedding/engine/renderer/FlutterRendererTest.java", "test/io/flutter/embedding/engine/systemchannels/PlatformChannelTest.java", + "test/io/flutter/plugin/common/StandardMessageCodecTest.java", "test/io/flutter/plugin/platform/SingleViewPresentationTest.java", "test/io/flutter/util/PreconditionsTest.java", ] diff --git a/shell/platform/android/test/io/flutter/FlutterTestSuite.java b/shell/platform/android/test/io/flutter/FlutterTestSuite.java index ca2a9ab5d24ca..0523a6b78b86a 100644 --- a/shell/platform/android/test/io/flutter/FlutterTestSuite.java +++ b/shell/platform/android/test/io/flutter/FlutterTestSuite.java @@ -15,6 +15,7 @@ import io.flutter.embedding.engine.RenderingComponentTest; import io.flutter.embedding.engine.renderer.FlutterRendererTest; import io.flutter.embedding.engine.systemchannels.PlatformChannelTest; +import io.flutter.plugin.common.StandardMessageCodecTest; import io.flutter.plugin.platform.SingleViewPresentationTest; import io.flutter.util.PreconditionsTest; @@ -29,6 +30,7 @@ PlatformChannelTest.class, PreconditionsTest.class, RenderingComponentTest.class, + StandardMessageCodecTest.class, SingleViewPresentationTest.class, SmokeTest.class, }) diff --git a/shell/platform/android/test/io/flutter/plugin/common/StandardMessageCodecTest.java b/shell/platform/android/test/io/flutter/plugin/common/StandardMessageCodecTest.java new file mode 100644 index 0000000000000..2919e57cbd51c --- /dev/null +++ b/shell/platform/android/test/io/flutter/plugin/common/StandardMessageCodecTest.java @@ -0,0 +1,80 @@ +package io.flutter.plugin.common; + +import io.flutter.plugin.common.StandardMessageCodec; +import java.nio.ByteBuffer; +import java.util.ArrayList; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.RobolectricTestRunner; +import org.robolectric.annotation.Config; + +import static org.junit.Assert.assertEquals; + +@Config(manifest=Config.NONE) +@RunWith(RobolectricTestRunner.class) +public class StandardMessageCodecTest { + // Data types defined as per StandardMessageCodec.Java + // XXX Please consider exposing these so that tests can access them + private static final byte NULL = 0; + private static final byte TRUE = 1; + private static final byte FALSE = 2; + private static final byte INT = 3; + private static final byte LONG = 4; + private static final byte BIGINT = 5; + private static final byte DOUBLE = 6; + private static final byte STRING = 7; + private static final byte BYTE_ARRAY = 8; + private static final byte INT_ARRAY = 9; + private static final byte LONG_ARRAY = 10; + private static final byte DOUBLE_ARRAY = 11; + private static final byte LIST = 12; + private static final byte MAP = 13; + + @Test + public void itEncodesNullLiterals() { + // Setup message codec + StandardMessageCodec codec = new StandardMessageCodec(); + + // Attempt to encode message with a null literal inside a list + // A list with a null is used instead of just a null literal because if + // only null is encoded, then no message is returned; null is returned instead + ArrayList messageContent = new ArrayList(); + messageContent.add(null); + ByteBuffer message = codec.encodeMessage(messageContent); + message.flip(); + ByteBuffer expected = ByteBuffer.allocateDirect(3); + expected.put(new byte[]{LIST, 1, NULL}); + expected.flip(); + assertEquals(expected, message); + } + + @Test + public void itEncodesNullObjects() { + // An example class that equals null + class ExampleNullObject { + @Override + public boolean equals(Object other) { + return other == null || other == this; + } + + @Override + public int hashCode() { + return 0; + } + } + + // Setup message codec + StandardMessageCodec codec = new StandardMessageCodec(); + + // Same as itEncodesNullLiterals but with objects that equal null instead + ArrayList messageContent = new ArrayList(); + messageContent.add(new ExampleNullObject()); + ByteBuffer message = codec.encodeMessage(messageContent); + message.flip(); + ByteBuffer expected = ByteBuffer.allocateDirect(3); + expected.put(new byte[]{LIST, 1, NULL}); + expected.flip(); + assertEquals(expected, message); + } +}