Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 27bf114

Browse files
authored
Reland: Started waiting for the notifications locally before asserting side-effects (#25257)
1 parent d6e5946 commit 27bf114

5 files changed

Lines changed: 71 additions & 21 deletions

File tree

shell/platform/darwin/ios/framework/Source/FlutterEnginePlatformViewTest.mm

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,17 +87,24 @@ - (void)testCallsNotifyLowMemory {
8787
OCMVerify([mockEngine notifyLowMemory]);
8888
OCMReject([mockEngine notifyLowMemory]);
8989

90+
XCTNSNotificationExpectation* memoryExpectation = [[XCTNSNotificationExpectation alloc]
91+
initWithName:UIApplicationDidReceiveMemoryWarningNotification];
9092
[[NSNotificationCenter defaultCenter]
9193
postNotificationName:UIApplicationDidReceiveMemoryWarningNotification
9294
object:nil];
95+
[self waitForExpectations:@[ memoryExpectation ] timeout:5.0];
9396
OCMVerify([mockEngine notifyLowMemory]);
9497
OCMReject([mockEngine notifyLowMemory]);
9598

99+
XCTNSNotificationExpectation* backgroundExpectation = [[XCTNSNotificationExpectation alloc]
100+
initWithName:UIApplicationDidEnterBackgroundNotification];
96101
[[NSNotificationCenter defaultCenter]
97102
postNotificationName:UIApplicationDidEnterBackgroundNotification
98103
object:nil];
104+
[self waitForExpectations:@[ backgroundExpectation ] timeout:5.0];
99105

100106
OCMVerify([mockEngine notifyLowMemory]);
107+
[mockEngine stopMocking];
101108
}
102109

103110
@end

shell/platform/darwin/ios/framework/Source/FlutterPluginAppLifeCycleDelegateTest.m

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
FLUTTER_ASSERT_ARC
1212

1313
@interface FlutterPluginAppLifeCycleDelegateTest : XCTestCase
14-
1514
@end
1615

1716
@implementation FlutterPluginAppLifeCycleDelegateTest
@@ -22,51 +21,71 @@ - (void)testCreate {
2221
}
2322

2423
- (void)testDidEnterBackground {
24+
XCTNSNotificationExpectation* expectation = [[XCTNSNotificationExpectation alloc]
25+
initWithName:UIApplicationDidEnterBackgroundNotification];
2526
FlutterPluginAppLifeCycleDelegate* delegate = [[FlutterPluginAppLifeCycleDelegate alloc] init];
2627
id plugin = OCMProtocolMock(@protocol(FlutterPlugin));
2728
[delegate addDelegate:plugin];
2829
[[NSNotificationCenter defaultCenter]
2930
postNotificationName:UIApplicationDidEnterBackgroundNotification
3031
object:nil];
32+
33+
[self waitForExpectations:@[ expectation ] timeout:5.0];
3134
OCMVerify([plugin applicationDidEnterBackground:[UIApplication sharedApplication]]);
3235
}
3336

3437
- (void)testWillEnterForeground {
38+
XCTNSNotificationExpectation* expectation = [[XCTNSNotificationExpectation alloc]
39+
initWithName:UIApplicationWillEnterForegroundNotification];
40+
3541
FlutterPluginAppLifeCycleDelegate* delegate = [[FlutterPluginAppLifeCycleDelegate alloc] init];
3642
id plugin = OCMProtocolMock(@protocol(FlutterPlugin));
3743
[delegate addDelegate:plugin];
3844
[[NSNotificationCenter defaultCenter]
3945
postNotificationName:UIApplicationWillEnterForegroundNotification
4046
object:nil];
47+
[self waitForExpectations:@[ expectation ] timeout:5.0];
4148
OCMVerify([plugin applicationWillEnterForeground:[UIApplication sharedApplication]]);
4249
}
4350

44-
- (void)skip_testWillResignActive {
51+
- (void)testWillResignActive {
52+
XCTNSNotificationExpectation* expectation =
53+
[[XCTNSNotificationExpectation alloc] initWithName:UIApplicationWillResignActiveNotification];
54+
4555
FlutterPluginAppLifeCycleDelegate* delegate = [[FlutterPluginAppLifeCycleDelegate alloc] init];
4656
id plugin = OCMProtocolMock(@protocol(FlutterPlugin));
4757
[delegate addDelegate:plugin];
4858
[[NSNotificationCenter defaultCenter]
4959
postNotificationName:UIApplicationWillResignActiveNotification
5060
object:nil];
61+
[self waitForExpectations:@[ expectation ] timeout:5.0];
5162
OCMVerify([plugin applicationWillResignActive:[UIApplication sharedApplication]]);
5263
}
5364

54-
- (void)skip_testDidBecomeActive {
65+
- (void)testDidBecomeActive {
66+
XCTNSNotificationExpectation* expectation =
67+
[[XCTNSNotificationExpectation alloc] initWithName:UIApplicationDidBecomeActiveNotification];
68+
5569
FlutterPluginAppLifeCycleDelegate* delegate = [[FlutterPluginAppLifeCycleDelegate alloc] init];
5670
id plugin = OCMProtocolMock(@protocol(FlutterPlugin));
5771
[delegate addDelegate:plugin];
5872
[[NSNotificationCenter defaultCenter]
5973
postNotificationName:UIApplicationDidBecomeActiveNotification
6074
object:nil];
75+
[self waitForExpectations:@[ expectation ] timeout:5.0];
6176
OCMVerify([plugin applicationDidBecomeActive:[UIApplication sharedApplication]]);
6277
}
6378

6479
- (void)testWillTerminate {
80+
XCTNSNotificationExpectation* expectation =
81+
[[XCTNSNotificationExpectation alloc] initWithName:UIApplicationWillTerminateNotification];
82+
6583
FlutterPluginAppLifeCycleDelegate* delegate = [[FlutterPluginAppLifeCycleDelegate alloc] init];
6684
id plugin = OCMProtocolMock(@protocol(FlutterPlugin));
6785
[delegate addDelegate:plugin];
6886
[[NSNotificationCenter defaultCenter] postNotificationName:UIApplicationWillTerminateNotification
6987
object:nil];
88+
[self waitForExpectations:@[ expectation ] timeout:5.0];
7089
OCMVerify([plugin applicationWillTerminate:[UIApplication sharedApplication]]);
7190
}
7291

shell/platform/darwin/ios/framework/Source/FlutterTextInputPluginTest.m

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -885,9 +885,8 @@ - (void)testFlutterTokenizerCanParseLines {
885885

886886
- (void)testFlutterTextInputPluginRetainsFlutterTextInputView {
887887
FlutterTextInputPlugin* myInputPlugin;
888-
id myEngine = OCMClassMock([FlutterEngine class]);
889888
myInputPlugin = [[FlutterTextInputPlugin alloc] init];
890-
myInputPlugin.textInputDelegate = myEngine;
889+
myInputPlugin.textInputDelegate = engine;
891890
__weak UIView* activeView;
892891
@autoreleasepool {
893892
FlutterMethodCall* setClientCall = [FlutterMethodCall

shell/platform/darwin/ios/framework/Source/FlutterViewControllerTest.mm

Lines changed: 40 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,22 @@
1616
class PointerDataPacket {};
1717
}
1818

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+
1935
@interface FlutterEngine ()
2036
- (BOOL)createShell:(NSString*)entrypoint
2137
libraryURI:(NSString*)libraryURI
@@ -87,30 +103,37 @@ - (void)tearDown {
87103

88104
- (void)testViewDidDisappearDoesntPauseEngineWhenNotTheViewController {
89105
id lifecycleChannel = OCMClassMock([FlutterBasicMessageChannel class]);
90-
OCMStub([self.mockEngine lifecycleChannel]).andReturn(lifecycleChannel);
106+
FlutterEnginePartialMock* mockEngine = [[FlutterEnginePartialMock alloc] init];
107+
mockEngine.lifecycleChannel = lifecycleChannel;
91108
FlutterViewController* viewControllerA =
92109
[[FlutterViewController alloc] initWithEngine:self.mockEngine nibName:nil bundle:nil];
93110
FlutterViewController* viewControllerB =
94111
[[FlutterViewController alloc] initWithEngine:self.mockEngine nibName:nil bundle:nil];
95112
id viewControllerMock = OCMPartialMock(viewControllerA);
96113
OCMStub([viewControllerMock surfaceUpdated:NO]);
97-
OCMStub([self.mockEngine viewController]).andReturn(viewControllerB);
114+
mockEngine.viewController = viewControllerB;
98115
[viewControllerA viewDidDisappear:NO];
99116
OCMReject([lifecycleChannel sendMessage:@"AppLifecycleState.paused"]);
100117
OCMReject([viewControllerMock surfaceUpdated:[OCMArg any]]);
101118
}
102119

103120
- (void)testViewDidDisappearDoesPauseEngineWhenIsTheViewController {
104121
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);
114137
}
115138

116139
- (void)testBinaryMessenger {
@@ -528,15 +551,16 @@ - (void)testHideOverlay {
528551
}
529552

530553
- (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];
534558
id viewControllerMock = OCMPartialMock(viewController);
535559
OCMStub([viewControllerMock surfaceUpdated:NO]);
536-
537560
[viewController beginAppearanceTransition:NO animated:NO];
538561
[viewController endAppearanceTransition];
539-
OCMVerify([self.mockEngine notifyLowMemory]);
562+
XCTAssertTrue(mockEngine.didCallNotifyLowMemory);
563+
[viewControllerMock stopMocking];
540564
}
541565

542566
- (void)testValidKeyUpEvent API_AVAILABLE(ios(13.4)) {

shell/platform/darwin/ios/framework/Source/accessibility_bridge_test.mm

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -970,5 +970,6 @@ - (void)testAccessibilityMessageAfterDeletion {
970970
});
971971
latch.Wait();
972972
OCMVerify([messenger cleanupConnection:connection]);
973+
[engine stopMocking];
973974
}
974975
@end

0 commit comments

Comments
 (0)