143 lines
4.0 KiB
Dart
143 lines
4.0 KiB
Dart
import 'dart:async';
|
|
|
|
import 'package:flutter/widgets.dart';
|
|
|
|
import 'dart:convert';
|
|
|
|
import 'package:webview_flutter_platform_interface/webview_flutter_platform_interface.dart';
|
|
|
|
class FakeSchoolPlatform extends WebViewPlatform {
|
|
@override
|
|
PlatformWebViewWidget createPlatformWebViewWidget(
|
|
PlatformWebViewWidgetCreationParams params,
|
|
) => FakeSchoolWidget(params);
|
|
int controllerCreations = 0;
|
|
late FakeSchoolWeb web;
|
|
late FakeSchoolDelegate delegate;
|
|
final cookies = FakeSchoolCookies();
|
|
@override
|
|
PlatformWebViewController createPlatformWebViewController(
|
|
PlatformWebViewControllerCreationParams params,
|
|
) {
|
|
controllerCreations++;
|
|
return web = FakeSchoolWeb(params);
|
|
}
|
|
|
|
@override
|
|
PlatformNavigationDelegate createPlatformNavigationDelegate(
|
|
PlatformNavigationDelegateCreationParams params,
|
|
) => delegate = FakeSchoolDelegate(params);
|
|
@override
|
|
PlatformWebViewCookieManager createPlatformCookieManager(
|
|
PlatformWebViewCookieManagerCreationParams params,
|
|
) => cookies;
|
|
}
|
|
|
|
class FakeSchoolWidget extends PlatformWebViewWidget {
|
|
FakeSchoolWidget(super.params) : super.implementation();
|
|
@override
|
|
Widget build(BuildContext context) =>
|
|
const SizedBox.expand(key: Key('fake-school-browser'));
|
|
}
|
|
|
|
class FakeSchoolWeb extends PlatformWebViewController {
|
|
FakeSchoolWeb(super.params) : super.implementation();
|
|
final loads = <String>[];
|
|
String? url;
|
|
String? documentUrl;
|
|
Future<Object> Function(String)? javascript;
|
|
Future<void> Function(Uri)? request;
|
|
@override
|
|
Future<void> setJavaScriptMode(JavaScriptMode mode) async {}
|
|
@override
|
|
Future<void> setPlatformNavigationDelegate(
|
|
PlatformNavigationDelegate handler,
|
|
) async {}
|
|
@override
|
|
Future<void> loadRequest(LoadRequestParams params) async {
|
|
url = params.uri.toString();
|
|
loads.add(url!);
|
|
await request?.call(params.uri);
|
|
}
|
|
|
|
@override
|
|
Future<String?> currentUrl() async => url;
|
|
@override
|
|
Future<Object> runJavaScriptReturningResult(String script) =>
|
|
script == 'location.href'
|
|
? Future.value(documentUrl ?? url ?? 'about:blank')
|
|
: javascript?.call(script) ?? Future.value(false);
|
|
@override
|
|
Future<void> clearCache() async {}
|
|
@override
|
|
Future<void> clearLocalStorage() async {}
|
|
@override
|
|
Future<void> loadHtmlString(String html, {String? baseUrl}) async {
|
|
url = 'about:blank';
|
|
loads.add('about:blank');
|
|
}
|
|
}
|
|
|
|
class FakeSchoolDelegate extends PlatformNavigationDelegate {
|
|
FakeSchoolDelegate(super.params) : super.implementation();
|
|
late PageEventCallback started, finished;
|
|
late WebResourceErrorCallback resourceError;
|
|
late NavigationRequestCallback navigation;
|
|
@override
|
|
Future<void> setOnNavigationRequest(
|
|
NavigationRequestCallback callback,
|
|
) async {
|
|
navigation = callback;
|
|
}
|
|
|
|
@override
|
|
Future<void> setOnPageStarted(PageEventCallback callback) async {
|
|
started = callback;
|
|
}
|
|
|
|
@override
|
|
Future<void> setOnPageFinished(PageEventCallback callback) async {
|
|
finished = callback;
|
|
}
|
|
|
|
@override
|
|
Future<void> setOnWebResourceError(WebResourceErrorCallback callback) async {
|
|
resourceError = callback;
|
|
}
|
|
|
|
@override
|
|
Future<void> setOnHttpError(HttpResponseErrorCallback callback) async {}
|
|
}
|
|
|
|
class FakeSchoolCookies extends PlatformWebViewCookieManager {
|
|
FakeSchoolCookies()
|
|
: super.implementation(const PlatformWebViewCookieManagerCreationParams());
|
|
Completer<bool>? clearing;
|
|
bool fail = false;
|
|
@override
|
|
Future<bool> clearCookies() => fail
|
|
? Future.error(StateError('native cookies'))
|
|
: clearing?.future ?? Future.value(true);
|
|
}
|
|
|
|
Future<void> flushSchoolEvents() async {
|
|
for (var i = 0; i < 12; i++) {
|
|
await Future<void>.delayed(Duration.zero);
|
|
}
|
|
}
|
|
|
|
String schoolPageEvidence({
|
|
required String topUrl,
|
|
bool authenticated = false,
|
|
String? html,
|
|
String? documentUrl,
|
|
String kind = 'timetable',
|
|
}) => jsonEncode({
|
|
'topUrl': topUrl,
|
|
'authenticated': authenticated,
|
|
'pages': [
|
|
if (html != null)
|
|
{'kind': kind, 'url': documentUrl ?? topUrl, 'html': html},
|
|
],
|
|
});
|