Skip to content

Commit 7fc8a16

Browse files
committed
iOS: Migrate the unit test host app to UIScene
Apps that haven't adopted the UIScene life cycle will not launch on iOS 27. The `IosUnitTests` host app was still using the pre-scene `UIApplicationMain` plus `UIMainStoryboardFile` setup, and failed with "Early unexpected exit ... The test runner crashed before establishing connection" when run with on an iOS 27 simulator. This adds a `UIApplicationSceneManifest` to the host app's `Info.plist` and a `SceneDelegate`. Migrating to scenes changes a couple things the tests were relying on. `FlutterSharedApplication.hasSceneDelegate` now returns `YES` in the host app, which suppresses forwarding of app lifecycle notifications to plugins. The `FlutterPluginAppLifeCycleDelegate` tests covering the pre-migration case stub this back out to `NO`. `[[UIWindow alloc] init]` no longer picks up a window scene so the window no longer has a screen. There were a few tests that need a screen, which now build their window with `initWithWindowScene:` instead. Without this we trip an `FML_DCHECK(self.screen)` in `FlutterView isWideGamutSupported]`. I'll send a followup to handle this more gracefully. Issue: flutter#188336
1 parent 710981c commit 7fc8a16

7 files changed

Lines changed: 78 additions & 4 deletions

File tree

