Skip to content

Commit

Permalink
[webview_flutter_platform_interface] Improves error message when `Web…
Browse files Browse the repository at this point in the history
…ViewPlatform.instance` is null (flutter#6938)

* add assertion

* formatting
  • Loading branch information
bparrishMines authored and mauricioluz committed Jan 26, 2023
1 parent 5da7853 commit e30e80e
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 2.0.1

* Improves error message when a platform interface class is used before `WebViewPlatform.instance` has been set.

## 2.0.0

* **Breaking Change**: Releases new interface. See [documentation](https://pub.dev/documentation/webview_flutter_platform_interface/2.0.0/) and [design doc](https://flutter.dev/go/webview_flutter_4_interface)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ abstract class PlatformNavigationDelegate extends PlatformInterface {
/// Creates a new [PlatformNavigationDelegate]
factory PlatformNavigationDelegate(
PlatformNavigationDelegateCreationParams params) {
assert(
WebViewPlatform.instance != null,
'A platform implementation for `webview_flutter` has not been set. Please '
'ensure that an implementation of `WebViewPlatform` has been set to '
'`WebViewPlatform.instance` before use. For unit testing, '
'`WebViewPlatform.instance` can be set with your own test implementation.',
);
final PlatformNavigationDelegate callbackDelegate =
WebViewPlatform.instance!.createPlatformNavigationDelegate(params);
PlatformInterface.verify(callbackDelegate, _token);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ abstract class PlatformWebViewController extends PlatformInterface {
/// Creates a new [PlatformWebViewController]
factory PlatformWebViewController(
PlatformWebViewControllerCreationParams params) {
assert(
WebViewPlatform.instance != null,
'A platform implementation for `webview_flutter` has not been set. Please '
'ensure that an implementation of `WebViewPlatform` has been set to '
'`WebViewPlatform.instance` before use. For unit testing, '
'`WebViewPlatform.instance` can be set with your own test implementation.',
);
final PlatformWebViewController webViewControllerDelegate =
WebViewPlatform.instance!.createPlatformWebViewController(params);
PlatformInterface.verify(webViewControllerDelegate, _token);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ abstract class PlatformWebViewCookieManager extends PlatformInterface {
/// Creates a new [PlatformWebViewCookieManager]
factory PlatformWebViewCookieManager(
PlatformWebViewCookieManagerCreationParams params) {
assert(
WebViewPlatform.instance != null,
'A platform implementation for `webview_flutter` has not been set. Please '
'ensure that an implementation of `WebViewPlatform` has been set to '
'`WebViewPlatform.instance` before use. For unit testing, '
'`WebViewPlatform.instance` can be set with your own test implementation.',
);
final PlatformWebViewCookieManager cookieManagerDelegate =
WebViewPlatform.instance!.createPlatformCookieManager(params);
PlatformInterface.verify(cookieManagerDelegate, _token);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ import 'webview_platform.dart';
abstract class PlatformWebViewWidget extends PlatformInterface {
/// Creates a new [PlatformWebViewWidget]
factory PlatformWebViewWidget(PlatformWebViewWidgetCreationParams params) {
assert(
WebViewPlatform.instance != null,
'A platform implementation for `webview_flutter` has not been set. Please '
'ensure that an implementation of `WebViewPlatform` has been set to '
'`WebViewPlatform.instance` before use. For unit testing, '
'`WebViewPlatform.instance` can be set with your own test implementation.',
);
final PlatformWebViewWidget webViewWidgetDelegate =
WebViewPlatform.instance!.createPlatformWebViewWidget(params);
PlatformInterface.verify(webViewWidgetDelegate, _token);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ repository: https://github.com/flutter/plugins/tree/main/packages/webview_flutte
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+webview_flutter%22
# NOTE: We strongly prefer non-breaking changes, even at the expense of a
# less-clean API. See https://flutter.dev/go/platform-interface-breaking-changes
version: 2.0.0
version: 2.0.1

environment:
sdk: ">=2.12.0 <3.0.0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,69 @@ void main() {
expect(WebViewPlatform.instance, isNull);
});

// This test can only run while `WebViewPlatform.instance` is still null.
test(
'Interface classes throw assertion error when `WebViewPlatform.instance` is null',
() {
expect(
() => PlatformNavigationDelegate(
const PlatformNavigationDelegateCreationParams(),
),
throwsA(isA<AssertionError>().having(
(AssertionError error) => error.message,
'message',
'A platform implementation for `webview_flutter` has not been set. Please '
'ensure that an implementation of `WebViewPlatform` has been set to '
'`WebViewPlatform.instance` before use. For unit testing, '
'`WebViewPlatform.instance` can be set with your own test implementation.',
)),
);

expect(
() => PlatformWebViewController(
const PlatformWebViewControllerCreationParams(),
),
throwsA(isA<AssertionError>().having(
(AssertionError error) => error.message,
'message',
'A platform implementation for `webview_flutter` has not been set. Please '
'ensure that an implementation of `WebViewPlatform` has been set to '
'`WebViewPlatform.instance` before use. For unit testing, '
'`WebViewPlatform.instance` can be set with your own test implementation.',
)),
);

expect(
() => PlatformWebViewCookieManager(
const PlatformWebViewCookieManagerCreationParams(),
),
throwsA(isA<AssertionError>().having(
(AssertionError error) => error.message,
'message',
'A platform implementation for `webview_flutter` has not been set. Please '
'ensure that an implementation of `WebViewPlatform` has been set to '
'`WebViewPlatform.instance` before use. For unit testing, '
'`WebViewPlatform.instance` can be set with your own test implementation.',
)),
);

expect(
() => PlatformWebViewWidget(
PlatformWebViewWidgetCreationParams(
controller: MockWebViewControllerDelegate(),
),
),
throwsA(isA<AssertionError>().having(
(AssertionError error) => error.message,
'message',
'A platform implementation for `webview_flutter` has not been set. Please '
'ensure that an implementation of `WebViewPlatform` has been set to '
'`WebViewPlatform.instance` before use. For unit testing, '
'`WebViewPlatform.instance` can be set with your own test implementation.',
)),
);
});

test('Cannot be implemented with `implements`', () {
expect(() {
WebViewPlatform.instance = ImplementsWebViewPlatform();
Expand Down

0 comments on commit e30e80e

Please sign in to comment.