Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
ShimPluginRegistryTest.java
Go to the documentation of this file.
1package io.flutter.embedding.engine.plugins.shim;
2
3import static org.junit.Assert.assertEquals;
4import static org.junit.Assert.assertNull;
5import static org.mockito.Mockito.times;
6import static org.mockito.Mockito.verify;
7import static org.mockito.Mockito.when;
8
9import android.app.Activity;
10import android.content.Context;
11import androidx.test.ext.junit.runners.AndroidJUnit4;
12import io.flutter.embedding.engine.FlutterEngine;
13import io.flutter.embedding.engine.plugins.FlutterPlugin;
14import io.flutter.embedding.engine.plugins.FlutterPlugin.FlutterPluginBinding;
15import io.flutter.embedding.engine.plugins.PluginRegistry;
16import io.flutter.embedding.engine.plugins.activity.ActivityAware;
17import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding;
18import org.junit.Before;
19import org.junit.Test;
20import org.junit.runner.RunWith;
21import org.mockito.ArgumentCaptor;
22import org.mockito.Mock;
23import org.mockito.MockitoAnnotations;
24import org.robolectric.annotation.Config;
25
26@Config(manifest = Config.NONE)
27@RunWith(AndroidJUnit4.class)
29
30 @Mock private FlutterEngine mockFlutterEngine;
31 @Mock private FlutterPluginBinding mockFlutterPluginBinding;
32 @Mock private ActivityPluginBinding mockActivityPluginBinding;
33 @Mock private PluginRegistry mockPluginRegistry;
34 @Mock private Context mockApplicationContext;
35 @Mock private Activity mockActivity;
36
37 @Before
38 public void setup() {
39 MockitoAnnotations.openMocks(this);
40 when(mockFlutterEngine.getPlugins()).thenReturn(mockPluginRegistry);
41 when(mockFlutterPluginBinding.getApplicationContext()).thenReturn(mockApplicationContext);
42 when(mockActivityPluginBinding.getActivity()).thenReturn(mockActivity);
43 }
44
45 @SuppressWarnings("deprecation")
46 // Test is intentionally verifying deprecated behavior.
47 @Test
49 ShimPluginRegistry registryUnderTest = new ShimPluginRegistry(mockFlutterEngine);
50 // Fully qualifed name because imports can not have deprecation supression.
51 // This is the consumption side of the old plugins.
53 registryUnderTest.registrarFor("test");
54
55 ArgumentCaptor<FlutterPlugin> shimAggregateCaptor =
56 ArgumentCaptor.forClass(FlutterPlugin.class);
57 // A single shim aggregate was added as a new plugin to the FlutterEngine's PluginRegistry.
58 verify(mockPluginRegistry).add(shimAggregateCaptor.capture());
59 // This is really a ShimRegistrarAggregate acting as a FlutterPlugin which is the
60 // intermediate consumption side of the new plugin inside the shim.
61 FlutterPlugin shimAggregateUnderTest = shimAggregateCaptor.getValue();
62 // The FlutterPluginBinding is the supply side of the new plugin.
63 shimAggregateUnderTest.onAttachedToEngine(mockFlutterPluginBinding);
64
65 // Consume something from the old plugin API.
66 assertEquals(mockApplicationContext, registrarUnderTest.context());
67 // Check that the value comes from the supply side of the new plugin.
68 verify(mockFlutterPluginBinding).getApplicationContext();
69 }
70
71 @SuppressWarnings("deprecation")
72 // Test is intentionally verifying deprecated behavior.
73 @Test
75 ShimPluginRegistry registryUnderTest = new ShimPluginRegistry(mockFlutterEngine);
76 // Fully qualifed name because imports can not have deprecation supression.
78 registryUnderTest.registrarFor("test1");
80 registryUnderTest.registrarFor("test2");
81
82 ArgumentCaptor<FlutterPlugin> shimAggregateCaptor =
83 ArgumentCaptor.forClass(FlutterPlugin.class);
84 verify(mockPluginRegistry).add(shimAggregateCaptor.capture());
85 // There's only one aggregate for many old plugins.
86 FlutterPlugin shimAggregateUnderTest = shimAggregateCaptor.getValue();
87
88 // The FlutterPluginBinding is the supply side of the new plugin.
89 shimAggregateUnderTest.onAttachedToEngine(mockFlutterPluginBinding);
90
91 // Since the 2 old plugins are supplied by the same intermediate FlutterPlugin, they should
92 // get the same value.
93 assertEquals(registrarUnderTest1.context(), registrarUnderTest2.context());
94 verify(mockFlutterPluginBinding, times(2)).getApplicationContext();
95 }
96
97 @SuppressWarnings("deprecation")
98 // Test is intentionally verifying deprecated behavior.
99 @Test
101 ShimPluginRegistry registryUnderTest = new ShimPluginRegistry(mockFlutterEngine);
103 registryUnderTest.registrarFor("test");
104
105 ArgumentCaptor<FlutterPlugin> shimAggregateCaptor =
106 ArgumentCaptor.forClass(FlutterPlugin.class);
107 verify(mockPluginRegistry).add(shimAggregateCaptor.capture());
108 FlutterPlugin shimAggregateAsPlugin = shimAggregateCaptor.getValue();
109 ActivityAware shimAggregateAsActivityAware = (ActivityAware) shimAggregateCaptor.getValue();
110
111 // Nothing is retrievable when nothing is attached.
112 assertNull(registrarUnderTest.context());
113 assertNull(registrarUnderTest.activity());
114
115 shimAggregateAsPlugin.onAttachedToEngine(mockFlutterPluginBinding);
116
117 assertEquals(mockApplicationContext, registrarUnderTest.context());
118 assertNull(registrarUnderTest.activity());
119
120 shimAggregateAsActivityAware.onAttachedToActivity(mockActivityPluginBinding);
121
122 // Now context is the activity context.
123 assertEquals(mockActivity, registrarUnderTest.activeContext());
124 assertEquals(mockActivity, registrarUnderTest.activity());
125
126 shimAggregateAsActivityAware.onDetachedFromActivityForConfigChanges();
127
128 assertEquals(mockApplicationContext, registrarUnderTest.activeContext());
129 assertNull(registrarUnderTest.activity());
130
131 shimAggregateAsActivityAware.onReattachedToActivityForConfigChanges(mockActivityPluginBinding);
132 assertEquals(mockActivity, registrarUnderTest.activeContext());
133 assertEquals(mockActivity, registrarUnderTest.activity());
134
135 shimAggregateAsActivityAware.onDetachedFromActivity();
136
137 assertEquals(mockApplicationContext, registrarUnderTest.activeContext());
138 assertNull(registrarUnderTest.activity());
139
140 // Attach an activity again.
141 shimAggregateAsActivityAware.onAttachedToActivity(mockActivityPluginBinding);
142
143 assertEquals(mockActivity, registrarUnderTest.activeContext());
144 assertEquals(mockActivity, registrarUnderTest.activity());
145
146 // Now rip out the whole engine.
147 shimAggregateAsPlugin.onDetachedFromEngine(mockFlutterPluginBinding);
148
149 // And everything should have been made unavailable.
150 assertNull(registrarUnderTest.activeContext());
151 assertNull(registrarUnderTest.activity());
152 }
153}
static SkISize times(const SkISize &size, float factor)
void onAttachedToEngine(@NonNull FlutterPluginBinding binding)
void onDetachedFromEngine(@NonNull FlutterPluginBinding binding)