|  | 
|  | 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