Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate RuntimeEnvironment#application to ApplicationProvider#getApplicationContext #32557

Merged
Show file tree
Hide file tree
Changes from all commits
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

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.Lifecycle;
import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import io.flutter.embedding.engine.FlutterEngine;
import io.flutter.embedding.engine.FlutterEngineCache;
Expand All @@ -40,22 +41,22 @@
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.robolectric.Robolectric;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.android.controller.ActivityController;
import org.robolectric.annotation.Config;

@Config(manifest = Config.NONE)
@RunWith(AndroidJUnit4.class)
public class FlutterAndroidComponentTest {
private final Context ctx = ApplicationProvider.getApplicationContext();

@Test
public void pluginsReceiveFlutterPluginBinding() {
// ---- Test setup ----
// Place a FlutterEngine in the static cache.
FlutterLoader mockFlutterLoader = mock(FlutterLoader.class);
FlutterJNI mockFlutterJni = mock(FlutterJNI.class);
when(mockFlutterJni.isAttached()).thenReturn(true);
FlutterEngine cachedEngine =
spy(new FlutterEngine(RuntimeEnvironment.application, mockFlutterLoader, mockFlutterJni));
FlutterEngine cachedEngine = spy(new FlutterEngine(ctx, mockFlutterLoader, mockFlutterJni));
FlutterEngineCache.getInstance().put("my_flutter_engine", cachedEngine);

// Add mock plugin.
Expand All @@ -71,7 +72,7 @@ public void pluginsReceiveFlutterPluginBinding() {

// --- Execute the behavior under test ---
// Push the delegate through all lifecycle methods all the way to destruction.
delegate.onAttach(RuntimeEnvironment.application);
delegate.onAttach(ctx);

// Verify that the plugin is attached to the FlutterEngine.
ArgumentCaptor<FlutterPlugin.FlutterPluginBinding> pluginBindingCaptor =
Expand Down Expand Up @@ -109,8 +110,7 @@ public void activityAwarePluginsReceiveActivityBinding() {
FlutterLoader mockFlutterLoader = mock(FlutterLoader.class);
FlutterJNI mockFlutterJni = mock(FlutterJNI.class);
when(mockFlutterJni.isAttached()).thenReturn(true);
FlutterEngine cachedEngine =
spy(new FlutterEngine(RuntimeEnvironment.application, mockFlutterLoader, mockFlutterJni));
FlutterEngine cachedEngine = spy(new FlutterEngine(ctx, mockFlutterLoader, mockFlutterJni));
FlutterEngineCache.getInstance().put("my_flutter_engine", cachedEngine);

// Add mock plugin.
Expand Down Expand Up @@ -143,7 +143,7 @@ public Object answer(InvocationOnMock invocation) throws Throwable {

// --- Execute the behavior under test ---
// Push the delegate through all lifecycle methods all the way to destruction.
delegate.onAttach(RuntimeEnvironment.application);
delegate.onAttach(ctx);

// Verify plugin was given an ActivityPluginBinding.
ArgumentCaptor<ActivityPluginBinding> pluginBindingCaptor =
Expand Down Expand Up @@ -182,8 +182,7 @@ public void normalLifecycleStepsDoNotTriggerADetachFromFlutterEngine() {
FlutterLoader mockFlutterLoader = mock(FlutterLoader.class);
FlutterJNI mockFlutterJni = mock(FlutterJNI.class);
when(mockFlutterJni.isAttached()).thenReturn(true);
FlutterEngine cachedEngine =
spy(new FlutterEngine(RuntimeEnvironment.application, mockFlutterLoader, mockFlutterJni));
FlutterEngine cachedEngine = spy(new FlutterEngine(ctx, mockFlutterLoader, mockFlutterJni));
FlutterEngineCache.getInstance().put("my_flutter_engine", cachedEngine);

// Create a fake Host, which is required by the delegate.
Expand All @@ -195,7 +194,7 @@ public void normalLifecycleStepsDoNotTriggerADetachFromFlutterEngine() {

// --- Execute the behavior under test ---
// Push the delegate through all lifecycle methods all the way to destruction.
delegate.onAttach(RuntimeEnvironment.application);
delegate.onAttach(ctx);
delegate.onRestoreInstanceState(null);
delegate.onCreateView(null, null, null, 0, true);
delegate.onStart();
Expand All @@ -215,14 +214,12 @@ public void twoOverlappingFlutterActivitiesDoNotCrosstalk() {
FlutterLoader mockFlutterLoader = mock(FlutterLoader.class);
FlutterJNI mockFlutterJni = mock(FlutterJNI.class);
when(mockFlutterJni.isAttached()).thenReturn(true);
FlutterEngine cachedEngine =
spy(new FlutterEngine(RuntimeEnvironment.application, mockFlutterLoader, mockFlutterJni));
FlutterEngine cachedEngine = spy(new FlutterEngine(ctx, mockFlutterLoader, mockFlutterJni));
FlutterEngineCache.getInstance().put("my_flutter_engine", cachedEngine);
LifecycleChannel mockLifecycleChannel = mock(LifecycleChannel.class);
when(cachedEngine.getLifecycleChannel()).thenReturn(mockLifecycleChannel);

Intent intent =
FlutterActivity.withCachedEngine("my_flutter_engine").build(RuntimeEnvironment.application);
Intent intent = FlutterActivity.withCachedEngine("my_flutter_engine").build(ctx);
ActivityController<FlutterActivity> activityController1 =
Robolectric.buildActivity(FlutterActivity.class, intent);
activityController1.create().start().resume();
Expand Down Expand Up @@ -265,7 +262,7 @@ private FakeHost(@NonNull FlutterEngine flutterEngine) {
@NonNull
@Override
public Context getContext() {
return RuntimeEnvironment.application;
return ApplicationProvider.getApplicationContext();
}

@Nullable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.test.core.app.ActivityScenario;
import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import io.flutter.FlutterInjector;
import io.flutter.embedding.android.FlutterActivityLaunchConfigs.BackgroundMode;
Expand All @@ -37,13 +38,14 @@
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.Robolectric;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.android.controller.ActivityController;
import org.robolectric.annotation.Config;

@Config(manifest = Config.NONE)
@RunWith(AndroidJUnit4.class)
public class FlutterFragmentActivityTest {
private final Context ctx = ApplicationProvider.getApplicationContext();

@Before
public void setUp() {
FlutterInjector.reset();
Expand Down Expand Up @@ -256,7 +258,7 @@ public void itHandlesNewFragmentRecreationDuringRestoreWhenActivityIsRecreated()

@Test
public void itDoesNotCrashWhenSplashScreenMetadataIsNotDefined() {
Intent intent = FlutterFragmentActivity.createDefaultIntent(RuntimeEnvironment.application);
Intent intent = FlutterFragmentActivity.createDefaultIntent(ctx);
ActivityController<FlutterFragmentActivity> activityController =
Robolectric.buildActivity(FlutterFragmentActivity.class, intent);
FlutterFragmentActivity fragmentActivity = activityController.get();
Expand All @@ -272,20 +274,20 @@ public void itDoesNotCrashWhenSplashScreenMetadataIsNotDefined() {
sdk = Build.VERSION_CODES.KITKAT,
shadows = {SplashShadowResources.class})
public void itLoadsSplashScreenDrawable() throws PackageManager.NameNotFoundException {
Intent intent = FlutterFragmentActivity.createDefaultIntent(RuntimeEnvironment.application);
Intent intent = FlutterFragmentActivity.createDefaultIntent(ctx);
ActivityController<FlutterFragmentActivity> activityController =
Robolectric.buildActivity(FlutterFragmentActivity.class, intent);
FlutterFragmentActivity activity = activityController.get();

// Inject splash screen drawable resource id in the metadata
PackageManager pm = RuntimeEnvironment.application.getPackageManager();
PackageManager pm = ctx.getPackageManager();
ActivityInfo activityInfo =
pm.getActivityInfo(activity.getComponentName(), PackageManager.GET_META_DATA);
activityInfo.metaData = new Bundle();
activityInfo.metaData.putInt(
FlutterActivityLaunchConfigs.SPLASH_SCREEN_META_DATA_KEY,
SplashShadowResources.SPLASH_DRAWABLE_ID);
shadowOf(RuntimeEnvironment.application.getPackageManager()).addOrUpdateActivity(activityInfo);
shadowOf(ctx.getPackageManager()).addOrUpdateActivity(activityInfo);

// It should load the drawable.
SplashScreen splashScreen = activity.provideSplashScreen();
Expand All @@ -301,20 +303,20 @@ public void itLoadsThemedSplashScreenDrawable() throws PackageManager.NameNotFou
// A drawable with theme references can be parsed only if the app theme is supplied
// in getDrawable methods. This test verifies it by fetching a (fake) themed drawable.
// On failure, a Resource.NotFoundException will ocurr.
Intent intent = FlutterFragmentActivity.createDefaultIntent(RuntimeEnvironment.application);
Intent intent = FlutterFragmentActivity.createDefaultIntent(ctx);
ActivityController<FlutterFragmentActivity> activityController =
Robolectric.buildActivity(FlutterFragmentActivity.class, intent);
FlutterFragmentActivity activity = activityController.get();

// Inject themed splash screen drawable resource id in the metadata.
PackageManager pm = RuntimeEnvironment.application.getPackageManager();
PackageManager pm = ctx.getPackageManager();
ActivityInfo activityInfo =
pm.getActivityInfo(activity.getComponentName(), PackageManager.GET_META_DATA);
activityInfo.metaData = new Bundle();
activityInfo.metaData.putInt(
FlutterActivityLaunchConfigs.SPLASH_SCREEN_META_DATA_KEY,
SplashShadowResources.THEMED_SPLASH_DRAWABLE_ID);
shadowOf(RuntimeEnvironment.application.getPackageManager()).addOrUpdateActivity(activityInfo);
shadowOf(ctx.getPackageManager()).addOrUpdateActivity(activityInfo);

// It should load the drawable.
SplashScreen splashScreen = activity.provideSplashScreen();
Expand All @@ -324,17 +326,17 @@ public void itLoadsThemedSplashScreenDrawable() throws PackageManager.NameNotFou
@Test
public void itWithMetadataWithoutSplashScreenResourceKeyDoesNotProvideSplashScreen()
throws PackageManager.NameNotFoundException {
Intent intent = FlutterFragmentActivity.createDefaultIntent(RuntimeEnvironment.application);
Intent intent = FlutterFragmentActivity.createDefaultIntent(ctx);
ActivityController<FlutterFragmentActivity> activityController =
Robolectric.buildActivity(FlutterFragmentActivity.class, intent);
FlutterFragmentActivity activity = activityController.get();

// Setup an empty metadata file.
PackageManager pm = RuntimeEnvironment.application.getPackageManager();
PackageManager pm = ctx.getPackageManager();
ActivityInfo activityInfo =
pm.getActivityInfo(activity.getComponentName(), PackageManager.GET_META_DATA);
activityInfo.metaData = new Bundle();
shadowOf(RuntimeEnvironment.application.getPackageManager()).addOrUpdateActivity(activityInfo);
shadowOf(ctx.getPackageManager()).addOrUpdateActivity(activityInfo);

// It should not load the drawable.
SplashScreen splashScreen = activity.provideSplashScreen();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import android.content.Context;
import androidx.activity.OnBackPressedCallback;
import androidx.fragment.app.FragmentActivity;
import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import io.flutter.embedding.engine.FlutterEngine;
import io.flutter.embedding.engine.FlutterEngineCache;
Expand All @@ -26,12 +28,12 @@
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.Robolectric;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;

@Config(manifest = Config.NONE)
@RunWith(AndroidJUnit4.class)
public class FlutterFragmentTest {
private final Context ctx = ApplicationProvider.getApplicationContext();
boolean isDelegateAttached;

@Test
Expand Down Expand Up @@ -234,8 +236,7 @@ public void itDelegatesOnBackPressedAutomaticallyWhenEnabled() {
when(flutterJNI.isAttached()).thenReturn(true);

FlutterEngine flutterEngine =
new FlutterEngine(
RuntimeEnvironment.application, new FlutterLoader(), flutterJNI, null, false);
new FlutterEngine(ctx, new FlutterLoader(), flutterJNI, null, false);
FlutterEngineCache.getInstance().put("my_cached_engine", flutterEngine);

FlutterFragment fragment =
Expand Down Expand Up @@ -268,8 +269,7 @@ public void itHandlesPopSystemNavigationAutomaticallyWhenEnabled() {
when(flutterJNI.isAttached()).thenReturn(true);

FlutterEngine flutterEngine =
new FlutterEngine(
RuntimeEnvironment.application, new FlutterLoader(), flutterJNI, null, false);
new FlutterEngine(ctx, new FlutterLoader(), flutterJNI, null, false);
FlutterEngineCache.getInstance().put("my_cached_engine", flutterEngine);

FlutterFragment fragment =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
import android.graphics.SurfaceTexture;
import android.view.Surface;
import android.view.TextureView;
import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;

@Config(manifest = Config.NONE)
Expand All @@ -19,7 +19,8 @@
public class FlutterTextureViewTest {
@Test
public void surfaceTextureListenerReleasesRenderer() {
final FlutterTextureView textureView = new FlutterTextureView(RuntimeEnvironment.application);
final FlutterTextureView textureView =
new FlutterTextureView(ApplicationProvider.getApplicationContext());
final Surface mockRenderSurface = mock(Surface.class);

textureView.setRenderSurface(mockRenderSurface);
Expand Down
Loading