Flutter Engine
The Flutter Engine
FlutterInjectorTest.java
Go to the documentation of this file.
1// Copyright 2013 The Flutter Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5package io.flutter;
6
7import static org.junit.Assert.assertEquals;
8import static org.junit.Assert.assertNotNull;
9import static org.junit.Assert.assertNull;
10import static org.junit.Assert.assertThrows;
11import static org.junit.Assert.assertTrue;
12
13import androidx.test.ext.junit.runners.AndroidJUnit4;
14import io.flutter.embedding.engine.deferredcomponents.PlayStoreDeferredComponentManager;
15import io.flutter.embedding.engine.loader.FlutterLoader;
16import java.util.Arrays;
17import java.util.List;
18import java.util.concurrent.Callable;
19import java.util.concurrent.ExecutionException;
20import java.util.concurrent.ExecutorService;
21import java.util.concurrent.Future;
22import org.junit.After;
23import org.junit.Before;
24import org.junit.Test;
25import org.junit.runner.RunWith;
26import org.mockito.Mock;
27import org.mockito.MockitoAnnotations;
28import org.robolectric.annotation.Config;
29
30@Config(manifest = Config.NONE)
31@RunWith(AndroidJUnit4.class)
32public class FlutterInjectorTest {
35 @Mock ExecutorService mockExecutorService;
36
37 @Before
38 public void setUp() {
39 // Since the intent is to have a convenient static class to use for production.
41 MockitoAnnotations.openMocks(this);
42 }
43
44 @After
45 public void tearDown() {
47 }
48
49 @Test
51 // Implicitly builds when first accessed.
52 FlutterInjector injector = FlutterInjector.instance();
53 assertNotNull(injector.flutterLoader());
54 assertNull(injector.deferredComponentManager());
55 assertNotNull(injector.executorService());
56 }
57
58 @Test
60 throws InterruptedException, ExecutionException {
61 // Implicitly builds when first accessed.
62 FlutterInjector injector = FlutterInjector.instance();
63
64 List<Callable<String>> callables =
65 Arrays.asList(
66 () -> {
67 return Thread.currentThread().getName();
68 },
69 () -> {
70 return Thread.currentThread().getName();
71 });
72
73 List<Future<String>> threadNames;
74 threadNames = injector.executorService().invokeAll(callables);
75
76 assertEquals(threadNames.size(), 2);
77 for (Future<String> name : threadNames) {
78 assertTrue(name.get().startsWith("flutter-worker-"));
79 }
80 }
81
82 @Test
83 public void canPartiallyOverride() {
85 new FlutterInjector.Builder().setFlutterLoader(mockFlutterLoader).build());
86 FlutterInjector injector = FlutterInjector.instance();
87 assertEquals(injector.flutterLoader(), mockFlutterLoader);
88 }
89
90 @Test
94 .setDeferredComponentManager(mockDeferredComponentManager)
95 .build());
96 FlutterInjector injector = FlutterInjector.instance();
97 assertEquals(injector.deferredComponentManager(), mockDeferredComponentManager);
98 }
99
100 @Test
103 new FlutterInjector.Builder().setExecutorService(mockExecutorService).build());
104 FlutterInjector injector = FlutterInjector.instance();
105 assertEquals(injector.executorService(), mockExecutorService);
106 }
107
108 @Test()
109 public void cannotBeChangedOnceRead() {
110 FlutterInjector.instance();
111
112 assertThrows(
113 IllegalStateException.class,
114 () -> {
115 FlutterInjector.setInstance(
116 new FlutterInjector.Builder().setFlutterLoader(mockFlutterLoader).build());
117 });
118 }
119}
PlayStoreDeferredComponentManager mockDeferredComponentManager
Builder setFlutterLoader(@NonNull FlutterLoader flutterLoader)
Builder setDeferredComponentManager( @Nullable DeferredComponentManager deferredComponentManager)
Builder setExecutorService(@NonNull ExecutorService executorService)
static void setInstance(@NonNull FlutterInjector injector)
DEF_SWITCHES_START aot vmservice shared library name
Definition: switches.h:32