|
| 1 | +// Copyright 2013 The Flutter Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +import 'package:flutter/foundation.dart'; |
| 6 | +import 'package:flutter/gestures.dart'; |
| 7 | +import 'package:flutter/widgets.dart'; |
| 8 | +import 'package:flutter_test/flutter_test.dart'; |
| 9 | +import 'package:mockito/annotations.dart'; |
| 10 | +import 'package:mockito/mockito.dart'; |
| 11 | +import 'package:webview_flutter/src/v4/webview_flutter.dart'; |
| 12 | +import 'package:webview_flutter_platform_interface/v4/webview_flutter_platform_interface.dart'; |
| 13 | + |
| 14 | +import 'webview_widget_test.mocks.dart'; |
| 15 | + |
| 16 | +@GenerateMocks(<Type>[PlatformWebViewController, PlatformWebViewWidget]) |
| 17 | +void main() { |
| 18 | + TestWidgetsFlutterBinding.ensureInitialized(); |
| 19 | + |
| 20 | + group('WebViewWidget', () { |
| 21 | + testWidgets('build', (WidgetTester tester) async { |
| 22 | + final MockPlatformWebViewWidget mockPlatformWebViewWidget = |
| 23 | + MockPlatformWebViewWidget(); |
| 24 | + when(mockPlatformWebViewWidget.build(any)).thenReturn(Container()); |
| 25 | + |
| 26 | + await tester.pumpWidget(WebViewWidget.fromPlatform( |
| 27 | + platform: mockPlatformWebViewWidget, |
| 28 | + )); |
| 29 | + |
| 30 | + expect(find.byType(Container), findsOneWidget); |
| 31 | + }); |
| 32 | + |
| 33 | + testWidgets( |
| 34 | + 'constructor parameters are correctly passed to creation params', |
| 35 | + (WidgetTester tester) async { |
| 36 | + WebViewPlatform.instance = TestWebViewPlatform(); |
| 37 | + |
| 38 | + final MockPlatformWebViewController mockPlatformWebViewController = |
| 39 | + MockPlatformWebViewController(); |
| 40 | + final WebViewController webViewController = |
| 41 | + WebViewController.fromPlatform( |
| 42 | + mockPlatformWebViewController, |
| 43 | + ); |
| 44 | + |
| 45 | + final WebViewWidget webViewWidget = WebViewWidget( |
| 46 | + key: GlobalKey(), |
| 47 | + controller: webViewController, |
| 48 | + layoutDirection: TextDirection.rtl, |
| 49 | + gestureRecognizers: <Factory<OneSequenceGestureRecognizer>>{ |
| 50 | + Factory<OneSequenceGestureRecognizer>(() => EagerGestureRecognizer()), |
| 51 | + }, |
| 52 | + ); |
| 53 | + |
| 54 | + // The key passed to the default constructor is used by the super class |
| 55 | + // and not passed to the platform implementation. |
| 56 | + expect(webViewWidget.platform.params.key, isNull); |
| 57 | + expect( |
| 58 | + webViewWidget.platform.params.controller, |
| 59 | + webViewController.platform, |
| 60 | + ); |
| 61 | + expect(webViewWidget.platform.params.layoutDirection, TextDirection.rtl); |
| 62 | + expect( |
| 63 | + webViewWidget.platform.params.gestureRecognizers.isNotEmpty, |
| 64 | + isTrue, |
| 65 | + ); |
| 66 | + }); |
| 67 | + }); |
| 68 | +} |
| 69 | + |
| 70 | +class TestWebViewPlatform extends WebViewPlatform { |
| 71 | + @override |
| 72 | + PlatformWebViewWidget createPlatformWebViewWidget( |
| 73 | + PlatformWebViewWidgetCreationParams params, |
| 74 | + ) { |
| 75 | + return TestPlatformWebViewWidget(params); |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +class TestPlatformWebViewWidget extends PlatformWebViewWidget { |
| 80 | + TestPlatformWebViewWidget(PlatformWebViewWidgetCreationParams params) |
| 81 | + : super.implementation(params); |
| 82 | + |
| 83 | + @override |
| 84 | + Widget build(BuildContext context) { |
| 85 | + throw UnimplementedError(); |
| 86 | + } |
| 87 | +} |
0 commit comments