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

Commit 2eca05e

Browse files
author
Emmanuel Garcia
authored
Migrate to Mockito 4.1.0 (#30257)
1 parent f764864 commit 2eca05e

26 files changed

+132
-116
lines changed

shell/platform/android/io/flutter/embedding/engine/FlutterJNI.java

Lines changed: 61 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,24 @@ public class FlutterJNI {
106106
// platform thread and doesn't require locking.
107107
private ReentrantReadWriteLock shellHolderLock = new ReentrantReadWriteLock();
108108

109+
// Prefer using the FlutterJNI.Factory so it's easier to test.
110+
public FlutterJNI() {
111+
// We cache the main looper so that we can ensure calls are made on the main thread
112+
// without consistently paying the synchronization cost of getMainLooper().
113+
mainLooper = Looper.getMainLooper();
114+
}
115+
116+
/**
117+
* A factory for creating {@code FlutterJNI} instances. Useful for FlutterJNI injections during
118+
* tests.
119+
*/
120+
public static class Factory {
121+
/** @return a {@link FlutterJNI} instance. */
122+
public FlutterJNI provideFlutterJNI() {
123+
return new FlutterJNI();
124+
}
125+
}
126+
109127
// BEGIN Methods related to loading for FlutterLoader.
110128
/**
111129
* Loads the libflutter.so C++ library.
@@ -126,6 +144,8 @@ public void loadLibrary() {
126144

127145
private static boolean loadLibraryCalled = false;
128146

147+
private static native void nativePrefetchDefaultFontManager();
148+
129149
/**
130150
* Prefetch the default font manager provided by SkFontMgr::RefDefault() which is a process-wide
131151
* singleton owned by Skia. Note that, the first call to SkFontMgr::RefDefault() will take
@@ -142,10 +162,16 @@ public void prefetchDefaultFontManager() {
142162
FlutterJNI.prefetchDefaultFontManagerCalled = true;
143163
}
144164

145-
private static native void nativePrefetchDefaultFontManager();
146-
147165
private static boolean prefetchDefaultFontManagerCalled = false;
148166

167+
private static native void nativeInit(
168+
@NonNull Context context,
169+
@NonNull String[] args,
170+
@Nullable String bundlePath,
171+
@NonNull String appStoragePath,
172+
@NonNull String engineCachesPath,
173+
long initTimeMillis);
174+
149175
/**
150176
* Perform one time initialization of the Dart VM and Flutter engine.
151177
*
@@ -174,14 +200,6 @@ public void init(
174200
FlutterJNI.initCalled = true;
175201
}
176202

177-
private static native void nativeInit(
178-
@NonNull Context context,
179-
@NonNull String[] args,
180-
@Nullable String bundlePath,
181-
@NonNull String appStoragePath,
182-
@NonNull String engineCachesPath,
183-
long initTimeMillis);
184-
185203
private static boolean initCalled = false;
186204
// END methods related to FlutterLoader
187205

@@ -201,23 +219,23 @@ private static native void nativeInit(
201219

202220
private native boolean nativeGetIsSoftwareRenderingEnabled();
203221

204-
@UiThread
205222
/**
206223
* Checks launch settings for whether software rendering is requested.
207224
*
208225
* <p>The value is the same per program.
209226
*/
227+
@UiThread
210228
public boolean getIsSoftwareRenderingEnabled() {
211229
return nativeGetIsSoftwareRenderingEnabled();
212230
}
213231

214-
@Nullable
215232
/**
216233
* Observatory URI for the VM instance.
217234
*
218235
* <p>Its value is set by the native engine once {@link #init(Context, String[], String, String,
219236
* String, long)} is run.
220237
*/
238+
@Nullable
221239
public static String getObservatoryUri() {
222240
return observatoryUri;
223241
}
@@ -245,7 +263,7 @@ public void setRefreshRateFPS(float refreshRateFPS) {
245263
* The Android vsync waiter implementation in C++ needs to know when a vsync signal arrives, which
246264
* is obtained via Java API. The delegate set here is called on the C++ side when the engine is
247265
* ready to wait for the next vsync signal. The delegate is expected to add a postFrameCallback to
248-
* the {@link android.view.Choreographer}, and call {@link nativeOnVsync} to notify the engine.
266+
* the {@link android.view.Choreographer}, and call {@link onVsync} to notify the engine.
249267
*
250268
* @param delegate The delegate that will call the engine back on the next vsync signal.
251269
*/
@@ -264,6 +282,8 @@ private static void asyncWaitForVsync(final long cookie) {
264282
}
265283
}
266284

285+
private native void nativeOnVsync(long frameDelayNanos, long refreshPeriodNanos, long cookie);
286+
267287
/**
268288
* Notifies the engine that the Choreographer has signaled a vsync.
269289
*
@@ -272,23 +292,44 @@ private static void asyncWaitForVsync(final long cookie) {
272292
* @param refreshPeriodNanos The display refresh period in nanoseconds.
273293
* @param cookie An opaque handle to the C++ VSyncWaiter object.
274294
*/
275-
public native void nativeOnVsync(long frameDelayNanos, long refreshPeriodNanos, long cookie);
295+
public void onVsync(long frameDelayNanos, long refreshPeriodNanos, long cookie) {
296+
nativeOnVsync(frameDelayNanos, refreshPeriodNanos, cookie);
297+
}
276298

277-
// TODO(mattcarroll): add javadocs
278299
@NonNull
300+
@Deprecated
279301
public static native FlutterCallbackInformation nativeLookupCallbackInformation(long handle);
280302

281303
// ----- Start FlutterTextUtils Methods ----
304+
private native boolean nativeFlutterTextUtilsIsEmoji(int codePoint);
282305

283-
public native boolean nativeFlutterTextUtilsIsEmoji(int codePoint);
306+
public boolean isCodePointEmoji(int codePoint) {
307+
return nativeFlutterTextUtilsIsEmoji(codePoint);
308+
}
309+
310+
private native boolean nativeFlutterTextUtilsIsEmojiModifier(int codePoint);
284311

285-
public native boolean nativeFlutterTextUtilsIsEmojiModifier(int codePoint);
312+
public boolean isCodePointEmojiModifier(int codePoint) {
313+
return nativeFlutterTextUtilsIsEmojiModifier(codePoint);
314+
}
286315

287-
public native boolean nativeFlutterTextUtilsIsEmojiModifierBase(int codePoint);
316+
private native boolean nativeFlutterTextUtilsIsEmojiModifierBase(int codePoint);
317+
318+
public boolean isCodePointEmojiModifierBase(int codePoint) {
319+
return nativeFlutterTextUtilsIsEmojiModifierBase(codePoint);
320+
}
288321

289-
public native boolean nativeFlutterTextUtilsIsVariationSelector(int codePoint);
322+
private native boolean nativeFlutterTextUtilsIsVariationSelector(int codePoint);
290323

291-
public native boolean nativeFlutterTextUtilsIsRegionalIndicator(int codePoint);
324+
public boolean isCodePointVariantSelector(int codePoint) {
325+
return nativeFlutterTextUtilsIsVariationSelector(codePoint);
326+
}
327+
328+
private native boolean nativeFlutterTextUtilsIsRegionalIndicator(int codePoint);
329+
330+
public boolean isCodePointRegionalIndicator(int codePoint) {
331+
return nativeFlutterTextUtilsIsRegionalIndicator(codePoint);
332+
}
292333

293334
// ----- End Engine FlutterTextUtils Methods ----
294335

@@ -312,13 +353,6 @@ private static void asyncWaitForVsync(final long cookie) {
312353

313354
@NonNull private final Looper mainLooper; // cached to avoid synchronization on repeat access.
314355

315-
// Prefer using the FlutterJNI.Factory so it's easier to test.
316-
public FlutterJNI() {
317-
// We cache the main looper so that we can ensure calls are made on the main thread
318-
// without consistently paying the synchronization cost of getMainLooper().
319-
mainLooper = Looper.getMainLooper();
320-
}
321-
322356
// ------ Start Native Attach/Detach Support ----
323357
/**
324358
* Returns true if this instance of {@code FlutterJNI} is connected to Flutter's native engine via
@@ -1402,15 +1436,4 @@ void updateSemantics(
14021436
public interface AsyncWaitForVsyncDelegate {
14031437
void asyncWaitForVsync(final long cookie);
14041438
}
1405-
1406-
/**
1407-
* A factory for creating {@code FlutterJNI} instances. Useful for FlutterJNI injections during
1408-
* tests.
1409-
*/
1410-
public static class Factory {
1411-
/** @return a {@link FlutterJNI} instance. */
1412-
public FlutterJNI provideFlutterJNI() {
1413-
return new FlutterJNI();
1414-
}
1415-
}
14161439
}

shell/platform/android/io/flutter/plugin/editing/FlutterTextUtils.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,23 @@ public FlutterTextUtils(FlutterJNI flutterJNI) {
1919
}
2020

2121
public boolean isEmoji(int codePoint) {
22-
return flutterJNI.nativeFlutterTextUtilsIsEmoji(codePoint);
22+
return flutterJNI.isCodePointEmoji(codePoint);
2323
}
2424

2525
public boolean isEmojiModifier(int codePoint) {
26-
return flutterJNI.nativeFlutterTextUtilsIsEmojiModifier(codePoint);
26+
return flutterJNI.isCodePointEmojiModifier(codePoint);
2727
}
2828

2929
public boolean isEmojiModifierBase(int codePoint) {
30-
return flutterJNI.nativeFlutterTextUtilsIsEmojiModifierBase(codePoint);
30+
return flutterJNI.isCodePointEmojiModifierBase(codePoint);
3131
}
3232

3333
public boolean isVariationSelector(int codePoint) {
34-
return flutterJNI.nativeFlutterTextUtilsIsVariationSelector(codePoint);
34+
return flutterJNI.isCodePointVariantSelector(codePoint);
3535
}
3636

3737
public boolean isRegionalIndicatorSymbol(int codePoint) {
38-
return flutterJNI.nativeFlutterTextUtilsIsRegionalIndicator(codePoint);
38+
return flutterJNI.isCodePointRegionalIndicator(codePoint);
3939
}
4040

4141
public boolean isTagSpecChar(int codePoint) {

shell/platform/android/io/flutter/view/VsyncWaiter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ public void doFrame(long frameTimeNanos) {
9797
if (delay < 0) {
9898
delay = 0;
9999
}
100-
flutterJNI.nativeOnVsync(delay, refreshPeriodNanos, cookie);
100+
flutterJNI.onVsync(delay, refreshPeriodNanos, cookie);
101101
}
102102
});
103103
}

