Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
change to external classes
  • Loading branch information
bparrishMines committed Feb 8, 2023
commit e050085a9de4a895a51cd2b6d038dd2f9e8335c1
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

package io.flutter.plugins.webviewflutter;

import android.webkit.WebView;

import androidx.annotation.Nullable;

import io.flutter.embedding.engine.FlutterEngine;

/**
* App and package facing native API provided by the `webview_flutter_android` plugin.
*
* This class follows the convention of breaking changes of the Dart API, which means that any
* changes to the class that are not backwards compatible will only be done with a major version
* change of the plugin.
*/
@SuppressWarnings("unused")
public interface WebViewFlutterAndroidExternalApi {
/**
* Retrieves the {@link WebView} that is associated with `identifier`.
*
* <p>See the Dart method `AndroidWebViewController.webViewIdentifier` to get the identifier of an
* underlying `WebView`.
*
* @param engine the execution environment the {@link WebViewFlutterPlugin} should belong to. If
* the engine doesn't contain an attached instance of {@link WebViewFlutterPlugin}, this
* method returns null.
* @param identifier the associated identifier of the `WebView`.
* @return the `WebView` associated with `identifier` or null if a `WebView` instance associated
* with `identifier` could not be found.
*/
@Nullable
static WebView getWebView(FlutterEngine engine, long identifier) {
final WebViewFlutterPlugin webViewPlugin =
(WebViewFlutterPlugin) engine.getPlugins().get(WebViewFlutterPlugin.class);

if (webViewPlugin != null && webViewPlugin.getInstanceManager() != null) {
final Object instance = webViewPlugin.getInstanceManager().getInstance(identifier);
if (instance instanceof WebView) {
return (WebView) instance;
}
}

return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,37 +42,6 @@ public class WebViewFlutterPlugin implements FlutterPlugin, ActivityAware {
private WebViewHostApiImpl webViewHostApi;
private JavaScriptChannelHostApiImpl javaScriptChannelHostApi;

/**
* Retrieves the {@link WebView} that is associated with `identifer`.
*
* <p>See the Dart method `AndroidWebViewController.webViewIdentifier` to get the identifier of an
* underlying `WebView`.
*
* @param engine the execution environment the {@link WebViewFlutterPlugin} should belong to. If
* the engine doesn't contain an attached instance of {@link WebViewFlutterPlugin}, this
* method returns null.
* @param identifier the associated identifier of the `WebView`.
* @return the `WebView` associated with `identifier` or null if a `WebView` instance associated
* with `identifier` could not be found.
*/
@SuppressWarnings("unused")
@Nullable
public static WebView getWebView(FlutterEngine engine, long identifier) {
final WebViewFlutterPlugin webViewPlugin =
(WebViewFlutterPlugin) engine.getPlugins().get(WebViewFlutterPlugin.class);

if (webViewPlugin == null || webViewPlugin.instanceManager == null) {
return null;
}

final Object instance = webViewPlugin.instanceManager.getInstance(identifier);
if (instance instanceof WebView) {
return (WebView) instance;
}

return null;
}

/**
* Add an instance of this to {@link io.flutter.embedding.engine.plugins.PluginRegistry} to
* register it.
Expand Down Expand Up @@ -216,7 +185,6 @@ private void updateContext(Context context) {

/** Maintains instances used to communicate with the corresponding objects in Dart. */
@Nullable
@VisibleForTesting
public InstanceManager getInstanceManager() {
return instanceManager;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,34 @@

import android.content.Context;
import android.webkit.WebView;
import io.flutter.embedding.engine.FlutterEngine;
import io.flutter.embedding.engine.plugins.FlutterPlugin;
import io.flutter.embedding.engine.plugins.PluginRegistry;
import io.flutter.plugin.common.BinaryMessenger;
import io.flutter.plugin.platform.PlatformViewRegistry;

import org.junit.Rule;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;

public class WebViewFlutterPluginTest {
@Rule public MockitoRule mockitoRule = MockitoJUnit.rule();
import io.flutter.embedding.engine.FlutterEngine;
import io.flutter.embedding.engine.plugins.FlutterPlugin;
import io.flutter.embedding.engine.plugins.PluginRegistry;
import io.flutter.plugin.common.BinaryMessenger;
import io.flutter.plugin.platform.PlatformViewRegistry;

public class WebViewFlutterAndroidExternalApiTest {
@Rule
public MockitoRule mockitoRule = MockitoJUnit.rule();

@Mock Context mockContext;
@Mock
Context mockContext;

@Mock BinaryMessenger mockBinaryMessenger;
@Mock
BinaryMessenger mockBinaryMessenger;

@Mock PlatformViewRegistry mockViewRegistry;
@Mock
PlatformViewRegistry mockViewRegistry;

@Mock FlutterPlugin.FlutterPluginBinding mockPluginBinding;
@Mock
FlutterPlugin.FlutterPluginBinding mockPluginBinding;

@Test
public void getWebView() {
Expand All @@ -55,7 +62,7 @@ public void getWebView() {
final FlutterEngine mockFlutterEngine = mock(FlutterEngine.class);
when(mockFlutterEngine.getPlugins()).thenReturn(mockPluginRegistry);

assertEquals(WebViewFlutterPlugin.getWebView(mockFlutterEngine, 0), mockWebView);
assertEquals(WebViewFlutterAndroidExternalApi.getWebView(mockFlutterEngine, 0), mockWebView);

webViewFlutterPlugin.onDetachedFromEngine(mockPluginBinding);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@

@import webview_flutter_wkwebview;

@interface FLTWebViewFlutterPluginTests : XCTestCase
@interface FWFWebViewFlutterWKWebViewExternalAPITests : XCTestCase
@end

@implementation FLTWebViewFlutterPluginTests
@implementation FWFWebViewFlutterWKWebViewExternalAPITests
- (void)testWebViewForIdentifier {
WKWebView *webView = [[WKWebView alloc] init];
FWFInstanceManager *instanceManager = [[FWFInstanceManager alloc] init];
Expand All @@ -20,7 +20,7 @@ - (void)testWebViewForIdentifier {
OCMStub([mockPluginRegistry valuePublishedByPlugin:@"FLTWebViewFlutterPlugin"])
.andReturn(instanceManager);

XCTAssertEqualObjects([FLTWebViewFlutterPlugin webViewForIdentifier:0
XCTAssertEqualObjects([FWFWebViewFlutterWKWebViewExternalAPI webViewForIdentifier:0
withPluginRegistry:mockPluginRegistry],
webView);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,6 @@
NS_ASSUME_NONNULL_BEGIN

@interface FLTWebViewFlutterPlugin : NSObject <FlutterPlugin>
/**
Retrieves the `WKWebView` that is associated with `identifer`.

See the Dart method `WebKitWebViewController.webViewIdentifier` to get the identifier of an
underlying `WKWebView`.

@param identifier The associated identifier of the `WebView`.
@param registry The plugin registry the `FLTWebViewFlutterPlugin` should belong to. If
the registry doesn't contain an attached instance of `FLTWebViewFlutterPlugin`,
this method returns nil.
@return The `WKWebView` associated with `identifier` or nil if a `WKWebView` instance associated
with `identifier` could not be found.
*/
+ (nullable WKWebView *)webViewForIdentifier:(long)identifier
withPluginRegistry:(id<FlutterPluginRegistry>)registry;
@end

NS_ASSUME_NONNULL_END
Original file line number Diff line number Diff line change
Expand Up @@ -108,19 +108,6 @@ + (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar> *)registrar {
[registrar publish:instanceManager];
}

+ (nullable WKWebView *)webViewForIdentifier:(long)identifier
withPluginRegistry:(id<FlutterPluginRegistry>)registry {
FWFInstanceManager *instanceManager =
(FWFInstanceManager *)[registry valuePublishedByPlugin:@"FLTWebViewFlutterPlugin"];

NSObject *instance = [instanceManager instanceForIdentifier:identifier];
if ([instance isKindOfClass:[WKWebView class]]) {
return (WKWebView *)instance;
}

return nil;
}

- (void)detachFromEngineForRegistrar:(NSObject<FlutterPluginRegistrar> *)registrar {
[registrar publish:[NSNull null]];
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#import <Flutter/Flutter.h>
#import <WebKit/WebKit.h>
#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

/**
App and package facing native API provided by the `webview_flutter_wkwebview` plugin.

This class follows the convention of breaking changes of the Dart API, which means that any
changes to the class that are not backwards compatible will only be done with a major version
change of the plugin.
*/
@interface FWFWebViewFlutterWKWebViewExternalAPI : NSObject
/**
Retrieves the `WKWebView` that is associated with `identifier`.

See the Dart method `WebKitWebViewController.webViewIdentifier` to get the identifier of an
underlying `WKWebView`.

@param identifier The associated identifier of the `WebView`.
@param registry The plugin registry the `FLTWebViewFlutterPlugin` should belong to. If
the registry doesn't contain an attached instance of `FLTWebViewFlutterPlugin`,
this method returns nil.
@return The `WKWebView` associated with `identifier` or nil if a `WKWebView` instance associated
with `identifier` could not be found.
*/
+ (nullable WKWebView *)webViewForIdentifier:(long)identifier
withPluginRegistry:(id<FlutterPluginRegistry>)registry;
@end

NS_ASSUME_NONNULL_END
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#import "FWFInstanceManager.h"
#import "FWFWebViewFlutterWKWebViewExternalAPI.h"

@implementation FWFWebViewFlutterWKWebViewExternalAPI
+ (nullable WKWebView *)webViewForIdentifier:(long)identifier
withPluginRegistry:(id<FlutterPluginRegistry>)registry {
FWFInstanceManager *instanceManager =
(FWFInstanceManager *)[registry valuePublishedByPlugin:@"FLTWebViewFlutterPlugin"];

NSObject *instance = [instanceManager instanceForIdentifier:identifier];
if ([instance isKindOfClass:[WKWebView class]]) {
return (WKWebView *)instance;
}

return nil;
}
@end
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@
#import <webview_flutter_wkwebview/FWFUIViewHostApi.h>
#import <webview_flutter_wkwebview/FWFUserContentControllerHostApi.h>
#import <webview_flutter_wkwebview/FWFWebViewConfigurationHostApi.h>
#import <webview_flutter_wkwebview/FWFWebViewFlutterWKWebViewExternalAPI.h>
#import <webview_flutter_wkwebview/FWFWebViewHostApi.h>
#import <webview_flutter_wkwebview/FWFWebsiteDataStoreHostApi.h>