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

Commit

Permalink
[webview_flutter_android] Expose the Java InstanceManager (#6421)
Browse files Browse the repository at this point in the history
  • Loading branch information
bparrishMines authored Sep 22, 2022
1 parent 7c6c784 commit 0177355
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 1 deletion.
4 changes: 4 additions & 0 deletions packages/webview_flutter/webview_flutter_android/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 2.10.2

* Adds a getter to expose the Java InstanceManager.

## 2.10.1

* Adds a method to the `WebView` wrapper to retrieve the X and Y positions simultaneously.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
/**
* Maintains instances used to communicate with the corresponding objects in Dart.
*
* <p>Objects stored in this container are represented by an object in Dart that is also stored in
* an InstanceManager with the same identifier.
*
* <p>When an instance is added with an identifier, either can be used to retrieve the other.
*
* <p>Added instances are added as a weak reference and a strong reference. When the strong
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import android.os.Handler;
import android.view.View;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import io.flutter.embedding.engine.plugins.FlutterPlugin;
import io.flutter.embedding.engine.plugins.activity.ActivityAware;
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding;
Expand Down Expand Up @@ -166,4 +167,10 @@ private void updateContext(Context context) {
webViewHostApi.setContext(context);
javaScriptChannelHostApi.setPlatformThreadHandler(new Handler(context.getMainLooper()));
}

/** Maintains instances used to communicate with the corresponding objects in Dart. */
@Nullable
public InstanceManager getInstanceManager() {
return instanceManager;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -511,4 +511,9 @@ public void setBackgroundColor(Long instanceId, Long color) {
final WebView webView = (WebView) instanceManager.getInstance(instanceId);
webView.setBackgroundColor(color.intValue());
}

/** Maintains instances used to communicate with the corresponding WebView Dart object. */
public InstanceManager getInstanceManager() {
return instanceManager;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// 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 static org.junit.Assert.assertNotNull;
import static org.mockito.Mockito.when;

import android.content.Context;
import io.flutter.embedding.engine.plugins.FlutterPlugin;
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();

@Mock Context mockContext;

@Mock BinaryMessenger mockBinaryMessenger;

@Mock PlatformViewRegistry mockViewRegistry;

@Mock FlutterPlugin.FlutterPluginBinding mockPluginBinding;

@Test
public void getInstanceManagerAfterOnAttachedToEngine() {
final WebViewFlutterPlugin webViewFlutterPlugin = new WebViewFlutterPlugin();

when(mockPluginBinding.getApplicationContext()).thenReturn(mockContext);
when(mockPluginBinding.getPlatformViewRegistry()).thenReturn(mockViewRegistry);
when(mockPluginBinding.getBinaryMessenger()).thenReturn(mockBinaryMessenger);

webViewFlutterPlugin.onAttachedToEngine(mockPluginBinding);

assertNotNull(webViewFlutterPlugin.getInstanceManager());

webViewFlutterPlugin.onDetachedFromEngine(mockPluginBinding);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: webview_flutter_android
description: A Flutter plugin that provides a WebView widget on Android.
repository: https://github.com/flutter/plugins/tree/main/packages/webview_flutter/webview_flutter_android
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+webview%22
version: 2.10.1
version: 2.10.2

environment:
sdk: ">=2.14.0 <3.0.0"
Expand Down

0 comments on commit 0177355

Please sign in to comment.