Skip to content

Commit a8c360d

Browse files
author
Chris Yang
authored
Update FlutterPlatformViewsTests (flutter#23158)
1 parent 418cc48 commit a8c360d

File tree

1 file changed

+129
-91
lines changed

1 file changed

+129
-91
lines changed

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

Lines changed: 129 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -621,15 +621,15 @@ - (void)testSetFlutterViewControllerAfterCreateCanStillDispatchTouchEvents {
621621
}
622622

623623
// Before setting flutter view controller, events are not dispatched.
624-
NSSet* touches1 = OCMClassMock([NSSet class]);
625-
UIEvent* event1 = OCMClassMock([UIEvent class]);
626-
UIViewController* mockFlutterViewContoller = OCMClassMock([UIViewController class]);
624+
NSSet* touches1 = [[[NSSet alloc] init] autorelease];
625+
id event1 = OCMClassMock([UIEvent class]);
626+
id mockFlutterViewContoller = OCMClassMock([UIViewController class]);
627627
[forwardGectureRecognizer touchesBegan:touches1 withEvent:event1];
628628
OCMReject([mockFlutterViewContoller touchesBegan:touches1 withEvent:event1]);
629629

630630
// Set flutter view controller allows events to be dispatched.
631-
NSSet* touches2 = OCMClassMock([NSSet class]);
632-
UIEvent* event2 = OCMClassMock([UIEvent class]);
631+
NSSet* touches2 = [[[NSSet alloc] init] autorelease];
632+
id event2 = OCMClassMock([UIEvent class]);
633633
flutterPlatformViewsController->SetFlutterViewController(mockFlutterViewContoller);
634634
[forwardGectureRecognizer touchesBegan:touches2 withEvent:event2];
635635
OCMVerify([mockFlutterViewContoller touchesBegan:touches2 withEvent:event2]);
@@ -681,39 +681,37 @@ - (void)testSetFlutterViewControllerInTheMiddleOfTouchEventShouldStillAllowGestu
681681
break;
682682
}
683683
}
684-
UIViewController* mockFlutterViewContoller = OCMClassMock([UIViewController class]);
684+
id mockFlutterViewContoller = OCMClassMock([UIViewController class]);
685685
{
686686
// ***** Sequence 1, finishing touch event with touchEnded ***** //
687-
688687
flutterPlatformViewsController->SetFlutterViewController(mockFlutterViewContoller);
689688

690-
NSSet* touches1 = OCMClassMock([NSSet class]);
691-
UIEvent* event1 = OCMClassMock([UIEvent class]);
689+
NSSet* touches1 = [[[NSSet alloc] init] autorelease];
690+
id event1 = OCMClassMock([UIEvent class]);
692691
[forwardGectureRecognizer touchesBegan:touches1 withEvent:event1];
693692
OCMVerify([mockFlutterViewContoller touchesBegan:touches1 withEvent:event1]);
694693

695694
flutterPlatformViewsController->SetFlutterViewController(nil);
696695

697696
// Allow the touch events to finish
698-
NSSet* touches2 = OCMClassMock([NSSet class]);
699-
UIEvent* event2 = OCMClassMock([UIEvent class]);
697+
NSSet* touches2 = [[[NSSet alloc] init] autorelease];
698+
id event2 = OCMClassMock([UIEvent class]);
700699
[forwardGectureRecognizer touchesMoved:touches2 withEvent:event2];
701700
OCMVerify([mockFlutterViewContoller touchesMoved:touches2 withEvent:event2]);
702701

703-
NSSet* touches3 = OCMClassMock([NSSet class]);
704-
UIEvent* event3 = OCMClassMock([UIEvent class]);
702+
NSSet* touches3 = [[[NSSet alloc] init] autorelease];
703+
id event3 = OCMClassMock([UIEvent class]);
705704
[forwardGectureRecognizer touchesEnded:touches3 withEvent:event3];
706705
OCMVerify([mockFlutterViewContoller touchesEnded:touches3 withEvent:event3]);
707706

708707
// Now the 2nd touch sequence should not be allowed.
709-
NSSet* touches4 = OCMClassMock([NSSet class]);
710-
UIEvent* event4 = OCMClassMock([UIEvent class]);
711-
mockFlutterViewContoller = OCMClassMock([UIViewController class]);
708+
NSSet* touches4 = [[[NSSet alloc] init] autorelease];
709+
id event4 = OCMClassMock([UIEvent class]);
712710
[forwardGectureRecognizer touchesBegan:touches4 withEvent:event4];
713711
OCMReject([mockFlutterViewContoller touchesBegan:touches4 withEvent:event4]);
714712

715-
NSSet* touches5 = OCMClassMock([NSSet class]);
716-
UIEvent* event5 = OCMClassMock([UIEvent class]);
713+
NSSet* touches5 = [[[NSSet alloc] init] autorelease];
714+
id event5 = OCMClassMock([UIEvent class]);
717715
[forwardGectureRecognizer touchesEnded:touches5 withEvent:event5];
718716
OCMReject([mockFlutterViewContoller touchesEnded:touches5 withEvent:event5]);
719717
}
@@ -722,105 +720,145 @@ - (void)testSetFlutterViewControllerInTheMiddleOfTouchEventShouldStillAllowGestu
722720
// ***** Sequence 2, finishing touch event with touchCancelled ***** //
723721
flutterPlatformViewsController->SetFlutterViewController(mockFlutterViewContoller);
724722