‎engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterPluginAppLifeCycleDelegateTest.mm‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,8 @@ - (void)testDidEnterBackground {
197197
XCTNSNotificationExpectation* expectation = [[XCTNSNotificationExpectation alloc]
198198
initWithName:UIApplicationDidEnterBackgroundNotification];
199199
FlutterPluginAppLifeCycleDelegate* delegate = [[FlutterPluginAppLifeCycleDelegate alloc] init];
200+
id mockApplication = OCMClassMock([FlutterSharedApplication class]);
201+
OCMStub([mockApplication hasSceneDelegate]).andReturn(NO);
200202
id plugin = OCMProtocolMock(@protocol(FlutterPlugin));
201203
[delegate addDelegate:plugin];
202204
[[NSNotificationCenter defaultCenter]
@@ -246,6 +248,8 @@ - (void)testWillEnterForeground {
246248
initWithName:UIApplicationWillEnterForegroundNotification];
247249

248250
FlutterPluginAppLifeCycleDelegate* delegate = [[FlutterPluginAppLifeCycleDelegate alloc] init];
251+
id mockApplication = OCMClassMock([FlutterSharedApplication class]);
252+
OCMStub([mockApplication hasSceneDelegate]).andReturn(NO);
249253
id plugin = OCMProtocolMock(@protocol(FlutterPlugin));
250254
[delegate addDelegate:plugin];
251255
[[NSNotificationCenter defaultCenter]
@@ -294,6 +298,8 @@ - (void)testWillResignActive {
294298
[[XCTNSNotificationExpectation alloc] initWithName:UIApplicationWillResignActiveNotification];
295299

296300
FlutterPluginAppLifeCycleDelegate* delegate = [[FlutterPluginAppLifeCycleDelegate alloc] init];
301+
id mockApplication = OCMClassMock([FlutterSharedApplication class]);
302+
OCMStub([mockApplication hasSceneDelegate]).andReturn(NO);
297303
id plugin = OCMProtocolMock(@protocol(FlutterPlugin));
298304
[delegate addDelegate:plugin];
299305
[[NSNotificationCenter defaultCenter]
@@ -342,6 +348,8 @@ - (void)testDidBecomeActive {
342348
[[XCTNSNotificationExpectation alloc] initWithName:UIApplicationDidBecomeActiveNotification];
343349

344350
FlutterPluginAppLifeCycleDelegate* delegate = [[FlutterPluginAppLifeCycleDelegate alloc] init];
351+
id mockApplication = OCMClassMock([FlutterSharedApplication class]);
352+
OCMStub([mockApplication hasSceneDelegate]).andReturn(NO);
345353
id plugin = OCMProtocolMock(@protocol(FlutterPlugin));
346354
[delegate addDelegate:plugin];
347355
[[NSNotificationCenter defaultCenter]

‎engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterViewControllerTest.mm‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2310,7 +2310,11 @@ - (void)testLifeCycleNotificationApplicationBecameActive {
23102310
[engine runWithEntrypoint:nil];
23112311
FlutterViewController* flutterViewController =
23122312
[[FlutterViewController alloc] initWithEngine:engine nibName:nil bundle:nil];
2313-
UIWindow* window = [[UIWindow alloc] init];
2313+
// The window must be attached to the connected scene to have a screen, without which the
2314+
// viewport metrics stay empty and the surface is never updated.
2315+
UIWindow* window = [[UIWindow alloc]
2316+
initWithWindowScene:(UIWindowScene*)
2317+
UIApplication.sharedApplication.connectedScenes.anyObject];
23142318
[window addSubview:flutterViewController.view];
23152319
flutterViewController.view.bounds = CGRectMake(0, 0, 100, 100);
23162320
[flutterViewController viewDidLayoutSubviews];
@@ -2348,7 +2352,11 @@ - (void)testLifeCycleNotificationSceneBecameActive {
23482352
[engine runWithEntrypoint:nil];
23492353
FlutterViewController* flutterViewController =
23502354
[[FlutterViewController alloc] initWithEngine:engine nibName:nil bundle:nil];
2351-
UIWindow* window = [[UIWindow alloc] init];
2355+
// The window must be attached to the connected scene to have a screen, without which the
2356+
// viewport metrics stay empty and the surface is never updated.
2357+
UIWindow* window = [[UIWindow alloc]
2358+
initWithWindowScene:(UIWindowScene*)
2359+
UIApplication.sharedApplication.connectedScenes.anyObject];
23522360
[window addSubview:flutterViewController.view];
23532361
flutterViewController.view.bounds = CGRectMake(0, 0, 100, 100);
23542362
[flutterViewController viewDidLayoutSubviews];

‎engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterViewTest.mm‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -225,8 +225,12 @@ - (FlutterView*)createViewInWindowWithWideGamut:(BOOL)enableWideGamut {
225225
FlutterView* view = [[FlutterView alloc] initWithDelegate:delegate
226226
opaque:NO
227227
enableWideGamut:enableWideGamut];
228-
// Add to a real window so layoutSubviews has access to screen.
229-
UIWindow* window = [[UIWindow alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
228+
// Add to a real window so layoutSubviews has access to screen. The host app uses the UIScene
229+
// life cycle, so the window only has a screen once it is attached to the connected scene.
230+
UIWindow* window = [[UIWindow alloc]
231+
initWithWindowScene:(UIWindowScene*)
232+
UIApplication.sharedApplication.connectedScenes.anyObject];
233+
window.frame = CGRectMake(0, 0, 100, 100);
230234
[window addSubview:view];
231235
view.frame = window.bounds;
232236
[view layoutSubviews];

‎engine/src/flutter/testing/ios/IosUnitTests/App/Info.plist‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,27 @@
2020
<string>1</string>
2121
<key>LSRequiresIPhoneOS</key>
2222
<true/>
23+
<key>UIApplicationSceneManifest</key>
24+
<dict>
25+
<key>UIApplicationSupportsMultipleScenes</key>
26+
<false/>
27+
<key>UISceneConfigurations</key>
28+
<dict>
29+
<key>UIWindowSceneSessionRoleApplication</key>
30+
<array>
31+
<dict>
32+
<key>UISceneClassName</key>
33+
<string>UIWindowScene</string>
34+
<key>UISceneConfigurationName</key>
35+
<string>Default Configuration</string>
36+
<key>UISceneDelegateClassName</key>
37+
<string>SceneDelegate</string>
38+
<key>UISceneStoryboardFile</key>
39+
<string>Main</string>
40+
</dict>
41+
</array>
42+
</dict>
43+
</dict>
2344
<key>UILaunchStoryboardName</key>
2445
<string>LaunchScreen</string>
2546
<key>UIMainStoryboardFile</key>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright 2013 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
#ifndef FLUTTER_TESTING_IOS_IOSUNITTESTS_APP_SCENEDELEGATE_H_
6+
#define FLUTTER_TESTING_IOS_IOSUNITTESTS_APP_SCENEDELEGATE_H_
7+
8+
#import <UIKit/UIKit.h>
9+
10+
@interface SceneDelegate : UIResponder <UIWindowSceneDelegate>
11+
12+
@property(nonatomic, strong, nullable) UIWindow* window;
13+
14+
@end
15+
16+
#endif // FLUTTER_TESTING_IOS_IOSUNITTESTS_APP_SCENEDELEGATE_H_
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Copyright 2013 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
#import "SceneDelegate.h"
6+
7+
@implementation SceneDelegate
8+
9+
// The window is created from the Main storyboard and attached to the scene by UIKit.
10+
11+
@end

‎engine/src/flutter/testing/ios/IosUnitTests/IosUnitTests.xcodeproj/project.pbxproj‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
0D6AB6BE22BB05E200EEE540 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 0D6AB6BD22BB05E200EEE540 /* Assets.xcassets */; };
1717
0D6AB6C122BB05E200EEE540 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 0D6AB6BF22BB05E200EEE540 /* LaunchScreen.storyboard */; };
1818
0D6AB6C422BB05E200EEE540 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 0D6AB6C322BB05E200EEE540 /* main.m */; };
19+
0D5CE1A22E4A000100AA0003 /* SceneDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 0D5CE1A12E4A000100AA0002 /* SceneDelegate.m */; };
1920
F7521D7826BB68BC005F15C5 /* libios_test_flutter.dylib in Embed Libraries */ = {isa = PBXBuildFile; fileRef = F7521D7226BB671E005F15C5 /* libios_test_flutter.dylib */; settings = {ATTRIBUTES = (CodeSignOnCopy, ); }; };
2021
F7521D7926BB68BC005F15C5 /* libocmock_shared.dylib in Embed Libraries */ = {isa = PBXBuildFile; fileRef = F7521D7526BB673E005F15C5 /* libocmock_shared.dylib */; settings = {ATTRIBUTES = (CodeSignOnCopy, ); }; };
2122
F77E081926FA9CE6003E6E4C /* Flutter.framework in Embed Libraries */ = {isa = PBXBuildFile; fileRef = F77E081726FA9CE6003E6E4C /* Flutter.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
@@ -61,6 +62,8 @@
6162
0D6AB6B122BB05E100EEE540 /* IosUnitTests.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = IosUnitTests.app; sourceTree = BUILT_PRODUCTS_DIR; };
6263
0D6AB6B422BB05E100EEE540 /* AppDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AppDelegate.h; sourceTree = "<group>"; };
6364
0D6AB6B522BB05E100EEE540 /* AppDelegate.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = AppDelegate.m; sourceTree = "<group>"; };
65+
0D5CE1A02E4A000100AA0001 /* SceneDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = SceneDelegate.h; sourceTree = "<group>"; };
66+
0D5CE1A12E4A000100AA0002 /* SceneDelegate.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SceneDelegate.m; sourceTree = "<group>"; };
6467
0D6AB6B722BB05E100EEE540 /* ViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ViewController.h; sourceTree = "<group>"; };
6568
0D6AB6B822BB05E100EEE540 /* ViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = ViewController.m; sourceTree = "<group>"; };
6669
0D6AB6BB22BB05E100EEE540 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = "<group>"; };
@@ -164,6 +167,8 @@
164167
children = (
165168
0D6AB6B422BB05E100EEE540 /* AppDelegate.h */,
166169
0D6AB6B522BB05E100EEE540 /* AppDelegate.m */,
170+
0D5CE1A02E4A000100AA0001 /* SceneDelegate.h */,
171+
0D5CE1A12E4A000100AA0002 /* SceneDelegate.m */,
167172
0D6AB6B722BB05E100EEE540 /* ViewController.h */,
168173
0D6AB6B822BB05E100EEE540 /* ViewController.m */,
169174
0D6AB6BA22BB05E100EEE540 /* Main.storyboard */,
@@ -302,6 +307,7 @@
302307
0D6AB6B922BB05E100EEE540 /* ViewController.m in Sources */,
303308
0D6AB6C422BB05E200EEE540 /* main.m in Sources */,
304309
0D6AB6B622BB05E100EEE540 /* AppDelegate.m in Sources */,
310+
0D5CE1A22E4A000100AA0003 /* SceneDelegate.m in Sources */,
305311
);
306312
runOnlyForDeploymentPostprocessing = 0;
307313
};

0 commit comments

Comments
 (0)