This repository was archived by the owner on Feb 25, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6k
Add tests for StandardMethodCodec #18521
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
shell/platform/android/test/io/flutter/plugin/common/StandardMethodCodecTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| package io.flutter.plugin.common; | ||
|
|
||
| import static org.junit.Assert.assertEquals; | ||
| import static org.junit.Assert.assertNotNull; | ||
| import static org.junit.Assert.assertNull; | ||
| import static org.junit.Assert.assertTrue; | ||
| import static org.junit.Assert.fail; | ||
|
|
||
| import java.nio.ByteBuffer; | ||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
| import org.junit.Test; | ||
| import org.junit.runner.RunWith; | ||
| import org.robolectric.RobolectricTestRunner; | ||
| import org.robolectric.annotation.Config; | ||
|
|
||
| @Config(manifest = Config.NONE) | ||
| @RunWith(RobolectricTestRunner.class) | ||
| public class StandardMethodCodecTest { | ||
|
|
||
| @Test | ||
| public void encodeMethodTest() { | ||
| final Map<String, String> args = new HashMap<>(); | ||
| args.put("testArg", "testValue"); | ||
| MethodCall call = new MethodCall("testMethod", args); | ||
| final ByteBuffer buffer = StandardMethodCodec.INSTANCE.encodeMethodCall(call); | ||
| assertNotNull(buffer); | ||
| buffer.flip(); | ||
| final MethodCall result = StandardMethodCodec.INSTANCE.decodeMethodCall(buffer); | ||
| assertEquals(call.method, result.method); | ||
| assertEquals(call.arguments, result.arguments); | ||
| } | ||
|
|
||
| @Test | ||
| public void encodeSuccessEnvelopeTest() { | ||
| final Map<String, Integer> success = new HashMap<>(); | ||
| success.put("result", 1); | ||
| final ByteBuffer buffer = StandardMethodCodec.INSTANCE.encodeSuccessEnvelope(success); | ||
| assertNotNull(buffer); | ||
| buffer.flip(); | ||
| final Object result = StandardMethodCodec.INSTANCE.decodeEnvelope(buffer); | ||
| assertEquals(success, result); | ||
| } | ||
|
|
||
| @Test | ||
| public void encodeSuccessEnvelopeUnsupportedObjectTest() { | ||
| final StandardMethodCodecTest joke = new StandardMethodCodecTest(); | ||
| try { | ||
| final ByteBuffer buffer = StandardMethodCodec.INSTANCE.encodeSuccessEnvelope(joke); | ||
| fail("Should have failed to convert unsupported type."); | ||
| } catch (IllegalArgumentException e) { | ||
| // pass. | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void encodeErrorEnvelopeWithNullDetailsTest() { | ||
| final ByteBuffer buffer = | ||
| StandardMethodCodec.INSTANCE.encodeErrorEnvelope("code", "error", null); | ||
| assertNotNull(buffer); | ||
| buffer.flip(); | ||
| try { | ||
| StandardMethodCodec.INSTANCE.decodeEnvelope(buffer); | ||
| fail("Should have thrown a FlutterException since this is an error envelope."); | ||
| } catch (FlutterException result) { | ||
| assertEquals("code", result.code); | ||
| assertEquals("error", result.getMessage()); | ||
| assertNull(result.details); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void encodeErrorEnvelopeWithThrowableTest() { | ||
| final Exception e = new IllegalArgumentException("foo"); | ||
| final ByteBuffer buffer = | ||
| StandardMethodCodec.INSTANCE.encodeErrorEnvelope("code", e.getMessage(), e); | ||
| assertNotNull(buffer); | ||
| buffer.flip(); | ||
| try { | ||
| StandardMethodCodec.INSTANCE.decodeEnvelope(buffer); | ||
| fail("Should have thrown a FlutterException since this is an error envelope."); | ||
| } catch (FlutterException result) { | ||
| assertEquals("code", result.code); | ||
| assertEquals("foo", result.getMessage()); | ||
| // Must contain part of a stack. | ||
| String stack = (String) result.details; | ||
| assertTrue( | ||
| stack.contains( | ||
| "at io.flutter.plugin.common.StandardMethodCodecTest.encodeErrorEnvelopeWithThrowableTest(StandardMethodCodecTest.java:")); | ||
| } | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.