Skip to content
Merged
Changes from 1 commit
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
Prev Previous commit
Update android-setIsRunningInRobolectricTest-removed.md
  • Loading branch information
dnfield authored and xster committed Oct 26, 2020
commit 1b44856ca3ec431c089992c8e6323114e6146f66
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,22 @@ description: The test-only FlutterMain.setIsRunningInRobolectricTest API on the
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
`FlutterInjector.setInstance(new FlutterInjector.Builder().setShouldLoadNative(false).build())`.

```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 itself used a number of
static variables and functions than make it difficult to test. 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
Expand All @@ -27,8 +34,9 @@ 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 `setShouldLoadNative()` Builder
function allows for control of whether to load the native library.
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

Expand All @@ -38,6 +46,9 @@ 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:
Expand All @@ -49,7 +60,11 @@ FlutterMain.setIsRunningInRobolectricTest(true);
Code after migration:

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

## Timeline
Expand Down