@@ -758,8 +758,9 @@ Future<void> main() async {
758
758
});
759
759
760
760
group ('Programmatic Scroll' , () {
761
- testWidgets ('setAndGetAndListenScrollPosition' ,
762
- (WidgetTester tester) async {
761
+ Future <PlatformWebViewController > pumpScrollTestPage (
762
+ WidgetTester tester,
763
+ ) async {
763
764
const String scrollTestPage = '''
764
765
<!DOCTYPE html>
765
766
<html>
@@ -785,28 +786,20 @@ Future<void> main() async {
785
786
base64Encode (const Utf8Encoder ().convert (scrollTestPage));
786
787
787
788
final Completer <void > pageLoaded = Completer <void >();
788
- ScrollPositionChange ? recordedPosition;
789
789
final PlatformWebViewController controller = PlatformWebViewController (
790
790
const PlatformWebViewControllerCreationParams (),
791
791
);
792
- await controller.setJavaScriptMode (JavaScriptMode .unrestricted);
793
792
final PlatformNavigationDelegate delegate = PlatformNavigationDelegate (
794
793
const PlatformNavigationDelegateCreationParams (),
795
794
);
796
795
await delegate.setOnPageFinished ((_) => pageLoaded.complete ());
797
796
await controller.setPlatformNavigationDelegate (delegate);
798
- await controller.setOnScrollPositionChange (
799
- (ScrollPositionChange contentOffsetChange) {
800
- recordedPosition = contentOffsetChange;
801
- });
802
797
803
- await controller.loadRequest (
804
- LoadRequestParams (
805
- uri: Uri .parse (
806
- 'data:text/html;charset=utf-8;base64,$scrollTestPageBase64 ' ,
807
- ),
798
+ await controller.loadRequest (LoadRequestParams (
799
+ uri: Uri .parse (
800
+ 'data:text/html;charset=utf-8;base64,$scrollTestPageBase64 ' ,
808
801
),
809
- );
802
+ )) ;
810
803
811
804
await tester.pumpWidget (Builder (
812
805
builder: (BuildContext context) {
@@ -815,37 +808,95 @@ Future<void> main() async {
815
808
).build (context);
816
809
},
817
810
));
818
-
819
811
await pageLoaded.future;
820
812
821
- await tester.pumpAndSettle (const Duration (seconds: 3 ));
822
-
823
- Offset scrollPos = await controller.getScrollPosition ();
824
-
825
- // Check scrollTo()
826
- const int X_SCROLL = 123 ;
827
- const int Y_SCROLL = 321 ;
828
- // Get the initial position; this ensures that scrollTo is actually
829
- // changing something, but also gives the native view's scroll position
830
- // time to settle.
831
- expect (scrollPos.dx, isNot (X_SCROLL ));
832
- expect (scrollPos.dy, isNot (Y_SCROLL ));
833
- expect (recordedPosition, null );
834
-
835
- await controller.scrollTo (X_SCROLL , Y_SCROLL );
836
- scrollPos = await controller.getScrollPosition ();
837
- expect (scrollPos.dx, X_SCROLL );
838
- expect (scrollPos.dy, Y_SCROLL );
839
- expect (recordedPosition? .x, X_SCROLL );
840
- expect (recordedPosition? .y, Y_SCROLL );
841
-
842
- // Check scrollBy() (on top of scrollTo())
843
- await controller.scrollBy (X_SCROLL , Y_SCROLL );
844
- scrollPos = await controller.getScrollPosition ();
845
- expect (scrollPos.dx, X_SCROLL * 2 );
846
- expect (scrollPos.dy, Y_SCROLL * 2 );
847
- expect (recordedPosition? .x, X_SCROLL * 2 );
848
- expect (recordedPosition? .y, Y_SCROLL * 2 );
813
+ return controller;
814
+ }
815
+
816
+ testWidgets ('getScrollPosition' , (WidgetTester tester) async {
817
+ final PlatformWebViewController controller =
818
+ await pumpScrollTestPage (tester);
819
+ await controller.setJavaScriptMode (JavaScriptMode .unrestricted);
820
+
821
+ const Offset testScrollPosition = Offset (123 , 321 );
822
+
823
+ // Ensure the start scroll position is not equal to the test position.
824
+ expect (await controller.getScrollPosition (), isNot (testScrollPosition));
825
+
826
+ final Completer <void > testScrollPositionCompleter = Completer <void >();
827
+ await controller.setOnScrollPositionChange (
828
+ (ScrollPositionChange contentOffsetChange) {
829
+ if (Offset (contentOffsetChange.x, contentOffsetChange.y) ==
830
+ testScrollPosition) {
831
+ testScrollPositionCompleter.complete ();
832
+ }
833
+ },
834
+ );
835
+
836
+ await controller.scrollTo (
837
+ testScrollPosition.dx.toInt (),
838
+ testScrollPosition.dy.toInt (),
839
+ );
840
+ await testScrollPositionCompleter.future;
841
+
842
+ expect (await controller.getScrollPosition (), testScrollPosition);
843
+ });
844
+
845
+ testWidgets ('scrollTo' , (WidgetTester tester) async {
846
+ final PlatformWebViewController controller =
847
+ await pumpScrollTestPage (tester);
848
+
849
+ const Offset testScrollPosition = Offset (123 , 321 );
850
+
851
+ // Ensure the start scroll position is not equal to the test position.
852
+ expect (await controller.getScrollPosition (), isNot (testScrollPosition));
853
+
854
+ late ScrollPositionChange lastPositionChange;
855
+ await controller.setOnScrollPositionChange (
856
+ expectAsyncUntil1 (
857
+ (ScrollPositionChange contentOffsetChange) {
858
+ lastPositionChange = contentOffsetChange;
859
+ },
860
+ () {
861
+ return Offset (lastPositionChange.x, lastPositionChange.y) ==
862
+ testScrollPosition;
863
+ },
864
+ ),
865
+ );
866
+
867
+ await controller.scrollTo (
868
+ testScrollPosition.dx.toInt (),
869
+ testScrollPosition.dy.toInt (),
870
+ );
871
+ });
872
+
873
+ testWidgets ('scrollBy' , (WidgetTester tester) async {
874
+ final PlatformWebViewController controller =
875
+ await pumpScrollTestPage (tester);
876
+
877
+ const Offset testScrollPosition = Offset (123 , 321 );
878
+
879
+ // Ensure the start scroll position is not equal to the test position.
880
+ expect (await controller.getScrollPosition (), isNot (testScrollPosition));
881
+
882
+ late ScrollPositionChange lastPositionChange;
883
+ await controller.setOnScrollPositionChange (
884
+ expectAsyncUntil1 (
885
+ (ScrollPositionChange contentOffsetChange) {
886
+ lastPositionChange = contentOffsetChange;
887
+ },
888
+ () {
889
+ return Offset (lastPositionChange.x, lastPositionChange.y) ==
890
+ testScrollPosition;
891
+ },
892
+ ),
893
+ );
894
+
895
+ await controller.scrollTo (0 , 0 );
896
+ await controller.scrollBy (
897
+ testScrollPosition.dx.toInt (),
898
+ testScrollPosition.dy.toInt (),
899
+ );
849
900
});
850
901
});
851
902
0 commit comments