Skip to content
This repository has been archived by the owner on Feb 22, 2023. It is now read-only.

[webview_flutter] Adds the loadFlutterAsset method to the interface. #4562

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 1.8.0

* Adds the `loadFlutterAsset` method to the platform interface.

## 1.7.0

* Add an option to set the background color of the webview.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,31 @@ class MethodChannelWebViewPlatform implements WebViewPlatformController {
@override
Future<void> loadFile(String absoluteFilePath) async {
assert(absoluteFilePath != null);
return _channel.invokeMethod<void>('loadFile', absoluteFilePath);

try {
return await _channel.invokeMethod<void>('loadFile', absoluteFilePath);
} on PlatformException catch (ex) {
if (ex.code == 'loadFile_failed') {
throw ArgumentError(ex.message);
}

rethrow;
}
}

@override
Future<void> loadFlutterAsset(String key) async {
assert(key.isNotEmpty);

try {
return await _channel.invokeMethod<void>('loadFlutterAsset', key);
} on PlatformException catch (ex) {
if (ex.code == 'loadFlutterAsset_invalidKey') {
throw ArgumentError(ex.message);
}

rethrow;
}
}

@override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,17 @@ abstract class WebViewPlatformController {
/// Throws an ArgumentError if the [absoluteFilePath] does not exist.
Future<void> loadFile(
String absoluteFilePath,
) {
throw UnimplementedError(
'WebView loadFile is not implemented on the current platform');
}

/// Loads the Flutter asset specified in the pubspec.yaml file.
///
/// Throws an ArgumentError if [key] is not part of the specified assets
/// in the pubspec.yaml file.
Future<void> loadFlutterAsset(
String key,
) {
throw UnimplementedError(
'WebView loadFlutterAsset is not implemented on the current platform');
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/master/packages/webview_flut
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: 1.7.0
version: 1.8.0

environment:
sdk: ">=2.12.0 <3.0.0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,35 @@ void main() {
case 'canGoBack':
case 'canGoForward':
return true;
case 'loadFile':
if (methodCall.arguments == 'invalid file') {
throw PlatformException(
code: 'loadFile_failed',
message: 'Failed loading file.',
details: null);
} else if (methodCall.arguments == 'some error') {
throw PlatformException(
code: 'some_error',
message: 'Some error occurred.',
details: null,
);
}
return null;
case 'loadFlutterAsset':
if (methodCall.arguments == 'invalid key') {
throw PlatformException(
code: 'loadFlutterAsset_invalidKey',
message: 'Failed loading asset.',
details: null,
);
} else if (methodCall.arguments == 'some error') {
throw PlatformException(
code: 'some_error',
message: 'Some error occurred.',
details: null,
);
}
return null;
case 'runJavascriptReturningResult':
case 'evaluateJavascript':
return methodCall.arguments as String;
Expand Down Expand Up @@ -74,6 +103,78 @@ void main() {
);
});

test('loadFile with invalid file', () async {
expect(
() => webViewPlatform.loadFile('invalid file'),
throwsA(
isA<ArgumentError>().having(
(ArgumentError error) => error.message,
'message',
'Failed loading file.',
),
),
);
});

test('loadFile with some error.', () async {
expect(
() => webViewPlatform.loadFile('some error'),
throwsA(
isA<PlatformException>().having(
(PlatformException error) => error.message,
'message',
'Some error occurred.',
),
),
);
});

test('loadFlutterAsset', () async {
await webViewPlatform.loadFlutterAsset(
'folder/asset.html',
);

expect(
log,
<Matcher>[
isMethodCall(
'loadFlutterAsset',
arguments: 'folder/asset.html',
),
],
);
});

test('loadFlutterAsset with empty key', () async {
expect(() => webViewPlatform.loadFlutterAsset(''), throwsAssertionError);
});

test('loadFlutterAsset with invalid key', () async {
expect(
() => webViewPlatform.loadFlutterAsset('invalid key'),
throwsA(
isA<ArgumentError>().having(
(ArgumentError error) => error.message,
'message',
'Failed loading asset.',
),
),
);
});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please also add tests for both that errors with different codes aren't remapped to ArgumentErrors.


test('loadFlutterAsset with some error.', () async {
expect(
() => webViewPlatform.loadFlutterAsset('some error'),
throwsA(
isA<PlatformException>().having(
(PlatformException error) => error.message,
'message',
'Some error occurred.',
),
),
);
});

test('loadHtmlString without base URL', () async {
await webViewPlatform.loadHtmlString(
'Test HTML string',
Expand Down