Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions shell/platform/android/test/io/flutter/FlutterTestSuite.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -29,6 +30,7 @@
PlatformChannelTest.class,
PreconditionsTest.class,
RenderingComponentTest.class,
StandardMessageCodecTest.class,
SingleViewPresentationTest.class,
SmokeTest.class,
})
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Object> 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<Object> 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);
}
}