updated macos tests, fixed macos/Classes/ISettings.swift, fixed macos NSColor.swift extension

This commit is contained in:
Lorenzo Pichilli 2022-10-19 04:40:44 +02:00
parent 486b3207a0
commit 69fb76d10c
18 changed files with 104 additions and 91 deletions

View File

@ -8,6 +8,7 @@
- Removed `PrintJobInfo.printerId` - Removed `PrintJobInfo.printerId`
- All `InAppWebViewSettings`, `InAppBrowserSettings` properties are optionals - All `InAppWebViewSettings`, `InAppBrowserSettings` properties are optionals
- `InAppBrowser.webViewController` can be null
## 6.0.0-beta.2 ## 6.0.0-beta.2

View File

@ -10,8 +10,7 @@ void customMenuItem() {
? true ? true
: ![ : ![
TargetPlatform.android, TargetPlatform.android,
TargetPlatform.iOS, TargetPlatform.iOS
TargetPlatform.macOS,
].contains(defaultTargetPlatform); ].contains(defaultTargetPlatform);
test('add custom menu item', () async { test('add custom menu item', () async {

View File

@ -8,7 +8,8 @@ import 'open_and_close.dart';
import 'trusted_web_activity.dart'; import 'trusted_web_activity.dart';
void main() { void main() {
const shouldSkip = kIsWeb; final shouldSkip =
kIsWeb || [TargetPlatform.macOS].contains(defaultTargetPlatform);
group('ChromeSafariBrowser', () { group('ChromeSafariBrowser', () {
openAndClose(); openAndClose();

View File

@ -10,8 +10,7 @@ void openAndClose() {
? true ? true
: ![ : ![
TargetPlatform.android, TargetPlatform.android,
TargetPlatform.iOS, TargetPlatform.iOS
TargetPlatform.macOS,
].contains(defaultTargetPlatform); ].contains(defaultTargetPlatform);
test('open and close', () async { test('open and close', () async {

View File

@ -21,25 +21,40 @@ void setGetDelete() {
final Completer<InAppWebViewController> controllerCompleter = final Completer<InAppWebViewController> controllerCompleter =
Completer<InAppWebViewController>(); Completer<InAppWebViewController>();
final Completer<String> pageLoaded = Completer<String>(); final Completer<String> pageLoaded = Completer<String>();
await tester.pumpWidget(
Directionality( var headlessWebView = new HeadlessInAppWebView(
textDirection: TextDirection.ltr, initialUrlRequest: URLRequest(url: TEST_CROSS_PLATFORM_URL_1),
child: InAppWebView( onWebViewCreated: (controller) {
key: GlobalKey(), controllerCompleter.complete(controller);
initialUrlRequest: URLRequest(url: TEST_CROSS_PLATFORM_URL_1), },
onWebViewCreated: (controller) {
controllerCompleter.complete(controller);
},
initialSettings: InAppWebViewSettings(
clearCache: true,
),
onLoadStop: (controller, url) {
pageLoaded.complete(url!.toString());
},
),
),
); );
if (defaultTargetPlatform == TargetPlatform.macOS) {
headlessWebView.onLoadStop = (controller, url) async {
pageLoaded.complete(url!.toString());
};
await headlessWebView.run();
} else {
await tester.pumpWidget(
Directionality(
textDirection: TextDirection.ltr,
child: InAppWebView(
key: GlobalKey(),
initialUrlRequest: URLRequest(url: TEST_CROSS_PLATFORM_URL_1),
onWebViewCreated: (controller) {
controllerCompleter.complete(controller);
},
initialSettings: InAppWebViewSettings(
clearCache: true,
),
onLoadStop: (controller, url) {
pageLoaded.complete(url!.toString());
},
),
),
);
}
final url = Uri.parse(await pageLoaded.future); final url = Uri.parse(await pageLoaded.future);
await cookieManager.setCookie(url: url, name: "myCookie", value: "myValue"); await cookieManager.setCookie(url: url, name: "myCookie", value: "myValue");
@ -57,5 +72,9 @@ void setGetDelete() {
url: url, domain: ".${TEST_CROSS_PLATFORM_URL_1.host}"); url: url, domain: ".${TEST_CROSS_PLATFORM_URL_1.host}");
cookies = await cookieManager.getCookies(url: url); cookies = await cookieManager.getCookies(url: url);
expect(cookies, isEmpty); expect(cookies, isEmpty);
if (defaultTargetPlatform == TargetPlatform.macOS) {
headlessWebView.dispose();
}
}, skip: shouldSkip); }, skip: shouldSkip);
} }

View File

@ -4,7 +4,8 @@ import 'package:flutter_test/flutter_test.dart';
import 'find_interactions.dart'; import 'find_interactions.dart';
void main() { void main() {
final shouldSkip = kIsWeb; final shouldSkip =
kIsWeb || [TargetPlatform.macOS].contains(defaultTargetPlatform);
group('FindInteractionController', () { group('FindInteractionController', () {
findInteractions(); findInteractions();

View File

@ -1,5 +1,4 @@
import 'package:flutter/foundation.dart'; import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';
import 'package:flutter_inappwebview/flutter_inappwebview.dart'; import 'package:flutter_inappwebview/flutter_inappwebview.dart';
import 'package:flutter_test/flutter_test.dart'; import 'package:flutter_test/flutter_test.dart';
@ -52,12 +51,12 @@ void openDataAndClose() {
await inAppBrowser.firstPageLoaded.future; await inAppBrowser.firstPageLoaded.future;
var controller = inAppBrowser.webViewController; var controller = inAppBrowser.webViewController;
final String? url = (await controller.getUrl())?.toString(); expect(controller, isNotNull);
final String? url = (await controller!.getUrl())?.toString();
expect(url, TEST_CROSS_PLATFORM_URL_1.toString()); expect(url, TEST_CROSS_PLATFORM_URL_1.toString());
await inAppBrowser.close(); await inAppBrowser.close();
expect(inAppBrowser.isOpened(), false); expect(inAppBrowser.isOpened(), false);
expect(() async => await inAppBrowser.webViewController.getUrl(), expect(inAppBrowser.webViewController, isNull);
throwsA(isInstanceOf<MissingPluginException>()));
}, skip: shouldSkip); }, skip: shouldSkip);
} }

View File

@ -1,5 +1,4 @@
import 'package:flutter/foundation.dart'; import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';
import 'package:flutter_inappwebview/flutter_inappwebview.dart'; import 'package:flutter_inappwebview/flutter_inappwebview.dart';
import 'package:flutter_test/flutter_test.dart'; import 'package:flutter_test/flutter_test.dart';
@ -34,12 +33,12 @@ void openFileAndClose() {
await inAppBrowser.firstPageLoaded.future; await inAppBrowser.firstPageLoaded.future;
var controller = inAppBrowser.webViewController; var controller = inAppBrowser.webViewController;
final String? url = (await controller.getUrl())?.toString(); expect(controller, isNotNull);
final String? url = (await controller!.getUrl())?.toString();
expect(url, endsWith("in_app_webview_initial_file_test.html")); expect(url, endsWith("in_app_webview_initial_file_test.html"));
await inAppBrowser.close(); await inAppBrowser.close();
expect(inAppBrowser.isOpened(), false); expect(inAppBrowser.isOpened(), false);
expect(() async => await inAppBrowser.webViewController.getUrl(), expect(inAppBrowser.webViewController, isNull);
throwsA(isInstanceOf<MissingPluginException>()));
}, skip: shouldSkip); }, skip: shouldSkip);
} }

View File

@ -1,5 +1,4 @@
import 'package:flutter/foundation.dart'; import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';
import 'package:flutter_inappwebview/flutter_inappwebview.dart'; import 'package:flutter_inappwebview/flutter_inappwebview.dart';
import 'package:flutter_test/flutter_test.dart'; import 'package:flutter_test/flutter_test.dart';
@ -33,12 +32,12 @@ void openUrlAndClose() {
await inAppBrowser.firstPageLoaded.future; await inAppBrowser.firstPageLoaded.future;
var controller = inAppBrowser.webViewController; var controller = inAppBrowser.webViewController;
final String? url = (await controller.getUrl())?.toString(); expect(controller, isNotNull);
final String? url = (await controller!.getUrl())?.toString();
expect(url, TEST_URL_1.toString()); expect(url, TEST_URL_1.toString());
await inAppBrowser.close(); await inAppBrowser.close();
expect(inAppBrowser.isOpened(), false); expect(inAppBrowser.isOpened(), false);
expect(() async => await inAppBrowser.webViewController.getUrl(), expect(inAppBrowser.webViewController, isNull);
throwsA(isInstanceOf<MissingPluginException>()));
}, skip: shouldSkip); }, skip: shouldSkip);
} }

View File

@ -1,3 +1,4 @@
import 'package:flutter/foundation.dart';
import 'package:flutter_test/flutter_test.dart'; import 'package:flutter_test/flutter_test.dart';
import 'apple_pay_api.dart'; import 'apple_pay_api.dart';
@ -84,6 +85,8 @@ import 'web_message.dart';
import 'webview_windows.dart'; import 'webview_windows.dart';
void main() { void main() {
final shouldSkip = [TargetPlatform.macOS].contains(defaultTargetPlatform);
group('InAppWebView', () { group('InAppWebView', () {
initialUrlRequest(); initialUrlRequest();
setGetSettings(); setGetSettings();
@ -167,5 +170,5 @@ void main() {
createPdf(); createPdf();
applePayAPI(); applePayAPI();
handlesURLScheme(); handlesURLScheme();
}); }, skip: shouldSkip);
} }

View File

@ -1,7 +1,6 @@
import 'dart:async'; import 'dart:async';
import 'package:flutter/foundation.dart'; import 'package:flutter/foundation.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_inappwebview/flutter_inappwebview.dart'; import 'package:flutter_inappwebview/flutter_inappwebview.dart';
import 'package:flutter_test/flutter_test.dart'; import 'package:flutter_test/flutter_test.dart';
@ -21,27 +20,23 @@ void takeScreenshot() {
Completer<InAppWebViewController>(); Completer<InAppWebViewController>();
final Completer<void> pageLoaded = Completer<void>(); final Completer<void> pageLoaded = Completer<void>();
await tester.pumpWidget( var headlessWebView = new HeadlessInAppWebView(
Directionality( initialUrlRequest: URLRequest(url: TEST_CROSS_PLATFORM_URL_1),
textDirection: TextDirection.ltr, onWebViewCreated: (controller) {
child: InAppWebView( controllerCompleter.complete(controller);
key: GlobalKey(), },
initialUrlRequest: URLRequest(url: TEST_CROSS_PLATFORM_URL_1),
onWebViewCreated: (controller) {
controllerCompleter.complete(controller);
},
onLoadStop: (controller, url) {
pageLoaded.complete();
},
),
),
); );
headlessWebView.onLoadStop = (controller, url) async {
pageLoaded.complete();
};
await headlessWebView.run();
expect(headlessWebView.isRunning(), true);
final InAppWebViewController controller = await controllerCompleter.future; final InAppWebViewController controller = await controllerCompleter.future;
await pageLoaded.future; await pageLoaded.future;
await Future.delayed(Duration(seconds: 1)); await Future.delayed(Duration(seconds: 1));
await tester.pump();
var screenshotConfiguration = ScreenshotConfiguration( var screenshotConfiguration = ScreenshotConfiguration(
compressFormat: CompressFormat.JPEG, compressFormat: CompressFormat.JPEG,

View File

@ -33,13 +33,13 @@ void main() {
FindInteractionController.debugLoggingSettings.usePrint = true; FindInteractionController.debugLoggingSettings.usePrint = true;
FindInteractionController.debugLoggingSettings.maxLogMessageLength = 7000; FindInteractionController.debugLoggingSettings.maxLogMessageLength = 7000;
in_app_webview_tests.main(); // in_app_webview_tests.main();
find_interaction_controller_tests.main(); // find_interaction_controller_tests.main();
service_worker_controller_tests.main(); service_worker_controller_tests.main();
proxy_controller_tests.main(); proxy_controller_tests.main();
headless_in_app_webview_tests.main(); headless_in_app_webview_tests.main();
cookie_manager_tests.main(); cookie_manager_tests.main();
in_app_browser_tests.main(); in_app_browser_tests.main();
chrome_safari_browser_tests.main(); // chrome_safari_browser_tests.main();
in_app_localhost_server_tests.main(); in_app_localhost_server_tests.main();
} }

View File

@ -3,12 +3,11 @@
export "FLUTTER_ROOT=/Users/lorenzopichilli/fvm/versions/2.10.4" export "FLUTTER_ROOT=/Users/lorenzopichilli/fvm/versions/2.10.4"
export "FLUTTER_APPLICATION_PATH=/Users/lorenzopichilli/Desktop/flutter_inappwebview/example" export "FLUTTER_APPLICATION_PATH=/Users/lorenzopichilli/Desktop/flutter_inappwebview/example"
export "COCOAPODS_PARALLEL_CODE_SIGN=true" export "COCOAPODS_PARALLEL_CODE_SIGN=true"
export "FLUTTER_TARGET=/Users/lorenzopichilli/Desktop/flutter_inappwebview/example/lib/main.dart" export "FLUTTER_TARGET=lib/main.dart"
export "FLUTTER_BUILD_DIR=build" export "FLUTTER_BUILD_DIR=build"
export "FLUTTER_BUILD_NAME=1.0.0" export "FLUTTER_BUILD_NAME=1.0.0"
export "FLUTTER_BUILD_NUMBER=1" export "FLUTTER_BUILD_NUMBER=1"
export "DART_DEFINES=Zmx1dHRlci5pbnNwZWN0b3Iuc3RydWN0dXJlZEVycm9ycz10cnVl,RkxVVFRFUl9XRUJfQVVUT19ERVRFQ1Q9dHJ1ZQ=="
export "DART_OBFUSCATION=false" export "DART_OBFUSCATION=false"
export "TRACK_WIDGET_CREATION=true" export "TRACK_WIDGET_CREATION=true"
export "TREE_SHAKE_ICONS=false" export "TREE_SHAKE_ICONS=false"
export "PACKAGE_CONFIG=/Users/lorenzopichilli/Desktop/flutter_inappwebview/example/.dart_tool/package_config.json" export "PACKAGE_CONFIG=.dart_tool/package_config.json"

View File

@ -76,11 +76,11 @@ class _InAppBrowserExampleScreenState extends State<InAppBrowserExampleScreen> {
), ),
onRefresh: () async { onRefresh: () async {
if (Platform.isAndroid) { if (Platform.isAndroid) {
widget.browser.webViewController.reload(); widget.browser.webViewController?.reload();
} else if (Platform.isIOS) { } else if (Platform.isIOS) {
widget.browser.webViewController.loadUrl( widget.browser.webViewController?.loadUrl(
urlRequest: URLRequest( urlRequest: URLRequest(
url: await widget.browser.webViewController.getUrl())); url: await widget.browser.webViewController?.getUrl()));
} }
}, },
); );

View File

@ -73,8 +73,12 @@ class InAppBrowser {
static const MethodChannel _sharedChannel = static const MethodChannel _sharedChannel =
const MethodChannel('com.pichillilorenzo/flutter_inappbrowser'); const MethodChannel('com.pichillilorenzo/flutter_inappbrowser');
/// WebView Controller that can be used to access the [InAppWebViewController] API. InAppWebViewController? _webViewController;
late final InAppWebViewController webViewController; ///WebView Controller that can be used to access the [InAppWebViewController] API.
///When [onExit] is fired, this will be `null` and cannot be used anymore.
InAppWebViewController? get webViewController {
return _webViewController;
}
///The window id of a [CreateWindowAction.windowId]. ///The window id of a [CreateWindowAction.windowId].
final int? windowId; final int? windowId;
@ -99,7 +103,7 @@ class InAppBrowser {
} }
}); });
_isOpened = false; _isOpened = false;
webViewController = new InAppWebViewController.fromInAppBrowser( _webViewController = new InAppWebViewController.fromInAppBrowser(
this._channel, this, this.initialUserScripts); this._channel, this, this.initialUserScripts);
} }
@ -139,10 +143,11 @@ class InAppBrowser {
case "onExit": case "onExit":
_debugLog(call.method, call.arguments); _debugLog(call.method, call.arguments);
this._isOpened = false; this._isOpened = false;
this._webViewController = null;
onExit(); onExit();
break; break;
default: default:
return webViewController.handleMethod(call); return _webViewController?.handleMethod(call);
} }
} }

