Flutter Engine
The Flutter Engine
Public Member Functions | List of all members
io.flutter.plugin.text.ProcessTextPluginTest Class Reference

Public Member Functions

void respondsToProcessTextChannelMessage ()
 
void performQueryTextActions ()
 
void performProcessTextActionWithNoReturnedValue ()
 
void performProcessTextActionWithReturnedValue ()
 
void doNotCrashOnNonRelatedActivityResult ()
 

Detailed Description

Definition at line 41 of file ProcessTextPluginTest.java.

Member Function Documentation

◆ doNotCrashOnNonRelatedActivityResult()

void io.flutter.plugin.text.ProcessTextPluginTest.doNotCrashOnNonRelatedActivityResult ( )
inline

Definition at line 206 of file ProcessTextPluginTest.java.

206 {
207 DartExecutor mockBinaryMessenger = mock(DartExecutor.class);
208 PackageManager mockPackageManager = mock(PackageManager.class);
209 ProcessTextChannel processTextChannel =
210 new ProcessTextChannel(mockBinaryMessenger, mockPackageManager);
211
212 // Set up mocked result for PackageManager.queryIntentActivities.
213 ResolveInfo action1 = createFakeResolveInfo("Action1", mockPackageManager);
214 ResolveInfo action2 = createFakeResolveInfo("Action2", mockPackageManager);
215 List<ResolveInfo> infos = new ArrayList<ResolveInfo>(Arrays.asList(action1, action2));
216 when(mockPackageManager.queryIntentActivities(
217 any(Intent.class), any(PackageManager.ResolveInfoFlags.class)))
218 .thenReturn(infos);
219
220 // ProcessTextPlugin should retrieve the mocked text actions.
221 ProcessTextPlugin processTextPlugin = new ProcessTextPlugin(processTextChannel);
222 Map<String, String> textActions = processTextPlugin.queryTextActions();
223 final String action1Id = "mockActivityName.Action1";
224 final String action2Id = "mockActivityName.Action2";
225 assertEquals(textActions, Map.of(action1Id, "Action1", action2Id, "Action2"));
226
227 // Set up the activity binding.
228 ActivityPluginBinding mockActivityPluginBinding = mock(ActivityPluginBinding.class);
229 Activity mockActivity = mock(Activity.class);
230 when(mockActivityPluginBinding.getActivity()).thenReturn(mockActivity);
231 processTextPlugin.onAttachedToActivity(mockActivityPluginBinding);
232
233 // Execute the first action.
234 String textToBeProcessed = "Flutter!";
235 MethodChannel.Result result = mock(MethodChannel.Result.class);
236 processTextPlugin.processTextAction(action1Id, textToBeProcessed, false, result);
237
238 // Activity.startActivityForResult should have been called.
239 ArgumentCaptor<Intent> intentCaptor = ArgumentCaptor.forClass(Intent.class);
240 verify(mockActivity, times(1)).startActivityForResult(intentCaptor.capture(), anyInt());
241 Intent intent = intentCaptor.getValue();
242 assertEquals(intent.getStringExtra(Intent.EXTRA_PROCESS_TEXT), textToBeProcessed);
243
244 // Result to a request not sent by this plugin should be ignored.
245 final int externalRequestCode = 42;
246 processTextPlugin.onActivityResult(externalRequestCode, Activity.RESULT_OK, new Intent());
247
248 // Simulate an Android activity answer which returns a transformed text.
249 String processedText = "Flutter!!!";
250 Intent resultIntent = new Intent();
251 resultIntent.putExtra(Intent.EXTRA_PROCESS_TEXT, processedText);
252 processTextPlugin.onActivityResult(result.hashCode(), Activity.RESULT_OK, resultIntent);
253
254 // Success with the transformed text is expected.
255 verify(result).success(processedText);
256 }
static SkISize times(const SkISize &size, float factor)
GAsyncResult * result
SIT bool any(const Vec< 1, T > &x)
Definition: SkVx.h:530

◆ performProcessTextActionWithNoReturnedValue()

void io.flutter.plugin.text.ProcessTextPluginTest.performProcessTextActionWithNoReturnedValue ( )
inline

Definition at line 106 of file ProcessTextPluginTest.java.

