Skip to content
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
---
title: FlutterMain.setIsRunningInRobolectricTest on Android removed
description: The test-only FlutterMain.setIsRunningInRobolectricTest API on the Android engine is consolidated into the FlutterInjector.
---

## Summary

If you write Java JUnit tests (such as Robolectric tests) against the Flutter
engine's Java embedding and used the
`FlutterMain.setIsRunningInRobolectricTest(true)` API, replace it with

```java
FlutterJNI mockFlutterJNI = mock(FlutterJNI.class);
FlutterInjector.setInstance(
new FlutterInjector.Builder()
.setFlutterLoader(new FlutterLoader(mockFlutterJNI))
.build());
```

This should be very uncommon.

## Context

The `FlutterMain` class itself is being deprecated and replaced with the
`FlutterInjector` class. The `FlutterMain` class uses a number of
static variables and functions than make it difficult to test.
`FlutterMain.setIsRunningInRobolectricTest()` is one ad-hoc static
mechanism to allow tests to run on the host machine on JVM without
loading the libflutter.so native library (which can't be done on the host
machine).

Rather than one-off solutions, all dependency injections needed for tests
in Flutter's Android/Java engine embedding are now moved to the
[`FlutterInjector`](https://cs.opensource.google/flutter/engine/+/master:shell/platform/android/io/flutter/FlutterInjector.java)
class.

Within the `FlutterInjector` class, the `setFlutterLoader()` Builder
function allows for control of how the [`FlutterLoader`](https://cs.opensource.google/flutter/engine/+/master:shell/platform/android/io/flutter/embedding/engine/loader/FlutterLoader.java)
class locates and loads the libflutter.so library.

## Description of change

https://github.com/flutter/engine/commit/15f5696c4139a21e1fc54014ce17d01f6ad1737c#diff-599e1d64442183ead768757cca6805c3L154
removes the `FlutterMain.setIsRunningInRobolectricTest()` testing function.

https://github.com/flutter/engine/commit/15f5696c4139a21e1fc54014ce17d01f6ad1737c#diff-f928557f2d60773a8435366400fa42ed
adds a `FlutterInjector` class to assist testing.

https://github.com/flutter/engine/pull/20473 further refactors FlutterLoader and FlutterJNI
to allow for additional mocking/testing.

## Migration guide

Code before migration:

```java
FlutterMain.setIsRunningInRobolectricTest(true);
```

Code after migration:

```java
FlutterJNI mockFlutterJNI = mock(FlutterJNI.class);
FlutterInjector.setInstance(
new FlutterInjector.Builder()
.setFlutterLoader(new FlutterLoader(mockFlutterJNI))
.build());
```

## Timeline

Landed in version: 1.22.0-2.0.pre.133<br>
In stable release: not yet
2 changes: 2 additions & 0 deletions src/docs/release/breaking-changes/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@ release, and listed in alphabetical order:
* [Network Policy on iOS and Android][]
* [SnackBars managed by the ScaffoldMessenger][]
* [TextSelectionTheme migration][]
* [Android FlutterMain.setIsRunningInRobolectricTest testing API removed][]
* [Clip behavior][]

[Android ActivityControlSurface attachToActivity signature change]: /docs/release/breaking-changes/android-activity-control-surface-attach
[Android FlutterMain.setIsRunningInRobolectricTest testing API removed]: /docs/release/breaking-changes/android-setIsRunningInRobolectricTest-removed
[Material Chip button semantics]: /docs/release/breaking-changes/material-chip-button-semantics
[Network Policy on iOS and Android]: /docs/release/breaking-changes/network-policy-ios-android
[SnackBars managed by the ScaffoldMessenger]: /docs/release/breaking-changes/scaffold-messenger
Expand Down