725-
NSSet* touches1 = OCMClassMock([NSSet class]);
726-
UIEvent* event1 = OCMClassMock([UIEvent class]);
723+
NSSet* touches1 = [[[NSSet alloc] init] autorelease];
724+
id event1 = OCMClassMock([UIEvent class]);
727725
[forwardGectureRecognizer touchesBegan:touches1 withEvent:event1];
728726
OCMVerify([mockFlutterViewContoller touchesBegan:touches1 withEvent:event1]);
729727

730728
flutterPlatformViewsController->SetFlutterViewController(nil);
731729

732730
// Allow the touch events to finish
733-
NSSet* touches2 = OCMClassMock([NSSet class]);
734-
UIEvent* event2 = OCMClassMock([UIEvent class]);
731+
NSSet* touches2 = [[[NSSet alloc] init] autorelease];
732+
id event2 = OCMClassMock([UIEvent class]);
735733
[forwardGectureRecognizer touchesMoved:touches2 withEvent:event2];
736734
OCMVerify([mockFlutterViewContoller touchesMoved:touches2 withEvent:event2]);
737735

738-
NSSet* touches3 = OCMClassMock([NSSet class]);
739-
UIEvent* event3 = OCMClassMock([UIEvent class]);
736+
NSSet* touches3 = [[[NSSet alloc] init] autorelease];
737+
id event3 = OCMClassMock([UIEvent class]);
740738
[forwardGectureRecognizer touchesCancelled:touches3 withEvent:event3];
741739
OCMVerify([mockFlutterViewContoller touchesCancelled:touches3 withEvent:event3]);
742740

743741
// Now the 2nd touch sequence should not be allowed.
744-
NSSet* touches4 = OCMClassMock([NSSet class]);
745-
UIEvent* event4 = OCMClassMock([UIEvent class]);
746-
mockFlutterViewContoller = OCMClassMock([UIViewController class]);
742+
NSSet* touches4 = [[[NSSet alloc] init] autorelease];
743+
id event4 = OCMClassMock([UIEvent class]);
747744
[forwardGectureRecognizer touchesBegan:touches4 withEvent:event4];
748745
OCMReject([mockFlutterViewContoller touchesBegan:touches4 withEvent:event4]);
749746

750-
NSSet* touches5 = OCMClassMock([NSSet class]);
751-
UIEvent* event5 = OCMClassMock([UIEvent class]);
747+
NSSet* touches5 = [[[NSSet alloc] init] autorelease];
748+
id event5 = OCMClassMock([UIEvent class]);
752749
[forwardGectureRecognizer touchesEnded:touches5 withEvent:event5];
753750
OCMReject([mockFlutterViewContoller touchesEnded:touches5 withEvent:event5]);
754751
}
755752