106 {
107 DartExecutor mockBinaryMessenger = mock(DartExecutor.class);
108 PackageManager mockPackageManager = mock(PackageManager.class);
109 ProcessTextChannel processTextChannel =
110 new ProcessTextChannel(mockBinaryMessenger, mockPackageManager);
111
112 // Set up mocked result for PackageManager.queryIntentActivities.
113 ResolveInfo action1 = createFakeResolveInfo("Action1", mockPackageManager);
114 ResolveInfo action2 = createFakeResolveInfo("Action2", mockPackageManager);
115 List<ResolveInfo> infos = new ArrayList<ResolveInfo>(Arrays.asList(action1, action2));
116 when(mockPackageManager.queryIntentActivities(
117 any(Intent.class), any(PackageManager.ResolveInfoFlags.class)))
118 .thenReturn(infos);
119
120 // ProcessTextPlugin should retrieve the mocked text actions.
121 ProcessTextPlugin processTextPlugin = new ProcessTextPlugin(processTextChannel);
122 Map<String, String> textActions = processTextPlugin.queryTextActions();
123 final String action1Id = "mockActivityName.Action1";
124 final String action2Id = "mockActivityName.Action2";
125 assertEquals(textActions, Map.of(action1Id, "Action1", action2Id, "Action2"));
126
127 // Set up the activity binding.
128 ActivityPluginBinding mockActivityPluginBinding = mock(ActivityPluginBinding.class);
129 Activity mockActivity = mock(Activity.class);
130 when(mockActivityPluginBinding.getActivity()).thenReturn(mockActivity);
131 processTextPlugin.onAttachedToActivity(mockActivityPluginBinding);
132
133 // Execute th first action.
134 String textToBeProcessed = "Flutter!";
135 MethodChannel.Result result = mock(MethodChannel.Result.class);
136 processTextPlugin.processTextAction(action1Id, textToBeProcessed, false, result);
137
138 // Activity.startActivityForResult should have been called.
139 ArgumentCaptor<Intent> intentCaptor = ArgumentCaptor.forClass(Intent.class);
140 verify(mockActivity, times(1)).startActivityForResult(intentCaptor.capture(), anyInt());
141 Intent intent = intentCaptor.getValue();
142 assertEquals(intent.getStringExtra(Intent.EXTRA_PROCESS_TEXT), textToBeProcessed);
143
144 // Simulate an Android activity answer which does not return a value.
145 Intent resultIntent = new Intent();
146 processTextPlugin.onActivityResult(result.hashCode(), Activity.RESULT_OK, resultIntent);
147
148 // Success with no returned value is expected.
149 verify(result).success(null);
150 }

◆ performProcessTextActionWithReturnedValue()

void io.flutter.plugin.text.ProcessTextPluginTest.performProcessTextActionWithReturnedValue ( )
inline

Definition at line 155 of file ProcessTextPluginTest.java.

155 {
156 DartExecutor mockBinaryMessenger = mock(DartExecutor.class);
157 PackageManager mockPackageManager = mock(PackageManager.class);
158 ProcessTextChannel processTextChannel =
159 new ProcessTextChannel(mockBinaryMessenger, mockPackageManager);
160
161 // Set up mocked result for PackageManager.queryIntentActivities.
162 ResolveInfo action1 = createFakeResolveInfo("Action1", mockPackageManager);
163 ResolveInfo action2 = createFakeResolveInfo("Action2", mockPackageManager);
164 List<ResolveInfo> infos = new ArrayList<ResolveInfo>(Arrays.asList(action1, action2));
165 when(mockPackageManager.queryIntentActivities(
166 any(Intent.class), any(PackageManager.ResolveInfoFlags.class)))
167 .thenReturn(infos);
168
169 // ProcessTextPlugin should retrieve the mocked text actions.
170 ProcessTextPlugin processTextPlugin = new ProcessTextPlugin(processTextChannel);
171 Map<String, String> textActions = processTextPlugin.queryTextActions();
172 final String action1Id = "mockActivityName.Action1";
173 final String action2Id = "mockActivityName.Action2";
174 assertEquals(textActions, Map.of(action1Id, "Action1", action2Id, "Action2"));
175
176 // Set up the activity binding.
177 ActivityPluginBinding mockActivityPluginBinding = mock(ActivityPluginBinding.class);
178 Activity mockActivity = mock(Activity.class);
179 when(mockActivityPluginBinding.getActivity()).thenReturn(mockActivity);
180 processTextPlugin.onAttachedToActivity(mockActivityPluginBinding);
181
182 // Execute the first action.
183 String textToBeProcessed = "Flutter!";
184 MethodChannel.Result result = mock(MethodChannel.Result.class);
185 processTextPlugin.processTextAction(action1Id, textToBeProcessed, false, result);
186
187 // Activity.startActivityForResult should have been called.
188 ArgumentCaptor<Intent> intentCaptor = ArgumentCaptor.forClass(Intent.class);
189 verify(mockActivity, times(1)).startActivityForResult(intentCaptor.capture(), anyInt());
190 Intent intent = intentCaptor.getValue();
191 assertEquals(intent.getStringExtra(Intent.EXTRA_PROCESS_TEXT), textToBeProcessed);
192
193 // Simulate an Android activity answer which returns a transformed text.
194 String processedText = "Flutter!!!";
195 Intent resultIntent = new Intent();
196 resultIntent.putExtra(Intent.EXTRA_PROCESS_TEXT, processedText);
197 processTextPlugin.onActivityResult(result.hashCode(), Activity.RESULT_OK, resultIntent);
198
199 // Success with the transformed text is expected.
200 verify(result).success(processedText);
201 }

