@@ -758,9 +758,8 @@ Future<void> main() async {
758
758
});
759
759
760
760
group ('Programmatic Scroll' , () {
761
- Future <PlatformWebViewController > pumpScrollTestPage (
762
- WidgetTester tester,
763
- ) async {
761
+ testWidgets ('setAndGetAndListenScrollPosition' ,
762
+ (WidgetTester tester) async {
764
763
const String scrollTestPage = '''
765
764
<!DOCTYPE html>
766
765
<html>
@@ -786,20 +785,28 @@ Future<void> main() async {
786
785
base64Encode (const Utf8Encoder ().convert (scrollTestPage));
787
786
788
787
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);
792
793
final PlatformNavigationDelegate delegate = PlatformNavigationDelegate (
793
794
const PlatformNavigationDelegateCreationParams (),
794
795
);
795
796
await delegate.setOnPageFinished ((_) => pageLoaded.complete ());
796
797
await controller.setPlatformNavigationDelegate (delegate);
798
+ await controller.setOnScrollPositionChange (
799
+ (ScrollPositionChange contentOffsetChange) {
800
+ recordedPosition = contentOffsetChange;
801
+ });
797
802
798
- await controller.loadRequest (LoadRequestParams (
799
- uri: Uri .parse (
800
- 'data:text/html;charset=utf-8;base64,$scrollTestPageBase64 ' ,
803
+ await controller.loadRequest (
804
+ LoadRequestParams (
805
+ uri: Uri .parse (
806
+ 'data:text/html;charset=utf-8;base64,$scrollTestPageBase64 ' ,
807
+ ),
801
808
),
802
- )) ;
809
+ );
803
810
804
811
await tester.pumpWidget (Builder (
805
812
builder: (BuildContext context) {
@@ -808,95 +815,37 @@ Future<void> main() async {
808
815
).build (context);
809
816
},
810
817
));
811
- await pageLoaded.future;
812
818
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
- );
819
+ await pageLoaded.future;
894
820
895
- await controller.scrollTo (0 , 0 );
896
- await controller.scrollBy (
897
- testScrollPosition.dx.toInt (),
898
- testScrollPosition.dy.toInt (),
899
- );
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 );
900
849
});
901
850
});
902
851
0 commit comments