|
| 1 | +#import <XCTest/XCTest.h> |
| 2 | +#import <Flutter/Flutter.h> |
| 3 | +#import "AblyStreamsChannel.h" |
| 4 | + |
| 5 | +// Mock binary messenger: captures the binaryMessageHandler registered by AblyStreamsChannel, |
| 6 | +// and optionally throws NSInternalInconsistencyException on sendOnChannel:message: to simulate |
| 7 | +// a suspended/detached FlutterEngine. |
| 8 | +@interface MockThrowingMessenger : NSObject <FlutterBinaryMessenger> |
| 9 | +@property(nonatomic, copy) FlutterBinaryMessageHandler capturedHandler; |
| 10 | +@property(nonatomic, assign) BOOL shouldThrow; |
| 11 | +@property(nonatomic, assign) NSInteger sendCallCount; |
| 12 | +@end |
| 13 | + |
| 14 | +@implementation MockThrowingMessenger |
| 15 | + |
| 16 | +- (void)sendOnChannel:(NSString*)channel message:(NSData*)message { |
| 17 | + _sendCallCount++; |
| 18 | + if (_shouldThrow) { |
| 19 | + [NSException raise:NSInternalInconsistencyException |
| 20 | + format:@"Sending a message before the FlutterEngine has been run."]; |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +- (void)sendOnChannel:(NSString*)channel message:(NSData*)message binaryReply:(FlutterBinaryReply)callback {} |
| 25 | + |
| 26 | +- (FlutterBinaryMessengerConnection)setMessageHandlerOnChannel:(NSString*)channel |
| 27 | + binaryMessageHandler:(FlutterBinaryMessageHandler _Nullable)handler { |
| 28 | + _capturedHandler = handler; |
| 29 | + return 0; |
| 30 | +} |
| 31 | + |
| 32 | +- (FlutterBinaryMessengerConnection)setMessageHandlerOnChannel:(NSString*)channel |
| 33 | + binaryMessageHandler:(FlutterBinaryMessageHandler _Nullable)handler |
| 34 | + taskQueue:(NSObject<FlutterTaskQueue>* _Nullable)taskQueue { |
| 35 | + _capturedHandler = handler; |
| 36 | + return 0; |
| 37 | +} |
| 38 | + |
| 39 | +- (void)cleanUpConnection:(FlutterBinaryMessengerConnection)connection {} |
| 40 | + |
| 41 | +- (NSObject<FlutterTaskQueue>*)makeBackgroundTaskQueue { return nil; } |
| 42 | + |
| 43 | +@end |
| 44 | + |
| 45 | +// Helper stream handler that captures the eventSink passed to it during stream setup. |
| 46 | +@interface CapturingSinkHandler : NSObject <FlutterStreamHandler> |
| 47 | +@property(nonatomic, copy) FlutterEventSink capturedSink; |
| 48 | +@end |
| 49 | + |
| 50 | +@implementation CapturingSinkHandler |
| 51 | + |
| 52 | +- (FlutterError*)onListenWithArguments:(id)arguments eventSink:(FlutterEventSink)events { |
| 53 | + _capturedSink = events; |
| 54 | + return nil; |
| 55 | +} |
| 56 | + |
| 57 | +- (FlutterError*)onCancelWithArguments:(id)arguments { return nil; } |
| 58 | + |
| 59 | +@end |
| 60 | + |
| 61 | +@interface AblyStreamsChannelTests : XCTestCase |
| 62 | +@end |
| 63 | + |
| 64 | +@implementation AblyStreamsChannelTests |
| 65 | + |
| 66 | +// Sets up an AblyStreamsChannel with the given messenger and triggers a "listen#1" call, |
| 67 | +// which causes the channel to create a sink and pass it to the handler's onListenWithArguments:. |
| 68 | +// Returns the handler so tests can invoke the captured sink. |
| 69 | +- (CapturingSinkHandler*)setupChannelWithMessenger:(MockThrowingMessenger*)messenger { |
| 70 | + AblyStreamsChannel *channel = [AblyStreamsChannel |
| 71 | + streamsChannelWithName:@"test.channel" |
| 72 | + binaryMessenger:messenger |
| 73 | + codec:[FlutterStandardMethodCodec sharedInstance]]; |
| 74 | + |
| 75 | + CapturingSinkHandler *handler = [CapturingSinkHandler new]; |
| 76 | + [channel setStreamHandlerFactory:^NSObject<FlutterStreamHandler>*(id args) { return handler; }]; |
| 77 | + |
| 78 | + // Simulate Flutter sending a "listen#1" message to open a stream. |
| 79 | + // Arguments must be non-nil: AblyStreamsChannel stores them in an NSMutableDictionary |
| 80 | + // which rejects nil values. |
| 81 | + NSData *listenMsg = [[FlutterStandardMethodCodec sharedInstance] |
| 82 | + encodeMethodCall:[FlutterMethodCall methodCallWithMethodName:@"listen#1" arguments:@{}]]; |
| 83 | + messenger.capturedHandler(listenMsg, ^(NSData* reply){}); |
| 84 | + |
| 85 | + return handler; |
| 86 | +} |
| 87 | + |
| 88 | +// Regression test for: NSInternalInconsistencyException crash when the FlutterEngine is |
| 89 | +// suspended after the app is backgrounded. |
| 90 | +// The sink block in AblyStreamsChannel must catch the exception instead of propagating it. |
| 91 | +- (void)testSinkDoesNotCrashWhenEngineNotRunning { |
| 92 | + MockThrowingMessenger *messenger = [MockThrowingMessenger new]; |
| 93 | + messenger.shouldThrow = YES; |
| 94 | + CapturingSinkHandler *handler = [self setupChannelWithMessenger:messenger]; |
| 95 | + |
| 96 | + XCTAssertNoThrow(handler.capturedSink(@"state-change-event"), |
| 97 | + @"Sink should swallow NSInternalInconsistencyException when engine is stopped"); |
| 98 | +} |
| 99 | + |
| 100 | +// Verify the normal (foreground) code path still works after the fix. |
| 101 | +- (void)testSinkEmitsNormallyWhenEngineIsRunning { |
| 102 | + MockThrowingMessenger *messenger = [MockThrowingMessenger new]; |
| 103 | + messenger.shouldThrow = NO; |
| 104 | + CapturingSinkHandler *handler = [self setupChannelWithMessenger:messenger]; |
| 105 | + |
| 106 | + XCTAssertNoThrow(handler.capturedSink(@"state-change-event")); |
| 107 | + XCTAssertEqual(messenger.sendCallCount, 1, |
| 108 | + @"Messenger should be called once for a normal event emission"); |
| 109 | +} |
| 110 | + |
| 111 | +@end |
0 commit comments