View File

@ -29,17 +29,12 @@ public class ISettings<T>: NSObject {
func toMap() -> [String: Any?] { func toMap() -> [String: Any?] {
var settings: [String: Any?] = [:] var settings: [String: Any?] = [:]
var counts = UInt32() let mirrored_object = Mirror(reflecting: self)
let properties = class_copyPropertyList(object_getClass(self), &counts) for (_, attr) in mirrored_object.children.enumerated() {
for i in 0..<counts { if let property_name = attr.label as String? {
if let property = properties?.advanced(by: Int(i)).pointee { settings[property_name] = attr.value
let cName = property_getName(property)
let name = String(cString: cName)
let key = !name.hasPrefix("_") ? name : String(name.suffix(from: name.index(name.startIndex, offsetBy: 1)))
settings[key] = self.value(forKey: key)
} }
} }
free(properties)
return settings return settings
} }

View File

@ -85,8 +85,8 @@ public class InAppWebViewSettings: ISettings<InAppWebView> {
realSettings["isFraudulentWebsiteWarningEnabled"] = configuration.preferences.isFraudulentWebsiteWarningEnabled realSettings["isFraudulentWebsiteWarningEnabled"] = configuration.preferences.isFraudulentWebsiteWarningEnabled
realSettings["preferredContentMode"] = configuration.defaultWebpagePreferences.preferredContentMode.rawValue realSettings["preferredContentMode"] = configuration.defaultWebpagePreferences.preferredContentMode.rawValue
} }
realSettings["allowUniversalAccessFromFileURLs"] = configuration.value(forKey: "allowUniversalAccessFromFileURLs") realSettings["allowUniversalAccessFromFileURLs"] = configuration.value(forKey: "allowUniversalAccessFromFileURLs") as? Bool
realSettings["allowFileAccessFromFileURLs"] = configuration.preferences.value(forKey: "allowFileAccessFromFileURLs") realSettings["allowFileAccessFromFileURLs"] = configuration.preferences.value(forKey: "allowFileAccessFromFileURLs") as? Bool
realSettings["javaScriptEnabled"] = configuration.preferences.javaScriptEnabled realSettings["javaScriptEnabled"] = configuration.preferences.javaScriptEnabled
if #available(macOS 11.0, *) { if #available(macOS 11.0, *) {
realSettings["mediaType"] = webView.mediaType realSettings["mediaType"] = webView.mediaType

