Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 4d40e3f

Browse files
committed
review
1 parent 0cb67b4 commit 4d40e3f

File tree

2 files changed

+21
-44
lines changed

2 files changed

+21
-44
lines changed

shell/platform/android/io/flutter/embedding/engine/loader/FlutterLoader.java

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import android.view.WindowManager;
1515
import androidx.annotation.NonNull;
1616
import androidx.annotation.Nullable;
17+
import androidx.annotation.VisibleForTesting;
1718
import io.flutter.BuildConfig;
1819
import io.flutter.embedding.engine.FlutterJNI;
1920
import io.flutter.util.PathUtils;
@@ -47,7 +48,10 @@ public class FlutterLoader {
4748
* <p>The returned instance loads Flutter native libraries in the standard way. A singleton object
4849
* is used instead of static methods to facilitate testing without actually running native library
4950
* linking.
51+
*
52+
* @deprecated Use the {@link io.flutter.FlutterInjector} instead.
5053
*/
54+
@Deprecated
5155
@NonNull
5256
public static FlutterLoader getInstance() {
5357
if (instance == null) {
@@ -56,13 +60,6 @@ public static FlutterLoader getInstance() {
5660
return instance;
5761
}
5862

59-
@NonNull
60-
public static FlutterLoader getInstanceForTest(FlutterApplicationInfo flutterApplicationInfo) {
61-
FlutterLoader loader = new FlutterLoader();
62-
loader.flutterApplicationInfo = flutterApplicationInfo;
63-
return loader;
64-
}
65-
6663
private boolean initialized = false;
6764
@Nullable private Settings settings;
6865
private long initStartTimestampMillis;
@@ -91,6 +88,12 @@ public void startInitialization(@NonNull Context applicationContext) {
9188
startInitialization(applicationContext, new Settings());
9289
}
9390

91+
@VisibleForTesting
92+
public void loadNativeLibrary() {
93+
// Let this be mockable.
94+
System.loadLibrary("flutter");
95+
}
96+
9497
/**
9598
* Starts initialization of the native system.
9699
*
@@ -128,7 +131,7 @@ public void startInitialization(@NonNull Context applicationContext, @NonNull Se
128131
public InitResult call() {
129132
ResourceExtractor resourceExtractor = initResources(appContext);
130133

131-
System.loadLibrary("flutter");
134+
loadNativeLibrary();
132135

133136
// Prefetch the default font manager as soon as possible on a background thread.
134137
// It helps to reduce time cost of engine setup that blocks the platform thread.

shell/platform/android/test/io/flutter/embedding/engine/PluginComponentTest.java

Lines changed: 10 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
import static junit.framework.TestCase.assertEquals;
44
import static org.mockito.Matchers.any;
5+
import static org.mockito.Mockito.doNothing;
56
import static org.mockito.Mockito.mock;
7+
import static org.mockito.Mockito.spy;
68
import static org.mockito.Mockito.when;
79

810
import androidx.annotation.NonNull;
@@ -13,8 +15,6 @@
1315
import io.flutter.embedding.engine.plugins.FlutterPlugin;
1416
import org.junit.Test;
1517
import org.junit.runner.RunWith;
16-
import org.mockito.invocation.InvocationOnMock;
17-
import org.mockito.stubbing.Answer;
1818
import org.robolectric.RobolectricTestRunner;
1919
import org.robolectric.RuntimeEnvironment;
2020
import org.robolectric.annotation.Config;
@@ -30,43 +30,17 @@ public void pluginsCanAccessFlutterAssetPaths() {
3030
FlutterApplicationInfo emptyInfo =
3131
new FlutterApplicationInfo(null, null, null, null, null, null, false, false);
3232

33-
// FlutterLoader is the object to which the PluginRegistry defers for obtaining
34-
// the path to a Flutter asset. Ideally in this component test we would use a
35-
// real FlutterLoader and directly verify the relationship between FlutterAssets
36-
// and FlutterLoader. However, a real FlutterLoader cannot be used in a JVM test
37-
// because it would attempt to load native libraries. Therefore, we create a fake
38-
// FlutterLoader, but then we defer the corresponding asset lookup methods to the
39-
// real FlutterLoader singleton. This test ends up verifying that when FlutterAssets
40-
// is queried for an asset path, it returns the real expected path based on real
41-
// FlutterLoader behavior.
42-
FlutterLoader flutterLoader = mock(FlutterLoader.class);
43-
when(flutterLoader.getLookupKeyForAsset(any(String.class)))
44-
.thenAnswer(
45-
new Answer<String>() {
46-
@Override
47-
public String answer(InvocationOnMock invocation) throws Throwable {
48-
// Defer to a real FlutterLoader to return the asset path.
49-
String fileNameOrSubpath = (String) invocation.getArguments()[0];
50-
return FlutterLoader.getInstanceForTest(emptyInfo)
51-
.getLookupKeyForAsset(fileNameOrSubpath);
52-
}
53-
});
54-
when(flutterLoader.getLookupKeyForAsset(any(String.class), any(String.class)))
55-
.thenAnswer(
56-
new Answer<String>() {
57-
@Override
58-
public String answer(InvocationOnMock invocation) throws Throwable {
59-
// Defer to a real FlutterLoader to return the asset path.
60-
String fileNameOrSubpath = (String) invocation.getArguments()[0];
61-
String packageName = (String) invocation.getArguments()[1];
62-
return FlutterLoader.getInstanceForTest(emptyInfo)
63-
.getLookupKeyForAsset(fileNameOrSubpath, packageName);
64-
}
65-
});
33+
FlutterLoader realFlutterLoader = new FlutterLoader();
34+
FlutterLoader spyFlutterLoader = spy(realFlutterLoader);
35+
36+
// Let the real startInitialization be called, but mock the rest so it doesn't try to load
37+
// the real native library.
38+
doNothing().when(spyFlutterLoader).loadNativeLibrary();
39+
doNothing().when(spyFlutterLoader).ensureInitializationComplete(any(), any());
6640

6741
// Execute behavior under test.
6842
FlutterEngine flutterEngine =
69-
new FlutterEngine(RuntimeEnvironment.application, flutterLoader, flutterJNI);
43+
new FlutterEngine(RuntimeEnvironment.application, spyFlutterLoader, flutterJNI);
7044

7145
// As soon as our plugin is registered it will look up asset paths and store them
7246
// for our verification.

0 commit comments

Comments
 (0)