1package io.flutter.embedding.android;
3import static io.flutter.embedding.android.FlutterActivityLaunchConfigs.HANDLE_DEEPLINKING_META_DATA_KEY;
4import static org.junit.Assert.assertEquals;
5import static org.junit.Assert.assertFalse;
6import static org.junit.Assert.assertNotNull;
7import static org.junit.Assert.assertTrue;
8import static org.mockito.Mockito.mock;
9import static org.mockito.Mockito.spy;
10import static org.mockito.Mockito.when;
14import android.content.pm.PackageManager;
18import android.widget.FrameLayout;
19import androidx.annotation.NonNull;
20import androidx.annotation.Nullable;
21import androidx.test.core.app.ActivityScenario;
22import androidx.test.core.app.ApplicationProvider;
23import androidx.test.ext.junit.runners.AndroidJUnit4;
24import io.flutter.FlutterInjector;
25import io.flutter.embedding.android.FlutterActivityLaunchConfigs.BackgroundMode;
26import io.flutter.embedding.engine.FlutterEngine;
27import io.flutter.embedding.engine.FlutterJNI;
28import io.flutter.embedding.engine.loader.FlutterLoader;
29import io.flutter.plugins.GeneratedPluginRegistrant;
31import org.junit.After;
32import org.junit.Before;
34import org.junit.runner.RunWith;
35import org.robolectric.Robolectric;
36import org.robolectric.annotation.Config;
39@RunWith(AndroidJUnit4.class)
41 private final Context ctx = ApplicationProvider.getApplicationContext();
48 when(mockFlutterJNI.
isAttached()).thenReturn(
true);
50 when(mockFlutterJNIFactory.provideFlutterJNI()).thenReturn(mockFlutterJNI);
70 new FakeFlutterFragmentActivity() {
82 new FakeFlutterFragmentActivity() {
94 new FakeFlutterFragmentActivity() {
96 public String getDartEntrypointLibraryUri() {
97 return "package:foo/bar.dart";
106 FlutterFragmentActivityWithRootLayout activity =
107 Robolectric.buildActivity(FlutterFragmentActivityWithRootLayout.class).get();
108 activity.onCreate(
null);
109 assertNotNull(activity.FRAGMENT_CONTAINER_ID);
110 assertTrue(activity.FRAGMENT_CONTAINER_ID != View.NO_ID);
115 try (ActivityScenario<FlutterFragmentActivity> scenario =
121 assertEquals(1, registeredEngines.size());
122 assertEquals(activity.getFlutterEngine(), registeredEngines.get(0));
129 try (ActivityScenario<FlutterFragmentActivity> scenario =
135 assertEquals(1, registeredEngines.size());
136 assertEquals(activity.getFlutterEngine(), registeredEngines.get(0));
144 assertEquals(1, registeredEngines.size());
149 throws PackageManager.NameNotFoundException {
153 Bundle bundle =
new Bundle();
154 bundle.putBoolean(HANDLE_DEEPLINKING_META_DATA_KEY,
true);
156 when(spyFlutterActivity.
getMetaData()).thenReturn(bundle);
162 throws PackageManager.NameNotFoundException {
166 Bundle bundle =
new Bundle();
167 bundle.putBoolean(HANDLE_DEEPLINKING_META_DATA_KEY,
false);
169 when(spyFlutterActivity.
getMetaData()).thenReturn(bundle);
175 throws PackageManager.NameNotFoundException {
180 Bundle bundle =
new Bundle();
182 when(spyFlutterActivity.
getMetaData()).thenReturn(bundle);
189 FlutterFragmentActivityWithRootLayout activity =
190 Robolectric.buildActivity(FlutterFragmentActivityWithRootLayout.class).get();
192 activity.onCreate(
null);
193 ViewGroup contentView = (ViewGroup) activity.findViewById(
android.R.id.content);
194 boolean foundCustomView =
false;
195 for (
int i = 0;
i < contentView.getChildCount();
i++) {
197 contentView.getChildAt(
i) instanceof FlutterFragmentActivityWithRootLayout.CustomLayout;
198 if (foundCustomView) {
202 assertTrue(foundCustomView);
207 try (ActivityScenario<FlutterFragmentActivityWithProvidedEngine> scenario =
211 assertNotNull(activity.getFlutterEngine());
212 assertEquals(1, activity.numberOfEnginesCreated);
254 int numberOfEnginesCreated = 0;
266 when(flutterJNI.
isAttached()).thenReturn(
true);
269 numberOfEnginesCreated++;
270 return new FlutterEngine(context, flutterLoader, flutterJNI,
new String[] {},
true);
276 public Intent getIntent() {
281 public String getDartEntrypointFunctionName() {
286 public String getDartEntrypointLibraryUri() {
291 protected String getInitialRoute() {
296 protected String getAppBundlePath() {
301 protected boolean shouldHandleDeeplinking() {
306 private static class FlutterFragmentActivityWithRootLayout
307 extends FlutterFragmentActivityWithProvidedEngine {
315 protected FrameLayout provideRootLayout(Context context) {
316 return new CustomLayout(context);
323 private static class FlutterFragmentActivityWithIntentBuilders
extends FlutterFragmentActivity {
324 public static NewEngineIntentBuilder withNewEngine() {
325 return new NewEngineIntentBuilder(FlutterFragmentActivityWithIntentBuilders.class);
328 public static CachedEngineIntentBuilder withCachedEngine(@NonNull String cachedEngineId) {
329 return new CachedEngineIntentBuilder(
330 FlutterFragmentActivityWithIntentBuilders.class, cachedEngineId);
333 public static NewEngineInGroupIntentBuilder withNewEngineInGroup(
334 @NonNull String engineGroupId) {
335 return new NewEngineInGroupIntentBuilder(
336 FlutterFragmentActivityWithIntentBuilders.class, engineGroupId);
Builder setFlutterJNIFactory(@NonNull FlutterJNI.Factory factory)
static void setInstance(@NonNull FlutterInjector injector)
CustomLayout(Context context)
FlutterEngine provideFlutterEngine(@NonNull Context context)
int numberOfEnginesCreated
FlutterFragment createFlutterFragment()
void itRegistersPluginsAtConfigurationTime()
void itHandlesNewFragmentRecreationDuringRestoreWhenActivityIsRecreated()
void itRetrievesExistingFlutterFragmentWhenRecreated()
void itReturnsValueFromMetaDataWhenCallsShouldHandleDeepLinkingCase3()
void createFlutterFragment_customDartEntrypointLibraryUri()
void createFlutterFragment_defaultRenderModeTexture()
void itCreatesAValidFlutterFragment()
void createFlutterFragment_defaultRenderModeSurface()
void itReturnsValueFromMetaDataWhenCallsShouldHandleDeepLinkingCase1()
void itAllowsRootLayoutOverride()
void itDoesNotRegisterPluginsTwiceWhenUsingACachedEngine()
void createFlutterFragment_customRenderMode()
void itReturnsValueFromMetaDataWhenCallsShouldHandleDeepLinkingCase2()
FlutterFragment createFlutterFragment()
FlutterFragment retrieveExistingFlutterFragmentIfPossible()
boolean shouldHandleDeeplinking()
void onCreate(@Nullable Bundle savedInstanceState)
FlutterEngine getFlutterEngine()
FlutterEngine getFlutterEngine()
static FlutterFragment createDefault()
String getDartEntrypointLibraryUri()
RenderMode getRenderMode()
boolean automaticallyRegisterPlugins()
static void clearRegisteredEngines()
static List< FlutterEngine > getRegisteredEngines()