|
43 | 43 | private: |
44 | 44 | TestPlatformMessageResponse() = default; |
45 | 45 | }; |
| 46 | + |
| 47 | +// Returns a mock view controller whose view is in a window on a screen with the given native size |
| 48 | +// and scale. |
| 49 | +id CreateMockViewControllerOnScreen(CGSize screenNativeSize, CGFloat screenScale) { |
| 50 | + UIScreen* screen = OCMClassMock([UIScreen class]); |
| 51 | + OCMStub([screen nativeBounds]).andDo(^(NSInvocation* invocation) { |
| 52 | + CGRect nativeBounds = CGRectMake(0, 0, screenNativeSize.width, screenNativeSize.height); |
| 53 | + [invocation setReturnValue:&nativeBounds]; |
| 54 | + }); |
| 55 | + OCMStub([screen scale]).andDo(^(NSInvocation* invocation) { |
| 56 | + CGFloat scale = screenScale; |
| 57 | + [invocation setReturnValue:&scale]; |
| 58 | + }); |
| 59 | + UIWindowScene* windowScene = OCMClassMock([UIWindowScene class]); |
| 60 | + OCMStub([windowScene screen]).andReturn(screen); |
| 61 | + UIWindow* window = OCMClassMock([UIWindow class]); |
| 62 | + OCMStub([window windowScene]).andReturn(windowScene); |
| 63 | + UIView* view = OCMClassMock([UIView class]); |
| 64 | + OCMStub([view window]).andReturn(window); |
| 65 | + FlutterViewController* viewController = OCMClassMock([FlutterViewController class]); |
| 66 | + OCMStub([viewController viewIfLoaded]).andReturn(view); |
| 67 | + return viewController; |
| 68 | +} |
46 | 69 | } // namespace |
47 | 70 |
|
48 | 71 | /// A `FlutterTexture` that vends no frames. Used to obtain a registered texture id. |
@@ -523,6 +546,109 @@ - (void)testFlutterEngineUpdatesDisplays { |
523 | 546 | OCMVerify(times(2), [mockEngine updateDisplays]); |
524 | 547 | } |
525 | 548 |
|
| 549 | +- (void)testUpdateDisplaysFallsBackToMainScreenBeforeViewIsInWindow { |
| 550 | + FlutterEngine* engine = [[FlutterEngine alloc] init]; |
| 551 | + id mockEngine = OCMPartialMock(engine); |
| 552 | + |
| 553 | + // The framework can read the display as soon as the isolate launches, before any view controller |
| 554 | + // is attached, so `run` must send a display even though there is no screen to ask yet. |
| 555 | + [engine run]; |
| 556 | + |
| 557 | + CGSize mainScreenNativeSize = UIScreen.mainScreen.nativeBounds.size; |
| 558 | + CGFloat mainScreenScale = UIScreen.mainScreen.scale; |
| 559 | + OCMVerify(times(1), [mockEngine sendDisplayWithScreenNativeSize:mainScreenNativeSize |
| 560 | + scale:mainScreenScale]); |
| 561 | +} |
| 562 | + |
| 563 | +- (void)testUpdateDisplaysUsesScreenOfViewWindow { |
| 564 | + FlutterEngine* engine = [[FlutterEngine alloc] init]; |
| 565 | + id mockEngine = OCMPartialMock(engine); |
| 566 | + __block id viewController = nil; |
| 567 | + OCMStub([mockEngine viewController]).andDo(^(NSInvocation* invocation) { |
| 568 | + __unsafe_unretained id value = viewController; |
| 569 | + [invocation setReturnValue:&value]; |
| 570 | + }); |
| 571 | + [engine run]; |
| 572 | + |
| 573 | + viewController = CreateMockViewControllerOnScreen(CGSizeMake(1001, 2002), 3); |
| 574 | + [engine updateDisplays]; |
| 575 | + |
| 576 | + OCMVerify(times(1), [mockEngine sendDisplayWithScreenNativeSize:CGSizeMake(1001, 2002) scale:3]); |
| 577 | +} |
| 578 | + |
| 579 | +- (void)testUpdateDisplaysDoesNotResendUnchangedDisplay { |
| 580 | + FlutterEngine* engine = [[FlutterEngine alloc] init]; |
| 581 | + id mockEngine = OCMPartialMock(engine); |
| 582 | + __block id viewController = nil; |
| 583 | + OCMStub([mockEngine viewController]).andDo(^(NSInvocation* invocation) { |
| 584 | + __unsafe_unretained id value = viewController; |
| 585 | + [invocation setReturnValue:&value]; |
| 586 | + }); |
| 587 | + [engine run]; |
| 588 | + |
| 589 | + viewController = CreateMockViewControllerOnScreen(CGSizeMake(1001, 2002), 3); |
| 590 | + [engine updateDisplays]; |
| 591 | + [engine updateDisplays]; |
| 592 | + // A different view controller on a screen with the same size and scale. |
| 593 | + viewController = CreateMockViewControllerOnScreen(CGSizeMake(1001, 2002), 3); |
| 594 | + [engine updateDisplays]; |
| 595 | + |
| 596 | + OCMVerify(times(1), [mockEngine sendDisplayWithScreenNativeSize:CGSizeMake(1001, 2002) scale:3]); |
| 597 | +} |
| 598 | + |
| 599 | +- (void)testUpdateDisplaysResendsWhenScreenChanges { |
| 600 | + FlutterEngine* engine = [[FlutterEngine alloc] init]; |
| 601 | + id mockEngine = OCMPartialMock(engine); |
| 602 | + __block id viewController = nil; |
| 603 | + OCMStub([mockEngine viewController]).andDo(^(NSInvocation* invocation) { |
| 604 | + __unsafe_unretained id value = viewController; |
| 605 | + [invocation setReturnValue:&value]; |
| 606 | + }); |
| 607 | + [engine run]; |
| 608 | + |
| 609 | + // E.g. moving between the outer and inner displays of a device with multiple built-in displays. |
| 610 | + viewController = CreateMockViewControllerOnScreen(CGSizeMake(1001, 2002), 3); |
| 611 | + [engine updateDisplays]; |
| 612 | + viewController = CreateMockViewControllerOnScreen(CGSizeMake(3003, 4004), 3); |
| 613 | + [engine updateDisplays]; |
| 614 | + viewController = CreateMockViewControllerOnScreen(CGSizeMake(1001, 2002), 3); |
| 615 | + [engine updateDisplays]; |
| 616 | + // Same size, different scale. |
| 617 | + viewController = CreateMockViewControllerOnScreen(CGSizeMake(1001, 2002), 2); |
| 618 | + [engine updateDisplays]; |
| 619 | + |
| 620 | + OCMVerify(times(2), [mockEngine sendDisplayWithScreenNativeSize:CGSizeMake(1001, 2002) scale:3]); |
| 621 | + OCMVerify(times(1), [mockEngine sendDisplayWithScreenNativeSize:CGSizeMake(3003, 4004) scale:3]); |
| 622 | + OCMVerify(times(1), [mockEngine sendDisplayWithScreenNativeSize:CGSizeMake(1001, 2002) scale:2]); |
| 623 | +} |
| 624 | + |
| 625 | +- (void)testUpdateDisplaysKeepsLastDisplayWhenViewIsNotInWindow { |
| 626 | + FlutterEngine* engine = [[FlutterEngine alloc] init]; |
| 627 | + id mockEngine = OCMPartialMock(engine); |
| 628 | + __block id viewController = nil; |
| 629 | + OCMStub([mockEngine viewController]).andDo(^(NSInvocation* invocation) { |
| 630 | + __unsafe_unretained id value = viewController; |
| 631 | + [invocation setReturnValue:&value]; |
| 632 | + }); |
| 633 | + [engine run]; |
| 634 | + |
| 635 | + viewController = CreateMockViewControllerOnScreen(CGSizeMake(1001, 2002), 3); |
| 636 | + [engine updateDisplays]; |
| 637 | + // A view controller whose view is not loaded (e.g. a newly attached view controller). |
| 638 | + viewController = OCMClassMock([FlutterViewController class]); |
| 639 | + [engine updateDisplays]; |
| 640 | + // No view controller. |
| 641 | + viewController = nil; |
| 642 | + [engine updateDisplays]; |
| 643 | + |
| 644 | + // Only the initial display (from `run`) used the `UIScreen.mainScreen` fallback. |
| 645 | + CGSize mainScreenNativeSize = UIScreen.mainScreen.nativeBounds.size; |
| 646 | + CGFloat mainScreenScale = UIScreen.mainScreen.scale; |
| 647 | + OCMVerify(times(1), [mockEngine sendDisplayWithScreenNativeSize:mainScreenNativeSize |
| 648 | + scale:mainScreenScale]); |
| 649 | + OCMVerify(times(1), [mockEngine sendDisplayWithScreenNativeSize:CGSizeMake(1001, 2002) scale:3]); |
| 650 | +} |
| 651 | + |
526 | 652 | - (void)testLifeCycleNotificationDidEnterBackgroundForApplication { |
527 | 653 | FlutterDartProject* project = [[FlutterDartProject alloc] init]; |
528 | 654 | FlutterEngine* engine = [[FlutterEngine alloc] initWithName:@"foobar" project:project]; |
|
0 commit comments