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 3 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
90 changes: 90 additions & 0 deletions
90
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,90 @@ | ||
| 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() { | ||
| Map<String, String> args = new HashMap<>(); | ||
| args.put("testArg", "testValue"); | ||
| MethodCall call = new MethodCall("testMethod", args); | ||
| ByteBuffer buffer = StandardMethodCodec.INSTANCE.encodeMethodCall(call); | ||
| assertNotNull(buffer); | ||
| buffer.flip(); | ||
| MethodCall result = StandardMethodCodec.INSTANCE.decodeMethodCall(buffer); | ||
| assertEquals(call.method, result.method); | ||
| assertEquals(call.arguments, result.arguments); | ||
| } | ||
|
|
||
| @Test | ||
| public void encodeSuccessEnvelopeTest() { | ||
| Map<String, Integer> success = new HashMap<>(); | ||
mehmetf marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| success.put("result", 1); | ||
| ByteBuffer buffer = StandardMethodCodec.INSTANCE.encodeSuccessEnvelope(success); | ||
| assertNotNull(buffer); | ||
| buffer.flip(); | ||
| Object result = StandardMethodCodec.INSTANCE.decodeEnvelope(buffer); | ||
| assertEquals(success, result); | ||
| } | ||
|
|
||
| @Test | ||
| public void encodeSuccessEnvelopeUnsupportedObjectTest() { | ||
| StandardMethodCodecTest joke = new StandardMethodCodecTest(); | ||
| try { | ||
| ByteBuffer buffer = StandardMethodCodec.INSTANCE.encodeSuccessEnvelope(joke); | ||
| fail("Should have failed to convert unsupported type."); | ||
| } catch (IllegalArgumentException e) { | ||
| // pass. | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void encodeErrorEnvelopeWithNullDetailsTest() { | ||
| 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() { | ||
| Exception e = new IllegalArgumentException("foo"); | ||
| 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.