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

Commit

Permalink
[webview_flutter_wkwebview] Update copy method for Dart classes and s…
Browse files Browse the repository at this point in the history
…upport the `NSObject.observeValue` for subclasses (#5961)
  • Loading branch information
bparrishMines authored Jun 22, 2022
1 parent df6260e commit f62b237
Show file tree
Hide file tree
Showing 34 changed files with 895 additions and 513 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,9 @@ - (id)mockFlutterApiWithManager:(FWFInstanceManager *)instanceManager {

- (void)testCreateWithIdentifier {
FWFInstanceManager *instanceManager = [[FWFInstanceManager alloc] init];
FWFScriptMessageHandlerHostApiImpl *hostAPI =
[[FWFScriptMessageHandlerHostApiImpl alloc] initWithInstanceManager:instanceManager];
FWFScriptMessageHandlerHostApiImpl *hostAPI = [[FWFScriptMessageHandlerHostApiImpl alloc]
initWithBinaryMessenger:OCMProtocolMock(@protocol(FlutterBinaryMessenger))
instanceManager:instanceManager];

FlutterError *error;
[hostAPI createWithIdentifier:@0 error:&error];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,9 @@ - (id)mockFlutterApiWithManager:(FWFInstanceManager *)instanceManager {

- (void)testCreateWithIdentifier {
FWFInstanceManager *instanceManager = [[FWFInstanceManager alloc] init];
FWFUIDelegateHostApiImpl *hostAPI =
[[FWFUIDelegateHostApiImpl alloc] initWithInstanceManager:instanceManager];
FWFUIDelegateHostApiImpl *hostAPI = [[FWFUIDelegateHostApiImpl alloc]
initWithBinaryMessenger:OCMProtocolMock(@protocol(FlutterBinaryMessenger))
instanceManager:instanceManager];

FlutterError *error;
[hostAPI createWithIdentifier:@0 error:&error];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ @interface FWFWebViewConfigurationHostApiTests : XCTestCase
@implementation FWFWebViewConfigurationHostApiTests
- (void)testCreateWithIdentifier {
FWFInstanceManager *instanceManager = [[FWFInstanceManager alloc] init];
FWFWebViewConfigurationHostApiImpl *hostAPI =
[[FWFWebViewConfigurationHostApiImpl alloc] initWithInstanceManager:instanceManager];
FWFWebViewConfigurationHostApiImpl *hostAPI = [[FWFWebViewConfigurationHostApiImpl alloc]
initWithBinaryMessenger:OCMProtocolMock(@protocol(FlutterBinaryMessenger))
instanceManager:instanceManager];

FlutterError *error;
[hostAPI createWithIdentifier:@0 error:&error];
Expand All @@ -27,8 +28,9 @@ - (void)testCreateWithIdentifier {

- (void)testCreateFromWebViewWithIdentifier {
FWFInstanceManager *instanceManager = [[FWFInstanceManager alloc] init];
FWFWebViewConfigurationHostApiImpl *hostAPI =
[[FWFWebViewConfigurationHostApiImpl alloc] initWithInstanceManager:instanceManager];
FWFWebViewConfigurationHostApiImpl *hostAPI = [[FWFWebViewConfigurationHostApiImpl alloc]
initWithBinaryMessenger:OCMProtocolMock(@protocol(FlutterBinaryMessenger))
instanceManager:instanceManager];

WKWebView *mockWebView = OCMClassMock([WKWebView class]);
OCMStub([mockWebView configuration]).andReturn(OCMClassMock([WKWebViewConfiguration class]));
Expand All @@ -48,8 +50,9 @@ - (void)testSetAllowsInlineMediaPlayback {
FWFInstanceManager *instanceManager = [[FWFInstanceManager alloc] init];
[instanceManager addDartCreatedInstance:mockWebViewConfiguration withIdentifier:0];

FWFWebViewConfigurationHostApiImpl *hostAPI =
[[FWFWebViewConfigurationHostApiImpl alloc] initWithInstanceManager:instanceManager];
FWFWebViewConfigurationHostApiImpl *hostAPI = [[FWFWebViewConfigurationHostApiImpl alloc]
initWithBinaryMessenger:OCMProtocolMock(@protocol(FlutterBinaryMessenger))
instanceManager:instanceManager];

FlutterError *error;
[hostAPI setAllowsInlineMediaPlaybackForConfigurationWithIdentifier:@0
Expand All @@ -65,8 +68,9 @@ - (void)testSetMediaTypesRequiringUserActionForPlayback {
FWFInstanceManager *instanceManager = [[FWFInstanceManager alloc] init];
[instanceManager addDartCreatedInstance:mockWebViewConfiguration withIdentifier:0];

FWFWebViewConfigurationHostApiImpl *hostAPI =
[[FWFWebViewConfigurationHostApiImpl alloc] initWithInstanceManager:instanceManager];
FWFWebViewConfigurationHostApiImpl *hostAPI = [[FWFWebViewConfigurationHostApiImpl alloc]
initWithBinaryMessenger:OCMProtocolMock(@protocol(FlutterBinaryMessenger))
instanceManager:instanceManager];

FlutterError *error;
[hostAPI
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ @interface FWFWebViewHostApiTests : XCTestCase
@implementation FWFWebViewHostApiTests
- (void)testCreateWithIdentifier {
FWFInstanceManager *instanceManager = [[FWFInstanceManager alloc] init];
FWFWebViewHostApiImpl *hostAPI =
[[FWFWebViewHostApiImpl alloc] initWithInstanceManager:instanceManager];
FWFWebViewHostApiImpl *hostAPI = [[FWFWebViewHostApiImpl alloc]
initWithBinaryMessenger:OCMProtocolMock(@protocol(FlutterBinaryMessenger))
instanceManager:instanceManager];

[instanceManager addDartCreatedInstance:[[WKWebViewConfiguration alloc] init] withIdentifier:0];

Expand All @@ -32,8 +33,9 @@ - (void)testLoadRequest {
FWFInstanceManager *instanceManager = [[FWFInstanceManager alloc] init];
[instanceManager addDartCreatedInstance:mockWebView withIdentifier:0];

FWFWebViewHostApiImpl *hostAPI =
[[FWFWebViewHostApiImpl alloc] initWithInstanceManager:instanceManager];
FWFWebViewHostApiImpl *hostAPI = [[FWFWebViewHostApiImpl alloc]
initWithBinaryMessenger:OCMProtocolMock(@protocol(FlutterBinaryMessenger))
instanceManager:instanceManager];

FlutterError *error;
FWFNSUrlRequestData *requestData = [FWFNSUrlRequestData makeWithUrl:@"https://www.flutter.dev"
Expand All @@ -57,8 +59,9 @@ - (void)testLoadRequestWithInvalidUrl {
FWFInstanceManager *instanceManager = [[FWFInstanceManager alloc] init];
[instanceManager addDartCreatedInstance:mockWebView withIdentifier:0];

FWFWebViewHostApiImpl *hostAPI =
[[FWFWebViewHostApiImpl alloc] initWithInstanceManager:instanceManager];
FWFWebViewHostApiImpl *hostAPI = [[FWFWebViewHostApiImpl alloc]
initWithBinaryMessenger:OCMProtocolMock(@protocol(FlutterBinaryMessenger))
instanceManager:instanceManager];

FlutterError *error;
FWFNSUrlRequestData *requestData = [FWFNSUrlRequestData makeWithUrl:@"%invalidUrl%"
Expand All @@ -78,8 +81,9 @@ - (void)testSetCustomUserAgent {
FWFInstanceManager *instanceManager = [[FWFInstanceManager alloc] init];
[instanceManager addDartCreatedInstance:mockWebView withIdentifier:0];

FWFWebViewHostApiImpl *hostAPI =
[[FWFWebViewHostApiImpl alloc] initWithInstanceManager:instanceManager];
FWFWebViewHostApiImpl *hostAPI = [[FWFWebViewHostApiImpl alloc]
initWithBinaryMessenger:OCMProtocolMock(@protocol(FlutterBinaryMessenger))
instanceManager:instanceManager];

FlutterError *error;
[hostAPI setUserAgentForWebViewWithIdentifier:@0 userAgent:@"userA" error:&error];
Expand All @@ -94,8 +98,9 @@ - (void)testURL {
FWFInstanceManager *instanceManager = [[FWFInstanceManager alloc] init];
[instanceManager addDartCreatedInstance:mockWebView withIdentifier:0];

FWFWebViewHostApiImpl *hostAPI =
[[FWFWebViewHostApiImpl alloc] initWithInstanceManager:instanceManager];
FWFWebViewHostApiImpl *hostAPI = [[FWFWebViewHostApiImpl alloc]
initWithBinaryMessenger:OCMProtocolMock(@protocol(FlutterBinaryMessenger))
instanceManager:instanceManager];

FlutterError *error;
XCTAssertEqualObjects([hostAPI URLForWebViewWithIdentifier:@0 error:&error],
Expand All @@ -110,8 +115,9 @@ - (void)testCanGoBack {
FWFInstanceManager *instanceManager = [[FWFInstanceManager alloc] init];
[instanceManager addDartCreatedInstance:mockWebView withIdentifier:0];

FWFWebViewHostApiImpl *hostAPI =
[[FWFWebViewHostApiImpl alloc] initWithInstanceManager:instanceManager];
FWFWebViewHostApiImpl *hostAPI = [[FWFWebViewHostApiImpl alloc]
initWithBinaryMessenger:OCMProtocolMock(@protocol(FlutterBinaryMessenger))
instanceManager:instanceManager];

FlutterError *error;
XCTAssertEqualObjects([hostAPI canGoBackForWebViewWithIdentifier:@0 error:&error], @YES);
Expand All @@ -124,8 +130,9 @@ - (void)testSetUIDelegate {
FWFInstanceManager *instanceManager = [[FWFInstanceManager alloc] init];
[instanceManager addDartCreatedInstance:mockWebView withIdentifier:0];

FWFWebViewHostApiImpl *hostAPI =
[[FWFWebViewHostApiImpl alloc] initWithInstanceManager:instanceManager];
FWFWebViewHostApiImpl *hostAPI = [[FWFWebViewHostApiImpl alloc]
initWithBinaryMessenger:OCMProtocolMock(@protocol(FlutterBinaryMessenger))
instanceManager:instanceManager];

id<WKUIDelegate> mockDelegate = OCMProtocolMock(@protocol(WKUIDelegate));
[instanceManager addDartCreatedInstance:mockDelegate withIdentifier:1];
Expand All @@ -142,8 +149,9 @@ - (void)testSetNavigationDelegate {
FWFInstanceManager *instanceManager = [[FWFInstanceManager alloc] init];
[instanceManager addDartCreatedInstance:mockWebView withIdentifier:0];

FWFWebViewHostApiImpl *hostAPI =
[[FWFWebViewHostApiImpl alloc] initWithInstanceManager:instanceManager];
FWFWebViewHostApiImpl *hostAPI = [[FWFWebViewHostApiImpl alloc]
initWithBinaryMessenger:OCMProtocolMock(@protocol(FlutterBinaryMessenger))
instanceManager:instanceManager];

id<WKNavigationDelegate> mockDelegate = OCMProtocolMock(@protocol(WKNavigationDelegate));
[instanceManager addDartCreatedInstance:mockDelegate withIdentifier:1];
Expand All @@ -161,8 +169,9 @@ - (void)testEstimatedProgress {
FWFInstanceManager *instanceManager = [[FWFInstanceManager alloc] init];
[instanceManager addDartCreatedInstance:mockWebView withIdentifier:0];

FWFWebViewHostApiImpl *hostAPI =
[[FWFWebViewHostApiImpl alloc] initWithInstanceManager:instanceManager];
FWFWebViewHostApiImpl *hostAPI = [[FWFWebViewHostApiImpl alloc]
initWithBinaryMessenger:OCMProtocolMock(@protocol(FlutterBinaryMessenger))
instanceManager:instanceManager];

FlutterError *error;
XCTAssertEqualObjects([hostAPI estimatedProgressForWebViewWithIdentifier:@0 error:&error], @34.0);
Expand All @@ -175,8 +184,9 @@ - (void)testloadHTMLString {
FWFInstanceManager *instanceManager = [[FWFInstanceManager alloc] init];
[instanceManager addDartCreatedInstance:mockWebView withIdentifier:0];

FWFWebViewHostApiImpl *hostAPI =
[[FWFWebViewHostApiImpl alloc] initWithInstanceManager:instanceManager];
FWFWebViewHostApiImpl *hostAPI = [[FWFWebViewHostApiImpl alloc]
initWithBinaryMessenger:OCMProtocolMock(@protocol(FlutterBinaryMessenger))
instanceManager:instanceManager];

FlutterError *error;
[hostAPI loadHTMLForWebViewWithIdentifier:@0
Expand All @@ -193,8 +203,9 @@ - (void)testLoadFileURL {
FWFInstanceManager *instanceManager = [[FWFInstanceManager alloc] init];
[instanceManager addDartCreatedInstance:mockWebView withIdentifier:0];

FWFWebViewHostApiImpl *hostAPI =
[[FWFWebViewHostApiImpl alloc] initWithInstanceManager:instanceManager];
FWFWebViewHostApiImpl *hostAPI = [[FWFWebViewHostApiImpl alloc]
initWithBinaryMessenger:OCMProtocolMock(@protocol(FlutterBinaryMessenger))
instanceManager:instanceManager];

FlutterError *error;
[hostAPI loadFileForWebViewWithIdentifier:@0
Expand Down Expand Up @@ -222,10 +233,11 @@ - (void)testLoadFlutterAsset {
OCMStub([mockBundle URLForResource:@"myFolder/assets/index" withExtension:@"html"])
.andReturn([NSURL URLWithString:@"webview_flutter/myFolder/assets/index.html"]);

FWFWebViewHostApiImpl *hostAPI =
[[FWFWebViewHostApiImpl alloc] initWithInstanceManager:instanceManager
bundle:mockBundle
assetManager:mockAssetManager];
FWFWebViewHostApiImpl *hostAPI = [[FWFWebViewHostApiImpl alloc]
initWithBinaryMessenger:OCMProtocolMock(@protocol(FlutterBinaryMessenger))
instanceManager:instanceManager
bundle:mockBundle
assetManager:mockAssetManager];

FlutterError *error;
[hostAPI loadAssetForWebViewWithIdentifier:@0 assetKey:@"assets/index.html" error:&error];
Expand All @@ -243,8 +255,9 @@ - (void)testCanGoForward {
FWFInstanceManager *instanceManager = [[FWFInstanceManager alloc] init];
[instanceManager addDartCreatedInstance:mockWebView withIdentifier:0];

FWFWebViewHostApiImpl *hostAPI =
[[FWFWebViewHostApiImpl alloc] initWithInstanceManager:instanceManager];
FWFWebViewHostApiImpl *hostAPI = [[FWFWebViewHostApiImpl alloc]
initWithBinaryMessenger:OCMProtocolMock(@protocol(FlutterBinaryMessenger))
instanceManager:instanceManager];

FlutterError *error;
XCTAssertEqualObjects([hostAPI canGoForwardForWebViewWithIdentifier:@0 error:&error], @NO);
Expand All @@ -257,8 +270,9 @@ - (void)testGoBack {
FWFInstanceManager *instanceManager = [[FWFInstanceManager alloc] init];
[instanceManager addDartCreatedInstance:mockWebView withIdentifier:0];

FWFWebViewHostApiImpl *hostAPI =
[[FWFWebViewHostApiImpl alloc] initWithInstanceManager:instanceManager];
FWFWebViewHostApiImpl *hostAPI = [[FWFWebViewHostApiImpl alloc]
initWithBinaryMessenger:OCMProtocolMock(@protocol(FlutterBinaryMessenger))
instanceManager:instanceManager];

FlutterError *error;
[hostAPI goBackForWebViewWithIdentifier:@0 error:&error];
Expand All @@ -272,8 +286,9 @@ - (void)testGoForward {
FWFInstanceManager *instanceManager = [[FWFInstanceManager alloc] init];
[instanceManager addDartCreatedInstance:mockWebView withIdentifier:0];

FWFWebViewHostApiImpl *hostAPI =
[[FWFWebViewHostApiImpl alloc] initWithInstanceManager:instanceManager];
FWFWebViewHostApiImpl *hostAPI = [[FWFWebViewHostApiImpl alloc]
initWithBinaryMessenger:OCMProtocolMock(@protocol(FlutterBinaryMessenger))
instanceManager:instanceManager];

FlutterError *error;
[hostAPI goForwardForWebViewWithIdentifier:@0 error:&error];
Expand All @@ -287,8 +302,9 @@ - (void)testReload {
FWFInstanceManager *instanceManager = [[FWFInstanceManager alloc] init];
[instanceManager addDartCreatedInstance:mockWebView withIdentifier:0];

FWFWebViewHostApiImpl *hostAPI =
[[FWFWebViewHostApiImpl alloc] initWithInstanceManager:instanceManager];
FWFWebViewHostApiImpl *hostAPI = [[FWFWebViewHostApiImpl alloc]
initWithBinaryMessenger:OCMProtocolMock(@protocol(FlutterBinaryMessenger))
instanceManager:instanceManager];

FlutterError *error;
[hostAPI reloadWebViewWithIdentifier:@0 error:&error];
Expand All @@ -303,8 +319,9 @@ - (void)testTitle {
FWFInstanceManager *instanceManager = [[FWFInstanceManager alloc] init];
[instanceManager addDartCreatedInstance:mockWebView withIdentifier:0];

FWFWebViewHostApiImpl *hostAPI =
[[FWFWebViewHostApiImpl alloc] initWithInstanceManager:instanceManager];
FWFWebViewHostApiImpl *hostAPI = [[FWFWebViewHostApiImpl alloc]
initWithBinaryMessenger:OCMProtocolMock(@protocol(FlutterBinaryMessenger))
instanceManager:instanceManager];

FlutterError *error;
XCTAssertEqualObjects([hostAPI titleForWebViewWithIdentifier:@0 error:&error], @"myTitle");
Expand All @@ -317,8 +334,9 @@ - (void)testSetAllowsBackForwardNavigationGestures {
FWFInstanceManager *instanceManager = [[FWFInstanceManager alloc] init];
[instanceManager addDartCreatedInstance:mockWebView withIdentifier:0];

FWFWebViewHostApiImpl *hostAPI =
[[FWFWebViewHostApiImpl alloc] initWithInstanceManager:instanceManager];
FWFWebViewHostApiImpl *hostAPI = [[FWFWebViewHostApiImpl alloc]
initWithBinaryMessenger:OCMProtocolMock(@protocol(FlutterBinaryMessenger))
instanceManager:instanceManager];

FlutterError *error;
[hostAPI setAllowsBackForwardForWebViewWithIdentifier:@0 isAllowed:@YES error:&error];
Expand All @@ -336,8 +354,9 @@ - (void)testEvaluateJavaScript {
FWFInstanceManager *instanceManager = [[FWFInstanceManager alloc] init];
[instanceManager addDartCreatedInstance:mockWebView withIdentifier:0];

FWFWebViewHostApiImpl *hostAPI =
[[FWFWebViewHostApiImpl alloc] initWithInstanceManager:instanceManager];
FWFWebViewHostApiImpl *hostAPI = [[FWFWebViewHostApiImpl alloc]
initWithBinaryMessenger:OCMProtocolMock(@protocol(FlutterBinaryMessenger))
instanceManager:instanceManager];

NSString __block *returnValue;
FlutterError __block *returnError;
Expand Down Expand Up @@ -369,8 +388,9 @@ - (void)testEvaluateJavaScriptReturnsNSErrorData {
FWFInstanceManager *instanceManager = [[FWFInstanceManager alloc] init];
[instanceManager addDartCreatedInstance:mockWebView withIdentifier:0];

FWFWebViewHostApiImpl *hostAPI =
[[FWFWebViewHostApiImpl alloc] initWithInstanceManager:instanceManager];
FWFWebViewHostApiImpl *hostAPI = [[FWFWebViewHostApiImpl alloc]
initWithBinaryMessenger:OCMProtocolMock(@protocol(FlutterBinaryMessenger))
instanceManager:instanceManager];

NSString __block *returnValue;
FlutterError __block *returnError;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
#import "FWFWebsiteDataStoreHostApi.h"

@interface FWFHTTPCookieStoreHostApiImpl ()
@property(nonatomic) FWFInstanceManager *instanceManager;
// InstanceManager must be weak to prevent a circular reference with the object it stores.
@property(nonatomic, weak) FWFInstanceManager *instanceManager;
@end

@implementation FWFHTTPCookieStoreHostApiImpl
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ NS_ASSUME_NONNULL_BEGIN
*/
@interface FWFNavigationDelegate : FWFObject <WKNavigationDelegate>
@property(readonly, nonnull, nonatomic) FWFNavigationDelegateFlutterApiImpl *navigationDelegateAPI;

- (instancetype)initWithBinaryMessenger:(id<FlutterBinaryMessenger>)binaryMessenger
instanceManager:(FWFInstanceManager *)instanceManager;
@end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#import "FWFWebViewConfigurationHostApi.h"

@interface FWFNavigationDelegateFlutterApiImpl ()
// This reference must be weak to prevent a circular reference with the objects it stores.
// InstanceManager must be weak to prevent a circular reference with the object it stores.
@property(nonatomic, weak) FWFInstanceManager *instanceManager;
@end

Expand Down Expand Up @@ -183,8 +183,10 @@ - (void)webViewWebContentProcessDidTerminate:(WKWebView *)webView {
@end

@interface FWFNavigationDelegateHostApiImpl ()
@property(weak) id<FlutterBinaryMessenger> binaryMessenger;
// This reference must be weak to prevent a circular reference with the objects it stores.
// BinaryMessenger must be weak to prevent a circular reference with the host API it
// references.
@property(nonatomic, weak) id<FlutterBinaryMessenger> binaryMessenger;
// InstanceManager must be weak to prevent a circular reference with the object it stores.
@property(nonatomic, weak) FWFInstanceManager *instanceManager;
@end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,20 @@ NS_ASSUME_NONNULL_BEGIN
@interface FWFObjectFlutterApiImpl : FWFNSObjectFlutterApi
- (instancetype)initWithBinaryMessenger:(id<FlutterBinaryMessenger>)binaryMessenger
instanceManager:(FWFInstanceManager *)instanceManager;

- (void)observeValueForObject:(NSObject *)instance
keyPath:(NSString *)keyPath
object:(NSObject *)object
change:(NSDictionary<NSKeyValueChangeKey, id> *)change
completion:(void (^)(NSError *_Nullable))completion;
@end

/**
* Implementation of NSObject for FWFObjectHostApiImpl.
*/
@interface FWFObject : NSObject
@property(readonly, nonnull, nonatomic) FWFObjectFlutterApiImpl *objectApi;

- (instancetype)initWithBinaryMessenger:(id<FlutterBinaryMessenger>)binaryMessenger
instanceManager:(FWFInstanceManager *)instanceManager;
@end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#import "FWFDataConverters.h"

@interface FWFObjectFlutterApiImpl ()
// This reference must be weak to prevent a circular reference with the objects it stores.
// InstanceManager must be weak to prevent a circular reference with the object it stores.
@property(nonatomic, weak) FWFInstanceManager *instanceManager;
@end

Expand Down Expand Up @@ -74,7 +74,7 @@ - (void)observeValueForKeyPath:(NSString *)keyPath
@end

@interface FWFObjectHostApiImpl ()
// This reference must be weak to prevent a circular reference with the objects it stores.
// InstanceManager must be weak to prevent a circular reference with the object it stores.
@property(nonatomic, weak) FWFInstanceManager *instanceManager;
@end

Expand Down
Loading

0 comments on commit f62b237

Please sign in to comment.