176 lines
5.4 KiB
Dart
176 lines
5.4 KiB
Dart
|
import 'dart:async';
|
||
|
import 'dart:convert';
|
||
|
|
||
|
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 programmaticScroll() {
|
||
|
final shouldSkip = !kIsWeb || ![
|
||
|
TargetPlatform.android,
|
||
|
TargetPlatform.iOS,
|
||
|
TargetPlatform.macOS,
|
||
|
].contains(defaultTargetPlatform);
|
||
|
|
||
|
group('Programmatic Scroll', () {
|
||
|
final shouldSkipTest1 = !kIsWeb || ![
|
||
|
TargetPlatform.android,
|
||
|
TargetPlatform.iOS,
|
||
|
TargetPlatform.macOS,
|
||
|
].contains(defaultTargetPlatform);
|
||
|
|
||
|
testWidgets('set and get scroll position', (WidgetTester tester) async {
|
||
|
final String scrollTestPage = '''
|
||
|
<!DOCTYPE html>
|
||
|
<html>
|
||
|
<head>
|
||
|
<style>
|
||
|
body {
|
||
|
height: 100%;
|
||
|
width: 100%;
|
||
|
}
|
||
|
#container{
|
||
|
width:5000px;
|
||
|
height:5000px;
|
||
|
}
|
||
|
</style>
|
||
|
</head>
|
||
|
<body>
|
||
|
<div id="container"/>
|
||
|
</body>
|
||
|
</html>
|
||
|
''';
|
||
|
|
||
|
final String scrollTestPageBase64 =
|
||
|
base64Encode(const Utf8Encoder().convert(scrollTestPage));
|
||
|
|
||
|
var url = !kIsWeb ? Uri.parse(
|
||
|
'data:text/html;charset=utf-8;base64,$scrollTestPageBase64') :
|
||
|
TEST_WEB_PLATFORM_URL_1;
|
||
|
|
||
|
final Completer<void> pageLoaded = Completer<void>();
|
||
|
final Completer controllerCompleter =
|
||
|
Completer<InAppWebViewController>();
|
||
|
|
||
|
await tester.pumpWidget(
|
||
|
Directionality(
|
||
|
textDirection: TextDirection.ltr,
|
||
|
child: InAppWebView(
|
||
|
initialUrlRequest: URLRequest(
|
||
|
url: url),
|
||
|
onWebViewCreated: (controller) {
|
||
|
controllerCompleter.complete(controller);
|
||
|
},
|
||
|
onLoadStop: (controller, url) {
|
||
|
pageLoaded.complete();
|
||
|
},
|
||
|
),
|
||
|
),
|
||
|
);
|
||
|
|
||
|
final InAppWebViewController controller =
|
||
|
await controllerCompleter.future;
|
||
|
await pageLoaded.future;
|
||
|
await controller.scrollTo(x: 0, y: 0);
|
||
|
|
||
|
await tester.pumpAndSettle(Duration(seconds: 3));
|
||
|
|
||
|
// Check scrollTo()
|
||
|
const int X_SCROLL = 123;
|
||
|
const int Y_SCROLL = 321;
|
||
|
|
||
|
await controller.scrollTo(x: X_SCROLL, y: Y_SCROLL);
|
||
|
await tester.pumpAndSettle(Duration(seconds: 2));
|
||
|
int? scrollPosX = await controller.getScrollX();
|
||
|
int? scrollPosY = await controller.getScrollY();
|
||
|
expect(scrollPosX, X_SCROLL);
|
||
|
expect(scrollPosY, Y_SCROLL);
|
||
|
|
||
|
// Check scrollBy() (on top of scrollTo())
|
||
|
await controller.scrollBy(x: X_SCROLL, y: Y_SCROLL);
|
||
|
await tester.pumpAndSettle(Duration(seconds: 2));
|
||
|
scrollPosX = await controller.getScrollX();
|
||
|
scrollPosY = await controller.getScrollY();
|
||
|
expect(scrollPosX, X_SCROLL * 2);
|
||
|
expect(scrollPosY, Y_SCROLL * 2);
|
||
|
}, skip: shouldSkipTest1);
|
||
|
|
||
|
testWidgets('set and get scroll position on Android without Hybrid Composition', (WidgetTester tester) async {
|
||
|
final String scrollTestPage = '''
|
||
|
<!DOCTYPE html>
|
||
|
<html>
|
||
|
<head>
|
||
|
<style>
|
||
|
body {
|
||
|
height: 100%;
|
||
|
width: 100%;
|
||
|
}
|
||
|
#container{
|
||
|
width:5000px;
|
||
|
height:5000px;
|
||
|
}
|
||
|
</style>
|
||
|
</head>
|
||
|
<body>
|
||
|
<div id="container"/>
|
||
|
</body>
|
||
|
</html>
|
||
|
''';
|
||
|
|
||
|
final String scrollTestPageBase64 =
|
||
|
base64Encode(const Utf8Encoder().convert(scrollTestPage));
|
||
|
|
||
|
final Completer<void> pageLoaded = Completer<void>();
|
||
|
final Completer controllerCompleter =
|
||
|
Completer<InAppWebViewController>();
|
||
|
|
||
|
await tester.pumpWidget(
|
||
|
Directionality(
|
||
|
textDirection: TextDirection.ltr,
|
||
|
child: InAppWebView(
|
||
|
initialUrlRequest: URLRequest(
|
||
|
url: Uri.parse(
|
||
|
'data:text/html;charset=utf-8;base64,$scrollTestPageBase64')),
|
||
|
onWebViewCreated: (controller) {
|
||
|
controllerCompleter.complete(controller);
|
||
|
},
|
||
|
initialSettings: InAppWebViewSettings(
|
||
|
useHybridComposition: false),
|
||
|
onLoadStop: (controller, url) {
|
||
|
pageLoaded.complete();
|
||
|
},
|
||
|
),
|
||
|
),
|
||
|
);
|
||
|
|
||
|
final InAppWebViewController controller =
|
||
|
await controllerCompleter.future;
|
||
|
await pageLoaded.future;
|
||
|
await controller.scrollTo(x: 0, y: 0);
|
||
|
|
||
|
await tester.pumpAndSettle(Duration(seconds: 3));
|
||
|
|
||
|
// Check scrollTo()
|
||
|
const int X_SCROLL = 123;
|
||
|
const int Y_SCROLL = 321;
|
||
|
|
||
|
await controller.scrollTo(x: X_SCROLL, y: Y_SCROLL);
|
||
|
await tester.pumpAndSettle(Duration(seconds: 2));
|
||
|
int? scrollPosX = await controller.getScrollX();
|
||
|
int? scrollPosY = await controller.getScrollY();
|
||
|
expect(scrollPosX, X_SCROLL);
|
||
|
expect(scrollPosY, Y_SCROLL);
|
||
|
|
||
|
// Check scrollBy() (on top of scrollTo())
|
||
|
await controller.scrollBy(x: X_SCROLL, y: Y_SCROLL);
|
||
|
await tester.pumpAndSettle(Duration(seconds: 2));
|
||
|
scrollPosX = await controller.getScrollX();
|
||
|
scrollPosY = await controller.getScrollY();
|
||
|
expect(scrollPosX, X_SCROLL * 2);
|
||
|
expect(scrollPosY, Y_SCROLL * 2);
|
||
|
}, skip: defaultTargetPlatform != TargetPlatform.android);
|
||
|
}, skip: shouldSkip);
|
||
|
}
|