756-
{
757-
// ***** Sequence 3, multile touches in one sequence with setting flutter view controllers in
758-
// between ***** //
759-
flutterPlatformViewsController->SetFlutterViewController(mockFlutterViewContoller);
753+
flutterPlatformViewsController->Reset();
754+
}
760755

761-
NSSet* touches1 = OCMClassMock([NSSet class]);
762-
OCMStub([touches1 count]).andReturn(1);
763-
UIEvent* event1 = OCMClassMock([UIEvent class]);
764-
[forwardGectureRecognizer touchesBegan:touches1 withEvent:event1];
765-
OCMVerify([mockFlutterViewContoller touchesBegan:touches1 withEvent:event1]);
756+
- (void)
757+
testSetFlutterViewControllerInTheMiddleOfTouchEventAllowsTheNewControllerToHandleSecondTouchSequence {
758+
flutter::FlutterPlatformViewsTestMockPlatformViewDelegate mock_delegate;
759+
auto thread_task_runner = CreateNewThread("FlutterPlatformViewsTest");
760+
flutter::TaskRunners runners(/*label=*/self.name.UTF8String,
761+
/*platform=*/thread_task_runner,
762+
/*raster=*/thread_task_runner,
763+
/*ui=*/thread_task_runner,
764+
/*io=*/thread_task_runner);
765+
auto flutterPlatformViewsController = std::make_shared<flutter::FlutterPlatformViewsController>();
766+
auto platform_view = std::make_unique<flutter::PlatformViewIOS>(
767+
/*delegate=*/mock_delegate,
768+
/*rendering_api=*/flutter::IOSRenderingAPI::kSoftware,
769+
/*platform_views_controller=*/flutterPlatformViewsController,
770+
/*task_runners=*/runners);
766771

767-
UIViewController* mockFlutterViewContoller2 = OCMClassMock([UIViewController class]);
768-
flutterPlatformViewsController->SetFlutterViewController(mockFlutterViewContoller2);
769-
770-
// Touch events should still send to the old FlutterViewController if FlutterViewController
771-
// is updated in between.
772-
NSSet* touches2 = OCMClassMock([NSSet class]);
773-
OCMStub([touches2 count]).andReturn(1);
774-
UIEvent* event2 = OCMClassMock([UIEvent class]);
775-
[forwardGectureRecognizer touchesBegan:touches2 withEvent:event2];
776-
OCMVerify([mockFlutterViewContoller touchesBegan:touches2 withEvent:event2]);
777-
OCMReject([mockFlutterViewContoller2 touchesBegan:touches2 withEvent:event2]);
778-
779-
NSSet* touches3 = OCMClassMock([NSSet class]);
780-
OCMStub([touches3 count]).andReturn(1);
781-
UIEvent* event3 = OCMClassMock([UIEvent class]);
782-
[forwardGectureRecognizer touchesMoved:touches3 withEvent:event3];
783-
OCMVerify([mockFlutterViewContoller touchesMoved:touches3 withEvent:event3]);
784-
OCMReject([mockFlutterViewContoller2 touchesMoved:touches3 withEvent:event3]);
785-
786-
NSSet* touches4 = OCMClassMock([NSSet class]);
787-
OCMStub([touches4 count]).andReturn(1);
788-
UIEvent* event4 = OCMClassMock([UIEvent class]);
789-
[forwardGectureRecognizer touchesEnded:touches4 withEvent:event4];
790-
OCMVerify([mockFlutterViewContoller touchesEnded:touches4 withEvent:event4]);
791-
OCMReject([mockFlutterViewContoller2 touchesEnded:touches4 withEvent:event4]);
792-
793-
NSSet* touches5 = OCMClassMock([NSSet class]);
794-
OCMStub([touches5 count]).andReturn(1);
795-
UIEvent* event5 = OCMClassMock([UIEvent class]);
796-
[forwardGectureRecognizer touchesEnded:touches5 withEvent:event5];
797-
OCMVerify([mockFlutterViewContoller touchesEnded:touches5 withEvent:event5]);
798-
OCMReject([mockFlutterViewContoller2 touchesEnded:touches5 withEvent:event5]);
772+
FlutterPlatformViewsTestMockFlutterPlatformFactory* factory =
773+
[[FlutterPlatformViewsTestMockFlutterPlatformFactory new] autorelease];
774+
flutterPlatformViewsController->RegisterViewFactory(
775+
factory, @"MockFlutterPlatformView",
776+
FlutterPlatformViewGestureRecognizersBlockingPolicyEager);
777+
FlutterResult result = ^(id result) {
778+
};
779+
flutterPlatformViewsController->OnMethodCall(
780+
[FlutterMethodCall
781+
methodCallWithMethodName:@"create"
782+
arguments:@{@"id" : @2, @"viewType" : @"MockFlutterPlatformView"}],
783+
result);
799784

800-
// Now the 2nd touch sequence should go to the new FlutterViewController
785+
XCTAssertNotNil(gMockPlatformView);
801786

802-
NSSet* touches6 = OCMClassMock([NSSet class]);
803-
OCMStub([touches6 count]).andReturn(1);
804-
UIEvent* event6 = OCMClassMock([UIEvent class]);
805-
[forwardGectureRecognizer touchesBegan:touches6 withEvent:event6];
806-
OCMVerify([mockFlutterViewContoller2 touchesBegan:touches6 withEvent:event6]);
807-
OCMReject([mockFlutterViewContoller touchesBegan:touches6 withEvent:event6]);
787+
// Find touch inteceptor view
788+
UIView* touchInteceptorView = gMockPlatformView;
789+
while (touchInteceptorView != nil &&
790+
![touchInteceptorView isKindOfClass:[FlutterTouchInterceptingView class]]) {
791+
touchInteceptorView = touchInteceptorView.superview;
792+
}
793+
XCTAssertNotNil(touchInteceptorView);
808794

809-
// Allow the touch events to finish
810-
NSSet* touches7 = OCMClassMock([NSSet class]);
811-
OCMStub([touches7 count]).andReturn(1);
812-
UIEvent* event7 = OCMClassMock([UIEvent class]);
813-
[forwardGectureRecognizer touchesMoved:touches7 withEvent:event7];
814-
OCMVerify([mockFlutterViewContoller2 touchesMoved:touches7 withEvent:event7]);
815-
OCMReject([mockFlutterViewContoller touchesMoved:touches7 withEvent:event7]);
816-
817-
NSSet* touches8 = OCMClassMock([NSSet class]);
818-
OCMStub([touches8 count]).andReturn(1);
819-
UIEvent* event8 = OCMClassMock([UIEvent class]);
820-
[forwardGectureRecognizer touchesEnded:touches8 withEvent:event8];
821-
OCMVerify([mockFlutterViewContoller2 touchesEnded:touches8 withEvent:event8]);
822-
OCMReject([mockFlutterViewContoller touchesEnded:touches8 withEvent:event8]);
795+
// Find ForwardGestureRecognizer
796+
UIGestureRecognizer* forwardGectureRecognizer = nil;
797+
for (UIGestureRecognizer* gestureRecognizer in touchInteceptorView.gestureRecognizers) {
798+
if ([gestureRecognizer isKindOfClass:NSClassFromString(@"ForwardingGestureRecognizer")]) {
799+
forwardGectureRecognizer = gestureRecognizer;
800+
break;
801+
}
823802
}
803+
id mockFlutterViewContoller = OCMClassMock([UIViewController class]);
804+
805+
flutterPlatformViewsController->SetFlutterViewController(mockFlutterViewContoller);
806+
807+
// The touches in this sequence requires 1 touch object, we always create the NSSet with one item.
808+
NSSet* touches1 = [NSSet setWithObject:@1];
809+
id event1 = OCMClassMock([UIEvent class]);
810+
[forwardGectureRecognizer touchesBegan:touches1 withEvent:event1];
811+
OCMVerify([mockFlutterViewContoller touchesBegan:touches1 withEvent:event1]);
812+
813+
UIViewController* mockFlutterViewContoller2 = OCMClassMock([UIViewController class]);
814+
flutterPlatformViewsController->SetFlutterViewController(mockFlutterViewContoller2);
815+
816+
// Touch events should still send to the old FlutterViewController if FlutterViewController
817+
// is updated in between.
818+
NSSet* touches2 = [NSSet setWithObject:@1];
819+
id event2 = OCMClassMock([UIEvent class]);
820+
[forwardGectureRecognizer touchesBegan:touches2 withEvent:event2];
821+
OCMVerify([mockFlutterViewContoller touchesBegan:touches2 withEvent:event2]);
822+
OCMReject([mockFlutterViewContoller2 touchesBegan:touches2 withEvent:event2]);
823+
824+
NSSet* touches3 = [NSSet setWithObject:@1];
825+
id event3 = OCMClassMock([UIEvent class]);
826+
[forwardGectureRecognizer touchesMoved:touches3 withEvent:event3];
827+
OCMVerify([mockFlutterViewContoller touchesMoved:touches3 withEvent:event3]);
828+
OCMReject([mockFlutterViewContoller2 touchesMoved:touches3 withEvent:event3]);
829+
830+
NSSet* touches4 = [NSSet setWithObject:@1];
831+
id event4 = OCMClassMock([UIEvent class]);
832+
[forwardGectureRecognizer touchesEnded:touches4 withEvent:event4];
833+
OCMVerify([mockFlutterViewContoller touchesEnded:touches4 withEvent:event4]);
834+
OCMReject([mockFlutterViewContoller2 touchesEnded:touches4 withEvent:event4]);
835+
836+
NSSet* touches5 = [NSSet setWithObject:@1];
837+
id event5 = OCMClassMock([UIEvent class]);
838+
[forwardGectureRecognizer touchesEnded:touches5 withEvent:event5];
839+
OCMVerify([mockFlutterViewContoller touchesEnded:touches5 withEvent:event5]);
840+
OCMReject([mockFlutterViewContoller2 touchesEnded:touches5 withEvent:event5]);
841+
842+
// Now the 2nd touch sequence should go to the new FlutterViewController
843+
844+
NSSet* touches6 = [NSSet setWithObject:@1];
845+
id event6 = OCMClassMock([UIEvent class]);
846+
[forwardGectureRecognizer touchesBegan:touches6 withEvent:event6];
847+
OCMVerify([mockFlutterViewContoller2 touchesBegan:touches6 withEvent:event6]);
848+
OCMReject([mockFlutterViewContoller touchesBegan:touches6 withEvent:event6]);
849+
850+
// Allow the touch events to finish
851+
NSSet* touches7 = [NSSet setWithObject:@1];
852+
id event7 = OCMClassMock([UIEvent class]);
853+
[forwardGectureRecognizer touchesMoved:touches7 withEvent:event7];
854+
OCMVerify([mockFlutterViewContoller2 touchesMoved:touches7 withEvent:event7]);
855+
OCMReject([mockFlutterViewContoller touchesMoved:touches7 withEvent:event7]);
856+
857+
NSSet* touches8 = [NSSet setWithObject:@1];
858+
id event8 = OCMClassMock([UIEvent class]);
859+
[forwardGectureRecognizer touchesEnded:touches8 withEvent:event8];
860+
OCMVerify([mockFlutterViewContoller2 touchesEnded:touches8 withEvent:event8]);
861+
OCMReject([mockFlutterViewContoller touchesEnded:touches8 withEvent:event8]);
824862

825863
flutterPlatformViewsController->Reset();
826864
}

0 commit comments

Comments
 (0)