Handle error more gracefully, add test for same (flutter/engine#40361)

Handle error more gracefully, add test for same
This commit is contained in:
Greg Spencer
2023-03-16 14:38:47 -07:00
committed by GitHub
parent c023f1986b
commit 0efa11abc1
2 changed files with 39 additions and 5 deletions

View File

@@ -215,9 +215,23 @@ constexpr char kTextPlainFormat[] = "text/plain";
[_engine sendOnChannel:kFlutterPlatformChannel
message:[codec encodeMethodCall:methodCall]
binaryReply:^(NSData* _Nullable reply) {
NSDictionary* replyArgs = [codec decodeEnvelope:reply];
NSAssert(_terminator, @"terminator shouldn't be nil");
id decoded_reply = [codec decodeEnvelope:reply];
if ([decoded_reply isKindOfClass:[FlutterError class]]) {
FlutterError* error = (FlutterError*)decoded_reply;
NSLog(@"Method call returned error[%@]: %@ %@", [error code], [error message],
[error details]);
_terminator(sender);
return;
}
if (![decoded_reply isKindOfClass:[NSDictionary class]]) {
NSLog(@"Call to System.requestAppExit returned an unexpected object: %@",
decoded_reply);
_terminator(sender);
return;
}
NSDictionary* replyArgs = (NSDictionary*)decoded_reply;
if ([replyArgs[@"response"] isEqual:@"exit"]) {
NSAssert(_terminator, @"terminator shouldn't be nil");
_terminator(sender);
}
if (result != nil) {

View File

@@ -12,6 +12,7 @@
#include "flutter/fml/synchronization/waitable_event.h"
#include "flutter/lib/ui/window/platform_message.h"
#include "flutter/shell/platform/common/accessibility_bridge.h"
#import "flutter/shell/platform/darwin/common/framework/Headers/FlutterChannels.h"
#import "flutter/shell/platform/darwin/macos/framework/Headers/FlutterAppDelegate.h"
#import "flutter/shell/platform/darwin/macos/framework/Headers/FlutterApplication.h"
#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterEngineTestUtils.h"
@@ -718,10 +719,19 @@ TEST_F(FlutterEngineTest, HandlesTerminationRequest) {
.andDo((^(NSInvocation* invocation) {
[invocation retainArguments];
FlutterBinaryReply callback;
NSData* returnedMessage;
[invocation getArgument:&callback atIndex:4];
NSDictionary* responseDict = @{@"response" : nextResponse};
NSData* returnedMessage =
[[FlutterJSONMethodCodec sharedInstance] encodeSuccessEnvelope:responseDict];
if ([nextResponse isEqualToString:@"error"]) {
FlutterError* errorResponse = [FlutterError errorWithCode:@"Error"
message:@"Failed"
details:@"Details"];
returnedMessage =
[[FlutterJSONMethodCodec sharedInstance] encodeErrorEnvelope:errorResponse];
} else {
NSDictionary* responseDict = @{@"response" : nextResponse};
returnedMessage =
[[FlutterJSONMethodCodec sharedInstance] encodeSuccessEnvelope:responseDict];
}
callback(returnedMessage);
}));
__block NSString* calledAfterTerminate = @"";
@@ -734,16 +744,26 @@ TEST_F(FlutterEngineTest, HandlesTerminationRequest) {
arguments:@{@"type" : @"cancelable"}];
triedToTerminate = FALSE;
calledAfterTerminate = @"";
nextResponse = @"exit";
[engineMock handleMethodCall:methodExitApplication result:appExitResult];
EXPECT_STREQ([calledAfterTerminate UTF8String], "exit");
EXPECT_TRUE(triedToTerminate);
triedToTerminate = FALSE;
calledAfterTerminate = @"";
nextResponse = @"cancel";
[engineMock handleMethodCall:methodExitApplication result:appExitResult];
EXPECT_STREQ([calledAfterTerminate UTF8String], "cancel");
EXPECT_FALSE(triedToTerminate);
// Check that it doesn't crash on error.
triedToTerminate = FALSE;
calledAfterTerminate = @"";
nextResponse = @"error";
[engineMock handleMethodCall:methodExitApplication result:appExitResult];
EXPECT_STREQ([calledAfterTerminate UTF8String], "");
EXPECT_TRUE(triedToTerminate);
}
} // namespace flutter::testing