View File

@ -27,30 +27,29 @@ extension NSColor {
} }
var hexString: String? { var hexString: String? {
var red: CGFloat = 0 guard let rgbColor = usingColorSpace(.sRGB) else {
var green: CGFloat = 0 return "#FFFFFF"
var blue: CGFloat = 0 }
var alpha: CGFloat = 0 var red: CGFloat = rgbColor.redComponent
var green: CGFloat = rgbColor.greenComponent
let multiplier = CGFloat(255.999999) var blue: CGFloat = rgbColor.blueComponent
var alpha: CGFloat = rgbColor.alphaComponent
self.getRed(&red, green: &green, blue: &blue, alpha: &alpha)
if alpha == 1.0 { if alpha == 1.0 {
return String( return String(
format: "#%02lX%02lX%02lX", format: "#%02lX%02lX%02lX",
Int(red * multiplier), Int(red * 0xFF),
Int(green * multiplier), Int(green * 0xFF),
Int(blue * multiplier) Int(blue * 0xFF)
) )
} }
else { else {
return String( return String(
format: "#%02lX%02lX%02lX%02lX", format: "#%02lX%02lX%02lX%02lX",
Int(red * multiplier), Int(red * 0xFF),
Int(green * multiplier), Int(green * 0xFF),
Int(blue * multiplier), Int(blue * 0xFF),
Int(alpha * multiplier) Int(alpha * 0xFF)
) )
} }
} }