|
16 | 16 | class PointerDataPacket {}; |
17 | 17 | } |
18 | 18 |
|
| 19 | +/// Sometimes we have to use a custom mock to avoid retain cycles in ocmock. |
| 20 | +@interface FlutterEnginePartialMock : FlutterEngine |
| 21 | +@property(nonatomic, strong) FlutterBasicMessageChannel* lifecycleChannel; |
| 22 | +@property(nonatomic, weak) FlutterViewController* viewController; |
| 23 | +@property(nonatomic, assign) BOOL didCallNotifyLowMemory; |
| 24 | +@end |
| 25 | + |
| 26 | +@implementation FlutterEnginePartialMock |
| 27 | +@synthesize viewController; |
| 28 | +@synthesize lifecycleChannel; |
| 29 | + |
| 30 | +- (void)notifyLowMemory { |
| 31 | + _didCallNotifyLowMemory = YES; |
| 32 | +} |
| 33 | +@end |
| 34 | + |
19 | 35 | @interface FlutterEngine () |
20 | 36 | - (BOOL)createShell:(NSString*)entrypoint |
21 | 37 | libraryURI:(NSString*)libraryURI |
@@ -87,30 +103,37 @@ - (void)tearDown { |
87 | 103 |
|
88 | 104 | - (void)testViewDidDisappearDoesntPauseEngineWhenNotTheViewController { |
89 | 105 | id lifecycleChannel = OCMClassMock([FlutterBasicMessageChannel class]); |
90 | | - OCMStub([self.mockEngine lifecycleChannel]).andReturn(lifecycleChannel); |
| 106 | + FlutterEnginePartialMock* mockEngine = [[FlutterEnginePartialMock alloc] init]; |
| 107 | + mockEngine.lifecycleChannel = lifecycleChannel; |
91 | 108 | FlutterViewController* viewControllerA = |
92 | 109 | [[FlutterViewController alloc] initWithEngine:self.mockEngine nibName:nil bundle:nil]; |
93 | 110 | FlutterViewController* viewControllerB = |
94 | 111 | [[FlutterViewController alloc] initWithEngine:self.mockEngine nibName:nil bundle:nil]; |
95 | 112 | id viewControllerMock = OCMPartialMock(viewControllerA); |
96 | 113 | OCMStub([viewControllerMock surfaceUpdated:NO]); |
97 | | - OCMStub([self.mockEngine viewController]).andReturn(viewControllerB); |
| 114 | + mockEngine.viewController = viewControllerB; |
98 | 115 | [viewControllerA viewDidDisappear:NO]; |
99 | 116 | OCMReject([lifecycleChannel sendMessage:@"AppLifecycleState.paused"]); |
100 | 117 | OCMReject([viewControllerMock surfaceUpdated:[OCMArg any]]); |
101 | 118 | } |
102 | 119 |
|
103 | 120 | - (void)testViewDidDisappearDoesPauseEngineWhenIsTheViewController { |
104 | 121 | id lifecycleChannel = OCMClassMock([FlutterBasicMessageChannel class]); |
105 | | - OCMStub([self.mockEngine lifecycleChannel]).andReturn(lifecycleChannel); |
106 | | - FlutterViewController* viewController = |
107 | | - [[FlutterViewController alloc] initWithEngine:self.mockEngine nibName:nil bundle:nil]; |
108 | | - id viewControllerMock = OCMPartialMock(viewController); |
109 | | - OCMStub([viewControllerMock surfaceUpdated:NO]); |
110 | | - OCMStub([self.mockEngine viewController]).andReturn(viewController); |
111 | | - [viewController viewDidDisappear:NO]; |
112 | | - OCMVerify([lifecycleChannel sendMessage:@"AppLifecycleState.paused"]); |
113 | | - OCMVerify([viewControllerMock surfaceUpdated:NO]); |
| 122 | + FlutterEnginePartialMock* mockEngine = [[FlutterEnginePartialMock alloc] init]; |
| 123 | + mockEngine.lifecycleChannel = lifecycleChannel; |
| 124 | + __weak FlutterViewController* weakViewController; |
| 125 | + @autoreleasepool { |
| 126 | + FlutterViewController* viewController = [[FlutterViewController alloc] initWithEngine:mockEngine |
| 127 | + nibName:nil |
| 128 | + bundle:nil]; |
| 129 | + weakViewController = viewController; |
| 130 | + id viewControllerMock = OCMPartialMock(viewController); |
| 131 | + OCMStub([viewControllerMock surfaceUpdated:NO]); |
| 132 | + [viewController viewDidDisappear:NO]; |
| 133 | + OCMVerify([lifecycleChannel sendMessage:@"AppLifecycleState.paused"]); |
| 134 | + OCMVerify([viewControllerMock surfaceUpdated:NO]); |
| 135 | + } |
| 136 | + XCTAssertNil(weakViewController); |
114 | 137 | } |
115 | 138 |
|
116 | 139 | - (void)testBinaryMessenger { |
@@ -528,15 +551,16 @@ - (void)testHideOverlay { |
528 | 551 | } |
529 | 552 |
|
530 | 553 | - (void)testNotifyLowMemory { |
531 | | - FlutterViewController* viewController = |
532 | | - [[FlutterViewController alloc] initWithEngine:self.mockEngine nibName:nil bundle:nil]; |
533 | | - OCMStub([self.mockEngine viewController]).andReturn(viewController); |
| 554 | + FlutterEnginePartialMock* mockEngine = [[FlutterEnginePartialMock alloc] init]; |
| 555 | + FlutterViewController* viewController = [[FlutterViewController alloc] initWithEngine:mockEngine |
| 556 | + nibName:nil |
| 557 | + bundle:nil]; |
534 | 558 | id viewControllerMock = OCMPartialMock(viewController); |
535 | 559 | OCMStub([viewControllerMock surfaceUpdated:NO]); |
536 | | - |
537 | 560 | [viewController beginAppearanceTransition:NO animated:NO]; |
538 | 561 | [viewController endAppearanceTransition]; |
539 | | - OCMVerify([self.mockEngine notifyLowMemory]); |
| 562 | + XCTAssertTrue(mockEngine.didCallNotifyLowMemory); |
| 563 | + [viewControllerMock stopMocking]; |
540 | 564 | } |
541 | 565 |
|
542 | 566 | - (void)testValidKeyUpEvent API_AVAILABLE(ios(13.4)) { |
|
0 commit comments