◆ performQueryTextActions()

void io.flutter.plugin.text.ProcessTextPluginTest.performQueryTextActions ( )
inline

Definition at line 80 of file ProcessTextPluginTest.java.

80 {
81 DartExecutor mockBinaryMessenger = mock(DartExecutor.class);
82 PackageManager mockPackageManager = mock(PackageManager.class);
83 ProcessTextChannel processTextChannel =
84 new ProcessTextChannel(mockBinaryMessenger, mockPackageManager);
85
86 // Set up mocked result for PackageManager.queryIntentActivities.
87 ResolveInfo action1 = createFakeResolveInfo("Action1", mockPackageManager);
88 ResolveInfo action2 = createFakeResolveInfo("Action2", mockPackageManager);
89 List<ResolveInfo> infos = new ArrayList<ResolveInfo>(Arrays.asList(action1, action2));
90 Intent intent = new Intent().setAction(Intent.ACTION_PROCESS_TEXT).setType("text/plain");
91 when(mockPackageManager.queryIntentActivities(
92 any(Intent.class), any(PackageManager.ResolveInfoFlags.class)))
93 .thenReturn(infos);
94
95 // ProcessTextPlugin should retrieve the mocked text actions.
96 ProcessTextPlugin processTextPlugin = new ProcessTextPlugin(processTextChannel);
97 Map<String, String> textActions = processTextPlugin.queryTextActions();
98 final String action1Id = "mockActivityName.Action1";
99 final String action2Id = "mockActivityName.Action2";
100 assertEquals(textActions, Map.of(action1Id, "Action1", action2Id, "Action2"));
101 }

◆ respondsToProcessTextChannelMessage()

void io.flutter.plugin.text.ProcessTextPluginTest.respondsToProcessTextChannelMessage ( )
inline

Definition at line 54 of file ProcessTextPluginTest.java.

54 {
55 ArgumentCaptor<BinaryMessenger.BinaryMessageHandler> binaryMessageHandlerCaptor =
56 ArgumentCaptor.forClass(BinaryMessenger.BinaryMessageHandler.class);
57 DartExecutor mockBinaryMessenger = mock(DartExecutor.class);
58 ProcessTextChannel.ProcessTextMethodHandler mockHandler =
59 mock(ProcessTextChannel.ProcessTextMethodHandler.class);
60 PackageManager mockPackageManager = mock(PackageManager.class);
61 ProcessTextChannel processTextChannel =
62 new ProcessTextChannel(mockBinaryMessenger, mockPackageManager);
63
64 processTextChannel.setMethodHandler(mockHandler);
65
66 verify(mockBinaryMessenger, times(1))
67 .setMessageHandler(any(String.class), binaryMessageHandlerCaptor.capture());
68
69 BinaryMessenger.BinaryMessageHandler binaryMessageHandler =
70 binaryMessageHandlerCaptor.getValue();
71
72 sendToBinaryMessageHandler(binaryMessageHandler, "ProcessText.queryTextActions", null);
73
74 verify(mockHandler).queryTextActions();
75 }

The documentation for this class was generated from the following file: