Flutter Engine
The Flutter Engine
DartExecutorTest.java
Go to the documentation of this file.
1package io.flutter.embedding.engine.dart;
2
3import static junit.framework.TestCase.assertNotNull;
4import static org.junit.Assert.assertEquals;
5import static org.junit.Assert.assertThrows;
6import static org.mockito.ArgumentMatchers.anyInt;
7import static org.mockito.ArgumentMatchers.eq;
8import static org.mockito.Mockito.mock;
9import static org.mockito.Mockito.times;
10import static org.mockito.Mockito.verify;
11import static org.mockito.Mockito.when;
12
13import android.content.res.AssetManager;
14import androidx.test.ext.junit.runners.AndroidJUnit4;
15import io.flutter.FlutterInjector;
16import io.flutter.embedding.engine.FlutterJNI;
17import io.flutter.embedding.engine.dart.DartExecutor.DartEntrypoint;
18import io.flutter.embedding.engine.loader.FlutterLoader;
19import java.nio.ByteBuffer;
20import org.junit.Before;
21import org.junit.Test;
22import org.junit.runner.RunWith;
23import org.mockito.Mock;
24import org.mockito.MockitoAnnotations;
25import org.robolectric.annotation.Config;
26
27@Config(manifest = Config.NONE)
28@RunWith(AndroidJUnit4.class)
29public class DartExecutorTest {
31
32 @Before
33 public void setUp() {
35 MockitoAnnotations.openMocks(this);
36 }
37
38 @Test
39 public void itSendsBinaryMessages() {
40 // Setup test.
41 FlutterJNI fakeFlutterJni = mock(FlutterJNI.class);
42
43 // Create object under test.
44 DartExecutor dartExecutor = new DartExecutor(fakeFlutterJni, mock(AssetManager.class));
45
46 // Verify a BinaryMessenger exists.
47 assertNotNull(dartExecutor.getBinaryMessenger());
48
49 // Execute the behavior under test.
50 ByteBuffer fakeMessage = mock(ByteBuffer.class);
51 dartExecutor.getBinaryMessenger().send("fake_channel", fakeMessage);
52
53 // Verify that DartExecutor sent our message to FlutterJNI.
54 verify(fakeFlutterJni, times(1))
55 .dispatchPlatformMessage(eq("fake_channel"), eq(fakeMessage), anyInt(), anyInt());
56 }
57
58 @Test
60 FlutterJNI mockFlutterJNI = mock(FlutterJNI.class);
61 when(mockFlutterJNI.isAttached()).thenReturn(true);
62
63 DartExecutor dartExecutor = new DartExecutor(mockFlutterJNI, mock(AssetManager.class));
64 dartExecutor.notifyLowMemoryWarning();
65 verify(mockFlutterJNI, times(1)).notifyLowMemoryWarning();
66 }
67
68 @Test
70 assertThrows(
71 AssertionError.class,
72 () -> {
73 DartEntrypoint.createDefault();
74 });
75 }
76
77 @Test
79 when(mockFlutterLoader.initialized()).thenReturn(true);
80 when(mockFlutterLoader.findAppBundlePath()).thenReturn("my/custom/path");
82 new FlutterInjector.Builder().setFlutterLoader(mockFlutterLoader).build());
84 assertEquals(entrypoint.pathToBundle, "my/custom/path");
85 assertEquals(entrypoint.dartEntrypointFunctionName, "main");
86 }
87}
static SkISize times(const SkISize &size, float factor)
static bool eq(const SkM44 &a, const SkM44 &b, float tol)
Definition: M44Test.cpp:18
Builder setFlutterLoader(@NonNull FlutterLoader flutterLoader)
static void setInstance(@NonNull FlutterInjector injector)
void send(@NonNull String channel, @Nullable ByteBuffer message)