shell/platform/android/test/io/flutter/FlutterInjectorTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public class FlutterInjectorTest {
3737
public void setUp() {
3838
// Since the intent is to have a convenient static class to use for production.
3939
FlutterInjector.reset();
40-
MockitoAnnotations.initMocks(this);
40+
MockitoAnnotations.openMocks(this);
4141
}
4242

4343
@After

shell/platform/android/test/io/flutter/embedding/android/FlutterActivityAndFragmentDelegateTest.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
import static org.junit.Assert.assertNotNull;
66
import static org.junit.Assert.assertNull;
77
import static org.junit.Assert.assertThrows;
8-
import static org.mockito.Matchers.any;
9-
import static org.mockito.Matchers.eq;
10-
import static org.mockito.Matchers.isNull;
11-
import static org.mockito.Matchers.notNull;
8+
import static org.mockito.ArgumentMatchers.any;
9+
import static org.mockito.ArgumentMatchers.eq;
10+
import static org.mockito.ArgumentMatchers.isNotNull;
11+
import static org.mockito.ArgumentMatchers.isNull;
1212
import static org.mockito.Mockito.mock;
1313
import static org.mockito.Mockito.never;
1414
import static org.mockito.Mockito.times;
@@ -230,7 +230,7 @@ public void itGivesHostAnOpportunityToConfigureFlutterSurfaceView() {
230230
delegate.onCreateView(null, null, null, 0, true);
231231

232232
// Verify that the host was asked to configure a FlutterSurfaceView.
233-
verify(mockHost, times(1)).onFlutterSurfaceViewCreated(notNull(FlutterSurfaceView.class));
233+
verify(mockHost, times(1)).onFlutterSurfaceViewCreated(isNotNull());
234234
}
235235

236236
@Test
@@ -259,7 +259,7 @@ public void itGivesHostAnOpportunityToConfigureFlutterTextureView() {
259259
delegate.onCreateView(null, null, null, 0, false);
260260

261261
// Verify that the host was asked to configure a FlutterTextureView.
262-
verify(customMockHost, times(1)).onFlutterTextureViewCreated(notNull(FlutterTextureView.class));
262+
verify(customMockHost, times(1)).onFlutterTextureViewCreated(isNotNull());
263263
}
264264

265265
@Test

shell/platform/android/test/io/flutter/embedding/android/FlutterAndroidComponentTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package io.flutter.embedding.android;
22

33
import static org.junit.Assert.assertNotNull;
4-
import static org.mockito.Matchers.any;
5-
import static org.mockito.Matchers.isNull;
4+
import static org.mockito.ArgumentMatchers.any;
5+
import static org.mockito.ArgumentMatchers.isNull;
66
import static org.mockito.Mockito.doAnswer;
77
import static org.mockito.Mockito.inOrder;
88
import static org.mockito.Mockito.mock;

shell/platform/android/test/io/flutter/embedding/android/FlutterViewTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
import static junit.framework.TestCase.assertEquals;
44
import static junit.framework.TestCase.assertFalse;
55
import static junit.framework.TestCase.assertTrue;
6-
import static org.mockito.Matchers.any;
7-
import static org.mockito.Matchers.anyInt;
6+
import static org.mockito.ArgumentMatchers.any;
7+
import static org.mockito.ArgumentMatchers.anyInt;
88
import static org.mockito.Mockito.doNothing;
99
import static org.mockito.Mockito.doReturn;
1010
import static org.mockito.Mockito.mock;
@@ -76,7 +76,7 @@ public class FlutterViewTest {
7676

7777
@Before
7878
public void setUp() {
79-
MockitoAnnotations.initMocks(this);
79+
MockitoAnnotations.openMocks(this);
8080
when(mockFlutterJni.isAttached()).thenReturn(true);
8181
}
8282

shell/platform/android/test/io/flutter/embedding/android/KeyChannelResponderTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public class KeyChannelResponderTest {
3030

3131
@Before
3232
public void setUp() {
33-
MockitoAnnotations.initMocks(this);
33+
MockitoAnnotations.openMocks(this);
3434
channelResponder = new KeyChannelResponder(keyEventChannel);
3535
}
3636

shell/platform/android/test/io/flutter/embedding/android/KeyboardManagerTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ private FlutterEngine mockFlutterEngine() {
6969

7070
@Before
7171
public void setUp() {
72-
MockitoAnnotations.initMocks(this);
72+
MockitoAnnotations.openMocks(this);
7373
when(mockFlutterJni.isAttached()).thenReturn(true);
7474
mockEngine = mockFlutterEngine();
7575
mockKeyEventChannel = mockEngine.getKeyEventChannel();

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public class FlutterEngineGroupComponentTest {
5151
public void setUp() {
5252
FlutterInjector.reset();
5353

54-
MockitoAnnotations.initMocks(this);
54+
MockitoAnnotations.openMocks(this);
5555
jniAttached = false;
5656
when(mockflutterJNI.isAttached()).thenAnswer(invocation -> jniAttached);
5757
doAnswer(invocation -> jniAttached = true).when(mockflutterJNI).attachToNative();
@@ -165,7 +165,7 @@ public void canCreateAndRunCustomEntrypoints() {
165165
.runBundleAndSnapshotFromLibrary(
166166
eq("some/path/to/flutter_assets"),
167167
eq("other entrypoint"),
168-
isNull(String.class),
168+
isNull(),
169169
any(AssetManager.class),
170170
nullable(List.class));
171171
}
@@ -215,7 +215,7 @@ public void canCreateAndRunWithCustomEntrypointArgs() {
215215
.runBundleAndSnapshotFromLibrary(
216216
nullable(String.class),
217217
nullable(String.class),
218-
isNull(String.class),
218+
isNull(),
219219
any(AssetManager.class),
220220
eq(firstDartEntrypointArgs));
221221

0 commit comments

Comments
 (0)