import 'dart:async'; import 'package:flutter/foundation.dart'; import 'package:flutter/widgets.dart'; import 'package:flutter_inappwebview/flutter_inappwebview.dart'; import 'package:flutter_test/flutter_test.dart'; import '../constants.dart'; void programmaticZoomScale() { final shouldSkip = kIsWeb ? true : ![ TargetPlatform.android, TargetPlatform.iOS, TargetPlatform.macOS, ].contains(defaultTargetPlatform); group('programmatic zoom scale', () { final shouldSkipTest1 = kIsWeb ? true : ![ TargetPlatform.android, ].contains(defaultTargetPlatform); testWidgets('zoomIn/zoomOut', (WidgetTester tester) async { final Completer controllerCompleter = Completer(); final Completer pageLoaded = Completer(); await tester.pumpWidget( Directionality( textDirection: TextDirection.ltr, child: InAppWebView( key: GlobalKey(), initialUrlRequest: URLRequest(url: TEST_CROSS_PLATFORM_URL_1), onWebViewCreated: (controller) { controllerCompleter.complete(controller); }, onLoadStop: (controller, url) { pageLoaded.complete(); }, ), ), ); final InAppWebViewController controller = await controllerCompleter.future; await pageLoaded.future; expect(await controller.zoomIn(), true); await Future.delayed(Duration(seconds: 1)); expect(await controller.zoomOut(), true); }, skip: shouldSkipTest1); testWidgets('onZoomScaleChanged', (WidgetTester tester) async { final Completer controllerCompleter = Completer(); final Completer pageLoaded = Completer(); final Completer onZoomScaleChangedCompleter = Completer(); var listenForScaleChange = false; await tester.pumpWidget( Directionality( textDirection: TextDirection.ltr, child: InAppWebView( key: GlobalKey(), initialUrlRequest: URLRequest(url: TEST_URL_1), onWebViewCreated: (controller) { controllerCompleter.complete(controller); }, onLoadStop: (controller, url) { if (!pageLoaded.isCompleted) { pageLoaded.complete(); } }, onZoomScaleChanged: (controller, oldScale, newScale) { if (listenForScaleChange) { onZoomScaleChangedCompleter.complete(); } }, ), ), ); final InAppWebViewController controller = await controllerCompleter.future; await pageLoaded.future; listenForScaleChange = true; await controller.zoomBy(zoomFactor: 2); await expectLater(onZoomScaleChangedCompleter.future, completes); }); testWidgets('zoomBy', (WidgetTester tester) async { final Completer controllerCompleter = Completer(); final Completer pageLoaded = Completer(); await tester.pumpWidget( Directionality( textDirection: TextDirection.ltr, child: InAppWebView( key: GlobalKey(), initialUrlRequest: URLRequest(url: WebUri('https://flutter.dev')), onWebViewCreated: (controller) { controllerCompleter.complete(controller); }, onLoadStop: (controller, url) { pageLoaded.complete(); }, ), ), ); final InAppWebViewController controller = await controllerCompleter.future; await pageLoaded.future; await expectLater( controller.zoomBy(zoomFactor: 3.0, animated: true), completes); }); testWidgets('getZoomScale', (WidgetTester tester) async { final Completer controllerCompleter = Completer(); final Completer pageLoaded = Completer(); await tester.pumpWidget( Directionality( textDirection: TextDirection.ltr, child: InAppWebView( key: GlobalKey(), initialUrlRequest: URLRequest(url: WebUri('https://flutter.dev')), onWebViewCreated: (controller) { controllerCompleter.complete(controller); }, onLoadStop: (controller, url) { pageLoaded.complete(); }, ), ), ); final InAppWebViewController controller = await controllerCompleter.future; await pageLoaded.future; final scale = await controller.getZoomScale(); expect(scale, isNonZero); expect(scale, isPositive); }); }, skip: shouldSkip); }