Skip to content

Commit

Permalink
[webview_flutter] Migrate webview_flutter package to analysis_options…
Browse files Browse the repository at this point in the history
….yaml (flutter#4552)
  • Loading branch information
mvanbeusekom authored Nov 30, 2021
1 parent cd7fae9 commit e345ef4
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 92 deletions.
1 change: 1 addition & 0 deletions packages/webview_flutter/webview_flutter/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

* Updates example app Android compileSdkVersion to 31.
* Integration test fixes.
* Updates code for new analysis options.

## 2.3.1

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ void main() {
);

resizeButtonTapped = true;
await tester.tap(find.byKey(const ValueKey('resizeButton')));
await tester.tap(find.byKey(const ValueKey<String>('resizeButton')));
await tester.pumpAndSettle();
expect(buttonTapResizeCompleter.future, completes);
});
Expand Down Expand Up @@ -460,10 +460,10 @@ void main() {

testWidgets('Video plays inline when allowsInlineMediaPlayback is true',
(WidgetTester tester) async {
Completer<WebViewController> controllerCompleter =
final Completer<WebViewController> controllerCompleter =
Completer<WebViewController>();
Completer<void> pageLoaded = Completer<void>();
Completer<void> videoPlaying = Completer<void>();
final Completer<void> pageLoaded = Completer<void>();
final Completer<void> videoPlaying = Completer<void>();

await tester.pumpWidget(
Directionality(
Expand Down Expand Up @@ -494,7 +494,7 @@ void main() {
),
),
);
WebViewController controller = await controllerCompleter.future;
final WebViewController controller = await controllerCompleter.future;
await pageLoaded.future;

// Pump once to trigger the video play.
Expand All @@ -503,7 +503,7 @@ void main() {
// Makes sure we get the correct event that indicates the video is actually playing.
await videoPlaying.future;

String fullScreen =
final String fullScreen =
await controller.runJavascriptReturningResult('isFullScreen();');
expect(fullScreen, _webviewBool(false));
});
Expand All @@ -512,10 +512,10 @@ void main() {
testWidgets(
'Video plays full screen when allowsInlineMediaPlayback is false',
(WidgetTester tester) async {
Completer<WebViewController> controllerCompleter =
final Completer<WebViewController> controllerCompleter =
Completer<WebViewController>();
Completer<void> pageLoaded = Completer<void>();
Completer<void> videoPlaying = Completer<void>();
final Completer<void> pageLoaded = Completer<void>();
final Completer<void> videoPlaying = Completer<void>();

await tester.pumpWidget(
Directionality(
Expand Down Expand Up @@ -546,7 +546,7 @@ void main() {
),
),
);
WebViewController controller = await controllerCompleter.future;
final WebViewController controller = await controllerCompleter.future;
await pageLoaded.future;

// Pump once to trigger the video play.
Expand All @@ -555,7 +555,7 @@ void main() {
// Makes sure we get the correct event that indicates the video is actually playing.
await videoPlaying.future;

String fullScreen =
final String fullScreen =
await controller.runJavascriptReturningResult('isFullScreen();');
expect(fullScreen, _webviewBool(true));
}, skip: Platform.isAndroid);
Expand Down Expand Up @@ -733,7 +733,7 @@ void main() {
});

testWidgets('getTitle', (WidgetTester tester) async {
final String getTitleTest = '''
const String getTitleTest = '''
<!DOCTYPE html><html>
<head><title>Some title</title>
</head>
Expand Down Expand Up @@ -784,7 +784,7 @@ void main() {
group('Programmatic Scroll', () {
// TODO(bparrishMines): skipped due to https://github.com/flutter/flutter/issues/86757.
testWidgets('setAndGetScrollPosition', (WidgetTester tester) async {
final String scrollTestPage = '''
const String scrollTestPage = '''
<!DOCTYPE html>
<html>
<head>
Expand Down Expand Up @@ -831,7 +831,7 @@ void main() {
final WebViewController controller = await controllerCompleter.future;
await pageLoaded.future;

await tester.pumpAndSettle(Duration(seconds: 3));
await tester.pumpAndSettle(const Duration(seconds: 3));

int scrollPosX = await controller.getScrollX();
int scrollPosY = await controller.getScrollY();
Expand Down Expand Up @@ -871,7 +871,7 @@ void main() {

// TODO(bparrishMines): skipped due to https://github.com/flutter/flutter/issues/86757.
testWidgets('setAndGetScrollPosition', (WidgetTester tester) async {
final String scrollTestPage = '''
const String scrollTestPage = '''
<!DOCTYPE html>
<html>
<head>
Expand Down Expand Up @@ -918,7 +918,7 @@ void main() {
final WebViewController controller = await controllerCompleter.future;
await pageLoaded.future;

await tester.pumpAndSettle(Duration(seconds: 3));
await tester.pumpAndSettle(const Duration(seconds: 3));

// Check scrollTo()
const int X_SCROLL = 123;
Expand All @@ -941,7 +941,7 @@ void main() {
// TODO(bparrishMines): skipped due to https://github.com/flutter/flutter/issues/86757.
testWidgets('inputs are scrolled into view when focused',
(WidgetTester tester) async {
final String scrollTestPage = '''
const String scrollTestPage = '''
<!DOCTYPE html>
<html>
<head>
Expand Down Expand Up @@ -993,7 +993,7 @@ void main() {
),
),
);
await Future.delayed(Duration(milliseconds: 20));
await Future<dynamic>.delayed(const Duration(milliseconds: 20));
await tester.pump();
});

Expand All @@ -1002,15 +1002,15 @@ void main() {
final String viewportRectJSON = await _runJavascriptReturningResult(
controller, 'JSON.stringify(viewport.getBoundingClientRect())');
final Map<String, dynamic> viewportRectRelativeToViewport =
jsonDecode(viewportRectJSON);
jsonDecode(viewportRectJSON) as Map<String, dynamic>;

// Check that the input is originally outside of the viewport.

final String initialInputClientRectJSON =
await _runJavascriptReturningResult(
controller, 'JSON.stringify(inputEl.getBoundingClientRect())');
final Map<String, dynamic> initialInputClientRectRelativeToViewport =
jsonDecode(initialInputClientRectJSON);
jsonDecode(initialInputClientRectJSON) as Map<String, dynamic>;

expect(
initialInputClientRectRelativeToViewport['bottom'] <=
Expand All @@ -1025,7 +1025,7 @@ void main() {
await _runJavascriptReturningResult(
controller, 'JSON.stringify(inputEl.getBoundingClientRect())');
final Map<String, dynamic> lastInputClientRectRelativeToViewport =
jsonDecode(lastInputClientRectJSON);
jsonDecode(lastInputClientRectJSON) as Map<String, dynamic>;

expect(
lastInputClientRectRelativeToViewport['top'] >=
Expand All @@ -1048,7 +1048,7 @@ void main() {
});

group('NavigationDelegate', () {
final String blankPage = "<!DOCTYPE html><head></head><body></body></html>";
const String blankPage = '<!DOCTYPE html><head></head><body></body></html>';
final String blankPageEncoded = 'data:text/html;charset=utf-8;base64,' +
base64Encode(const Utf8Encoder().convert(blankPage));

Expand Down Expand Up @@ -1144,7 +1144,7 @@ void main() {
testWidgets(
'onWebResourceError only called for main frame',
(WidgetTester tester) async {
final String iframeTest = '''
const String iframeTest = '''
<!DOCTYPE html>
<html>
<head>
Expand Down Expand Up @@ -1357,7 +1357,7 @@ void main() {
testWidgets(
'JavaScript does not run in parent window',
(WidgetTester tester) async {
final String iframe = '''
const String iframe = '''
<!DOCTYPE html>
<script>
window.onload = () => {
Expand Down Expand Up @@ -1443,11 +1443,13 @@ Future<String> _runJavascriptReturningResult(
if (defaultTargetPlatform == TargetPlatform.iOS) {
return await controller.runJavascriptReturningResult(js);
}
return jsonDecode(await controller.runJavascriptReturningResult(js));
return jsonDecode(await controller.runJavascriptReturningResult(js))
as String;
}

class ResizableWebView extends StatefulWidget {
ResizableWebView({required this.onResize, required this.onPageFinished});
const ResizableWebView(
{required this.onResize, required this.onPageFinished});

final JavascriptMessageHandler onResize;
final VoidCallback onPageFinished;
Expand Down Expand Up @@ -1502,14 +1504,14 @@ class ResizableWebViewState extends State<ResizableWebView> {
),
),
TextButton(
key: Key('resizeButton'),
key: const Key('resizeButton'),
onPressed: () {
setState(() {
webViewWidth += 100.0;
webViewHeight += 100.0;
});
},
child: Text('ResizeButton'),
child: const Text('ResizeButton'),
),
],
),
Expand Down
29 changes: 17 additions & 12 deletions packages/webview_flutter/webview_flutter/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ class _WebViewExampleState extends State<WebViewExample> {
@override
void initState() {
super.initState();
if (Platform.isAndroid) WebView.platform = SurfaceAndroidWebView();
if (Platform.isAndroid) {
WebView.platform = SurfaceAndroidWebView();
}
}

@override
Expand All @@ -64,7 +66,7 @@ class _WebViewExampleState extends State<WebViewExample> {
_controller.complete(webViewController);
},
onProgress: (int progress) {
print("WebView is loading (progress : $progress%)");
print('WebView is loading (progress : $progress%)');
},
javascriptChannels: <JavascriptChannel>{
_toasterJavascriptChannel(context),
Expand Down Expand Up @@ -207,15 +209,15 @@ class SampleMenu extends StatelessWidget {
);
}

void _onShowUserAgent(
Future<void> _onShowUserAgent(
WebViewController controller, BuildContext context) async {
// Send a message with the user agent string to the Toaster JavaScript channel we registered
// with the WebView.
await controller.runJavascript(
'Toaster.postMessage("User Agent: " + navigator.userAgent);');
}

void _onListCookies(
Future<void> _onListCookies(
WebViewController controller, BuildContext context) async {
final String cookies =
await controller.runJavascriptReturningResult('document.cookie');
Expand All @@ -232,7 +234,8 @@ class SampleMenu extends StatelessWidget {
));
}

void _onAddToCache(WebViewController controller, BuildContext context) async {
Future<void> _onAddToCache(
WebViewController controller, BuildContext context) async {
await controller.runJavascript(
'caches.open("test_caches_entry"); localStorage["test_localStorage"] = "dummy_entry";');
// ignore: deprecated_member_use
Expand All @@ -241,21 +244,23 @@ class SampleMenu extends StatelessWidget {
));
}

void _onListCache(WebViewController controller, BuildContext context) async {
Future<void> _onListCache(
WebViewController controller, BuildContext context) async {
await controller.runJavascript('caches.keys()'
'.then((cacheKeys) => JSON.stringify({"cacheKeys" : cacheKeys, "localStorage" : localStorage}))'
'.then((caches) => Toaster.postMessage(caches))');
}

void _onClearCache(WebViewController controller, BuildContext context) async {
Future<void> _onClearCache(
WebViewController controller, BuildContext context) async {
await controller.clearCache();
// ignore: deprecated_member_use
Scaffold.of(context).showSnackBar(const SnackBar(
content: Text("Cache cleared."),
content: Text('Cache cleared.'),
));
}

void _onClearCookies(BuildContext context) async {
Future<void> _onClearCookies(BuildContext context) async {
final bool hadCookies = await cookieManager.clearCookies();
String message = 'There were cookies. Now, they are gone!';
if (!hadCookies) {
Expand All @@ -267,7 +272,7 @@ class SampleMenu extends StatelessWidget {
));
}

void _onNavigationDelegateExample(
Future<void> _onNavigationDelegateExample(
WebViewController controller, BuildContext context) async {
final String contentBase64 =
base64Encode(const Utf8Encoder().convert(kNavigationExamplePage));
Expand Down Expand Up @@ -316,7 +321,7 @@ class NavigationControls extends StatelessWidget {
} else {
// ignore: deprecated_member_use
Scaffold.of(context).showSnackBar(
const SnackBar(content: Text("No back history item")),
const SnackBar(content: Text('No back history item')),
);
return;
}
Expand All @@ -333,7 +338,7 @@ class NavigationControls extends StatelessWidget {
// ignore: deprecated_member_use
Scaffold.of(context).showSnackBar(
const SnackBar(
content: Text("No forward history item")),
content: Text('No forward history item')),
);
return;
}
Expand Down
4 changes: 2 additions & 2 deletions packages/webview_flutter/webview_flutter/example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ dependencies:

dev_dependencies:
espresso: ^0.1.0+2
flutter_test:
sdk: flutter
flutter_driver:
sdk: flutter
flutter_test:
sdk: flutter
integration_test:
sdk: flutter
pedantic: ^1.10.0
Expand Down
Loading

0 comments on commit e345ef4

Please sign in to comment.