1package io.flutter.embedding.engine;
3import static org.junit.Assert.*;
4import static org.mockito.ArgumentMatchers.*;
5import static org.mockito.Mockito.*;
10import androidx.annotation.NonNull;
11import androidx.lifecycle.Lifecycle;
12import androidx.test.ext.junit.runners.AndroidJUnit4;
13import io.flutter.embedding.android.ExclusiveAppComponent;
14import io.flutter.embedding.engine.loader.FlutterLoader;
15import io.flutter.embedding.engine.plugins.FlutterPlugin;
16import io.flutter.embedding.engine.plugins.activity.ActivityAware;
17import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding;
18import io.flutter.plugin.common.PluginRegistry;
19import io.flutter.plugin.platform.PlatformViewsController;
20import java.util.concurrent.atomic.AtomicBoolean;
22import org.junit.runner.RunWith;
23import org.robolectric.annotation.Config;
27@RunWith(AndroidJUnit4.class)
31 Context context = mock(Context.class);
39 FakeFlutterPlugin fakePlugin1 =
new FakeFlutterPlugin();
40 FakeFlutterPlugin fakePlugin2 =
new FakeFlutterPlugin();
46 assertFalse(registry.
has(fakePlugin1.getClass()));
49 registry.
add(fakePlugin1);
52 assertTrue(registry.
has(fakePlugin1.getClass()));
53 assertEquals(1, fakePlugin1.attachmentCallCount);
56 registry.
add(fakePlugin2);
60 assertEquals(1, fakePlugin1.attachmentCallCount);
61 assertEquals(0, fakePlugin1.detachmentCallCount);
63 assertEquals(0, fakePlugin2.attachmentCallCount);
64 assertEquals(0, fakePlugin2.detachmentCallCount);
69 Context context = mock(Context.class);
78 Activity activity = mock(Activity.class);
81 Intent intent = mock(Intent.class);
82 when(activity.getIntent()).thenReturn(intent);
84 Lifecycle lifecycle = mock(Lifecycle.class);
85 AtomicBoolean isFirstCall =
new AtomicBoolean(
true);
90 FakeActivityAwareFlutterPlugin fakePlugin =
new FakeActivityAwareFlutterPlugin();
91 registry.
add(fakePlugin);
95 FakeActivityResultListener listener1 =
96 new FakeActivityResultListener(isFirstCall, fakePlugin.binding);
97 FakeActivityResultListener listener2 =
98 new FakeActivityResultListener(isFirstCall, fakePlugin.binding);
100 fakePlugin.binding.addActivityResultListener(listener1);
101 fakePlugin.binding.addActivityResultListener(listener2);
106 assertEquals(1, listener1.callCount);
107 assertEquals(1, listener2.callCount);
113 assertEquals(3, listener1.callCount + listener2.callCount);
118 Context context = mock(Context.class);
127 Activity activity = mock(Activity.class);
134 verify(platformViewsController).setSoftwareRendering(
false);
136 Intent intent = mock(Intent.class);
137 when(intent.getBooleanExtra(
"enable-software-rendering",
false)).thenReturn(
false);
138 when(activity.getIntent()).thenReturn(intent);
141 verify(platformViewsController,
times(2)).setSoftwareRendering(
false);
143 when(intent.getBooleanExtra(
"enable-software-rendering",
false)).thenReturn(
true);
146 verify(platformViewsController).setSoftwareRendering(
true);
149 private static class FakeFlutterPlugin
implements FlutterPlugin {
150 public int attachmentCallCount = 0;
151 public int detachmentCallCount = 0;
154 public void onAttachedToEngine(@NonNull FlutterPluginBinding binding) {
155 attachmentCallCount += 1;
159 public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {
160 detachmentCallCount += 1;
164 private static class FakeActivityAwareFlutterPlugin
implements FlutterPlugin, ActivityAware {
165 public ActivityPluginBinding binding;
168 public void onAttachedToEngine(@NonNull FlutterPluginBinding binding) {}
171 public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {}
174 public void onAttachedToActivity(
final ActivityPluginBinding binding) {
175 this.binding = binding;
179 public void onDetachedFromActivityForConfigChanges() {}
182 public void onReattachedToActivityForConfigChanges(
final ActivityPluginBinding binding) {}
185 public void onDetachedFromActivity() {}
188 private static class FakeActivityResultListener
implements PluginRegistry.ActivityResultListener {
189 public int callCount = 0;
190 private final AtomicBoolean isFirstCall;
191 private final ActivityPluginBinding binding;
193 public FakeActivityResultListener(AtomicBoolean isFirstCall, ActivityPluginBinding binding) {
194 this.isFirstCall = isFirstCall;
195 this.binding = binding;
199 public boolean onActivityResult(
200 final int requestCode,
final int resultCode,
final Intent
data) {
202 if (isFirstCall.get()) {
203 isFirstCall.set(
false);
204 binding.removeActivityResultListener(
this);
static SkISize times(const SkISize &size, float factor)
void itDoesNotRegisterTheSamePluginTwice()
void activityResultListenerCanBeRemovedFromListener()
void attachToActivity( @NonNull ExclusiveAppComponent< Activity > exclusiveActivity, @NonNull Lifecycle lifecycle)
void add(@NonNull FlutterPlugin plugin)
boolean has(@NonNull Class<? extends FlutterPlugin > pluginClass)
boolean onActivityResult(int requestCode, int resultCode, @Nullable Intent data)
PlatformViewsController getPlatformViewsController()
std::shared_ptr< const fml::Mapping > data